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

Refactor - Bincode 2 #47

Open
wants to merge 6 commits into
base: develop
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
20 changes: 18 additions & 2 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exclude = ["**/tests/**", "**/examples/**", "**/benchmarks/**", "docs/**", ".hoo
[dependencies]
actix-rt = "2.8.0"
base64 = "0.20.0"
bincode = "1.3.3"
bincode = { version = "2.0.0-rc.3", features = ["serde"] }
bytes = "1.4.0"
colored = { version = "2.1.0", optional = true }
hex = "0.4.3"
Expand Down
12 changes: 6 additions & 6 deletions src/primitives/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::crypto::sha3_256::{self, Sha3_256};
use crate::crypto::sign_ed25519::PublicKey;
use crate::primitives::asset::Asset;
use crate::primitives::transaction::{Transaction, TxIn, TxOut};
use bincode::{deserialize, serialize};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
Expand Down Expand Up @@ -82,25 +81,25 @@ impl Block {

/// Sets the internal number of bits based on length
pub fn set_bits(&mut self) {
let bytes = Bytes::from(match serialize(&self) {
let bytes = match bincode::serde::encode_to_vec(&self, bincode::config::legacy()) {
Ok(bytes) => bytes,
Err(e) => {
warn!("Failed to serialize block: {:?}", e);
return;
}
});
};
self.header.bits = bytes.len();
}

/// Checks whether a block has hit its maximum size
pub fn is_full(&self) -> bool {
let bytes = Bytes::from(match serialize(&self) {
let bytes = match bincode::serde::encode_to_vec(&self, bincode::config::legacy()) {
Ok(bytes) => bytes,
Err(e) => {
warn!("Failed to serialize block: {:?}", e);
return false;
}
});
};
bytes.len() >= MAX_BLOCK_SIZE
}

Expand Down Expand Up @@ -143,7 +142,8 @@ pub fn gen_random_hash() -> String {
///
/// * `transactions` - Transactions to construct a merkle tree for
pub fn build_hex_txs_hash(transactions: &[String]) -> String {
let txs = match serialize(transactions) {
// TODO: This is bad, it won't produce the same result on 32-bit systems
let txs = match bincode::serde::encode_to_vec(transactions, bincode::config::legacy()) {
Ok(bytes) => bytes,
Err(e) => {
warn!("Failed to serialize transactions: {:?}", e);
Expand Down
11 changes: 0 additions & 11 deletions src/primitives/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::primitives::{
use crate::script::lang::Script;
use crate::script::{OpCodes, StackEntry};
use crate::utils::is_valid_amount;
use bincode::serialize;
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::fmt;

Expand Down Expand Up @@ -205,15 +203,6 @@ impl Transaction {
}
}

/// Get the total transaction size in bytes
pub fn get_total_size(&self) -> usize {
let bytes = match serialize(self) {
Ok(bytes) => bytes,
Err(_) => vec![],
};
bytes.len()
}

/// Gets the create asset assigned to this transaction, if it exists
fn get_create_asset(&self) -> Option<&Asset> {
let is_create = self.inputs.len() == 1
Expand Down
2 changes: 0 additions & 2 deletions src/script/interface_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use crate::utils::error_utils::*;
use crate::utils::transaction_utils::{
construct_address, construct_address_temp, construct_address_v0,
};
use bincode::de;
use bincode::serialize;
use bytes::Bytes;
use hex::encode;
use std::collections::BTreeMap;
Expand Down
2 changes: 0 additions & 2 deletions src/script/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::script::interface_ops::*;
use crate::script::{OpCodes, StackEntry};
use crate::utils::error_utils::*;
use crate::utils::transaction_utils::{construct_address, construct_address_for};
use bincode::serialize;
use bytes::Bytes;
use hex::encode;
use serde::{Deserialize, Serialize};
use tracing::{error, warn};
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::primitives::asset::TokenAmount;
pub mod druid_utils;
pub mod error_utils;
pub mod script_utils;
pub mod serialize_utils;
pub mod test_utils;
pub mod transaction_utils;

Expand Down
2 changes: 0 additions & 2 deletions src/utils/script_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ use crate::utils::transaction_utils::{
construct_address, construct_tx_hash, construct_tx_in_out_signable_hash,
construct_tx_in_signable_asset_hash, construct_tx_in_signable_hash,
};
use bincode::serialize;
use bytes::Bytes;
use hex::encode;
use ring::error;
use std::collections::{BTreeMap, BTreeSet};
Expand Down
Loading