|
8 | 8 | chain::ethereum::PythContract,
|
9 | 9 | command::register_provider::CommitmentMetadata,
|
10 | 10 | config::{
|
| 11 | + Commitment, |
11 | 12 | Config,
|
| 13 | + ProviderConfig, |
12 | 14 | RunOptions,
|
13 | 15 | },
|
14 | 16 | keeper,
|
|
27 | 29 | collections::HashMap,
|
28 | 30 | net::SocketAddr,
|
29 | 31 | sync::Arc,
|
30 |
| - vec, |
31 | 32 | },
|
32 | 33 | tokio::{
|
33 | 34 | spawn,
|
@@ -121,38 +122,62 @@ pub async fn run_keeper(
|
121 | 122 |
|
122 | 123 | pub async fn run(opts: &RunOptions) -> Result<()> {
|
123 | 124 | let config = Config::load(&opts.config.config)?;
|
| 125 | + let provider_config = opts |
| 126 | + .provider_config |
| 127 | + .provider_config |
| 128 | + .as_ref() |
| 129 | + .map(|path| ProviderConfig::load(&path).expect("Failed to load provider config")); |
124 | 130 | let private_key = opts.load_private_key()?;
|
125 | 131 | let secret = opts.randomness.load_secret()?;
|
126 | 132 | let (tx_exit, rx_exit) = watch::channel(false);
|
127 | 133 |
|
128 | 134 | let mut chains: HashMap<ChainId, BlockchainState> = HashMap::new();
|
129 | 135 | for (chain_id, chain_config) in &config.chains {
|
130 | 136 | let contract = Arc::new(PythContract::from_config(&chain_config)?);
|
| 137 | + let provider_chain_config = provider_config |
| 138 | + .as_ref() |
| 139 | + .and_then(|c| c.get_chain_config(chain_id)); |
| 140 | + let mut provider_commitments = provider_chain_config |
| 141 | + .as_ref() |
| 142 | + .map(|c| c.get_sorted_commitments()) |
| 143 | + .unwrap_or_else(|| Vec::new()); |
| 144 | + println!("{} {:?}", chain_id, provider_commitments); |
| 145 | + |
131 | 146 | let provider_info = contract.get_provider_info(opts.provider).call().await?;
|
| 147 | + let latest_metadata = |
| 148 | + bincode::deserialize::<CommitmentMetadata>(&provider_info.commitment_metadata)?; |
| 149 | + |
| 150 | + provider_commitments.push(Commitment { |
| 151 | + seed: latest_metadata.seed, |
| 152 | + chain_length: latest_metadata.chain_length, |
| 153 | + original_commitment_sequence_number: provider_info.original_commitment_sequence_number, |
| 154 | + }); |
132 | 155 |
|
133 |
| - // Reconstruct the hash chain based on the metadata and check that it matches the on-chain commitment. |
134 |
| - // TODO: we should instantiate the state here with multiple hash chains. |
135 |
| - // This approach works fine as long as we haven't rotated the commitment (i.e., all user requests |
136 |
| - // are for the most recent chain). |
137 | 156 | // TODO: we may want to load the hash chain in a lazy/fault-tolerant way. If there are many blockchains,
|
138 | 157 | // then it's more likely that some RPC fails. We should tolerate these faults and generate the hash chain
|
139 | 158 | // later when a user request comes in for that chain.
|
140 |
| - let metadata = |
141 |
| - bincode::deserialize::<CommitmentMetadata>(&provider_info.commitment_metadata)?; |
142 | 159 |
|
143 |
| - let hash_chain = PebbleHashChain::from_config( |
144 |
| - &secret, |
145 |
| - &chain_id, |
146 |
| - &opts.provider, |
147 |
| - &chain_config.contract_addr, |
148 |
| - &metadata.seed, |
149 |
| - metadata.chain_length, |
150 |
| - )?; |
| 160 | + let mut offsets = Vec::<usize>::new(); |
| 161 | + let mut hash_chains = Vec::<PebbleHashChain>::new(); |
| 162 | + |
| 163 | + for commitment in &provider_commitments { |
| 164 | + let offset = commitment.original_commitment_sequence_number.try_into()?; |
| 165 | + offsets.push(offset); |
| 166 | + |
| 167 | + let pebble_hash_chain = PebbleHashChain::from_config( |
| 168 | + &secret, |
| 169 | + &chain_id, |
| 170 | + &opts.provider, |
| 171 | + &chain_config.contract_addr, |
| 172 | + &commitment.seed, |
| 173 | + commitment.chain_length, |
| 174 | + )?; |
| 175 | + hash_chains.push(pebble_hash_chain); |
| 176 | + } |
| 177 | + |
151 | 178 | let chain_state = HashChainState {
|
152 |
| - offsets: vec![provider_info |
153 |
| - .original_commitment_sequence_number |
154 |
| - .try_into()?], |
155 |
| - hash_chains: vec![hash_chain], |
| 179 | + offsets, |
| 180 | + hash_chains, |
156 | 181 | };
|
157 | 182 |
|
158 | 183 | if chain_state.reveal(provider_info.original_commitment_sequence_number)?
|
|
0 commit comments