Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce serialized size of BlockResults #859

Merged
merged 1 commit into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where
}

// Tracks the accepted transactions
self.storage.block.results = BlockResults::with_len(req.txs.len());
self.storage.block.results = BlockResults::default();
for (tx_index, processed_tx) in req.txs.iter().enumerate() {
let tx = if let Ok(tx) = Tx::try_from(processed_tx.tx.as_ref()) {
tx
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ ark-serialize = {version = "0.3"}
arse-merkle-tree = {package = "sparse-merkle-tree", git = "https://github.com/heliaxdev/sparse-merkle-tree", rev = "04ad1eeb28901b57a7599bbe433b3822965dabe8", default-features = false, features = ["std", "borsh"]}
bech32 = "0.8.0"
bellman = "0.11.2"
bit-vec = "0.6.3"
borsh = "0.9.0"
chrono = {version = "0.4.22", default-features = false, features = ["clock", "std"]}
data-encoding = "2.3.2"
Expand All @@ -75,6 +74,7 @@ ibc-proto = {version = "0.17.1", default-features = false, optional = true}
ibc-abcipp = {package = "ibc", git = "https://github.com/heliaxdev/ibc-rs", rev = "9fcc1c8c19db6af50806ffe5b2f6c214adcbfd5d", default-features = false, optional = true}
ibc-proto-abcipp = {package = "ibc-proto", git = "https://github.com/heliaxdev/ibc-rs", rev = "9fcc1c8c19db6af50806ffe5b2f6c214adcbfd5d", default-features = false, optional = true}
ics23 = "0.7.0"
index-set = {git = "https://github.com/heliaxdev/index-set", tag = "v0.7.1", features = ["serialize-borsh", "serialize-serde"]}
itertools = "0.10.0"
libsecp256k1 = {git = "https://github.com/heliaxdev/libsecp256k1", rev = "bbb3bd44a49db361f21d9db80f9a087c194c0ae9", default-features = false, features = ["std", "static-context"]}
masp_primitives = { git = "https://github.com/anoma/masp", rev = "bee40fc465f6afbd10558d12fe96eb1742eee45c" }
Expand Down
74 changes: 24 additions & 50 deletions core/src/types/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::str::FromStr;

use arse_merkle_tree::traits::Value;
use arse_merkle_tree::{InternalKey, Key as TreeKey};
use bit_vec::BitVec;
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use data_encoding::BASE32HEX_NOPAD;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use index_set::vec::VecIndexSet;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::bytes::ByteBuf;
Expand Down Expand Up @@ -92,24 +92,8 @@ impl From<TxIndex> for u32 {
}
}

fn serialize_bitvec<S>(x: &BitVec, s: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
Serialize::serialize(&x.to_bytes(), s)
}

fn deserialize_bitvec<'de, D>(
deserializer: D,
) -> std::result::Result<BitVec, D::Error>
where
D: Deserializer<'de>,
{
let s: Vec<u8> = Deserialize::deserialize(deserializer)?;
Ok(BitVec::from_bytes(&s))
}

/// Represents the accepted transactions in a block
/// Represents the indices of the accepted transactions
/// in a block.
#[derive(
Clone,
PartialEq,
Expand All @@ -120,46 +104,36 @@ where
Debug,
Serialize,
Deserialize,
BorshSerialize,
BorshDeserialize,
Default,
)]
pub struct BlockResults(
#[serde(serialize_with = "serialize_bitvec")]
#[serde(deserialize_with = "deserialize_bitvec")]
BitVec,
);
pub struct BlockResults(VecIndexSet<u128>);

impl BlockResults {
/// Create `len` rejection results
pub fn with_len(len: usize) -> Self {
BlockResults(BitVec::from_elem(len, true))
/// Accept the tx at the given position.
#[inline]
pub fn accept(&mut self, index: usize) {
self.0.remove(index)
}

/// Accept the tx at the given position
pub fn accept(&mut self, idx: usize) {
self.0.set(idx, false)
/// Reject the tx at the given position.
#[inline]
pub fn reject(&mut self, index: usize) {
self.0.insert(index)
}

/// Reject the tx at the given position
pub fn reject(&mut self, idx: usize) {
self.0.set(idx, true)
/// Check if the tx at the given position is accepted.
#[inline]
pub fn is_accepted(&self, index: usize) -> bool {
!self.0.contains(index)
}

/// Check if the tx at the given position is accepted
pub fn is_accepted(&self, idx: usize) -> bool {
!self.0[idx]
}
}

impl BorshSerialize for BlockResults {
fn serialize<W: Write>(&self, writer: &mut W) -> std::io::Result<()> {
BorshSerialize::serialize(&self.0.to_bytes(), writer)
}
}

impl BorshDeserialize for BlockResults {
fn deserialize(buf: &mut &[u8]) -> std::io::Result<Self> {
let vec: Vec<_> = BorshDeserialize::deserialize(buf)?;
Ok(Self(BitVec::from_bytes(&vec)))
/// Return an iterator over the removed txs
/// in this [`BlockResults`] instance.
#[inline]
pub fn iter_removed(&self) -> impl Iterator<Item = usize> + '_ {
self.0.iter()
}
}

Expand Down
11 changes: 10 additions & 1 deletion wasm/Cargo.lock

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

11 changes: 10 additions & 1 deletion wasm_for_tests/wasm_source/Cargo.lock

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