This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib.rs
212 lines (185 loc) · 6.89 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::future::Future;
use std::path::PathBuf;
use alloy::primitives::{BlockNumber, U256};
use anyhow::{Context, Result};
use common::fs::generate_block_proof_file_name;
use futures::{future::BoxFuture, stream::FuturesOrdered, FutureExt, TryFutureExt, TryStreamExt};
use num_traits::ToPrimitive as _;
use ops::TxProof;
use paladin::{
directive::{Directive, IndexedStream},
runtime::Runtime,
};
use proof_gen::proof_types::GeneratedBlockProof;
use serde::{Deserialize, Serialize};
use tokio::io::AsyncWriteExt;
use tokio::sync::oneshot;
use trace_decoder::{
processed_block_trace::ProcessingMeta,
trace_protocol::BlockTrace,
types::{CodeHash, OtherBlockData},
};
use tracing::info;
#[derive(Debug, Deserialize, Serialize)]
pub struct BlockProverInput {
pub block_trace: BlockTrace,
pub other_data: OtherBlockData,
}
fn resolve_code_hash_fn(_: &CodeHash) -> Vec<u8> {
todo!()
}
impl BlockProverInput {
pub fn get_block_number(&self) -> U256 {
self.other_data.b_data.b_meta.block_number.into()
}
#[cfg(not(feature = "test_only"))]
pub async fn prove(
self,
runtime: &Runtime,
previous: Option<impl Future<Output = Result<GeneratedBlockProof>>>,
save_inputs_on_error: bool,
) -> Result<GeneratedBlockProof> {
use anyhow::Context as _;
let block_number = self.get_block_number();
let other_data = self.other_data;
let txs = self.block_trace.into_txn_proof_gen_ir(
&ProcessingMeta::new(resolve_code_hash_fn),
other_data.clone(),
)?;
let agg_proof = IndexedStream::from(txs)
.map(&TxProof {
save_inputs_on_error,
})
.fold(&ops::AggProof {
save_inputs_on_error,
})
.run(runtime)
.await?;
if let proof_gen::proof_types::AggregatableProof::Agg(proof) = agg_proof {
let block_number = block_number
.to_u64()
.context("block number overflows u64")?;
let prev = match previous {
Some(it) => Some(it.await?),
None => None,
};
let block_proof = paladin::directive::Literal(proof)
.map(&ops::BlockProof {
prev,
save_inputs_on_error,
})
.run(runtime)
.await?;
info!("Successfully proved block {block_number}");
Ok(block_proof.0)
} else {
anyhow::bail!("AggProof is is not GeneratedAggProof")
}
}
#[cfg(feature = "test_only")]
pub async fn prove(
self,
runtime: &Runtime,
_previous: Option<impl Future<Output = Result<GeneratedBlockProof>>>,
save_inputs_on_error: bool,
) -> Result<GeneratedBlockProof> {
let block_number = self.get_block_number();
info!("Testing witness generation for block {block_number}.");
let other_data = self.other_data;
let txs = self.block_trace.into_txn_proof_gen_ir(
&ProcessingMeta::new(resolve_code_hash_fn),
other_data.clone(),
)?;
IndexedStream::from(txs)
.map(&TxProof {
save_inputs_on_error,
})
.run(runtime)
.await?
.try_collect::<Vec<_>>()
.await?;
// Dummy proof to match expected output type.
Ok(GeneratedBlockProof {
b_height: block_number
.to_u64()
.expect("Block number should fit in a u64"),
intern: proof_gen::proof_gen::dummy_proof()?,
})
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct ProverInput {
pub blocks: Vec<BlockProverInput>,
}
impl ProverInput {
/// Prove all the blocks in the input.
/// Return the list of block numbers that are proved and if the proof data
/// is not saved to disk, return the generated block proofs as well.
pub async fn prove(
self,
runtime: &Runtime,
previous_proof: Option<GeneratedBlockProof>,
save_inputs_on_error: bool,
proof_output_dir: Option<PathBuf>,
) -> Result<Vec<(BlockNumber, Option<GeneratedBlockProof>)>> {
let mut prev: Option<BoxFuture<Result<GeneratedBlockProof>>> =
previous_proof.map(|proof| Box::pin(futures::future::ok(proof)) as BoxFuture<_>);
let results: FuturesOrdered<_> = self
.blocks
.into_iter()
.map(|block| {
let block_number = block.get_block_number();
info!("Proving block {block_number}");
let (tx, rx) = oneshot::channel::<GeneratedBlockProof>();
// Prove the block
let proof_output_dir = proof_output_dir.clone();
let fut = block
.prove(runtime, prev.take(), save_inputs_on_error)
.then(move |proof| async move {
let proof = proof?;
let block_number = proof.b_height;
// Write latest generated proof to disk if proof_output_dir is provided
let return_proof: Option<GeneratedBlockProof> =
if proof_output_dir.is_some() {
ProverInput::write_proof(proof_output_dir, &proof).await?;
None
} else {
Some(proof.clone())
};
if tx.send(proof).is_err() {
anyhow::bail!("Failed to send proof");
}
Ok((block_number, return_proof))
})
.boxed();
prev = Some(Box::pin(rx.map_err(anyhow::Error::new)));
fut
})
.collect();
results.try_collect().await
}
/// Write the proof to the disk (if `output_dir` is provided) or stdout.
pub(crate) async fn write_proof(
output_dir: Option<PathBuf>,
proof: &GeneratedBlockProof,
) -> Result<()> {
let proof_serialized = serde_json::to_vec(proof)?;
let block_proof_file_path =
output_dir.map(|path| generate_block_proof_file_name(&path.to_str(), proof.b_height));
match block_proof_file_path {
Some(p) => {
if let Some(parent) = p.parent() {
tokio::fs::create_dir_all(parent).await?;
}
let mut f = tokio::fs::File::create(p).await?;
f.write_all(&proof_serialized)
.await
.context("Failed to write proof to disk")
}
None => tokio::io::stdout()
.write_all(&proof_serialized)
.await
.context("Failed to write proof to stdout"),
}
}
}