Skip to content

Commit ae7b40c

Browse files
SmaGManRexagon
authored andcommitted
feat(models): add ValueFlow::burned
1 parent 245c7f0 commit ae7b40c

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/models/block/mod.rs

+45-4
Original file line numberDiff line numberDiff line change
@@ -600,18 +600,42 @@ pub struct ValueFlow {
600600

601601
/// Total fees collected in this block.
602602
pub fees_collected: CurrencyCollection,
603+
604+
/// Burned native tokens and extra currencies.
605+
pub burned: CurrencyCollection,
606+
603607
/// Shard fees imported to this block.
604608
pub fees_imported: CurrencyCollection,
605609
/// Fee recovery (?)
606610
pub recovered: CurrencyCollection,
607611
/// Block creation fees.
608612
pub created: CurrencyCollection,
609-
/// Minted extra currencies.
613+
/// Minted native tokens and extra currencies.
610614
pub minted: CurrencyCollection,
611615
}
612616

613617
impl ValueFlow {
614618
const TAG_V1: u32 = 0xb8e48dfb;
619+
const TAG_V2: u32 = 0x3ebf98b7;
620+
621+
/// Returns `true` if an inbound value flow is in sync with an outbound.
622+
pub fn validate(&self) -> Result<bool, Error> {
623+
let in_val = self
624+
.from_prev_block
625+
.checked_add(&self.imported)
626+
.and_then(|val| val.checked_add(&self.fees_imported))
627+
.and_then(|val| val.checked_add(&self.created))
628+
.and_then(|val| val.checked_add(&self.minted))
629+
.and_then(|val| val.checked_add(&self.recovered))?;
630+
631+
let out_val = self
632+
.to_next_block
633+
.checked_add(&self.exported)
634+
.and_then(|val| val.checked_add(&self.fees_collected))
635+
.and_then(|val| val.checked_add(&self.burned))?;
636+
637+
Ok(in_val == out_val)
638+
}
615639
}
616640

617641
impl Store for ValueFlow {
@@ -620,6 +644,12 @@ impl Store for ValueFlow {
620644
builder: &mut CellBuilder,
621645
context: &dyn CellContext,
622646
) -> Result<(), Error> {
647+
let (tag, store_burned) = if self.burned.is_zero() {
648+
(Self::TAG_V1, false)
649+
} else {
650+
(Self::TAG_V2, true)
651+
};
652+
623653
let cell1 = {
624654
let mut builder = CellBuilder::new();
625655
ok!(self.from_prev_block.store_into(&mut builder, context));
@@ -629,10 +659,13 @@ impl Store for ValueFlow {
629659
ok!(builder.build_ext(context))
630660
};
631661

632-
ok!(builder.store_u32(Self::TAG_V1));
662+
ok!(builder.store_u32(tag));
633663
ok!(builder.store_reference(cell1));
634664

635665
ok!(self.fees_collected.store_into(builder, context));
666+
if store_burned {
667+
ok!(self.burned.store_into(builder, context));
668+
}
636669

637670
let cell2 = {
638671
let mut builder = CellBuilder::new();
@@ -648,12 +681,19 @@ impl Store for ValueFlow {
648681

649682
impl<'a> Load<'a> for ValueFlow {
650683
fn load_from(slice: &mut CellSlice<'a>) -> Result<Self, Error> {
651-
match ok!(slice.load_u32()) {
652-
Self::TAG_V1 => {}
684+
let with_burned = match ok!(slice.load_u32()) {
685+
Self::TAG_V1 => false,
686+
Self::TAG_V2 => true,
653687
_ => return Err(Error::InvalidTag),
654688
};
655689

656690
let fees_collected = ok!(CurrencyCollection::load_from(slice));
691+
let burned = if with_burned {
692+
ok!(CurrencyCollection::load_from(slice))
693+
} else {
694+
CurrencyCollection::ZERO
695+
};
696+
657697
let slice1 = &mut ok!(slice.load_reference_as_slice());
658698
let slice2 = &mut ok!(slice.load_reference_as_slice());
659699

@@ -663,6 +703,7 @@ impl<'a> Load<'a> for ValueFlow {
663703
imported: ok!(CurrencyCollection::load_from(slice1)),
664704
exported: ok!(CurrencyCollection::load_from(slice1)),
665705
fees_collected,
706+
burned,
666707
fees_imported: ok!(CurrencyCollection::load_from(slice2)),
667708
recovered: ok!(CurrencyCollection::load_from(slice2)),
668709
created: ok!(CurrencyCollection::load_from(slice2)),

src/models/block/tests/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ fn block_with_tycho_updates_store_load() {
398398
tokens: Tokens::new(3195840031),
399399
other: ExtraCurrencyCollection::new(),
400400
},
401+
burned: CurrencyCollection {
402+
tokens: Tokens::new(1),
403+
other: ExtraCurrencyCollection::new(),
404+
},
401405
fees_imported: CurrencyCollection {
402406
tokens: Tokens::new(1495840031),
403407
other: ExtraCurrencyCollection::new(),

0 commit comments

Comments
 (0)