Skip to content

Commit

Permalink
Replace Finalizer with CellContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Sep 24, 2023
1 parent b839777 commit 8c7b115
Show file tree
Hide file tree
Showing 42 changed files with 731 additions and 814 deletions.
4 changes: 2 additions & 2 deletions proc/src/derive_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn impl_derive(input: syn::DeriveInput) -> Result<TokenStream, Vec<syn::Erro
fn store_into(
&self,
__builder: &mut ::everscale_types::cell::CellBuilder,
__finalizer: &mut dyn ::everscale_types::cell::Finalizer
__context: &mut dyn ::everscale_types::cell::CellContext,
) -> ::core::result::Result<(), ::everscale_types::error::Error> {
#body
}
Expand Down Expand Up @@ -151,7 +151,7 @@ fn store_op(field_ident: &TokenStream, ty: &syn::Type) -> TokenStream {
}
};

quote! { <#ty as ::everscale_types::cell::Store>::store_into(&#field_ident, __builder, __finalizer) }
quote! { <#ty as ::everscale_types::cell::Store>::store_into(&#field_ident, __builder, __context) }
}

fn into_ok(tokens: TokenStream) -> TokenStream {
Expand Down
14 changes: 7 additions & 7 deletions src/abi/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use sha2::Digest;
use crate::abi::value::ser::AbiSerializer;
use crate::abi::AbiHeader;
use crate::cell::{
Cell, CellBuilder, CellFamily, CellSlice, CellSliceRange, CellSliceSize, DefaultCellContext,
DynCell, HashBytes, Store,
Cell, CellBuilder, CellFamily, CellSlice, CellSliceRange, CellSliceSize, DynCell, HashBytes,
Store,
};
use crate::dict::RawDict;
use crate::models::{
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Contract {
return Ok(data.clone());
}

let context = &mut Cell::default_cell_context();
let context = &mut Cell::empty_context();
let mut key_builder = CellBuilder::new();

for token in tokens {
Expand Down Expand Up @@ -129,7 +129,7 @@ impl Contract {
.map(|(name, value)| (name.as_ref(), value))
.collect::<HashMap<_, _>>();

let context = &mut Cell::default_cell_context();
let context = &mut Cell::empty_context();
let mut key_builder = CellBuilder::new();

// Write explicitly provided values
Expand Down Expand Up @@ -444,7 +444,7 @@ impl Function {
serializer.reserve_value(&token.value);
}

let context = &mut Cell::default_cell_context();
let context = &mut Cell::empty_context();
serializer.write_value(&output_id, context)?;
serializer.write_tuple(tokens, context)?;
serializer.finalize(context).map_err(From::from)
Expand Down Expand Up @@ -684,7 +684,7 @@ impl<'f, 'a> ExternalInput<'f, 'a> {
fn build_input_ext(&self, address: Option<&StdAddr>) -> Result<UnsignedBody> {
let (expire_at, payload) = self.build_payload(true)?;

let context = &mut Cell::default_cell_context();
let context = &mut Cell::empty_context();
let hash = if self.function.abi_version >= AbiVersion::V2_3 {
let mut to_sign = CellBuilder::new();
match address {
Expand Down Expand Up @@ -753,7 +753,7 @@ impl<'f, 'a> ExternalInput<'f, 'a> {
serializer.reserve_value(&token.value);
}

let context = &mut Cell::default_cell_context();
let context = &mut Cell::empty_context();

if !reserve_signature {
let value = if abi_version.major == 1 {
Expand Down
10 changes: 5 additions & 5 deletions src/abi/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytes::Bytes;

use crate::abi::*;
use crate::models::StdAddr;
use crate::prelude::{Cell, CellBuilder, DefaultFinalizer, HashBytes, RawDict, Store};
use crate::prelude::{Cell, CellBuilder, CellFamily, HashBytes, RawDict, Store};

const DEPOOL_ABI: &str = include_str!("depool.abi.json");

Expand Down Expand Up @@ -106,7 +106,7 @@ fn encode_external_input() {
.store_reference({
let mut builder = CellBuilder::new();
StdAddr::default()
.store_into(&mut builder, &mut Cell::default_finalizer())
.store_into(&mut builder, &mut Cell::empty_context())
.unwrap();
builder.store_u8(1).unwrap();
builder.build().unwrap()
Expand Down Expand Up @@ -152,7 +152,7 @@ fn decode_external_input() {
.store_reference({
let mut builder = CellBuilder::new();
StdAddr::default()
.store_into(&mut builder, &mut Cell::default_finalizer())
.store_into(&mut builder, &mut Cell::empty_context())
.unwrap();
builder.store_u8(1).unwrap();
builder.build().unwrap()
Expand Down Expand Up @@ -183,7 +183,7 @@ fn encode_unsigned_external_input() {
builder.store_u64(321).unwrap();
builder.store_reference(Cell::default()).unwrap();
StdAddr::default()
.store_into(&mut builder, &mut Cell::default_finalizer())
.store_into(&mut builder, &mut Cell::empty_context())
.unwrap();
builder.store_u8(1).unwrap();
builder.build().unwrap()
Expand Down Expand Up @@ -223,7 +223,7 @@ fn decode_unsigned_external_input() {
builder.store_u64(321).unwrap();
builder.store_reference(Cell::default()).unwrap();
StdAddr::default()
.store_into(&mut builder, &mut Cell::default_finalizer())
.store_into(&mut builder, &mut Cell::empty_context())
.unwrap();
builder.store_u8(1).unwrap();
builder.build().unwrap()
Expand Down
19 changes: 9 additions & 10 deletions src/abi/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ mod tests {
use std::sync::Arc;

use crate::boc::Boc;
use crate::cell::DefaultFinalizer;
use crate::dict::Dict;
use crate::models::{StdAddr, VarAddr};
use crate::num::{Uint9, VarUint24, VarUint56};
Expand Down Expand Up @@ -1092,24 +1091,24 @@ mod tests {
]);

//
let finalizer = &mut Cell::default_finalizer();
let context = &mut Cell::empty_context();
let mut builder = CellBuilder::new();
builder.store_u32(0)?;
builder.store_reference(Cell::empty_cell())?;

bytes_map.store_into(&mut builder, finalizer)?;
int_map.store_into(&mut builder, finalizer)?;
bytes_map.store_into(&mut builder, context)?;
int_map.store_into(&mut builder, context)?;

let cell_v2 = {
let mut builder = builder.clone();
tuples_map.store_into(&mut builder, finalizer)?;
tuples_map.store_into(&mut builder, context)?;
builder.store_bit_zero()?;
builder.build()?
};

let cell_v1 = {
let mut child_builder = CellBuilder::new();
tuples_map.store_into(&mut child_builder, finalizer)?;
tuples_map.store_into(&mut child_builder, context)?;
child_builder.store_bit_zero()?;

builder.store_reference(child_builder.build()?)?;
Expand Down Expand Up @@ -1154,7 +1153,7 @@ mod tests {
let mut builder = CellBuilder::new();
builder.store_u32(0)?;
builder.store_reference(Cell::empty_cell())?;
addr_map.store_into(&mut builder, &mut Cell::default_finalizer())?;
addr_map.store_into(&mut builder, &mut Cell::empty_context())?;
builder.build()?
};

Expand Down Expand Up @@ -1222,15 +1221,15 @@ mod tests {

//
let cell = {
let finalizer = &mut Cell::default_finalizer();
let context = &mut Cell::empty_context();
let mut builder = CellBuilder::new();
builder.store_u32(0)?;
builder.store_reference(Cell::empty_cell())?;

map.store_into(&mut builder, finalizer)?;
map.store_into(&mut builder, context)?;

builder.store_u32(1)?;
array.store_into(&mut builder, finalizer)?;
array.store_into(&mut builder, context)?;

builder.build()?
};
Expand Down
41 changes: 20 additions & 21 deletions src/abi/value/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ use crate::abi::{
PlainAbiValue,
};
use crate::cell::{
Cell, CellBuilder, CellContext, CellSlice, CellSliceSize, CellTreeStats, DefaultCellContext,
DefaultFinalizer, Finalizer, Store, MAX_BIT_LEN, MAX_REF_COUNT,
Cell, CellBuilder, CellContext, CellSlice, CellSliceSize, CellTreeStats, Store, MAX_BIT_LEN,
MAX_REF_COUNT,
};
use crate::dict::{self, RawDict};
use crate::error::Error;
use crate::models::IntAddr;
use crate::num::Tokens;
use crate::prelude::CellFamily;

impl NamedAbiValue {
/// Tries to store multiple values into a new builder according to the specified ABI version.
pub fn tuple_to_builder(items: &[Self], version: AbiVersion) -> Result<CellBuilder, Error> {
let context = &mut Cell::default_cell_context();
let context = &mut Cell::empty_context();
let mut serializer = AbiSerializer::new(version);
for item in items {
serializer.reserve_value(&item.value);
Expand Down Expand Up @@ -49,7 +50,7 @@ impl NamedAbiValue {
impl AbiValue {
/// Tries to store multiple values into a new builder according to the specified ABI version.
pub fn tuple_to_builder(values: &[Self], version: AbiVersion) -> Result<CellBuilder, Error> {
let context = &mut Cell::default_cell_context();
let context = &mut Cell::empty_context();
let mut serializer = AbiSerializer::new(version);
for value in values {
serializer.reserve_value(value);
Expand All @@ -67,7 +68,7 @@ impl AbiValue {

/// Tries to store this value into a new builder according to the specified ABI version.
pub fn make_builder(&self, version: AbiVersion) -> Result<CellBuilder, Error> {
let context = &mut Cell::default_cell_context();
let context = &mut Cell::empty_context();
let mut serializer = AbiSerializer::new(version);
serializer.reserve_value(self);
ok!(serializer.write_value(self, context));
Expand Down Expand Up @@ -268,8 +269,7 @@ impl AbiSerializer {

let mut result = self.stack.pop().unwrap_or_default();
while let Some(builder) = self.stack.pop() {
let child =
ok!(std::mem::replace(&mut result, builder).build_ext(context.as_finalizer()));
let child = ok!(std::mem::replace(&mut result, builder).build_ext(context));
ok!(result.store_reference(child));
}
Ok(result)
Expand All @@ -283,8 +283,7 @@ impl AbiSerializer {

let mut result = self.stack.pop().unwrap_or_default();
while let Some(builder) = self.stack.pop() {
let child =
ok!(std::mem::replace(&mut result, builder).build_ext(context.as_finalizer()));
let child = ok!(std::mem::replace(&mut result, builder).build_ext(context));
ok!(result.store_reference(child));
}
Ok(result)
Expand Down Expand Up @@ -437,7 +436,7 @@ impl AbiSerializer {
},
refs: 0,
});
address.store_into(target, &mut Cell::default_finalizer())
address.store_into(target, &mut Cell::empty_context())
}

fn write_tokens(&mut self, tokens: &Tokens) -> Result<(), Error> {
Expand All @@ -449,7 +448,7 @@ impl AbiSerializer {
},
refs: 0,
});
tokens.store_into(target, &mut Cell::default_finalizer())
tokens.store_into(target, &mut Cell::empty_context())
}

fn write_bytes(&mut self, mut data: &[u8], context: &mut dyn CellContext) -> Result<(), Error> {
Expand All @@ -472,7 +471,7 @@ impl AbiSerializer {
data = head;

if result.bit_len() > 0 {
let child = ok!(std::mem::take(&mut result).build_ext(context.as_finalizer()));
let child = ok!(std::mem::take(&mut result).build_ext(context));
ok!(result.store_reference(child));
}

Expand Down Expand Up @@ -508,7 +507,7 @@ impl AbiSerializer {
let value = if inline_value {
InlineOrRef::Inline(value.as_full_slice())
} else {
InlineOrRef::Ref(ok!(value.build_ext(context.as_finalizer())))
InlineOrRef::Ref(ok!(value.build_ext(context)))
};

ok!(dict.set(key_builder.as_data_slice(), value));
Expand All @@ -528,7 +527,7 @@ impl AbiSerializer {
if !fixed_len {
ok!(target.store_u32(values.len() as u32));
}
dict.store_into(target, context.as_finalizer())
dict.store_into(target, context)
}

fn write_map(
Expand All @@ -545,7 +544,7 @@ impl AbiSerializer {
let mut key_builder = CellBuilder::new();
let mut serializer = self.begin_child();
for (key, value) in value {
ok!(key.store_into(&mut key_builder, context.as_finalizer()));
ok!(key.store_into(&mut key_builder, context));

let value = {
serializer.reserve_value(value);
Expand All @@ -555,7 +554,7 @@ impl AbiSerializer {
let value = if inline_value {
InlineOrRef::Inline(value.as_full_slice())
} else {
InlineOrRef::Ref(ok!(value.build_ext(context.as_finalizer())))
InlineOrRef::Ref(ok!(value.build_ext(context)))
};

let (new_dict, _) = ok!(dict::dict_insert(
Expand All @@ -572,7 +571,7 @@ impl AbiSerializer {
}

let target = self.require_builder(CellSliceSize { bits: 1, refs: 1 });
dict.store_into(target, context.as_finalizer())
dict.store_into(target, context)
}

fn write_optional(
Expand Down Expand Up @@ -618,7 +617,7 @@ impl AbiSerializer {
if inline {
target.store_builder(&value)
} else {
target.store_reference(ok!(value.build_ext(context.as_finalizer())))
target.store_reference(ok!(value.build_ext(context)))
}
}
None => {
Expand All @@ -638,7 +637,7 @@ impl AbiSerializer {
serializer.reserve_value(value);
ok!(serializer.write_value(value, context));
let builder = ok!(serializer.finalize(context));
ok!(builder.build_ext(context.as_finalizer()))
ok!(builder.build_ext(context))
};
let target = self.require_builder(CellSliceSize { bits: 0, refs: 1 });
target.store_reference(cell)
Expand Down Expand Up @@ -697,7 +696,7 @@ enum InlineOrRef<'a> {
}

impl Store for InlineOrRef<'_> {
fn store_into(&self, builder: &mut CellBuilder, _: &mut dyn Finalizer) -> Result<(), Error> {
fn store_into(&self, builder: &mut CellBuilder, _: &mut dyn CellContext) -> Result<(), Error> {
match self {
Self::Inline(slice) => builder.store_slice(slice),
Self::Ref(cell) => builder.store_reference(cell.clone()),
Expand All @@ -706,7 +705,7 @@ impl Store for InlineOrRef<'_> {
}

impl Store for PlainAbiValue {
fn store_into(&self, builder: &mut CellBuilder, f: &mut dyn Finalizer) -> Result<(), Error> {
fn store_into(&self, builder: &mut CellBuilder, f: &mut dyn CellContext) -> Result<(), Error> {
match self {
Self::Uint(bits, value) => write_int(*bits, Sign::Plus, value, builder),
Self::Int(bits, value) => write_int(*bits, value.sign(), value.magnitude(), builder),
Expand Down
8 changes: 4 additions & 4 deletions src/boc/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Deref;
use smallvec::SmallVec;

use super::BocTag;
use crate::cell::{Cell, CellDescriptor, CellParts, Finalizer, LevelMask, MAX_REF_COUNT};
use crate::cell::{Cell, CellContext, CellDescriptor, CellParts, LevelMask, MAX_REF_COUNT};
use crate::util::{read_be_u32_fast, read_be_u64_fast, unlikely, ArrayVec};

#[cfg(feature = "stats")]
Expand Down Expand Up @@ -247,8 +247,8 @@ impl<'a> BocHeader<'a> {
})
}

/// Assembles cell tree from slices using the specified finalizer.
pub fn finalize(&self, finalizer: &mut dyn Finalizer) -> Result<ProcessedCells, Error> {
/// Assembles cell tree from slices using the specified cell context.
pub fn finalize(&self, context: &mut dyn CellContext) -> Result<ProcessedCells, Error> {
let ref_size = self.ref_size;
let cell_count = self.cells.len() as u32;

Expand All @@ -266,7 +266,7 @@ impl<'a> BocHeader<'a> {
))
};

let cell = match finalizer.finalize_cell(ctx) {
let cell = match context.finalize_cell(ctx) {
Ok(cell) => cell,
Err(_) => return Err(Error::InvalidCell),
};
Expand Down
Loading

0 comments on commit 8c7b115

Please sign in to comment.