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

Variable-sized Coin serialization #2373

Open
wants to merge 6 commits into
base: albatross
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion genesis-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ impl GenesisBuilder {
parent_election_hash,
interlink: Some(vec![]),
seed,
extra_data: supply.serialize_to_vec(),
extra_data: u64::from(supply).to_be_bytes().to_vec(),
state_root,
body_root,
diff_root: TreeProof::empty().root_hash(),
Expand Down
4 changes: 2 additions & 2 deletions mempool/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,8 @@ async fn mempool_tps() {
let mut prev_txn = txns.first().expect("Is vector empty?").clone();
for txn in txns {
assert!(
prev_txn.fee >= txn.fee,
"Transactions in mempool are not ordered by fee"
prev_txn.fee_per_byte() >= txn.fee_per_byte(),
"Transactions in mempool are not ordered by fee per byte"
);
prev_txn = txn.clone();
}
Expand Down
8 changes: 4 additions & 4 deletions primitives/account/src/account/vesting_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ impl AccountTransactionInteraction for VestingContract {
owner: data.owner.clone(),
start_time: data.start_time,
time_step: data.time_step,
step_amount: data.step_amount,
total_amount: data.total_amount,
step_amount: data.step_amount.into(),
total_amount: data.total_amount.into(),
});

Ok(Account::Vesting(VestingContract {
balance: initial_balance + transaction.value,
owner: data.owner,
start_time: data.start_time,
time_step: data.time_step,
step_amount: data.step_amount,
total_amount: data.total_amount,
step_amount: data.step_amount.into(),
total_amount: data.total_amount.into(),
}))
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/account/tests/htlc_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use nimiq_transaction::{
SignatureProof, Transaction,
};

const HTLC: &str = "00000000000000001b215589344cf570d36bec770825eae30b73213924786862babbdb05e7c4430612135eb2a836812303daebe368963c60d22098a5e9f1ebcb8e54d0b7beca942a2a0a9d95391804fe8f0100000000000296350000000000000001";
const HTLC: &str = "001b215589344cf570d36bec770825eae30b73213924786862babbdb05e7c4430612135eb2a836812303daebe368963c60d22098a5e9f1ebcb8e54d0b7beca942a2a0a9d95391804fe8f01000000000002963501";

fn prepare_outgoing_transaction() -> (
HashedTimeLockedContract,
Expand Down
10 changes: 5 additions & 5 deletions primitives/account/tests/vesting_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use nimiq_database::traits::Database;
use nimiq_keys::{Address, KeyPair};
use nimiq_primitives::{
account::{AccountError, AccountType},
coin::Coin,
coin::{Coin, CoinBe},
networks::NetworkId,
transaction::TransactionError,
};
Expand All @@ -18,7 +18,7 @@ use nimiq_test_utils::{accounts_revert::TestCommitRevert, test_rng::test_rng};
use nimiq_transaction::{SignatureProof, Transaction};
use nimiq_utils::key_rng::SecureGenerate;

const CONTRACT: &str = "00002fbf9bd9c800fd34ab7265a0e48c454ccbf4c9c61dfdf68f9a220000000000000001000000000003f480000002632e314a0000002fbf9bd9c800";
const CONTRACT: &str = "8090e7def9f70bfd34ab7265a0e48c454ccbf4c9c61dfdf68f9a220000000000000001000000000003f4808094c5f1b24c8090e7def9f70b";

fn init_tree() -> (TestCommitRevert, VestingContract, KeyPair, KeyPair) {
let mut rng = test_rng(true);
Expand Down Expand Up @@ -172,7 +172,7 @@ fn it_can_create_contract_from_transaction() {
Serialize::serialize_to_writer(&owner, &mut data);
Serialize::serialize_to_writer(&0u64.to_be_bytes(), &mut data);
Serialize::serialize_to_writer(&100u64.to_be_bytes(), &mut data);
Serialize::serialize_to_writer(&Coin::try_from(50).unwrap(), &mut data);
Serialize::serialize_to_writer(&CoinBe::from(Coin::try_from(50).unwrap()), &mut data);
tx.recipient_data = data;
tx.recipient = tx.contract_creation_address();

Expand Down Expand Up @@ -206,8 +206,8 @@ fn it_can_create_contract_from_transaction() {
Serialize::serialize_to_writer(&owner, &mut data);
Serialize::serialize_to_writer(&0u64.to_be_bytes(), &mut data);
Serialize::serialize_to_writer(&100u64.to_be_bytes(), &mut data);
Serialize::serialize_to_writer(&Coin::try_from(50).unwrap(), &mut data);
Serialize::serialize_to_writer(&Coin::try_from(150).unwrap(), &mut data);
Serialize::serialize_to_writer(&CoinBe::from(Coin::try_from(50).unwrap()), &mut data);
Serialize::serialize_to_writer(&CoinBe::from(Coin::try_from(150).unwrap()), &mut data);
tx.recipient_data = data;
tx.recipient = tx.contract_creation_address();

Expand Down
102 changes: 87 additions & 15 deletions primitives/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl FromStr for Coin {

#[cfg(feature = "serde-derive")]
mod serialization {
use nimiq_serde::SerializedSize;
use nimiq_serde::SerializedMaxSize;
use serde::{
de::{Error as DeError, Unexpected},
ser::Error as SerError,
Expand All @@ -268,8 +268,10 @@ mod serialization {

use super::*;

impl SerializedSize for Coin {
const SIZE: usize = 8;
impl SerializedMaxSize for Coin {
// u64::MAX takes up 10 bytes, but Coin is limited to Javascript's Number.MAX_SAFE_INTEGER,
// which only takes up 8 bytes.
const MAX_SIZE: usize = 8;
}

impl Serialize for Coin {
Expand All @@ -278,11 +280,7 @@ mod serialization {
S: Serializer,
{
if self.0 <= Coin::MAX_SAFE_VALUE {
if serializer.is_human_readable() {
self.0.serialize(serializer)
} else {
nimiq_serde::fixint::be::serialize(&self.0, serializer)
}
self.0.serialize(serializer)
} else {
Err(S::Error::custom("Overflow detected for a Coin value"))
}
Expand All @@ -294,13 +292,7 @@ mod serialization {
where
D: Deserializer<'de>,
{
let value: u64 = if deserializer.is_human_readable() {
Deserialize::deserialize(deserializer)?
} else {
// No need to fuzz, it just delegates to
// `nimiq_serde::fixint::be` and `Coin::try_from` looks sane.
nimiq_serde::fixint::be::deserialize(deserializer)?
};
let value: u64 = Deserialize::deserialize(deserializer)?;
Coin::try_from(value).map_err(|_| {
D::Error::invalid_value(
Unexpected::Unsigned(value),
Expand All @@ -322,3 +314,83 @@ mod serialization {
}
}
}

/// A newtype around [`Coin`] that serializes as a fixed-size integer in big-endian order.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Default, Hash)]
pub struct CoinBe(Coin);

#[cfg(feature = "serde-derive")]
mod serialization_fixint_be {
use nimiq_serde::SerializedSize;
use serde::{
de::{Error as DeError, Unexpected},
ser::Error as SerError,
Deserialize, Deserializer, Serialize, Serializer,
};

use super::*;

impl SerializedSize for CoinBe {
const SIZE: usize = 8;
}

impl Serialize for CoinBe {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if serializer.is_human_readable() {
self.0.serialize(serializer)
} else if self.0 .0 <= Coin::MAX_SAFE_VALUE {
nimiq_serde::fixint::be::serialize(&self.0 .0, serializer)
} else {
Err(S::Error::custom("Overflow detected for a Coin value"))
}
}
}

impl<'de> Deserialize<'de> for CoinBe {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let coin: Coin = if deserializer.is_human_readable() {
Deserialize::deserialize(deserializer)?
} else {
let value: u64 = nimiq_serde::fixint::be::deserialize(deserializer)?;
Coin::try_from(value).map_err(|_| {
D::Error::invalid_value(
Unexpected::Unsigned(value),
&"An u64 below the Coin maximum value",
)
})?
};
Ok(CoinBe(coin))
}
}

// Test must live here as we cannot create an out-of-range `CoinBe` from the
// outside.
#[test]
fn test_serialize_out_of_bounds() {
let mut vec = Vec::with_capacity(8);
let res =
nimiq_serde::Serialize::serialize_to_writer(&CoinBe(Coin(9007199254740992)), &mut vec);
match res {
Ok(_) => panic!("Didn't fail"),
Err(err) => assert_eq!(err.kind(), std::io::ErrorKind::Other),
}
}
}

impl From<Coin> for CoinBe {
fn from(coin: Coin) -> Self {
CoinBe(coin)
}
}

impl From<CoinBe> for Coin {
fn from(coin_be: CoinBe) -> Self {
coin_be.0
}
}
16 changes: 8 additions & 8 deletions primitives/tests/coin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ impl NonFailingTest {
}

static NON_FAILING_TESTS: [NonFailingTest; 7] = [
NonFailingTest::new("0000000000000000", 0u64),
NonFailingTest::new("0000000000000001", 1u64),
NonFailingTest::new("0000000000000005", 5u64),
NonFailingTest::new("0000000100000005", 4294967301),
NonFailingTest::new("000000000001e240", 123456u64),
NonFailingTest::new("0000001234561234", 78187467316u64),
NonFailingTest::new("001fffffffffffff", Coin::MAX_SAFE_VALUE),
NonFailingTest::new("00", 0u64),
NonFailingTest::new("01", 1u64),
NonFailingTest::new("05", 5u64),
NonFailingTest::new("8580808010", 4294967301),
NonFailingTest::new("c0c407", 123456u64),
NonFailingTest::new("b4a4d8a2a302", 78187467316u64),
NonFailingTest::new("ffffffffffffff0f", Coin::MAX_SAFE_VALUE),
];

#[test]
Expand All @@ -45,7 +45,7 @@ fn test_non_failing() {
fn test_deserialize_out_of_bounds() {
use nimiq_serde::DeserializeError;

match Coin::deserialize_from_vec(&hex::decode("0020000000000000").unwrap()) {
match Coin::deserialize_from_vec(&hex::decode("8080808080808010").unwrap()) {
Ok(coin) => panic!("Instead of failing, got {}", coin),
Err(err) => assert_eq!(err, DeserializeError::serde_custom()),
}
Expand Down
21 changes: 12 additions & 9 deletions primitives/transaction/src/account/vesting_contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use nimiq_keys::Address;
use nimiq_primitives::{account::AccountType, coin::Coin};
use nimiq_primitives::{
account::AccountType,
coin::{Coin, CoinBe},
};
use nimiq_serde::{Deserialize, Serialize, SerializedSize};

use crate::{
Expand Down Expand Up @@ -77,9 +80,9 @@ pub struct CreationTransactionData {
/// The frequency at which funds are released.
pub time_step: u64,
/// The amount released at each [`time_step`](Self::time_step).
pub step_amount: Coin,
pub step_amount: CoinBe,
/// Initially locked balance.
pub total_amount: Coin,
pub total_amount: CoinBe,
}

#[derive(Deserialize, Serialize, SerializedSize)]
Expand All @@ -98,7 +101,7 @@ struct CreationTransactionData24 {
#[serde(with = "nimiq_serde::fixint::be")]
#[serialize_size(fixed_size)]
pub time_step: u64,
pub step_amount: Coin,
pub step_amount: CoinBe,
}
#[derive(Deserialize, Serialize, SerializedSize)]
struct CreationTransactionData32 {
Expand All @@ -109,8 +112,8 @@ struct CreationTransactionData32 {
#[serde(with = "nimiq_serde::fixint::be")]
#[serialize_size(fixed_size)]
pub time_step: u64,
pub step_amount: Coin,
pub total_amount: Coin,
pub step_amount: CoinBe,
pub total_amount: CoinBe,
}

impl CreationTransactionData {
Expand All @@ -124,8 +127,8 @@ impl CreationTransactionData {
owner,
start_time: 0,
time_step,
step_amount: tx_value,
total_amount: tx_value,
step_amount: tx_value.into(),
total_amount: tx_value.into(),
}
}
CreationTransactionData24::SIZE => {
Expand All @@ -140,7 +143,7 @@ impl CreationTransactionData {
start_time,
time_step,
step_amount,
total_amount: tx_value,
total_amount: tx_value.into(),
}
}
CreationTransactionData32::SIZE => {
Expand Down
9 changes: 6 additions & 3 deletions primitives/transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use nimiq_hash::{Blake2bHash, Hash, SerializeContent};
use nimiq_keys::{Address, PublicKey};
use nimiq_network_interface::network::Topic;
use nimiq_primitives::{
account::AccountType, coin::Coin, networks::NetworkId, policy::Policy,
account::AccountType,
coin::{Coin, CoinBe},
networks::NetworkId,
policy::Policy,
transaction::TransactionError,
};
use nimiq_serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -591,8 +594,8 @@ impl SerializeContent for Transaction {
self.sender_type.serialize_to_writer(writer)?;
self.recipient.serialize_to_writer(writer)?;
self.recipient_type.serialize_to_writer(writer)?;
self.value.serialize_to_writer(writer)?;
self.fee.serialize_to_writer(writer)?;
CoinBe::from(self.value).serialize_to_writer(writer)?;
CoinBe::from(self.fee).serialize_to_writer(writer)?;
writer.write_all(&self.validity_start_height.to_be_bytes())?;
self.network_id.serialize_to_writer(writer)?;
self.flags.serialize_to_writer(writer)?;
Expand Down
4 changes: 2 additions & 2 deletions primitives/transaction/src/reward.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use nimiq_keys::Address;
use nimiq_primitives::coin::Coin;
use nimiq_serde::{Deserialize, Serialize, SerializedSize};
use nimiq_serde::{Deserialize, Serialize, SerializedMaxSize};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, SerializedSize)]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, SerializedMaxSize)]
pub struct RewardTransaction {
/// The validator address of the rewarded validator.
pub validator_address: Address,
Expand Down
6 changes: 3 additions & 3 deletions primitives/transaction/tests/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use nimiq_test_log::test;
use nimiq_transaction::*;
use nimiq_utils::merkle::MerklePath;

const EXTENDED_TRANSACTION: &str = "014a88aaad038f9b8248865c4b9249efc554960e160000ad25610feb43d75307763d3f010822a757027429000000000746a52880000000000000000000000136c32a00e2010e4712ea5b1703873529dd195b2b8f014c295ab352a12e3332d8f30cfc2db9680480c77af04feb0d89bdb5d5d9432d4ca17866abf3b4d6c1a05fa0fbdaed056181eaff68db063c759a0964bceb5f262f7335ed97c5471e773429926c106eae50881b998c516581e6d93933bb92feb2edcdbdb1b118fc000f8f1df8715538840b79e74721c631efe0f9977ccd88773b022a07b3935f2e8546e20ed7f7e1a0c77da7a7e1737bf0625170610846792ea16bc0f6d8cf9ded8a9da1d467f4191a3a97d5fc17d08d699dfa486787f70eb09e2cdbd5b63fd1a8357e1cd24cd37aa2f3408400";
const BASIC_TRANSACTION: &str = "00000222666efadc937148a6d61589ce6d4aeecca97fda4c32348d294eab582f14a0754d1260f15bea0e8fb07ab18f45301483599e34000000000000c350000000000000008a00019640023fecb82d3aef4be76853d5c5b263754b7d495d9838f6ae5df60cf3addd3512a82988db0056059c7a52ae15285983ef0db8229ae446c004559147686d28f0a30a";
const INVALID_EXTENDED_TRANSACTION: &str = "014a88aaad038f9b8248865c4b9249efc554960e16000000ad25610feb43d75307763d3f010822a75702742900000000000746a52880000000000000000000000136c32a0500e20e4712ea5b1703873529dd195b2b8f014c295ab352a12e3332d8f30cfc2db9680480c77af04feb0d89bdb5d5d9432d4ca17866abf3b4d6c1a05fa0fbdaed056181eaff68db063c759a0964bceb5f262f7335ed97c5471e773429926c106eae50881b998c516581e6d93933bb92feb2edcdbdb1b118fc000f8f1df8715538840b79e74721c631efe0f9977ccd88773b022a07b3935f2e8546e20ed7f7e1a0c77da7a7e1737bf0625170610846792ea16bc0f6d8cf9ded8a9da1d467f4191a3a97d5fc17d08d699dfa486787f70eb09e2cdbd5b63fd1a8357e1cd24cd37aa2f3408400";
const EXTENDED_TRANSACTION: &str = "014a88aaad038f9b8248865c4b9249efc554960e160000ad25610feb43d75307763d3f010822a75702742900008080a2a9eae80100000136c32a00e2010e4712ea5b1703873529dd195b2b8f014c295ab352a12e3332d8f30cfc2db9680480c77af04feb0d89bdb5d5d9432d4ca17866abf3b4d6c1a05fa0fbdaed056181eaff68db063c759a0964bceb5f262f7335ed97c5471e773429926c106eae50881b998c516581e6d93933bb92feb2edcdbdb1b118fc000f8f1df8715538840b79e74721c631efe0f9977ccd88773b022a07b3935f2e8546e20ed7f7e1a0c77da7a7e1737bf0625170610846792ea16bc0f6d8cf9ded8a9da1d467f4191a3a97d5fc17d08d699dfa486787f70eb09e2cdbd5b63fd1a8357e1cd24cd37aa2f3408400";
const BASIC_TRANSACTION: &str = "00000222666efadc937148a6d61589ce6d4aeecca97fda4c32348d294eab582f14a0754d1260f15bea0e8fb07ab18f45301483599e34d086038a0100019640023fecb82d3aef4be76853d5c5b263754b7d495d9838f6ae5df60cf3addd3512a82988db0056059c7a52ae15285983ef0db8229ae446c004559147686d28f0a30a";
const INVALID_EXTENDED_TRANSACTION: &str = "014a88aaad038f9b8248865c4b9249efc554960e16000000ad25610feb43d75307763d3f010822a7570274290000008080a2a9eae80100000136c32a0500e20e4712ea5b1703873529dd195b2b8f014c295ab352a12e3332d8f30cfc2db9680480c77af04feb0d89bdb5d5d9432d4ca17866abf3b4d6c1a05fa0fbdaed056181eaff68db063c759a0964bceb5f262f7335ed97c5471e773429926c106eae50881b998c516581e6d93933bb92feb2edcdbdb1b118fc000f8f1df8715538840b79e74721c631efe0f9977ccd88773b022a07b3935f2e8546e20ed7f7e1a0c77da7a7e1737bf0625170610846792ea16bc0f6d8cf9ded8a9da1d467f4191a3a97d5fc17d08d699dfa486787f70eb09e2cdbd5b63fd1a8357e1cd24cd37aa2f3408400";

#[test]
fn it_can_deserialize_historic_transaction() {
Expand Down
Loading
Loading