Skip to content

Commit

Permalink
refactor(models): remove copyleft
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Feb 5, 2025
1 parent 607ce68 commit fd1338f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 177 deletions.
31 changes: 4 additions & 27 deletions src/models/block/block_extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::OnceLock;
use crate::cell::*;
use crate::dict::{AugDict, AugDictExtra, Dict, DictKey};
use crate::error::Error;
use crate::num::Uint15;

use crate::models::config::{BlockchainConfig, ValidatorDescription};
use crate::models::currency::CurrencyCollection;
Expand Down Expand Up @@ -220,9 +219,6 @@ pub struct McBlockExtra {
pub recover_create_msg: Option<Lazy<InMsg>>,
/// An optional message with minting.
pub mint_msg: Option<Lazy<InMsg>>,
/// Copyleft messages if present.
#[cfg_attr(feature = "serde", serde(skip))]
pub copyleft_msgs: Dict<Uint15, Cell>,
/// Blockchain config (if the block is a key block).
pub config: Option<BlockchainConfig>,
}
Expand All @@ -235,15 +231,13 @@ impl Default for McBlockExtra {
prev_block_signatures: Dict::new(),
recover_create_msg: None,
mint_msg: None,
copyleft_msgs: Dict::new(),
config: None,
}
}
}

impl McBlockExtra {
const TAG_V1: u16 = 0xcca5;
const TAG_V2: u16 = 0xdc75;

/// Tries to load recover/create message.
pub fn load_recover_create_msg(&self) -> Result<Option<InMsg>, Error> {
Expand All @@ -268,26 +262,15 @@ impl Store for McBlockExtra {
builder: &mut CellBuilder,
context: &dyn CellContext,
) -> Result<(), Error> {
let tag = if self.copyleft_msgs.is_empty() {
Self::TAG_V1
} else {
Self::TAG_V2
};

let cell = {
let mut builder = CellBuilder::new();
ok!(self.prev_block_signatures.store_into(&mut builder, context));
ok!(self.recover_create_msg.store_into(&mut builder, context));
ok!(self.mint_msg.store_into(&mut builder, context));

if !self.copyleft_msgs.is_empty() {
ok!(self.copyleft_msgs.store_into(&mut builder, context));
}

ok!(builder.build_ext(context))
};

ok!(builder.store_u16(tag));
ok!(builder.store_u16(Self::TAG_V1));
ok!(builder.store_bit(self.config.is_some()));
ok!(self.shards.store_into(builder, context));
ok!(self.fees.store_into(builder, context));
Expand All @@ -303,12 +286,11 @@ impl Store for McBlockExtra {

impl<'a> Load<'a> for McBlockExtra {
fn load_from(slice: &mut CellSlice<'a>) -> Result<Self, Error> {
let with_copyleft = match slice.load_u16() {
Ok(Self::TAG_V1) => false,
Ok(Self::TAG_V2) => true,
match slice.load_u16() {
Ok(Self::TAG_V1) => {}
Ok(_) => return Err(Error::InvalidTag),
Err(e) => return Err(e),
};
}

let with_config = ok!(slice.load_bit());
let shards = ok!(ShardHashes::load_from(slice));
Expand All @@ -330,11 +312,6 @@ impl<'a> Load<'a> for McBlockExtra {
prev_block_signatures: ok!(Dict::load_from(slice)),
recover_create_msg: ok!(Option::<Lazy<_>>::load_from(slice)),
mint_msg: ok!(Option::<Lazy<_>>::load_from(slice)),
copyleft_msgs: if with_copyleft {
ok!(Dict::load_from(slice))
} else {
Dict::new()
},
config,
})
}
Expand Down
33 changes: 5 additions & 28 deletions src/models/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
use std::sync::OnceLock;

use crate::cell::*;
#[allow(unused)]
use crate::dict::Dict;
use crate::error::Error;
use crate::merkle::MerkleUpdate;
use crate::num::*;
use crate::util::*;

use crate::models::currency::CurrencyCollection;
Expand Down Expand Up @@ -609,14 +609,10 @@ pub struct ValueFlow {
pub created: CurrencyCollection,
/// Minted extra currencies.
pub minted: CurrencyCollection,

/// Optional copyleft rewards.
pub copyleft_rewards: Dict<HashBytes, Tokens>,
}

impl ValueFlow {
const TAG_V1: u32 = 0xb8e48dfb;
const TAG_V2: u32 = 0xe0864f6d;
}

impl Store for ValueFlow {
Expand All @@ -625,12 +621,6 @@ impl Store for ValueFlow {
builder: &mut CellBuilder,
context: &dyn CellContext,
) -> Result<(), Error> {
let tag = if self.copyleft_rewards.is_empty() {
Self::TAG_V1
} else {
Self::TAG_V2
};

let cell1 = {
let mut builder = CellBuilder::new();
ok!(self.from_prev_block.store_into(&mut builder, context));
Expand All @@ -640,7 +630,7 @@ impl Store for ValueFlow {
ok!(builder.build_ext(context))
};

ok!(builder.store_u32(tag));
ok!(builder.store_u32(Self::TAG_V1));
ok!(builder.store_reference(cell1));

ok!(self.fees_collected.store_into(builder, context));
Expand All @@ -653,32 +643,20 @@ impl Store for ValueFlow {
ok!(self.minted.store_into(&mut builder, context));
ok!(builder.build_ext(context))
};
ok!(builder.store_reference(cell2));

if !self.copyleft_rewards.is_empty() {
self.copyleft_rewards.store_into(builder, context)
} else {
Ok(())
}
builder.store_reference(cell2)
}
}

impl<'a> Load<'a> for ValueFlow {
fn load_from(slice: &mut CellSlice<'a>) -> Result<Self, Error> {
let with_copyleft_rewards = match ok!(slice.load_u32()) {
Self::TAG_V1 => false,
Self::TAG_V2 => true,
match ok!(slice.load_u32()) {
Self::TAG_V1 => {}
_ => return Err(Error::InvalidTag),
};

let fees_collected = ok!(CurrencyCollection::load_from(slice));
let slice1 = &mut ok!(slice.load_reference_as_slice());
let slice2 = &mut ok!(slice.load_reference_as_slice());
let copyleft_rewards = if with_copyleft_rewards {
ok!(Dict::load_from(slice))
} else {
Dict::new()
};

Ok(Self {
from_prev_block: ok!(CurrencyCollection::load_from(slice1)),
Expand All @@ -690,7 +668,6 @@ impl<'a> Load<'a> for ValueFlow {
recovered: ok!(CurrencyCollection::load_from(slice2)),
created: ok!(CurrencyCollection::load_from(slice2)),
minted: ok!(CurrencyCollection::load_from(slice2)),
copyleft_rewards,
})
}
}
Expand Down
92 changes: 7 additions & 85 deletions src/models/block/shard_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::dict::{self, Dict, DictKey};
use crate::error::Error;
use crate::models::block::block_id::{BlockId, ShardIdent};
use crate::models::currency::CurrencyCollection;
use crate::num::Tokens;
use crate::util::*;

/// A tree of the most recent descriptions for all currently existing shards
Expand Down Expand Up @@ -423,20 +422,13 @@ pub struct ShardDescription {
pub fees_collected: CurrencyCollection,
/// Amount of funds created in this shard since the last masterchain block.
pub funds_created: CurrencyCollection,
/// Copyleft rewards if present.
pub copyleft_rewards: Dict<HashBytes, Tokens>,
/// Proofs from other workchains.
#[cfg_attr(feature = "serde", serde(skip))]
pub proof_chain: Option<ProofChain>,
}

impl ShardDescription {
const TAG_LEN: u16 = 4;

const TAG_V1: u8 = 0xa;
const TAG_V2: u8 = 0xb;
const TAG_V3: u8 = 0xc;
const TAG_V4: u8 = 0xd;

/// Converts a `ShardDescription` to a `BlockId` given a shard identifier.
pub fn as_block_id(&self, shard: ShardIdent) -> BlockId {
Expand All @@ -455,15 +447,6 @@ impl Store for ShardDescription {
builder: &mut CellBuilder,
context: &dyn CellContext,
) -> Result<(), Error> {
#[allow(unused_mut)]
let mut tag = if self.proof_chain.is_some() {
Self::TAG_V4
} else if !self.copyleft_rewards.is_empty() {
Self::TAG_V3
} else {
Self::TAG_V1
};

let flags = ((self.before_split as u8) << 7)
| ((self.before_merge as u8) << 6)
| ((self.want_split as u8) << 5)
Expand All @@ -472,7 +455,7 @@ impl Store for ShardDescription {
#[cfg(feature = "tycho")]
let flags = flags | ((self.top_sc_block_updated as u8) << 2);

ok!(builder.store_small_uint(tag, Self::TAG_LEN));
ok!(builder.store_small_uint(Self::TAG_V1, Self::TAG_LEN));
ok!(builder.store_u32(self.seqno));
ok!(builder.store_u32(self.reg_mc_seqno));
ok!(builder.store_u64(self.start_lt));
Expand All @@ -493,19 +476,6 @@ impl Store for ShardDescription {
let mut builder = CellBuilder::new();
ok!(self.fees_collected.store_into(&mut builder, context));
ok!(self.funds_created.store_into(&mut builder, context));

if let Some(proof_chain) = &self.proof_chain {
ok!(if self.copyleft_rewards.is_empty() {
builder.store_bit_zero()
} else {
ok!(builder.store_bit_one());
self.copyleft_rewards.store_into(&mut builder, context)
});
ok!(proof_chain.store_into(&mut builder, context));
} else if !self.copyleft_rewards.is_empty() {
ok!(self.copyleft_rewards.store_into(&mut builder, context));
}

ok!(builder.build_ext(context))
};

Expand All @@ -515,16 +485,12 @@ impl Store for ShardDescription {

impl<'a> Load<'a> for ShardDescription {
fn load_from(slice: &mut CellSlice<'a>) -> Result<Self, Error> {
#[allow(unused_mut)]
let (cont_in_cell, with_copyleft, mut with_proof_chain) =
match slice.load_small_uint(Self::TAG_LEN) {
Ok(Self::TAG_V1) => (true, false, false),
Ok(Self::TAG_V2) => (false, false, false),
Ok(Self::TAG_V3) => (true, true, false),
Ok(Self::TAG_V4) => (true, true, true),
Ok(_) => return Err(Error::InvalidTag),
Err(e) => return Err(e),
};
let cont_in_cell = match slice.load_small_uint(Self::TAG_LEN) {
Ok(Self::TAG_V1) => true,
Ok(Self::TAG_V2) => false,
Ok(_) => return Err(Error::InvalidTag),
Err(e) => return Err(e),
};

let seqno = ok!(slice.load_u32());
let reg_mc_seqno = ok!(slice.load_u32());
Expand Down Expand Up @@ -560,17 +526,6 @@ impl<'a> Load<'a> for ShardDescription {

let fees_collected = ok!(CurrencyCollection::load_from(slice));
let funds_created = ok!(CurrencyCollection::load_from(slice));
let copyleft_rewards = if with_copyleft && (!with_proof_chain || ok!(slice.load_bit())) {
ok!(Dict::load_from(slice))
} else {
Dict::new()
};

let proof_chain = if with_proof_chain {
Some(ok!(ProofChain::load_from(slice)))
} else {
None
};

Ok(Self {
seqno,
Expand All @@ -596,8 +551,6 @@ impl<'a> Load<'a> for ShardDescription {
split_merge_at,
fees_collected,
funds_created,
copyleft_rewards,
proof_chain,
})
}
}
Expand Down Expand Up @@ -680,35 +633,6 @@ impl<'a> Load<'a> for FutureSplitMerge {
}
}

/// Proofs from other workchains.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ProofChain {
/// Amount of proofs (`1..=8`)
len: u8,
/// Start cell for proofs.
child: Cell,
}

impl Store for ProofChain {
fn store_into(&self, builder: &mut CellBuilder, _: &dyn CellContext) -> Result<(), Error> {
ok!(builder.store_u8(self.len));
builder.store_reference(self.child.clone())
}
}

impl<'a> Load<'a> for ProofChain {
fn load_from(slice: &mut CellSlice<'a>) -> Result<Self, Error> {
let len = ok!(slice.load_u8());
if !(1..=8).contains(&len) {
return Err(Error::InvalidData);
}
Ok(Self {
len,
child: ok!(slice.load_reference_cloned()),
})
}
}

/// An iterator over the raw entries of shard trees in multiple workchains.
#[derive(Clone)]
pub struct ShardsTreeRawIter<'a> {
Expand Down Expand Up @@ -997,8 +921,6 @@ mod test {
split_merge_at: None,
fees_collected: Default::default(),
funds_created: Default::default(),
copyleft_rewards: Default::default(),
proof_chain: None,
};
// arbitrary order
let input = HashMap::from([
Expand Down
2 changes: 1 addition & 1 deletion src/models/block/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ fn proof_for_shardchain_block() {
#[cfg(feature = "tycho")]
fn block_with_tycho_updates_store_load() {
use crate::models::{ExtraCurrencyCollection, GlobalCapabilities};
use crate::num::Tokens;

let block = Block {
global_id: 42,
Expand Down Expand Up @@ -425,7 +426,6 @@ fn block_with_tycho_updates_store_load() {
tokens: Tokens::new(0),
other: ExtraCurrencyCollection::new(),
},
copyleft_rewards: Dict::new(),
})
.unwrap(),
state_update: Lazy::new(&MerkleUpdate {
Expand Down
Loading

0 comments on commit fd1338f

Please sign in to comment.