From 77e06c3f727224a56c23a407600343354c0fc8bd Mon Sep 17 00:00:00 2001 From: Maico Date: Thu, 9 Nov 2023 14:28:29 -0300 Subject: [PATCH] feat: adding protocol parameters values --- src/bin/dolos/bootstrap.rs | 2 +- src/storage/applydb/mod.rs | 52 ++++++++++++++++++++++++++++++++++---- src/sync/ledger.rs | 33 +++--------------------- 3 files changed, 52 insertions(+), 35 deletions(-) diff --git a/src/bin/dolos/bootstrap.rs b/src/bin/dolos/bootstrap.rs index ef028808..a515b77e 100644 --- a/src/bin/dolos/bootstrap.rs +++ b/src/bin/dolos/bootstrap.rs @@ -179,7 +179,7 @@ pub fn run(config: &super::Config, args: &Args) -> miette::Result<()> { .context("adding chain entry")?; ledger - .apply_block(&block, None) + .apply_block(&block, &0) .into_diagnostic() .context("applyting ledger block")?; } diff --git a/src/storage/applydb/mod.rs b/src/storage/applydb/mod.rs index a122b129..15ed1a06 100644 --- a/src/storage/applydb/mod.rs +++ b/src/storage/applydb/mod.rs @@ -2,7 +2,7 @@ pub mod genesis; use pallas::{ applying::{ - types::{Environment, UTxOs}, + types::{ByronProtParams, Environment, MultiEraProtParams, UTxOs}, validate, }, codec::utils::CborWrap, @@ -299,7 +299,7 @@ impl ApplyDB { Ok(dbval.map(|x| x.0)) } - pub fn apply_block(&mut self, cbor: &[u8], env: Option<&Environment>) -> Result<(), Error> { + pub fn apply_block(&mut self, cbor: &[u8], prot_magic: &u32) -> Result<(), Error> { let block = MultiEraBlock::decode(cbor).map_err(|_| Error::Cbor)?; let slot = block.slot(); let hash = block.hash(); @@ -333,11 +333,12 @@ impl ApplyDB { } for tx in txs.iter() { - if let (MultiEraTx::Byron(_), Some(e)) = (&tx, env) { + if tx.era() == Era::Byron { match self.get_inputs(tx, &batch) { Ok(inputs) => { let utxos: UTxOs = Self::mk_utxo(&inputs); - match validate(tx, &utxos, e) { + let env: Environment = Self::mk_environment(&block, prot_magic)?; + match validate(tx, &utxos, &env) { Ok(()) => (), Err(err) => warn!("Transaction validation failed ({:?})", err), } @@ -399,6 +400,47 @@ impl ApplyDB { utxos } + fn mk_environment(block: &MultiEraBlock, prot_magic: &u32) -> Result { + if block.era() == Era::Byron { + let slot: u64 = block.header().slot(); + if slot <= 322876 { + // These are the genesis values. + Ok(Environment { + prot_params: MultiEraProtParams::Byron(ByronProtParams { + min_fees_const: 155381, + min_fees_factor: 44, + max_tx_size: 4096, + }), + prot_magic: *prot_magic, + }) + } else if slot > 322876 && slot <= 1784895 { + // Block hash were the update proposal was submitted: + // 850805044e0df6c13ced2190db7b11489672b0225d478a35a6db71fbfb33afc0 + Ok(Environment { + prot_params: MultiEraProtParams::Byron(ByronProtParams { + min_fees_const: 155381, + min_fees_factor: 44, + max_tx_size: 65536, + }), + prot_magic: *prot_magic, + }) + } else { + // Block hash were the update proposal was submitted: + // d798a8d617b25fc6456ffe2d90895a2c15a7271b671dab2d18d46f3d0e4ef495 + Ok(Environment { + prot_params: MultiEraProtParams::Byron(ByronProtParams { + min_fees_const: 155381, + min_fees_factor: 44, + max_tx_size: 8192, + }), + prot_magic: *prot_magic, + }) + } + } else { + Err(Error::UnimplementedEra) + } + } + pub fn undo_block(&mut self, cbor: &[u8]) -> Result<(), Error> { let block = MultiEraBlock::decode(cbor).map_err(|_| Error::Cbor)?; let slot = block.slot(); @@ -522,7 +564,7 @@ mod tests { } } - db.apply_block(&cbor, None).unwrap(); + db.apply_block(&cbor, &764824073).unwrap(); // This is mainnet's protocol magic number. for tx in block.txs() { for input in tx.consumes() { diff --git a/src/sync/ledger.rs b/src/sync/ledger.rs index b410e9a3..2a223da2 100644 --- a/src/sync/ledger.rs +++ b/src/sync/ledger.rs @@ -1,8 +1,5 @@ use gasket::framework::*; -use pallas::{ - applying::types::{ByronProtParams, Environment, MultiEraProtParams}, - ledger::configs::byron::GenesisFile, -}; +use pallas::ledger::configs::byron::GenesisFile; use tracing::info; use crate::prelude::*; @@ -15,7 +12,7 @@ pub type UpstreamPort = gasket::messaging::tokio::InputPort; pub struct Stage { ledger: ApplyDB, genesis: GenesisFile, - environment: Environment, + prot_magic: u32, pub upstream: UpstreamPort, @@ -28,32 +25,10 @@ pub struct Stage { impl Stage { pub fn new(ledger: ApplyDB, genesis: GenesisFile, prot_magic: u64) -> Self { - let env: Environment = Environment { - prot_params: MultiEraProtParams::Byron(ByronProtParams { - min_fees_const: genesis - .block_version_data - .tx_fee_policy - .summand - .parse::() - .unwrap_or_else(|err| panic!("{:?}", err)), - min_fees_factor: genesis - .block_version_data - .tx_fee_policy - .multiplier - .parse::() - .unwrap_or_else(|err| panic!("{:?}", err)), - max_tx_size: genesis - .block_version_data - .max_tx_size - .parse::() - .unwrap_or_else(|err| panic!("{:?}", err)), - }), - prot_magic: prot_magic as u32, - }; Self { ledger, genesis, - environment: env, + prot_magic: prot_magic as u32, upstream: Default::default(), // downstream: Default::default(), block_count: Default::default(), @@ -85,7 +60,7 @@ impl gasket::framework::Worker for Worker { info!(slot, "applying block"); stage .ledger - .apply_block(cbor, Some(&stage.environment)) + .apply_block(cbor, &stage.prot_magic) .or_panic()?; } RollEvent::Undo(slot, _, cbor) => {