Skip to content

Commit

Permalink
Merge branch 'tiago/main/index-set-block-results' (#859) into main
Browse files Browse the repository at this point in the history
* tiago/main/index-set-block-results:
  changelog: add #859
  Reduce serialized size of BlockResults
  • Loading branch information
juped committed Dec 13, 2022
2 parents c324544 + 685125b commit 0d4c521
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Use index-set to reduce serialized size of block results.
([#859](https://github.com/anoma/namada/pull/859))
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
pub fn accept(&mut self, idx: usize) {
self.0.set(idx, false)
/// Accept the tx at the given position.
#[inline]
pub fn accept(&mut self, index: usize) {
self.0.remove(index)
}

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

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

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.

36 changes: 18 additions & 18 deletions wasm/checksums.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"tx_bond.wasm": "tx_bond.f5d548aad31a708eaba6efce8fa7098307386d765a4b0c4c69968d1d58fc78de.wasm",
"tx_change_validator_commission.wasm": "tx_change_validator_commission.e04bb99f65b51db0930162334d4ca0a79b0de58077b77cd6f70419791d4bc0a0.wasm",
"tx_ibc.wasm": "tx_ibc.fef8f4de6d037dda95c2432d037072e21a58e26778cd6bea84fc4027c4cb6789.wasm",
"tx_init_account.wasm": "tx_init_account.761e80df256282d664d6badb9cad835e12b6d3780a9c6fe98189cc6e2d1fdd9a.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.fa45417163d128fa1b563dc75c43123b79e70685a90a59f60443e2278871f1ef.wasm",
"tx_init_validator.wasm": "tx_init_validator.722cf578bf77e6d0f93a6c0c615965a1ab1e1718d81ead4c26871d723e5ca9bb.wasm",
"tx_reveal_pk.wasm": "tx_reveal_pk.0edd9dfc5a90e25dfdbe0d3eab88e2dea603876f2dad0f5963e6f30fe8cdfe7b.wasm",
"tx_transfer.wasm": "tx_transfer.77d63ff3b47efdf00e937c297c52692a085624e26e4d0a937c7133e4b60b1a38.wasm",
"tx_unbond.wasm": "tx_unbond.727e961f34d68b89bdb5e09d44c6bf3e6f8bfc5997969864f5256a15d9bd38f0.wasm",
"tx_update_vp.wasm": "tx_update_vp.ee2e9b882c4accadf4626e87d801c9ac8ea8c61ccea677e0532fc6c1ee7db6a2.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.a6c9b8c6ae8cc7ec3193e6d6be46be4e8f329f173391ff7a498977d732fdfd73.wasm",
"tx_withdraw.wasm": "tx_withdraw.809c18e2424942fab464d95b57681ee79bcb49aede0deceddff313e1337dd091.wasm",
"vp_implicit.wasm": "vp_implicit.2ec7662257847b995b66f236afea692b047af8226aa26d504026a30a498ac9f1.wasm",
"vp_masp.wasm": "vp_masp.8c9610656fd8084611909902750965d933525e5f6cfdfa4caaf38b53d43f358a.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.f40b4c65f0736f10af775916fe7defacdc1ae83694cb051224ba6682dcd081a1.wasm",
"vp_token.wasm": "vp_token.405aa6d1107bb99233d11681b37a4a4e13f48552c3031b9503cf213b5c424d41.wasm",
"vp_user.wasm": "vp_user.3a556baef41c7862fcb5fc4dfcb0f24655c85d77148b5eaafcfb89ecc2fc8f41.wasm",
"vp_validator.wasm": "vp_validator.71a76c63818268e8c609661027519692de34d0d9d3ac22337115b269d9ed10a0.wasm"
"tx_bond.wasm": "tx_bond.aec5b289d8450c4e65939c5883f2868f9f77389c8dbf59a445e72e33faefc919.wasm",
"tx_change_validator_commission.wasm": "tx_change_validator_commission.1edf31292f2e5693e6193ca7d9cce7d004d726d972ceea0df7f7a437689939f9.wasm",
"tx_ibc.wasm": "tx_ibc.4fd06fe6f9edc763c9d860bc0d61947a5c2d6fb35bb8eb5110e476cf8d6c79b7.wasm",
"tx_init_account.wasm": "tx_init_account.9b74ae5630bf2fbf76a1225f5e0e09ff9caf136bfe8652bc4f5d5a03058e14f1.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.c65e1c614ca8389a1c9a8fcb0610fb79df6466b646d8a7881fe9031140a8dae0.wasm",
"tx_init_validator.wasm": "tx_init_validator.c1db05715b899c8137783a0e8a6d53da61821337f66da5af5e6cd8072a7f601f.wasm",
"tx_reveal_pk.wasm": "tx_reveal_pk.f0014d214e63424a433c6be6db6c79e74287cdc4b2549fd15d93e8f30ce42381.wasm",
"tx_transfer.wasm": "tx_transfer.a01e7c3b87edb1e60f0916b3037a2f966ccdf3a7d05ececa865375bc7630002f.wasm",
"tx_unbond.wasm": "tx_unbond.47e7f9e63d29062ef5f6053bf46d1c270574c63b7f3bacf6ca57a64a457ec432.wasm",
"tx_update_vp.wasm": "tx_update_vp.e6ed544a61534b5f317b5707e82217acf6a613a90f477988ddb8c1578f762997.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.885ead3f51aa2b697730ad645470b024963d4412c0a3d31a233ea2d2d3bfeef1.wasm",
"tx_withdraw.wasm": "tx_withdraw.646065e1c698e99c9e97fdd2532370575d416fcbb38c8e5d985e7e3bf914a883.wasm",
"vp_implicit.wasm": "vp_implicit.751ce0bbe7cd7ec4a6a4a6429f6324c40fe4630a39a1cc416501f911d19cc7e4.wasm",
"vp_masp.wasm": "vp_masp.3dd1b4906712ad66d784b39c9fad127d4a2a221533f706b8309db797d050053c.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.bf5b09d4fe21e1e9a1da59114a55c93772cf1f34f9de59a6c92781be5974c765.wasm",
"vp_token.wasm": "vp_token.6fe84584802e659d87d1839fa326a5208d738c14db85f29a44c6ac2f05371f98.wasm",
"vp_user.wasm": "vp_user.2bc988fa034a998c9e948a7f1bd8ca9681e8366646055e125117185a43b229ba.wasm",
"vp_validator.wasm": "vp_validator.42913ba7b50073ac862b2fcc10fc8fa04216abdd7fff12614bffaae464edccf4.wasm"
}
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.

0 comments on commit 0d4c521

Please sign in to comment.