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

Draft
wants to merge 5 commits into
base: albatross
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions mempool/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,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 @@ -96,17 +96,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 @@ -174,7 +174,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 @@ -208,8 +208,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
87 changes: 77 additions & 10 deletions primitives/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,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 @@ -289,11 +285,7 @@ mod serialization {
where
D: Deserializer<'de>,
{
let value: u64 = if deserializer.is_human_readable() {
Deserialize::deserialize(deserializer)?
} else {
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 @@ -315,3 +307,78 @@ 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 serde::{
de::{Error as DeError, Unexpected},
ser::Error as SerError,
Deserialize, Deserializer, Serialize, Serializer,
};

use super::*;

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;

let vec = hex::decode("0020000000000000").unwrap();
let vec = hex::decode("8080808080808010").unwrap();
let res: Result<Coin, DeserializeError> = Deserialize::deserialize_from_vec(&vec[..]);
match res {
Ok(coin) => panic!("Instead of failing, got {}", coin),
Expand Down
12 changes: 6 additions & 6 deletions primitives/transaction/src/account/vesting_contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use log::error;
use nimiq_keys::Address;
use nimiq_primitives::{account::AccountType, coin::Coin};
use nimiq_primitives::{account::AccountType, coin::CoinBe};
use nimiq_serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -76,8 +76,8 @@ pub struct CreationTransactionData {
pub start_time: u64,
#[serde(with = "nimiq_serde::fixint::be")]
pub time_step: u64,
pub step_amount: Coin,
pub total_amount: Coin,
pub step_amount: CoinBe,
pub total_amount: CoinBe,
}

impl CreationTransactionData {
Expand All @@ -92,8 +92,8 @@ impl CreationTransactionData {
owner,
start_time: 0,
time_step: u64::from_be_bytes(time_step),
step_amount: transaction.value,
total_amount: transaction.value,
step_amount: transaction.value.into(),
total_amount: transaction.value.into(),
})
} else if transaction.recipient_data.len() == Address::SIZE + 24 {
let (start_time, left_over) = <[u8; 8]>::deserialize_take(left_over)?;
Expand All @@ -104,7 +104,7 @@ impl CreationTransactionData {
start_time: u64::from_be_bytes(start_time),
time_step: u64::from_be_bytes(time_step),
step_amount,
total_amount: transaction.value,
total_amount: transaction.value.into(),
})
} else if transaction.recipient_data.len() == Address::SIZE + 32 {
// Create a vesting account with some instantly vested funds or additional funds considered.
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 @@ -15,7 +15,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 @@ -447,8 +450,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
6 changes: 3 additions & 3 deletions primitives/transaction/tests/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use nimiq_serde::{Deserialize, DeserializeError, Serialize};
use nimiq_test_log::test;
use nimiq_transaction::*;

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