Skip to content

Commit 21a4ec8

Browse files
authored
[entropy] Update readmes and docs (#1163)
* add some docs * more doc edits
1 parent 0a2378e commit 21a4ec8

File tree

4 files changed

+62
-23
lines changed

4 files changed

+62
-23
lines changed

fortuna/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Fortuna
2+
3+
Fortuna is a webservice that serves random numbers according to the Entropy protocol.
4+
The webservice generates a hash chain of random numbers and reveals them to callers when permitted by the protocol.
5+
The hash chain is generated from a secret key that is provided to the server on startup.
6+
7+
A single instance of this webservice can simultaneously serve random numbers for several different blockchains.
8+
Each blockchain is configured in `config.yaml`.
9+
10+
## Build & Test
11+
12+
Fortuna uses Cargo for building and dependency management.
13+
Simply run `cargo build` and `cargo test` to build and test the project.
14+
15+
## Command-Line Interface
16+
17+
The Fortuna binary has a command-line interface to perform useful operations on the contract, such as
18+
registering a new randomness provider, or drawing a random value. To see the available commands, simply run `cargo run`.
19+
20+
## Local Development
21+
22+
To start an instance of the webserver for local testing, you first need to perform a few setup steps:
23+
24+
1. Edit `config.yaml` to point to the desired blockchains and Entropy contracts.
25+
1. Generate a secret key. The secret key is a 32-byte random value used to construct the hash chains.
26+
You can generate this value using the `openssl` command:
27+
`openssl rand -hex 32`
28+
1. Generate an ethereum wallet for the provider. You can do this in foundry using `cast wallet new`.
29+
Note both the private key and the address; you will need both for subsequent steps.
30+
1. Register a randomness provider for this service: `cargo run -- register-provider --chain-id <chain id> --secret <secret> --private-key <private-key>`.
31+
The chain id is the key of the blockchain in `config.yaml`, the secret is from step (2), and the private key is from step (3).
32+
Note that you need to run this command once per blockchain configured in `config.yaml`.
33+
34+
Once you've completed the setup, simply run the following command, using the secret from step (2) and the wallet address from step (3) as the provider:
35+
36+
```bash
37+
cargo run -- run --secret <secret> --provider <provider>
38+
```
39+
40+
This command will start the webservice on `localhost:34000`.

fortuna/src/config/register_provider.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub struct RegisterProviderOptions {
2626
/// This key is required to submit transactions (such as registering with the contract).
2727
#[arg(long = "private-key")]
2828
#[arg(env = "PRIVATE_KEY")]
29-
#[arg(default_value = None)]
3029
pub private_key: String,
3130

3231
#[command(flatten)]

target_chains/ethereum/contracts/README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,29 @@ This directory contains The Pyth contract on Ethereum and utilities to deploy it
44

55
## Installation
66

7-
Run the following command on the repo root to install required dependencies for the contract:
7+
The contracts are built and tested using Foundry. Follow the [Foundry installation instructions](https://book.getfoundry.sh/getting-started/installation) to install it if you do not already have it.
8+
9+
Next, run the following command from the repo root to install required dependencies for the contract:
810

911
```
1012
npm ci
1113
npx lerna run build --scope="@pythnetwork/pyth-evm-contract" --include-dependencies
1214
```
1315

14-
## Foundry
15-
16-
Foundry can be installed by the official installer, or by running our helper script which will automatically pull the correct installation script individually for Foundry and the Solidity compiler for your current OS. This may work better if you are running into networking/firewall issues using Foundry's Solidity installer. To use helper script, run the command below from this directory:
17-
18-
```sh
19-
pyth-crosschain/target_chains/ethereum/contracts $ bash ../../../scripts/install-foundry.sh
20-
```
21-
22-
You need to install npm dependencies as described in [Installation](#installation). Also, you need to run the following
23-
command in the `contracts` directory to install forge dependencies:
16+
Next, from the `contracts` directory, run the following command to install forge dependencies:
2417

2518
```
2619
npm run install-forge-deps
2720
```
2821

29-
After installing the dependencies. Run `forge build` to build the contracts and `forge test` to
30-
test the contracts using tests in `forge-test` directory. To see line by line test coverage:
22+
## Testing
23+
24+
Run `forge build` to build the contracts and `forge test` to run the contract unit tests.
25+
The unit tests live in the `forge-test` directory.
26+
27+
### Code Coverage
28+
29+
To see line-by-line test coverage:
3130

3231
```
3332
npm run coverage
@@ -57,9 +56,9 @@ cp .env.test .env && npx truffle compile --all && npx truffle migrate
5756
npm run test-contract
5857
```
5958

60-
### Gas Benchmark
59+
### Gas Benchmarks
6160

62-
You can use foundry to run benchmark tests written in [`forge-test/GasBenchmark.t.sol`](./forge-test/GasBenchmark.t.sol). To run the tests with gas report
61+
You can use foundry to run gas benchmark tests (which can be found in the `forge-test` directory). To run the tests with gas report
6362
you can run `forge test --gas-report --match-contract GasBenchmark`. However, as there are multiple benchmarks, this might not be useful. You can run a
6463
specific benchmark test by passing the test name using `--match-test`. A full command to run `testBenchmarkUpdatePriceFeedsFresh` benchmark test is like this:
6564

target_chains/ethereum/contracts/contracts/entropy/Entropy.sol

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ import "./EntropyState.sol";
3838
// of the provider's random numbers the user will receive.
3939
// 3. The user submits an off-chain request (e.g. via HTTP) to the provider to reveal the i'th random number.
4040
// 4. The provider checks the on-chain sequence number and ensures it is > i. If it is not, the provider
41-
// refuses to reveal the ith random number.
41+
// refuses to reveal the ith random number. The provider should wait for a sufficient number of block confirmations
42+
// to ensure that the request does not get re-orged out of the blockchain.
4243
// 5. The provider reveals x_i to the user.
4344
// 6. The user submits both the provider's revealed number x_i and their own x_U to the contract.
4445
// 7. The contract verifies hash(x_i) == x_{i-1} to prove that x_i is the i'th random number. The contract also checks that hash(x_U) == h_U.
4546
// The contract stores x_i as the i'th random number to reuse for future verifications.
4647
// 8. If both of the above conditions are satisfied, the random number r = hash(x_i, x_U).
47-
// (Optional) as an added security mechanism, this step can further incorporate the blockhash of the request transaction,
48-
// r = hash(x_i, x_U, blockhash).
48+
// (Optional) as an added security mechanism, this step can further incorporate the blockhash of the block that the
49+
// request transaction landed in: r = hash(x_i, x_U, blockhash).
4950
//
5051
// This protocol has the same security properties as the 2-party randomness protocol above: as long as either
5152
// the provider or user is honest, the number r is random. Honesty here means that the participant keeps their
@@ -63,12 +64,12 @@ import "./EntropyState.sol";
6364
// random number. Verification therefore may require computing multiple hashes (~ the number of concurrent requests).
6465
// Second, the implementation allows providers to rotate their commitment at any time. This operation allows
6566
// providers to commit to additional random numbers once they reach the end of their initial sequence, or rotate out
66-
// a compromised sequence. On rotation, any in-flight requests are continue to use the pre-rotation commitment.
67-
// Each commitment has a metadata field that providers can use to determine which commitment a request is for.
68-
// Providers *must* retrieve the metadata for a request from the blockchain itself to prevent user manipulation of this field.
67+
// a compromised sequence. On rotation, any in-flight requests continue to use the pre-rotation commitment.
68+
// Providers can use the sequence number of the request along with the event log of their registrations to determine
69+
// which hash chain contains the requested random number.
6970
//
7071
// Warning to integrators:
71-
// An important caveat for users of this protocol is that the user can compute the random number r before
72+
// An important caveat of this protocol is that the user can compute the random number r before
7273
// revealing their own number to the contract. This property means that the user can choose to halt the
7374
// protocol prior to the random number being revealed (i.e., prior to step (6) above). Integrators should ensure that
7475
// the user is always incentivized to reveal their random number, and that the protocol has an escape hatch for

0 commit comments

Comments
 (0)