Skip to content

Commit

Permalink
feat: adding protocol parameters values
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicoLeberle committed Nov 9, 2023
1 parent b375190 commit 0681f13
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 63 deletions.
38 changes: 12 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ authors = ["Santiago Carmuega <[email protected]>"]


[dependencies]
# pallas = "0.19.0"
# pallas = { path = "../pallas/pallas" }
pallas = { git = "https://github.com/txpipe/pallas.git", branch = "dev/byron-phase-1-validations", features = ["unstable"] }
pallas = { git = "https://github.com/txpipe/pallas.git", branch = "feat/byron-phase-1-validations", features = ["unstable"] }

gasket = { version = "^0.5", features = ["derive"] }
# gasket = { path = "../../construkts/gasket-rs/gasket", features = ["derive"] }
Expand Down
52 changes: 47 additions & 5 deletions src/storage/applydb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod genesis;

use pallas::{
applying::{
types::{Environment, UTxOs},
types::{ByronProtParams, Environment, MultiEraProtParams, UTxOs},
validate,
},
codec::utils::CborWrap,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -399,6 +400,47 @@ impl ApplyDB {
utxos
}

fn mk_environment(block: &MultiEraBlock, prot_magic: &u32) -> Result<Environment, Error> {
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();
Expand Down Expand Up @@ -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() {
Expand Down
33 changes: 4 additions & 29 deletions src/sync/ledger.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -15,7 +12,7 @@ pub type UpstreamPort = gasket::messaging::tokio::InputPort<RollEvent>;
pub struct Stage {
ledger: ApplyDB,
genesis: GenesisFile,
environment: Environment,
prot_magic: u32,

pub upstream: UpstreamPort,

Expand All @@ -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::<u64>()
.unwrap_or_else(|err| panic!("{:?}", err)),
min_fees_factor: genesis
.block_version_data
.tx_fee_policy
.multiplier
.parse::<u64>()
.unwrap_or_else(|err| panic!("{:?}", err)),
max_tx_size: genesis
.block_version_data
.max_tx_size
.parse::<u64>()
.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(),
Expand Down Expand Up @@ -85,7 +60,7 @@ impl gasket::framework::Worker<Stage> 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) => {
Expand Down

0 comments on commit 0681f13

Please sign in to comment.