diff --git a/Cargo.lock b/Cargo.lock index a4b024cc3ef..73ea1f674c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -825,12 +825,12 @@ dependencies = [ [[package]] name = "borsh" -version = "0.10.2" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40f9ca3698b2e4cb7c15571db0abc5551dca417a21ae8140460b50309bb2cc62" +checksum = "4e6cb63579996213e822f6d828b0a47e1d23b1e8708f52d18a6b1af5670dd207" dependencies = [ - "borsh-derive 0.10.2", - "hashbrown 0.13.2", + "borsh-derive 1.0.0", + "cfg_aliases", ] [[package]] @@ -839,24 +839,25 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6441c552f230375d18e3cc377677914d2ca2b0d36e52129fe15450a2dce46775" dependencies = [ - "borsh-derive-internal 0.9.3", - "borsh-schema-derive-internal 0.9.3", - "proc-macro-crate", + "borsh-derive-internal", + "borsh-schema-derive-internal", + "proc-macro-crate 0.1.5", "proc-macro2", "syn 1.0.103", ] [[package]] name = "borsh-derive" -version = "0.10.2" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "598b3eacc6db9c3ee57b22707ad8f6a8d2f6d442bfe24ffeb8cbb70ca59e6a35" +checksum = "35b4db62e0515621636e47f425d78a40bdea94c2d23713428fb12194cf5459a4" dependencies = [ - "borsh-derive-internal 0.10.2", - "borsh-schema-derive-internal 0.10.2", - "proc-macro-crate", + "once_cell", + "proc-macro-crate 1.3.0", "proc-macro2", - "syn 1.0.103", + "quote", + "syn 2.0.15", + "syn_derive", ] [[package]] @@ -870,17 +871,6 @@ dependencies = [ "syn 1.0.103", ] -[[package]] -name = "borsh-derive-internal" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "186b734fa1c9f6743e90c95d7233c9faab6360d1a96d4ffa19d9cfd1e9350f8a" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.103", -] - [[package]] name = "borsh-schema-derive-internal" version = "0.9.3" @@ -892,17 +882,6 @@ dependencies = [ "syn 1.0.103", ] -[[package]] -name = "borsh-schema-derive-internal" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99b7ff1008316626f485991b960ade129253d4034014616b94f309a15366cc49" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.103", -] - [[package]] name = "brotli" version = "3.3.4" @@ -1078,6 +1057,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "chainsync-loadtest" version = "0.0.0" @@ -1239,7 +1224,7 @@ name = "cold-store-tool" version = "0.0.0" dependencies = [ "anyhow", - "borsh 0.10.2", + "borsh 1.0.0", "clap 4.2.4", "near-chain-configs", "near-epoch-manager", @@ -2365,7 +2350,7 @@ dependencies = [ name = "genesis-populate" version = "0.0.0" dependencies = [ - "borsh 0.10.2", + "borsh 1.0.0", "clap 4.2.4", "indicatif", "near-chain", @@ -2783,7 +2768,7 @@ dependencies = [ "actix-rt", "anyhow", "assert_matches", - "borsh 0.10.2", + "borsh 1.0.0", "chrono", "clap 4.2.4", "futures", @@ -3343,7 +3328,7 @@ version = "0.0.0" dependencies = [ "arbitrary", "bolero", - "borsh 0.10.2", + "borsh 1.0.0", "serde", "serde_json", ] @@ -3352,7 +3337,7 @@ dependencies = [ name = "near-account-id-fuzz" version = "0.0.0" dependencies = [ - "borsh 0.10.2", + "borsh 1.0.0", "libfuzzer-sys", "near-account-id", "serde_json", @@ -3373,7 +3358,7 @@ name = "near-amend-genesis" version = "0.0.0" dependencies = [ "anyhow", - "borsh 0.10.2", + "borsh 1.0.0", "clap 4.2.4", "near-chain", "near-chain-configs", @@ -3427,7 +3412,7 @@ dependencies = [ "actix", "ansi_term", "assert_matches", - "borsh 0.10.2", + "borsh 1.0.0", "bytesize", "chrono", "crossbeam-channel", @@ -3495,7 +3480,7 @@ version = "0.0.0" dependencies = [ "actix", "assert_matches", - "borsh 0.10.2", + "borsh 1.0.0", "chrono", "derive-enum-from-into", "derive_more", @@ -3540,7 +3525,7 @@ dependencies = [ "anyhow", "assert_matches", "async-trait", - "borsh 0.10.2", + "borsh 1.0.0", "chrono", "cloud-storage", "derive_more", @@ -3616,7 +3601,7 @@ name = "near-crypto" version = "0.0.0" dependencies = [ "blake2", - "borsh 0.10.2", + "borsh 1.0.0", "bs58", "c2-chacha", "curve25519-dalek", @@ -3678,7 +3663,7 @@ dependencies = [ name = "near-epoch-manager" version = "0.0.0" dependencies = [ - "borsh 0.10.2", + "borsh 1.0.0", "chrono", "itertools", "near-cache", @@ -3701,7 +3686,7 @@ name = "near-flat-storage" version = "0.0.0" dependencies = [ "anyhow", - "borsh 0.10.2", + "borsh 1.0.0", "clap 4.2.4", "near-chain", "near-chain-configs", @@ -3865,7 +3850,7 @@ version = "0.0.0" dependencies = [ "actix", "awc", - "borsh 0.10.2", + "borsh 1.0.0", "futures", "near-actix-test-utils", "near-chain-configs", @@ -3899,7 +3884,7 @@ dependencies = [ "actix", "anyhow", "async-trait", - "borsh 0.10.2", + "borsh 1.0.0", "bs58", "clap 4.2.4", "ed25519-dalek", @@ -3943,7 +3928,7 @@ dependencies = [ "arc-swap", "assert_matches", "async-trait", - "borsh 0.10.2", + "borsh 1.0.0", "bytes", "bytesize", "chrono", @@ -4068,7 +4053,7 @@ dependencies = [ name = "near-pool" version = "0.0.0" dependencies = [ - "borsh 0.10.2", + "borsh 1.0.0", "near-crypto", "near-o11y", "near-primitives", @@ -4084,7 +4069,7 @@ dependencies = [ "assert_matches", "base64 0.21.0", "bencher", - "borsh 0.10.2", + "borsh 1.0.0", "bytesize", "cfg-if 1.0.0", "chrono", @@ -4121,7 +4106,7 @@ version = "0.0.0" dependencies = [ "arbitrary", "base64 0.21.0", - "borsh 0.10.2", + "borsh 1.0.0", "bs58", "derive_more", "enum-map", @@ -4226,7 +4211,7 @@ dependencies = [ "anyhow", "assert_matches", "bencher", - "borsh 0.10.2", + "borsh 1.0.0", "bytesize", "crossbeam", "derive_more", @@ -4392,7 +4377,7 @@ dependencies = [ "assert_matches", "base64 0.21.0", "bolero", - "borsh 0.10.2", + "borsh 1.0.0", "clap 4.2.4", "ed25519-dalek", "enum-map", @@ -4549,7 +4534,7 @@ dependencies = [ "anyhow", "awc", "bencher", - "borsh 0.10.2", + "borsh 1.0.0", "chrono", "cloud-storage", "dirs", @@ -4681,7 +4666,7 @@ name = "node-runtime" version = "0.0.0" dependencies = [ "assert_matches", - "borsh 0.10.2", + "borsh 1.0.0", "enum-map", "hex", "indicatif", @@ -4718,6 +4703,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nom8" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae01545c9c7fc4486ab7debaf2aad7003ac19431791868fb2e8066df97fad2f8" +dependencies = [ + "memchr", +] + [[package]] name = "ntapi" version = "0.3.7" @@ -4811,9 +4805,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.13.1" +version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "oorandom" @@ -5254,6 +5248,16 @@ dependencies = [ "toml", ] +[[package]] +name = "proc-macro-crate" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66618389e4ec1c7afe67d51a9bf34ff9236480f8d51e7489b7d5ab0303c13f34" +dependencies = [ + "once_cell", + "toml_edit", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -5839,7 +5843,7 @@ name = "runtime-params-estimator" version = "0.0.0" dependencies = [ "anyhow", - "borsh 0.10.2", + "borsh 1.0.0", "bs58", "bytesize", "cfg-if 1.0.0", @@ -6528,7 +6532,7 @@ dependencies = [ name = "speedy_sync" version = "0.0.0" dependencies = [ - "borsh 0.10.2", + "borsh 1.0.0", "clap 4.2.4", "near-chain", "near-chain-configs", @@ -6560,7 +6564,7 @@ dependencies = [ "actix", "ansi_term", "anyhow", - "borsh 0.10.2", + "borsh 1.0.0", "chrono", "clap 4.2.4", "cloud-storage", @@ -6713,6 +6717,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn_derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8128874d02f9a114ade6d9ad252078cb32d3cb240e26477ac73d7e9c495c605e" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "synstructure" version = "0.12.6" @@ -7048,6 +7064,23 @@ dependencies = [ "serde", ] +[[package]] +name = "toml_datetime" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4553f467ac8e3d374bc9a177a26801e5d0f9b211aa1673fb137a403afd1c9cf5" + +[[package]] +name = "toml_edit" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c59d8dd7d0dcbc6428bf7aa2f0e823e26e43b3c9aca15bbc9475d23e5fa12b" +dependencies = [ + "indexmap", + "nom8", + "toml_datetime", +] + [[package]] name = "tonic" version = "0.6.2" diff --git a/Cargo.toml b/Cargo.toml index 6016943fd26..6838d091593 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -110,7 +110,7 @@ bitflags = "1.2" blake2 = "0.9.1" bn = { package = "zeropool-bn", version = "0.5.11" } bolero = "0.8.0" -borsh = { version = "0.10.2", features = ["rc"] } +borsh = { version = "1.0.0", features = ["derive", "rc"] } bs58 = "0.4" bytes = "1" bytesize = { version = "1.1", features = ["serde"] } diff --git a/chain/chain/src/blocks_delay_tracker.rs b/chain/chain/src/blocks_delay_tracker.rs index d2cb88ec852..1e40038d3b1 100644 --- a/chain/chain/src/blocks_delay_tracker.rs +++ b/chain/chain/src/blocks_delay_tracker.rs @@ -1,7 +1,6 @@ use chrono::DateTime; use near_epoch_manager::EpochManagerAdapter; use near_primitives::block::{Block, Tip}; -use near_primitives::borsh::maybestd::collections::hash_map::Entry; use near_primitives::hash::CryptoHash; use near_primitives::sharding::{ChunkHash, ShardChunkHeader}; use near_primitives::static_clock::StaticClock; @@ -10,7 +9,7 @@ use near_primitives::views::{ BlockProcessingInfo, BlockProcessingStatus, ChainProcessingInfo, ChunkProcessingInfo, ChunkProcessingStatus, DroppedReason, }; -use std::collections::{BTreeMap, HashMap}; +use std::collections::{hash_map::Entry, BTreeMap, HashMap}; use std::mem; use std::time::Instant; use tracing::error; diff --git a/chain/chain/src/chain.rs b/chain/chain/src/chain.rs index f4e77a7c5a8..56749670e26 100644 --- a/chain/chain/src/chain.rs +++ b/chain/chain/src/chain.rs @@ -20,7 +20,7 @@ use crate::validate::{ }; use crate::{byzantine_assert, create_light_client_block_view, Doomslug}; use crate::{metrics, DoomslugThresholdMode}; -use borsh::BorshSerialize; + use chrono::Duration; use crossbeam_channel::{unbounded, Receiver, Sender}; use itertools::Itertools; @@ -1350,8 +1350,8 @@ impl Chain { let other_header = self.get_block_header(block_hashes.iter().next().unwrap())?; challenges.push(ChallengeBody::BlockDoubleSign(BlockDoubleSign { - left_block_header: header.try_to_vec().expect("Failed to serialize"), - right_block_header: other_header.try_to_vec().expect("Failed to serialize"), + left_block_header: borsh::to_vec(&header).expect("Failed to serialize"), + right_block_header: borsh::to_vec(&other_header).expect("Failed to serialize"), })); } } @@ -1696,7 +1696,7 @@ impl Chain { if let Some(encoded_chunk) = self.store.is_invalid_chunk(&chunk_header.chunk_hash())? { let merkle_paths = Block::compute_chunk_headers_root(block.chunks().iter()).1; let chunk_proof = ChunkProofs { - block_header: block.header().try_to_vec().expect("Failed to serialize"), + block_header: borsh::to_vec(&block.header()).expect("Failed to serialize"), merkle_proof: merkle_paths[shard_id].clone(), chunk: MaybeEncodedShardChunk::Encoded(EncodedShardChunk::clone( &encoded_chunk, @@ -3089,7 +3089,7 @@ impl Chain { sync_hash: CryptoHash, ) -> Result { // Check cache - let key = StateHeaderKey(shard_id, sync_hash).try_to_vec()?; + let key = borsh::to_vec(&StateHeaderKey(shard_id, sync_hash))?; if let Ok(Some(header)) = self.store.store().get_ser(DBCol::StateHeaders, &key) { return Ok(header); } @@ -3118,7 +3118,7 @@ impl Chain { %sync_hash) .entered(); // Check cache - let key = StatePartKey(sync_hash, shard_id, part_id).try_to_vec()?; + let key = borsh::to_vec(&StatePartKey(sync_hash, shard_id, part_id))?; if let Ok(Some(state_part)) = self.store.store().get(DBCol::StateParts, &key) { return Ok(state_part.into()); } @@ -3330,7 +3330,7 @@ impl Chain { // Saving the header data. let mut store_update = self.store.store().store_update(); - let key = StateHeaderKey(shard_id, sync_hash).try_to_vec()?; + let key = borsh::to_vec(&StateHeaderKey(shard_id, sync_hash))?; store_update.set_ser(DBCol::StateHeaders, &key, &shard_state_header)?; store_update.commit()?; @@ -3365,7 +3365,7 @@ impl Chain { // Saving the part data. let mut store_update = self.store.store().store_update(); - let key = StatePartKey(sync_hash, shard_id, part_id.idx).try_to_vec()?; + let key = borsh::to_vec(&StatePartKey(sync_hash, shard_id, part_id.idx))?; store_update.set(DBCol::StateParts, &key, data); store_update.commit()?; Ok(()) @@ -3822,8 +3822,8 @@ impl Chain { // .unwrap(); // let partial_state = apply_result.proof.unwrap().nodes; Ok(ChunkState { - prev_block_header: prev_block.header().try_to_vec()?, - block_header: block.header().try_to_vec()?, + prev_block_header: borsh::to_vec(&prev_block.header())?, + block_header: borsh::to_vec(&block.header())?, prev_merkle_proof: prev_merkle_proofs[chunk_shard_id as usize].clone(), merkle_proof: merkle_proofs[chunk_shard_id as usize].clone(), prev_chunk, @@ -4069,7 +4069,7 @@ impl Chain { if !validate_transactions_order(transactions) { let merkle_paths = Block::compute_chunk_headers_root(block.chunks().iter()).1; let chunk_proof = ChunkProofs { - block_header: block.header().try_to_vec().expect("Failed to serialize"), + block_header: borsh::to_vec(&block.header()).expect("Failed to serialize"), merkle_proof: merkle_paths[shard_id as usize].clone(), chunk: MaybeEncodedShardChunk::Decoded(chunk), }; @@ -4921,7 +4921,7 @@ impl Chain { shard_receipts .into_iter() .map(|(i, rs)| { - let bytes = (i, rs).try_to_vec().unwrap(); + let bytes = borsh::to_vec(&(i, rs)).unwrap(); hash(&bytes) }) .collect() diff --git a/chain/chain/src/store.rs b/chain/chain/src/store.rs index 2ba08539af4..ecbb6d71d45 100644 --- a/chain/chain/src/store.rs +++ b/chain/chain/src/store.rs @@ -881,7 +881,7 @@ impl ChainStore { shard_id: ShardId, block_hash: CryptoHash, ) -> Result { - let key = StateHeaderKey(shard_id, block_hash).try_to_vec()?; + let key = borsh::to_vec(&StateHeaderKey(shard_id, block_hash))?; match self.store.get_ser(DBCol::StateHeaders, &key) { Ok(Some(header)) => Ok(header), _ => Err(Error::Other("Cannot get shard_state_header".into())), @@ -2400,7 +2400,7 @@ impl<'a> ChainStoreUpdate<'a> { let state_num_parts = get_num_state_parts(shard_state_header.state_root_node().memory_usage); self.gc_col_state_parts(block_hash, shard_id, state_num_parts)?; - let key = StateHeaderKey(shard_id, block_hash).try_to_vec()?; + let key = borsh::to_vec(&StateHeaderKey(shard_id, block_hash))?; self.gc_col(DBCol::StateHeaders, &key); } } @@ -2500,7 +2500,7 @@ impl<'a> ChainStoreUpdate<'a> { let state_num_parts = get_num_state_parts(shard_state_header.state_root_node().memory_usage); self.gc_col_state_parts(block_hash, shard_id, state_num_parts)?; - let state_header_key = StateHeaderKey(shard_id, block_hash).try_to_vec()?; + let state_header_key = borsh::to_vec(&StateHeaderKey(shard_id, block_hash))?; self.gc_col(DBCol::StateHeaders, &state_header_key); } @@ -2610,7 +2610,7 @@ impl<'a> ChainStoreUpdate<'a> { num_parts: u64, ) -> Result<(), Error> { for part_id in 0..num_parts { - let key = StatePartKey(sync_hash, shard_id, part_id).try_to_vec()?; + let key = borsh::to_vec(&StatePartKey(sync_hash, shard_id, part_id))?; self.gc_col(DBCol::StateParts, &key); } Ok(()) @@ -3036,7 +3036,7 @@ impl<'a> ChainStoreUpdate<'a> { // Increase transaction refcounts for all included txs for tx in chunk.transactions().iter() { - let bytes = tx.try_to_vec().expect("Borsh cannot fail"); + let bytes = borsh::to_vec(&tx).expect("Borsh cannot fail"); store_update.increment_refcount( DBCol::Transactions, tx.get_hash().as_ref(), @@ -3046,7 +3046,7 @@ impl<'a> ChainStoreUpdate<'a> { // Increase receipt refcounts for all included receipts for receipt in chunk.prev_outgoing_receipts().iter() { - let bytes = receipt.try_to_vec().expect("Borsh cannot fail"); + let bytes = borsh::to_vec(&receipt).expect("Borsh cannot fail"); store_update.increment_refcount( DBCol::Receipts, receipt.get_hash().as_ref(), @@ -3116,7 +3116,7 @@ impl<'a> ChainStoreUpdate<'a> { )?; } for (receipt_id, shard_id) in self.chain_store_cache_update.receipt_id_to_shard_id.iter() { - let data = shard_id.try_to_vec()?; + let data = borsh::to_vec(&shard_id)?; store_update.increment_refcount(DBCol::ReceiptIdToShardId, receipt_id.as_ref(), &data); } for (block_hash, refcount) in self.chain_store_cache_update.block_refcounts.iter() { diff --git a/chain/chain/src/store_validator/validate.rs b/chain/chain/src/store_validator/validate.rs index 485be1b62c0..7edbc20e3f8 100644 --- a/chain/chain/src/store_validator/validate.rs +++ b/chain/chain/src/store_validator/validate.rs @@ -1,5 +1,5 @@ use crate::StoreValidator; -use borsh::BorshSerialize; + use near_primitives::block::{Block, BlockHeader, Tip}; use near_primitives::epoch_manager::block_info::BlockInfo; use near_primitives::epoch_manager::epoch_info::EpochInfo; @@ -854,7 +854,7 @@ pub(crate) fn state_part_header_exists( ) -> Result<(), StoreValidatorError> { let StatePartKey(block_hash, shard_id, part_id) = *key; let state_header_key = unwrap_or_err!( - StateHeaderKey(shard_id, block_hash).try_to_vec(), + borsh::to_vec(&StateHeaderKey(shard_id, block_hash)), "Can't serialize StateHeaderKey" ); let header = unwrap_or_err_db!( diff --git a/chain/chain/src/test_utils/kv_runtime.rs b/chain/chain/src/test_utils/kv_runtime.rs index d30c19f485e..c00f15cd294 100644 --- a/chain/chain/src/test_utils/kv_runtime.rs +++ b/chain/chain/src/test_utils/kv_runtime.rs @@ -349,7 +349,7 @@ impl KeyValueRuntime { receipt_nonces: HashSet::default(), tx_nonces: HashSet::default(), }; - let data = kv_state.try_to_vec().unwrap(); + let data = borsh::to_vec(&kv_state).unwrap(); let data_len = data.len() as u64; // StateRoot is actually faked here. // We cannot do any reasonable validations of it in test_utils. @@ -1158,7 +1158,7 @@ impl RuntimeAdapter for KeyValueRuntime { } } - let data = state.try_to_vec()?; + let data = borsh::to_vec(&state)?; let state_size = data.len() as u64; let state_root = hash(&data); self.state.write().unwrap().insert(state_root, state); @@ -1286,7 +1286,7 @@ impl RuntimeAdapter for KeyValueRuntime { return Ok(vec![]); } let state = self.state.read().unwrap().get(state_root).unwrap().clone(); - let data = state.try_to_vec().expect("should never fall"); + let data = borsh::to_vec(&state).expect("should never fall"); Ok(data) } @@ -1308,7 +1308,7 @@ impl RuntimeAdapter for KeyValueRuntime { } let state = KVState::try_from_slice(data).unwrap(); self.state.write().unwrap().insert(*state_root, state.clone()); - let data = state.try_to_vec()?; + let data = borsh::to_vec(&state)?; let state_size = data.len() as u64; self.state_size.write().unwrap().insert(*state_root, state_size); Ok(()) @@ -1320,14 +1320,7 @@ impl RuntimeAdapter for KeyValueRuntime { _block_hash: &CryptoHash, state_root: &StateRoot, ) -> Result { - let data = self - .state - .read() - .unwrap() - .get(state_root) - .unwrap() - .clone() - .try_to_vec() + let data = borsh::to_vec(&self.state.read().unwrap().get(state_root).unwrap().clone()) .expect("should never fall") .into(); let memory_usage = *self.state_size.read().unwrap().get(state_root).unwrap(); diff --git a/chain/client/src/debug.rs b/chain/client/src/debug.rs index 79b2276da8a..1fdbe62476a 100644 --- a/chain/client/src/debug.rs +++ b/chain/client/src/debug.rs @@ -2,7 +2,7 @@ //! without backwards compatibility. use crate::ClientActor; use actix::{Context, Handler}; -use borsh::BorshSerialize; + use itertools::Itertools; use near_chain::crypto_hash_timer::CryptoHashTimer; use near_chain::{near_chain_primitives, Chain, ChainStoreAccess}; @@ -255,7 +255,7 @@ impl ClientActor { let state_header_exists: Vec = (0..block.chunks().len()) .map(|shard_id| { - let key = StateHeaderKey(shard_id as u64, *block.hash()).try_to_vec(); + let key = borsh::to_vec(&StateHeaderKey(shard_id as u64, *block.hash())); match key { Ok(key) => { matches!( diff --git a/chain/client/src/sync_jobs_actor.rs b/chain/client/src/sync_jobs_actor.rs index 06a8aa7bd8f..319d19e980b 100644 --- a/chain/client/src/sync_jobs_actor.rs +++ b/chain/client/src/sync_jobs_actor.rs @@ -1,6 +1,6 @@ use crate::ClientActor; use actix::AsyncContext; -use borsh::BorshSerialize; + use near_chain::chain::{ do_apply_chunks, ApplyStatePartsRequest, ApplyStatePartsResponse, BlockCatchUpRequest, BlockCatchUpResponse, @@ -53,7 +53,7 @@ impl SyncJobsActor { let shard_id = msg.shard_uid.shard_id as ShardId; for part_id in 0..msg.num_parts { - let key = StatePartKey(msg.sync_hash, shard_id, part_id).try_to_vec()?; + let key = borsh::to_vec(&StatePartKey(msg.sync_hash, shard_id, part_id))?; let part = store.get(DBCol::StateParts, &key)?.unwrap(); msg.runtime_adapter.apply_state_part( diff --git a/chain/client/src/tests/catching_up.rs b/chain/client/src/tests/catching_up.rs index 5389feef0a7..b7a15637621 100644 --- a/chain/client/src/tests/catching_up.rs +++ b/chain/client/src/tests/catching_up.rs @@ -266,10 +266,10 @@ fn test_catchup_receipts_sync_common(wait_till: u64, send: u64, sync_hold: bool) peer_id: peer_id.clone(), }; if !seen_hashes_with_state - .contains(&hash_func(&srs.try_to_vec().unwrap())) + .contains(&hash_func(&borsh::to_vec(&srs).unwrap())) { seen_hashes_with_state - .insert(hash_func(&srs.try_to_vec().unwrap())); + .insert(hash_func(&borsh::to_vec(&srs).unwrap())); return (NetworkResponses::NoResponse.into(), false); } } @@ -289,10 +289,10 @@ fn test_catchup_receipts_sync_common(wait_till: u64, send: u64, sync_hold: bool) peer_id: peer_id.clone(), }; if !seen_hashes_with_state - .contains(&hash_func(&srs.try_to_vec().unwrap())) + .contains(&hash_func(&borsh::to_vec(&srs).unwrap())) { seen_hashes_with_state - .insert(hash_func(&srs.try_to_vec().unwrap())); + .insert(hash_func(&borsh::to_vec(&srs).unwrap())); return (NetworkResponses::NoResponse.into(), false); } } diff --git a/chain/jsonrpc/jsonrpc-tests/tests/rpc_transactions.rs b/chain/jsonrpc/jsonrpc-tests/tests/rpc_transactions.rs index 02646196fc1..e396189a3fd 100644 --- a/chain/jsonrpc/jsonrpc-tests/tests/rpc_transactions.rs +++ b/chain/jsonrpc/jsonrpc-tests/tests/rpc_transactions.rs @@ -1,7 +1,7 @@ use std::sync::{Arc, Mutex}; use actix::{Actor, System}; -use borsh::BorshSerialize; + use futures::{future, FutureExt, TryFutureExt}; use near_actix_test_utils::run_actix; @@ -44,7 +44,7 @@ fn test_send_tx_async() { 100, block_hash, ); - let bytes = tx.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&tx).unwrap(); let tx_hash = tx.get_hash().to_string(); *tx_hash2_1.lock().unwrap() = Some(tx.get_hash()); client @@ -93,7 +93,7 @@ fn test_send_tx_commit() { 100, block_hash, ); - let bytes = tx.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&tx).unwrap(); let result = client.broadcast_tx_commit(to_base64(&bytes)).await.unwrap(); assert_eq!( result.final_execution_outcome.unwrap().into_outcome().status, @@ -146,7 +146,7 @@ fn test_expired_tx() { 100, block_hash, ); - let bytes = tx.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&tx).unwrap(); actix::spawn( client .broadcast_tx_commit(to_base64(&bytes)) @@ -190,7 +190,7 @@ fn test_replay_protection() { 100, hash(&[1]), ); - let bytes = tx.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&tx).unwrap(); if let Ok(_) = client.broadcast_tx_commit(to_base64(&bytes)).await { panic!("transaction should not succeed"); } diff --git a/chain/jsonrpc/src/api/transactions.rs b/chain/jsonrpc/src/api/transactions.rs index 31aef01b3da..5ed87a277a0 100644 --- a/chain/jsonrpc/src/api/transactions.rs +++ b/chain/jsonrpc/src/api/transactions.rs @@ -68,7 +68,7 @@ mod tests { use near_jsonrpc_primitives::types::transactions::{ RpcBroadcastTransactionRequest, RpcTransactionStatusCommonRequest, }; - use near_primitives::borsh::BorshSerialize; + use near_primitives::borsh; use near_primitives::hash::CryptoHash; use near_primitives::serialize::to_base64; use near_primitives::transaction::SignedTransaction; @@ -85,7 +85,7 @@ mod tests { fn test_serialize_tx_status_params_as_binary_signed_tx() { let tx_hash = CryptoHash::new(); let tx = SignedTransaction::empty(tx_hash); - let bytes_tx = tx.try_to_vec().unwrap(); + let bytes_tx = borsh::to_vec(&tx).unwrap(); let str_tx = to_base64(&bytes_tx); let params = serde_json::json!([str_tx]); assert!(RpcTransactionStatusCommonRequest::parse(params).is_ok()); @@ -103,7 +103,7 @@ mod tests { fn test_serialize_send_tx_params_as_binary_signed_tx() { let tx_hash = CryptoHash::new(); let tx = SignedTransaction::empty(tx_hash); - let bytes_tx = tx.try_to_vec().unwrap(); + let bytes_tx = borsh::to_vec(&tx).unwrap(); let str_tx = to_base64(&bytes_tx); let params = serde_json::json!([str_tx]); assert!(RpcBroadcastTransactionRequest::parse(params).is_ok()); diff --git a/chain/network/src/network_protocol/edge.rs b/chain/network/src/network_protocol/edge.rs index 19163b85aaa..010af0c2ff3 100644 --- a/chain/network/src/network_protocol/edge.rs +++ b/chain/network/src/network_protocol/edge.rs @@ -1,10 +1,10 @@ use borsh::{BorshDeserialize, BorshSerialize}; use near_async::time; use near_crypto::{KeyType, SecretKey, Signature}; -use near_primitives::borsh::maybestd::sync::Arc; use near_primitives::hash::CryptoHash; use near_primitives::network::PeerId; use once_cell::sync::Lazy; +use std::sync::Arc; // We'd treat all nonces that are below this values as 'old style' (without any expiration time). // And all nonces above this value as new style (that would expire after some time). diff --git a/chain/network/src/network_protocol/mod.rs b/chain/network/src/network_protocol/mod.rs index 1a94f7034e8..016534794ac 100644 --- a/chain/network/src/network_protocol/mod.rs +++ b/chain/network/src/network_protocol/mod.rs @@ -22,7 +22,7 @@ pub use _proto::network as proto; use crate::network_protocol::proto_conv::trace_context::{ extract_span_context, inject_trace_context, }; -use borsh::{BorshDeserialize as _, BorshSerialize as _}; +use borsh::BorshDeserialize as _; use near_async::time; use near_crypto::PublicKey; use near_crypto::Signature; @@ -442,7 +442,7 @@ impl PeerMessage { /// If the encoding is `Proto`, then also attaches current Span's context to the message. pub(crate) fn serialize(&self, enc: Encoding) -> Vec { match enc { - Encoding::Borsh => borsh_::PeerMessage::from(self).try_to_vec().unwrap(), + Encoding::Borsh => borsh::to_vec(&borsh_::PeerMessage::from(self)).unwrap(), Encoding::Proto => { let mut msg = proto::PeerMessage::from(self); let cx = Span::current().context(); diff --git a/chain/network/src/network_protocol/proto_conv/crypto.rs b/chain/network/src/network_protocol/proto_conv/crypto.rs index cbadf92b1b3..028e25d2ec2 100644 --- a/chain/network/src/network_protocol/proto_conv/crypto.rs +++ b/chain/network/src/network_protocol/proto_conv/crypto.rs @@ -1,6 +1,6 @@ /// Conversion functions for messages representing crypto primitives. use crate::network_protocol::proto; -use borsh::{BorshDeserialize as _, BorshSerialize as _}; +use borsh::BorshDeserialize as _; use near_crypto::PublicKey; use near_primitives::hash::CryptoHash; use near_primitives::network::PeerId; @@ -26,11 +26,11 @@ impl TryFrom<&proto::CryptoHash> for CryptoHash { ////////////////////////////////////////// -pub type ParsePublicKeyError = borsh::maybestd::io::Error; +pub type ParsePublicKeyError = std::io::Error; impl From<&PublicKey> for proto::PublicKey { fn from(x: &PublicKey) -> Self { - Self { borsh: x.try_to_vec().unwrap(), ..Self::default() } + Self { borsh: borsh::to_vec(&x).unwrap(), ..Self::default() } } } @@ -56,11 +56,11 @@ impl TryFrom<&proto::PublicKey> for PeerId { ////////////////////////////////////////// -pub type ParseSignatureError = borsh::maybestd::io::Error; +pub type ParseSignatureError = std::io::Error; impl From<&near_crypto::Signature> for proto::Signature { fn from(x: &near_crypto::Signature) -> Self { - Self { borsh: x.try_to_vec().unwrap(), ..Self::default() } + Self { borsh: borsh::to_vec(&x).unwrap(), ..Self::default() } } } diff --git a/chain/network/src/network_protocol/proto_conv/net.rs b/chain/network/src/network_protocol/proto_conv/net.rs index 8fc16aef16b..d19d7b02515 100644 --- a/chain/network/src/network_protocol/proto_conv/net.rs +++ b/chain/network/src/network_protocol/proto_conv/net.rs @@ -4,7 +4,7 @@ use super::*; use crate::network_protocol::proto; use crate::network_protocol::PeerAddr; use crate::network_protocol::{Edge, PartialEdgeInfo, PeerInfo}; -use borsh::{BorshDeserialize as _, BorshSerialize as _}; +use borsh::BorshDeserialize as _; use near_primitives::network::AnnounceAccount; use protobuf::MessageField as MF; use std::net::{IpAddr, SocketAddr}; @@ -77,11 +77,11 @@ impl TryFrom<&proto::PeerAddr> for PeerAddr { impl From<&PeerInfo> for proto::PeerInfo { fn from(x: &PeerInfo) -> Self { - Self { borsh: x.try_to_vec().unwrap(), ..Self::default() } + Self { borsh: borsh::to_vec(&x).unwrap(), ..Self::default() } } } -pub type ParsePeerInfoError = borsh::maybestd::io::Error; +pub type ParsePeerInfoError = std::io::Error; impl TryFrom<&proto::PeerInfo> for PeerInfo { type Error = ParsePeerInfoError; @@ -92,11 +92,11 @@ impl TryFrom<&proto::PeerInfo> for PeerInfo { //////////////////////////////////////// -pub type ParsePartialEdgeInfoError = borsh::maybestd::io::Error; +pub type ParsePartialEdgeInfoError = std::io::Error; impl From<&PartialEdgeInfo> for proto::PartialEdgeInfo { fn from(x: &PartialEdgeInfo) -> Self { - Self { borsh: x.try_to_vec().unwrap(), ..Self::default() } + Self { borsh: borsh::to_vec(&x).unwrap(), ..Self::default() } } } @@ -109,11 +109,11 @@ impl TryFrom<&proto::PartialEdgeInfo> for PartialEdgeInfo { //////////////////////////////////////// -pub type ParseEdgeError = borsh::maybestd::io::Error; +pub type ParseEdgeError = std::io::Error; impl From<&Edge> for proto::Edge { fn from(x: &Edge) -> Self { - Self { borsh: x.try_to_vec().unwrap(), ..Self::default() } + Self { borsh: borsh::to_vec(&x).unwrap(), ..Self::default() } } } @@ -126,11 +126,11 @@ impl TryFrom<&proto::Edge> for Edge { //////////////////////////////////////// -pub type ParseAnnounceAccountError = borsh::maybestd::io::Error; +pub type ParseAnnounceAccountError = std::io::Error; impl From<&AnnounceAccount> for proto::AnnounceAccount { fn from(x: &AnnounceAccount) -> Self { - Self { borsh: x.try_to_vec().unwrap(), ..Self::default() } + Self { borsh: borsh::to_vec(&x).unwrap(), ..Self::default() } } } diff --git a/chain/network/src/network_protocol/proto_conv/peer_message.rs b/chain/network/src/network_protocol/proto_conv/peer_message.rs index e82a7f8c38b..c2728a1627f 100644 --- a/chain/network/src/network_protocol/proto_conv/peer_message.rs +++ b/chain/network/src/network_protocol/proto_conv/peer_message.rs @@ -9,7 +9,7 @@ use crate::network_protocol::{ }; use crate::network_protocol::{RoutedMessage, RoutedMessageV2}; use crate::types::StateResponseInfo; -use borsh::{BorshDeserialize as _, BorshSerialize as _}; +use borsh::BorshDeserialize as _; use near_async::time::error::ComponentRange; use near_primitives::block::{Block, BlockHeader}; use near_primitives::challenge::Challenge; @@ -113,11 +113,11 @@ impl TryFrom<&proto::DistanceVector> for DistanceVector { impl From<&BlockHeader> for proto::BlockHeader { fn from(x: &BlockHeader) -> Self { - Self { borsh: x.try_to_vec().unwrap(), ..Default::default() } + Self { borsh: borsh::to_vec(&x).unwrap(), ..Default::default() } } } -pub type ParseBlockHeaderError = borsh::maybestd::io::Error; +pub type ParseBlockHeaderError = std::io::Error; impl TryFrom<&proto::BlockHeader> for BlockHeader { type Error = ParseBlockHeaderError; @@ -130,11 +130,11 @@ impl TryFrom<&proto::BlockHeader> for BlockHeader { impl From<&Block> for proto::Block { fn from(x: &Block) -> Self { - Self { borsh: x.try_to_vec().unwrap(), ..Default::default() } + Self { borsh: borsh::to_vec(&x).unwrap(), ..Default::default() } } } -pub type ParseBlockError = borsh::maybestd::io::Error; +pub type ParseBlockError = std::io::Error; impl TryFrom<&proto::Block> for Block { type Error = ParseBlockError; @@ -147,11 +147,11 @@ impl TryFrom<&proto::Block> for Block { impl From<&StateResponseInfo> for proto::StateResponseInfo { fn from(x: &StateResponseInfo) -> Self { - Self { borsh: x.try_to_vec().unwrap(), ..Default::default() } + Self { borsh: borsh::to_vec(&x).unwrap(), ..Default::default() } } } -pub type ParseStateInfoError = borsh::maybestd::io::Error; +pub type ParseStateInfoError = std::io::Error; impl TryFrom<&proto::StateResponseInfo> for StateResponseInfo { type Error = ParseStateInfoError; @@ -226,11 +226,11 @@ impl From<&PeerMessage> for proto::PeerMessage { ..Default::default() }), PeerMessage::Transaction(t) => ProtoMT::Transaction(proto::SignedTransaction { - borsh: t.try_to_vec().unwrap(), + borsh: borsh::to_vec(&t).unwrap(), ..Default::default() }), PeerMessage::Routed(r) => ProtoMT::Routed(proto::RoutedMessage { - borsh: r.msg.try_to_vec().unwrap(), + borsh: borsh::to_vec(&r.msg).unwrap(), created_at: MF::from_option(r.created_at.as_ref().map(utc_to_proto)), num_hops: r.num_hops, ..Default::default() @@ -240,7 +240,7 @@ impl From<&PeerMessage> for proto::PeerMessage { ..Default::default() }), PeerMessage::Challenge(r) => ProtoMT::Challenge(proto::Challenge { - borsh: r.try_to_vec().unwrap(), + borsh: borsh::to_vec(&r).unwrap(), ..Default::default() }), PeerMessage::StateRequestHeader(shard_id, sync_hash) => { @@ -270,10 +270,10 @@ impl From<&PeerMessage> for proto::PeerMessage { } } -pub type ParsePeersRequestError = borsh::maybestd::io::Error; -pub type ParseTransactionError = borsh::maybestd::io::Error; -pub type ParseRoutedError = borsh::maybestd::io::Error; -pub type ParseChallengeError = borsh::maybestd::io::Error; +pub type ParsePeersRequestError = std::io::Error; +pub type ParseTransactionError = std::io::Error; +pub type ParseRoutedError = std::io::Error; +pub type ParseChallengeError = std::io::Error; #[derive(thiserror::Error, Debug)] pub enum ParsePeerMessageError { diff --git a/chain/network/src/store/schema/tests.rs b/chain/network/src/store/schema/tests.rs index fc642e611c2..40c319fe750 100644 --- a/chain/network/src/store/schema/tests.rs +++ b/chain/network/src/store/schema/tests.rs @@ -9,5 +9,5 @@ fn borsh_wrapper_is_transparent() { let s1 = data::make_secret_key(rng); let s2 = data::make_secret_key(rng); let e = data::make_edge(&s1, &s2, 1); - assert_eq!(Borsh(e.clone()).try_to_vec().unwrap(), e.try_to_vec().unwrap()); + assert_eq!(borsh::to_vec(&Borsh(e.clone())).unwrap(), borsh::to_vec(&e).unwrap()); } diff --git a/chain/network/src/types.rs b/chain/network/src/types.rs index 1086b37d49c..c119b22a9e1 100644 --- a/chain/network/src/types.rs +++ b/chain/network/src/types.rs @@ -53,6 +53,7 @@ pub struct KnownProducer { /// Ban reason. #[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug, Clone, PartialEq, Eq, Copy)] +#[borsh(use_discriminant = false)] pub enum ReasonForBan { None = 0, BadBlock = 1, @@ -389,7 +390,6 @@ impl< mod tests { use super::*; use crate::network_protocol::{RawRoutedMessage, RoutedMessage, RoutedMessageBody}; - use borsh::BorshSerialize as _; const ALLOWED_SIZE: usize = 1 << 20; const NOTIFY_SIZE: usize = 1024; @@ -449,7 +449,7 @@ mod tests { fn routed_message_body_compatibility_smoke_test() { #[track_caller] fn check(msg: RoutedMessageBody, expected: &[u8]) { - let actual = msg.try_to_vec().unwrap(); + let actual = borsh::to_vec(&msg).unwrap(); assert_eq!(actual.as_slice(), expected); } diff --git a/chain/pool/src/lib.rs b/chain/pool/src/lib.rs index 803feaf9d40..3d5e6fd3395 100644 --- a/chain/pool/src/lib.rs +++ b/chain/pool/src/lib.rs @@ -2,7 +2,7 @@ use std::collections::btree_map::Entry; use std::collections::{BTreeMap, HashMap, HashSet, VecDeque}; use crate::types::{PoolIterator, PoolKey, TransactionGroup}; -use borsh::BorshSerialize; + use near_crypto::PublicKey; use near_o11y::metrics::prometheus::core::{AtomicI64, GenericGauge}; use near_primitives::epoch_manager::RngSeed; @@ -72,7 +72,7 @@ impl TransactionPool { } fn key(&self, account_id: &AccountId, public_key: &PublicKey) -> PoolKey { - let mut v = public_key.try_to_vec().unwrap(); + let mut v = borsh::to_vec(&public_key).unwrap(); v.extend_from_slice(&self.key_seed); v.extend_from_slice(account_id.as_ref().as_bytes()); hash(&v) diff --git a/chain/rosetta-rpc/src/utils.rs b/chain/rosetta-rpc/src/utils.rs index 9b55f60af01..756754b369b 100644 --- a/chain/rosetta-rpc/src/utils.rs +++ b/chain/rosetta-rpc/src/utils.rs @@ -8,7 +8,7 @@ use futures::StreamExt; use near_chain_configs::ProtocolConfigView; use near_client::ViewClientActor; use near_o11y::WithSpanContextExt; -use near_primitives::borsh::{BorshDeserialize, BorshSerialize}; +use near_primitives::borsh::{self, BorshDeserialize, BorshSerialize}; #[derive(Debug, Clone, PartialEq, derive_more::AsRef, derive_more::From)] pub(crate) struct BorshInHexString(T); @@ -40,7 +40,7 @@ where S: serde::Serializer, { serializer.serialize_str(&hex::encode( - self.0.try_to_vec().expect("borsh serialization should never fail"), + borsh::to_vec(&self.0).expect("borsh serialization should never fail"), )) } } diff --git a/core/account-id/fuzz/fuzz_targets/borsh.rs b/core/account-id/fuzz/fuzz_targets/borsh.rs index 168e9cfe18b..1173fd2f43b 100644 --- a/core/account-id/fuzz/fuzz_targets/borsh.rs +++ b/core/account-id/fuzz/fuzz_targets/borsh.rs @@ -1,6 +1,6 @@ #![no_main] -use borsh::{BorshDeserialize, BorshSerialize}; +use borsh::BorshDeserialize; use libfuzzer_sys::fuzz_target; use near_account_id::AccountId; @@ -8,7 +8,7 @@ fuzz_target!(|bytes: &[u8]| { if let Ok(account_id) = AccountId::try_from_slice(bytes) { assert_eq!( account_id, - AccountId::try_from_slice(account_id.try_to_vec().unwrap().as_slice()).unwrap() + AccountId::try_from_slice(borsh::to_vec(&account_id).unwrap().as_slice()).unwrap() ); } }); diff --git a/core/account-id/src/borsh.rs b/core/account-id/src/borsh.rs index de56b52cbf4..8e0d6e7da5a 100644 --- a/core/account-id/src/borsh.rs +++ b/core/account-id/src/borsh.rs @@ -37,7 +37,7 @@ mod tests { panic!("Valid account id {:?} marked invalid: {}", account_id, err) }); - let str_serialized_account_id = account_id.try_to_vec().unwrap(); + let str_serialized_account_id = borsh::to_vec(&account_id).unwrap(); let deserialized_account_id = AccountId::try_from_slice(&str_serialized_account_id) .unwrap_or_else(|err| { @@ -46,14 +46,14 @@ mod tests { assert_eq!(deserialized_account_id, parsed_account_id); let serialized_account_id = - deserialized_account_id.try_to_vec().unwrap_or_else(|err| { + borsh::to_vec(&deserialized_account_id).unwrap_or_else(|err| { panic!("failed to serialize account ID {:?}: {}", account_id, err) }); assert_eq!(serialized_account_id, str_serialized_account_id); } for account_id in BAD_ACCOUNT_IDS.iter() { - let str_serialized_account_id = account_id.try_to_vec().unwrap(); + let str_serialized_account_id = borsh::to_vec(&account_id).unwrap(); assert!( AccountId::try_from_slice(&str_serialized_account_id).is_err(), @@ -69,7 +69,8 @@ mod tests { if let Ok(account_id) = AccountId::try_from_slice(input) { assert_eq!( account_id, - AccountId::try_from_slice(account_id.try_to_vec().unwrap().as_slice()).unwrap() + AccountId::try_from_slice(borsh::to_vec(&account_id).unwrap().as_slice()) + .unwrap() ); } }); diff --git a/core/crypto/src/signature.rs b/core/crypto/src/signature.rs index efc7c7e0a8b..966559c1fb5 100644 --- a/core/crypto/src/signature.rs +++ b/core/crypto/src/signature.rs @@ -817,11 +817,11 @@ mod tests { for key_type in [KeyType::ED25519, KeyType::SECP256K1] { let sk = SecretKey::from_seed(key_type, "test"); let pk = sk.public_key(); - let bytes = pk.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&pk).unwrap(); assert_eq!(PublicKey::try_from_slice(&bytes).unwrap(), pk); let signature = sk.sign(&data); - let bytes = signature.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&signature).unwrap(); assert_eq!(Signature::try_from_slice(&bytes).unwrap(), signature); assert!(PublicKey::try_from_slice(&[0]).is_err()); diff --git a/core/primitives-core/src/account.rs b/core/primitives-core/src/account.rs index 50a3a340d52..380bf449421 100644 --- a/core/primitives-core/src/account.rs +++ b/core/primitives-core/src/account.rs @@ -234,7 +234,6 @@ pub struct FunctionCallPermission { #[cfg(test)] mod tests { - use borsh::BorshSerialize; use crate::hash::hash; @@ -243,7 +242,7 @@ mod tests { #[test] fn test_account_serialization() { let acc = Account::new(1_000_000, 1_000_000, CryptoHash::default(), 100); - let bytes = acc.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&acc).unwrap(); assert_eq!(hash(&bytes).to_string(), "EVk5UaxBe8LQ8r8iD5EAxVBs6TJcMDKqyH7PBuho6bBJ"); } @@ -255,14 +254,14 @@ mod tests { code_hash: CryptoHash::default(), storage_usage: 300, }; - let mut old_bytes = &old_account.try_to_vec().unwrap()[..]; + let mut old_bytes = &borsh::to_vec(&old_account).unwrap()[..]; let new_account = ::deserialize(&mut old_bytes).unwrap(); assert_eq!(new_account.amount, old_account.amount); assert_eq!(new_account.locked, old_account.locked); assert_eq!(new_account.code_hash, old_account.code_hash); assert_eq!(new_account.storage_usage, old_account.storage_usage); assert_eq!(new_account.version, AccountVersion::V1); - let mut new_bytes = &new_account.try_to_vec().unwrap()[..]; + let mut new_bytes = &borsh::to_vec(&new_account).unwrap()[..]; let deserialized_account = ::deserialize(&mut new_bytes).unwrap(); assert_eq!(deserialized_account, new_account); diff --git a/core/primitives/benches/serialization.rs b/core/primitives/benches/serialization.rs index 6de717a58e7..68750c477d7 100644 --- a/core/primitives/benches/serialization.rs +++ b/core/primitives/benches/serialization.rs @@ -2,7 +2,7 @@ extern crate bencher; use bencher::{black_box, Bencher}; -use borsh::{BorshDeserialize, BorshSerialize}; +use borsh::BorshDeserialize; use near_primitives::static_clock::StaticClock; use near_crypto::{KeyType, PublicKey, Signature}; @@ -79,14 +79,14 @@ fn create_account() -> Account { fn serialize_tx(bench: &mut Bencher) { let t = create_transaction(); bench.iter(|| { - let bytes = t.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&t).unwrap(); assert!(!bytes.is_empty()); }); } fn deserialize_tx(bench: &mut Bencher) { let t = create_transaction(); - let bytes = t.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&t).unwrap(); bench.iter(|| { let nt = SignedTransaction::try_from_slice(&bytes).unwrap(); assert_eq!(nt, t); @@ -96,14 +96,14 @@ fn deserialize_tx(bench: &mut Bencher) { fn serialize_block(bench: &mut Bencher) { let b = create_block(); bench.iter(|| { - let bytes = b.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&b).unwrap(); assert!(!bytes.is_empty()); }); } fn deserialize_block(bench: &mut Bencher) { let b = create_block(); - let bytes = b.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&b).unwrap(); bench.iter(|| { let nb = Block::try_from_slice(&bytes).unwrap(); assert_eq!(nb, b); @@ -113,14 +113,14 @@ fn deserialize_block(bench: &mut Bencher) { fn serialize_account(bench: &mut Bencher) { let acc = create_account(); bench.iter(|| { - let bytes = acc.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&acc).unwrap(); assert!(!bytes.is_empty()); }); } fn deserialize_account(bench: &mut Bencher) { let acc = create_account(); - let bytes = acc.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&acc).unwrap(); bench.iter(|| { let nacc = Account::try_from_slice(&bytes).unwrap(); assert_eq!(nacc, acc); diff --git a/core/primitives/src/action/delegate.rs b/core/primitives/src/action/delegate.rs index 0963d7e8dd2..864c2f6076a 100644 --- a/core/primitives/src/action/delegate.rs +++ b/core/primitives/src/action/delegate.rs @@ -12,7 +12,7 @@ use near_primitives_core::hash::{hash, CryptoHash}; use near_primitives_core::types::BlockHeight; use near_primitives_core::types::{AccountId, Nonce}; use serde::{Deserialize, Serialize}; -use std::io::{Error, ErrorKind}; +use std::io::{Error, ErrorKind, Read}; /// This is an index number of Action::Delegate in Action enumeration const ACTION_DELEGATE_NUMBER: u8 = 8; @@ -71,7 +71,7 @@ impl DelegateAction { /// For more details, see: [NEP-461](https://github.com/near/NEPs/pull/461) pub fn get_nep461_hash(&self) -> CryptoHash { let signable = SignableMessage::new(&self, SignableMessageType::DelegateAction); - let bytes = signable.try_to_vec().expect("Failed to deserialize"); + let bytes = borsh::to_vec(&signable).expect("Failed to deserialize"); hash(&bytes) } } @@ -116,9 +116,7 @@ mod private_non_delegate_action { } impl borsh::de::BorshDeserialize for NonDelegateAction { - fn deserialize_reader( - rd: &mut R, - ) -> ::core::result::Result { + fn deserialize_reader(rd: &mut R) -> ::core::result::Result { match u8::deserialize_reader(rd)? { ACTION_DELEGATE_NUMBER => Err(Error::new( ErrorKind::InvalidInput, @@ -171,11 +169,11 @@ mod tests { // Expected an error. Buffer is empty assert_eq!( NonDelegateAction::try_from_slice(Vec::new().as_ref()).map_err(|e| e.kind()), - Err(ErrorKind::InvalidInput) + Err(ErrorKind::InvalidData) ); let delegate_action = create_delegate_action(Vec::::new()); - let serialized_non_delegate_action = delegate_action.try_to_vec().expect("Expect ok"); + let serialized_non_delegate_action = borsh::to_vec(&delegate_action).expect("Expect ok"); // Expected Action::Delegate has not been moved in enum Action assert_eq!(serialized_non_delegate_action[0], ACTION_DELEGATE_NUMBER); @@ -189,7 +187,7 @@ mod tests { let delegate_action = create_delegate_action(vec![Action::CreateAccount(CreateAccountAction {})]); - let serialized_delegate_action = delegate_action.try_to_vec().expect("Expect ok"); + let serialized_delegate_action = borsh::to_vec(&delegate_action).expect("Expect ok"); // Valid action assert_eq!( diff --git a/core/primitives/src/block_header.rs b/core/primitives/src/block_header.rs index 79e825585aa..312542b1bac 100644 --- a/core/primitives/src/block_header.rs +++ b/core/primitives/src/block_header.rs @@ -298,7 +298,7 @@ impl Approval { } pub fn get_data_for_sig(inner: &ApprovalInner, target_height: BlockHeight) -> Vec { - [inner.try_to_vec().unwrap().as_ref(), target_height.to_le_bytes().as_ref()].concat() + [borsh::to_vec(&inner).unwrap().as_ref(), target_height.to_le_bytes().as_ref()].concat() } } @@ -309,7 +309,7 @@ impl ApprovalMessage { } #[derive(BorshSerialize, BorshDeserialize, serde::Serialize, Debug, Clone, Eq, PartialEq)] -#[borsh_init(init)] +#[borsh(init=init)] pub struct BlockHeaderV1 { pub prev_hash: CryptoHash, @@ -322,7 +322,7 @@ pub struct BlockHeaderV1 { pub signature: Signature, /// Cached value of hash for this block. - #[borsh_skip] + #[borsh(skip)] pub hash: CryptoHash, } @@ -330,15 +330,15 @@ impl BlockHeaderV1 { pub fn init(&mut self) { self.hash = BlockHeader::compute_hash( self.prev_hash, - &self.inner_lite.try_to_vec().expect("Failed to serialize"), - &self.inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&self.inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&self.inner_rest).expect("Failed to serialize"), ); } } /// V1 -> V2: Remove `chunks_included` from `inner_reset` #[derive(BorshSerialize, BorshDeserialize, serde::Serialize, Debug, Clone, Eq, PartialEq)] -#[borsh_init(init)] +#[borsh(init=init)] pub struct BlockHeaderV2 { pub prev_hash: CryptoHash, @@ -351,14 +351,14 @@ pub struct BlockHeaderV2 { pub signature: Signature, /// Cached value of hash for this block. - #[borsh_skip] + #[borsh(skip)] pub hash: CryptoHash, } /// V2 -> V3: Add `prev_height` to `inner_rest` and use new `ValidatorStake` // Add `block_ordinal` to `inner_rest` #[derive(BorshSerialize, BorshDeserialize, serde::Serialize, Debug, Clone, Eq, PartialEq)] -#[borsh_init(init)] +#[borsh(init=init)] pub struct BlockHeaderV3 { pub prev_hash: CryptoHash, @@ -371,13 +371,13 @@ pub struct BlockHeaderV3 { pub signature: Signature, /// Cached value of hash for this block. - #[borsh_skip] + #[borsh(skip)] pub hash: CryptoHash, } /// V3 -> V4: Add hash of block body to inner_rest #[derive(BorshSerialize, BorshDeserialize, serde::Serialize, Debug, Clone, Eq, PartialEq)] -#[borsh_init(init)] +#[borsh(init=init)] pub struct BlockHeaderV4 { pub prev_hash: CryptoHash, @@ -390,13 +390,13 @@ pub struct BlockHeaderV4 { pub signature: Signature, /// Cached value of hash for this block. - #[borsh_skip] + #[borsh(skip)] pub hash: CryptoHash, } /// V4 -> V5: Switch to post-state-root #[derive(BorshSerialize, BorshDeserialize, serde::Serialize, Debug, Clone, Eq, PartialEq)] -#[borsh_init(init)] +#[borsh(init=init)] pub struct BlockHeaderV5 { pub prev_hash: CryptoHash, @@ -408,8 +408,8 @@ pub struct BlockHeaderV5 { /// Signature of the block producer. pub signature: Signature, - /// Cached value of hash for this block. - #[borsh_skip] + /// Cached value of hash for this block + #[borsh(skip)] pub hash: CryptoHash, } @@ -417,8 +417,8 @@ impl BlockHeaderV2 { pub fn init(&mut self) { self.hash = BlockHeader::compute_hash( self.prev_hash, - &self.inner_lite.try_to_vec().expect("Failed to serialize"), - &self.inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&self.inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&self.inner_rest).expect("Failed to serialize"), ); } } @@ -427,8 +427,8 @@ impl BlockHeaderV3 { pub fn init(&mut self) { self.hash = BlockHeader::compute_hash( self.prev_hash, - &self.inner_lite.try_to_vec().expect("Failed to serialize"), - &self.inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&self.inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&self.inner_rest).expect("Failed to serialize"), ); } } @@ -437,8 +437,8 @@ impl BlockHeaderV4 { pub fn init(&mut self) { self.hash = BlockHeader::compute_hash( self.prev_hash, - &self.inner_lite.try_to_vec().expect("Failed to serialize"), - &self.inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&self.inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&self.inner_rest).expect("Failed to serialize"), ); } } @@ -447,8 +447,8 @@ impl BlockHeaderV5 { pub fn init(&mut self) { self.hash = BlockHeader::compute_hash( self.prev_hash, - &self.inner_lite.try_to_vec().expect("Failed to serialize"), - &self.inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&self.inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&self.inner_rest).expect("Failed to serialize"), ); } } @@ -549,8 +549,8 @@ impl BlockHeader { }; let (hash, signature) = signer.sign_block_header_parts( prev_hash, - &inner_lite.try_to_vec().expect("Failed to serialize"), - &inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&inner_rest).expect("Failed to serialize"), ); Self::BlockHeaderV1(Arc::new(BlockHeaderV1 { prev_hash, @@ -581,8 +581,8 @@ impl BlockHeader { }; let (hash, signature) = signer.sign_block_header_parts( prev_hash, - &inner_lite.try_to_vec().expect("Failed to serialize"), - &inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&inner_rest).expect("Failed to serialize"), ); Self::BlockHeaderV2(Arc::new(BlockHeaderV2 { prev_hash, @@ -613,8 +613,8 @@ impl BlockHeader { }; let (hash, signature) = signer.sign_block_header_parts( prev_hash, - &inner_lite.try_to_vec().expect("Failed to serialize"), - &inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&inner_rest).expect("Failed to serialize"), ); Self::BlockHeaderV3(Arc::new(BlockHeaderV3 { prev_hash, @@ -646,8 +646,8 @@ impl BlockHeader { }; let (hash, signature) = signer.sign_block_header_parts( prev_hash, - &inner_lite.try_to_vec().expect("Failed to serialize"), - &inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&inner_rest).expect("Failed to serialize"), ); Self::BlockHeaderV4(Arc::new(BlockHeaderV4 { prev_hash, @@ -707,8 +707,8 @@ impl BlockHeader { }; let hash = BlockHeader::compute_hash( CryptoHash::default(), - &inner_lite.try_to_vec().expect("Failed to serialize"), - &inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&inner_rest).expect("Failed to serialize"), ); Self::BlockHeaderV1(Arc::new(BlockHeaderV1 { prev_hash: CryptoHash::default(), @@ -736,8 +736,8 @@ impl BlockHeader { }; let hash = BlockHeader::compute_hash( CryptoHash::default(), - &inner_lite.try_to_vec().expect("Failed to serialize"), - &inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&inner_rest).expect("Failed to serialize"), ); Self::BlockHeaderV2(Arc::new(BlockHeaderV2 { prev_hash: CryptoHash::default(), @@ -768,8 +768,8 @@ impl BlockHeader { }; let hash = BlockHeader::compute_hash( CryptoHash::default(), - &inner_lite.try_to_vec().expect("Failed to serialize"), - &inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&inner_rest).expect("Failed to serialize"), ); Self::BlockHeaderV3(Arc::new(BlockHeaderV3 { prev_hash: CryptoHash::default(), @@ -801,8 +801,8 @@ impl BlockHeader { }; let hash = BlockHeader::compute_hash( CryptoHash::default(), - &inner_lite.try_to_vec().expect("Failed to serialize"), - &inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&inner_rest).expect("Failed to serialize"), ); Self::BlockHeaderV4(Arc::new(BlockHeaderV4 { prev_hash: CryptoHash::default(), @@ -1193,19 +1193,19 @@ impl BlockHeader { pub fn inner_lite_bytes(&self) -> Vec { match self { BlockHeader::BlockHeaderV1(header) => { - header.inner_lite.try_to_vec().expect("Failed to serialize") + borsh::to_vec(&header.inner_lite).expect("Failed to serialize") } BlockHeader::BlockHeaderV2(header) => { - header.inner_lite.try_to_vec().expect("Failed to serialize") + borsh::to_vec(&header.inner_lite).expect("Failed to serialize") } BlockHeader::BlockHeaderV3(header) => { - header.inner_lite.try_to_vec().expect("Failed to serialize") + borsh::to_vec(&header.inner_lite).expect("Failed to serialize") } BlockHeader::BlockHeaderV4(header) => { - header.inner_lite.try_to_vec().expect("Failed to serialize") + borsh::to_vec(&header.inner_lite).expect("Failed to serialize") } BlockHeader::BlockHeaderV5(header) => { - header.inner_lite.try_to_vec().expect("Failed to serialize") + borsh::to_vec(&header.inner_lite).expect("Failed to serialize") } } } @@ -1213,19 +1213,19 @@ impl BlockHeader { pub fn inner_rest_bytes(&self) -> Vec { match self { BlockHeader::BlockHeaderV1(header) => { - header.inner_rest.try_to_vec().expect("Failed to serialize") + borsh::to_vec(&header.inner_rest).expect("Failed to serialize") } BlockHeader::BlockHeaderV2(header) => { - header.inner_rest.try_to_vec().expect("Failed to serialize") + borsh::to_vec(&header.inner_rest).expect("Failed to serialize") } BlockHeader::BlockHeaderV3(header) => { - header.inner_rest.try_to_vec().expect("Failed to serialize") + borsh::to_vec(&header.inner_rest).expect("Failed to serialize") } BlockHeader::BlockHeaderV4(header) => { - header.inner_rest.try_to_vec().expect("Failed to serialize") + borsh::to_vec(&header.inner_rest).expect("Failed to serialize") } BlockHeader::BlockHeaderV5(header) => { - header.inner_rest.try_to_vec().expect("Failed to serialize") + borsh::to_vec(&header.inner_rest).expect("Failed to serialize") } } } diff --git a/core/primitives/src/challenge.rs b/core/primitives/src/challenge.rs index 36b7924abcd..75dcfbc9226 100644 --- a/core/primitives/src/challenge.rs +++ b/core/primitives/src/challenge.rs @@ -87,13 +87,13 @@ pub enum ChallengeBody { } #[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)] -#[borsh_init(init)] +#[borsh(init=init)] pub struct Challenge { pub body: ChallengeBody, pub account_id: AccountId, pub signature: Signature, - #[borsh_skip] + #[borsh(skip)] pub hash: CryptoHash, } diff --git a/core/primitives/src/receipt.rs b/core/primitives/src/receipt.rs index 30af36fb9c6..4c9dc5e1e57 100644 --- a/core/primitives/src/receipt.rs +++ b/core/primitives/src/receipt.rs @@ -1,4 +1,3 @@ -use crate::borsh::maybestd::collections::HashMap; use crate::hash::CryptoHash; use crate::serialize::dec_format; use crate::transaction::{Action, TransferAction}; @@ -9,6 +8,7 @@ use near_fmt::AbbrBytes; use serde_with::base64::Base64; use serde_with::serde_as; use std::borrow::Borrow; +use std::collections::HashMap; use std::fmt; pub use near_vm_runner::logic::DataReceiver; diff --git a/core/primitives/src/sharding.rs b/core/primitives/src/sharding.rs index 8f031d3fc2d..815884df2fc 100644 --- a/core/primitives/src/sharding.rs +++ b/core/primitives/src/sharding.rs @@ -72,7 +72,7 @@ pub use shard_chunk_header_inner::{ }; #[derive(BorshSerialize, BorshDeserialize, Clone, PartialEq, Eq, Debug)] -#[borsh_init(init)] +#[borsh(init=init)] pub struct ShardChunkHeaderV1 { pub inner: ShardChunkHeaderInnerV1, @@ -81,12 +81,12 @@ pub struct ShardChunkHeaderV1 { /// Signature of the chunk producer. pub signature: Signature, - #[borsh_skip] + #[borsh(skip)] pub hash: ChunkHash, } #[derive(BorshSerialize, BorshDeserialize, Clone, PartialEq, Eq, Debug)] -#[borsh_init(init)] +#[borsh(init=init)] pub struct ShardChunkHeaderV2 { pub inner: ShardChunkHeaderInnerV1, @@ -95,7 +95,7 @@ pub struct ShardChunkHeaderV2 { /// Signature of the chunk producer. pub signature: Signature, - #[borsh_skip] + #[borsh(skip)] pub hash: ChunkHash, } @@ -105,7 +105,7 @@ impl ShardChunkHeaderV2 { } pub fn compute_hash(inner: &ShardChunkHeaderInnerV1) -> ChunkHash { - let inner_bytes = inner.try_to_vec().expect("Failed to serialize"); + let inner_bytes = borsh::to_vec(&inner).expect("Failed to serialize"); let inner_hash = hash(&inner_bytes); ChunkHash(combine_hash(&inner_hash, &inner.encoded_merkle_root)) @@ -150,7 +150,7 @@ impl ShardChunkHeaderV2 { // V2 -> V3: Use versioned ShardChunkHeaderInner structure #[derive(BorshSerialize, BorshDeserialize, Clone, PartialEq, Eq, Debug)] -#[borsh_init(init)] +#[borsh(init=init)] pub struct ShardChunkHeaderV3 { pub inner: ShardChunkHeaderInner, @@ -159,7 +159,7 @@ pub struct ShardChunkHeaderV3 { /// Signature of the chunk producer. pub signature: Signature, - #[borsh_skip] + #[borsh(skip)] pub hash: ChunkHash, } @@ -169,7 +169,7 @@ impl ShardChunkHeaderV3 { } pub fn compute_hash(inner: &ShardChunkHeaderInner) -> ChunkHash { - let inner_bytes = inner.try_to_vec().expect("Failed to serialize"); + let inner_bytes = borsh::to_vec(&inner).expect("Failed to serialize"); let inner_hash = hash(&inner_bytes); ChunkHash(combine_hash(&inner_hash, inner.encoded_merkle_root())) @@ -235,9 +235,9 @@ impl ShardChunkHeader { pub fn inner_header_hash(&self) -> CryptoHash { let inner_bytes = match self { - Self::V1(header) => header.inner.try_to_vec(), - Self::V2(header) => header.inner.try_to_vec(), - Self::V3(header) => header.inner.try_to_vec(), + Self::V1(header) => borsh::to_vec(&header.inner), + Self::V2(header) => borsh::to_vec(&header.inner), + Self::V3(header) => borsh::to_vec(&header.inner), }; hash(&inner_bytes.expect("Failed to serialize")) } @@ -430,7 +430,7 @@ impl ShardChunkHeaderV1 { } pub fn compute_hash(inner: &ShardChunkHeaderInnerV1) -> ChunkHash { - let inner_bytes = inner.try_to_vec().expect("Failed to serialize"); + let inner_bytes = borsh::to_vec(&inner).expect("Failed to serialize"); let inner_hash = hash(&inner_bytes); ChunkHash(inner_hash) @@ -1019,7 +1019,7 @@ impl EncodedShardChunk { outgoing_receipts: &[Receipt], ) -> Result<(Vec>>, u64), std::io::Error> { let mut bytes = - TransactionReceipt(transactions, outgoing_receipts.to_vec()).try_to_vec()?; + borsh::to_vec(&TransactionReceipt(transactions, outgoing_receipts.to_vec()))?; let mut parts = Vec::with_capacity(rs.total_shard_count()); let data_parts = rs.data_shard_count(); diff --git a/core/primitives/src/signable_message.rs b/core/primitives/src/signable_message.rs index efdd489acb1..124eb4f6d53 100644 --- a/core/primitives/src/signable_message.rs +++ b/core/primitives/src/signable_message.rs @@ -55,7 +55,7 @@ pub struct MessageDiscriminant { /// Only used for constructing a signature, not used to transmit messages. The /// discriminant prefix is implicit and should be known by the receiver based on /// the context in which the message is received. -#[derive(BorshSerialize, BorshDeserialize)] +#[derive(BorshSerialize)] pub struct SignableMessage<'a, T> { pub discriminant: MessageDiscriminant, pub msg: &'a T, @@ -95,7 +95,7 @@ impl<'a, T: BorshSerialize> SignableMessage<'a, T> { } pub fn sign(&self, signer: &dyn Signer) -> Signature { - let bytes = self.try_to_vec().expect("Failed to deserialize"); + let bytes = borsh::to_vec(&self).expect("Failed to deserialize"); let hash = hash(&bytes); signer.sign(hash.as_bytes()) } diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index 97d1c4ff98a..671960d15a5 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -48,7 +48,7 @@ pub struct Transaction { impl Transaction { /// Computes a hash of the transaction for signing and size of serialized transaction pub fn get_hash_and_size(&self) -> (CryptoHash, u64) { - let bytes = self.try_to_vec().expect("Failed to deserialize"); + let bytes = borsh::to_vec(&self).expect("Failed to deserialize"); (hash(&bytes), bytes.len() as u64) } } @@ -56,13 +56,13 @@ impl Transaction { #[derive( BorshSerialize, BorshDeserialize, serde::Serialize, serde::Deserialize, Eq, Debug, Clone, )] -#[borsh_init(init)] +#[borsh(init=init)] pub struct SignedTransaction { pub transaction: Transaction, pub signature: Signature, - #[borsh_skip] + #[borsh(skip)] hash: CryptoHash, - #[borsh_skip] + #[borsh(skip)] size: u64, } @@ -193,7 +193,7 @@ pub struct ExecutionOutcome { // At the moment this field is only set at runtime and is not persisted in the database. // This means that when execution outcomes are read from the database, this value will not be // set and any code that attempts to use it will crash. - #[borsh_skip] + #[borsh(skip)] pub compute_usage: Option, /// The amount of tokens burnt corresponding to the burnt gas amount. /// This value doesn't always equal to the `gas_burnt` multiplied by the gas price, because @@ -313,7 +313,7 @@ mod tests { let invalid_keys = vec![wrong_public_key]; assert!(!verify_transaction_signature(&transaction, &invalid_keys)); - let bytes = transaction.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&transaction).unwrap(); let decoded_tx = SignedTransaction::try_from_slice(&bytes).unwrap(); assert!(verify_transaction_signature(&decoded_tx, &valid_keys)); } @@ -362,7 +362,7 @@ mod tests { }; let signed_tx = SignedTransaction::new(Signature::empty(KeyType::ED25519), transaction); let new_signed_tx = - SignedTransaction::try_from_slice(&signed_tx.try_to_vec().unwrap()).unwrap(); + SignedTransaction::try_from_slice(&borsh::to_vec(&signed_tx).unwrap()).unwrap(); assert_eq!( new_signed_tx.get_hash().to_string(), diff --git a/core/primitives/src/trie_key.rs b/core/primitives/src/trie_key.rs index 6e1b3e6bf24..3dbdf9f04b3 100644 --- a/core/primitives/src/trie_key.rs +++ b/core/primitives/src/trie_key.rs @@ -163,7 +163,7 @@ impl TrieKey { buf.push(col::ACCESS_KEY); buf.extend(account_id.as_ref().as_bytes()); buf.push(col::ACCESS_KEY); - buf.extend(public_key.try_to_vec().unwrap()); + buf.extend(borsh::to_vec(&public_key).unwrap()); } TrieKey::ReceivedData { receiver_id, data_id } => { buf.push(col::RECEIVED_DATA); diff --git a/core/primitives/src/views.rs b/core/primitives/src/views.rs index b139d337eb5..28d82715a09 100644 --- a/core/primitives/src/views.rs +++ b/core/primitives/src/views.rs @@ -2093,7 +2093,7 @@ impl LightClientBlockLiteView { let block_header_inner_lite: BlockHeaderInnerLite = self.inner_lite.clone().into(); combine_hash( &combine_hash( - &hash(&block_header_inner_lite.try_to_vec().unwrap()), + &hash(&borsh::to_vec(&block_header_inner_lite).unwrap()), &self.inner_rest_hash, ), &self.prev_block_hash, diff --git a/core/store/benches/finalize_bench.rs b/core/store/benches/finalize_bench.rs index 4c9b8bc1709..5c5c720c48c 100644 --- a/core/store/benches/finalize_bench.rs +++ b/core/store/benches/finalize_bench.rs @@ -261,7 +261,7 @@ where T: Clone + BorshSerialize, { let memory_range = 200_000_000; - let num_chunks = memory_range / chunk.try_to_vec().unwrap().len(); + let num_chunks = memory_range / borsh::object_length(&chunk).unwrap(); let chunks: Vec<_> = std::iter::repeat(chunk).take(num_chunks).collect(); chunks } diff --git a/core/store/src/cold_storage.rs b/core/store/src/cold_storage.rs index a29b0480ee7..3898308fae1 100644 --- a/core/store/src/cold_storage.rs +++ b/core/store/src/cold_storage.rs @@ -3,7 +3,7 @@ use crate::db::{ColdDB, COLD_HEAD_KEY, HEAD_KEY}; use crate::trie::TrieRefcountChange; use crate::{metrics, DBCol, DBTransaction, Database, Store, TrieChanges}; -use borsh::{BorshDeserialize, BorshSerialize}; +use borsh::BorshDeserialize; use near_primitives::block::{Block, BlockHeader, Tip}; use near_primitives::hash::CryptoHash; use near_primitives::shard_layout::ShardLayout; @@ -167,21 +167,21 @@ pub fn update_cold_head( // Write HEAD to the cold db. { let mut transaction = DBTransaction::new(); - transaction.set(DBCol::BlockMisc, HEAD_KEY.to_vec(), tip.try_to_vec()?); + transaction.set(DBCol::BlockMisc, HEAD_KEY.to_vec(), borsh::to_vec(&tip)?); cold_db.write(transaction)?; } // Write COLD_HEAD_KEY to the cold db. { let mut transaction = DBTransaction::new(); - transaction.set(DBCol::BlockMisc, COLD_HEAD_KEY.to_vec(), tip.try_to_vec()?); + transaction.set(DBCol::BlockMisc, COLD_HEAD_KEY.to_vec(), borsh::to_vec(&tip)?); cold_db.write(transaction)?; } // Write COLD_HEAD to the hot db. { let mut transaction = DBTransaction::new(); - transaction.set(DBCol::BlockMisc, COLD_HEAD_KEY.to_vec(), tip.try_to_vec()?); + transaction.set(DBCol::BlockMisc, COLD_HEAD_KEY.to_vec(), borsh::to_vec(&tip)?); hot_store.storage.write(transaction)?; crate::metrics::COLD_HEAD_HEIGHT.set(*height as i64); diff --git a/core/store/src/flat/inlining_migration.rs b/core/store/src/flat/inlining_migration.rs index 68d08ab8555..49652e06587 100644 --- a/core/store/src/flat/inlining_migration.rs +++ b/core/store/src/flat/inlining_migration.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use std::thread::JoinHandle; use std::time::Duration; -use borsh::{BorshDeserialize, BorshSerialize}; +use borsh::BorshDeserialize; use crossbeam::channel; use itertools::Itertools; use near_primitives::hash::CryptoHash; @@ -258,8 +258,7 @@ pub fn inline_flat_state_values( store_update.set( DBCol::FlatState, &key, - &FlatStateValue::inlined(value) - .try_to_vec() + &borsh::to_vec(&FlatStateValue::inlined(value)) .expect("borsh should not fail here"), ); inlined_batch_count += 1; @@ -313,7 +312,7 @@ mod tests { use crate::flat::store_helper::encode_flat_state_db_key; use crate::flat::{FlatStateValuesInliningMigrationHandle, FlatStorageManager}; use crate::{DBCol, NodeStorage, Store, TrieCachingStorage}; - use borsh::{BorshDeserialize, BorshSerialize}; + use borsh::BorshDeserialize; use near_o11y::testonly::init_test_logger; use near_primitives::hash::{hash, CryptoHash}; use near_primitives::shard_layout::{ShardLayout, ShardUId}; @@ -442,7 +441,7 @@ mod tests { TrieCachingStorage::get_key_from_shard_uid_and_hash(shard_uid, &hash(&value)); store_update.increment_refcount(DBCol::State, &trie_key, &value); let fs_key = encode_flat_state_db_key(shard_uid, &[i as u8]); - let fs_value = FlatStateValue::value_ref(&value).try_to_vec().unwrap(); + let fs_value = borsh::to_vec(&FlatStateValue::value_ref(&value)).unwrap(); store_update.set(DBCol::FlatState, &fs_key, &fs_value); } store_update.commit().unwrap(); diff --git a/core/store/src/flat/storage.rs b/core/store/src/flat/storage.rs index 68de1d3911b..077d59345dc 100644 --- a/core/store/src/flat/storage.rs +++ b/core/store/src/flat/storage.rs @@ -492,7 +492,7 @@ mod tests { use crate::test_utils::create_test_store; use crate::StorageError; use assert_matches::assert_matches; - use borsh::BorshSerialize; + use near_o11y::testonly::init_test_logger; use near_primitives::hash::{hash, CryptoHash}; use near_primitives::shard_layout::ShardUId; @@ -513,7 +513,7 @@ mod tests { } fn block_hash(height: BlockHeight) -> CryptoHash { - hash(&height.try_to_vec().unwrap()) + hash(&borsh::to_vec(&height).unwrap()) } /// Build a chain with given set of heights and a function mapping block heights to heights of their parents. diff --git a/core/store/src/genesis/state_applier.rs b/core/store/src/genesis/state_applier.rs index 1f821d96b8e..bec652166fa 100644 --- a/core/store/src/genesis/state_applier.rs +++ b/core/store/src/genesis/state_applier.rs @@ -3,7 +3,7 @@ use crate::{ get_account, get_received_data, set, set_access_key, set_account, set_code, set_delayed_receipt, set_postponed_receipt, set_received_data, ShardTries, TrieUpdate, }; -use borsh::BorshSerialize; + use near_chain_configs::Genesis; use near_crypto::PublicKey; use near_primitives::account::{AccessKey, Account}; @@ -52,8 +52,8 @@ impl<'a> StorageComputer<'a> { let public_key: PublicKey = public_key.clone(); let access_key: AccessKey = access_key.clone(); let storage_usage = self.config.num_extra_bytes_record - + public_key.try_to_vec().unwrap().len() as u64 - + access_key.try_to_vec().unwrap().len() as u64; + + borsh::object_length(&public_key).unwrap() as u64 + + borsh::object_length(&access_key).unwrap() as u64; Some((account_id.clone(), storage_usage)) } StateRecord::PostponedReceipt(_) => None, diff --git a/core/store/src/lib.rs b/core/store/src/lib.rs index 77970609765..07f37ce7e00 100644 --- a/core/store/src/lib.rs +++ b/core/store/src/lib.rs @@ -454,7 +454,7 @@ impl StoreUpdate { value: &T, ) -> io::Result<()> { assert!(column.is_insert_only(), "can't insert_ser: {column}"); - let data = value.try_to_vec()?; + let data = borsh::to_vec(&value)?; self.insert(column, key.to_vec(), data); Ok(()) } @@ -532,7 +532,7 @@ impl StoreUpdate { value: &T, ) -> io::Result<()> { assert!(!(column.is_rc() || column.is_insert_only()), "can't set_ser: {column}"); - let data = value.try_to_vec()?; + let data = borsh::to_vec(&value)?; self.set(column, key, &data); Ok(()) } @@ -671,7 +671,7 @@ pub fn get( /// Writes an object into Trie. pub fn set(state_update: &mut TrieUpdate, key: TrieKey, value: &T) { - let data = value.try_to_vec().expect("Borsh serializer is not expected to ever fail"); + let data = borsh::to_vec(&value).expect("Borsh serializer is not expected to ever fail"); state_update.set(key, data); } @@ -898,7 +898,11 @@ impl CompiledContractCache for StoreCompiledContractCache { // guarantee deterministic compilation, so, if we happen to compile the // same contract concurrently on two threads, the `value`s might differ, // but this doesn't matter. - update.set(DBCol::CachedContractCode, key.as_ref().to_vec(), value.try_to_vec().unwrap()); + update.set( + DBCol::CachedContractCode, + key.as_ref().to_vec(), + borsh::to_vec(&value).unwrap(), + ); self.db.write(update) } @@ -1104,7 +1108,7 @@ mod tests { core::mem::drop(file); let store = crate::test_utils::create_test_store(); assert_eq!( - std::io::ErrorKind::InvalidInput, + std::io::ErrorKind::InvalidData, store.load_state_from_file(tmp.path()).unwrap_err().kind() ); } diff --git a/core/store/src/migrations.rs b/core/store/src/migrations.rs index b81d765a803..a43a9b3e630 100644 --- a/core/store/src/migrations.rs +++ b/core/store/src/migrations.rs @@ -45,7 +45,7 @@ impl<'a> BatchedStoreUpdate<'a> { value: &T, insert: bool, ) -> std::io::Result<()> { - let value_bytes = value.try_to_vec()?; + let value_bytes = borsh::to_vec(&value)?; let entry_size = key.as_ref().len() + value_bytes.len() + 8; self.batch_size += entry_size; self.total_size_written += entry_size as u64; @@ -193,14 +193,13 @@ pub fn migrate_36_to_37(store: &Store) -> anyhow::Result<()> { update.delete_all(DBCol::FlatStateChanges); for result in store.iter(DBCol::FlatStateChanges) { let (key, old_value) = result?; - let new_value = crate::flat::FlatStateChanges( + let new_value = borsh::to_vec(&crate::flat::FlatStateChanges( LegacyFlatStateChanges::try_from_slice(&old_value)? .0 .into_iter() .map(|(key, value_ref)| (key, value_ref.map(|v| FlatStateValue::Ref(v)))) .collect(), - ) - .try_to_vec()?; + ))?; update.set(DBCol::FlatStateChanges, &key, &new_value); } update.commit()?; @@ -225,7 +224,7 @@ pub fn migrate_37_to_38(store: &Store) -> anyhow::Result<()> { LegacyFlatStateDeltaMetadata::try_from_slice(&old_value)?; let new_value = crate::flat::FlatStateDeltaMetadata { block, prev_block_with_changes: None }; - update.set(DBCol::FlatStateDeltaMetadata, &key, &new_value.try_to_vec()?); + update.set(DBCol::FlatStateDeltaMetadata, &key, &borsh::to_vec(&new_value)?); } update.commit()?; Ok(()) diff --git a/core/store/src/trie/mem/node/mutation.rs b/core/store/src/trie/mem/node/mutation.rs index 3718c6fbb8f..1912d7b951c 100644 --- a/core/store/src/trie/mem/node/mutation.rs +++ b/core/store/src/trie/mem/node/mutation.rs @@ -3,7 +3,7 @@ use crate::trie::mem::flexible_data::encoding::RawDecoderMut; use super::encoding::{CommonHeader, NodeKind, NonLeafHeader}; use super::{MemTrieNodePtr, MemTrieNodePtrMut}; -use borsh::BorshSerialize; + use near_primitives::hash::{hash, CryptoHash}; impl<'a> MemTrieNodePtrMut<'a> { @@ -58,7 +58,7 @@ impl<'a> MemTrieNodePtrMut<'a> { NodeKind::Leaf => {} _ => { let mut nonleaf = decoder.peek::(); - nonleaf.hash = hash(&raw_trie_node_with_size.try_to_vec().unwrap()); + nonleaf.hash = hash(&borsh::to_vec(&raw_trie_node_with_size).unwrap()); decoder.overwrite(nonleaf); } } diff --git a/core/store/src/trie/mem/node/tests.rs b/core/store/src/trie/mem/node/tests.rs index 623ae9a4c76..c7d3d9e445a 100644 --- a/core/store/src/trie/mem/node/tests.rs +++ b/core/store/src/trie/mem/node/tests.rs @@ -2,7 +2,6 @@ use crate::trie::mem::arena::Arena; use crate::trie::mem::node::{InputMemTrieNode, MemTrieNodeId, MemTrieNodeView}; use crate::trie::Children; use crate::{RawTrieNode, RawTrieNodeWithSize}; -use borsh::BorshSerialize; use near_primitives::hash::hash; use near_primitives::state::{FlatStateValue, ValueRef}; @@ -28,7 +27,7 @@ fn test_basic_leaf_node_inlined() { } ); assert_eq!(view.memory_usage(), 115); - assert_eq!(view.node_hash(), hash(&view.to_raw_trie_node_with_size().try_to_vec().unwrap())); + assert_eq!(view.node_hash(), hash(&borsh::to_vec(&view.to_raw_trie_node_with_size()).unwrap())); match view { MemTrieNodeView::Leaf { extension, value } => { assert_eq!(extension.raw_slice(), &[0, 1, 2, 3, 4]); @@ -58,7 +57,7 @@ fn test_basic_leaf_node_ref() { } ); assert_eq!(view.memory_usage(), 115); - assert_eq!(view.node_hash(), hash(&view.to_raw_trie_node_with_size().try_to_vec().unwrap())); + assert_eq!(view.node_hash(), hash(&borsh::to_vec(&view.to_raw_trie_node_with_size()).unwrap())); match view { MemTrieNodeView::Leaf { extension, value } => { assert_eq!(extension.raw_slice(), &[0, 1, 2, 3, 4]); @@ -90,7 +89,7 @@ fn test_basic_leaf_node_empty_extension_empty_value() { } ); assert_eq!(view.memory_usage(), 100); - assert_eq!(view.node_hash(), hash(&view.to_raw_trie_node_with_size().try_to_vec().unwrap())); + assert_eq!(view.node_hash(), hash(&borsh::to_vec(&view.to_raw_trie_node_with_size()).unwrap())); match view { MemTrieNodeView::Leaf { extension, value } => { assert!(extension.raw_slice().is_empty()); @@ -127,7 +126,7 @@ fn test_basic_extension_node() { assert_eq!(node_ptr.view().memory_usage(), child_ptr.view().memory_usage() + 60); assert_eq!( node_ptr.view().node_hash(), - hash(&node_ptr.view().to_raw_trie_node_with_size().try_to_vec().unwrap()) + hash(&borsh::to_vec(&node_ptr.view().to_raw_trie_node_with_size()).unwrap()) ); match node_ptr.view() { MemTrieNodeView::Extension { hash, memory_usage, extension, child: actual_child } => { @@ -203,7 +202,7 @@ fn test_basic_branch_node() { ); assert_eq!( node_ptr.view().node_hash(), - hash(&node_ptr.view().to_raw_trie_node_with_size().try_to_vec().unwrap()) + hash(&borsh::to_vec(&node_ptr.view().to_raw_trie_node_with_size()).unwrap()) ); match node_ptr.view() { MemTrieNodeView::Branch { hash, memory_usage, children } => { @@ -281,7 +280,7 @@ fn test_basic_branch_with_value_node() { ); assert_eq!( node_ptr.view().node_hash(), - hash(&node_ptr.view().to_raw_trie_node_with_size().try_to_vec().unwrap()) + hash(&borsh::to_vec(&node_ptr.view().to_raw_trie_node_with_size()).unwrap()) ); match node_ptr.view() { MemTrieNodeView::BranchWithValue { hash, memory_usage, children, value } => { diff --git a/core/store/src/trie/mem/node/view.rs b/core/store/src/trie/mem/node/view.rs index f2448f40cae..f54f01b69cb 100644 --- a/core/store/src/trie/mem/node/view.rs +++ b/core/store/src/trie/mem/node/view.rs @@ -1,7 +1,7 @@ use super::{MemTrieNodePtr, MemTrieNodeView}; use crate::trie::TRIE_COSTS; use crate::{RawTrieNode, RawTrieNodeWithSize}; -use borsh::BorshSerialize; + use near_primitives::hash::{hash, CryptoHash}; impl<'a> MemTrieNodeView<'a> { @@ -10,7 +10,7 @@ impl<'a> MemTrieNodeView<'a> { match self { Self::Leaf { .. } => { let node = self.clone().to_raw_trie_node_with_size(); - hash(&node.try_to_vec().unwrap()) + hash(&borsh::to_vec(&node).unwrap()) } Self::Extension { hash, .. } | Self::Branch { hash, .. } diff --git a/core/store/src/trie/mod.rs b/core/store/src/trie/mod.rs index 2f343a940ea..91f784b858c 100644 --- a/core/store/src/trie/mod.rs +++ b/core/store/src/trie/mod.rs @@ -1095,7 +1095,7 @@ impl TrieAccess for Trie { /// Methods used in the runtime-parameter-estimator for measuring trie internal /// operations. pub mod estimator { - use borsh::{BorshDeserialize, BorshSerialize}; + use borsh::BorshDeserialize; use near_primitives::hash::CryptoHash; /// Create an encoded extension node with the given value as the key. @@ -1104,7 +1104,7 @@ pub mod estimator { let hash = CryptoHash::hash_bytes(&key); let node = super::RawTrieNode::Extension(key, hash); let node = super::RawTrieNodeWithSize { node, memory_usage: 1 }; - node.try_to_vec().unwrap() + borsh::to_vec(&node).unwrap() } /// Decode am extension node and return its inner key. /// This serves no purpose other than for the estimator. diff --git a/core/store/src/trie/raw_node.rs b/core/store/src/trie/raw_node.rs index b896adf7a1d..427e25fc3d7 100644 --- a/core/store/src/trie/raw_node.rs +++ b/core/store/src/trie/raw_node.rs @@ -116,7 +116,7 @@ fn test_encode_decode() { #[track_caller] fn test(node: RawTrieNode, encoded: &[u8]) { let node = RawTrieNodeWithSize { node, memory_usage: 42 }; - let mut buf = node.try_to_vec().unwrap(); + let mut buf = borsh::to_vec(&node).unwrap(); assert_eq!(encoded, buf.as_slice()); assert_eq!(node, RawTrieNodeWithSize::try_from_slice(&buf).unwrap()); diff --git a/core/store/src/trie/shard_tries.rs b/core/store/src/trie/shard_tries.rs index 424fc8135ff..f76604e28e0 100644 --- a/core/store/src/trie/shard_tries.rs +++ b/core/store/src/trie/shard_tries.rs @@ -5,8 +5,7 @@ use crate::trie::trie_storage::{TrieCache, TrieCachingStorage}; use crate::trie::{TrieRefcountChange, POISONED_LOCK_ERR}; use crate::{metrics, DBCol, PrefetchApi}; use crate::{Store, StoreUpdate, Trie, TrieChanges, TrieUpdate}; -use borsh::BorshSerialize; -use near_primitives::borsh::maybestd::collections::HashMap; + use near_primitives::errors::StorageError; use near_primitives::hash::CryptoHash; use near_primitives::shard_layout::{self, ShardUId, ShardVersion}; @@ -14,6 +13,7 @@ use near_primitives::trie_key::TrieKey; use near_primitives::types::{ NumShards, RawStateChange, RawStateChangesWithTrieKey, StateChangeCause, StateRoot, }; +use std::collections::HashMap; use std::rc::Rc; use std::sync::{Arc, RwLock}; @@ -461,7 +461,7 @@ impl WrappedTrieChanges { store_update.set( DBCol::StateChanges, storage_key.as_ref(), - &change_with_trie_key.try_to_vec().expect("Borsh serialize cannot fail"), + &borsh::to_vec(&change_with_trie_key).expect("Borsh serialize cannot fail"), ); } } diff --git a/core/store/src/trie/split_state.rs b/core/store/src/trie/split_state.rs index 63e427ffaf4..7c9dcf7bf53 100644 --- a/core/store/src/trie/split_state.rs +++ b/core/store/src/trie/split_state.rs @@ -324,7 +324,7 @@ mod tests { use crate::{set, ShardTries, ShardUId, Trie}; use near_primitives::account::id::AccountId; - use near_primitives::borsh::BorshSerialize; + use near_primitives::hash::hash; use near_primitives::receipt::{DelayedReceiptIndices, Receipt}; use near_primitives::trie_key::TrieKey; @@ -414,12 +414,14 @@ mod tests { assert_eq!(receipts, all_receipts[start_index as usize..next_index as usize]); start_index = next_index; - let total_memory_use = receipts + let total_memory_use: u64 = receipts .iter() - .fold(0_u64, |sum, receipt| sum + receipt.try_to_vec().unwrap().len() as u64); - let memory_use_without_last_receipt = receipts[..receipts.len() - 1] + .map(|receipt| borsh::object_length(&receipt).unwrap() as u64) + .sum(); + let memory_use_without_last_receipt: u64 = receipts[..receipts.len() - 1] .iter() - .fold(0_u64, |sum, receipt| sum + receipt.try_to_vec().unwrap().len() as u64); + .map(|receipt| borsh::object_length(&receipt).unwrap() as u64) + .sum(); assert!( total_memory_use >= memory_limit.as_u64() diff --git a/genesis-tools/genesis-populate/src/state_dump.rs b/genesis-tools/genesis-populate/src/state_dump.rs index 81b9b673acc..e43aec621ba 100644 --- a/genesis-tools/genesis-populate/src/state_dump.rs +++ b/genesis-tools/genesis-populate/src/state_dump.rs @@ -1,9 +1,9 @@ -use borsh::{BorshDeserialize, BorshSerialize}; +use borsh::BorshDeserialize; use near_primitives::types::StateRoot; use near_store::db::TestDB; use near_store::Store; use std::fs::File; -use std::io::{Read, Write}; +use std::io::Read; use std::path::{Path, PathBuf}; const STATE_DUMP_FILE: &str = "state_dump"; @@ -43,9 +43,8 @@ impl StateDump { { let mut roots_files = dir; roots_files.push(GENESIS_ROOTS_FILE); - let mut file = File::create(roots_files)?; - let data = self.roots.try_to_vec()?; - file.write_all(&data)?; + let file = File::create(roots_files)?; + borsh::to_writer(&file, &self.roots)?; } Ok(()) } diff --git a/integration-tests/src/tests/client/benchmarks.rs b/integration-tests/src/tests/client/benchmarks.rs index d3d0f096111..a74de8656da 100644 --- a/integration-tests/src/tests/client/benchmarks.rs +++ b/integration-tests/src/tests/client/benchmarks.rs @@ -4,7 +4,6 @@ //! we want to test here are pretty heavy and its enough to run them once and //! note the wall-clock time. -use borsh::BorshSerialize; use near_chain::ChainGenesis; use near_chain_configs::Genesis; use near_client::test_utils::{create_chunk_on_height, TestEnv}; @@ -56,7 +55,7 @@ fn benchmark_large_chunk_production_time() { let (chunk, _, _) = create_chunk_on_height(&mut env.clients[0], 0); let time = t.elapsed(); - let size = chunk.try_to_vec().unwrap().len(); + let size = borsh::object_length(&chunk).unwrap(); eprintln!("chunk size: {}kb", size / 1024); eprintln!("time to produce: {:0.2?}", time); diff --git a/integration-tests/src/tests/client/challenges.rs b/integration-tests/src/tests/client/challenges.rs index d1638ed66ea..f9a5c965120 100644 --- a/integration-tests/src/tests/client/challenges.rs +++ b/integration-tests/src/tests/client/challenges.rs @@ -1,5 +1,5 @@ use assert_matches::assert_matches; -use borsh::BorshSerialize; + use near_async::messaging::CanSend; use near_network::shards_manager::ShardsManagerRequestFromNetwork; use near_primitives::test_utils::create_test_signer; @@ -46,8 +46,8 @@ fn test_block_with_challenges() { { let challenge_body = ChallengeBody::BlockDoubleSign(BlockDoubleSign { - left_block_header: genesis.header().try_to_vec().unwrap(), - right_block_header: genesis.header().try_to_vec().unwrap(), + left_block_header: borsh::to_vec(&genesis.header()).unwrap(), + right_block_header: borsh::to_vec(&genesis.header()).unwrap(), }); let challenge = Challenge::produce(challenge_body, &*signer); let challenges = vec![challenge]; @@ -138,8 +138,8 @@ fn test_verify_block_double_sign_challenge() { let epoch_id = b1.header().epoch_id().clone(); let valid_challenge = Challenge::produce( ChallengeBody::BlockDoubleSign(BlockDoubleSign { - left_block_header: b2.header().try_to_vec().unwrap(), - right_block_header: b1.header().try_to_vec().unwrap(), + left_block_header: borsh::to_vec(&b2.header()).unwrap(), + right_block_header: borsh::to_vec(&b1.header()).unwrap(), }), &signer, ); @@ -157,8 +157,8 @@ fn test_verify_block_double_sign_challenge() { ); let invalid_challenge = Challenge::produce( ChallengeBody::BlockDoubleSign(BlockDoubleSign { - left_block_header: b1.header().try_to_vec().unwrap(), - right_block_header: b1.header().try_to_vec().unwrap(), + left_block_header: borsh::to_vec(&b1.header()).unwrap(), + right_block_header: borsh::to_vec(&b1.header()).unwrap(), }), &signer, ); @@ -173,8 +173,8 @@ fn test_verify_block_double_sign_challenge() { let b3 = env.clients[0].produce_block(3).unwrap().unwrap(); let invalid_challenge = Challenge::produce( ChallengeBody::BlockDoubleSign(BlockDoubleSign { - left_block_header: b1.header().try_to_vec().unwrap(), - right_block_header: b3.header().try_to_vec().unwrap(), + left_block_header: borsh::to_vec(&b1.header()).unwrap(), + right_block_header: borsh::to_vec(&b3.header()).unwrap(), }), &signer, ); @@ -331,7 +331,7 @@ fn challenge( let merkle_paths = Block::compute_chunk_headers_root(block.chunks().iter()).1; let valid_challenge = Challenge::produce( ChallengeBody::ChunkProofs(ChunkProofs { - block_header: block.header().try_to_vec().unwrap(), + block_header: borsh::to_vec(&block.header()).unwrap(), chunk, merkle_proof: merkle_paths[shard_id].clone(), }), @@ -596,7 +596,7 @@ fn test_block_challenge() { let shard_id = chunk.cloned_header().shard_id(); let challenge = Challenge::produce( ChallengeBody::ChunkProofs(ChunkProofs { - block_header: block.header().try_to_vec().unwrap(), + block_header: borsh::to_vec(&block.header()).unwrap(), chunk: MaybeEncodedShardChunk::Encoded(chunk), merkle_proof: merkle_paths[shard_id as usize].clone(), }), @@ -648,7 +648,7 @@ fn test_fishermen_challenge() { let merkle_paths = Block::compute_chunk_headers_root(block.chunks().iter()).1; let shard_id = chunk.cloned_header().shard_id(); let challenge_body = ChallengeBody::ChunkProofs(ChunkProofs { - block_header: block.header().try_to_vec().unwrap(), + block_header: borsh::to_vec(&block.header()).unwrap(), chunk: MaybeEncodedShardChunk::Encoded(chunk), merkle_proof: merkle_paths[shard_id as usize].clone(), }); diff --git a/integration-tests/src/tests/client/features/zero_balance_account.rs b/integration-tests/src/tests/client/features/zero_balance_account.rs index 3537a24def5..ed7651c38bb 100644 --- a/integration-tests/src/tests/client/features/zero_balance_account.rs +++ b/integration-tests/src/tests/client/features/zero_balance_account.rs @@ -1,6 +1,5 @@ use assert_matches::assert_matches; -use borsh::BorshSerialize; use near_chain::ChainGenesis; use near_chain_configs::Genesis; use near_client::adapter::ProcessTxResponse; @@ -323,10 +322,13 @@ fn test_storage_usage_components() { const FUNCTION_ACCESS_PERMISSION_STORAGE_USAGE: usize = 98; let edwards_public_key = PublicKey::from_seed(KeyType::ED25519, "seed"); - assert_eq!(PUBLIC_KEY_STORAGE_USAGE, edwards_public_key.try_to_vec().unwrap().len()); + assert_eq!(PUBLIC_KEY_STORAGE_USAGE, borsh::object_length(&edwards_public_key).unwrap()); let full_access_key = AccessKey::full_access(); - assert_eq!(FULL_ACCESS_PERMISSION_STORAGE_USAGE, full_access_key.try_to_vec().unwrap().len()); + assert_eq!( + FULL_ACCESS_PERMISSION_STORAGE_USAGE, + borsh::object_length(&full_access_key).unwrap() + ); let fn_access_key = AccessKey { nonce: u64::MAX, @@ -336,7 +338,10 @@ fn test_storage_usage_components() { method_names: vec![], }), }; - assert_eq!(FUNCTION_ACCESS_PERMISSION_STORAGE_USAGE, fn_access_key.try_to_vec().unwrap().len()); + assert_eq!( + FUNCTION_ACCESS_PERMISSION_STORAGE_USAGE, + borsh::object_length(&fn_access_key).unwrap() + ); let config_store = RuntimeConfigStore::new(None); let config = config_store.get_config(PROTOCOL_VERSION); diff --git a/integration-tests/src/tests/client/process_blocks.rs b/integration-tests/src/tests/client/process_blocks.rs index 5e075fed65b..d350a39bf64 100644 --- a/integration-tests/src/tests/client/process_blocks.rs +++ b/integration-tests/src/tests/client/process_blocks.rs @@ -2618,7 +2618,6 @@ fn test_catchup_gas_price_change() { } let rt = Arc::clone(&env.clients[1].runtime_adapter); let f = move |msg: ApplyStatePartsRequest| { - use borsh::BorshSerialize; let store = rt.store(); let shard_id = msg.shard_uid.shard_id as ShardId; @@ -2627,7 +2626,7 @@ fn test_catchup_gas_price_change() { .remove_flat_storage_for_shard(msg.shard_uid) .unwrap()); for part_id in 0..msg.num_parts { - let key = StatePartKey(msg.sync_hash, shard_id, part_id).try_to_vec().unwrap(); + let key = borsh::to_vec(&StatePartKey(msg.sync_hash, shard_id, part_id)).unwrap(); let part = store.get(DBCol::StateParts, &key).unwrap().unwrap(); rt.apply_state_part( @@ -3321,7 +3320,7 @@ fn test_not_broadcast_block_on_accept() { #[test] fn test_header_version_downgrade() { init_test_logger(); - use borsh::ser::BorshSerialize; + let mut genesis = Genesis::test(vec!["test0".parse().unwrap(), "test1".parse().unwrap()], 1); genesis.config.epoch_length = 5; let chain_genesis = ChainGenesis::new(&genesis); @@ -3348,8 +3347,8 @@ fn test_header_version_downgrade() { header.inner_rest.latest_protocol_version = PROTOCOL_VERSION; let (hash, signature) = validator_signer.sign_block_header_parts( header.prev_hash, - &header.inner_lite.try_to_vec().expect("Failed to serialize"), - &header.inner_rest.try_to_vec().expect("Failed to serialize"), + &borsh::to_vec(&header.inner_lite).expect("Failed to serialize"), + &borsh::to_vec(&header.inner_rest).expect("Failed to serialize"), ); header.hash = hash; header.signature = signature; diff --git a/integration-tests/src/tests/client/resharding.rs b/integration-tests/src/tests/client/resharding.rs index 1aeea8ee834..94aedcf2364 100644 --- a/integration-tests/src/tests/client/resharding.rs +++ b/integration-tests/src/tests/client/resharding.rs @@ -1,6 +1,5 @@ use crate::tests::client::process_blocks::set_block_protocol_version; use assert_matches::assert_matches; -use borsh::BorshSerialize; use near_chain::near_chain_primitives::Error; use near_chain::test_utils::wait_for_all_blocks_in_processing; use near_chain::{ChainGenesis, ChainStoreAccess, Provenance}; @@ -1074,7 +1073,7 @@ fn gen_cross_contract_tx_impl( }, "id": 0 }, {"action_add_key_with_full_access": { "promise_index": 0, - "public_key": to_base64(&signer_new_account.public_key.try_to_vec().unwrap()), + "public_key": to_base64(&borsh::to_vec(&signer_new_account.public_key).unwrap()), "nonce": 0, }, "id": 0 } ], diff --git a/integration-tests/src/tests/client/state_dump.rs b/integration-tests/src/tests/client/state_dump.rs index 5b102fcb95c..0d21a071c34 100644 --- a/integration-tests/src/tests/client/state_dump.rs +++ b/integration-tests/src/tests/client/state_dump.rs @@ -1,5 +1,5 @@ use assert_matches::assert_matches; -use borsh::BorshSerialize; + use near_chain::near_chain_primitives::error::QueryError; use near_chain::{ChainGenesis, ChainStoreAccess, Provenance}; use near_chain_configs::ExternalStorageLocation::Filesystem; @@ -303,7 +303,7 @@ fn run_state_sync_with_dumped_parts( .unwrap()); for part_id in 0..num_parts { - let key = StatePartKey(sync_hash, 0, part_id).try_to_vec().unwrap(); + let key = borsh::to_vec(&StatePartKey(sync_hash, 0, part_id)).unwrap(); let part = client_0_store.get(DBCol::StateParts, &key).unwrap().unwrap(); runtime_client_1 diff --git a/integration-tests/src/tests/client/sync_state_nodes.rs b/integration-tests/src/tests/client/sync_state_nodes.rs index 39b73a27156..aad72070676 100644 --- a/integration-tests/src/tests/client/sync_state_nodes.rs +++ b/integration-tests/src/tests/client/sync_state_nodes.rs @@ -660,7 +660,6 @@ fn test_dump_epoch_missing_chunk_in_last_block() { } let rt = Arc::clone(&env.clients[1].runtime_adapter); let f = move |msg: ApplyStatePartsRequest| { - use borsh::BorshSerialize; let store = rt.store(); let shard_id = msg.shard_uid.shard_id as ShardId; @@ -671,7 +670,8 @@ fn test_dump_epoch_missing_chunk_in_last_block() { .unwrap()); for part_id in 0..msg.num_parts { - let key = StatePartKey(msg.sync_hash, shard_id, part_id).try_to_vec().unwrap(); + let key = + borsh::to_vec(&StatePartKey(msg.sync_hash, shard_id, part_id)).unwrap(); let part = store.get(DBCol::StateParts, &key).unwrap().unwrap(); rt.apply_state_part( diff --git a/integration-tests/src/tests/nearcore/rpc_error_structs.rs b/integration-tests/src/tests/nearcore/rpc_error_structs.rs index f1b36ef7cae..d6dcb9fbb10 100644 --- a/integration-tests/src/tests/nearcore/rpc_error_structs.rs +++ b/integration-tests/src/tests/nearcore/rpc_error_structs.rs @@ -1,7 +1,7 @@ use std::str::FromStr; use actix::{Actor, System}; -use borsh::BorshSerialize; + use futures::{future, FutureExt, TryFutureExt}; use crate::genesis_helpers::genesis_block; @@ -384,7 +384,7 @@ fn test_tx_invalid_tx_error() { if let Ok(Ok(block)) = res { if block.header.height > 10 { let client = new_client(&format!("http://{}", rpc_addrs_copy[2])); - let bytes = transaction_copy.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&transaction_copy).unwrap(); spawn_interruptible( client .EXPERIMENTAL_broadcast_tx_sync(to_base64(&bytes)) diff --git a/integration-tests/src/tests/nearcore/rpc_nodes.rs b/integration-tests/src/tests/nearcore/rpc_nodes.rs index e7babb43da5..1e05e4fea4f 100644 --- a/integration-tests/src/tests/nearcore/rpc_nodes.rs +++ b/integration-tests/src/tests/nearcore/rpc_nodes.rs @@ -3,7 +3,7 @@ use crate::tests::nearcore::node_cluster::NodeCluster; use actix::clock::sleep; use actix::{Actor, System}; use assert_matches::assert_matches; -use borsh::BorshSerialize; + use futures::future::join_all; use futures::{future, FutureExt, TryFutureExt}; use near_actix_test_utils::spawn_interruptible; @@ -133,7 +133,7 @@ fn test_get_execution_outcome(is_tx_successful: bool) { WaitOrTimeoutActor::new( Box::new(move |_ctx| { let client = new_client(&format!("http://{}", rpc_addrs[0])); - let bytes = transaction.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&transaction).unwrap(); let view_client1 = view_client.clone(); spawn_interruptible(client.broadcast_tx_commit(to_base64(&bytes)).then( move |res| { @@ -378,7 +378,7 @@ fn test_tx_not_enough_balance_must_return_error() { ); let client = new_client(&format!("http://{}", rpc_addrs[0])); - let bytes = transaction.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&transaction).unwrap(); spawn_interruptible(async move { loop { @@ -443,7 +443,7 @@ fn test_send_tx_sync_returns_transaction_hash() { let client = new_client(&format!("http://{}", rpc_addrs[0])); let tx_hash = transaction.get_hash(); - let bytes = transaction.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&transaction).unwrap(); spawn_interruptible(async move { loop { @@ -492,7 +492,7 @@ fn test_send_tx_sync_to_lightclient_must_be_routed() { let client = new_client(&format!("http://{}", rpc_addrs[1])); let tx_hash = transaction.get_hash(); - let bytes = transaction.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&transaction).unwrap(); spawn_interruptible(async move { loop { @@ -552,7 +552,7 @@ fn test_check_unknown_tx_must_return_error() { let client = new_client(&format!("http://{}", rpc_addrs[0])); let tx_hash = transaction.get_hash(); - let bytes = transaction.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&transaction).unwrap(); spawn_interruptible(async move { loop { diff --git a/integration-tests/src/user/rpc_user.rs b/integration-tests/src/user/rpc_user.rs index c89858d61a0..a4f07f3c481 100644 --- a/integration-tests/src/user/rpc_user.rs +++ b/integration-tests/src/user/rpc_user.rs @@ -2,7 +2,6 @@ use std::sync::Arc; use std::thread; use std::time::Duration; -use borsh::BorshSerialize; use futures::{Future, TryFutureExt}; use near_client::StatusResponse; @@ -116,7 +115,7 @@ impl User for RpcUser { } fn add_transaction(&self, transaction: SignedTransaction) -> Result<(), ServerError> { - let bytes = transaction.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&transaction).unwrap(); let _ = self.actix(move |client| client.broadcast_tx_async(to_base64(&bytes))).map_err( |err| { serde_json::from_value::( @@ -132,7 +131,7 @@ impl User for RpcUser { &self, transaction: SignedTransaction, ) -> Result { - let bytes = transaction.try_to_vec().unwrap(); + let bytes = borsh::to_vec(&transaction).unwrap(); let result = self.actix(move |client| client.broadcast_tx_commit(to_base64(&bytes))); // Wait for one more block, to make sure all nodes actually apply the state transition. let height = self.get_best_height().unwrap(); diff --git a/nearcore/src/entity_debug.rs b/nearcore/src/entity_debug.rs index fb9ddbfccef..a34523fda51 100644 --- a/nearcore/src/entity_debug.rs +++ b/nearcore/src/entity_debug.rs @@ -1,6 +1,6 @@ use crate::entity_debug_serializer::serialize_entity; use anyhow::anyhow; -use borsh::BorshSerialize; + use near_chain::types::RuntimeAdapter; use near_chain::{Block, BlockHeader}; use near_epoch_manager::EpochManagerAdapter; @@ -44,7 +44,7 @@ impl EntityDebugHandlerImpl { EntityQuery::BlockByHash { block_hash } => { let block = self .store - .get_ser::(DBCol::Block, &block_hash.try_to_vec().unwrap())? + .get_ser::(DBCol::Block, &borsh::to_vec(&block_hash).unwrap())? .ok_or_else(|| anyhow!("Block not found"))?; let author = self .epoch_manager @@ -54,21 +54,27 @@ impl EntityDebugHandlerImpl { EntityQuery::BlockHashByHeight { block_height } => { let block_hash = self .store - .get_ser::(DBCol::BlockHeight, &block_height.try_to_vec().unwrap())? + .get_ser::( + DBCol::BlockHeight, + &borsh::to_vec(&block_height).unwrap(), + )? .ok_or_else(|| anyhow!("Block height not found"))?; Ok(serialize_entity(&block_hash)) } EntityQuery::BlockHeaderByHash { block_hash } => { let block_header = self .store - .get_ser::(DBCol::BlockHeader, &block_hash.try_to_vec().unwrap())? + .get_ser::( + DBCol::BlockHeader, + &borsh::to_vec(&block_hash).unwrap(), + )? .ok_or_else(|| anyhow!("Block header not found"))?; Ok(serialize_entity(&BlockHeaderView::from(block_header))) } EntityQuery::ChunkByHash { chunk_hash } => { let chunk = self .store - .get_ser::(DBCol::Chunks, &chunk_hash.try_to_vec().unwrap())? + .get_ser::(DBCol::Chunks, &borsh::to_vec(&chunk_hash).unwrap())? .ok_or_else(|| anyhow!("Chunk not found"))?; let epoch_id = self.epoch_manager.get_epoch_id_from_prev_block(chunk.prev_block())?; @@ -99,7 +105,7 @@ impl EntityDebugHandlerImpl { .store .get_ser::( DBCol::FlatStateChanges, - &KeyForFlatStateDelta { block_hash, shard_uid }.try_to_vec().unwrap(), + &borsh::to_vec(&KeyForFlatStateDelta { block_hash, shard_uid }).unwrap(), )? .ok_or_else(|| anyhow!("Flat state changes not found"))?; let mut changes_view = Vec::new(); @@ -118,7 +124,7 @@ impl EntityDebugHandlerImpl { .store .get_ser::( DBCol::FlatStateDeltaMetadata, - &KeyForFlatStateDelta { block_hash, shard_uid }.try_to_vec().unwrap(), + &borsh::to_vec(&KeyForFlatStateDelta { block_hash, shard_uid }).unwrap(), )? .ok_or_else(|| anyhow!("Flat state delta metadata not found"))?; Ok(serialize_entity(&metadata)) @@ -128,7 +134,7 @@ impl EntityDebugHandlerImpl { .store .get_ser::( DBCol::FlatStorageStatus, - &shard_uid.try_to_vec().unwrap(), + &borsh::to_vec(&shard_uid).unwrap(), )? .ok_or_else(|| anyhow!("Flat storage status not found"))?; Ok(serialize_entity(&status)) @@ -139,7 +145,7 @@ impl EntityDebugHandlerImpl { .store .iter_prefix_ser::( DBCol::TransactionResultForBlock, - &outcome_id.try_to_vec().unwrap(), + &borsh::to_vec(&outcome_id).unwrap(), ) .next() .ok_or_else(|| anyhow!("Outcome not found"))??; @@ -162,7 +168,7 @@ impl EntityDebugHandlerImpl { EntityQuery::ReceiptById { receipt_id } => { let receipt = self .store - .get_ser::(DBCol::Receipts, &receipt_id.try_to_vec().unwrap())? + .get_ser::(DBCol::Receipts, &borsh::to_vec(&receipt_id).unwrap())? .ok_or_else(|| anyhow!("Receipt not found"))?; Ok(serialize_entity(&ReceiptView::from(receipt))) } @@ -209,7 +215,7 @@ impl EntityDebugHandlerImpl { .store .get_ser::( DBCol::Transactions, - &transaction_hash.try_to_vec().unwrap(), + &borsh::to_vec(&transaction_hash).unwrap(), )? .ok_or_else(|| anyhow!("Transaction not found"))?; Ok(serialize_entity(&SignedTransactionView::from(transaction))) @@ -313,7 +319,7 @@ impl EntityDebugHandlerImpl { EntityQuery::TrieRootByChunkHash { chunk_hash } => { let chunk = self .store - .get_ser::(DBCol::Chunks, &chunk_hash.try_to_vec().unwrap())? + .get_ser::(DBCol::Chunks, &borsh::to_vec(&chunk_hash).unwrap())? .ok_or_else(|| anyhow!("Chunk not found"))?; let shard_layout = self .epoch_manager diff --git a/nearcore/src/runtime/mod.rs b/nearcore/src/runtime/mod.rs index 20d4aa1cf8d..ef32a80b603 100644 --- a/nearcore/src/runtime/mod.rs +++ b/nearcore/src/runtime/mod.rs @@ -1,7 +1,7 @@ use crate::metrics; use crate::migrations::load_migration_data; use crate::NearConfig; -use borsh::ser::BorshSerialize; + use borsh::BorshDeserialize; use errors::FromStateViewerErrors; use near_chain::types::{ApplySplitStateResult, ApplyTransactionResult, RuntimeAdapter, Tip}; @@ -543,14 +543,13 @@ impl NightshadeRuntime { .tries .get_trie_with_block_hash_for_shard_from_snapshot(shard_uid, *state_root, &prev_hash) .map_err(|err| Error::Other(err.to_string()))?; - let state_part = match snapshot_trie.get_trie_nodes_for_part_with_flat_storage(part_id, partial_state, nibbles_begin, nibbles_end, &trie_with_state) { + let state_part = borsh::to_vec(&match snapshot_trie.get_trie_nodes_for_part_with_flat_storage(part_id, partial_state, nibbles_begin, nibbles_end, &trie_with_state) { Ok(partial_state) => partial_state, Err(err) => { error!(target: "runtime", ?err, part_id.idx, part_id.total, %prev_hash, %state_root, %shard_id, "Can't get trie nodes for state part"); return Err(err.into()); } - } - .try_to_vec() + }) .expect("serializer should not fail"); Ok(state_part) diff --git a/nearcore/src/state_sync.rs b/nearcore/src/state_sync.rs index b3596b0aa8e..a08ac5a5868 100644 --- a/nearcore/src/state_sync.rs +++ b/nearcore/src/state_sync.rs @@ -1,5 +1,5 @@ use crate::metrics; -use borsh::BorshSerialize; + use near_chain::types::RuntimeAdapter; use near_chain::{Chain, ChainGenesis, ChainStoreAccess, DoomslugThresholdMode, Error}; use near_chain_configs::{ClientConfig, ExternalStorageLocation}; @@ -466,7 +466,7 @@ fn obtain_and_store_state_part( PartId::new(part_id, num_parts), )?; - let key = StatePartKey(sync_hash, shard_id, part_id).try_to_vec()?; + let key = borsh::to_vec(&StatePartKey(sync_hash, shard_id, part_id))?; let mut store_update = chain.store().store().store_update(); store_update.set(DBCol::StateParts, &key, &state_part); store_update.commit()?; diff --git a/runtime/near-vm-runner/src/logic/tests/gas_counter.rs b/runtime/near-vm-runner/src/logic/tests/gas_counter.rs index b46bf250783..65d8849e57b 100644 --- a/runtime/near-vm-runner/src/logic/tests/gas_counter.rs +++ b/runtime/near-vm-runner/src/logic/tests/gas_counter.rs @@ -3,7 +3,7 @@ use crate::logic::tests::vm_logic_builder::{TestVMLogic, VMLogicBuilder}; use crate::logic::types::Gas; use crate::logic::{Config, MemSlice}; use crate::logic::{HostError, VMLogicError}; -use borsh::BorshSerialize; + use expect_test::expect; use near_primitives_core::config::{ActionCosts, ExtCosts}; use near_primitives_core::runtime::fees::Fee; @@ -721,11 +721,12 @@ fn op_limit(gas_limit: Gas) -> u32 { } fn test_pk() -> Vec { - let pk = "ed25519:22W5rKuvbMRphnDoCj6nfrWhRKvh9Xf9SWXfGHaeXGde" - .parse::() - .unwrap() - .try_to_vec() - .unwrap(); + let pk = borsh::to_vec( + &"ed25519:22W5rKuvbMRphnDoCj6nfrWhRKvh9Xf9SWXfGHaeXGde" + .parse::() + .unwrap(), + ) + .unwrap(); pk } diff --git a/runtime/near-vm-runner/src/logic/tests/promises.rs b/runtime/near-vm-runner/src/logic/tests/promises.rs index 9c429fb1dc6..1352f0acd08 100644 --- a/runtime/near-vm-runner/src/logic/tests/promises.rs +++ b/runtime/near-vm-runner/src/logic/tests/promises.rs @@ -2,7 +2,7 @@ use crate::logic::mocks::mock_external::MockedExternal; use crate::logic::tests::helpers::*; use crate::logic::tests::vm_logic_builder::VMLogicBuilder; use crate::logic::types::PromiseResult; -use borsh::BorshSerialize; + use near_crypto::PublicKey; use serde_json; @@ -365,11 +365,10 @@ fn test_promise_batch_action_stake() { let mut logic_builder = VMLogicBuilder::default(); let mut logic = logic_builder.build(); let index = promise_create(&mut logic, b"rick.test", 0, 0).expect("should create a promise"); - let key = "ed25519:5do5nkAEVhL8iteDvXNgxi4pWK78Y7DDadX11ArFNyrf" - .parse::() - .unwrap() - .try_to_vec() - .unwrap(); + let key = borsh::to_vec( + &"ed25519:5do5nkAEVhL8iteDvXNgxi4pWK78Y7DDadX11ArFNyrf".parse::().unwrap(), + ) + .unwrap(); let key = logic.internal_mem_write(&key); let index_ptr = logic.internal_mem_write(&index.to_le_bytes()).ptr; @@ -443,11 +442,10 @@ fn test_promise_batch_action_add_key_with_function_call() { let mut logic = logic_builder.build(); let index = promise_create(&mut logic, b"rick.test", 0, 0).expect("should create a promise"); let index_ptr = logic.internal_mem_write(&index.to_le_bytes()).ptr; - let key = "ed25519:5do5nkAEVhL8iteDvXNgxi4pWK78Y7DDadX11ArFNyrf" - .parse::() - .unwrap() - .try_to_vec() - .unwrap(); + let key = borsh::to_vec( + &"ed25519:5do5nkAEVhL8iteDvXNgxi4pWK78Y7DDadX11ArFNyrf".parse::().unwrap(), + ) + .unwrap(); let nonce = 1; let allowance = 999u128; let receiver_id = b"sam"; diff --git a/runtime/near-vm-runner/src/profile.rs b/runtime/near-vm-runner/src/profile.rs index a25a62e94dd..c99fe86abc9 100644 --- a/runtime/near-vm-runner/src/profile.rs +++ b/runtime/near-vm-runner/src/profile.rs @@ -393,7 +393,7 @@ mod test { for (i, cost) in ActionCosts::iter().enumerate() { profile_data.add_action_cost(cost, i as Gas + 1000); } - let buf = profile_data.try_to_vec().expect("failed serializing a normal profile"); + let buf = borsh::to_vec(&profile_data).expect("failed serializing a normal profile"); let restored: ProfileDataV3 = BorshDeserialize::deserialize(&mut buf.as_slice()) .expect("failed deserializing a normal profile"); diff --git a/runtime/runtime/src/actions.rs b/runtime/runtime/src/actions.rs index af3dffa3a8c..c594f347bb5 100644 --- a/runtime/runtime/src/actions.rs +++ b/runtime/runtime/src/actions.rs @@ -5,7 +5,7 @@ use crate::config::{ use crate::ext::{ExternalError, RuntimeExt}; use crate::receipt_manager::ReceiptManager; use crate::{metrics, ActionResult, ApplyState}; -use borsh::BorshSerialize; + use near_crypto::PublicKey; use near_primitives::account::{AccessKey, AccessKeyPermission, Account}; use near_primitives::action::delegate::{DelegateAction, SignedDelegateAction}; @@ -81,9 +81,7 @@ pub(crate) fn execute_function_call( let context = VMContext { current_account_id: runtime_ext.account_id().clone(), signer_account_id: action_receipt.signer_id.clone(), - signer_account_pk: action_receipt - .signer_public_key - .try_to_vec() + signer_account_pk: borsh::to_vec(&action_receipt.signer_public_key) .expect("Failed to serialize"), predecessor_account_id: predecessor_id.clone(), input: function_call.args.clone(), @@ -461,7 +459,7 @@ pub(crate) fn action_implicit_account_creation_transfer( CryptoHash::default(), fee_config.storage_usage_config.num_bytes_account + public_key.len() as u64 - + access_key.try_to_vec().unwrap().len() as u64 + + borsh::object_length(&access_key).unwrap() as u64 + fee_config.storage_usage_config.num_extra_bytes_record, )); @@ -552,12 +550,12 @@ pub(crate) fn action_delete_key( let storage_usage_config = &fee_config.storage_usage_config; let storage_usage = if current_protocol_version >= DELETE_KEY_STORAGE_USAGE_PROTOCOL_VERSION { - delete_key.public_key.try_to_vec().unwrap().len() as u64 - + access_key.try_to_vec().unwrap().len() as u64 + borsh::object_length(&delete_key.public_key).unwrap() as u64 + + borsh::object_length(&access_key).unwrap() as u64 + storage_usage_config.num_extra_bytes_record } else { - delete_key.public_key.try_to_vec().unwrap().len() as u64 - + Some(access_key).try_to_vec().unwrap().len() as u64 + borsh::object_length(&delete_key.public_key).unwrap() as u64 + + borsh::object_length(&Some(access_key)).unwrap() as u64 + storage_usage_config.num_extra_bytes_record }; // Remove access key @@ -607,8 +605,8 @@ pub(crate) fn action_add_key( account .storage_usage() .checked_add( - add_key.public_key.try_to_vec().unwrap().len() as u64 - + add_key.access_key.try_to_vec().unwrap().len() as u64 + borsh::object_length(&add_key.public_key).unwrap() as u64 + + borsh::object_length(&add_key.access_key).unwrap() as u64 + storage_config.num_extra_bytes_record, ) .ok_or_else(|| { diff --git a/runtime/runtime/src/verifier.rs b/runtime/runtime/src/verifier.rs index ecd91a764bb..08323a15948 100644 --- a/runtime/runtime/src/verifier.rs +++ b/runtime/runtime/src/verifier.rs @@ -539,7 +539,6 @@ fn truncate_string(s: &str, limit: usize) -> String { mod tests { use std::sync::Arc; - use crate::near_primitives::borsh::BorshSerialize; use near_crypto::{InMemorySigner, KeyType, PublicKey, Signature, Signer}; use near_primitives::account::{AccessKey, FunctionCallPermission}; use near_primitives::action::delegate::{DelegateAction, NonDelegateAction}; @@ -625,8 +624,8 @@ mod tests { initial_account .storage_usage() .checked_add( - public_key.try_to_vec().unwrap().len() as u64 - + access_key.try_to_vec().unwrap().len() as u64 + borsh::object_length(&public_key).unwrap() as u64 + + borsh::object_length(&access_key).unwrap() as u64 + 40, // storage_config.num_extra_bytes_record, ) .unwrap(), diff --git a/runtime/runtime/tests/test_async_calls.rs b/runtime/runtime/tests/test_async_calls.rs index 7890e2fb96b..cead94c01d2 100644 --- a/runtime/runtime/tests/test_async_calls.rs +++ b/runtime/runtime/tests/test_async_calls.rs @@ -1,5 +1,5 @@ use crate::runtime_group_tools::RuntimeGroup; -use borsh::ser::BorshSerialize; + use near_crypto::{InMemorySigner, KeyType}; use near_primitives::account::{AccessKeyPermission, FunctionCallPermission}; use near_primitives::hash::CryptoHash; @@ -614,7 +614,7 @@ fn test_create_account_with_transfer_and_full_key() { }, "id": 0 }, {"action_add_key_with_full_access": { "promise_index": 0, - "public_key": to_base64(&signer_new_account.public_key.try_to_vec().unwrap()), + "public_key": to_base64(&borsh::to_vec(&signer_new_account.public_key).unwrap()), "nonce": 0, }, "id": 0 } ]); @@ -687,7 +687,7 @@ fn test_account_factory() { }, "id": 0 }, {"action_add_key_with_function_call": { "promise_index": 0, - "public_key": to_base64(&signer_new_account.public_key.try_to_vec().unwrap()), + "public_key": to_base64(&borsh::to_vec(&signer_new_account.public_key).unwrap()), "nonce": 0, "allowance": (TESTING_INIT_BALANCE / 2).to_string(), "receiver_id": "near_1", @@ -844,7 +844,7 @@ fn test_create_account_add_key_call_delete_key_delete_account() { }, "id": 0 }, {"action_add_key_with_full_access": { "promise_index": 0, - "public_key": to_base64(&signer_new_account.public_key.try_to_vec().unwrap()), + "public_key": to_base64(&borsh::to_vec(&signer_new_account.public_key).unwrap()), "nonce": 1, }, "id": 0 }, {"action_deploy_contract": { @@ -868,7 +868,7 @@ fn test_create_account_add_key_call_delete_key_delete_account() { }, "id": 0 }, {"action_delete_key": { "promise_index": 0, - "public_key": to_base64(&signer_new_account.public_key.try_to_vec().unwrap()), + "public_key": to_base64(&borsh::to_vec(&signer_new_account.public_key).unwrap()), "nonce": 0, }, "id": 0 }, {"action_delete_account": { diff --git a/tools/amend-genesis/src/lib.rs b/tools/amend-genesis/src/lib.rs index 391745690b1..ae6373e25d8 100644 --- a/tools/amend-genesis/src/lib.rs +++ b/tools/amend-genesis/src/lib.rs @@ -1,5 +1,5 @@ use anyhow::Context; -use borsh::BorshSerialize; + use near_chain_configs::{Genesis, GenesisValidationMode}; use near_crypto::PublicKey; use near_primitives::hash::CryptoHash; @@ -107,7 +107,7 @@ impl AccountRecords { for (public_key, access_key) in self.keys { let storage_usage = account.storage_usage() + public_key.len() as u64 - + access_key.try_to_vec().unwrap().len() as u64 + + borsh::object_length(&access_key).unwrap() as u64 + num_extra_bytes_record; account.set_storage_usage(storage_usage); diff --git a/tools/fork-network/src/cli.rs b/tools/fork-network/src/cli.rs index 3bf6dcbeef5..c2a22179c7a 100644 --- a/tools/fork-network/src/cli.rs +++ b/tools/fork-network/src/cli.rs @@ -6,7 +6,10 @@ use near_primitives::account::AccessKey; use near_primitives::runtime::config_store::RuntimeConfigStore; use near_primitives::version::PROTOCOL_VERSION; use near_primitives::{ - account::Account, borsh::BorshSerialize, hash::CryptoHash, types::AccountInfo, + account::Account, + borsh::{self}, + hash::CryptoHash, + types::AccountInfo, }; use near_store::{flat::FlatStorageStatus, DBCol, Mode, NodeStorage, HEAD_KEY}; use nearcore::{load_config, NightshadeRuntime, NEAR_BASE}; @@ -131,7 +134,7 @@ impl ForkNetworkCommand { let fork_head_block = store .get_ser::( DBCol::Block, - &fork_head.try_to_vec().unwrap(), + &borsh::to_vec(&fork_head).unwrap(), )? .unwrap(); diff --git a/tools/fork-network/src/storage_mutator.rs b/tools/fork-network/src/storage_mutator.rs index 029f572f68e..eeeff2076cf 100644 --- a/tools/fork-network/src/storage_mutator.rs +++ b/tools/fork-network/src/storage_mutator.rs @@ -5,7 +5,7 @@ use near_crypto::PublicKey; use near_epoch_manager::EpochManagerAdapter; use near_primitives::{ account::{AccessKey, Account}, - borsh::BorshSerialize, + borsh::{self}, hash::CryptoHash, trie_key::TrieKey, types::{AccountId, EpochId, StateRoot}, @@ -51,7 +51,7 @@ impl StorageMutator { pub fn set_account(&mut self, account_id: AccountId, value: Account) -> anyhow::Result<()> { let shard_id = self.epoch_manager.account_id_to_shard_id(&account_id, &self.epoch_id)?; - self.tries[shard_id as usize].set(TrieKey::Account { account_id }, value.try_to_vec()?); + self.tries[shard_id as usize].set(TrieKey::Account { account_id }, borsh::to_vec(&value)?); Ok(()) } @@ -63,7 +63,7 @@ impl StorageMutator { ) -> anyhow::Result<()> { let shard_id = self.epoch_manager.account_id_to_shard_id(&account_id, &self.epoch_id)?; self.tries[shard_id as usize] - .set(TrieKey::AccessKey { account_id, public_key }, access_key.try_to_vec()?); + .set(TrieKey::AccessKey { account_id, public_key }, borsh::to_vec(&access_key)?); Ok(()) } diff --git a/tools/mirror/src/lib.rs b/tools/mirror/src/lib.rs index b3c4dad8be5..dfb23c10da7 100644 --- a/tools/mirror/src/lib.rs +++ b/tools/mirror/src/lib.rs @@ -112,7 +112,7 @@ enum NonceUpdater { // returns bytes that serve as the key corresponding to this pair in the Nonces column fn nonce_col_key(account_id: &AccountId, public_key: &PublicKey) -> Vec { - (account_id.clone(), public_key.clone()).try_to_vec().unwrap() + borsh::to_vec(&(account_id.clone(), public_key.clone())).unwrap() } // this serves a similar purpose to `LatestTargetNonce`. The difference is @@ -163,7 +163,11 @@ fn put_target_nonce( ) -> anyhow::Result<()> { tracing::debug!(target: "mirror", "storing {:?} in DB for ({}, {:?})", &nonce, account_id, public_key); let db_key = nonce_col_key(account_id, public_key); - db.put_cf(db.cf_handle(DBCol::Nonces.name()).unwrap(), &db_key, &nonce.try_to_vec().unwrap())?; + db.put_cf( + db.cf_handle(DBCol::Nonces.name()).unwrap(), + &db_key, + &borsh::to_vec(&nonce).unwrap(), + )?; Ok(()) } @@ -179,7 +183,10 @@ fn read_pending_outcome( id: &CryptoHash, ) -> anyhow::Result>> { Ok(db - .get_cf(db.cf_handle(DBCol::AccessKeyOutcomes.name()).unwrap(), &id.try_to_vec().unwrap())? + .get_cf( + db.cf_handle(DBCol::AccessKeyOutcomes.name()).unwrap(), + &borsh::to_vec(&id).unwrap(), + )? .map(|v| HashSet::try_from_slice(&v).unwrap())) } @@ -191,8 +198,8 @@ fn put_pending_outcome( tracing::debug!(target: "mirror", "storing {:?} in DB for {:?}", &access_keys, &id); Ok(db.put_cf( db.cf_handle(DBCol::AccessKeyOutcomes.name()).unwrap(), - &id.try_to_vec().unwrap(), - &access_keys.try_to_vec().unwrap(), + &borsh::to_vec(&id).unwrap(), + &borsh::to_vec(&access_keys).unwrap(), )?) } @@ -200,7 +207,7 @@ fn delete_pending_outcome(db: &DB, id: &CryptoHash) -> anyhow::Result<()> { tracing::debug!(target: "mirror", "deleting {:?} from DB", &id); Ok(db.delete_cf( db.cf_handle(DBCol::AccessKeyOutcomes.name()).unwrap(), - &id.try_to_vec().unwrap(), + &borsh::to_vec(&id).unwrap(), )?) } @@ -215,7 +222,7 @@ fn set_last_source_height(db: &DB, height: BlockHeight) -> anyhow::Result<()> { db.put_cf( db.cf_handle(DBCol::Misc.name()).unwrap(), "last_source_height", - height.try_to_vec().unwrap(), + borsh::to_vec(&height).unwrap(), )?; Ok(()) } diff --git a/tools/speedy_sync/src/main.rs b/tools/speedy_sync/src/main.rs index b98bf1f12ae..29109ce4bb3 100644 --- a/tools/speedy_sync/src/main.rs +++ b/tools/speedy_sync/src/main.rs @@ -187,7 +187,7 @@ fn create_snapshot(create_cmd: CreateCmd) { fs::write( Path::new(&create_cmd.destination_dir).join("snapshot.borsh"), - checkpoint.try_to_vec().unwrap(), + borsh::to_vec(&checkpoint).unwrap(), ) .expect("Failed writing to destination file"); diff --git a/tools/state-viewer/src/apply_chain_range.rs b/tools/state-viewer/src/apply_chain_range.rs index b9dc3d2407d..82dce57411d 100644 --- a/tools/state-viewer/src/apply_chain_range.rs +++ b/tools/state-viewer/src/apply_chain_range.rs @@ -4,7 +4,6 @@ use near_chain::types::{ApplyTransactionResult, RuntimeAdapter}; use near_chain::{ChainStore, ChainStoreAccess, ChainStoreUpdate}; use near_chain_configs::Genesis; use near_epoch_manager::{EpochManagerAdapter, EpochManagerHandle}; -use near_primitives::borsh::maybestd::sync::Arc; use near_primitives::hash::CryptoHash; use near_primitives::receipt::DelayedReceiptIndices; use near_primitives::transaction::{Action, ExecutionOutcomeWithId, ExecutionOutcomeWithProof}; @@ -17,7 +16,7 @@ use rayon::iter::{IntoParallelIterator, ParallelIterator}; use std::fs::File; use std::io::Write; use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::Mutex; +use std::sync::{Arc, Mutex}; fn timestamp_ms() -> u64 { use std::time::{SystemTime, UNIX_EPOCH}; diff --git a/tools/state-viewer/src/cli.rs b/tools/state-viewer/src/cli.rs index a09a0ecdf9e..6fbefafe05d 100644 --- a/tools/state-viewer/src/cli.rs +++ b/tools/state-viewer/src/cli.rs @@ -2,7 +2,7 @@ use crate::commands::*; use crate::contract_accounts::ContractAccountFilter; use crate::rocksdb_stats::get_rocksdb_stats; use crate::trie_iteration_benchmark::TrieIterationBenchmarkCmd; -use borsh::BorshSerialize; + use near_chain_configs::{GenesisChangeConfig, GenesisValidationMode}; use near_primitives::account::id::AccountId; use near_primitives::hash::CryptoHash; @@ -562,7 +562,7 @@ impl ScanDbColumnCmd { (None, Some(bytes), None) => { Some(bytes.split(",").map(|s| s.parse::().unwrap()).collect::>()) } - (None, None, Some(hash)) => Some(hash.try_to_vec().unwrap()), + (None, None, Some(hash)) => Some(borsh::to_vec(&hash).unwrap()), _ => panic!("Need to provide exactly one of bytes, str, or hash"), } } diff --git a/tools/state-viewer/src/contract_accounts.rs b/tools/state-viewer/src/contract_accounts.rs index 225af8ec4dd..95bf5d9c8c8 100644 --- a/tools/state-viewer/src/contract_accounts.rs +++ b/tools/state-viewer/src/contract_accounts.rs @@ -692,11 +692,7 @@ mod tests { key: &impl BorshSerialize, value: &impl BorshSerialize, ) -> (DBCol, Vec, Vec) { - ( - col, - borsh::ser::BorshSerialize::try_to_vec(key).unwrap(), - borsh::ser::BorshSerialize::try_to_vec(value).unwrap(), - ) + (col, borsh::to_vec(key).unwrap(), borsh::to_vec(value).unwrap()) } /// A filter that collects all data. diff --git a/tools/state-viewer/src/state_changes.rs b/tools/state-viewer/src/state_changes.rs index 37b04021fa9..9dee25a6c7a 100644 --- a/tools/state-viewer/src/state_changes.rs +++ b/tools/state-viewer/src/state_changes.rs @@ -1,4 +1,4 @@ -use borsh::{BorshDeserialize, BorshSerialize}; +use borsh::BorshDeserialize; use near_chain::types::RuntimeAdapter; use near_chain::{ChainStore, ChainStoreAccess}; use near_epoch_manager::{EpochManager, EpochManagerAdapter}; @@ -112,7 +112,7 @@ fn dump_state_changes( let state_changes_for_block_range = StateChangesForBlockRange { blocks }; tracing::info!(target: "state-changes", ?file, "Writing state changes to a file"); - let data: Vec = state_changes_for_block_range.try_to_vec().unwrap(); + let data: Vec = borsh::to_vec(&state_changes_for_block_range).unwrap(); std::fs::write(&file, data).unwrap(); } diff --git a/tools/state-viewer/src/state_dump.rs b/tools/state-viewer/src/state_dump.rs index c78baf1f07f..58862b47be5 100644 --- a/tools/state-viewer/src/state_dump.rs +++ b/tools/state-viewer/src/state_dump.rs @@ -1,4 +1,3 @@ -use borsh::BorshSerialize; use chrono::Utc; use near_chain::types::RuntimeAdapter; use near_chain_configs::{Genesis, GenesisChangeConfig, GenesisConfig}; @@ -157,7 +156,7 @@ pub fn state_dump_redis( block_hash.as_ref(), block_height, )?; - let value = account.try_to_vec().unwrap(); + let value = borsh::to_vec(&account).unwrap(); redis_connection.set( [b"account-data:", redis_key, b":", block_hash.as_ref()].concat(), value,