Skip to content

Commit db604b5

Browse files
committed
avs v0.2.3: support scroll curie v5.5.0
1 parent c3ba1a1 commit db604b5

22 files changed

+6447
-155
lines changed

Cargo.lock

+243-48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/sgx/Cargo.lock

+238-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/apps/prover/src/api.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct PublicApi {
2727
pub prover: Arc<Prover>,
2828
pub verifier: Option<Arc<verifier::Client>>,
2929
pub l2_el: Option<Arc<ExecutionClient>>,
30+
pub l2_chain_id: u64,
3031
pub l1_el: Option<Arc<L1ExecutionClient>>,
3132
// pub relay: Option<Secp256k1PrivateKey>,
3233
pub insecure: bool,
@@ -194,8 +195,14 @@ impl PublicApi {
194195
Some(poe) => poe,
195196
None => {
196197
let start = Time::now();
197-
let result =
198-
App::generate_poe_by_pob(&self.alive, &self.prover, &batch, pob_list, 4);
198+
let result = App::generate_poe_by_pob(
199+
&self.alive,
200+
self.l2_chain_id,
201+
&self.prover,
202+
&batch,
203+
pob_list,
204+
4,
205+
);
199206
self.pobda_task_mgr.update_task(key, result.clone());
200207
self.metrics
201208
.gauge_prove_ms
@@ -289,15 +296,24 @@ impl PublicApi {
289296
let poe = match self.task_mgr.process_task(batch_task.clone()) {
290297
Some(poe) => poe,
291298
None => {
292-
let result =
293-
App::generate_poe(&self.alive, l2_el, &self.prover, batch_task.clone());
299+
let result = App::generate_poe(
300+
&self.alive,
301+
self.l2_chain_id,
302+
l2_el,
303+
&self.prover,
304+
batch_task.clone(),
305+
);
294306
self.task_mgr
295307
.update_task(batch_task.clone(), result.clone());
296308
result
297309
}
298310
}
299311
.map_err(JsonrpcErrorObj::unknown)?;
300312

313+
if poe.batch_hash != batch_hash {
314+
return Err(JsonrpcErrorObj::client("batch hash mismatch, skip".into()));
315+
}
316+
301317
Ok(PoeResponse {
302318
not_ready: false,
303319
start_block,
@@ -467,6 +483,7 @@ impl Getter<RpcServer<PublicApi>> for App {
467483
let api = Arc::new(PublicApi {
468484
alive: self.alive.clone(),
469485
l2_el: self.l2_el.option_get(self),
486+
l2_chain_id: self.l2_chain_id.get(self).0,
470487
l1_el: self.l1_el.option_get(self),
471488
prover: self.prover.get(self),
472489
verifier: self.verifier.option_get(self),

src/apps/prover/src/app.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use eth_types::SH256;
99
use jsonrpc::{MixRpcClient, RpcServer};
1010
use prometheus::CollectorRegistry;
1111
use prover::{Database, Pob, Prover};
12-
use scroll_types::{BatchChunkBlockTx, BatchChunkBuilder, BatchTask, Poe, TransactionInner};
12+
use scroll_types::{BatchChunkBlockTx, BatchChunkBuilder, BatchTask, Poe, TransactionInner, get_fork};
1313
use std::collections::btree_map::Entry;
1414
use std::collections::BTreeMap;
1515
use std::sync::{Arc, Mutex};
@@ -114,6 +114,7 @@ impl App {
114114

115115
pub fn generate_poe_by_pob(
116116
alive: &Alive,
117+
chain_id: u64,
117118
prover: &Arc<Prover>,
118119
batch: &BatchTask,
119120
pob_list: Arc<Vec<Pob>>,
@@ -162,8 +163,11 @@ impl App {
162163

163164
let reports = Arc::try_unwrap(ctx).unwrap();
164165
let reports = reports.into_inner().unwrap();
166+
let version = get_fork(chain_id, block_numbers[0]);
165167

166-
let batch_header = batch.build_header(&batch_chunk.chunks).map_err(debug)?;
168+
let batch_header = batch
169+
.build_header(version, &batch_chunk.chunks)
170+
.map_err(debug)?;
167171
let poe = prover
168172
.merge_poe(batch_header.hash(), &reports)
169173
.ok_or(format!("fail to gen poe"))?;
@@ -174,6 +178,7 @@ impl App {
174178

175179
pub fn generate_poe(
176180
alive: &Alive,
181+
chain_id: u64,
177182
l2: &Arc<ExecutionClient>,
178183
prover: &Arc<Prover>,
179184
task: BatchTask,
@@ -247,8 +252,11 @@ impl App {
247252

248253
let ctx = ctx.lock().unwrap();
249254
let reports = &ctx.0;
255+
let fork = get_fork(chain_id, block_numbers[0]);
250256

251-
let batch_header = task.build_header(&batch_chunk.chunks).map_err(debug)?;
257+
let batch_header = task
258+
.build_header(fork, &batch_chunk.chunks)
259+
.map_err(debug)?;
252260

253261
let poe = prover
254262
.merge_poe(batch_header.hash(), &reports)
@@ -260,13 +268,14 @@ impl App {
260268
pub fn commit_batch(
261269
alive: &Alive,
262270
l2: &Arc<ExecutionClient>,
271+
chain_id: u64,
263272
prover: &Arc<Prover>,
264273
relay_acc: &Secp256k1PrivateKey,
265274
verifier: &verifier::Client,
266275
task: BatchTask,
267276
) -> Result<SH256, String> {
268277
let batch_id = task.id().into();
269-
let poe = Self::generate_poe(alive, l2, prover, task)?;
278+
let poe = Self::generate_poe(alive, chain_id, l2, prover, task)?;
270279
verifier.commit_batch(relay_acc, &batch_id, &poe.encode())
271280
}
272281

@@ -415,7 +424,7 @@ impl OptionGetter<ExecutionClient> for App {
415424
}
416425
}
417426

418-
pub struct L2ChainID(u64);
427+
pub struct L2ChainID(pub u64);
419428

420429
impl Getter<L2ChainID> for App {
421430
fn generate(&self) -> L2ChainID {

src/common/evm_executor/Cargo.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ resolver = "2"
1010
[features]
1111
default = ["std"]
1212

13-
std = ["num-bigint/std", "evm/std", "glog/std", "crypto/std", "eth_types/std", "statedb/std", "base/std", "eth_client/std", "scroll_types/std"]
14-
tstd = ["sgxlib/tstd", "num-bigint/tstd", "evm/tstd", "glog/tstd", "crypto/tstd", "eth_types/tstd", "statedb/tstd", "base/tstd", "eth_client/tstd", "scroll_types/tstd"]
13+
std = ["num-bigint/std", "evm/std", "glog/std", "crypto/std", "eth_types/std", "statedb/std", "base/std", "eth_client/std", "scroll_types/std", "serde/std", "serde_json/std"]
14+
tstd = ["sgxlib/tstd", "num-bigint/tstd", "evm/tstd", "glog/tstd", "crypto/tstd", "eth_types/tstd", "statedb/tstd", "base/tstd", "eth_client/tstd", "scroll_types/tstd", "serde/tstd", "serde_json/tstd"]
1515

1616
[dependencies]
1717
sgxlib = { git = "https://github.com/automata-network/sgxlib", default-features = false }
@@ -25,7 +25,12 @@ base = { git = "https://github.com/automata-network/base-rs", default-features =
2525
scale-info-derive = "=2.10"
2626

2727
evm = { git = "https://github.com/automata-network/evm-rs", default-features = false }
28+
serde = { git = "https://github.com/automata-network/sgxlib-thirdparty", default-features = false }
29+
serde_json = { git = "https://github.com/automata-network/sgxlib-thirdparty", default-features = false }
2830
num-bigint = { git = "https://github.com/automata-network/sgxlib-thirdparty", default-features = false }
31+
p256 = { version = "0.11.1", default-features = false, features = [
32+
"ecdsa",
33+
] }
2934
# revm = { path = "../../revm/crates/revm", default-features = false }
3035

3136
bn = { package = "substrate-bn", version = "0.6", default-features = false }

src/common/evm_executor/src/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ pub struct Context<'a> {
1212
pub tx: &'a PoolTx,
1313
pub header: &'a BlockHeader,
1414
pub extra_fee: Option<SU256>,
15+
pub burn_base_fee: bool,
1516
}

src/common/evm_executor/src/executor.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::prelude::v1::*;
22

33
use super::{Context, StateProxy};
44
use base::format::parse_ether;
5+
use base::thread::PanicContext;
56
use eth_types::{HexBytes, Log, Receipt, SH256, SU256, SU64};
67
use statedb::StateDB;
78

@@ -42,6 +43,8 @@ impl<'a, D: StateDB> Executor<'a, D> {
4243
state_db: &'a mut D,
4344
tx_idx: u64,
4445
) -> Result<Receipt, ExecuteError> {
46+
let _panic_ctx = PanicContext(ctx.tx.tx.hash());
47+
4548
let mut result = Executor::new(ctx.clone(), state_db).run(false)?;
4649
for log in &mut result.logs {
4750
log.transaction_hash = ctx.tx.hash;
@@ -72,11 +75,14 @@ impl<'a, D: StateDB> Executor<'a, D> {
7275
let config = self.ctx.cfg;
7376
let tx = &self.ctx.tx.tx;
7477
let mut base_fee = self.ctx.header.base_fee_per_gas;
75-
let gas_fee_cap = tx.max_fee_per_gas();
78+
let mut gas_fee_cap = tx.max_fee_per_gas();
7679

7780
if tx.can_check_nonce() {
7881
self.check_nonce(true, dry_run)?;
7982
}
83+
if !tx.cost_gas() {
84+
base_fee = None;
85+
}
8086

8187
if let Some(fee) = &base_fee {
8288
if fee > gas_fee_cap {
@@ -257,9 +263,11 @@ impl<'a, D: StateDB> Executor<'a, D> {
257263
}
258264
}
259265

260-
let gas_tip_cap = tx.max_priority_fee_per_gas();
261-
let gas_fee_cap = tx.max_fee_per_gas();
266+
let mut gas_tip_cap = tx.max_priority_fee_per_gas();
262267
if let Some(base_fee) = &base_fee {
268+
if !tx.cost_gas() {
269+
gas_tip_cap = base_fee;
270+
}
263271
if base_fee > gas_fee_cap {
264272
glog::info!(
265273
"invalid tx: {:?}, base_fee:{:?}, gas_fee_cap:{:?}",
@@ -269,7 +277,14 @@ impl<'a, D: StateDB> Executor<'a, D> {
269277
)
270278
}
271279
}
272-
let effective_tip = (*gas_tip_cap).min(*gas_fee_cap - base_fee.unwrap_or(SU256::zero()));
280+
281+
let mut effective_tip =
282+
(*gas_tip_cap).min(*gas_fee_cap - base_fee.unwrap_or(SU256::zero()));
283+
if !self.ctx.burn_base_fee {
284+
if let Some(base_fee) = base_fee {
285+
effective_tip += base_fee;
286+
}
287+
}
273288
let miner = if dry_run {
274289
eth_types::zero_addr()
275290
} else {

src/common/evm_executor/src/fee.rs

+45-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
use std::prelude::v1::*;
22

33
use eth_types::{SH160, SH256, SU256};
4-
use scroll_types::TransactionInner;
4+
use scroll_types::{ScrollFork, TransactionInner};
55
use statedb::StateDB;
66

77
lazy_static::lazy_static! {
88
static ref SLOT_L1_BASE_FEE: SH256 = SU256::from(1).into();
99
static ref SLOT_OVERHEAD: SH256 = SU256::from(2).into();
1010
static ref SLOT_SCALAR: SH256 = SU256::from(3).into();
11+
12+
// curie
13+
static ref SLOT_L1_BLOB_BASE_FEE: SH256 = SU256::from(5).into();
14+
static ref SLOT_COMMIT_SCALAR: SH256 = SU256::from(6).into();
15+
static ref SLOT_BLOB_SCALAR_SLOT: SH256 = SU256::from(7).into();
16+
static ref SLOT_IS_CURIE: SH256 = SU256::from(8).into();
17+
18+
static ref INITIAL_COMMIT_SCALAR: SU256 = "230759955285".into();
19+
static ref INITIAL_BLOB_SCALAR: SU256 = "417565260".into();
20+
1121
static ref GAS_FEE_PRECISION: SU256 = SU256::from(1_000_000_000u64);
1222
static ref L1_GAS_PRICE_ORACLE_ADDRESS: SH160 = "0x5300000000000000000000000000000000000002".into();
1323
static ref FEE_VAULT_ADDRESS: SH160 = "0x5300000000000000000000000000000000000005".into();
@@ -23,6 +33,7 @@ where
2333
}
2434

2535
pub fn calculate_l1_data_fee<S>(
36+
fork: ScrollFork,
2637
cfg: &evm::Config,
2738
tx: &TransactionInner,
2839
state: &mut S,
@@ -35,14 +46,20 @@ where
3546
}
3647
let raw = tx.to_bytes();
3748
let l1_fee = read_gpo_storage_slots(&L1_GAS_PRICE_ORACLE_ADDRESS, state)?;
38-
let data_fee = l1_fee.data_fee(cfg, &raw);
49+
let data_fee = l1_fee.data_fee(fork, cfg, &raw);
3950
Ok(data_fee)
4051
}
4152

53+
#[derive(Debug)]
4254
pub struct L1GasFee {
4355
pub l1_base_fee: SU256,
4456
pub overhead: SU256,
4557
pub scalar: SU256,
58+
59+
// curie
60+
pub l1_blob_base_fee: SU256,
61+
pub commit_scalar: SU256,
62+
pub blob_scalar: SU256,
4663
}
4764

4865
impl L1GasFee {
@@ -72,11 +89,28 @@ impl L1GasFee {
7289
SU256::from(l1_gas) + self.overhead
7390
}
7491

75-
pub fn data_fee(&self, cfg: &evm::Config, data: &[u8]) -> SU256 {
92+
pub fn data_fee(&self, fork: ScrollFork, cfg: &evm::Config, data: &[u8]) -> SU256 {
93+
match fork {
94+
ScrollFork::Bernoulli => self.data_fee_bernoulli(cfg, data),
95+
ScrollFork::Curie => self.data_fee_curie(data),
96+
}
97+
}
98+
99+
pub fn data_fee_bernoulli(&self, cfg: &evm::Config, data: &[u8]) -> SU256 {
76100
let l1_gas_used = self.gas_used(cfg, data);
77101
let l1_data_fee = l1_gas_used * self.l1_base_fee;
78102
l1_data_fee * self.scalar / *GAS_FEE_PRECISION
79103
}
104+
105+
pub fn data_fee_curie(&self, data: &[u8]) -> SU256 {
106+
let calldata_gas = self.commit_scalar * self.l1_base_fee;
107+
108+
// blob component of commit fees
109+
let blob_gas: SU256 = self.l1_blob_base_fee * self.blob_scalar * SU256::from(data.len() as u64);
110+
111+
// combined
112+
(calldata_gas + blob_gas) / *GAS_FEE_PRECISION
113+
}
80114
}
81115

82116
fn read_gpo_storage_slots<S>(addr: &SH160, state: &mut S) -> Result<L1GasFee, statedb::Error>
@@ -86,10 +120,18 @@ where
86120
let l1_base_fee = state.get_state(addr, &SLOT_L1_BASE_FEE)?;
87121
let overhead = state.get_state(addr, &SLOT_OVERHEAD)?;
88122
let scalar = state.get_state(addr, &SLOT_SCALAR)?;
123+
124+
let l1_blob_base_fee = state.get_state(addr, &SLOT_L1_BLOB_BASE_FEE)?;
125+
let commit_scalar = state.get_state(addr, &SLOT_COMMIT_SCALAR)?;
126+
let blob_scalar = state.get_state(addr, &SLOT_BLOB_SCALAR_SLOT)?;
127+
89128
let fee = L1GasFee {
90129
l1_base_fee: (&l1_base_fee).into(),
91130
overhead: (&overhead).into(),
92131
scalar: (&scalar).into(),
132+
l1_blob_base_fee: (&l1_blob_base_fee).into(),
133+
commit_scalar: (&commit_scalar).into(),
134+
blob_scalar: (&blob_scalar).into(),
93135
};
94136
Ok(fee)
95137
}

0 commit comments

Comments
 (0)