From 8c136b1da274a70f1629c559d9e571bb0759081a Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Tue, 13 Aug 2019 22:28:30 -0700 Subject: [PATCH 01/25] Step 1. Remove protos. --- Cargo.lock | 94 +----- Cargo.toml | 2 - chain/chain/Cargo.toml | 2 - chain/jsonrpc/Cargo.toml | 2 - chain/jsonrpc/client/Cargo.toml | 1 - chain/jsonrpc/src/lib.rs | 18 +- chain/jsonrpc/tests/rpc_transactions.rs | 15 +- chain/network/Cargo.toml | 2 - chain/network/src/codec.rs | 19 +- chain/network/src/types.rs | 319 +----------------- core/primitives/Cargo.toml | 4 +- core/primitives/src/account.rs | 88 +---- core/primitives/src/block.rs | 161 ++------- core/primitives/src/crypto/group_signature.rs | 22 -- core/primitives/src/crypto/signature.rs | 22 -- core/primitives/src/network.rs | 62 ---- core/primitives/src/receipt.rs | 130 +------ core/primitives/src/transaction.rs | 292 +--------------- core/primitives/src/types.rs | 30 -- core/primitives/src/utils.rs | 25 -- core/protos/Cargo.toml | 13 - core/protos/README.md | 4 - core/protos/build.rs | 5 - core/protos/protos/access_key.proto | 18 - core/protos/protos/account.proto | 14 - core/protos/protos/chain.proto | 28 -- core/protos/protos/network.proto | 87 ----- core/protos/protos/public_key.proto | 9 - core/protos/protos/receipt.proto | 45 --- core/protos/protos/signed_transaction.proto | 73 ---- core/protos/protos/types.proto | 21 -- core/protos/protos/uint128.proto | 6 - core/protos/protos/wrappers.proto | 123 ------- core/protos/src/lib.rs | 13 - core/protos/src/uint128_ext.rs | 54 --- core/store/Cargo.toml | 2 - core/store/src/lib.rs | 37 +- runtime/runtime/Cargo.toml | 2 - runtime/verifier/Cargo.toml | 1 - test-utils/loadtester/Cargo.toml | 2 - test-utils/loadtester/src/remote_node.rs | 13 +- test-utils/state-viewer/Cargo.toml | 2 - test-utils/state-viewer/src/main.rs | 12 +- test-utils/testlib/Cargo.toml | 2 - test-utils/testlib/src/user/rpc_user.rs | 15 +- 45 files changed, 112 insertions(+), 1799 deletions(-) delete mode 100644 core/protos/Cargo.toml delete mode 100644 core/protos/README.md delete mode 100644 core/protos/build.rs delete mode 100644 core/protos/protos/access_key.proto delete mode 100644 core/protos/protos/account.proto delete mode 100644 core/protos/protos/chain.proto delete mode 100644 core/protos/protos/network.proto delete mode 100644 core/protos/protos/public_key.proto delete mode 100644 core/protos/protos/receipt.proto delete mode 100644 core/protos/protos/signed_transaction.proto delete mode 100644 core/protos/protos/types.proto delete mode 100644 core/protos/protos/uint128.proto delete mode 100644 core/protos/protos/wrappers.proto delete mode 100644 core/protos/src/lib.rs delete mode 100644 core/protos/src/uint128_ext.rs diff --git a/Cargo.lock b/Cargo.lock index 1e869a3c129..991ff9a9309 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1460,6 +1460,11 @@ dependencies = [ "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "half" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "hashbrown" version = "0.1.8" @@ -1807,9 +1812,7 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "near 0.2.7", "near-primitives 0.1.0", - "near-protos 0.1.0", "node-runtime 0.0.1", - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.18 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2058,9 +2061,7 @@ dependencies = [ "kvdb 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "near-primitives 0.1.0", - "near-protos 0.1.0", "near-store 0.1.0", - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2109,9 +2110,7 @@ dependencies = [ "near-network 0.1.0", "near-pool 0.1.0", "near-primitives 0.1.0", - "near-protos 0.1.0", "near-store 0.1.0", - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2126,7 +2125,6 @@ dependencies = [ "actix-web 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "near-primitives 0.1.0", - "near-protos 0.1.0", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2145,10 +2143,8 @@ dependencies = [ "near-chain 0.1.0", "near-client 0.1.0", "near-primitives 0.1.0", - "near-protos 0.1.0", "near-store 0.1.0", "near-telemetry 0.1.0", - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2184,28 +2180,18 @@ dependencies = [ "jemallocator 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "near-protos 0.1.0", "pairing 0.14.1 (git+https://github.com/nearprotocol/pairing.git?rev=f009a9f54c1c1149cea4ee3e6e58ed71d72bb2e9)", - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "reed-solomon-erasure 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_cbor 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "near-protos" -version = "0.1.0" -dependencies = [ - "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "protos-autogen 0.1.0", -] - [[package]] name = "near-store" version = "0.1.0" @@ -2220,8 +2206,6 @@ dependencies = [ "kvdb-rocksdb 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "near-primitives 0.1.0", - "near-protos 0.1.0", - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2243,7 +2227,6 @@ name = "near-verifier" version = "0.1.0" dependencies = [ "near-primitives 0.1.0", - "near-protos 0.1.0", "near-store 0.1.0", ] @@ -2293,7 +2276,6 @@ dependencies = [ "near-jsonrpc 0.1.0", "near-network 0.1.0", "near-primitives 0.1.0", - "near-protos 0.1.0", "near-store 0.1.0", "node-runtime 0.0.1", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2339,12 +2321,10 @@ dependencies = [ "kvdb 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "near-primitives 0.1.0", - "near-protos 0.1.0", "near-store 0.1.0", "near-verifier 0.1.0", "near-vm-logic 0.2.0", "near-vm-runner 0.2.0", - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2692,48 +2672,6 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "protobuf" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "protobuf-codegen" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "protoc" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "protoc-rust" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "protobuf-codegen 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "protoc 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tempfile 3.0.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "protos-autogen" -version = "0.1.0" -dependencies = [ - "protoc-rust 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "publicsuffix" version = "1.5.2" @@ -3214,6 +3152,16 @@ dependencies = [ "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "serde_cbor" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "half 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde_derive" version = "1.0.92" @@ -3357,10 +3305,8 @@ dependencies = [ "near-chain 0.1.0", "near-network 0.1.0", "near-primitives 0.1.0", - "near-protos 0.1.0", "near-store 0.1.0", "node-runtime 0.0.1", - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -3491,10 +3437,8 @@ dependencies = [ "near-client 0.1.0", "near-jsonrpc 0.1.0", "near-primitives 0.1.0", - "near-protos 0.1.0", "near-store 0.1.0", "node-runtime 0.0.1", - "protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.18 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4373,6 +4317,7 @@ dependencies = [ "checksum git-version-macro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7d3da7c317307fc64fd46622563d50d0cddf47053f92894f7a8584a277f621e5" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum h2 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "69b2a5a3092cbebbc951fe55408402e696ee2ed09019137d1800fc2c411265d2" +"checksum half 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9353c2a89d550b58fa0061d8ed8d002a7d8cdf2494eb0e432859bd3a9e543836" "checksum hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3bae29b6653b3412c2e71e9d486db9f9df5d701941d86683005efb9f2d28e3da" "checksum hashbrown 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "29fba9abe4742d586dfd0c06ae4f7e73a1c2d86b856933509b269d82cdf06e18" "checksum hashbrown 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e1de41fb8dba9714efd92241565cdff73f78508c95697dd56787d3cba27e2353" @@ -4473,10 +4418,6 @@ dependencies = [ "checksum proc-macro-hack-impl 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2b753ad9ed99dd8efeaa7d2fb8453c8f6bc3e54b97966d35f1bc77ca6865254a" "checksum proc-macro-nested 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "369a6ed065f249a159e06c45752c780bda2fb53c995718f9e484d08daa9eb42e" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" -"checksum protobuf 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5f00e4a3cb64ecfeac2c0a73c74c68ae3439d7a6bead3870be56ad5dd2620a6f" -"checksum protobuf-codegen 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d2c6e555166cdb646306f599da020e01548e9f4d6ec2fd39802c6db2347cbd3e" -"checksum protoc 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f26e88abedb402073b87981faecc3b973dd5b19b7002821ed47c457bc5b367a4" -"checksum protoc-rust 2.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94ce06ff9a4fbc3a695f8e8ed0b2455744d79108110ac70cb8a091f25d3d3c09" "checksum publicsuffix 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5afecba86dcf1e4fd610246f89899d1924fe12e1e89f555eb7c7f710f3c5ad1d" "checksum pulldown-cmark 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8361e81576d2e02643b04950e487ec172b687180da65c731c03cf336784e6c07" "checksum pwasm-utils 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "77980ccf39548fa7a7fba5f0159c6c53a04c71a92db6ea1fa65161c7da1f870c" @@ -4528,6 +4469,7 @@ dependencies = [ "checksum serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)" = "32746bf0f26eab52f06af0d0aa1984f641341d06d8d673c693871da2d188c9be" "checksum serde-bench 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "d733da87e79faaac25616e33d26299a41143fd4cd42746cbb0e91d8feea243fd" "checksum serde_bytes 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)" = "defbb8a83d7f34cc8380751eeb892b825944222888aff18996ea7901f24aec88" +"checksum serde_cbor 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "318690c4f04ae6553665f3846c0614c9995bb1ea51a2f1c5c4b4ed338c248b49" "checksum serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)" = "46a3223d0c9ba936b61c0d2e3e559e3217dbfb8d65d06d26e8b3c25de38bae3e" "checksum serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d" "checksum serde_urlencoded 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "642dd69105886af2efd227f75a520ec9b44a820d65bc133a9131f7d229fd165a" diff --git a/Cargo.toml b/Cargo.toml index c152260f873..de10be89db1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ edition = "2018" [workspace] members = [ "async-utils/", - "protos/builder", "core/primitives", "core/store", "runtime/runtime", @@ -38,7 +37,6 @@ serde_json = "1.0.0" reqwest = "0.9" futures = "0.1.25" -near-protos = { path = "./core/protos" } near-primitives = { path = "./core/primitives" } near-store = { path = "./core/store" } diff --git a/chain/chain/Cargo.toml b/chain/chain/Cargo.toml index 2138cac7f56..0ee36d49ed0 100644 --- a/chain/chain/Cargo.toml +++ b/chain/chain/Cargo.toml @@ -10,11 +10,9 @@ log = "0.4" failure = "0.1" failure_derive = "0.1" kvdb = "0.1" -protobuf = { version = "2.7", features = ["with-bytes"] } serde = "1.0" serde_derive = "1.0" cached = { git = "https://github.com/nearprotocol/cached", rev = "7e472eddef68607e344d5a106a0e6705d92e55be" } near-primitives = { path = "../../core/primitives" } -near-protos = { path = "../../core/protos" } near-store = { path = "../../core/store" } diff --git a/chain/jsonrpc/Cargo.toml b/chain/jsonrpc/Cargo.toml index b67bfc574e3..b3a530fe5c4 100644 --- a/chain/jsonrpc/Cargo.toml +++ b/chain/jsonrpc/Cargo.toml @@ -20,11 +20,9 @@ serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } tokio = "0.1.15" uuid = { version = "~0.6", features = ["v4"] } -protobuf = { version = "2.7", features = ["with-bytes"] } async-utils = { path = "../../async-utils" } near-primitives = { path = "../../core/primitives" } -near-protos = { path = "../../core/protos" } near-store = { path = "../../core/store" } near-chain = { path = "../chain" } near-client = { path = "../client" } diff --git a/chain/jsonrpc/client/Cargo.toml b/chain/jsonrpc/client/Cargo.toml index 382b88362d9..ddca6c0a21f 100644 --- a/chain/jsonrpc/client/Cargo.toml +++ b/chain/jsonrpc/client/Cargo.toml @@ -13,4 +13,3 @@ serde = "1.0" uuid = { version = "~0.6", features = ["v4"] } near-primitives = { path = "../../../core/primitives" } -near-protos = { path = "../../../core/protos" } diff --git a/chain/jsonrpc/src/lib.rs b/chain/jsonrpc/src/lib.rs index b154ab31dd6..e4243265a30 100644 --- a/chain/jsonrpc/src/lib.rs +++ b/chain/jsonrpc/src/lib.rs @@ -1,7 +1,6 @@ #![feature(await_macro, async_await)] use std::convert::TryFrom; -use std::convert::TryInto; use std::time::Duration; use actix::{Addr, MailboxError}; @@ -9,7 +8,6 @@ use actix_cors::Cors; use actix_web::{App, Error as HttpError, http, HttpResponse, HttpServer, middleware, web}; use futures03::{compat::Future01CompatExt as _, FutureExt as _, TryFutureExt as _}; use futures::future::Future; -use protobuf::parse_from_bytes; use serde::de::DeserializeOwned; use serde_derive::{Deserialize, Serialize}; use serde_json::Value; @@ -22,10 +20,9 @@ pub use near_jsonrpc_client as client; use near_jsonrpc_client::message as message; use near_network::{NetworkClientMessages, NetworkClientResponses}; use near_primitives::hash::CryptoHash; -use near_primitives::serialize::{BaseEncode, from_base, from_base64}; +use near_primitives::serialize::{BaseEncode, Decode, from_base, from_base64}; use near_primitives::transaction::{FinalTransactionStatus, SignedTransaction}; use near_primitives::types::BlockIndex; -use near_protos::signed_transaction as transaction_proto; pub mod test_utils; @@ -101,12 +98,13 @@ fn jsonify( fn parse_tx(params: Option) -> Result { let (encoded,) = parse_params::<(String,)>(params)?; let bytes = from_base64_or_parse_err(encoded)?; - let tx: transaction_proto::SignedTransaction = parse_from_bytes(&bytes).map_err(|e| { - RpcError::invalid_params(Some(format!("Failed to decode transaction proto: {}", e))) - })?; - Ok(tx.try_into().map_err(|e| { - RpcError::invalid_params(Some(format!("Failed to decode transaction: {}", e))) - })?) + Decode::decode(&bytes).map_err(|e| RpcError::invalid_params(Some(format!("Failed to decode transaction: {}", e)))) +// let tx: transaction_proto::SignedTransaction = parse_from_bytes(&bytes).map_err(|e| { +// RpcError::invalid_params(Some(format!("Failed to decode transaction proto: {}", e))) +// })?; +// Ok(tx.try_into().map_err(|e| { +// RpcError::invalid_params(Some(format!("Failed to decode transaction: {}", e))) +// })?) } fn parse_hash(params: Option) -> Result { diff --git a/chain/jsonrpc/tests/rpc_transactions.rs b/chain/jsonrpc/tests/rpc_transactions.rs index 181f0751c02..c529035dd83 100644 --- a/chain/jsonrpc/tests/rpc_transactions.rs +++ b/chain/jsonrpc/tests/rpc_transactions.rs @@ -1,16 +1,15 @@ +use std::sync::Arc; + use actix::{Actor, System}; use futures::future::Future; -use protobuf::Message; use near_jsonrpc::client::new_client; use near_jsonrpc::test_utils::start_all; use near_network::test_utils::{wait_or_panic, WaitOrTimeout}; use near_primitives::crypto::signer::InMemorySigner; -use near_primitives::serialize::to_base64; +use near_primitives::serialize::{to_base64, Encode}; use near_primitives::test_utils::init_test_logger; use near_primitives::transaction::{FinalTransactionStatus, SignedTransaction}; -use near_protos::signed_transaction as transaction_proto; -use std::sync::Arc; /// Test sending transaction via json rpc without waiting. #[test] @@ -31,10 +30,10 @@ fn test_send_tx_async() { ); let tx_hash: String = (&tx.get_hash()).into(); let tx_hash2 = tx_hash.clone(); - let proto: transaction_proto::SignedTransaction = tx.into(); + let bytes = tx.encode().unwrap(); actix::spawn( client - .broadcast_tx_async(to_base64(&proto.write_to_bytes().unwrap())) + .broadcast_tx_async(to_base64(&bytes)) .map_err(|_| ()) .map(move |result| assert_eq!(tx_hash, result)), ); @@ -75,10 +74,10 @@ fn test_send_tx_commit() { Arc::new(signer), 100, ); - let proto: transaction_proto::SignedTransaction = tx.into(); + let bytes = tx.encode().unwrap(); actix::spawn( client - .broadcast_tx_commit(to_base64(&proto.write_to_bytes().unwrap())) + .broadcast_tx_commit(to_base64(&bytes)) .map_err(|why| { System::current().stop(); panic!(why); diff --git a/chain/network/Cargo.toml b/chain/network/Cargo.toml index 29bb4b064ab..30b9aece9f9 100644 --- a/chain/network/Cargo.toml +++ b/chain/network/Cargo.toml @@ -9,7 +9,6 @@ bytes = "0.4" actix = "0.8.1" log = "0.4" tokio = "0.1" -protobuf = { version = "2.7", features = ["with-bytes"] } futures = "0.1" chrono = { version = "0.4.4", features = ["serde"] } serde = "1.0" @@ -17,7 +16,6 @@ serde_derive = "1.0" rand = "0.6.5" near-primitives = { path = "../../core/primitives" } -near-protos = { path = "../../core/protos" } near-store = { path = "../../core/store" } near-chain = { path = "../chain" } diff --git a/chain/network/src/codec.rs b/chain/network/src/codec.rs index 7a7d5ba2485..2bb6f634ddd 100644 --- a/chain/network/src/codec.rs +++ b/chain/network/src/codec.rs @@ -1,13 +1,10 @@ -use std::convert::TryInto; use std::io::{Error, ErrorKind}; use bytes::{BufMut, BytesMut}; -use protobuf::{parse_from_bytes, Message}; use tokio::codec::{Decoder, Encoder}; -use near_protos::network::PeerMessage as ProtoMessage; - use crate::types::PeerMessage; +use near_primitives::serialize::{Decode, Encode}; pub struct Codec { max_length: u32, @@ -59,16 +56,14 @@ impl Decoder for Codec { } } -pub fn peer_message_to_bytes( - peer_message: PeerMessage, -) -> Result, Box> { - let proto: ProtoMessage = peer_message.into(); - proto.write_to_bytes().map_err(|err| err.into()) +pub fn peer_message_to_bytes(peer_message: PeerMessage) -> Result, std::io::Error> { + // MOO + Encode::encode(&peer_message) } -pub fn bytes_to_peer_message(bytes: &[u8]) -> Result> { - let proto: ProtoMessage = parse_from_bytes(bytes)?; - proto.try_into() +pub fn bytes_to_peer_message(bytes: &[u8]) -> Result { + // MOO + Decode::decode(bytes) } #[cfg(test)] diff --git a/chain/network/src/types.rs b/chain/network/src/types.rs index f416c649312..1840072b9b6 100644 --- a/chain/network/src/types.rs +++ b/chain/network/src/types.rs @@ -1,16 +1,14 @@ +use std::collections::HashMap; use std::convert::From; use std::convert::{Into, TryFrom, TryInto}; use std::fmt; use std::hash::{Hash, Hasher}; -use std::iter::FromIterator; use std::net::SocketAddr; use std::time::Duration; use actix::dev::{MessageResponse, ResponseChannel}; use actix::{Actor, Addr, Message}; use chrono::{DateTime, Utc}; -use protobuf::well_known_types::UInt32Value; -use protobuf::{RepeatedField, SingularPtrField}; use serde_derive::{Deserialize, Serialize}; use tokio::net::TcpStream; @@ -22,11 +20,8 @@ use near_primitives::receipt::Receipt; use near_primitives::serialize::{BaseEncode, Decode}; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, BlockIndex, ShardId}; -use near_primitives::utils::{proto_to_type, to_string_value}; -use near_protos::network as network_proto; use crate::peer::Peer; -use std::collections::HashMap; /// Current latest version of the protocol pub const PROTOCOL_VERSION: u32 = 2; @@ -135,29 +130,8 @@ impl TryFrom<&str> for PeerInfo { } } -impl TryFrom for PeerInfo { - type Error = Box; - - fn try_from(proto: network_proto::PeerInfo) -> Result { - let addr = proto.addr.into_option().and_then(|s| s.value.parse::().ok()); - let account_id = proto.account_id.into_option().map(|s| s.value); - Ok(PeerInfo { id: PublicKey::try_from(proto.id)?.into(), addr, account_id }) - } -} - -impl From for network_proto::PeerInfo { - fn from(peer_info: PeerInfo) -> network_proto::PeerInfo { - let id = peer_info.id; - let addr = SingularPtrField::from_option( - peer_info.addr.map(|s| to_string_value(format!("{}", s))), - ); - let account_id = SingularPtrField::from_option(peer_info.account_id.map(to_string_value)); - network_proto::PeerInfo { id: (&id.0).into(), addr, account_id, ..Default::default() } - } -} - /// Peer chain information. -#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)] pub struct PeerChainInfo { /// Genesis hash. pub genesis: CryptoHash, @@ -167,29 +141,6 @@ pub struct PeerChainInfo { pub total_weight: Weight, } -impl TryFrom for PeerChainInfo { - type Error = Box; - - fn try_from(proto: network_proto::PeerChainInfo) -> Result { - Ok(PeerChainInfo { - genesis: proto.genesis.try_into()?, - height: proto.height, - total_weight: proto.total_weight.into(), - }) - } -} - -impl From for network_proto::PeerChainInfo { - fn from(chain_peer_info: PeerChainInfo) -> network_proto::PeerChainInfo { - network_proto::PeerChainInfo { - genesis: chain_peer_info.genesis.into(), - height: chain_peer_info.height, - total_weight: chain_peer_info.total_weight.to_num(), - ..Default::default() - } - } -} - /// Peer type. #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum PeerType { @@ -210,7 +161,7 @@ pub enum PeerStatus { Banned(ReasonForBan), } -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] pub struct Handshake { /// Protocol version. pub version: u32, @@ -228,66 +179,16 @@ impl Handshake { } } -impl TryFrom for Handshake { - type Error = Box; - - fn try_from(proto: network_proto::Handshake) -> Result { - let listen_port = proto.listen_port.into_option().map(|v| v.value as u16); - let peer_id: PublicKey = proto.peer_id.try_into().map_err(|e| format!("{}", e))?; - let chain_info = proto_to_type(proto.chain_info)?; - Ok(Handshake { version: proto.version, peer_id: peer_id.into(), listen_port, chain_info }) - } -} - -impl From for network_proto::Handshake { - fn from(handshake: Handshake) -> network_proto::Handshake { - let listen_port = SingularPtrField::from_option(handshake.listen_port.map(|v| { - let mut res = UInt32Value::new(); - res.set_value(u32::from(v)); - res - })); - network_proto::Handshake { - version: handshake.version, - peer_id: handshake.peer_id.into(), - listen_port, - chain_info: SingularPtrField::some(handshake.chain_info.into()), - ..Default::default() - } - } -} - /// Account route description -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] pub struct AnnounceAccountRoute { pub peer_id: PeerId, pub hash: CryptoHash, pub signature: Signature, } -impl TryFrom for AnnounceAccountRoute { - type Error = Box; - - fn try_from(proto: network_proto::AnnounceAccountRoute) -> Result { - let peer_id: PeerId = proto.peer_id.try_into().map_err(|e| format!("{}", e))?; - let hash: CryptoHash = proto.hash.try_into().map_err(|e| format!("{}", e))?; - let signature: Signature = proto.signature.try_into().map_err(|e| format!("{}", e))?; - Ok(AnnounceAccountRoute { peer_id, hash, signature }) - } -} - -impl From for network_proto::AnnounceAccountRoute { - fn from(announce_account_route: AnnounceAccountRoute) -> network_proto::AnnounceAccountRoute { - network_proto::AnnounceAccountRoute { - peer_id: announce_account_route.peer_id.into(), - hash: announce_account_route.hash.into(), - signature: announce_account_route.signature.into(), - ..Default::default() - } - } -} - /// Account announcement information -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] pub struct AnnounceAccount { /// AccountId to be announced pub account_id: AccountId, @@ -353,40 +254,7 @@ impl AnnounceAccount { } } -impl TryFrom for AnnounceAccount { - type Error = Box; - - fn try_from(proto: network_proto::AnnounceAccount) -> Result { - let epoch: CryptoHash = proto.epoch.try_into().map_err(|e| format!("{}", e))?; - Ok(AnnounceAccount { - account_id: proto.account_id, - epoch, - route: proto - .route - .into_iter() - .filter_map(|hop| match hop.try_into() { - Ok(hop) => Some(hop), - Err(_) => None, - }) - .collect(), - }) - } -} - -impl From for network_proto::AnnounceAccount { - fn from(announce_account: AnnounceAccount) -> network_proto::AnnounceAccount { - network_proto::AnnounceAccount { - account_id: announce_account.account_id, - epoch: announce_account.epoch.into(), - route: RepeatedField::from_iter( - announce_account.route.into_iter().map(|hop| hop.into()), - ), - ..Default::default() - } - } -} - -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] pub enum PeerMessage { Handshake(Handshake), @@ -429,181 +297,6 @@ impl fmt::Display for PeerMessage { } } -impl TryFrom for PeerMessage { - type Error = Box; - - fn try_from(proto: network_proto::PeerMessage) -> Result { - match proto.message_type { - Some(network_proto::PeerMessage_oneof_message_type::hand_shake(hand_shake)) => { - hand_shake.try_into().map(PeerMessage::Handshake) - } - Some(network_proto::PeerMessage_oneof_message_type::peers_request(_)) => { - Ok(PeerMessage::PeersRequest) - } - Some(network_proto::PeerMessage_oneof_message_type::peers_response(peers_response)) => { - let peers_response = peers_response - .peers - .into_iter() - .map(TryInto::try_into) - .collect::, _>>()?; - Ok(PeerMessage::PeersResponse(peers_response)) - } - Some(network_proto::PeerMessage_oneof_message_type::block(block)) => { - Ok(PeerMessage::Block(block.try_into()?)) - } - Some(network_proto::PeerMessage_oneof_message_type::block_header_announce(header)) => { - Ok(PeerMessage::BlockHeaderAnnounce(header.try_into()?)) - } - Some(network_proto::PeerMessage_oneof_message_type::transaction(transaction)) => { - Ok(PeerMessage::Transaction(transaction.try_into()?)) - } - Some(network_proto::PeerMessage_oneof_message_type::block_approval(block_approval)) => { - Ok(PeerMessage::BlockApproval( - block_approval.account_id, - block_approval.hash.try_into()?, - block_approval.signature.try_into()?, - )) - } - Some(network_proto::PeerMessage_oneof_message_type::block_request(block_request)) => { - Ok(PeerMessage::BlockRequest(block_request.try_into()?)) - } - Some(network_proto::PeerMessage_oneof_message_type::block_headers_request( - block_headers_request, - )) => Ok(PeerMessage::BlockHeadersRequest( - block_headers_request - .hashes - .into_iter() - .map(TryInto::try_into) - .collect::, _>>()?, - )), - Some(network_proto::PeerMessage_oneof_message_type::block_headers(block_headers)) => { - Ok(PeerMessage::BlockHeaders( - block_headers - .headers - .into_iter() - .map(TryInto::try_into) - .collect::, _>>()?, - )) - } - Some(network_proto::PeerMessage_oneof_message_type::state_request(state_request)) => { - Ok(PeerMessage::StateRequest( - state_request.shard_id, - state_request.hash.try_into()?, - )) - } - Some(network_proto::PeerMessage_oneof_message_type::state_response(state_response)) => { - Ok(PeerMessage::StateResponse( - state_response.shard_id, - state_response.hash.try_into()?, - state_response.payload, - state_response - .receipts - .into_iter() - .map(TryInto::try_into) - .collect::, _>>()?, - )) - } - Some(network_proto::PeerMessage_oneof_message_type::announce_account( - announce_account, - )) => announce_account.try_into().map(PeerMessage::AnnounceAccount), - None => Err(format!("Unexpected empty message body").into()), - } - } -} - -impl From for network_proto::PeerMessage { - fn from(message: PeerMessage) -> network_proto::PeerMessage { - let message_type = match message { - PeerMessage::Handshake(hand_shake) => { - Some(network_proto::PeerMessage_oneof_message_type::hand_shake(hand_shake.into())) - } - PeerMessage::PeersRequest => { - Some(network_proto::PeerMessage_oneof_message_type::peers_request(true)) - } - PeerMessage::PeersResponse(peers_response) => { - let peers_response = network_proto::PeersResponse { - peers: RepeatedField::from_iter( - peers_response.into_iter().map(std::convert::Into::into), - ), - cached_size: Default::default(), - unknown_fields: Default::default(), - }; - Some(network_proto::PeerMessage_oneof_message_type::peers_response(peers_response)) - } - PeerMessage::Block(block) => { - Some(network_proto::PeerMessage_oneof_message_type::block(block.into())) - } - PeerMessage::BlockHeaderAnnounce(header) => Some( - network_proto::PeerMessage_oneof_message_type::block_header_announce(header.into()), - ), - PeerMessage::Transaction(transaction) => { - Some(network_proto::PeerMessage_oneof_message_type::transaction(transaction.into())) - } - PeerMessage::BlockApproval(account_id, hash, signature) => { - let block_approval = network_proto::BlockApproval { - account_id, - hash: hash.into(), - signature: signature.into(), - cached_size: Default::default(), - unknown_fields: Default::default(), - }; - Some(network_proto::PeerMessage_oneof_message_type::block_approval(block_approval)) - } - PeerMessage::BlockRequest(hash) => { - Some(network_proto::PeerMessage_oneof_message_type::block_request(hash.into())) - } - PeerMessage::BlockHeadersRequest(hashes) => { - let request = network_proto::BlockHeaderRequest { - hashes: RepeatedField::from_iter( - hashes.into_iter().map(std::convert::Into::into), - ), - cached_size: Default::default(), - unknown_fields: Default::default(), - }; - Some(network_proto::PeerMessage_oneof_message_type::block_headers_request(request)) - } - PeerMessage::BlockHeaders(headers) => { - let block_headers = network_proto::BlockHeaders { - headers: RepeatedField::from_iter( - headers.into_iter().map(std::convert::Into::into), - ), - cached_size: Default::default(), - unknown_fields: Default::default(), - }; - Some(network_proto::PeerMessage_oneof_message_type::block_headers(block_headers)) - } - PeerMessage::StateRequest(shard_id, hash) => { - let state_request = network_proto::StateRequest { - shard_id, - hash: hash.into(), - cached_size: Default::default(), - unknown_fields: Default::default(), - }; - Some(network_proto::PeerMessage_oneof_message_type::state_request(state_request)) - } - PeerMessage::StateResponse(shard_id, hash, payload, receipts) => { - let state_response = network_proto::StateResponse { - shard_id, - hash: hash.into(), - payload, - receipts: RepeatedField::from_iter( - receipts.into_iter().map(std::convert::Into::into), - ), - cached_size: Default::default(), - unknown_fields: Default::default(), - }; - Some(network_proto::PeerMessage_oneof_message_type::state_response(state_response)) - } - PeerMessage::AnnounceAccount(announce_account) => { - Some(network_proto::PeerMessage_oneof_message_type::announce_account( - announce_account.into(), - )) - } - }; - network_proto::PeerMessage { message_type, ..Default::default() } - } -} - /// Configuration for the peer-to-peer manager. #[derive(Clone)] pub struct NetworkConfig { diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index 94870d8d551..3bad1327910 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -17,17 +17,15 @@ serde = "1.0" serde_derive = "1.0" sha2 = "0.8.0" serde_json = "1.0" +serde_cbor = "0.10" pairing = { git = "https://github.com/nearprotocol/pairing.git", rev = "f009a9f54c1c1149cea4ee3e6e58ed71d72bb2e9" } rand = "0.6" rand_xorshift = "0.1" -protobuf = { version = "2.7", features = ["with-bytes"] } env_logger = "0.6.0" log = "0.4" reed-solomon-erasure = "3.1.1" jemallocator = { version = "0.3.0", optional = true } -near-protos = { path = "../protos" } - [features] default = ["jemallocator"] diff --git a/core/primitives/src/account.rs b/core/primitives/src/account.rs index 97acf89def9..130112b6779 100644 --- a/core/primitives/src/account.rs +++ b/core/primitives/src/account.rs @@ -2,12 +2,6 @@ use std::convert::{TryFrom, TryInto}; use std::fmt; use std::iter::FromIterator; -use protobuf::well_known_types::{BytesValue, StringValue}; -use protobuf::{RepeatedField, SingularPtrField}; - -use near_protos::access_key as access_key_proto; -use near_protos::account as account_proto; - use crate::crypto::signature::PublicKey; use crate::hash::CryptoHash; use crate::logging; @@ -31,6 +25,12 @@ pub struct Account { /// Last block index at which the storage was paid for. pub storage_paid_at: BlockIndex, } +// +///// A view of the account +//#[serde(remote = "Account")] +//pub struct AccountView { +// pub public +//} impl Account { pub fn new( @@ -62,44 +62,6 @@ impl Account { } } -impl TryFrom for Account { - type Error = Box; - - fn try_from(account: account_proto::Account) -> Result { - Ok(Account { - public_keys: account - .public_keys - .into_iter() - .map(TryInto::try_into) - .collect::, _>>()?, - nonce: account.nonce, - amount: account.amount.unwrap_or_default().try_into()?, - staked: account.staked.unwrap_or_default().try_into()?, - code_hash: account.code_hash.try_into()?, - storage_usage: account.storage_usage, - storage_paid_at: account.storage_paid_at, - }) - } -} - -impl From for account_proto::Account { - fn from(account: Account) -> Self { - account_proto::Account { - public_keys: RepeatedField::from_iter( - account.public_keys.iter().map(std::convert::Into::into), - ), - nonce: account.nonce, - amount: SingularPtrField::some(account.amount.into()), - staked: SingularPtrField::some(account.staked.into()), - code_hash: account.code_hash.into(), - storage_usage: account.storage_usage, - storage_paid_at: account.storage_paid_at, - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - /// Limited Access key to use owner's account with the fixed public_key. /// Access Key is stored under the key of owner's `account_id` and the `public_key`. #[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone)] @@ -126,41 +88,3 @@ impl fmt::Debug for AccessKey { .finish() } } - -impl TryFrom for AccessKey { - type Error = Box; - - fn try_from(access_key: access_key_proto::AccessKey) -> Result { - Ok(AccessKey { - amount: access_key.amount.unwrap_or_default().try_into()?, - balance_owner: access_key.balance_owner.into_option().map(|s| s.value), - contract_id: access_key.contract_id.into_option().map(|s| s.value), - method_name: access_key.method_name.into_option().map(|s| s.value), - }) - } -} - -impl From for access_key_proto::AccessKey { - fn from(access_key: AccessKey) -> Self { - access_key_proto::AccessKey { - amount: SingularPtrField::some(access_key.amount.into()), - balance_owner: SingularPtrField::from_option(access_key.balance_owner.map(|v| { - let mut res = StringValue::new(); - res.set_value(v); - res - })), - contract_id: SingularPtrField::from_option(access_key.contract_id.map(|v| { - let mut res = StringValue::new(); - res.set_value(v); - res - })), - method_name: SingularPtrField::from_option(access_key.method_name.map(|v| { - let mut res = BytesValue::new(); - res.set_value(v); - res - })), - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} diff --git a/core/primitives/src/block.rs b/core/primitives/src/block.rs index 2f3bb3a5cf5..7c6285531ce 100644 --- a/core/primitives/src/block.rs +++ b/core/primitives/src/block.rs @@ -5,9 +5,6 @@ use std::sync::Arc; use chrono::prelude::{DateTime, NaiveDateTime, Utc}; use chrono::serde::ts_nanoseconds; -use protobuf::{Message as ProtoMessage, RepeatedField, SingularPtrField}; - -use near_protos::chain as chain_proto; use crate::crypto::signature::{verify, PublicKey, Signature, DEFAULT_SIGNATURE}; use crate::crypto::signer::EDSigner; @@ -15,7 +12,7 @@ use crate::hash::{hash, CryptoHash}; use crate::serialize::vec_base_format; use crate::transaction::SignedTransaction; use crate::types::{BlockIndex, MerkleHash, ValidatorStake}; -use crate::utils::proto_to_type; +use bs58::alphabet::DEFAULT; /// Number of nano seconds in one second. const NS_IN_SECOND: u64 = 1_000_000_000; @@ -65,23 +62,27 @@ impl BlockHeader { total_weight: Weight, mut validator_proposal: Vec, epoch_hash: CryptoHash, - ) -> chain_proto::BlockHeaderBody { - chain_proto::BlockHeaderBody { + ) -> BlockHeader { + //chain_proto::BlockHeaderBody { + BlockHeader { height, prev_hash: prev_hash.into(), prev_state_root: prev_state_root.into(), tx_root: tx_root.into(), - timestamp: timestamp.timestamp_nanos() as u64, + timestamp, //: timestamp.timestamp_nanos() as u64, approval_mask, - approval_sigs: RepeatedField::from_iter( - approval_sigs.iter().map(std::convert::Into::into), - ), - total_weight: total_weight.to_num(), - validator_proposal: RepeatedField::from_iter( - validator_proposal.drain(..).map(std::convert::Into::into), - ), + approval_sigs, + // approval_sigs: RepeatedField::from_iter( + // approval_sigs.iter().map(std::convert::Into::into), + // ), + total_weight, //: total_weight.to_num(), + validator_proposal, + // validator_proposal: RepeatedField::from_iter( + // validator_proposal.drain(..).map(std::convert::Into::into), + // ), epoch_hash: epoch_hash.into(), - ..Default::default() + hash: CryptoHash::default(), + signature: DEFAULT_SIGNATURE, } } @@ -98,7 +99,7 @@ impl BlockHeader { epoch_hash: CryptoHash, signer: Arc, ) -> Self { - let hb = Self::header_body( + Self::header_body( height, prev_hash, prev_state_root, @@ -109,19 +110,19 @@ impl BlockHeader { total_weight, validator_proposal, epoch_hash, - ); - let bytes = hb.write_to_bytes().expect("Failed to serialize"); - let hash = hash(&bytes); - let h = chain_proto::BlockHeader { - body: SingularPtrField::some(hb), - signature: signer.sign(hash.as_ref()).into(), - ..Default::default() - }; - h.try_into().expect("Failed to parse just created header") + ) + // let bytes = hb.write_to_bytes().expect("Failed to serialize"); + // let hash = hash(&bytes); + // let h = chain_proto::BlockHeader { + // body: SingularPtrField::some(hb), + // signature: signer.sign(hash.as_ref()).into(), + // ..Default::default() + // }; + // h.try_into().expect("Failed to parse just created header") } pub fn genesis(state_root: MerkleHash, timestamp: DateTime) -> Self { - let header_body = Self::header_body( + Self::header_body( 0, CryptoHash::default(), state_root, @@ -132,14 +133,14 @@ impl BlockHeader { 0.into(), vec![], CryptoHash::default(), - ); - chain_proto::BlockHeader { - body: SingularPtrField::some(header_body), - signature: DEFAULT_SIGNATURE.into(), - ..Default::default() - } - .try_into() - .expect("Failed to parse just created header") + ) + // chain_proto::BlockHeader { + // body: SingularPtrField::some(header_body), + // signature: DEFAULT_SIGNATURE.into(), + // ..Default::default() + // } + // .try_into() + // .expect("Failed to parse just created header") } pub fn hash(&self) -> CryptoHash { @@ -152,78 +153,6 @@ impl BlockHeader { } } -impl TryFrom for BlockHeader { - type Error = Box; - - fn try_from(proto: chain_proto::BlockHeader) -> Result { - let body = proto.body.into_option().ok_or("Missing Header body")?; - let bytes = body.write_to_bytes().map_err(|err| err.to_string())?; - let hash = hash(&bytes); - let height = body.height; - let prev_hash = body.prev_hash.try_into()?; - let prev_state_root = body.prev_state_root.try_into()?; - let tx_root = body.tx_root.try_into()?; - let timestamp = DateTime::from_utc( - NaiveDateTime::from_timestamp( - (body.timestamp / NS_IN_SECOND) as i64, - (body.timestamp % NS_IN_SECOND) as u32, - ), - Utc, - ); - let approval_mask = body.approval_mask; - let approval_sigs = - body.approval_sigs.into_iter().map(TryInto::try_into).collect::, _>>()?; - let total_weight = body.total_weight.into(); - let signature = proto.signature.try_into()?; - let validator_proposal = body - .validator_proposal - .into_iter() - .map(TryInto::try_into) - .collect::, _>>()?; - let epoch_hash = body.epoch_hash.try_into()?; - Ok(BlockHeader { - height, - prev_hash, - prev_state_root, - tx_root, - timestamp, - approval_mask, - approval_sigs, - total_weight, - validator_proposal, - epoch_hash, - signature, - hash, - }) - } -} - -impl From for chain_proto::BlockHeader { - fn from(mut header: BlockHeader) -> Self { - chain_proto::BlockHeader { - body: SingularPtrField::some(chain_proto::BlockHeaderBody { - height: header.height, - prev_hash: header.prev_hash.into(), - prev_state_root: header.prev_state_root.into(), - tx_root: header.tx_root.into(), - timestamp: header.timestamp.timestamp_nanos() as u64, - approval_mask: header.approval_mask, - approval_sigs: RepeatedField::from_iter( - header.approval_sigs.iter().map(std::convert::Into::into), - ), - total_weight: header.total_weight.to_num(), - validator_proposal: RepeatedField::from_iter( - header.validator_proposal.drain(..).map(std::convert::Into::into), - ), - epoch_hash: header.epoch_hash.into(), - ..Default::default() - }), - signature: header.signature.into(), - ..Default::default() - } - } -} - #[derive(Serialize, Deserialize, Debug)] pub struct Bytes(Vec); @@ -298,26 +227,6 @@ impl Block { } } -impl TryFrom for Block { - type Error = Box; - - fn try_from(proto: chain_proto::Block) -> Result { - let transactions = - proto.transactions.into_iter().map(TryInto::try_into).collect::, _>>()?; - Ok(Block { header: proto_to_type(proto.header)?, transactions }) - } -} - -impl From for chain_proto::Block { - fn from(block: Block) -> Self { - chain_proto::Block { - header: SingularPtrField::some(block.header.into()), - transactions: block.transactions.into_iter().map(std::convert::Into::into).collect(), - ..Default::default() - } - } -} - /// The weight is defined as the number of unique validators approving this fork. #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Serialize, Deserialize, Default)] pub struct Weight { diff --git a/core/primitives/src/crypto/group_signature.rs b/core/primitives/src/crypto/group_signature.rs index 32948848c3c..156542a5ccf 100644 --- a/core/primitives/src/crypto/group_signature.rs +++ b/core/primitives/src/crypto/group_signature.rs @@ -1,8 +1,6 @@ use core::fmt; use std::convert::TryFrom; -use near_protos::types as types_proto; - use crate::crypto::aggregate_signature::{ BlsAggregatePublicKey, BlsAggregateSignature, BlsPublicKey, BlsSignature, }; @@ -17,26 +15,6 @@ pub struct GroupSignature { pub authority_mask: ValidatorMask, } -impl TryFrom for GroupSignature { - type Error = Box; - - fn try_from(proto: types_proto::GroupSignature) -> Result { - BaseDecode::from_base(&proto.signature) - .map(|signature| GroupSignature { signature, authority_mask: proto.authority_mask }) - .map_err(|e| format!("cannot decode signature {:?}", e).into()) - } -} - -impl From for types_proto::GroupSignature { - fn from(signature: GroupSignature) -> Self { - types_proto::GroupSignature { - signature: BaseEncode::to_base(&signature.signature), - authority_mask: signature.authority_mask, - ..Default::default() - } - } -} - impl fmt::Debug for GroupSignature { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?} {:?}", self.authority_mask, pretty_hash(&self.signature.clone().to_base())) diff --git a/core/primitives/src/crypto/signature.rs b/core/primitives/src/crypto/signature.rs index eeb851c4033..f2f4d267762 100644 --- a/core/primitives/src/crypto/signature.rs +++ b/core/primitives/src/crypto/signature.rs @@ -4,8 +4,6 @@ use std::convert::TryFrom; use std::fmt; use std::hash::{Hash, Hasher}; -use near_protos::public_key as public_key_proto; - pub use exonum_sodiumoxide::crypto::sign::ed25519::Seed; use crate::logging::pretty_hash; @@ -121,26 +119,6 @@ impl TryFrom<&str> for PublicKey { } } -impl TryFrom for PublicKey { - type Error = Box; - - fn try_from(p: public_key_proto::PublicKey) -> Result { - // TODO(#979): Need to check `key_type` when we add other than ED25519 types. - PublicKey::try_from(p.data).map_err(std::convert::Into::into) - } -} - -impl From for public_key_proto::PublicKey { - fn from(p: PublicKey) -> public_key_proto::PublicKey { - public_key_proto::PublicKey { - key_type: public_key_proto::PublicKey_KeyType::ED25519, - data: p.as_ref().to_vec(), - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - impl Serialize for PublicKey { fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> where diff --git a/core/primitives/src/network.rs b/core/primitives/src/network.rs index 92e2f17e49d..899952dd008 100644 --- a/core/primitives/src/network.rs +++ b/core/primitives/src/network.rs @@ -1,6 +1,3 @@ -use near_protos::network as network_proto; -use protobuf::well_known_types::UInt32Value; -use protobuf::{RepeatedField, SingularPtrField}; use std::borrow::Borrow; use std::convert::{Into, TryFrom, TryInto}; use std::fmt; @@ -99,27 +96,6 @@ impl From for PeerInfo { } } -impl TryFrom for PeerInfo { - type Error = Box; - - fn try_from(proto: network_proto::PeerInfo) -> Result { - let addr = proto.addr.into_option().and_then(|s| s.value.parse::().ok()); - let account_id = proto.account_id.into_option().map(|s| s.value); - Ok(PeerInfo { id: CryptoHash::try_from(proto.id)?, addr, account_id }) - } -} - -impl From for network_proto::PeerInfo { - fn from(peer_info: PeerInfo) -> network_proto::PeerInfo { - let id = peer_info.id; - let addr = SingularPtrField::from_option( - peer_info.addr.map(|s| to_string_value(format!("{}", s))), - ); - let account_id = SingularPtrField::from_option(peer_info.account_id.map(to_string_value)); - network_proto::PeerInfo { id: id.into(), addr, account_id, ..Default::default() } - } -} - pub type PeersInfo = Vec; #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] @@ -133,41 +109,3 @@ pub struct Handshake { /// Sender's information about known peers. pub peers_info: PeersInfo, } - -impl TryFrom for Handshake { - type Error = Box; - - fn try_from(proto: network_proto::Handshake) -> Result { - let account_id = proto.account_id.into_option().map(|s| s.value); - let listen_port = proto.listen_port.into_option().map(|v| v.value as u16); - let peers_info = - proto.peers_info.into_iter().map(TryInto::try_into).collect::, _>>()?; - Ok(Handshake { - version: proto.version, - peer_id: proto.peer_id.try_into()?, - listen_port, - peers_info, - }) - } -} - -impl From for network_proto::Handshake { - fn from(hand_shake: Handshake) -> network_proto::Handshake { - let account_id = SingularPtrField::from_option(hand_shake.account_id.map(to_string_value)); - let listen_port = SingularPtrField::from_option(hand_shake.listen_port.map(|v| { - let mut res = UInt32Value::new(); - res.set_value(u32::from(v)); - res - })); - network_proto::Handshake { - version: hand_shake.version, - peer_id: hand_shake.peer_id.into(), - peers_info: RepeatedField::from_iter( - hand_shake.peers_info.into_iter().map(std::convert::Into::into), - ), - account_id, - listen_port, - ..Default::default() - } - } -} diff --git a/core/primitives/src/receipt.rs b/core/primitives/src/receipt.rs index 26977980ca8..2e868fbfaf0 100644 --- a/core/primitives/src/receipt.rs +++ b/core/primitives/src/receipt.rs @@ -3,18 +3,13 @@ use std::convert::{TryFrom, TryInto}; use std::fmt; use std::iter::FromIterator; -use protobuf::well_known_types::BytesValue; -use protobuf::{RepeatedField, SingularPtrField}; - -use near_protos::receipt as receipt_proto; - use crate::crypto::signature::PublicKey; use crate::hash::CryptoHash; use crate::logging; use crate::serialize::{option_bytes_format, u128_dec_format}; use crate::transaction::{Action, TransactionResult, TransferAction}; use crate::types::{AccountId, Balance}; -use crate::utils::{proto_to_type, proto_to_vec, system_account}; +use crate::utils::system_account; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct ReceiptInfo { @@ -32,47 +27,6 @@ pub struct Receipt { pub receipt: ReceiptEnum, } -impl TryFrom for Receipt { - type Error = Box; - - fn try_from(r: receipt_proto::Receipt) -> Result { - Ok(Receipt { - predecessor_id: r.predecessor_id, - receiver_id: r.receiver_id, - receipt_id: r.receipt_id.try_into()?, - receipt: match r.receipt { - Some(receipt_proto::Receipt_oneof_receipt::action(action)) => { - action.try_into().map(ReceiptEnum::Action) - } - Some(receipt_proto::Receipt_oneof_receipt::data(data)) => { - data.try_into().map(ReceiptEnum::Data) - } - None => Err("No such receipt enum".into()), - }?, - }) - } -} - -impl From for receipt_proto::Receipt { - fn from(r: Receipt) -> Self { - let receipt = match r.receipt { - ReceiptEnum::Action(action) => { - receipt_proto::Receipt_oneof_receipt::action(action.into()) - } - ReceiptEnum::Data(data) => receipt_proto::Receipt_oneof_receipt::data(data.into()), - }; - receipt_proto::Receipt { - predecessor_id: r.predecessor_id, - receiver_id: r.receiver_id, - receipt_id: r.receipt_id.into(), - receipt: Some(receipt), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - impl Borrow for Receipt { fn borrow(&self) -> &CryptoHash { &self.receipt_id @@ -126,67 +80,12 @@ pub struct ActionReceipt { pub actions: Vec, } -impl TryFrom for ActionReceipt { - type Error = Box; - - fn try_from(a: receipt_proto::ActionReceipt) -> Result { - Ok(ActionReceipt { - signer_id: a.signer_id, - signer_public_key: proto_to_type(a.signer_public_key)?, - gas_price: proto_to_type(a.gas_price)?, - output_data_receivers: proto_to_vec(a.output_data_receivers)?, - input_data_ids: proto_to_vec(a.input_data_ids)?, - actions: proto_to_vec(a.actions)?, - }) - } -} - -impl From for receipt_proto::ActionReceipt { - fn from(a: ActionReceipt) -> receipt_proto::ActionReceipt { - receipt_proto::ActionReceipt { - signer_id: a.signer_id, - signer_public_key: SingularPtrField::some(a.signer_public_key.into()), - gas_price: SingularPtrField::some(a.gas_price.into()), - output_data_receivers: RepeatedField::from_iter( - a.output_data_receivers.into_iter().map(std::convert::Into::into), - ), - input_data_ids: RepeatedField::from_iter( - a.input_data_ids.into_iter().map(std::convert::Into::into), - ), - actions: RepeatedField::from_iter(a.actions.into_iter().map(std::convert::Into::into)), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - #[derive(Hash, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] pub struct DataReceiver { pub data_id: CryptoHash, pub receiver_id: AccountId, } -impl TryFrom for DataReceiver { - type Error = Box; - - fn try_from(d: receipt_proto::ActionReceipt_DataReceiver) -> Result { - Ok(DataReceiver { data_id: d.data_id.try_into()?, receiver_id: d.receiver_id }) - } -} - -impl From for receipt_proto::ActionReceipt_DataReceiver { - fn from(d: DataReceiver) -> receipt_proto::ActionReceipt_DataReceiver { - receipt_proto::ActionReceipt_DataReceiver { - data_id: d.data_id.into(), - receiver_id: d.receiver_id, - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - #[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone)] pub struct DataReceipt { pub data_id: CryptoHash, @@ -194,33 +93,6 @@ pub struct DataReceipt { pub data: Option>, } -impl TryFrom for DataReceipt { - type Error = Box; - - fn try_from(d: receipt_proto::DataReceipt) -> Result { - Ok(DataReceipt { - data_id: d.data_id.try_into()?, - data: d.data.into_option().map(|v| v.value), - }) - } -} - -impl From for receipt_proto::DataReceipt { - fn from(d: DataReceipt) -> receipt_proto::DataReceipt { - receipt_proto::DataReceipt { - data_id: d.data_id.into(), - data: SingularPtrField::from_option(d.data.map(|v| { - let mut res = BytesValue::new(); - res.set_value(v); - res - })), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - impl fmt::Debug for DataReceipt { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("DataReceipt") diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index ac814c75843..7c3283ca5b9 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -3,11 +3,6 @@ use std::fmt; use std::hash::{Hash, Hasher}; use std::iter::FromIterator; -use protobuf::{RepeatedField, SingularPtrField}; - -use near_protos::signed_transaction as transaction_proto; -use near_protos::Message as ProtoMessage; - use crate::account::AccessKey; use crate::crypto::signature::{verify, PublicKey, Signature}; use crate::crypto::signer::EDSigner; @@ -15,7 +10,6 @@ use crate::hash::{hash, CryptoHash}; use crate::logging; use crate::serialize::{base_bytes_format, u128_dec_format}; use crate::types::{AccountId, Balance, Gas, Nonce, ShardId, StructSignature}; -use crate::utils::{proto_to_result, proto_to_type, proto_to_vec}; use std::sync::Arc; pub type LogEntry = String; @@ -32,41 +26,13 @@ pub struct Transaction { impl Transaction { pub fn get_hash(&self) -> CryptoHash { - let proto: transaction_proto::Transaction = self.clone().into(); - let bytes = proto.write_to_bytes().unwrap(); + // let proto: transaction_proto::Transaction = self.clone().into(); + // let bytes = proto.write_to_bytes().unwrap(); + let bytes = vec![]; hash(&bytes) } } -impl TryFrom for Transaction { - type Error = Box; - - fn try_from(t: transaction_proto::Transaction) -> Result { - Ok(Transaction { - signer_id: t.signer_id, - public_key: proto_to_type(t.public_key)?, - nonce: t.nonce, - receiver_id: t.receiver_id, - actions: proto_to_vec(t.actions)?, - }) - } -} - -impl From for transaction_proto::Transaction { - fn from(t: Transaction) -> transaction_proto::Transaction { - transaction_proto::Transaction { - signer_id: t.signer_id, - public_key: SingularPtrField::some(t.public_key.into()), - nonce: t.nonce, - receiver_id: t.receiver_id, - actions: RepeatedField::from_iter(t.actions.into_iter().map(std::convert::Into::into)), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - #[derive(Hash, PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub enum Action { CreateAccount(CreateAccountAction), @@ -79,70 +45,6 @@ pub enum Action { DeleteAccount(DeleteAccountAction), } -impl TryFrom for Action { - type Error = Box; - - fn try_from(a: transaction_proto::Action) -> Result { - match a.action { - Some(transaction_proto::Action_oneof_action::create_account(t)) => { - Ok(Action::CreateAccount(CreateAccountAction::try_from(t)?)) - } - Some(transaction_proto::Action_oneof_action::deploy_contract(t)) => { - Ok(Action::DeployContract(DeployContractAction::try_from(t)?)) - } - Some(transaction_proto::Action_oneof_action::function_call(t)) => { - Ok(Action::FunctionCall(FunctionCallAction::try_from(t)?)) - } - Some(transaction_proto::Action_oneof_action::transfer(t)) => { - Ok(Action::Transfer(TransferAction::try_from(t)?)) - } - Some(transaction_proto::Action_oneof_action::stake(t)) => { - Ok(Action::Stake(StakeAction::try_from(t)?)) - } - Some(transaction_proto::Action_oneof_action::add_key(t)) => { - Ok(Action::AddKey(AddKeyAction::try_from(t)?)) - } - Some(transaction_proto::Action_oneof_action::delete_key(t)) => { - Ok(Action::DeleteKey(DeleteKeyAction::try_from(t)?)) - } - Some(transaction_proto::Action_oneof_action::delete_account(t)) => { - Ok(Action::DeleteAccount(DeleteAccountAction::try_from(t)?)) - } - None => Err("No such action".into()), - } - } -} - -impl From for transaction_proto::Action { - fn from(a: Action) -> transaction_proto::Action { - let action = match a { - Action::CreateAccount(a) => { - transaction_proto::Action_oneof_action::create_account(a.into()) - } - Action::DeployContract(a) => { - transaction_proto::Action_oneof_action::deploy_contract(a.into()) - } - Action::FunctionCall(a) => { - transaction_proto::Action_oneof_action::function_call(a.into()) - } - Action::Transfer(a) => transaction_proto::Action_oneof_action::transfer(a.into()), - Action::Stake(a) => transaction_proto::Action_oneof_action::stake(a.into()), - Action::AddKey(a) => transaction_proto::Action_oneof_action::add_key(a.into()), - Action::DeleteKey(a) => transaction_proto::Action_oneof_action::delete_key(a.into()), - Action::DeleteAccount(a) => { - transaction_proto::Action_oneof_action::delete_account(a.into()) - } - }; - - transaction_proto::Action { - action: Some(action), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - impl Action { pub fn get_prepaid_gas(&self) -> Gas { match self { @@ -162,48 +64,12 @@ impl Action { #[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] pub struct CreateAccountAction {} -impl TryFrom for CreateAccountAction { - type Error = Box; - - fn try_from(_: transaction_proto::Action_CreateAccount) -> Result { - Ok(CreateAccountAction {}) - } -} - -impl From for transaction_proto::Action_CreateAccount { - fn from(_: CreateAccountAction) -> transaction_proto::Action_CreateAccount { - transaction_proto::Action_CreateAccount { - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - #[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone)] pub struct DeployContractAction { #[serde(with = "base_bytes_format")] pub code: Vec, } -impl TryFrom for DeployContractAction { - type Error = Box; - - fn try_from(a: transaction_proto::Action_DeployContract) -> Result { - Ok(DeployContractAction { code: a.code }) - } -} - -impl From for transaction_proto::Action_DeployContract { - fn from(a: DeployContractAction) -> transaction_proto::Action_DeployContract { - transaction_proto::Action_DeployContract { - code: a.code, - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - impl fmt::Debug for DeployContractAction { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("DeployContractAction") @@ -222,33 +88,6 @@ pub struct FunctionCallAction { pub deposit: Balance, } -impl TryFrom for FunctionCallAction { - type Error = Box; - - fn try_from(a: transaction_proto::Action_FunctionCall) -> Result { - Ok(FunctionCallAction { - method_name: a.method_name, - args: a.args, - gas: a.gas, - deposit: proto_to_type(a.deposit)?, - }) - } -} - -impl From for transaction_proto::Action_FunctionCall { - fn from(a: FunctionCallAction) -> transaction_proto::Action_FunctionCall { - transaction_proto::Action_FunctionCall { - method_name: a.method_name, - args: a.args, - gas: a.gas, - deposit: SingularPtrField::some(a.deposit.into()), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - impl fmt::Debug for FunctionCallAction { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("FunctionCallAction") @@ -266,25 +105,6 @@ pub struct TransferAction { pub deposit: Balance, } -impl TryFrom for TransferAction { - type Error = Box; - - fn try_from(a: transaction_proto::Action_Transfer) -> Result { - Ok(TransferAction { deposit: proto_to_type(a.deposit)? }) - } -} - -impl From for transaction_proto::Action_Transfer { - fn from(a: TransferAction) -> transaction_proto::Action_Transfer { - transaction_proto::Action_Transfer { - deposit: SingularPtrField::some(a.deposit.into()), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - #[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] pub struct StakeAction { #[serde(with = "u128_dec_format")] @@ -292,103 +112,22 @@ pub struct StakeAction { pub public_key: PublicKey, } -impl TryFrom for StakeAction { - type Error = Box; - - fn try_from(a: transaction_proto::Action_Stake) -> Result { - Ok(StakeAction { stake: proto_to_type(a.stake)?, public_key: proto_to_type(a.public_key)? }) - } -} - -impl From for transaction_proto::Action_Stake { - fn from(a: StakeAction) -> transaction_proto::Action_Stake { - transaction_proto::Action_Stake { - stake: SingularPtrField::some(a.stake.into()), - public_key: SingularPtrField::some(a.public_key.into()), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - #[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] pub struct AddKeyAction { pub public_key: PublicKey, pub access_key: AccessKey, } -impl TryFrom for AddKeyAction { - type Error = Box; - - fn try_from(a: transaction_proto::Action_AddKey) -> Result { - Ok(AddKeyAction { - public_key: proto_to_type(a.public_key)?, - access_key: proto_to_type(a.access_key)?, - }) - } -} - -impl From for transaction_proto::Action_AddKey { - fn from(a: AddKeyAction) -> transaction_proto::Action_AddKey { - transaction_proto::Action_AddKey { - public_key: SingularPtrField::some(a.public_key.into()), - access_key: SingularPtrField::some(a.access_key.into()), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - #[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] pub struct DeleteKeyAction { pub public_key: PublicKey, } -impl TryFrom for DeleteKeyAction { - type Error = Box; - - fn try_from(a: transaction_proto::Action_DeleteKey) -> Result { - Ok(DeleteKeyAction { public_key: proto_to_type(a.public_key)? }) - } -} - -impl From for transaction_proto::Action_DeleteKey { - fn from(a: DeleteKeyAction) -> transaction_proto::Action_DeleteKey { - transaction_proto::Action_DeleteKey { - public_key: SingularPtrField::some(a.public_key.into()), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - #[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] pub struct DeleteAccountAction { pub beneficiary_id: AccountId, } -impl TryFrom for DeleteAccountAction { - type Error = Box; - - fn try_from(a: transaction_proto::Action_DeleteAccount) -> Result { - Ok(DeleteAccountAction { beneficiary_id: a.beneficiary_id }) - } -} - -impl From for transaction_proto::Action_DeleteAccount { - fn from(a: DeleteAccountAction) -> transaction_proto::Action_DeleteAccount { - transaction_proto::Action_DeleteAccount { - beneficiary_id: a.beneficiary_id, - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - #[derive(Eq, Debug, Clone, Serialize, Deserialize)] pub struct SignedTransaction { pub signature: StructSignature, @@ -446,31 +185,6 @@ impl PartialEq for SignedTransaction { } } -impl TryFrom for SignedTransaction { - type Error = Box; - - fn try_from(t: transaction_proto::SignedTransaction) -> Result { - let signature: Signature = - Signature::try_from(&t.signature as &[u8]).map_err(|e| format!("{}", e))?; - let transaction_message = proto_to_result(t.transaction)?; - let hash = hash(&transaction_message.write_to_bytes()?); - let transaction = transaction_message.try_into()?; - Ok(SignedTransaction { signature, transaction, hash }) - } -} - -impl From for transaction_proto::SignedTransaction { - fn from(t: SignedTransaction) -> transaction_proto::SignedTransaction { - transaction_proto::SignedTransaction { - signature: t.signature.as_ref().to_vec(), - transaction: SingularPtrField::some(t.transaction.into()), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - #[derive(Hash, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub enum TransactionStatus { Unknown, diff --git a/core/primitives/src/types.rs b/core/primitives/src/types.rs index 52c9ccb73c1..5b99325938c 100644 --- a/core/primitives/src/types.rs +++ b/core/primitives/src/types.rs @@ -1,15 +1,10 @@ use std::convert::TryFrom; -use protobuf::SingularPtrField; - -use near_protos::types as types_proto; - // pub use crate::balance::Balance; use crate::crypto::aggregate_signature::BlsSignature; use crate::crypto::signature::{PublicKey, Signature}; use crate::hash::CryptoHash; use crate::serialize::u128_dec_format; -use crate::utils::proto_to_type; /// Public key alias. Used to human readable public key. #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)] @@ -73,31 +68,6 @@ impl ValidatorStake { } } -impl TryFrom for ValidatorStake { - type Error = Box; - - fn try_from(proto: types_proto::ValidatorStake) -> Result { - Ok(ValidatorStake { - account_id: proto.account_id, - public_key: proto_to_type(proto.public_key)?, - amount: proto_to_type(proto.amount)?, - }) - } -} - -impl From for types_proto::ValidatorStake { - fn from(validator: ValidatorStake) -> Self { - types_proto::ValidatorStake { - account_id: validator.account_id, - public_key: SingularPtrField::some(validator.public_key.into()), - amount: SingularPtrField::some(validator.amount.into()), - - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - impl PartialEq for ValidatorStake { fn eq(&self, other: &Self) -> bool { self.account_id == other.account_id && self.public_key == other.public_key diff --git a/core/primitives/src/utils.rs b/core/primitives/src/utils.rs index 8d1c8445e41..25681b88798 100644 --- a/core/primitives/src/utils.rs +++ b/core/primitives/src/utils.rs @@ -2,7 +2,6 @@ use std::convert::{AsRef, TryFrom, TryInto}; use std::fmt; use byteorder::{LittleEndian, WriteBytesExt}; -use protobuf::{well_known_types::StringValue, RepeatedField, SingularPtrField}; use regex::Regex; use lazy_static::lazy_static; @@ -126,30 +125,6 @@ pub fn is_valid_account_id(account_id: &AccountId) -> bool { VALID_ACCOUNT_ID.is_match(account_id) } -pub fn to_string_value(s: String) -> StringValue { - let mut res = StringValue::new(); - res.set_value(s); - res -} - -pub fn proto_to_result(proto: SingularPtrField) -> Result> { - proto.into_option().ok_or_else(|| "Bad Proto".into()) -} - -pub fn proto_to_type(proto: SingularPtrField) -> Result> -where - U: TryFrom>, -{ - proto_to_result(proto).and_then(TryInto::try_into) -} - -pub fn proto_to_vec(proto: RepeatedField) -> Result, Box> -where - U: TryFrom>, -{ - proto.into_iter().map(|v| v.try_into()).collect::, _>>() -} - /// A wrapper around Option that provides native Display trait. /// Simplifies propagating automatic Display trait on parent structs. pub struct DisplayOption(pub Option); diff --git a/core/protos/Cargo.toml b/core/protos/Cargo.toml deleted file mode 100644 index bbd3ee86404..00000000000 --- a/core/protos/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "near-protos" -version = "0.1.0" -authors = ["Near Inc "] -edition = "2018" -build = "build.rs" - -[dependencies] -protobuf = { version = "2.7", features = ["with-bytes"] } -byteorder = "1.2" - -[build-dependencies] -protos-autogen = { path = "../../protos/builder" } diff --git a/core/protos/README.md b/core/protos/README.md deleted file mode 100644 index 60b2e14a371..00000000000 --- a/core/protos/README.md +++ /dev/null @@ -1,4 +0,0 @@ -## Development -`.proto` files are under the `protos` directory. To add/delete protos, one also needs to change the include macro at the beginning of `src/lib.rs`. - -For example, if `example.proto` is added to `protos`, `include!(concat!(env!("OUT_DIR"), "/example.rs"))` needs to be added to `src/lib.rs`. \ No newline at end of file diff --git a/core/protos/build.rs b/core/protos/build.rs deleted file mode 100644 index d487c525dad..00000000000 --- a/core/protos/build.rs +++ /dev/null @@ -1,5 +0,0 @@ -use protos_autogen; - -fn main() { - protos_autogen::autogenerate(); -} diff --git a/core/protos/protos/access_key.proto b/core/protos/protos/access_key.proto deleted file mode 100644 index 45a7a2984c0..00000000000 --- a/core/protos/protos/access_key.proto +++ /dev/null @@ -1,18 +0,0 @@ -syntax = "proto3"; - -import "wrappers.proto"; -import "uint128.proto"; - -// Limited Access key to use owner's account with the fixed public_key. -// Access Key is stored under the key of owner's `account_id` and the `public_key`. -message AccessKey { - // Balance amount on this Access Key. Can be used to pay for the transactions. - Uint128 amount = 1; - // Owner of the balance of this Access Key. None means the account owner. - google.protobuf.StringValue balance_owner = 2; - // Contract ID that can be called with this Access Key. None means the account owner. - // Access key only allows to call given contract_id. - google.protobuf.StringValue contract_id = 3; - // The only method name that can be called with this Access Key. None means any method name. - google.protobuf.BytesValue method_name = 4; -} diff --git a/core/protos/protos/account.proto b/core/protos/protos/account.proto deleted file mode 100644 index daedb904ae5..00000000000 --- a/core/protos/protos/account.proto +++ /dev/null @@ -1,14 +0,0 @@ -syntax = "proto3"; - -import "wrappers.proto"; -import "uint128.proto"; - -message Account { - repeated bytes public_keys = 1; - uint64 nonce = 2; - Uint128 amount = 3; - Uint128 staked = 4; - bytes code_hash = 5; - uint64 storage_usage = 6; - uint64 storage_paid_at = 7; -} diff --git a/core/protos/protos/chain.proto b/core/protos/protos/chain.proto deleted file mode 100644 index 9dccc1a751f..00000000000 --- a/core/protos/protos/chain.proto +++ /dev/null @@ -1,28 +0,0 @@ -syntax = "proto3"; - -import "types.proto"; -import "receipt.proto"; -import "signed_transaction.proto"; - -message BlockHeaderBody { - uint64 height = 1; - bytes prev_hash = 2; - bytes prev_state_root = 3; - bytes tx_root = 4; - uint64 timestamp = 5; - repeated bool approval_mask = 6; - repeated bytes approval_sigs = 7; - uint64 total_weight = 8; - repeated ValidatorStake validator_proposal = 9; - bytes epoch_hash = 10; -} - -message BlockHeader { - BlockHeaderBody body = 1; - bytes signature = 2; -} - -message Block { - BlockHeader header = 1; - repeated SignedTransaction transactions = 2; -} diff --git a/core/protos/protos/network.proto b/core/protos/protos/network.proto deleted file mode 100644 index c4d0bcb2e06..00000000000 --- a/core/protos/protos/network.proto +++ /dev/null @@ -1,87 +0,0 @@ -syntax = "proto3"; - -import "chain.proto"; -import "signed_transaction.proto"; -import "receipt.proto"; -import "wrappers.proto"; - -message PeerInfo { - bytes id = 1; - google.protobuf.StringValue addr = 2; - google.protobuf.StringValue account_id = 3; -} - -message PeerChainInfo { - bytes genesis = 1; - uint64 height = 2; - uint64 total_weight = 3; -} - -message Handshake { - uint32 version = 1; - bytes peer_id = 2; - google.protobuf.StringValue account_id = 3; - google.protobuf.UInt32Value listen_port = 4; - PeerChainInfo chain_info = 5; -} - -message PeersResponse { - repeated PeerInfo peers = 1; -} - -message BlockApproval { - string account_id = 1; - bytes hash = 2; - bytes signature = 3; -} - -message BlockHeaderRequest { - repeated bytes hashes = 1; -} - -message BlockHeaders { - repeated BlockHeader headers = 1; -} - -message StateRequest { - uint64 shard_id = 1; - bytes hash = 2; -} - -message StateResponse { - uint64 shard_id = 1; - bytes hash = 2; - bytes payload = 3; - repeated Receipt receipts = 4; -} - -message AnnounceAccountRoute { - bytes peer_id = 1; - bytes hash = 2; - bytes signature = 3; - -} - -message AnnounceAccount { - string account_id = 1; - bytes epoch = 2; - repeated AnnounceAccountRoute route = 3; -} - -message PeerMessage { - oneof message_type { - Handshake hand_shake = 1; - bool peers_request = 2; - PeersResponse peers_response = 3; - BlockHeaderRequest block_headers_request = 4; - BlockHeaders block_headers = 5; - BlockHeader block_header_announce = 6; - bytes block_request = 7; - Block block = 8; - BlockApproval block_approval = 9; - SignedTransaction transaction = 10; - StateRequest state_request = 11; - StateResponse state_response = 12; - AnnounceAccount announce_account = 13; - } -} diff --git a/core/protos/protos/public_key.proto b/core/protos/protos/public_key.proto deleted file mode 100644 index 8b5d4e98fdb..00000000000 --- a/core/protos/protos/public_key.proto +++ /dev/null @@ -1,9 +0,0 @@ -syntax = "proto3"; - -message PublicKey { - enum KeyType { - ED25519 = 0; - } - KeyType key_type = 1; - bytes data = 2; -} diff --git a/core/protos/protos/receipt.proto b/core/protos/protos/receipt.proto deleted file mode 100644 index c41dd6686af..00000000000 --- a/core/protos/protos/receipt.proto +++ /dev/null @@ -1,45 +0,0 @@ -syntax = "proto3"; - -import "public_key.proto"; -import "signed_transaction.proto"; -import "uint128.proto"; -import "wrappers.proto"; - -message DataReceipt { - bytes data_id = 1; - google.protobuf.BytesValue data = 2; -} - -message ActionReceipt { - message DataReceiver { - bytes data_id = 1; - string receiver_id = 2; - } - - string signer_id = 1; - PublicKey signer_public_key = 2; - - // The price of gas is determined when the original SignedTransaction is - // converted into the Receipt. It's used for refunds. - Uint128 gas_price = 3; - - // List of data receivers where to route the output data - // (e.g. result of execution) - repeated DataReceiver output_data_receivers = 4; - - // Ordered list of data ID to provide as input results. - repeated bytes input_data_ids = 5; - - repeated Action actions = 6; -} - -message Receipt { - string predecessor_id = 1; - string receiver_id = 2; - bytes receipt_id = 3; - - oneof receipt { - ActionReceipt action = 4; - DataReceipt data = 5; - } -} diff --git a/core/protos/protos/signed_transaction.proto b/core/protos/protos/signed_transaction.proto deleted file mode 100644 index 4899f265a86..00000000000 --- a/core/protos/protos/signed_transaction.proto +++ /dev/null @@ -1,73 +0,0 @@ -syntax = "proto3"; - -import "access_key.proto"; -import "public_key.proto"; -import "uint128.proto"; - -message Action { - message CreateAccount { - // empty - } - - message DeployContract { - // Binary wasm code - bytes code = 1; - } - - message FunctionCall { - string method_name = 1; - bytes args = 2; - uint64 gas = 3; - Uint128 deposit = 4; - } - - message Transfer { - Uint128 deposit = 1; - } - - message Stake { - // New total stake - Uint128 stake = 1; - PublicKey public_key = 2; - } - - message AddKey { - PublicKey public_key = 1; - AccessKey access_key = 2; - } - - message DeleteKey { - PublicKey public_key = 1; - } - - message DeleteAccount { - // The account ID which would receive the remaining funds. - string beneficiary_id = 1; - } - - oneof action { - CreateAccount create_account = 1; - DeployContract deploy_contract = 2; - FunctionCall function_call = 3; - Transfer transfer = 4; - Stake stake = 5; - AddKey add_key = 6; - DeleteKey delete_key = 7; - DeleteAccount delete_account = 8; - } -} - -message Transaction { - string signer_id = 1; - PublicKey public_key = 2; - uint64 nonce = 3; - string receiver_id = 4; - - repeated Action actions = 5; -} - -message SignedTransaction { - bytes signature = 1; - - Transaction transaction = 2; -} diff --git a/core/protos/protos/types.proto b/core/protos/protos/types.proto deleted file mode 100644 index 90bc4bdd133..00000000000 --- a/core/protos/protos/types.proto +++ /dev/null @@ -1,21 +0,0 @@ -syntax = "proto3"; - -import "public_key.proto"; -import "uint128.proto"; - -message GroupSignature { - string signature = 1; - repeated bool authority_mask = 2; -} - -message MerkleNode { - bytes hash = 1; - // Direction encoded as bool. true -> Left, false -> right - bool direction = 2; -} - -message ValidatorStake { - string account_id = 1; - PublicKey public_key = 2; - Uint128 amount = 3; -} diff --git a/core/protos/protos/uint128.proto b/core/protos/protos/uint128.proto deleted file mode 100644 index 95e7fe0ec46..00000000000 --- a/core/protos/protos/uint128.proto +++ /dev/null @@ -1,6 +0,0 @@ -syntax = "proto3"; - -/// Provides container for unsigned 128 bit integers. -message Uint128 { - bytes number = 1; -} diff --git a/core/protos/protos/wrappers.proto b/core/protos/protos/wrappers.proto deleted file mode 100644 index 9ee41e384ac..00000000000 --- a/core/protos/protos/wrappers.proto +++ /dev/null @@ -1,123 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// Wrappers for primitive (non-message) types. These types are useful -// for embedding primitives in the `google.protobuf.Any` type and for places -// where we need to distinguish between the absence of a primitive -// typed field and its default value. -// -// These wrappers have no meaningful use within repeated fields as they lack -// the ability to detect presence on individual elements. -// These wrappers have no meaningful use within a map or a oneof since -// individual entries of a map or fields of a oneof can already detect presence. - -syntax = "proto3"; - -package google.protobuf; - -option csharp_namespace = "Google.Protobuf.WellKnownTypes"; -option cc_enable_arenas = true; -option go_package = "github.com/golang/protobuf/ptypes/wrappers"; -option java_package = "com.google.protobuf"; -option java_outer_classname = "WrappersProto"; -option java_multiple_files = true; -option objc_class_prefix = "GPB"; - -// Wrapper message for `double`. -// -// The JSON representation for `DoubleValue` is JSON number. -message DoubleValue { - // The double value. - double value = 1; -} - -// Wrapper message for `float`. -// -// The JSON representation for `FloatValue` is JSON number. -message FloatValue { - // The float value. - float value = 1; -} - -// Wrapper message for `int64`. -// -// The JSON representation for `Int64Value` is JSON string. -message Int64Value { - // The int64 value. - int64 value = 1; -} - -// Wrapper message for `uint64`. -// -// The JSON representation for `UInt64Value` is JSON string. -message UInt64Value { - // The uint64 value. - uint64 value = 1; -} - -// Wrapper message for `int32`. -// -// The JSON representation for `Int32Value` is JSON number. -message Int32Value { - // The int32 value. - int32 value = 1; -} - -// Wrapper message for `uint32`. -// -// The JSON representation for `UInt32Value` is JSON number. -message UInt32Value { - // The uint32 value. - uint32 value = 1; -} - -// Wrapper message for `bool`. -// -// The JSON representation for `BoolValue` is JSON `true` and `false`. -message BoolValue { - // The bool value. - bool value = 1; -} - -// Wrapper message for `string`. -// -// The JSON representation for `StringValue` is JSON string. -message StringValue { - // The string value. - string value = 1; -} - -// Wrapper message for `bytes`. -// -// The JSON representation for `BytesValue` is JSON string. -message BytesValue { - // The bytes value. - bytes value = 1; -} diff --git a/core/protos/src/lib.rs b/core/protos/src/lib.rs deleted file mode 100644 index c8692fb987a..00000000000 --- a/core/protos/src/lib.rs +++ /dev/null @@ -1,13 +0,0 @@ -include!(concat!(env!("OUT_DIR"), "/access_key.rs")); -include!(concat!(env!("OUT_DIR"), "/account.rs")); -include!(concat!(env!("OUT_DIR"), "/chain.rs")); -include!(concat!(env!("OUT_DIR"), "/network.rs")); -include!(concat!(env!("OUT_DIR"), "/public_key.rs")); -include!(concat!(env!("OUT_DIR"), "/receipt.rs")); -include!(concat!(env!("OUT_DIR"), "/signed_transaction.rs")); -include!(concat!(env!("OUT_DIR"), "/types.rs")); -include!(concat!(env!("OUT_DIR"), "/uint128.rs")); - -pub use protobuf::Message; - -pub mod uint128_ext; diff --git a/core/protos/src/uint128_ext.rs b/core/protos/src/uint128_ext.rs deleted file mode 100644 index 3648b79ddb2..00000000000 --- a/core/protos/src/uint128_ext.rs +++ /dev/null @@ -1,54 +0,0 @@ -use std::convert::TryFrom; -use std::io::Cursor; - -use byteorder::LittleEndian; -use byteorder::ReadBytesExt; - -use crate::uint128 as uint128_proto; - -impl TryFrom for u128 { - type Error = Box; - - fn try_from(value: uint128_proto::Uint128) -> Result { - let len = value.number.len(); - if len != 16 { - return Err(format!("uint128 proto has {} bytes, but expected up to 16.", len).into()); - } - let mut rdr = Cursor::new(value.number); - rdr.read_uint128::(len).map_err(|err| err.into()) - } -} - -impl From for uint128_proto::Uint128 { - fn from(value: u128) -> Self { - uint128_proto::Uint128 { - number: value.to_le_bytes().to_vec(), - cached_size: Default::default(), - unknown_fields: Default::default(), - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use std::convert::TryInto; - - #[test] - fn parse_u128() { - let x: u128 = 123_123_123_123_123_123_123_123; - let y: uint128_proto::Uint128 = x.into(); - let z: u128 = y.try_into().unwrap(); - assert_eq!(z, x); - } - - #[test] - fn fail_parse_u128() { - let x = uint128_proto::Uint128 { number: vec![], ..Default::default() }; - let y: Result = x.try_into(); - assert!(y.is_err()); - let x = uint128_proto::Uint128 { number: vec![0; 20], ..Default::default() }; - let y: Result = x.try_into(); - assert!(y.is_err()); - } -} diff --git a/core/store/Cargo.toml b/core/store/Cargo.toml index 6323078d337..ee92c848d84 100644 --- a/core/store/Cargo.toml +++ b/core/store/Cargo.toml @@ -6,7 +6,6 @@ edition = "2018" [dependencies] byteorder = "1.2" -protobuf = { version = "2.7", features = ["with-bytes"] } elastic-array = { version = "0.10" } kvdb = "0.1" kvdb-memorydb = "0.1" @@ -16,7 +15,6 @@ serde_derive = "1.0" cached = { git = "https://github.com/nearprotocol/cached", rev = "7e472eddef68607e344d5a106a0e6705d92e55be" } log = "0.4" -near-protos = { path = "../protos" } near-primitives = { path = "../primitives" } [dev-dependencies] diff --git a/core/store/src/lib.rs b/core/store/src/lib.rs index e304503cc3e..df368257c82 100644 --- a/core/store/src/lib.rs +++ b/core/store/src/lib.rs @@ -1,4 +1,3 @@ -use std::convert::TryInto; use std::sync::Arc; use std::{fmt, io}; @@ -6,7 +5,6 @@ use cached::{Cached, SizedCache}; pub use kvdb::DBValue; use kvdb::{DBOp, DBTransaction, KeyValueDB}; use kvdb_rocksdb::{Database, DatabaseConfig}; -use protobuf::{parse_from_bytes, Message}; use serde::de::DeserializeOwned; use serde::Serialize; @@ -18,8 +16,6 @@ use near_primitives::types::{AccountId, StorageUsage}; use near_primitives::utils::{ key_for_access_key, key_for_account, key_for_code, prefix_for_access_key, prefix_for_data, }; -use near_protos::access_key as access_key_proto; -use near_protos::account as account_proto; pub use crate::trie::{ update::TrieUpdate, update::TrieUpdateIterator, Trie, TrieChanges, TrieIterator, @@ -192,20 +188,6 @@ pub fn create_store(path: &str) -> Arc { Arc::new(Store::new(db)) } -/// Reads a proto from Trie. -pub fn get_proto(state_update: &TrieUpdate, key: &[u8]) -> Option { - state_update.get(key).and_then(|data| parse_from_bytes(&data).ok()) -} - -/// Writes a proto into Trie. -pub fn set_proto(state_update: &mut TrieUpdate, key: Vec, value: &T) { - value - .write_to_bytes() - .ok() - .map(|data| state_update.set(key, DBValue::from_vec(data))) - .or_else(|| None); -} - /// Reads an object from Trie. pub fn get(state_update: &TrieUpdate, key: &[u8]) -> Option { state_update.get(key).and_then(|data| Decode::decode(&data).ok()) @@ -218,8 +200,7 @@ pub fn set(state_update: &mut TrieUpdate, key: Vec, value: &T) /// Number of bytes the account data structure occupies in the storage. pub fn account_storage_size(account: &Account) -> StorageUsage { - let proto: account_proto::Account = account.clone().into(); - proto.write_to_bytes().map(|bytes| bytes.len() as StorageUsage).unwrap_or(0) + account.encode().map(|bytes| bytes.len() as StorageUsage).unwrap_or(0) } /// Number of bytes account and all of it's other data occupies in the storage. @@ -231,14 +212,11 @@ pub fn total_account_storage(account_id: &AccountId, account: &Account) -> Stora } pub fn set_account(state_update: &mut TrieUpdate, key: &AccountId, account: &Account) { - let proto: account_proto::Account = account.clone().into(); - set_proto(state_update, key_for_account(key), &proto) + set(state_update, key_for_account(key), &account) } pub fn get_account(state_update: &TrieUpdate, key: &AccountId) -> Option { - let proto: Option = get_proto(state_update, &key_for_account(&key)); - // TODO(1083): consider returning proto and adapting code to work with proto. - proto.and_then(|value| value.try_into().ok()) + get(state_update, &key_for_account(key)) } pub fn set_access_key( @@ -247,8 +225,7 @@ pub fn set_access_key( public_key: &PublicKey, access_key: &AccessKey, ) { - let proto: access_key_proto::AccessKey = access_key.clone().into(); - set_proto(state_update, key_for_access_key(account_id, public_key), &proto); + set(state_update, key_for_access_key(account_id, public_key), &access_key); } pub fn get_access_key( @@ -256,13 +233,11 @@ pub fn get_access_key( account_id: &AccountId, public_key: &PublicKey, ) -> Option { - get_proto(state_update, &key_for_access_key(account_id, public_key)) - .and_then(|value: access_key_proto::AccessKey| value.try_into().ok()) + get(state_update, &key_for_access_key(account_id, public_key)) } pub fn get_access_key_raw(state_update: &TrieUpdate, key: &[u8]) -> Option { - get_proto(state_update, key) - .and_then(|value: access_key_proto::AccessKey| value.try_into().ok()) + get(state_update, key) } pub fn set_code(state_update: &mut TrieUpdate, account_id: &AccountId, code: &ContractCode) { diff --git a/runtime/runtime/Cargo.toml b/runtime/runtime/Cargo.toml index c4e98955de1..36e7e92c18a 100644 --- a/runtime/runtime/Cargo.toml +++ b/runtime/runtime/Cargo.toml @@ -16,9 +16,7 @@ rand = "0.6" rand_xorshift = "0.1" ethash = "0.3" ethereum-bigint = "0.2" -protobuf = { version = "2.7", features = ["with-bytes"] } -near-protos = { path = "../../core/protos" } near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } near-verifier = { path = "../../runtime/verifier" } diff --git a/runtime/verifier/Cargo.toml b/runtime/verifier/Cargo.toml index a7654a29d06..c25df7a444b 100644 --- a/runtime/verifier/Cargo.toml +++ b/runtime/verifier/Cargo.toml @@ -6,6 +6,5 @@ edition = "2018" [dependencies] -near-protos = { path = "../../core/protos" } near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store"} diff --git a/test-utils/loadtester/Cargo.toml b/test-utils/loadtester/Cargo.toml index cb82407e8ef..37fc914140b 100644 --- a/test-utils/loadtester/Cargo.toml +++ b/test-utils/loadtester/Cargo.toml @@ -13,11 +13,9 @@ rand_xorshift = "0.1" hex = "0.3" futures = "0.1.25" tokio = "0.1" -protobuf = { version = "2.7", features = ["with-bytes"] } serde_json = "1.0.0" near-primitives = { path = "../../core/primitives" } -near-protos = { path = "../../core/protos" } node-runtime = { path = "../../runtime/runtime" } testlib = { path = "../testlib" } near = { path = "../../near" } diff --git a/test-utils/loadtester/src/remote_node.rs b/test-utils/loadtester/src/remote_node.rs index 25c372d5dd1..bfba12dbb31 100644 --- a/test-utils/loadtester/src/remote_node.rs +++ b/test-utils/loadtester/src/remote_node.rs @@ -4,13 +4,12 @@ use std::thread; use std::time::{Duration, Instant}; use futures::Future; -use protobuf::Message; use reqwest::r#async::Client as AsyncClient; use reqwest::Client as SyncClient; use near_primitives::crypto::signer::InMemorySigner; use near_primitives::rpc::AccountViewCallResult; -use near_primitives::serialize::from_base; +use near_primitives::serialize::{from_base, Encode}; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, Nonce}; @@ -159,13 +158,12 @@ impl RemoteNode { &self, transaction: SignedTransaction, ) -> Box + Send> { - let transaction: near_protos::signed_transaction::SignedTransaction = transaction.into(); - let tx_bytes = transaction.write_to_bytes().expect("write to bytes failed"); + let bytes = transaction.encode().unwrap(); let url = format!("{}{}", self.url, "/broadcast_tx_sync"); let response = self .async_client .post(url.as_str()) - .form(&[("tx", format!("0x{}", hex::encode(&tx_bytes)))]) + .form(&[("tx", format!("0x{}", hex::encode(&bytes)))]) .send() .map(|_| ()) .map_err(|err| format!("{}", err)); @@ -178,13 +176,12 @@ impl RemoteNode { &self, transaction: SignedTransaction, ) -> Result> { - let transaction: near_protos::signed_transaction::SignedTransaction = transaction.into(); - let tx_bytes = transaction.write_to_bytes().expect("write to bytes failed"); + let bytes = transaction.encode().unwrap(); let url = format!("{}{}", self.url, "/broadcast_tx_sync"); let result: serde_json::Value = self .sync_client .post(url.as_str()) - .form(&[("tx", format!("0x{}", hex::encode(&tx_bytes)))]) + .form(&[("tx", format!("0x{}", hex::encode(&bytes)))]) .send()? .json()?; Ok(result["result"]["hash"].as_str().ok_or(VALUE_NOT_STR_ERR)?.to_owned()) diff --git a/test-utils/state-viewer/Cargo.toml b/test-utils/state-viewer/Cargo.toml index c27bd5fef6d..16265bef740 100644 --- a/test-utils/state-viewer/Cargo.toml +++ b/test-utils/state-viewer/Cargo.toml @@ -7,9 +7,7 @@ edition = "2018" [dependencies] clap = "2.32.0" hex = "0.3" -protobuf = "2.7" -near-protos = { path = "../../core/protos" } near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } node-runtime = { path = "../../runtime/runtime" } diff --git a/test-utils/state-viewer/src/main.rs b/test-utils/state-viewer/src/main.rs index e961867ffb0..65c02d2ae7b 100644 --- a/test-utils/state-viewer/src/main.rs +++ b/test-utils/state-viewer/src/main.rs @@ -1,10 +1,8 @@ use std::convert::TryFrom; -use std::convert::TryInto; use std::path::Path; use std::sync::Arc; use clap::{App, Arg, SubCommand}; -use protobuf::parse_from_bytes; use near::{get_default_home, get_store_path, load_config, NearConfig, NightshadeRuntime}; use near_chain::{ChainStore, ChainStoreAccess}; @@ -12,12 +10,10 @@ use near_network::peer_store::PeerStore; use near_primitives::account::{AccessKey, Account}; use near_primitives::crypto::signature::PublicKey; use near_primitives::hash::{hash, CryptoHash}; -use near_primitives::serialize::{from_base64, to_base64}; +use near_primitives::serialize::{from_base64, to_base64, Decode}; use near_primitives::test_utils::init_integration_logger; use near_primitives::types::BlockIndex; use near_primitives::utils::{col, ACCOUNT_DATA_SEPARATOR}; -use near_protos::access_key as access_key_proto; -use near_protos::account as account_proto; use near_store::{create_store, DBValue, Store, TrieIterator}; use node_runtime::StateRecord; @@ -44,8 +40,7 @@ fn kv_to_state_record(key: Vec, value: DBValue) -> StateRecord { if separator.is_some() { StateRecord::Data { key: to_base64(&key), value: to_base64(&value) } } else { - let proto: account_proto::Account = parse_from_bytes(&value).unwrap(); - let account: Account = proto.try_into().unwrap(); + let account = Account::decode(&value).unwrap(); StateRecord::Account { account_id: to_printable(&key[1..]), account } } } @@ -60,8 +55,7 @@ fn kv_to_state_record(key: Vec, value: DBValue) -> StateRecord { } col::ACCESS_KEY => { let separator = (1..key.len()).find(|&x| key[x] == col::ACCESS_KEY[0]).unwrap(); - let proto: access_key_proto::AccessKey = parse_from_bytes(&value).unwrap(); - let access_key: AccessKey = proto.try_into().unwrap(); + let access_key = AccessKey::decode(&value).unwrap(); let account_id = to_printable(&key[1..separator]); let public_key = PublicKey::try_from(&key[(separator + 1)..]).unwrap(); StateRecord::AccessKey { account_id, public_key: public_key.to_readable(), access_key } diff --git a/test-utils/testlib/Cargo.toml b/test-utils/testlib/Cargo.toml index e72272e4387..883455a2dd4 100644 --- a/test-utils/testlib/Cargo.toml +++ b/test-utils/testlib/Cargo.toml @@ -21,10 +21,8 @@ lazy_static = "1.3" byteorder = "1.2" tempdir = "0.3" tokio-signal = "0.2" -protobuf = { version = "2.7", features = ["with-bytes"] } near-primitives = { path = "../../core/primitives" } -near-protos = { path = "../../core/protos" } near-store = { path = "../../core/store" } node-runtime = { path = "../../runtime/runtime" } near-chain = { path = "../../chain/chain" } diff --git a/test-utils/testlib/src/user/rpc_user.rs b/test-utils/testlib/src/user/rpc_user.rs index 5195b05c5c3..43fd8b9e464 100644 --- a/test-utils/testlib/src/user/rpc_user.rs +++ b/test-utils/testlib/src/user/rpc_user.rs @@ -2,7 +2,6 @@ use std::convert::TryInto; use std::sync::{Arc, RwLock}; use actix::System; -use protobuf::Message; use near_chain::Block; use near_client::StatusResponse; @@ -13,10 +12,9 @@ use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; use near_primitives::rpc::{AccountViewCallResult, QueryResponse, ViewStateResult}; -use near_primitives::serialize::{to_base, to_base64, BaseEncode}; +use near_primitives::serialize::{to_base, to_base64, BaseEncode, Encode}; use near_primitives::transaction::{FinalTransactionResult, SignedTransaction, TransactionResult}; use near_primitives::types::{AccountId, MerkleHash}; -use near_protos::signed_transaction as transaction_proto; use crate::user::User; @@ -49,10 +47,9 @@ impl User for RpcUser { } fn add_transaction(&self, transaction: SignedTransaction) -> Result<(), String> { - let proto: transaction_proto::SignedTransaction = transaction.into(); - let bytes = to_base64(&proto.write_to_bytes().unwrap()); + let bytes = transaction.encode().unwrap(); let _ = System::new("actix") - .block_on(self.client.write().unwrap().broadcast_tx_async(bytes))?; + .block_on(self.client.write().unwrap().broadcast_tx_async(to_base64(&bytes)))?; Ok(()) } @@ -60,9 +57,9 @@ impl User for RpcUser { &self, transaction: SignedTransaction, ) -> Result { - let proto: transaction_proto::SignedTransaction = transaction.into(); - let bytes = to_base64(&proto.write_to_bytes().unwrap()); - System::new("actix").block_on(self.client.write().unwrap().broadcast_tx_commit(bytes)) + let bytes = transaction.encode().unwrap(); + System::new("actix") + .block_on(self.client.write().unwrap().broadcast_tx_commit(to_base64(&bytes))) } fn add_receipt(&self, _receipt: Receipt) -> Result<(), String> { From 70cb07a3a610d5d15c0da1f972f8567fac61cc8a Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Tue, 13 Aug 2019 23:09:39 -0700 Subject: [PATCH 02/25] Step 2. View for Account and AccessKey instead of special encoding on them --- chain/chain/src/test_utils.rs | 13 +- core/primitives/src/account.rs | 15 +- core/primitives/src/block.rs | 2 - core/primitives/src/rpc.rs | 68 ++++++-- near/src/runtime.rs | 149 ++++++++++-------- near/tests/stake_nodes.rs | 9 +- runtime/runtime/src/adapter.rs | 25 +-- runtime/runtime/src/state_viewer.rs | 15 +- test-utils/loadtester/src/remote_node.rs | 4 +- test-utils/testlib/src/node/mod.rs | 4 +- test-utils/testlib/src/standard_test_cases.rs | 101 ++++++------ test-utils/testlib/src/user/mod.rs | 8 +- test-utils/testlib/src/user/rpc_user.rs | 7 +- test-utils/testlib/src/user/runtime_user.rs | 13 +- 14 files changed, 241 insertions(+), 192 deletions(-) diff --git a/chain/chain/src/test_utils.rs b/chain/chain/src/test_utils.rs index 44ee5e38ed3..57a8bfe770b 100644 --- a/chain/chain/src/test_utils.rs +++ b/chain/chain/src/test_utils.rs @@ -7,7 +7,7 @@ use near_primitives::crypto::signature::{verify, Signature}; use near_primitives::crypto::signer::InMemorySigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::Receipt; -use near_primitives::rpc::{AccountViewCallResult, QueryResponse}; +use near_primitives::rpc::QueryResponse; use near_primitives::test_utils::get_public_key_from_seed; use near_primitives::transaction::{ SignedTransaction, TransactionLog, TransactionResult, TransactionStatus, @@ -19,6 +19,7 @@ use near_store::{Store, StoreUpdate, Trie, TrieChanges, WrappedTrieChanges}; use crate::error::{Error, ErrorKind}; use crate::types::{BlockHeader, ReceiptResult, RuntimeAdapter, Weight}; use crate::{Chain, ValidTransaction}; +use near_primitives::account::Account; /// Simple key value runtime for tests. pub struct KeyValueRuntime { @@ -193,15 +194,7 @@ impl RuntimeAdapter for KeyValueRuntime { path: &str, _data: &[u8], ) -> Result> { - let path = path.split("/").collect::>(); - Ok(QueryResponse::ViewAccount(AccountViewCallResult { - account_id: path[1].to_string(), - nonce: 0, - amount: 1000, - stake: 0, - public_keys: vec![], - code_hash: CryptoHash::default(), - })) + Ok(QueryResponse::ViewAccount(Account::new(vec![], 0, CryptoHash::default(), 0).into())) } fn dump_state( diff --git a/core/primitives/src/account.rs b/core/primitives/src/account.rs index 130112b6779..d75f4d9da07 100644 --- a/core/primitives/src/account.rs +++ b/core/primitives/src/account.rs @@ -1,6 +1,4 @@ -use std::convert::{TryFrom, TryInto}; use std::fmt; -use std::iter::FromIterator; use crate::crypto::signature::PublicKey; use crate::hash::CryptoHash; @@ -11,13 +9,11 @@ use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage}; /// Per account information stored in the state. #[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] pub struct Account { - #[serde(with = "vec_base_format")] pub public_keys: Vec, pub nonce: Nonce, - // amount + staked is the total value of the account - #[serde(with = "u128_dec_format")] + /// The sum of `amount` and `staked` is the total value of the account. pub amount: Balance, - #[serde(with = "u128_dec_format")] + /// The amount staked by given account. pub staked: Balance, pub code_hash: CryptoHash, /// Storage used by the given account. @@ -25,12 +21,6 @@ pub struct Account { /// Last block index at which the storage was paid for. pub storage_paid_at: BlockIndex, } -// -///// A view of the account -//#[serde(remote = "Account")] -//pub struct AccountView { -// pub public -//} impl Account { pub fn new( @@ -67,7 +57,6 @@ impl Account { #[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone)] pub struct AccessKey { /// Balance amount on this Access Key. Can be used to pay for the transactions. - #[serde(with = "u128_dec_format")] pub amount: Balance, /// Owner of the balance of this Access Key. None means the account owner. pub balance_owner: Option, diff --git a/core/primitives/src/block.rs b/core/primitives/src/block.rs index 7c6285531ce..c5dc75b3767 100644 --- a/core/primitives/src/block.rs +++ b/core/primitives/src/block.rs @@ -1,6 +1,4 @@ use std::collections::HashMap; -use std::convert::{TryFrom, TryInto}; -use std::iter::FromIterator; use std::sync::Arc; use chrono::prelude::{DateTime, NaiveDateTime, Utc}; diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index 89034fdf064..1bb0e8a0c4d 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -4,23 +4,59 @@ use std::convert::TryFrom; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; -use crate::account::AccessKey; +use crate::account::{AccessKey, Account}; use crate::crypto::signature::PublicKey; use crate::hash::CryptoHash; use crate::serialize::{u128_dec_format, vec_base_format}; -use crate::types::{AccountId, Balance, BlockIndex, MerkleHash, Nonce, Version}; +use crate::types::{AccountId, Balance, BlockIndex, MerkleHash, Nonce, StorageUsage, Version}; -#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] -pub struct AccountViewCallResult { - pub account_id: AccountId, +/// A view of the account +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] +pub struct AccountView { + #[serde(with = "vec_base_format")] + pub public_keys: Vec, pub nonce: Nonce, #[serde(with = "u128_dec_format")] pub amount: Balance, #[serde(with = "u128_dec_format")] - pub stake: Balance, - #[serde(with = "vec_base_format")] - pub public_keys: Vec, + pub staked: Balance, pub code_hash: CryptoHash, + pub storage_usage: StorageUsage, + pub storage_paid_at: BlockIndex, +} + +impl From for AccountView { + fn from(account: Account) -> Self { + AccountView { + public_keys: account.public_keys, + nonce: account.nonce, + amount: account.amount, + staked: account.staked, + code_hash: account.code_hash, + storage_usage: account.storage_usage, + storage_paid_at: account.storage_paid_at, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] +pub struct AccessKeyView { + #[serde(with = "u128_dec_format")] + pub amount: Balance, + pub balance_owner: Option, + pub contract_id: Option, + pub method_name: Option>, +} + +impl From for AccessKeyView { + fn from(access_key: AccessKey) -> Self { + Self { + amount: access_key.amount, + balance_owner: access_key.balance_owner, + contract_id: access_key.contract_id, + method_name: access_key.method_name, + } + } } #[derive(Serialize, Deserialize, Debug)] @@ -40,15 +76,21 @@ pub struct QueryError { pub logs: Vec, } +#[derive(Serialize, Deserialize, Debug)] +pub struct AccessKeyInfo { + pub public_key: PublicKey, + pub access_key: AccessKeyView, +} + #[derive(Serialize, Deserialize, Debug)] #[serde(untagged)] pub enum QueryResponse { - ViewAccount(AccountViewCallResult), + ViewAccount(AccountView), ViewState(ViewStateResult), CallResult(CallResult), Error(QueryError), - AccessKey(Option), - AccessKeyList(Vec<(PublicKey, AccessKey)>), + AccessKey(Option), + AccessKeyList(Vec), } #[derive(Serialize, Deserialize, Debug)] @@ -82,7 +124,7 @@ pub struct StatusResponse { pub sync_info: StatusSyncInfo, } -impl TryFrom for AccountViewCallResult { +impl TryFrom for AccountView { type Error = String; fn try_from(query_response: QueryResponse) -> Result { @@ -104,7 +146,7 @@ impl TryFrom for ViewStateResult { } } -impl TryFrom for Option { +impl TryFrom for Option { type Error = String; fn try_from(query_response: QueryResponse) -> Result { diff --git a/near/src/runtime.rs b/near/src/runtime.rs index 826ea1c42b7..48c604628df 100644 --- a/near/src/runtime.rs +++ b/near/src/runtime.rs @@ -15,7 +15,7 @@ use near_primitives::account::{AccessKey, Account}; use near_primitives::crypto::signature::{verify, PublicKey, Signature}; use near_primitives::hash::{hash, CryptoHash}; use near_primitives::receipt::Receipt; -use near_primitives::rpc::{AccountViewCallResult, QueryResponse, ViewStateResult}; +use near_primitives::rpc::{QueryResponse, ViewStateResult}; use near_primitives::transaction::{SignedTransaction, TransactionLog}; use near_primitives::types::{AccountId, BlockIndex, MerkleHash, ShardId, ValidatorStake}; use near_primitives::utils::prefix_for_access_key; @@ -392,7 +392,7 @@ impl node_runtime::adapter::ViewRuntimeAdapter for NightshadeRuntime { &self, state_root: MerkleHash, account_id: &AccountId, - ) -> Result> { + ) -> Result> { let state_update = TrieUpdate::new(self.trie.clone(), state_root); self.trie_viewer.view_account(&state_update, account_id) } @@ -462,7 +462,6 @@ mod test { use near_primitives::crypto::signer::{EDSigner, InMemorySigner}; use near_primitives::hash::{hash, CryptoHash}; use near_primitives::receipt::Receipt; - use near_primitives::rpc::AccountViewCallResult; use near_primitives::transaction::{ Action, AddKeyAction, CreateAccountAction, SignedTransaction, StakeAction, TransferAction, }; @@ -475,7 +474,7 @@ mod test { use crate::test_utils::*; use crate::{get_store_path, GenesisConfig, NightshadeRuntime}; - use near_primitives::account::AccessKey; + use near_primitives::account::{AccessKey, Account}; fn stake(nonce: Nonce, sender: &BlockProducer, amount: Balance) -> SignedTransaction { SignedTransaction::from_actions( @@ -579,13 +578,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[0].account_id.clone(), + Account { nonce: 1, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE * 2, - stake: TESTING_INIT_STAKE * 2, + staked: TESTING_INIT_STAKE * 2, public_keys: vec![block_producers[0].signer.public_key()], code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); @@ -664,13 +664,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[0].account_id.clone(), + Account { nonce: 2, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE * 5, - stake: TESTING_INIT_STAKE * 2, + staked: TESTING_INIT_STAKE * 2, public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); state_root = nightshade.update(&state_root, 5, &h4, &h5, &vec![], &vec![]).0; @@ -681,26 +682,28 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[1].account_id.clone(), + Account { nonce: 0, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); let account = nightshade.view_account(state_root, &new_validator.account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: new_validator.account_id.clone(), + Account { nonce: 1, amount: TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE * 2, + staked: TESTING_INIT_STAKE * 2, public_keys: vec![new_validator.signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); @@ -712,13 +715,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[1].account_id.clone(), + Account { nonce: 0, amount: TESTING_INIT_BALANCE, - stake: 0, + staked: 0, public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); @@ -731,13 +735,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[1].account_id.clone(), + Account { nonce: 0, amount: TESTING_INIT_BALANCE, - stake: 0, + staked: 0, public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); } @@ -780,13 +785,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[0].account_id.clone(), + Account { nonce: 1, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0, } ); @@ -809,13 +815,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[0].account_id.clone(), + Account { nonce: 1, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); @@ -828,13 +835,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[0].account_id.clone(), + Account { nonce: 1, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE + 1, - stake: TESTING_INIT_STAKE - 1, + staked: TESTING_INIT_STAKE - 1, public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); } @@ -880,13 +888,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[0].account_id.clone(), + Account { nonce: 2, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); @@ -921,26 +930,28 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[0].account_id.clone(), + Account { nonce: 3, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, - stake: TESTING_INIT_STAKE + 1, + staked: TESTING_INIT_STAKE + 1, public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[1].account_id.clone(), + Account { nonce: 3, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, - stake: TESTING_INIT_STAKE + 1, + staked: TESTING_INIT_STAKE + 1, public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); @@ -953,26 +964,28 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[0].account_id.clone(), + Account { nonce: 3, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, - stake: TESTING_INIT_STAKE + 1, + staked: TESTING_INIT_STAKE + 1, public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[1].account_id.clone(), + Account { nonce: 3, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, - stake: TESTING_INIT_STAKE + 1, + staked: TESTING_INIT_STAKE + 1, public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); @@ -985,26 +998,28 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[0].account_id.clone(), + Account { nonce: 3, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, - stake: TESTING_INIT_STAKE + 1, + staked: TESTING_INIT_STAKE + 1, public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { - account_id: block_producers[1].account_id.clone(), + Account { nonce: 3, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE + 1, - stake: TESTING_INIT_STAKE - 1, + staked: TESTING_INIT_STAKE - 1, public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash + code_hash: account.code_hash, + storage_paid_at: 0, + storage_usage: 0 } ); } diff --git a/near/tests/stake_nodes.rs b/near/tests/stake_nodes.rs index 1032379a3cb..ca5bdd00a79 100644 --- a/near/tests/stake_nodes.rs +++ b/near/tests/stake_nodes.rs @@ -14,7 +14,6 @@ use near_network::test_utils::{convert_boot_nodes, open_port, WaitOrTimeout}; use near_network::NetworkClientMessages; use near_primitives::crypto::signer::EDSigner; use near_primitives::rpc::{QueryResponse, ValidatorInfo}; -use near_primitives::serialize::BaseEncode; use near_primitives::test_utils::init_integration_logger; use near_primitives::transaction::{Action, SignedTransaction, StakeAction}; use near_primitives::types::{AccountId, Balance, Nonce}; @@ -195,7 +194,7 @@ fn test_validator_kickout() { }) .then(move |res| match res.unwrap().unwrap() { QueryResponse::ViewAccount(result) => { - if result.stake == 0 + if result.staked == 0 || result.amount == TESTING_INIT_BALANCE { mark.store(true, Ordering::SeqCst); @@ -221,7 +220,7 @@ fn test_validator_kickout() { }) .then(move |res| match res.unwrap().unwrap() { QueryResponse::ViewAccount(result) => { - assert_eq!(result.stake, TESTING_INIT_STAKE); + assert_eq!(result.staked, TESTING_INIT_STAKE); assert_eq!( result.amount, TESTING_INIT_BALANCE - TESTING_INIT_STAKE @@ -305,7 +304,7 @@ fn test_validator_join() { }) .then(move |res| match res.unwrap().unwrap() { QueryResponse::ViewAccount(result) => { - if result.stake == 0 + if result.staked == 0 && result.amount == TESTING_INIT_BALANCE { done1_copy2.store(true, Ordering::SeqCst); @@ -324,7 +323,7 @@ fn test_validator_join() { }) .then(move |res| match res.unwrap().unwrap() { QueryResponse::ViewAccount(result) => { - if result.stake == TESTING_INIT_STAKE + if result.staked == TESTING_INIT_STAKE && result.amount == TESTING_INIT_BALANCE - TESTING_INIT_STAKE { diff --git a/runtime/runtime/src/adapter.rs b/runtime/runtime/src/adapter.rs index 3584a2a1a38..0a474498bc5 100644 --- a/runtime/runtime/src/adapter.rs +++ b/runtime/runtime/src/adapter.rs @@ -1,8 +1,6 @@ -use near_primitives::account::AccessKey; +use near_primitives::account::{AccessKey, Account}; use near_primitives::crypto::signature::PublicKey; -use near_primitives::rpc::{ - AccountViewCallResult, CallResult, QueryError, QueryResponse, ViewStateResult, -}; +use near_primitives::rpc::{AccessKeyInfo, CallResult, QueryError, QueryResponse, ViewStateResult}; use near_primitives::serialize::BaseDecode; use near_primitives::types::{AccountId, BlockIndex, MerkleHash}; @@ -12,7 +10,7 @@ pub trait ViewRuntimeAdapter { &self, state_root: MerkleHash, account_id: &AccountId, - ) -> Result>; + ) -> Result>; fn call_function( &self, @@ -60,7 +58,7 @@ pub fn query_client( } match path_parts[0] { "account" => match adapter.view_account(state_root, &AccountId::from(path_parts[1])) { - Ok(r) => Ok(QueryResponse::ViewAccount(r)), + Ok(account) => Ok(QueryResponse::ViewAccount(account.into())), Err(e) => Err(e), }, "call" => { @@ -85,9 +83,16 @@ pub fn query_client( }, "access_key" => { let result = if path_parts.len() == 2 { - adapter - .view_access_keys(state_root, &AccountId::from(path_parts[1])) - .map(|r| QueryResponse::AccessKeyList(r)) + adapter.view_access_keys(state_root, &AccountId::from(path_parts[1])).map(|mut r| { + QueryResponse::AccessKeyList( + r.drain(..) + .map(|(public_key, access_key)| AccessKeyInfo { + public_key, + access_key: access_key.into(), + }) + .collect(), + ) + }) } else { adapter .view_access_key( @@ -95,7 +100,7 @@ pub fn query_client( &AccountId::from(path_parts[1]), &PublicKey::from_base(path_parts[2])?, ) - .map(|r| QueryResponse::AccessKey(r)) + .map(|r| QueryResponse::AccessKey(r.map(|access_key| access_key.into()))) }; match result { Ok(result) => Ok(result), diff --git a/runtime/runtime/src/state_viewer.rs b/runtime/runtime/src/state_viewer.rs index 732ab99bb5c..d47063bcc3b 100644 --- a/runtime/runtime/src/state_viewer.rs +++ b/runtime/runtime/src/state_viewer.rs @@ -3,10 +3,10 @@ use std::str; use std::sync::{Arc, Mutex}; use std::time::Instant; -use near_primitives::account::AccessKey; +use near_primitives::account::{AccessKey, Account}; use near_primitives::crypto::signature::PublicKey; use near_primitives::hash::CryptoHash; -use near_primitives::rpc::{AccountViewCallResult, ViewStateResult}; +use near_primitives::rpc::ViewStateResult; use near_primitives::types::AccountId; use near_primitives::utils::{is_valid_account_id, prefix_for_data}; use near_store::{get_access_key, get_account, TrieUpdate}; @@ -30,20 +30,13 @@ impl TrieViewer { &self, state_update: &TrieUpdate, account_id: &AccountId, - ) -> Result> { + ) -> Result> { if !is_valid_account_id(account_id) { return Err(format!("Account ID '{}' is not valid", account_id).into()); } match get_account(state_update, &account_id) { - Some(account) => Ok(AccountViewCallResult { - account_id: account_id.clone(), - nonce: account.nonce, - amount: account.amount, - stake: account.staked, - public_keys: account.public_keys, - code_hash: account.code_hash, - }), + Some(account) => Ok(account), _ => Err(format!("account {} does not exist while viewing", account_id).into()), } } diff --git a/test-utils/loadtester/src/remote_node.rs b/test-utils/loadtester/src/remote_node.rs index bfba12dbb31..e833814faf7 100644 --- a/test-utils/loadtester/src/remote_node.rs +++ b/test-utils/loadtester/src/remote_node.rs @@ -8,7 +8,7 @@ use reqwest::r#async::Client as AsyncClient; use reqwest::Client as SyncClient; use near_primitives::crypto::signer::InMemorySigner; -use near_primitives::rpc::AccountViewCallResult; +use near_primitives::rpc::AccountView; use near_primitives::serialize::{from_base, Encode}; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, Nonce}; @@ -139,7 +139,7 @@ impl RemoteNode { fn view_account( &self, account_id: &AccountId, - ) -> Result> { + ) -> Result> { let url = format!("{}{}", self.url, "/abci_query"); let response: serde_json::Value = self .sync_client diff --git a/test-utils/testlib/src/node/mod.rs b/test-utils/testlib/src/node/mod.rs index 48345f324d7..3f9adffa740 100644 --- a/test-utils/testlib/src/node/mod.rs +++ b/test-utils/testlib/src/node/mod.rs @@ -8,7 +8,6 @@ use near::config::{ }; use near::NearConfig; use near_primitives::crypto::signer::{EDSigner, InMemorySigner}; -use near_primitives::rpc::AccountViewCallResult; use near_primitives::serialize::to_base64; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, Balance}; @@ -18,6 +17,7 @@ pub use crate::node::process_node::ProcessNode; pub use crate::node::runtime_node::RuntimeNode; pub use crate::node::thread_node::ThreadNode; use crate::user::{AsyncUser, User}; +use near_primitives::rpc::AccountView; mod process_node; mod runtime_node; @@ -51,7 +51,7 @@ pub trait Node: Send + Sync { fn kill(&mut self); - fn view_account(&self, account_id: &AccountId) -> Result { + fn view_account(&self, account_id: &AccountId) -> Result { self.user().view_account(account_id) } diff --git a/test-utils/testlib/src/standard_test_cases.rs b/test-utils/testlib/src/standard_test_cases.rs index f75c60609f0..35560d3dd1a 100644 --- a/test-utils/testlib/src/standard_test_cases.rs +++ b/test-utils/testlib/src/standard_test_cases.rs @@ -2,7 +2,6 @@ use near::config::{TESTING_INIT_BALANCE, TESTING_INIT_STAKE}; use near_primitives::account::AccessKey; use near_primitives::crypto::signer::InMemorySigner; use near_primitives::hash::{hash, CryptoHash}; -use near_primitives::rpc::AccountViewCallResult; use near_primitives::transaction::FinalTransactionStatus; use near_primitives::types::Balance; @@ -11,6 +10,7 @@ use crate::runtime_utils::{ alice_account, bob_account, default_code_hash, encode_int, eve_account, }; use crate::user::User; +use near_primitives::rpc::AccountView; use std::sync::Arc; /// The amount to send with function call. @@ -209,26 +209,28 @@ pub fn test_send_money(node: impl Node) { let result1 = node_user.view_account(account_id); assert_eq!( result1.unwrap(), - AccountViewCallResult { + AccountView { nonce: 1, - account_id: account_id.clone(), public_keys: vec![node.signer().public_key()], amount: TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); let result2 = node_user.view_account(&bob_account()).unwrap(); let public_keys = result2.public_keys.clone(); assert_eq!( result2, - AccountViewCallResult { + AccountView { nonce: 0, - account_id: bob_account(), public_keys, amount: TESTING_INIT_BALANCE + money_used - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); } @@ -246,26 +248,28 @@ pub fn test_send_money_over_balance(node: impl Node) { let result1 = node_user.view_account(account_id); assert_eq!( result1.unwrap(), - AccountViewCallResult { + AccountView { nonce: 0, - account_id: account_id.clone(), public_keys: vec![node.signer().public_key()], amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); let result2 = node_user.view_account(&bob_account()).unwrap(); let public_keys = result2.public_keys.clone(); assert_eq!( result2, - AccountViewCallResult { + AccountView { nonce: 0, - account_id: bob_account(), public_keys, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); } @@ -283,13 +287,14 @@ pub fn test_refund_on_send_money_to_non_existent_account(node: impl Node) { let result1 = node_user.view_account(account_id); assert_eq!( result1.unwrap(), - AccountViewCallResult { + AccountView { nonce: 1, - account_id: account_id.clone(), public_keys: vec![node.signer().public_key()], amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); let result2 = node_user.view_account(&eve_account()); @@ -316,13 +321,14 @@ pub fn test_create_account(node: impl Node) { let result1 = node_user.view_account(account_id); assert_eq!( result1.unwrap(), - AccountViewCallResult { + AccountView { nonce: 1, - account_id: account_id.clone(), public_keys: vec![node.signer().public_key()], amount: TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); @@ -330,13 +336,14 @@ pub fn test_create_account(node: impl Node) { let public_keys = result2.public_keys.clone(); assert_eq!( result2, - AccountViewCallResult { + AccountView { nonce: 0, - account_id: eve_account(), public_keys, amount: money_used, - stake: 0, + staked: 0, code_hash: CryptoHash::default(), + storage_paid_at: 0, + storage_usage: 0, } ); } @@ -356,13 +363,14 @@ pub fn test_create_account_again(node: impl Node) { let result1 = node_user.view_account(account_id); assert_eq!( result1.unwrap(), - AccountViewCallResult { + AccountView { nonce: 1, - account_id: account_id.clone(), public_keys: vec![node.signer().public_key()], amount: TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); @@ -370,13 +378,14 @@ pub fn test_create_account_again(node: impl Node) { let public_keys = result2.public_keys.clone(); assert_eq!( result2, - AccountViewCallResult { + AccountView { nonce: 0, - account_id: eve_account(), public_keys, amount: money_used, - stake: 0, + staked: 0, code_hash: CryptoHash::default(), + storage_paid_at: 0, + storage_usage: 0, } ); @@ -395,13 +404,14 @@ pub fn test_create_account_again(node: impl Node) { let result1 = node_user.view_account(account_id); assert_eq!( result1.unwrap(), - AccountViewCallResult { + AccountView { nonce: 2, - account_id: account_id.clone(), public_keys: vec![node.signer().public_key()], amount: TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); } @@ -434,13 +444,14 @@ pub fn test_create_account_failure_invalid_name(node: impl Node) { let account = node_user.view_account(account_id).unwrap(); assert_eq!( account, - AccountViewCallResult { + AccountView { nonce: 0, - account_id: account_id.clone(), public_keys: vec![node.signer().public_key()], amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); } @@ -466,13 +477,14 @@ pub fn test_create_account_failure_already_exists(node: impl Node) { let result1 = node_user.view_account(account_id); assert_eq!( result1.unwrap(), - AccountViewCallResult { + AccountView { nonce: 1, - account_id: account_id.clone(), public_keys: vec![node.signer().public_key()], amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); @@ -480,13 +492,14 @@ pub fn test_create_account_failure_already_exists(node: impl Node) { let public_keys = result2.public_keys.clone(); assert_eq!( result2, - AccountViewCallResult { + AccountView { nonce: 0, - account_id: bob_account(), public_keys, amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - stake: TESTING_INIT_STAKE, + staked: TESTING_INIT_STAKE, code_hash: default_code_hash(), + storage_paid_at: 0, + storage_usage: 0, } ); } @@ -625,7 +638,7 @@ pub fn test_add_access_key(node: impl Node) { assert_eq!(account.public_keys.len(), 1); let view_access_key = node_user.get_access_key(account_id, &signer2.public_key).unwrap(); - assert_eq!(view_access_key, Some(access_key)); + assert_eq!(view_access_key, Some(access_key.into())); } pub fn test_delete_access_key(node: impl Node) { @@ -674,7 +687,7 @@ pub fn test_add_access_key_with_funding(node: impl Node) { assert_eq!(account.amount, initial_balance - 10); let view_access_key = node_user.get_access_key(account_id, &signer2.public_key).unwrap(); - assert_eq!(view_access_key, Some(access_key)); + assert_eq!(view_access_key, Some(access_key.into())); } pub fn test_delete_access_key_with_owner_refund(node: impl Node) { @@ -810,7 +823,7 @@ pub fn test_increase_stake(node: impl Node) { let account = node_user.view_account(account_id).unwrap(); assert_eq!(account.amount, TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1); - assert_eq!(account.stake, amount_staked) + assert_eq!(account.staked, amount_staked) } pub fn test_decrease_stake(node: impl Node) { @@ -827,7 +840,7 @@ pub fn test_decrease_stake(node: impl Node) { let account = node_user.view_account(account_id).unwrap(); assert_eq!(account.amount, TESTING_INIT_BALANCE - TESTING_INIT_STAKE); - assert_eq!(account.stake, TESTING_INIT_STAKE); + assert_eq!(account.staked, TESTING_INIT_STAKE); } pub fn test_unstake_while_not_staked(node: impl Node) { diff --git a/test-utils/testlib/src/user/mod.rs b/test-utils/testlib/src/user/mod.rs index e3a46111f77..9ac7a4fab00 100644 --- a/test-utils/testlib/src/user/mod.rs +++ b/test-utils/testlib/src/user/mod.rs @@ -8,7 +8,7 @@ use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; -use near_primitives::rpc::{AccountViewCallResult, ViewStateResult}; +use near_primitives::rpc::{AccessKeyView, AccountView, ViewStateResult}; use near_primitives::transaction::{ Action, AddKeyAction, CreateAccountAction, DeleteAccountAction, DeleteKeyAction, DeployContractAction, FinalTransactionResult, FunctionCallAction, SignedTransaction, @@ -24,7 +24,7 @@ pub mod runtime_user; const POISONED_LOCK_ERR: &str = "The lock was poisoned."; pub trait User { - fn view_account(&self, account_id: &AccountId) -> Result; + fn view_account(&self, account_id: &AccountId) -> Result; fn view_balance(&self, account_id: &AccountId) -> Result { Ok(self.view_account(account_id)?.amount) @@ -59,7 +59,7 @@ pub trait User { &self, account_id: &AccountId, public_key: &PublicKey, - ) -> Result, String>; + ) -> Result, String>; fn signer(&self) -> Arc; @@ -218,7 +218,7 @@ pub trait AsyncUser: Send + Sync { fn view_account( &self, account_id: &AccountId, - ) -> Box>; + ) -> Box>; fn view_balance( &self, diff --git a/test-utils/testlib/src/user/rpc_user.rs b/test-utils/testlib/src/user/rpc_user.rs index 43fd8b9e464..49e29dbe901 100644 --- a/test-utils/testlib/src/user/rpc_user.rs +++ b/test-utils/testlib/src/user/rpc_user.rs @@ -6,12 +6,11 @@ use actix::System; use near_chain::Block; use near_client::StatusResponse; use near_jsonrpc::client::{new_client, JsonRpcClient}; -use near_primitives::account::AccessKey; use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; -use near_primitives::rpc::{AccountViewCallResult, QueryResponse, ViewStateResult}; +use near_primitives::rpc::{AccessKeyView, AccountView, QueryResponse, ViewStateResult}; use near_primitives::serialize::{to_base, to_base64, BaseEncode, Encode}; use near_primitives::transaction::{FinalTransactionResult, SignedTransaction, TransactionResult}; use near_primitives::types::{AccountId, MerkleHash}; @@ -38,7 +37,7 @@ impl RpcUser { } impl User for RpcUser { - fn view_account(&self, account_id: &AccountId) -> Result { + fn view_account(&self, account_id: &AccountId) -> Result { self.query(format!("account/{}", account_id), &[])?.try_into() } @@ -100,7 +99,7 @@ impl User for RpcUser { &self, account_id: &AccountId, public_key: &PublicKey, - ) -> Result, String> { + ) -> Result, String> { self.query(format!("access_key/{}/{}", account_id, public_key.to_base()), &[])?.try_into() } diff --git a/test-utils/testlib/src/user/runtime_user.rs b/test-utils/testlib/src/user/runtime_user.rs index f2c3d7bbbbb..cb922d69048 100644 --- a/test-utils/testlib/src/user/runtime_user.rs +++ b/test-utils/testlib/src/user/runtime_user.rs @@ -6,12 +6,11 @@ use tempdir::TempDir; use lazy_static::lazy_static; use near_chain::Block; -use near_primitives::account::AccessKey; use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; -use near_primitives::rpc::{AccountViewCallResult, ViewStateResult}; +use near_primitives::rpc::{AccessKeyView, AccountView, ViewStateResult}; use near_primitives::transaction::{ FinalTransactionResult, FinalTransactionStatus, SignedTransaction, TransactionLog, TransactionResult, TransactionStatus, @@ -158,9 +157,12 @@ impl RuntimeUser { } impl User for RuntimeUser { - fn view_account(&self, account_id: &AccountId) -> Result { + fn view_account(&self, account_id: &AccountId) -> Result { let state_update = self.client.read().expect(POISONED_LOCK_ERR).get_state_update(); - self.trie_viewer.view_account(&state_update, account_id).map_err(|err| err.to_string()) + self.trie_viewer + .view_account(&state_update, account_id) + .map(|account| account.into()) + .map_err(|err| err.to_string()) } fn view_state(&self, account_id: &AccountId, prefix: &[u8]) -> Result { @@ -222,10 +224,11 @@ impl User for RuntimeUser { &self, account_id: &AccountId, public_key: &PublicKey, - ) -> Result, String> { + ) -> Result, String> { let state_update = self.client.read().expect(POISONED_LOCK_ERR).get_state_update(); self.trie_viewer .view_access_key(&state_update, account_id, public_key) + .map(|value| value.map(|access_key| access_key.into())) .map_err(|err| err.to_string()) } From 00572138c9aa85cff65a1cda74349440ff6c311f Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Tue, 13 Aug 2019 23:35:13 -0700 Subject: [PATCH 03/25] Step 3. Public/SecretKey/Signature moved into views as well. Tests in runtime pass --- core/primitives/src/account.rs | 1 - core/primitives/src/block.rs | 6 +- core/primitives/src/crypto/group_signature.rs | 3 +- core/primitives/src/crypto/signature.rs | 65 +------- core/primitives/src/receipt.rs | 2 - core/primitives/src/rpc.rs | 112 ++++++++++++- near/src/runtime.rs | 148 +++--------------- runtime/runtime/src/adapter.rs | 2 +- 8 files changed, 133 insertions(+), 206 deletions(-) diff --git a/core/primitives/src/account.rs b/core/primitives/src/account.rs index d75f4d9da07..ffefc1e3965 100644 --- a/core/primitives/src/account.rs +++ b/core/primitives/src/account.rs @@ -3,7 +3,6 @@ use std::fmt; use crate::crypto::signature::PublicKey; use crate::hash::CryptoHash; use crate::logging; -use crate::serialize::{u128_dec_format, vec_base_format}; use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage}; /// Per account information stored in the state. diff --git a/core/primitives/src/block.rs b/core/primitives/src/block.rs index c5dc75b3767..973a96e7b5b 100644 --- a/core/primitives/src/block.rs +++ b/core/primitives/src/block.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::sync::Arc; -use chrono::prelude::{DateTime, NaiveDateTime, Utc}; +use chrono::prelude::{DateTime, Utc}; use chrono::serde::ts_nanoseconds; use crate::crypto::signature::{verify, PublicKey, Signature, DEFAULT_SIGNATURE}; @@ -10,10 +10,6 @@ use crate::hash::{hash, CryptoHash}; use crate::serialize::vec_base_format; use crate::transaction::SignedTransaction; use crate::types::{BlockIndex, MerkleHash, ValidatorStake}; -use bs58::alphabet::DEFAULT; - -/// Number of nano seconds in one second. -const NS_IN_SECOND: u64 = 1_000_000_000; #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] pub struct BlockHeader { diff --git a/core/primitives/src/crypto/group_signature.rs b/core/primitives/src/crypto/group_signature.rs index 156542a5ccf..3fdb8bb5d88 100644 --- a/core/primitives/src/crypto/group_signature.rs +++ b/core/primitives/src/crypto/group_signature.rs @@ -1,11 +1,10 @@ use core::fmt; -use std::convert::TryFrom; use crate::crypto::aggregate_signature::{ BlsAggregatePublicKey, BlsAggregateSignature, BlsPublicKey, BlsSignature, }; use crate::logging::pretty_hash; -use crate::serialize::{base_format, BaseDecode, BaseEncode}; +use crate::serialize::{base_format, BaseEncode}; use crate::types::{PartialSignature, ValidatorMask}; #[derive(Clone, Serialize, Deserialize, PartialEq, Eq)] diff --git a/core/primitives/src/crypto/signature.rs b/core/primitives/src/crypto/signature.rs index f2f4d267762..fbee401e9f5 100644 --- a/core/primitives/src/crypto/signature.rs +++ b/core/primitives/src/crypto/signature.rs @@ -9,15 +9,15 @@ pub use exonum_sodiumoxide::crypto::sign::ed25519::Seed; use crate::logging::pretty_hash; use crate::serialize::{from_base, to_base, BaseDecode, BaseEncode}; use crate::types::ReadablePublicKey; -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; -#[derive(Copy, Clone, Eq, PartialOrd, Ord, PartialEq)] +#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialOrd, Ord, PartialEq)] pub struct PublicKey(pub sodiumoxide::crypto::sign::ed25519::PublicKey); -#[derive(Clone, Eq, PartialEq)] +#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)] pub struct SecretKey(pub sodiumoxide::crypto::sign::ed25519::SecretKey); -#[derive(Clone, Eq, PartialEq, Hash)] +#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Hash)] pub struct Signature(pub sodiumoxide::crypto::sign::ed25519::Signature); pub fn sign(data: &[u8], secret_key: &SecretKey) -> Signature { @@ -119,25 +119,6 @@ impl TryFrom<&str> for PublicKey { } } -impl Serialize for PublicKey { - fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> - where - S: Serializer, - { - serializer.serialize_str(&self.to_base()) - } -} - -impl<'de> Deserialize<'de> for PublicKey { - fn deserialize(deserializer: D) -> Result>::Error> - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - Self::from_base(&s).map_err(|err| serde::de::Error::custom(err.to_string())) - } -} - impl TryFrom<&[u8]> for SecretKey { type Error = Box; @@ -170,25 +151,6 @@ impl TryFrom<&str> for SecretKey { } } -impl Serialize for SecretKey { - fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> - where - S: Serializer, - { - serializer.serialize_str(&self.to_base()) - } -} - -impl<'de> Deserialize<'de> for SecretKey { - fn deserialize(deserializer: D) -> Result>::Error> - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - Self::from_base(&s).map_err(|err| serde::de::Error::custom(err.to_string())) - } -} - impl TryFrom<&[u8]> for Signature { type Error = Box; @@ -230,25 +192,6 @@ impl TryFrom<&str> for Signature { } } -impl Serialize for Signature { - fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> - where - S: Serializer, - { - serializer.serialize_str(&self.to_base()) - } -} - -impl<'de> Deserialize<'de> for Signature { - fn deserialize(deserializer: D) -> Result>::Error> - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - Self::from_base(&s).map_err(|err| serde::de::Error::custom(err.to_string())) - } -} - impl std::convert::AsRef<[u8]> for PublicKey { fn as_ref(&self) -> &[u8] { &self.0[..] diff --git a/core/primitives/src/receipt.rs b/core/primitives/src/receipt.rs index 2e868fbfaf0..9e9a1e6afbe 100644 --- a/core/primitives/src/receipt.rs +++ b/core/primitives/src/receipt.rs @@ -1,7 +1,5 @@ use std::borrow::Borrow; -use std::convert::{TryFrom, TryInto}; use std::fmt; -use std::iter::FromIterator; use crate::crypto::signature::PublicKey; use crate::hash::CryptoHash; diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index 1bb0e8a0c4d..b525d71a629 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -2,19 +2,111 @@ use std::collections::HashMap; use std::convert::TryFrom; use chrono::{DateTime, Utc}; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::account::{AccessKey, Account}; -use crate::crypto::signature::PublicKey; +use crate::crypto::signature::{PublicKey, SecretKey, Signature}; use crate::hash::CryptoHash; -use crate::serialize::{u128_dec_format, vec_base_format}; +use crate::serialize::{from_base, to_base, u128_dec_format, vec_base_format}; use crate::types::{AccountId, Balance, BlockIndex, MerkleHash, Nonce, StorageUsage, Version}; +/// Number of nano seconds in one second. +//const NS_IN_SECOND: u64 = 1_000_000_000; + +#[derive(Debug, Eq, PartialEq)] +pub struct PublicKeyView(Vec); + +impl Serialize for PublicKeyView { + fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> + where + S: Serializer, + { + serializer.serialize_str(&to_base(&self.0)) + } +} + +impl<'de> Deserialize<'de> for PublicKeyView { + fn deserialize(deserializer: D) -> Result>::Error> + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + from_base(&s) + .map(|v| PublicKeyView(v)) + .map_err(|err| serde::de::Error::custom(err.to_string())) + } +} + +impl From for PublicKeyView { + fn from(public_key: PublicKey) -> Self { + Self(public_key.0.as_ref().to_vec()) + } +} + +#[derive(Debug)] +pub struct SecretKeyView(Vec); + +impl Serialize for SecretKeyView { + fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> + where + S: Serializer, + { + serializer.serialize_str(&to_base(&self.0)) + } +} + +impl<'de> Deserialize<'de> for SecretKeyView { + fn deserialize(deserializer: D) -> Result>::Error> + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + from_base(&s) + .map(|v| SecretKeyView(v)) + .map_err(|err| serde::de::Error::custom(err.to_string())) + } +} + +impl From for SecretKeyView { + fn from(secret_key: SecretKey) -> Self { + Self(secret_key.0[..].to_vec()) + } +} + +#[derive(Debug)] +pub struct SignatureView(Vec); + +impl Serialize for SignatureView { + fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> + where + S: Serializer, + { + serializer.serialize_str(&to_base(&self.0)) + } +} + +impl<'de> Deserialize<'de> for SignatureView { + fn deserialize(deserializer: D) -> Result>::Error> + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + from_base(&s) + .map(|v| SignatureView(v)) + .map_err(|err| serde::de::Error::custom(err.to_string())) + } +} + +impl From for SignatureView { + fn from(signature: Signature) -> Self { + Self(signature.0.as_ref().to_vec()) + } +} + /// A view of the account #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] pub struct AccountView { - #[serde(with = "vec_base_format")] - pub public_keys: Vec, + pub public_keys: Vec, pub nonce: Nonce, #[serde(with = "u128_dec_format")] pub amount: Balance, @@ -26,9 +118,13 @@ pub struct AccountView { } impl From for AccountView { - fn from(account: Account) -> Self { + fn from(mut account: Account) -> Self { AccountView { - public_keys: account.public_keys, + public_keys: account + .public_keys + .drain(..) + .map(|public_key| public_key.into()) + .collect(), nonce: account.nonce, amount: account.amount, staked: account.staked, @@ -78,7 +174,7 @@ pub struct QueryError { #[derive(Serialize, Deserialize, Debug)] pub struct AccessKeyInfo { - pub public_key: PublicKey, + pub public_key: PublicKeyView, pub access_key: AccessKeyView, } diff --git a/near/src/runtime.rs b/near/src/runtime.rs index 48c604628df..2887380b60d 100644 --- a/near/src/runtime.rs +++ b/near/src/runtime.rs @@ -577,16 +577,8 @@ mod test { ); let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( - account, - Account { - nonce: 1, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE * 2, - staked: TESTING_INIT_STAKE * 2, - public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } + (account.amount, account.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE * 2, 2 * TESTING_INIT_STAKE) ); nightshade @@ -663,17 +655,10 @@ mod test { nightshade.add_validator_proposals(h3, h4, 4, vec![], vec![], vec![]).unwrap(); let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( - account, - Account { - nonce: 2, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE * 5, - staked: TESTING_INIT_STAKE * 2, - public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } + (account.amount, account.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE * 5, 2 * TESTING_INIT_STAKE) ); + state_root = nightshade.update(&state_root, 5, &h4, &h5, &vec![], &vec![]).0; nightshade.add_validator_proposals(h4, h5, 5, vec![], vec![], vec![]).unwrap(); state_root = nightshade.update(&state_root, 6, &h5, &h6, &vec![], &vec![]).0; @@ -681,31 +666,12 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); assert_eq!( - account, - Account { - nonce: 0, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE, - public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } + (account.amount, account.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE, TESTING_INIT_STAKE) ); let account = nightshade.view_account(state_root, &new_validator.account_id).unwrap(); - assert_eq!( - account, - Account { - nonce: 1, - amount: TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE * 2, - public_keys: vec![new_validator.signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } - ); + assert_eq!((account.amount, account.staked), (TESTING_INIT_STAKE, TESTING_INIT_STAKE * 2)); state_root = nightshade.update(&state_root, 7, &h6, &h7, &vec![], &vec![]).0; nightshade.add_validator_proposals(h6, h7, 7, vec![], vec![], vec![]).unwrap(); @@ -713,18 +679,7 @@ mod test { nightshade.add_validator_proposals(h7, h8, 8, vec![], vec![], vec![]).unwrap(); let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); - assert_eq!( - account, - Account { - nonce: 0, - amount: TESTING_INIT_BALANCE, - staked: 0, - public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } - ); + assert_eq!((account.amount, account.staked), (TESTING_INIT_BALANCE, 0)); state_root = nightshade.update(&state_root, 9, &h8, &h9, &vec![], &vec![]).0; nightshade.add_validator_proposals(h8, h9, 9, vec![], vec![], vec![]).unwrap(); @@ -733,18 +688,7 @@ mod test { // make sure their is no double return of stake let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); - assert_eq!( - account, - Account { - nonce: 0, - amount: TESTING_INIT_BALANCE, - staked: 0, - public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } - ); + assert_eq!((account.amount, account.staked), (TESTING_INIT_BALANCE, 0)); } #[test] @@ -929,30 +873,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( - account, - Account { - nonce: 3, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, - staked: TESTING_INIT_STAKE + 1, - public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } + (account.amount, account.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, 1 + TESTING_INIT_STAKE) ); let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); assert_eq!( - account, - Account { - nonce: 3, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, - staked: TESTING_INIT_STAKE + 1, - public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } + (account.amount, account.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, 1 + TESTING_INIT_STAKE) ); state_root = nightshade.update(&state_root, 5, &h4, &h5, &vec![], &vec![]).0; @@ -963,30 +891,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( - account, - Account { - nonce: 3, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, - staked: TESTING_INIT_STAKE + 1, - public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } + (account.amount, account.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, 1 + TESTING_INIT_STAKE) ); let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); assert_eq!( - account, - Account { - nonce: 3, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, - staked: TESTING_INIT_STAKE + 1, - public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } + (account.amount, account.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, 1 + TESTING_INIT_STAKE) ); state_root = nightshade.update(&state_root, 7, &h6, &h7, &vec![], &vec![]).0; @@ -997,30 +909,14 @@ mod test { let account = nightshade.view_account(state_root, &block_producers[0].account_id).unwrap(); assert_eq!( - account, - Account { - nonce: 3, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, - staked: TESTING_INIT_STAKE + 1, - public_keys: vec![block_producers[0].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } + (account.amount, account.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE - 1, 1 + TESTING_INIT_STAKE) ); let account = nightshade.view_account(state_root, &block_producers[1].account_id).unwrap(); assert_eq!( - account, - Account { - nonce: 3, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE + 1, - staked: TESTING_INIT_STAKE - 1, - public_keys: vec![block_producers[1].signer.public_key()], - code_hash: account.code_hash, - storage_paid_at: 0, - storage_usage: 0 - } + (account.amount, account.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE + 1, TESTING_INIT_STAKE - 1) ); } diff --git a/runtime/runtime/src/adapter.rs b/runtime/runtime/src/adapter.rs index 0a474498bc5..315d57303d3 100644 --- a/runtime/runtime/src/adapter.rs +++ b/runtime/runtime/src/adapter.rs @@ -87,7 +87,7 @@ pub fn query_client( QueryResponse::AccessKeyList( r.drain(..) .map(|(public_key, access_key)| AccessKeyInfo { - public_key, + public_key: public_key.into(), access_key: access_key.into(), }) .collect(), From 46aea7311d47f4fba4bc045e5322d22d9a2fd03e Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Tue, 13 Aug 2019 23:41:44 -0700 Subject: [PATCH 04/25] Step 4. CryptoHash View --- chain/client/src/client.rs | 4 ++-- core/primitives/src/hash.rs | 23 ++------------------- core/primitives/src/rpc.rs | 40 ++++++++++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/chain/client/src/client.rs b/chain/client/src/client.rs index 2ee84cded93..5d851ccac0b 100644 --- a/chain/client/src/client.rs +++ b/chain/client/src/client.rs @@ -343,9 +343,9 @@ impl Handler for ClientActor { rpc_addr: self.config.rpc_addr.clone(), validators, sync_info: StatusSyncInfo { - latest_block_hash: head.last_block_hash, + latest_block_hash: head.last_block_hash.into(), latest_block_height: head.height, - latest_state_root: state_root.clone(), + latest_state_root: state_root.clone().into(), latest_block_time, syncing: self.sync_status.is_syncing(), }, diff --git a/core/primitives/src/hash.rs b/core/primitives/src/hash.rs index fd57d990cd0..c996a874121 100644 --- a/core/primitives/src/hash.rs +++ b/core/primitives/src/hash.rs @@ -7,9 +7,9 @@ use exonum_sodiumoxide::crypto::hash::sha256::Digest; use crate::logging::pretty_hash; use crate::serialize::{from_base, to_base, BaseDecode, BaseEncode, Encode}; -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; -#[derive(Copy, Clone, PartialOrd, Ord)] +#[derive(Copy, Clone, PartialOrd, Ord, Serialize, Deserialize)] pub struct CryptoHash(pub Digest); impl<'a> From<&'a CryptoHash> for String { @@ -104,25 +104,6 @@ impl PartialEq for CryptoHash { } } -impl Serialize for CryptoHash { - fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> - where - S: Serializer, - { - serializer.serialize_str(&self.to_base()) - } -} - -impl<'de> Deserialize<'de> for CryptoHash { - fn deserialize(deserializer: D) -> Result>::Error> - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - Self::from_base(&s).map_err(|err| serde::de::Error::custom(err.to_string())) - } -} - impl Eq for CryptoHash {} /// Calculates a hash of a bytes slice. diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index b525d71a629..acee25906ad 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -8,7 +8,7 @@ use crate::account::{AccessKey, Account}; use crate::crypto::signature::{PublicKey, SecretKey, Signature}; use crate::hash::CryptoHash; use crate::serialize::{from_base, to_base, u128_dec_format, vec_base_format}; -use crate::types::{AccountId, Balance, BlockIndex, MerkleHash, Nonce, StorageUsage, Version}; +use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage, Version}; /// Number of nano seconds in one second. //const NS_IN_SECOND: u64 = 1_000_000_000; @@ -103,6 +103,36 @@ impl From for SignatureView { } } +#[derive(Debug, PartialEq, Eq)] +pub struct CryptoHashView(Vec); + +impl Serialize for CryptoHashView { + fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> + where + S: Serializer, + { + serializer.serialize_str(&to_base(&self.0)) + } +} + +impl<'de> Deserialize<'de> for CryptoHashView { + fn deserialize(deserializer: D) -> Result>::Error> + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + from_base(&s) + .map(|v| CryptoHashView(v)) + .map_err(|err| serde::de::Error::custom(err.to_string())) + } +} + +impl From for CryptoHashView { + fn from(hash: CryptoHash) -> Self { + CryptoHashView(hash.as_ref().to_vec()) + } +} + /// A view of the account #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] pub struct AccountView { @@ -112,7 +142,7 @@ pub struct AccountView { pub amount: Balance, #[serde(with = "u128_dec_format")] pub staked: Balance, - pub code_hash: CryptoHash, + pub code_hash: CryptoHashView, pub storage_usage: StorageUsage, pub storage_paid_at: BlockIndex, } @@ -128,7 +158,7 @@ impl From for AccountView { nonce: account.nonce, amount: account.amount, staked: account.staked, - code_hash: account.code_hash, + code_hash: account.code_hash.into(), storage_usage: account.storage_usage, storage_paid_at: account.storage_paid_at, } @@ -191,9 +221,9 @@ pub enum QueryResponse { #[derive(Serialize, Deserialize, Debug)] pub struct StatusSyncInfo { - pub latest_block_hash: CryptoHash, + pub latest_block_hash: CryptoHashView, pub latest_block_height: BlockIndex, - pub latest_state_root: MerkleHash, + pub latest_state_root: CryptoHashView, pub latest_block_time: DateTime, pub syncing: bool, } From 43ba7609a9e4422c103de2235bf7f02dfb350bcd Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Wed, 14 Aug 2019 11:07:36 -0700 Subject: [PATCH 05/25] Adding Readable/Writable for custom deserialization --- chain/chain/src/chain.rs | 29 +++--- core/primitives/Cargo.toml | 4 + core/primitives/benches/serialization.rs | 116 +++++++++++++++++++++ core/primitives/src/account.rs | 1 + core/primitives/src/block.rs | 123 +++++++++++----------- core/primitives/src/rpc.rs | 40 +++++++- core/primitives/src/serialize.rs | 125 +++++++++++++++++++++++ core/primitives/src/transaction.rs | 9 +- 8 files changed, 372 insertions(+), 75 deletions(-) create mode 100644 core/primitives/benches/serialization.rs diff --git a/chain/chain/src/chain.rs b/chain/chain/src/chain.rs index ac3e6d9b671..27d06df9c27 100644 --- a/chain/chain/src/chain.rs +++ b/chain/chain/src/chain.rs @@ -57,10 +57,11 @@ impl OrphanBlockPool { } fn add(&mut self, orphan: Orphan) { - let height_hashes = self.height_idx.entry(orphan.block.header.height).or_insert(vec![]); + let height_hashes = + self.height_idx.entry(orphan.block.header.inner.height).or_insert(vec![]); height_hashes.push(orphan.block.hash()); let prev_hash_entries = - self.prev_hash_idx.entry(orphan.block.header.prev_hash).or_insert(vec![]); + self.prev_hash_idx.entry(orphan.block.header.inner.prev_hash).or_insert(vec![]); prev_hash_entries.push(orphan.block.hash()); self.orphans.insert(orphan.block.hash(), orphan); @@ -176,8 +177,10 @@ impl Chain { vec![], ) .map_err(|err| ErrorKind::Other(err.to_string()))?; - store_update - .save_post_state_root(&genesis.hash(), &genesis.header.prev_state_root); + store_update.save_post_state_root( + &genesis.hash(), + &genesis.header.inner.prev_state_root, + ); store_update.save_post_validator_proposals(&genesis.hash(), vec![]); store_update.save_block_header(genesis.header.clone()); store_update.save_block(genesis.clone()); @@ -271,13 +274,13 @@ impl Chain { let mut oldest_height = 0; let mut current = self.get_block_header(&header_head.last_block_hash).map(|h| h.clone()); while let Ok(header) = current { - if header.height <= block_head.height { + if header.inner.height <= block_head.height { if self.is_on_current_chain(&header).is_ok() { break; } } - oldest_height = header.height; + oldest_height = header.inner.height; hashes.push(header.hash()); current = self.get_previous_header(&header).map(|h| h.clone()); } @@ -291,7 +294,7 @@ impl Chain { /// Returns if given block header on the current chain. fn is_on_current_chain(&mut self, header: &BlockHeader) -> Result<(), Error> { - let chain_header = self.get_header_by_height(header.height)?; + let chain_header = self.get_header_by_height(header.inner.height)?; if chain_header.hash() == header.hash() { Ok(()) } else { @@ -303,7 +306,7 @@ impl Chain { pub fn find_common_header(&mut self, hashes: &Vec) -> Option { for hash in hashes { if let Ok(header) = self.get_block_header(&hash).map(|h| h.clone()) { - if let Ok(header_at_height) = self.get_header_by_height(header.height) { + if let Ok(header_at_height) = self.get_header_by_height(header.inner.height) { if header.hash() == header_at_height.hash() { return Some(header); } @@ -382,7 +385,7 @@ impl Chain { target: "chain", "Block {} at {} is unfit at this time: {}", block.hash(), - block.header.height, + block.header.inner.height, msg ); Err(ErrorKind::Unfit(msg.clone()).into()) @@ -446,7 +449,7 @@ impl Chain { ) -> Result<(), Error> { // TODO(1046): update this with any required changes for chunks support. let header = self.get_block_header(&hash)?; - let (prev_hash, state_root) = (header.prev_hash, header.prev_state_root); + let (prev_hash, state_root) = (header.inner.prev_hash, header.inner.prev_state_root); // Save state in the runtime, will also check it's validity. self.runtime_adapter @@ -623,7 +626,7 @@ impl<'a> ChainUpdate<'a> { /// based on this. We will update these once we get the block back after /// requesting it. pub fn process_block_header(&mut self, header: &BlockHeader) -> Result<(), Error> { - debug!(target: "chain", "Process block header: {} at {}", header.hash(), header.height); + debug!(target: "chain", "Process block header: {} at {}", header.hash(), header.inner.height); self.check_header_known(header)?; self.validate_header(header, &Provenance::NONE)?; @@ -645,14 +648,14 @@ impl<'a> ChainUpdate<'a> { block: &Block, provenance: &Provenance, ) -> Result, Error> { - debug!(target: "chain", "Process block {} at {}, approvals: {}, tx: {}", block.hash(), block.header.height, block.header.approval_sigs.len(), block.transactions.len()); + debug!(target: "chain", "Process block {} at {}, approvals: {}, tx: {}", block.hash(), block.header.inner.height, block.header.inner.approval_sigs.len(), block.transactions.len()); // Check if we have already processed this block previously. self.check_known(&block)?; // Delay hitting the db for current chain head until we know this block is not already known. let head = self.chain_store_update.head()?; - let is_next = block.header.prev_hash == head.last_block_hash; + let is_next = block.header.inner.prev_hash == head.last_block_hash; // First real I/O expense. self.check_header_signature(&block.header)?; diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index 3bad1327910..1f53fe7cb51 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -35,3 +35,7 @@ bencher = "0.1.5" [[bench]] name = "bls" harness = false + +[[bench]] +name = "serialization" +harness = false diff --git a/core/primitives/benches/serialization.rs b/core/primitives/benches/serialization.rs new file mode 100644 index 00000000000..4f81397dc0f --- /dev/null +++ b/core/primitives/benches/serialization.rs @@ -0,0 +1,116 @@ +#[macro_use] +extern crate bencher; + +use std::collections::HashMap; +use std::sync::Arc; + +use bencher::Bencher; +use chrono::Utc; + +use near_primitives::account::Account; +use near_primitives::block::Block; +use near_primitives::crypto::signature::{PublicKey, DEFAULT_SIGNATURE}; +use near_primitives::crypto::signer::InMemorySigner; +use near_primitives::hash::CryptoHash; +use near_primitives::serialize::{Decode, Encode}; +use near_primitives::transaction::{Action, SignedTransaction, Transaction, TransferAction}; +use near_primitives::types::MerkleHash; + +fn create_transaction() -> SignedTransaction { + let mut actions = vec![]; + for _ in 0..10 { + actions.push(Action::Transfer(TransferAction { deposit: 1_000_000_000 })); + } + SignedTransaction::new( + DEFAULT_SIGNATURE, + Transaction { + signer_id: "123213123123".to_string(), + public_key: PublicKey::empty(), + nonce: 123, + receiver_id: "1231231232131".to_string(), + actions, + }, + ) +} + +fn create_block() -> Block { + let transactions = (0..1000).map(|_| create_transaction()).collect::>(); + let genesis = Block::genesis(MerkleHash::default(), Utc::now()); + let signer = Arc::new(InMemorySigner::from_random()); + Block::produce( + &genesis.header, + 10, + MerkleHash::default(), + CryptoHash::default(), + transactions, + HashMap::default(), + vec![], + signer.clone(), + ) +} + +fn create_account() -> Account { + Account::new(vec![PublicKey::empty()], 0, CryptoHash::default(), 1_000) +} + +fn serialize_tx(bench: &mut Bencher) { + let t = create_transaction(); + bench.iter(|| { + let bytes = t.encode().unwrap(); + assert!(bytes.len() > 0); + }); +} + +fn deserialize_tx(bench: &mut Bencher) { + let t = create_transaction(); + let bytes = t.encode().unwrap(); + bench.iter(|| { + let nt = SignedTransaction::decode(&bytes).unwrap(); + // assert_eq!(nt, t); + }); +} + +fn serialize_block(bench: &mut Bencher) { + let b = create_block(); + bench.iter(|| { + let bytes = b.encode().unwrap(); + assert!(bytes.len() > 0); + }); +} + +fn deserialize_block(bench: &mut Bencher) { + let b = create_block(); + let bytes = b.encode().unwrap(); + bench.iter(|| { + let nb = Block::decode(&bytes).unwrap(); + // assert_eq!(nb, b); + }); +} + +fn serialize_account(bench: &mut Bencher) { + let acc = create_account(); + bench.iter(|| { + let bytes = acc.encode().unwrap(); + assert!(bytes.len() > 0); + }); +} + +fn deserialize_account(bench: &mut Bencher) { + let acc = create_account(); + let bytes = acc.encode().unwrap(); + bench.iter(|| { + let nacc = Account::decode(&bytes).unwrap(); + assert_eq!(nacc, acc); + }); +} + +benchmark_group!( + benches, + serialize_tx, + serialize_block, + deserialize_tx, + deserialize_block, + serialize_account, + deserialize_account +); +benchmark_main!(benches); diff --git a/core/primitives/src/account.rs b/core/primitives/src/account.rs index ffefc1e3965..d44155a6267 100644 --- a/core/primitives/src/account.rs +++ b/core/primitives/src/account.rs @@ -3,6 +3,7 @@ use std::fmt; use crate::crypto::signature::PublicKey; use crate::hash::CryptoHash; use crate::logging; +use crate::serialize::{Encode, EncodeResult}; use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage}; /// Per account information stored in the state. diff --git a/core/primitives/src/block.rs b/core/primitives/src/block.rs index 973a96e7b5b..45565afd520 100644 --- a/core/primitives/src/block.rs +++ b/core/primitives/src/block.rs @@ -7,14 +7,18 @@ use chrono::serde::ts_nanoseconds; use crate::crypto::signature::{verify, PublicKey, Signature, DEFAULT_SIGNATURE}; use crate::crypto::signer::EDSigner; use crate::hash::{hash, CryptoHash}; -use crate::serialize::vec_base_format; +use crate::serialize::{vec_base_format, Encode}; use crate::transaction::SignedTransaction; use crate::types::{BlockIndex, MerkleHash, ValidatorStake}; +use serde::{Deserialize, Deserializer}; #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] -pub struct BlockHeader { +pub struct BlockHeaderInner { /// Height of this block since the genesis block (height 0). pub height: BlockIndex, + /// Epoch start hash of the previous epoch. + /// Used for retrieving validator information + pub epoch_hash: CryptoHash, /// Hash of the block previous to this in the chain. pub prev_hash: CryptoHash, /// Root hash of the state at the previous block. @@ -27,26 +31,17 @@ pub struct BlockHeader { /// Approval mask, given current block producers. pub approval_mask: Vec, /// Approval signatures. - #[serde(with = "vec_base_format")] pub approval_sigs: Vec, /// Total weight. pub total_weight: Weight, /// Validator proposals. - pub validator_proposal: Vec, - /// Epoch start hash of the previous epoch. - /// Used for retrieving validator information - pub epoch_hash: CryptoHash, - - /// Signature of the block producer. - pub signature: Signature, - - /// Cached value of hash for this block. - hash: CryptoHash, + pub validator_proposals: Vec, } -impl BlockHeader { - fn header_body( +impl BlockHeaderInner { + pub fn new( height: BlockIndex, + epoch_hash: CryptoHash, prev_hash: CryptoHash, prev_state_root: MerkleHash, tx_root: MerkleHash, @@ -54,32 +49,37 @@ impl BlockHeader { approval_mask: Vec, approval_sigs: Vec, total_weight: Weight, - mut validator_proposal: Vec, - epoch_hash: CryptoHash, - ) -> BlockHeader { - //chain_proto::BlockHeaderBody { - BlockHeader { + validator_proposals: Vec, + ) -> Self { + BlockHeaderInner { height, - prev_hash: prev_hash.into(), - prev_state_root: prev_state_root.into(), - tx_root: tx_root.into(), - timestamp, //: timestamp.timestamp_nanos() as u64, + epoch_hash, + prev_hash, + prev_state_root, + tx_root, + timestamp, approval_mask, approval_sigs, - // approval_sigs: RepeatedField::from_iter( - // approval_sigs.iter().map(std::convert::Into::into), - // ), - total_weight, //: total_weight.to_num(), - validator_proposal, - // validator_proposal: RepeatedField::from_iter( - // validator_proposal.drain(..).map(std::convert::Into::into), - // ), - epoch_hash: epoch_hash.into(), - hash: CryptoHash::default(), - signature: DEFAULT_SIGNATURE, + total_weight, + validator_proposals, } } +} + +#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] +pub struct BlockHeader { + /// Inner part of the block header that gets hashed. + pub inner: BlockHeaderInner, + + /// Signature of the block producer. + pub signature: Signature, + + /// Cached value of hash for this block. + #[serde(skip)] + hash: CryptoHash, +} +impl BlockHeader { pub fn new( height: BlockIndex, prev_hash: CryptoHash, @@ -93,8 +93,9 @@ impl BlockHeader { epoch_hash: CryptoHash, signer: Arc, ) -> Self { - Self::header_body( + let inner = BlockHeaderInner::new( height, + epoch_hash, prev_hash, prev_state_root, tx_root, @@ -103,22 +104,17 @@ impl BlockHeader { approval_sigs, total_weight, validator_proposal, - epoch_hash, - ) - // let bytes = hb.write_to_bytes().expect("Failed to serialize"); - // let hash = hash(&bytes); - // let h = chain_proto::BlockHeader { - // body: SingularPtrField::some(hb), - // signature: signer.sign(hash.as_ref()).into(), - // ..Default::default() - // }; - // h.try_into().expect("Failed to parse just created header") + ); + let bytes = inner.encode().expect("Failed to serialze block header"); + let hash = hash(&bytes); + Self { inner, signature: signer.sign(hash.as_ref()).into(), hash } } pub fn genesis(state_root: MerkleHash, timestamp: DateTime) -> Self { - Self::header_body( + let inner = BlockHeaderInner::new( 0, CryptoHash::default(), + CryptoHash::default(), state_root, MerkleHash::default(), timestamp, @@ -126,15 +122,10 @@ impl BlockHeader { vec![], 0.into(), vec![], - CryptoHash::default(), - ) - // chain_proto::BlockHeader { - // body: SingularPtrField::some(header_body), - // signature: DEFAULT_SIGNATURE.into(), - // ..Default::default() - // } - // .try_into() - // .expect("Failed to parse just created header") + ); + let bytes = inner.encode().expect("Failed to serialze block header"); + let hash = hash(&bytes); + Self { inner, signature: DEFAULT_SIGNATURE.into(), hash } } pub fn hash(&self) -> CryptoHash { @@ -147,6 +138,17 @@ impl BlockHeader { } } +//impl<'de> Deserialize<'de> for BlockHeader { +// fn deserialize(deserializer: D) -> Result>::Error> +// where +// D: Deserializer<'de>, +// { +// let mut header = BlockHeader::deserialize(deserializer)?; +// header.hash = hash(&header.inner.encode().expect("Failed serializing header")); +// Ok(header) +// } +//} + #[derive(Serialize, Deserialize, Debug)] pub struct Bytes(Vec); @@ -183,7 +185,8 @@ impl Block { } else { (vec![], vec![]) }; - let total_weight = (prev.total_weight.to_num() + (approval_sigs.len() as u64) + 1).into(); + let total_weight = + (prev.inner.total_weight.to_num() + (approval_sigs.len() as u64) + 1).into(); Block { header: BlockHeader::new( height, @@ -210,9 +213,9 @@ impl Block { pub fn empty(prev: &BlockHeader, signer: Arc) -> Self { Block::produce( prev, - prev.height + 1, - prev.prev_state_root, - prev.epoch_hash, + prev.inner.height + 1, + prev.inner.prev_state_root, + prev.inner.epoch_hash, vec![], HashMap::default(), vec![], diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index acee25906ad..ae7f9551ea7 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -5,10 +5,11 @@ use chrono::{DateTime, Utc}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::account::{AccessKey, Account}; +use crate::block::BlockHeader; use crate::crypto::signature::{PublicKey, SecretKey, Signature}; use crate::hash::CryptoHash; use crate::serialize::{from_base, to_base, u128_dec_format, vec_base_format}; -use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage, Version}; +use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage, ValidatorStake, Version}; /// Number of nano seconds in one second. //const NS_IN_SECOND: u64 = 1_000_000_000; @@ -282,3 +283,40 @@ impl TryFrom for Option { } } } + +struct BlockHeaderView { + pub height: BlockIndex, + pub epoch_hash: CryptoHash, + pub prev_hash: CryptoHashView, + pub prev_state_root: CryptoHashView, + pub tx_root: CryptoHashView, + pub timestamp: u64, + pub approval_mask: Vec, + pub approval_sigs: Vec, + pub total_weight: u64, + pub validator_proposals: Vec, + pub signature: Signature, +} + +impl From for BlockHeaderView { + fn from(mut header: BlockHeader) -> Self { + Self { + height: header.inner.height, + epoch_hash: header.inner.epoch_hash, + prev_hash: header.inner.prev_hash.into(), + prev_state_root: header.inner.prev_state_root.into(), + tx_root: header.inner.tx_root.into(), + timestamp: header.inner.timestamp.timestamp() as u64, + approval_mask: header.inner.approval_mask, + approval_sigs: header + .inner + .approval_sigs + .drain(..) + .map(|signature| signature.into()) + .collect(), + total_weight: header.inner.total_weight.to_num(), + validator_proposals: header.inner.validator_proposals, + signature: header.signature.into(), + } + } +} diff --git a/core/primitives/src/serialize.rs b/core/primitives/src/serialize.rs index c996a480346..5452986e9fc 100644 --- a/core/primitives/src/serialize.rs +++ b/core/primitives/src/serialize.rs @@ -1,11 +1,101 @@ use std::convert::TryFrom; use std::io; +use byteorder::{LittleEndian, ReadBytesExt}; use serde::{de::DeserializeOwned, Serialize}; +use std::io::Cursor; pub type EncodeResult = Result, io::Error>; pub type DecodeResult = Result; +pub type WritableResult = Result<(), std::io::Error>; +pub type ReadableResult = Result; + +pub trait Writable { + fn write(&self) -> Result, std::io::Error> { + let mut out = vec![]; + self.write_into(&mut out)?; + Ok(out) + } + fn write_into(&self, out: &mut Vec) -> WritableResult; +} + +macro_rules! impl_writable_for_uint { + ($type: ident) => { + impl Writable for $type { + fn write_into(&self, out: &mut Vec) -> WritableResult { + out.extend_from_slice(&self.to_le_bytes()); + Ok(()) + } + } + }; +} + +impl_writable_for_uint!(u8); +impl_writable_for_uint!(u16); +impl_writable_for_uint!(u32); +impl_writable_for_uint!(u64); +impl_writable_for_uint!(u128); + +impl Writable for Vec +where + T: Writable, +{ + fn write_into(&self, out: &mut Vec) -> WritableResult { + (self.len() as u32).write_into(out)?; + for item in self.iter() { + item.write_into(out)?; + } + Ok(()) + } +} + +pub trait Readable: Sized { + fn read(bytes: &[u8]) -> ReadableResult { + let mut cursor = Cursor::new(bytes); + Self::read_from_cursor(&mut cursor) + } + + fn read_from_cursor(cursor: &mut Cursor<&[u8]>) -> ReadableResult; +} + +macro_rules! impl_readable_for_primitive { + ($type: ident, $func: ident) => { + impl Readable for $type { + fn read_from_cursor(cursor: &mut Cursor<&[u8]>) -> ReadableResult { + cursor.$func() + } + } + }; + ($type: ident, $func: ident, $qual: ident) => { + impl Readable for $type { + fn read_from_cursor(cursor: &mut Cursor<&[u8]>) -> ReadableResult { + cursor.$func::<$qual>() + } + } + }; +} + +impl_readable_for_primitive!(u8, read_u8); +impl_readable_for_primitive!(u16, read_u16, LittleEndian); +impl_readable_for_primitive!(u32, read_u32, LittleEndian); +impl_readable_for_primitive!(u64, read_u64, LittleEndian); +impl_readable_for_primitive!(u128, read_u128, LittleEndian); + +impl Readable for Vec +where + T: Readable, +{ + fn read_from_cursor(mut cursor: &mut Cursor<&[u8]>) -> ReadableResult { + let len = u32::read_from_cursor(&mut cursor)?; + let mut result = vec![]; + for _ in 0..len { + result.push(T::read_from_cursor(&mut cursor)?); + } + Ok(result) + } +} + // encode a type to byte array pub trait Encode { fn encode(&self) -> EncodeResult; @@ -233,12 +323,47 @@ pub mod u128_dec_format { mod tests { use super::*; + #[derive(Debug, Eq, PartialEq)] + struct SomeStruct { + a: u8, + b: u32, + c: u128, + x: Vec, + } + #[derive(Deserialize, Serialize)] struct OptionBytesStruct { #[serde(with = "option_bytes_format")] data: Option>, } + impl Writable for SomeStruct { + fn write_into(&self, out: &mut Vec) -> WritableResult { + self.a.write_into(out)?; + self.b.write_into(out)?; + self.c.write_into(out)?; + self.x.write_into(out) + } + } + + impl Readable for SomeStruct { + fn read_from_cursor(mut cursor: &mut Cursor<&[u8]>) -> ReadableResult { + let a = u8::read_from_cursor(&mut cursor)?; + let b = u32::read_from_cursor(&mut cursor)?; + let c = u128::read_from_cursor(&mut cursor)?; + let x = >::read_from_cursor(&mut cursor)?; + Ok(Self { a, b, c, x }) + } + } + + #[test] + fn test_serialize_struct() { + let s = SomeStruct { a: 1, b: 2, c: 3, x: vec![1, 2, 3] }; + let bytes = s.write().unwrap(); + let ns = SomeStruct::read(&bytes).unwrap(); + assert_eq!(s, ns); + } + #[test] fn test_serialize_some() { let s = OptionBytesStruct { data: Some(vec![10, 20, 30]) }; diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index 7c3283ca5b9..9f2e62a29e6 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -8,8 +8,9 @@ use crate::crypto::signature::{verify, PublicKey, Signature}; use crate::crypto::signer::EDSigner; use crate::hash::{hash, CryptoHash}; use crate::logging; -use crate::serialize::{base_bytes_format, u128_dec_format}; +use crate::serialize::{base_bytes_format, u128_dec_format, Decode, DecodeResult, Encode}; use crate::types::{AccountId, Balance, Gas, Nonce, ShardId, StructSignature}; +use serde::{Deserialize, Deserializer}; use std::sync::Arc; pub type LogEntry = String; @@ -132,6 +133,7 @@ pub struct DeleteAccountAction { pub struct SignedTransaction { pub signature: StructSignature, pub transaction: Transaction, + #[serde(skip)] hash: CryptoHash, } @@ -319,6 +321,7 @@ mod tests { use crate::crypto::signature::{get_key_pair, sign, DEFAULT_SIGNATURE}; use super::*; + use crate::serialize::{Decode, Encode}; #[test] fn test_verify_transaction() { @@ -340,5 +343,9 @@ mod tests { let invalid_keys = vec![wrong_public_key]; assert!(!verify_transaction_signature(&transaction, &invalid_keys)); + + let bytes = transaction.encode().unwrap(); + let new_tx = SignedTransaction::decode(&bytes).unwrap().into(); + assert!(verify_transaction_signature(&new_tx, &valid_keys)); } } From f98b96396d79ecc1027191c830e39c0b124eb3ba Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Wed, 14 Aug 2019 17:05:07 -0700 Subject: [PATCH 06/25] Step 5. Finish BlockHeader -> BlockHeaderInner. Fixups for View usages in tests --- chain/chain/src/chain.rs | 50 ++--- chain/chain/src/store.rs | 6 +- chain/chain/src/test_utils.rs | 6 +- chain/chain/src/types.rs | 12 +- chain/chain/tests/simple_chain.rs | 6 +- chain/client/src/client.rs | 57 +++--- chain/client/src/sync.rs | 4 +- chain/client/tests/process_blocks.rs | 20 +- chain/jsonrpc/tests/rpc_query.rs | 2 +- chain/network/src/peer.rs | 8 +- core/primitives/src/rpc.rs | 2 +- near/src/runtime.rs | 4 +- near/tests/run_nodes.rs | 18 +- near/tests/sync_nodes.rs | 13 +- near/tests/sync_state_nodes.rs | 2 +- test-utils/state-viewer/src/main.rs | 2 +- test-utils/testlib/src/standard_test_cases.rs | 175 ++++-------------- test-utils/testlib/src/user/mod.rs | 4 +- test-utils/testlib/src/user/rpc_user.rs | 8 +- test-utils/testlib/src/user/runtime_user.rs | 6 +- 20 files changed, 161 insertions(+), 244 deletions(-) diff --git a/chain/chain/src/chain.rs b/chain/chain/src/chain.rs index 27d06df9c27..d13c5a1edf7 100644 --- a/chain/chain/src/chain.rs +++ b/chain/chain/src/chain.rs @@ -676,7 +676,7 @@ impl<'a> ChainUpdate<'a> { // Check that state root we computed from previous block matches recorded in this block. let state_root = self.chain_store_update.get_post_state_root(&prev_hash)?; - if &block.header.prev_state_root != state_root { + if &block.header.inner.prev_state_root != state_root { return Err(ErrorKind::InvalidStateRoot.into()); } @@ -688,9 +688,9 @@ impl<'a> ChainUpdate<'a> { .runtime_adapter .apply_transactions( 0, - &block.header.prev_state_root, - block.header.height, - &block.header.prev_hash, + &block.header.inner.prev_state_root, + block.header.inner.height, + &block.header.inner.prev_hash, &block.header.hash(), &vec![receipts.clone()], // TODO: currently only taking into account one shard. &block.transactions, @@ -705,9 +705,9 @@ impl<'a> ChainUpdate<'a> { // If block checks out, record validator proposals for given block. self.runtime_adapter .add_validator_proposals( - block.header.prev_hash, + block.header.inner.prev_hash, block.hash(), - block.header.height, + block.header.inner.height, validator_proposals, vec![], vec![], @@ -749,10 +749,10 @@ impl<'a> ChainUpdate<'a> { /// Process incoming block headers for syncing. fn sync_block_headers(&mut self, mut headers: Vec) -> Result, Error> { // Sort headers by heights if they are out of order. - headers.sort_by(|left, right| left.height.cmp(&right.height)); + headers.sort_by(|left, right| left.inner.height.cmp(&right.inner.height)); let _first_header = if let Some(header) = headers.first() { - debug!(target: "chain", "Sync block headers: {} headers from {} at {}", headers.len(), header.hash(), header.height); + debug!(target: "chain", "Sync block headers: {} headers from {} at {}", headers.len(), header.hash(), header.inner.height); header } else { return Ok(None); @@ -773,10 +773,10 @@ impl<'a> ChainUpdate<'a> { // Add validator proposals for given header. self.runtime_adapter .add_validator_proposals( - header.prev_hash, + header.inner.prev_hash, header.hash(), - header.height, - header.validator_proposal.clone(), + header.inner.height, + header.inner.validator_proposals.clone(), vec![], vec![], ) @@ -797,10 +797,10 @@ impl<'a> ChainUpdate<'a> { fn check_header_signature(&self, header: &BlockHeader) -> Result<(), Error> { let validator = self .runtime_adapter - .get_block_proposer(&header.epoch_hash, header.height) + .get_block_proposer(&header.inner.epoch_hash, header.inner.height) .map_err(|e| Error::from(ErrorKind::Other(e.to_string())))?; if self.runtime_adapter.check_validator_signature( - &header.epoch_hash, + &header.inner.epoch_hash, &validator, header.hash().as_ref(), &header.signature, @@ -817,8 +817,8 @@ impl<'a> ChainUpdate<'a> { provenance: &Provenance, ) -> Result<(), Error> { // Refuse blocks from the too distant future. - if header.timestamp > Utc::now() + Duration::seconds(ACCEPTABLE_TIME_DIFFERENCE) { - return Err(ErrorKind::InvalidBlockFutureTime(header.timestamp).into()); + if header.inner.timestamp > Utc::now() + Duration::seconds(ACCEPTABLE_TIME_DIFFERENCE) { + return Err(ErrorKind::InvalidBlockFutureTime(header.inner.timestamp).into()); } // First I/O cost, delay as much as possible. @@ -828,17 +828,19 @@ impl<'a> ChainUpdate<'a> { // Prevent time warp attacks and some timestamp manipulations by forcing strict // time progression. - if header.timestamp <= prev_header.timestamp { - return Err( - ErrorKind::InvalidBlockPastTime(prev_header.timestamp, header.timestamp).into() - ); + if header.inner.timestamp <= prev_header.inner.timestamp { + return Err(ErrorKind::InvalidBlockPastTime( + prev_header.inner.timestamp, + header.inner.timestamp, + ) + .into()); } // If this is not the block we produced (hence trust in it) - validates block // producer, confirmation signatures and returns new total weight. if *provenance != Provenance::PRODUCED { let prev_header = self.get_previous_header(header)?.clone(); let weight = self.runtime_adapter.compute_block_weight(&prev_header, header)?; - if weight != header.total_weight { + if weight != header.inner.total_weight { return Err(ErrorKind::InvalidBlockWeight.into()); } } @@ -849,7 +851,7 @@ impl<'a> ChainUpdate<'a> { /// Update the header head if this header has most work. fn update_header_head(&mut self, header: &BlockHeader) -> Result, Error> { let header_head = self.chain_store_update.header_head()?; - if header.total_weight > header_head.total_weight { + if header.inner.total_weight > header_head.total_weight { let tip = Tip::from_header(header); self.chain_store_update.save_header_head(&tip)?; debug!(target: "chain", "Header head updated to {} at {}", tip.last_block_hash, tip.height); @@ -867,7 +869,7 @@ impl<'a> ChainUpdate<'a> { // if we made a fork with more work than the head (which should also be true // when extending the head), update it let head = self.chain_store_update.head()?; - if block.header.total_weight > head.total_weight { + if block.header.inner.total_weight > head.total_weight { let tip = Tip::from_header(&block.header); self.chain_store_update.save_body_head(&tip); @@ -917,12 +919,12 @@ impl<'a> ChainUpdate<'a> { Ok(()) } - /// Check if this block is ini the store already. + /// Check if this block is in the store already. fn check_known_store(&self, header: &BlockHeader) -> Result<(), Error> { match self.chain_store_update.block_exists(&header.hash()) { Ok(true) => { let head = self.chain_store_update.head()?; - if header.height > 50 && header.height < head.height - 50 { + if header.inner.height > 50 && header.inner.height < head.height - 50 { // We flag this as an "abusive peer" but only in the case // where we have the full block in our store. // So this is not a particularly exhaustive check. diff --git a/chain/chain/src/store.rs b/chain/chain/src/store.rs index 53bcc094fc3..90a40159cc1 100644 --- a/chain/chain/src/store.rs +++ b/chain/chain/src/store.rs @@ -154,7 +154,7 @@ impl ChainStoreAccess for ChainStore { /// Get previous header. fn get_previous_header(&mut self, header: &BlockHeader) -> Result<&BlockHeader, Error> { - self.get_block_header(&header.prev_hash) + self.get_block_header(&header.inner.prev_hash) } /// Get state root hash after applying header with given hash. @@ -330,7 +330,7 @@ impl<'a, T: ChainStoreAccess> ChainStoreAccess for ChainStoreUpdate<'a, T> { /// Get previous header. fn get_previous_header(&mut self, header: &BlockHeader) -> Result<&BlockHeader, Error> { - self.get_block_header(&header.prev_hash) + self.get_block_header(&header.inner.prev_hash) } /// Get state root hash after applying header with given hash. @@ -404,7 +404,7 @@ impl<'a, T: ChainStoreAccess> ChainStoreUpdate<'a, T> { loop { let header = self.get_block_header(&prev_hash)?; let (header_height, header_hash, header_prev_hash) = - (header.height, header.hash(), header.prev_hash); + (header.inner.height, header.hash(), header.inner.prev_hash); // Clean up block indicies between blocks. for height in (header_height + 1)..prev_height { self.block_index.insert(height, None); diff --git a/chain/chain/src/test_utils.rs b/chain/chain/src/test_utils.rs index 57a8bfe770b..521a84840e2 100644 --- a/chain/chain/src/test_utils.rs +++ b/chain/chain/src/test_utils.rs @@ -66,11 +66,11 @@ impl RuntimeAdapter for KeyValueRuntime { prev_header: &BlockHeader, header: &BlockHeader, ) -> Result { - let validator = &self.validators[(header.height as usize) % self.validators.len()]; + let validator = &self.validators[(header.inner.height as usize) % self.validators.len()]; if !header.verify_block_producer(&validator.public_key) { return Err(ErrorKind::InvalidBlockProposer.into()); } - Ok(prev_header.total_weight.next(header.approval_sigs.len() as u64)) + Ok(prev_header.inner.total_weight.next(header.inner.approval_sigs.len() as u64)) } fn get_epoch_block_proposers( @@ -191,7 +191,7 @@ impl RuntimeAdapter for KeyValueRuntime { &self, _state_root: MerkleHash, _height: BlockIndex, - path: &str, + _path: &str, _data: &[u8], ) -> Result> { Ok(QueryResponse::ViewAccount(Account::new(vec![], 0, CryptoHash::default(), 0).into())) diff --git a/chain/chain/src/types.rs b/chain/chain/src/types.rs index 0a1cb3d5ca0..419390d3a66 100644 --- a/chain/chain/src/types.rs +++ b/chain/chain/src/types.rs @@ -184,11 +184,11 @@ impl Tip { /// Creates a new tip based on provided header. pub fn from_header(header: &BlockHeader) -> Tip { Tip { - height: header.height, + height: header.inner.height, last_block_hash: header.hash(), - prev_block_hash: header.prev_hash, - total_weight: header.total_weight, - epoch_hash: header.epoch_hash, + prev_block_hash: header.inner.prev_hash, + total_weight: header.inner.total_weight, + epoch_hash: header.inner.epoch_hash, } } } @@ -233,7 +233,7 @@ mod tests { signer.clone(), ); assert!(signer.verify(b1.hash().as_ref(), &b1.header.signature)); - assert_eq!(b1.header.total_weight.to_num(), 1); + assert_eq!(b1.header.inner.total_weight.to_num(), 1); let other_signer = Arc::new(InMemorySigner::from_seed("other2", "other2")); let approvals: HashMap = vec![(1, other_signer.sign(b1.hash().as_ref()))].into_iter().collect(); @@ -248,6 +248,6 @@ mod tests { signer.clone(), ); assert!(signer.verify(b2.hash().as_ref(), &b2.header.signature)); - assert_eq!(b2.header.total_weight.to_num(), 3); + assert_eq!(b2.header.inner.total_weight.to_num(), 3); } } diff --git a/chain/chain/tests/simple_chain.rs b/chain/chain/tests/simple_chain.rs index 9c02ce07f0c..5850727d5a2 100644 --- a/chain/chain/tests/simple_chain.rs +++ b/chain/chain/tests/simple_chain.rs @@ -38,8 +38,8 @@ fn build_chain_with_orhpans() { let block = Block::produce( &blocks[blocks.len() - 1].header, 10, - blocks[blocks.len() - 1].header.prev_state_root, - blocks[blocks.len() - 1].header.epoch_hash, + blocks[blocks.len() - 1].header.inner.prev_state_root, + blocks[blocks.len() - 1].header.inner.epoch_hash, vec![], HashMap::default(), vec![], @@ -107,5 +107,5 @@ fn build_chain_with_skips_and_forks() { assert!(chain.process_block(b4, Provenance::PRODUCED, |_, _, _| {}).is_ok()); assert!(chain.process_block(b5, Provenance::PRODUCED, |_, _, _| {}).is_ok()); assert!(chain.get_header_by_height(1).is_err()); - assert_eq!(chain.get_header_by_height(5).unwrap().height, 5); + assert_eq!(chain.get_header_by_height(5).unwrap().inner.height, 5); } diff --git a/chain/client/src/client.rs b/chain/client/src/client.rs index 5d851ccac0b..cb8b9d6a359 100644 --- a/chain/client/src/client.rs +++ b/chain/client/src/client.rs @@ -327,7 +327,7 @@ impl Handler for ClientActor { let head = self.chain.head().map_err(|err| err.to_string())?; let prev_header = self.chain.get_block_header(&head.last_block_hash).map_err(|err| err.to_string())?; - let latest_block_time = prev_header.timestamp.clone(); + let latest_block_time = prev_header.inner.timestamp.clone(); let state_root = self.chain.get_post_state_root(&head.last_block_hash).map_err(|err| err.to_string())?; let validators = self @@ -394,8 +394,8 @@ impl ClientActor { self.handle_scheduling_block_production( ctx, block.hash(), - block.header.height, - block.header.height, + block.header.inner.height, + block.header.inner.height, ); } @@ -406,7 +406,7 @@ impl ClientActor { self.tx_pool.reconcile_block(&block); } - self.check_send_announce_account(&block.hash(), block.header.height); + self.check_send_announce_account(&block.hash(), block.header.inner.height); } /// Check if client Account Id should be sent and send it. @@ -432,7 +432,7 @@ impl ClientActor { }; if let Ok(epoch_block) = self.chain.get_block(&epoch_hash) { - let epoch_height = epoch_block.header.height; + let epoch_height = epoch_block.header.inner.height; if let Some(last_val_announce_height) = self.last_val_announce_height { if last_val_announce_height == epoch_height { @@ -508,10 +508,10 @@ impl ClientActor { fn get_block_approval(&mut self, block: &Block) -> Option { let (mut epoch_hash, offset) = self .runtime_adapter - .get_epoch_offset(block.header.epoch_hash, block.header.height + 1) + .get_epoch_offset(block.header.inner.epoch_hash, block.header.inner.height + 1) .ok()?; let next_block_producer_account = - self.get_block_proposer(&epoch_hash, block.header.height + 1); + self.get_block_proposer(&epoch_hash, block.header.inner.height + 1); if let (Some(block_producer), Ok(next_block_producer_account)) = (&self.block_producer, &next_block_producer_account) { @@ -520,7 +520,7 @@ impl ClientActor { if offset == 0 { epoch_hash = self .runtime_adapter - .get_epoch_offset(block.header.prev_hash, block.header.height) + .get_epoch_offset(block.header.inner.prev_hash, block.header.inner.height) .ok()? .0; } @@ -737,8 +737,8 @@ impl ClientActor { was_requested: bool, ) -> NetworkClientResponses { let hash = block.hash(); - debug!(target: "client", "Received block {} at {} from {}", hash, block.header.height, peer_id); - let prev_hash = block.header.prev_hash; + debug!(target: "client", "Received block {} at {} from {}", hash, block.header.inner.height, peer_id); + let prev_hash = block.header.inner.prev_hash; let provenance = if was_requested { near_chain::Provenance::SYNC } else { near_chain::Provenance::NONE }; match self.process_block(ctx, block, provenance) { @@ -773,7 +773,7 @@ impl ClientActor { fn receive_header(&mut self, header: BlockHeader, peer_info: PeerId) -> NetworkClientResponses { let hash = header.hash(); - debug!(target: "client", "Received block header {} at {} from {}", hash, header.height, peer_info); + debug!(target: "client", "Received block header {} at {} from {}", hash, header.inner.height, peer_info); // Process block by chain, if it's valid header ask for the block. let result = self.chain.process_block_header(&header); @@ -844,7 +844,7 @@ impl ClientActor { let mut headers = vec![]; let max_height = self.chain.header_head()?.height; // TODO: this may be inefficient if there are a lot of skipped blocks. - for h in header.height + 1..=max_height { + for h in header.inner.height + 1..=max_height { if let Ok(header) = self.chain.get_header_by_height(h) { headers.push(header.clone()); if headers.len() >= sync::MAX_BLOCK_HEADERS as usize { @@ -1070,27 +1070,28 @@ impl ClientActor { // TODO: Access runtime adapter only once to find the position and public key. // If given account is not current block proposer. - let position = match self.get_epoch_block_proposers(&header.epoch_hash, &header.hash()) { - Ok(validators) => { - let position = validators.iter().position(|x| &(x.0) == account_id); - if let Some(idx) = position { - if !validators[idx].1 { - idx + let position = + match self.get_epoch_block_proposers(&header.inner.epoch_hash, &header.hash()) { + Ok(validators) => { + let position = validators.iter().position(|x| &(x.0) == account_id); + if let Some(idx) = position { + if !validators[idx].1 { + idx + } else { + return false; + } } else { return false; } - } else { + } + Err(err) => { + error!(target: "client", "Block approval error: {}", err); return false; } - } - Err(err) => { - error!(target: "client", "Block approval error: {}", err); - return false; - } - }; + }; // Check signature is correct for given validator. if !self.runtime_adapter.check_validator_signature( - &header.epoch_hash, + &header.inner.epoch_hash, account_id, hash.as_ref(), signature, @@ -1108,10 +1109,10 @@ impl ClientActor { hash: CryptoHash, ) -> Result<(Vec, Vec), near_chain::Error> { let header = self.chain.get_block_header(&hash)?; - let prev_hash = header.prev_hash; + let prev_hash = header.inner.prev_hash; let payload = self .runtime_adapter - .dump_state(shard_id, header.prev_state_root) + .dump_state(shard_id, header.inner.prev_state_root) .map_err(|err| ErrorKind::Other(err.to_string()))?; let receipts = self.chain.get_receipts(&prev_hash)?.clone(); Ok((payload, receipts)) diff --git a/chain/client/src/sync.rs b/chain/client/src/sync.rs index 91ac2f9e4cb..9f79c325737 100644 --- a/chain/client/src/sync.rs +++ b/chain/client/src/sync.rs @@ -417,7 +417,7 @@ impl StateSync { let header_head = chain.header_head()?; let mut sync_hash = header_head.prev_block_hash; for _ in 0..self.state_fetch_horizon { - sync_hash = chain.get_block_header(&sync_hash)?.prev_hash; + sync_hash = chain.get_block_header(&sync_hash)?.inner.prev_hash; } Ok(sync_hash) } @@ -465,7 +465,7 @@ impl StateSync { // Get header we were syncing into. let header = chain.get_block_header(&sync_hash)?; - let hash = header.prev_hash; + let hash = header.inner.prev_hash; let prev_header = chain.get_block_header(&hash)?; let tip = Tip::from_header(prev_header); // Update related heads now. diff --git a/chain/client/tests/process_blocks.rs b/chain/client/tests/process_blocks.rs index a7d31c99552..89fecec4dcc 100644 --- a/chain/client/tests/process_blocks.rs +++ b/chain/client/tests/process_blocks.rs @@ -99,7 +99,7 @@ fn receive_network_block() { let signer = Arc::new(InMemorySigner::from_seed("test1", "test1")); let block = Block::produce( &last_block.header, - last_block.header.height + 1, + last_block.header.inner.height + 1, MerkleHash::default(), CryptoHash::default(), vec![], @@ -148,7 +148,7 @@ fn receive_network_block_header() { let signer = Arc::new(InMemorySigner::from_seed("test", "test")); let block = Block::produce( &last_block.header, - last_block.header.height + 1, + last_block.header.inner.height + 1, MerkleHash::default(), CryptoHash::default(), vec![], @@ -178,7 +178,7 @@ fn produce_block_with_approvals() { true, Box::new(move |msg, _ctx, _| { if let NetworkRequests::Block { block } = msg { - assert!(block.header.approval_sigs.len() > 0); + assert!(block.header.inner.approval_sigs.len() > 0); System::current().stop(); } NetworkResponses::NoResponse @@ -190,7 +190,7 @@ fn produce_block_with_approvals() { let signer3 = Arc::new(InMemorySigner::from_seed("test3", "test3")); let block = Block::produce( &last_block.header, - last_block.header.height + 1, + last_block.header.inner.height + 1, MerkleHash::default(), CryptoHash::default(), vec![], @@ -223,8 +223,8 @@ fn invalid_blocks() { Box::new(move |msg, _ctx, _client_actor| { match msg { NetworkRequests::BlockHeaderAnnounce { header, approval } => { - assert_eq!(header.height, 1); - assert_eq!(header.prev_state_root, MerkleHash::default()); + assert_eq!(header.inner.height, 1); + assert_eq!(header.inner.prev_state_root, MerkleHash::default()); assert_eq!(*approval, None); System::current().stop(); } @@ -239,7 +239,7 @@ fn invalid_blocks() { // Send invalid state root. let block = Block::produce( &last_block.header, - last_block.header.height + 1, + last_block.header.inner.height + 1, hash(&[0]), CryptoHash::default(), vec![], @@ -255,7 +255,7 @@ fn invalid_blocks() { // Send block that builds on invalid one. let block2 = Block::produce( &block.header, - block.header.height + 1, + block.header.inner.height + 1, hash(&[1]), CryptoHash::default(), vec![], @@ -267,7 +267,7 @@ fn invalid_blocks() { // Send proper block. let block3 = Block::produce( &last_block.header, - last_block.header.height + 1, + last_block.header.inner.height + 1, MerkleHash::default(), CryptoHash::default(), vec![], @@ -296,7 +296,7 @@ fn skip_block_production() { Box::new(move |msg, _ctx, _client_actor| { match msg { NetworkRequests::Block { block } => { - if block.header.height > 3 { + if block.header.inner.height > 3 { System::current().stop(); } } diff --git a/chain/jsonrpc/tests/rpc_query.rs b/chain/jsonrpc/tests/rpc_query.rs index 4a533539102..acac7ae3cfb 100644 --- a/chain/jsonrpc/tests/rpc_query.rs +++ b/chain/jsonrpc/tests/rpc_query.rs @@ -16,7 +16,7 @@ fn test_block() { let mut client = new_client(&format!("http://{}", addr)); actix::spawn(client.block(0).then(|res| { - assert_eq!(res.unwrap().header.height, 0); + assert_eq!(res.unwrap().header.inner.height, 0); System::current().stop(); future::result(Ok(())) })); diff --git a/chain/network/src/peer.rs b/chain/network/src/peer.rs index a95fc336e7e..40de1566119 100644 --- a/chain/network/src/peer.rs +++ b/chain/network/src/peer.rs @@ -223,17 +223,17 @@ impl Peer { PeerMessage::Block(block) => { let block_hash = block.hash(); self.tracker.push_received(block_hash); - self.chain_info.height = max(self.chain_info.height, block.header.height); + self.chain_info.height = max(self.chain_info.height, block.header.inner.height); self.chain_info.total_weight = - max(self.chain_info.total_weight, block.header.total_weight); + max(self.chain_info.total_weight, block.header.inner.total_weight); NetworkClientMessages::Block(block, peer_id, self.tracker.has_request(block_hash)) } PeerMessage::BlockHeaderAnnounce(header) => { let block_hash = header.hash(); self.tracker.push_received(block_hash); - self.chain_info.height = max(self.chain_info.height, header.height); + self.chain_info.height = max(self.chain_info.height, header.inner.height); self.chain_info.total_weight = - max(self.chain_info.total_weight, header.total_weight); + max(self.chain_info.total_weight, header.inner.total_weight); NetworkClientMessages::BlockHeader(header, peer_id) } PeerMessage::Transaction(transaction) => { diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index ae7f9551ea7..e8d94c661b0 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -14,7 +14,7 @@ use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage, Validato /// Number of nano seconds in one second. //const NS_IN_SECOND: u64 = 1_000_000_000; -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Clone)] pub struct PublicKeyView(Vec); impl Serialize for PublicKeyView { diff --git a/near/src/runtime.rs b/near/src/runtime.rs index 2887380b60d..df608e911e0 100644 --- a/near/src/runtime.rs +++ b/near/src/runtime.rs @@ -124,12 +124,12 @@ impl RuntimeAdapter for NightshadeRuntime { ) -> Result { let mut vm = self.validator_manager.write().expect(POISONED_LOCK_ERR); let validator = vm - .get_block_proposer_info(header.epoch_hash, header.height) + .get_block_proposer_info(header.inner.epoch_hash, header.inner.height) .map_err(|err| ErrorKind::Other(err.to_string()))?; if !header.verify_block_producer(&validator.public_key) { return Err(ErrorKind::InvalidBlockProposer.into()); } - Ok(prev_header.total_weight.next(header.approval_sigs.len() as u64)) + Ok(prev_header.inner.total_weight.next(header.inner.approval_sigs.len() as u64)) } fn get_epoch_block_proposers( diff --git a/near/tests/run_nodes.rs b/near/tests/run_nodes.rs index 4404f99a5a2..9ca30094158 100644 --- a/near/tests/run_nodes.rs +++ b/near/tests/run_nodes.rs @@ -4,7 +4,7 @@ use tempdir::TempDir; use near::{load_test_config, start_with_config, GenesisConfig}; use near_client::GetBlock; -use near_network::test_utils::{convert_boot_nodes, WaitOrTimeout, open_port}; +use near_network::test_utils::{convert_boot_nodes, open_port, WaitOrTimeout}; use near_primitives::test_utils::init_test_logger; fn run_nodes(num_nodes: usize) { @@ -16,10 +16,15 @@ fn run_nodes(num_nodes: usize) { let mut near_configs = vec![]; let first_node = open_port(); for i in 0..num_nodes { - let mut near_config = load_test_config(&validators[i], if i == 0 { first_node } else { open_port() }, &genesis_config); + let mut near_config = load_test_config( + &validators[i], + if i == 0 { first_node } else { open_port() }, + &genesis_config, + ); near_config.client_config.min_num_peers = num_nodes - 1; if i > 0 { - near_config.network_config.boot_nodes = convert_boot_nodes(vec![(&validators[0], first_node)]); + near_config.network_config.boot_nodes = + convert_boot_nodes(vec![(&validators[0], first_node)]); } near_configs.push(near_config); } @@ -38,7 +43,10 @@ fn run_nodes(num_nodes: usize) { Box::new(move |_ctx| { actix::spawn(view_client.send(GetBlock::Best).then(|res| { match &res { - Ok(Ok(b)) if b.header.height > 4 && b.header.total_weight.to_num() > 6 => { + Ok(Ok(b)) + if b.header.inner.height > 4 + && b.header.inner.total_weight.to_num() > 6 => + { System::current().stop() } Err(_) => return futures::future::err(()), @@ -50,7 +58,7 @@ fn run_nodes(num_nodes: usize) { 100, 60000, ) - .start(); + .start(); system.run().unwrap(); } diff --git a/near/tests/sync_nodes.rs b/near/tests/sync_nodes.rs index 02284b3f4bc..3a153e821f1 100644 --- a/near/tests/sync_nodes.rs +++ b/near/tests/sync_nodes.rs @@ -13,7 +13,6 @@ use near_client::{ClientActor, GetBlock}; use near_network::test_utils::{convert_boot_nodes, open_port, WaitOrTimeout}; use near_network::{NetworkClientMessages, PeerInfo}; use near_primitives::crypto::signer::InMemorySigner; -use near_primitives::serialize::BaseEncode; use near_primitives::test_utils::{init_integration_logger, init_test_logger}; use near_primitives::transaction::{Action, SignedTransaction, StakeAction}; use near_store::test_utils::create_test_store; @@ -81,7 +80,7 @@ fn sync_nodes() { Box::new(move |_ctx| { actix::spawn(view_client2.send(GetBlock::Best).then(|res| { match &res { - Ok(Ok(b)) if b.header.height == 11 => System::current().stop(), + Ok(Ok(b)) if b.header.inner.height == 11 => System::current().stop(), Err(_) => return futures::future::err(()), _ => {} }; @@ -131,13 +130,13 @@ fn sync_after_sync_nodes() { let next_step1 = next_step.clone(); actix::spawn(view_client2.send(GetBlock::Best).then(move |res| { match &res { - Ok(Ok(b)) if b.header.height == 11 => { + Ok(Ok(b)) if b.header.inner.height == 11 => { if !next_step1.load(Ordering::Relaxed) { let _ = add_blocks(&last_block1, client11, 11, signer1); next_step1.store(true, Ordering::Relaxed); } } - Ok(Ok(b)) if b.header.height > 20 => System::current().stop(), + Ok(Ok(b)) if b.header.inner.height > 20 => System::current().stop(), Err(_) => return futures::future::err(()), _ => {} }; @@ -203,7 +202,7 @@ fn sync_state_stake_change() { let near2_copy = near2.clone(); let dir2_path_copy = dir2_path.clone(); actix::spawn(view_client1.send(GetBlock::Best).then(move |res| { - let latest_block_height = res.unwrap().unwrap().header.height; + let latest_block_height = res.unwrap().unwrap().header.inner.height; if !started_copy.load(Ordering::SeqCst) && latest_block_height > 10 { started_copy.store(true, Ordering::SeqCst); let (_, view_client2) = start_with_config(&dir2_path_copy, near2_copy); @@ -211,7 +210,9 @@ fn sync_state_stake_change() { WaitOrTimeout::new( Box::new(move |_ctx| { actix::spawn(view_client2.send(GetBlock::Best).then(move |res| { - if res.unwrap().unwrap().header.height > latest_block_height + 1 { + if res.unwrap().unwrap().header.inner.height + > latest_block_height + 1 + { System::current().stop() } future::result::<_, ()>(Ok(())) diff --git a/near/tests/sync_state_nodes.rs b/near/tests/sync_state_nodes.rs index 98857065662..7dfbd328f6a 100644 --- a/near/tests/sync_state_nodes.rs +++ b/near/tests/sync_state_nodes.rs @@ -65,7 +65,7 @@ fn sync_state_nodes() { Box::new(move |_ctx| { actix::spawn(view_client2.send(GetBlock::Best).then(|res| { match &res { - Ok(Ok(b)) if b.header.height >= 101 => System::current().stop(), + Ok(Ok(b)) if b.header.inner.height >= 101 => System::current().stop(), Err(_) => return futures::future::err(()), _ => {} }; diff --git a/test-utils/state-viewer/src/main.rs b/test-utils/state-viewer/src/main.rs index 65c02d2ae7b..87354259ae0 100644 --- a/test-utils/state-viewer/src/main.rs +++ b/test-utils/state-viewer/src/main.rs @@ -104,7 +104,7 @@ fn load_trie( let head = chain_store.head().unwrap(); let last_header = chain_store.get_block_header(&head.last_block_hash).unwrap().clone(); let state_root = chain_store.get_post_state_root(&head.last_block_hash).unwrap(); - (runtime, *state_root, last_header.height) + (runtime, *state_root, last_header.inner.height) } fn main() { diff --git a/test-utils/testlib/src/standard_test_cases.rs b/test-utils/testlib/src/standard_test_cases.rs index 35560d3dd1a..d0be7fbc1b8 100644 --- a/test-utils/testlib/src/standard_test_cases.rs +++ b/test-utils/testlib/src/standard_test_cases.rs @@ -1,7 +1,7 @@ use near::config::{TESTING_INIT_BALANCE, TESTING_INIT_STAKE}; use near_primitives::account::AccessKey; use near_primitives::crypto::signer::InMemorySigner; -use near_primitives::hash::{hash, CryptoHash}; +use near_primitives::hash::hash; use near_primitives::transaction::FinalTransactionStatus; use near_primitives::types::Balance; @@ -179,7 +179,7 @@ pub fn test_upload_contract(node: impl Node) { let new_root = node_user.get_state_root(); assert_ne!(root, new_root); let account = node_user.view_account(&eve_account()).unwrap(); - assert_eq!(account.code_hash, hash(wasm_binary)); + assert_eq!(account.code_hash, hash(wasm_binary).into()); } pub fn test_redeploy_contract(node: impl Node) { @@ -193,7 +193,7 @@ pub fn test_redeploy_contract(node: impl Node) { let new_root = node_user.get_state_root(); assert_ne!(root, new_root); let account = node_user.view_account(account_id).unwrap(); - assert_eq!(account.code_hash, hash(test_binary)); + assert_eq!(account.code_hash, hash(test_binary).into()); } pub fn test_send_money(node: impl Node) { @@ -211,10 +211,10 @@ pub fn test_send_money(node: impl Node) { result1.unwrap(), AccountView { nonce: 1, - public_keys: vec![node.signer().public_key()], + public_keys: vec![node.signer().public_key().into()], amount: TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), + code_hash: default_code_hash().into(), storage_paid_at: 0, storage_usage: 0, } @@ -228,7 +228,7 @@ pub fn test_send_money(node: impl Node) { public_keys, amount: TESTING_INIT_BALANCE + money_used - TESTING_INIT_STAKE, staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), + code_hash: default_code_hash().into(), storage_paid_at: 0, storage_usage: 0, } @@ -245,32 +245,16 @@ pub fn test_send_money_over_balance(node: impl Node) { assert_eq!(transaction_result.transactions.len(), 1); let new_root = node_user.get_state_root(); assert_eq!(root, new_root); - let result1 = node_user.view_account(account_id); + let result1 = node_user.view_account(account_id).unwrap(); assert_eq!( - result1.unwrap(), - AccountView { - nonce: 0, - public_keys: vec![node.signer().public_key()], - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), - storage_paid_at: 0, - storage_usage: 0, - } + (result1.amount, result1.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE, TESTING_INIT_STAKE) ); + let result2 = node_user.view_account(&bob_account()).unwrap(); - let public_keys = result2.public_keys.clone(); assert_eq!( - result2, - AccountView { - nonce: 0, - public_keys, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), - storage_paid_at: 0, - storage_usage: 0, - } + (result2.amount, result2.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE, TESTING_INIT_STAKE) ); } @@ -284,18 +268,10 @@ pub fn test_refund_on_send_money_to_non_existent_account(node: impl Node) { assert_eq!(transaction_result.transactions.len(), 3); let new_root = node_user.get_state_root(); assert_ne!(root, new_root); - let result1 = node_user.view_account(account_id); + let result1 = node_user.view_account(account_id).unwrap(); assert_eq!( - result1.unwrap(), - AccountView { - nonce: 1, - public_keys: vec![node.signer().public_key()], - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), - storage_paid_at: 0, - storage_usage: 0, - } + (result1.amount, result1.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE, TESTING_INIT_STAKE) ); let result2 = node_user.view_account(&eve_account()); assert!(result2.is_err()); @@ -318,34 +294,14 @@ pub fn test_create_account(node: impl Node) { let new_root = node_user.get_state_root(); assert_ne!(root, new_root); - let result1 = node_user.view_account(account_id); + let result1 = node_user.view_account(account_id).unwrap(); assert_eq!( - result1.unwrap(), - AccountView { - nonce: 1, - public_keys: vec![node.signer().public_key()], - amount: TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), - storage_paid_at: 0, - storage_usage: 0, - } + (result1.amount, result1.staked), + (TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, TESTING_INIT_STAKE) ); let result2 = node_user.view_account(&eve_account()).unwrap(); - let public_keys = result2.public_keys.clone(); - assert_eq!( - result2, - AccountView { - nonce: 0, - public_keys, - amount: money_used, - staked: 0, - code_hash: CryptoHash::default(), - storage_paid_at: 0, - storage_usage: 0, - } - ); + assert_eq!((result2.amount, result2.staked), (money_used, 0)); } pub fn test_create_account_again(node: impl Node) { @@ -360,34 +316,14 @@ pub fn test_create_account_again(node: impl Node) { money_used, ); - let result1 = node_user.view_account(account_id); + let result1 = node_user.view_account(account_id).unwrap(); assert_eq!( - result1.unwrap(), - AccountView { - nonce: 1, - public_keys: vec![node.signer().public_key()], - amount: TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), - storage_paid_at: 0, - storage_usage: 0, - } + (result1.amount, result1.staked), + (TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, TESTING_INIT_STAKE) ); let result2 = node_user.view_account(&eve_account()).unwrap(); - let public_keys = result2.public_keys.clone(); - assert_eq!( - result2, - AccountView { - nonce: 0, - public_keys, - amount: money_used, - staked: 0, - code_hash: CryptoHash::default(), - storage_paid_at: 0, - storage_usage: 0, - } - ); + assert_eq!((result2.amount, result2.staked), (money_used, 0)); let transaction_result = node_user.create_account( account_id.clone(), @@ -401,18 +337,10 @@ pub fn test_create_account_again(node: impl Node) { let new_root = node_user.get_state_root(); assert_ne!(root, new_root); - let result1 = node_user.view_account(account_id); + let result1 = node_user.view_account(account_id).unwrap(); assert_eq!( - result1.unwrap(), - AccountView { - nonce: 2, - public_keys: vec![node.signer().public_key()], - amount: TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), - storage_paid_at: 0, - storage_usage: 0, - } + (result1.amount, result1.staked), + (TESTING_INIT_BALANCE - money_used - TESTING_INIT_STAKE, TESTING_INIT_STAKE) ); } @@ -441,18 +369,10 @@ pub fn test_create_account_failure_invalid_name(node: impl Node) { assert_eq!(transaction_result.transactions.len(), 1); let new_root = node_user.get_state_root(); assert_eq!(root, new_root); - let account = node_user.view_account(account_id).unwrap(); + let result1 = node_user.view_account(account_id).unwrap(); assert_eq!( - account, - AccountView { - nonce: 0, - public_keys: vec![node.signer().public_key()], - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), - storage_paid_at: 0, - storage_usage: 0, - } + (result1.amount, result1.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE, TESTING_INIT_STAKE) ); } } @@ -474,33 +394,16 @@ pub fn test_create_account_failure_already_exists(node: impl Node) { let new_root = node_user.get_state_root(); assert_ne!(root, new_root); - let result1 = node_user.view_account(account_id); + let result1 = node_user.view_account(account_id).unwrap(); assert_eq!( - result1.unwrap(), - AccountView { - nonce: 1, - public_keys: vec![node.signer().public_key()], - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), - storage_paid_at: 0, - storage_usage: 0, - } + (result1.amount, result1.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE, TESTING_INIT_STAKE) ); let result2 = node_user.view_account(&bob_account()).unwrap(); - let public_keys = result2.public_keys.clone(); assert_eq!( - result2, - AccountView { - nonce: 0, - public_keys, - amount: TESTING_INIT_BALANCE - TESTING_INIT_STAKE, - staked: TESTING_INIT_STAKE, - code_hash: default_code_hash(), - storage_paid_at: 0, - storage_usage: 0, - } + (result2.amount, result2.staked), + (TESTING_INIT_BALANCE - TESTING_INIT_STAKE, TESTING_INIT_STAKE) ); } @@ -531,7 +434,7 @@ pub fn test_swap_key(node: impl Node) { assert_ne!(new_root, new_root1); let account = node_user.view_account(&eve_account()).unwrap(); - assert_eq!(account.public_keys, vec![signer2.public_key]); + assert_eq!(account.public_keys, vec![signer2.public_key.into()]); } pub fn test_add_key(node: impl Node) { @@ -548,7 +451,7 @@ pub fn test_add_key(node: impl Node) { let account = node_user.view_account(account_id).unwrap(); assert_eq!(account.public_keys.len(), 2); - assert_eq!(account.public_keys[1], signer2.public_key); + assert_eq!(account.public_keys[1], signer2.public_key.into()); } pub fn test_add_existing_key(node: impl Node) { @@ -588,7 +491,7 @@ pub fn test_delete_key(node: impl Node) { let account = node_user.view_account(account_id).unwrap(); assert_eq!(account.public_keys.len(), 1); - assert_eq!(account.public_keys[0], signer2.public_key); + assert_eq!(account.public_keys[0], signer2.public_key.into()); } pub fn test_delete_key_not_owned(node: impl Node) { @@ -662,7 +565,7 @@ pub fn test_delete_access_key(node: impl Node) { let account = node_user.view_account(account_id).unwrap(); assert_eq!(account.public_keys.len(), 1); - assert_eq!(account.public_keys[0], node.signer().public_key()); + assert_eq!(account.public_keys[0], node.signer().public_key().into()); let view_access_key = node_user.get_access_key(account_id, &signer2.public_key).unwrap(); assert_eq!(view_access_key, None); @@ -713,7 +616,7 @@ pub fn test_delete_access_key_with_owner_refund(node: impl Node) { let account = node_user.view_account(account_id).unwrap(); assert_eq!(account.public_keys.len(), 1); - assert_eq!(account.public_keys[0], node.signer().public_key()); + assert_eq!(account.public_keys[0], node.signer().public_key().into()); // Shit Happens. AccessKey should be switched to allowance and currently don't give refunds. assert_eq!(account.amount, initial_balance - 10); diff --git a/test-utils/testlib/src/user/mod.rs b/test-utils/testlib/src/user/mod.rs index 9ac7a4fab00..5da44d49609 100644 --- a/test-utils/testlib/src/user/mod.rs +++ b/test-utils/testlib/src/user/mod.rs @@ -8,7 +8,7 @@ use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; -use near_primitives::rpc::{AccessKeyView, AccountView, ViewStateResult}; +use near_primitives::rpc::{AccessKeyView, AccountView, CryptoHashView, ViewStateResult}; use near_primitives::transaction::{ Action, AddKeyAction, CreateAccountAction, DeleteAccountAction, DeleteKeyAction, DeployContractAction, FinalTransactionResult, FunctionCallAction, SignedTransaction, @@ -51,7 +51,7 @@ pub trait User { fn get_transaction_final_result(&self, hash: &CryptoHash) -> FinalTransactionResult; - fn get_state_root(&self) -> MerkleHash; + fn get_state_root(&self) -> CryptoHashView; fn get_receipt_info(&self, hash: &CryptoHash) -> Option; diff --git a/test-utils/testlib/src/user/rpc_user.rs b/test-utils/testlib/src/user/rpc_user.rs index 49e29dbe901..acc88ddd224 100644 --- a/test-utils/testlib/src/user/rpc_user.rs +++ b/test-utils/testlib/src/user/rpc_user.rs @@ -10,10 +10,12 @@ use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; -use near_primitives::rpc::{AccessKeyView, AccountView, QueryResponse, ViewStateResult}; +use near_primitives::rpc::{ + AccessKeyView, AccountView, CryptoHashView, QueryResponse, ViewStateResult, +}; use near_primitives::serialize::{to_base, to_base64, BaseEncode, Encode}; use near_primitives::transaction::{FinalTransactionResult, SignedTransaction, TransactionResult}; -use near_primitives::types::{AccountId, MerkleHash}; +use near_primitives::types::AccountId; use crate::user::User; @@ -86,7 +88,7 @@ impl User for RpcUser { System::new("actix").block_on(self.client.write().unwrap().tx(hash.into())).unwrap() } - fn get_state_root(&self) -> MerkleHash { + fn get_state_root(&self) -> CryptoHashView { self.get_status().map(|status| status.sync_info.latest_state_root).unwrap() } diff --git a/test-utils/testlib/src/user/runtime_user.rs b/test-utils/testlib/src/user/runtime_user.rs index cb922d69048..082e16cb8fa 100644 --- a/test-utils/testlib/src/user/runtime_user.rs +++ b/test-utils/testlib/src/user/runtime_user.rs @@ -10,7 +10,7 @@ use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; -use near_primitives::rpc::{AccessKeyView, AccountView, ViewStateResult}; +use near_primitives::rpc::{AccessKeyView, AccountView, CryptoHashView, ViewStateResult}; use near_primitives::transaction::{ FinalTransactionResult, FinalTransactionStatus, SignedTransaction, TransactionLog, TransactionResult, TransactionStatus, @@ -210,8 +210,8 @@ impl User for RuntimeUser { self.get_final_transaction_result(hash) } - fn get_state_root(&self) -> MerkleHash { - self.client.read().expect(POISONED_LOCK_ERR).state_root + fn get_state_root(&self) -> CryptoHashView { + self.client.read().expect(POISONED_LOCK_ERR).state_root.into() } fn get_receipt_info(&self, hash: &CryptoHash) -> Option { From 604cdc044a1c2931614ffd9911f1d6893273843f Mon Sep 17 00:00:00 2001 From: Maksym Zavershynskyi Date: Wed, 14 Aug 2019 17:18:13 -0700 Subject: [PATCH 07/25] NBOR --- core/nbor/src/de/mod.rs | 0 core/nbor/src/ser/mod.rs | 0 core/nbor/tests/test_simple_structs.rs | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 core/nbor/src/de/mod.rs create mode 100644 core/nbor/src/ser/mod.rs create mode 100644 core/nbor/tests/test_simple_structs.rs diff --git a/core/nbor/src/de/mod.rs b/core/nbor/src/de/mod.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/core/nbor/src/ser/mod.rs b/core/nbor/src/ser/mod.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/core/nbor/tests/test_simple_structs.rs b/core/nbor/tests/test_simple_structs.rs new file mode 100644 index 00000000000..e69de29bb2d From 58cc5d28e6a187fddf10970e00b45f9c049b9512 Mon Sep 17 00:00:00 2001 From: Maksym Zavershynskyi Date: Wed, 14 Aug 2019 17:18:22 -0700 Subject: [PATCH 08/25] NBOR --- Cargo.lock | 16 ++ Cargo.toml | 2 + core/nbor-derive/Cargo.toml | 14 ++ core/nbor-derive/src/lib.rs | 308 +++++++++++++++++++++++++ core/nbor/Cargo.toml | 10 + core/nbor/src/de/mod.rs | 75 ++++++ core/nbor/src/lib.rs | 7 + core/nbor/src/ser/mod.rs | 70 ++++++ core/nbor/tests/test_simple_structs.rs | 63 +++++ 9 files changed, 565 insertions(+) create mode 100644 core/nbor-derive/Cargo.toml create mode 100644 core/nbor-derive/src/lib.rs create mode 100644 core/nbor/Cargo.toml create mode 100644 core/nbor/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 1e869a3c129..57374fa588c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2013,6 +2013,22 @@ dependencies = [ "tempfile 3.0.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "nbor" +version = "0.1.0" +dependencies = [ + "nbor-derive 0.1.0", +] + +[[package]] +name = "nbor-derive" +version = "0.1.0" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "near" version = "0.2.7" diff --git a/Cargo.toml b/Cargo.toml index c152260f873..79d934dc3dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,8 @@ edition = "2018" members = [ "async-utils/", "protos/builder", + "core/nbor", + "core/nbor-derive", "core/primitives", "core/store", "runtime/runtime", diff --git a/core/nbor-derive/Cargo.toml b/core/nbor-derive/Cargo.toml new file mode 100644 index 00000000000..e019d76a3d7 --- /dev/null +++ b/core/nbor-derive/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "nbor-derive" +version = "0.1.0" +authors = ["Near Inc "] +edition = "2018" + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = "0.4.30" +syn = {version = "0.15.34", features = ["full", "fold"] } +quote = "0.6.12" + diff --git a/core/nbor-derive/src/lib.rs b/core/nbor-derive/src/lib.rs new file mode 100644 index 00000000000..c6ae22d7769 --- /dev/null +++ b/core/nbor-derive/src/lib.rs @@ -0,0 +1,308 @@ +#![recursion_limit = "128"] +extern crate proc_macro; + +use proc_macro::TokenStream; +use proc_macro2::Span; +use quote::quote; +use syn::export::TokenStream2; +use syn::{Attribute, Fields, Ident, ItemEnum, ItemStruct, ItemUnion, Meta, NestedMeta}; + +#[proc_macro_derive(nbor, attributes(nbor_skip, nbor_init))] +pub fn nbor(input: TokenStream) -> TokenStream { + if let Ok(input) = syn::parse::(input.clone()) { + let mut res = nbor_struct_ser(&input); + res.extend(nbor_struct_de(&input)); + TokenStream::from(res) + } else if let Ok(input) = syn::parse::(input.clone()) { + let mut res = nbor_enum_ser(&input); + res.extend(nbor_enum_de(&input)); + TokenStream::from(res) + } else if let Ok(input) = syn::parse::(input.clone()) { + let mut res = nbor_union_ser(&input); + res.extend(nbor_union_de(&input)); + TokenStream::from(res) + } else { + // Derive macros can only be defined on structs, enums, and unions. + unreachable!() + } +} + +// Check if the field is marked with `#[nbor(skip)]` or `#[.., nbor(.., skip, ...), ...]`. +fn contains_skip(attrs: &[Attribute]) -> bool { + for attr in attrs.iter() { + if let Ok(Meta::Word(ident)) = attr.parse_meta() { + if ident.to_string().as_str() == "nbor_skip" { + return true; + } + } + } + false +} + +// Checks if struct should be initialized with some method after the object is constructed. +// Should be marked as #[nbor_init()] +fn contains_initialize_with(attrs: &[Attribute]) -> Option { + for attr in attrs.iter() { + if let Ok(Meta::List(meta_list)) = attr.parse_meta() { + if meta_list.ident.to_string().as_str() == "nbor_init" { + assert_eq!( + meta_list.nested.len(), + 1, + "nbor_init requires exactly one initialization method." + ); + let nested_meta = meta_list.nested.iter().next().unwrap(); + if let NestedMeta::Meta(Meta::Word(ident)) = nested_meta { + return Some(ident.clone()); + } + } + } + } + None +} + +fn nbor_struct_ser(input: &ItemStruct) -> TokenStream2 { + let name = &input.ident; + let mut body = TokenStream2::new(); + match &input.fields { + Fields::Named(fields) => { + for field in &fields.named { + if contains_skip(&field.attrs) { + continue; + } + let field_name = field.ident.as_ref().unwrap(); + let delta = quote! { + nbor::Serializable::write(&self.#field_name, writer)?; + }; + body.extend(delta); + } + } + Fields::Unnamed(fields) => { + for field_idx in 0..fields.unnamed.len() { + let delta = quote! { + nbor::Serializable::write(&self.#field_idx, writer)?; + }; + body.extend(delta); + } + } + Fields::Unit => {} + } + quote! { + impl nbor::ser::Serializable for #name { + fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { + #body + Ok(()) + } + } + } +} + +fn nbor_enum_ser(input: &ItemEnum) -> TokenStream2 { + let name = &input.ident; + let mut body = TokenStream2::new(); + for (variant_idx, variant) in input.variants.iter().enumerate() { + let variant_idx = variant_idx as u32; + let variant_ident = &variant.ident; + let mut variant_header = TokenStream2::new(); + let mut variant_body = TokenStream2::new(); + match &variant.fields { + Fields::Named(fields) => { + for field in &fields.named { + let field_name = field.ident.as_ref().unwrap(); + if contains_skip(&field.attrs) { + variant_header.extend(quote! { _#field_name, }); + continue; + } else { + variant_header.extend(quote! { #field_name, }); + } + variant_body.extend(quote! { + nbor::Serializable::write(#field_name, writer)?; + }) + } + variant_header = quote! { { #variant_header }}; + } + Fields::Unnamed(fields) => { + for (field_idx, field) in fields.unnamed.iter().enumerate() { + let field_idx = field_idx as u32; + if contains_skip(&field.attrs) { + let field_ident = + Ident::new(format!("_id{}", field_idx).as_str(), Span::call_site()); + variant_header.extend(quote! { #field_ident, }); + continue; + } else { + let field_ident = + Ident::new(format!("id{}", field_idx).as_str(), Span::call_site()); + variant_header.extend(quote! { #field_ident, }); + } + variant_body.extend(quote! { + nbor::Serializable::write(&#field_idx, writer)?; + }) + } + variant_header = quote! { ( #variant_header )}; + } + Fields::Unit => {} + } + body.extend(quote!( + #name::#variant_ident #variant_header => { + let variant_idx: u32 = #variant_idx; + writer.write(&variant_idx.to_le_bytes())?; + #variant_body + } + )) + } + let res = quote! { + impl nbor::ser::Serializable for #name { + fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { + match self { + #body + } + Ok(()) + } + } + }; + println!("{}", res.to_string()); + res +} + +fn nbor_union_ser(_input: &ItemUnion) -> TokenStream2 { + unimplemented!() +} + +fn nbor_struct_de(input: &ItemStruct) -> TokenStream2 { + let name = &input.ident; + let init_method = contains_initialize_with(&input.attrs); + let return_value = match &input.fields { + Fields::Named(fields) => { + let mut body = TokenStream2::new(); + for field in &fields.named { + let field_name = field.ident.as_ref().unwrap(); + let delta = if contains_skip(&field.attrs) { + quote! { + #field_name: Default::default(), + } + } else { + quote! { + #field_name: nbor::Deserializable::read(reader)?, + } + }; + body.extend(delta); + } + quote! { + Self { #body } + } + } + Fields::Unnamed(fields) => { + let mut body = TokenStream2::new(); + for _ in 0..fields.unnamed.len() { + let delta = quote! { + nbor::Deserializable::read(reader)?, + }; + body.extend(delta); + } + quote! { + Self( #body ) + } + } + Fields::Unit => { + quote! { + Self {} + } + } + }; + if let Some(method_ident) = init_method { + quote! { + impl nbor::de::Deserializable for #name { + fn read(reader: &mut R) -> Result { + let mut return_value = #return_value; + return_value.#method_ident(); + Ok(return_value) + } + } + } + } else { + quote! { + impl nbor::de::Deserializable for #name { + fn read(reader: &mut R) -> Result { + Ok(#return_value) + } + } + } + } +} +fn nbor_enum_de(input: &ItemEnum) -> TokenStream2 { + let name = &input.ident; + let init_method = contains_initialize_with(&input.attrs); + let mut variant_arms = TokenStream2::new(); + for (variant_idx, variant) in input.variants.iter().enumerate() { + let variant_idx = variant_idx as u32; + let variant_ident = &variant.ident; + let mut variant_header = TokenStream2::new(); + match &variant.fields { + Fields::Named(fields) => { + for field in &fields.named { + let field_name = field.ident.as_ref().unwrap(); + if contains_skip(&field.attrs) { + variant_header.extend(quote! { + #field_name: Default::default(), + }); + } else { + variant_header.extend(quote! { + #field_name: nbor::Deserializable::read(reader)?, + }); + } + } + variant_header = quote! { { #variant_header }}; + } + Fields::Unnamed(fields) => { + for field in fields.unnamed.iter() { + if contains_skip(&field.attrs) { + variant_header.extend(quote! { Default::default(), }); + } else { + variant_header.extend(quote! { nbor::Deserializable::read(reader)?, }); + } + } + variant_header = quote! { ( #variant_header )}; + } + Fields::Unit => {} + } + variant_arms.extend(quote! { + #variant_idx => #name::#variant_ident #variant_header , + }); + } + let variant_idx = quote! { + let mut variant_idx = [0u8; std::mem::size_of::()]; + reader.read(&mut variant_idx)?; + let variant_idx = u32::from_le_bytes(variant_idx); + }; + if let Some(method_ident) = init_method { + quote! { + impl nbor::de::Deserializable for #name { + fn read(reader: &mut R) -> Result { + #variant_idx + let mut return_value = match variant_idx { + #variant_arms + _ => panic!("Unexpeted variant index"), + }; + return_value.#method_ident(); + Ok(return_value) + } + } + } + } else { + quote! { + impl nbor::de::Deserializable for #name { + fn read(reader: &mut R) -> Result { + #variant_idx + let return_value = match variant_idx { + #variant_arms + _ => panic!("Unexpeted variant index"), + }; + Ok(return_value) + } + } + } + } +} + +fn nbor_union_de(_input: &ItemUnion) -> TokenStream2 { + unimplemented!() +} diff --git a/core/nbor/Cargo.toml b/core/nbor/Cargo.toml new file mode 100644 index 00000000000..7e083e9a04b --- /dev/null +++ b/core/nbor/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "nbor" +version = "0.1.0" +authors = ["Near Inc "] +edition = "2018" +description = """Near Binary Object Representation +""" + +[dependencies] +nbor-derive = { path = "../nbor-derive" } diff --git a/core/nbor/src/de/mod.rs b/core/nbor/src/de/mod.rs index e69de29bb2d..3087a04b780 100644 --- a/core/nbor/src/de/mod.rs +++ b/core/nbor/src/de/mod.rs @@ -0,0 +1,75 @@ +use std::io::{Cursor, Error, Read}; +use std::mem::size_of; + +/// A data-structure that can be de-serialized from binary format by NBOR. +pub trait Deserializable: Sized { + fn read(reader: &mut R) -> Result; + + /// Deserialize this instance from a slice of bytes. + fn from_slice(v: &[u8]) -> Result { + let mut c = Cursor::new(v); + Self::read(&mut c) + } +} + +macro_rules! impl_for_integer { + ($type: ident) => { + impl Deserializable for $type { + fn read(reader: &mut R) -> Result { + let mut data = [0u8; size_of::<$type>()]; + reader.read_exact(&mut data)?; + Ok($type::from_le_bytes(data)) + } + } + }; +} + +impl_for_integer!(i8); +impl_for_integer!(i16); +impl_for_integer!(i32); +impl_for_integer!(i64); +impl_for_integer!(i128); +impl_for_integer!(isize); +impl_for_integer!(u8); +impl_for_integer!(u16); +impl_for_integer!(u32); +impl_for_integer!(u64); +impl_for_integer!(u128); +impl_for_integer!(usize); + +// Note NaNs have a portability issue. Specifically, signalling NaNs on MIPS are quiet NaNs on x86, +// and vice-versa. We disallow NaNs to avoid this issue. +macro_rules! impl_for_float { + ($type: ident, $int_type: ident) => { + impl Deserializable for $type { + fn read(reader: &mut R) -> Result { + let mut data = [0u8; size_of::<$type>()]; + reader.read_exact(&mut data)?; + let res = $type::from_bits($int_type::from_le_bytes(data)); + assert!( + !res.is_nan(), + "For portability reasons we do not allow to deserialize NaNs." + ); + Ok(res) + } + } + }; +} + +impl_for_float!(f32, u32); +impl_for_float!(f64, u64); + +impl Deserializable for Option +where + T: Deserializable, +{ + fn read(reader: &mut R) -> Result { + let mut flag = [0u8]; + reader.read_exact(&mut flag)?; + if flag[0] == 0 { + Ok(None) + } else { + Ok(Some(T::read(reader)?)) + } + } +} diff --git a/core/nbor/src/lib.rs b/core/nbor/src/lib.rs new file mode 100644 index 00000000000..2cbf47de7b1 --- /dev/null +++ b/core/nbor/src/lib.rs @@ -0,0 +1,7 @@ +pub use nbor_derive::nbor; + +pub mod de; +pub mod ser; + +pub use de::Deserializable; +pub use ser::Serializable; diff --git a/core/nbor/src/ser/mod.rs b/core/nbor/src/ser/mod.rs index e69de29bb2d..8d552fe0ad2 100644 --- a/core/nbor/src/ser/mod.rs +++ b/core/nbor/src/ser/mod.rs @@ -0,0 +1,70 @@ +use std::io::{Error, Write}; + +/// A data-structure that can be serialized into binary format by NBOR. +pub trait Serializable { + fn write(&self, writer: &mut W) -> Result<(), Error>; + + /// Serialize this instance into a vector of bytes. + fn to_vec(&self) -> Result, Error> { + let mut result = vec![]; + self.write(&mut result)?; + Ok(result) + } +} + +macro_rules! impl_for_integer { + ($type: ident) => { + impl Serializable for $type { + fn write(&self, writer: &mut W) -> Result<(), Error> { + writer.write(&self.to_le_bytes()).map(|_| ()) + } + } + }; +} + +impl_for_integer!(i8); +impl_for_integer!(i16); +impl_for_integer!(i32); +impl_for_integer!(i64); +impl_for_integer!(i128); +impl_for_integer!(isize); +impl_for_integer!(u8); +impl_for_integer!(u16); +impl_for_integer!(u32); +impl_for_integer!(u64); +impl_for_integer!(u128); +impl_for_integer!(usize); + +// Note NaNs have a portability issue. Specifically, signalling NaNs on MIPS are quiet NaNs on x86, +// and vice-versa. We disallow NaNs to avoid this issue. +macro_rules! impl_for_float { + ($type: ident) => { + impl Serializable for $type { + fn write(&self, writer: &mut W) -> Result<(), Error> { + assert!( + !self.is_nan(), + "For portability reasons we do not allow to serialize NaNs." + ); + writer.write(&self.to_bits().to_le_bytes()).map(|_| ()) + } + } + }; +} + +impl_for_float!(f32); +impl_for_float!(f64); + +impl Serializable for Option +where + T: Serializable, +{ + fn write(&self, writer: &mut W) -> Result<(), Error> { + match self { + None => writer.write(&[0u8]).map(|_| ()), + Some(value) => { + writer.write(&[1u8])?; + value.write(writer) + } + } + } +} diff --git a/core/nbor/tests/test_simple_structs.rs b/core/nbor/tests/test_simple_structs.rs index e69de29bb2d..cef37efb3d2 100644 --- a/core/nbor/tests/test_simple_structs.rs +++ b/core/nbor/tests/test_simple_structs.rs @@ -0,0 +1,63 @@ +use nbor::{nbor, Deserializable, Serializable}; + +#[derive(nbor, PartialEq, Debug)] +#[nbor_init(init)] +struct A { + x: u64, + b: B, + y: f32, + lazy: Option, + #[nbor_skip] + skipped: Option, +} + +impl A { + pub fn init(&mut self) { + if let Some(v) = self.lazy.as_mut() { + *v *= 10; + } + } +} + +#[derive(nbor, PartialEq, Debug)] +struct B { + x: u64, + y: i32, + c: C, +} + +#[derive(nbor, PartialEq, Debug)] +enum C { + C1, + C2(u64), + C3(u64, u64), + C4 { x: u64, y: u64 }, + C5(D), +} + +#[derive(nbor, PartialEq, Debug)] +struct D { + x: u64, +} + +#[test] +fn test_simple_struct() { + let a = A { + x: 1, + b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, + y: 4.0, + lazy: Some(5), + skipped: Some(6), + }; + let encoded_a = a.to_vec().unwrap(); + let decoded_a = A::from_slice(&encoded_a).unwrap(); + let expected_a = A { + x: 1, + b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, + y: 4.0, + lazy: Some(50), + skipped: None, + }; + + assert_eq!(expected_a, decoded_a); +} From 9cc8e29b434ca9ab69de685a8516ebfad2f03eb3 Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Wed, 14 Aug 2019 17:21:10 -0700 Subject: [PATCH 09/25] Adding change checker for Transaction --- core/primitives/src/crypto/signature.rs | 11 +++++++- core/primitives/src/serialize.rs | 8 ++++++ core/primitives/src/transaction.rs | 37 +++++++++++++++++++++---- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/core/primitives/src/crypto/signature.rs b/core/primitives/src/crypto/signature.rs index fbee401e9f5..46bc00af00b 100644 --- a/core/primitives/src/crypto/signature.rs +++ b/core/primitives/src/crypto/signature.rs @@ -7,7 +7,7 @@ use std::hash::{Hash, Hasher}; pub use exonum_sodiumoxide::crypto::sign::ed25519::Seed; use crate::logging::pretty_hash; -use crate::serialize::{from_base, to_base, BaseDecode, BaseEncode}; +use crate::serialize::{from_base, to_base, BaseDecode, BaseEncode, Writable, WritableResult}; use crate::types::ReadablePublicKey; use serde::{Deserialize, Serialize}; @@ -119,6 +119,15 @@ impl TryFrom<&str> for PublicKey { } } +impl Writable for PublicKey { + fn write_into(&self, out: &mut Vec) -> WritableResult { + // 1 byte for kind. + out.extend_from_slice(&vec![0]); + out.extend_from_slice(&(self.0).0); + Ok(()) + } +} + impl TryFrom<&[u8]> for SecretKey { type Error = Box; diff --git a/core/primitives/src/serialize.rs b/core/primitives/src/serialize.rs index 5452986e9fc..1949eecf04e 100644 --- a/core/primitives/src/serialize.rs +++ b/core/primitives/src/serialize.rs @@ -37,6 +37,14 @@ impl_writable_for_uint!(u32); impl_writable_for_uint!(u64); impl_writable_for_uint!(u128); +impl Writable for String { + fn write_into(&self, out: &mut Vec) -> WritableResult { + (self.len() as u32).write_into(out)?; + out.extend_from_slice(self.as_bytes()); + Ok(()) + } +} + impl Writable for Vec where T: Writable, diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index 9f2e62a29e6..06fcd7ed0ff 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -8,7 +8,9 @@ use crate::crypto::signature::{verify, PublicKey, Signature}; use crate::crypto::signer::EDSigner; use crate::hash::{hash, CryptoHash}; use crate::logging; -use crate::serialize::{base_bytes_format, u128_dec_format, Decode, DecodeResult, Encode}; +use crate::serialize::{ + base_bytes_format, u128_dec_format, Decode, DecodeResult, Encode, Writable, WritableResult, +}; use crate::types::{AccountId, Balance, Gas, Nonce, ShardId, StructSignature}; use serde::{Deserialize, Deserializer}; use std::sync::Arc; @@ -27,13 +29,22 @@ pub struct Transaction { impl Transaction { pub fn get_hash(&self) -> CryptoHash { - // let proto: transaction_proto::Transaction = self.clone().into(); - // let bytes = proto.write_to_bytes().unwrap(); - let bytes = vec![]; + let bytes = self.write().expect("Failed to deserialize"); hash(&bytes) } } +impl Writable for Transaction { + fn write_into(&self, out: &mut Vec) -> WritableResult { + self.signer_id.write_into(out)?; + self.public_key.write_into(out)?; + self.nonce.write_into(out)?; + self.receiver_id.write_into(out)?; + self.actions.write_into(out)?; + Ok(()) + } +} + #[derive(Hash, PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub enum Action { CreateAccount(CreateAccountAction), @@ -321,7 +332,7 @@ mod tests { use crate::crypto::signature::{get_key_pair, sign, DEFAULT_SIGNATURE}; use super::*; - use crate::serialize::{Decode, Encode}; + use crate::serialize::{to_base, Decode, Encode}; #[test] fn test_verify_transaction() { @@ -348,4 +359,20 @@ mod tests { let new_tx = SignedTransaction::decode(&bytes).unwrap().into(); assert!(verify_transaction_signature(&new_tx, &valid_keys)); } + + /// This test is change checker for a reason - we don't expect transaction format to change. + /// If it does - you MUST update all of the dependencies: like nearlib. + #[test] + fn test_serialize_transaction() { + let transaction = Transaction { + signer_id: "test.near".to_string(), + public_key: PublicKey::try_from("22skMptHjFWNyuEWY22ftn2AbLPSYpmYwGJRGwpNHbTV") + .unwrap(), + nonce: 1, + receiver_id: "123".to_string(), + actions: vec![], + }; + let hash = to_base(&transaction.get_hash()); + assert_eq!(hash, "EsTRpLernDsH2hzznZ6wKMu1XYdyT4ynKK2H13hbMyzb"); + } } From cb640c6098dec988daa0642efb661422a4f0a2b2 Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Thu, 15 Aug 2019 00:42:47 -0700 Subject: [PATCH 10/25] Switch to using NBOR instead of serde. Compiles --- Cargo.lock | 9 ++ chain/chain/Cargo.toml | 1 + chain/chain/src/chain.rs | 8 +- chain/chain/src/lib.rs | 3 - chain/chain/src/types.rs | 3 +- chain/client/src/client.rs | 3 +- chain/client/src/types.rs | 5 +- chain/client/src/view_client.rs | 7 +- chain/client/tests/process_blocks.rs | 22 ++-- chain/jsonrpc/Cargo.toml | 1 + chain/jsonrpc/client/src/lib.rs | 5 +- chain/jsonrpc/src/lib.rs | 11 +- chain/jsonrpc/tests/rpc_query.rs | 2 +- chain/jsonrpc/tests/rpc_transactions.rs | 7 +- chain/network/Cargo.toml | 1 + chain/network/src/codec.rs | 9 +- chain/network/src/peer_manager.rs | 3 +- chain/network/src/peer_store.rs | 21 ++-- chain/network/src/types.rs | 45 ++++--- core/nbor-derive/src/lib.rs | 4 +- core/nbor/Cargo.toml | 7 ++ core/nbor/src/de/mod.rs | 129 +++++++++++++++++++ core/nbor/src/ser/mod.rs | 129 ++++++++++++++++++- core/nbor/tests/test_simple_structs.rs | 12 +- core/primitives/Cargo.toml | 2 + core/primitives/benches/serialization.rs | 25 ++-- core/primitives/src/account.rs | 7 +- core/primitives/src/block.rs | 56 ++++----- core/primitives/src/crypto/signature.rs | 43 +++++-- core/primitives/src/hash.rs | 18 ++- core/primitives/src/receipt.rs | 17 ++- core/primitives/src/rpc.rs | 118 +++++++++++++++-- core/primitives/src/serialize.rs | 133 -------------------- core/primitives/src/transaction.rs | 120 ++++++++++-------- core/primitives/src/types.rs | 7 +- core/primitives/src/utils.rs | 14 ++- core/store/Cargo.toml | 1 + core/store/src/lib.rs | 29 +++-- near/Cargo.toml | 1 + near/src/config.rs | 8 +- near/src/runtime.rs | 20 +-- near/src/test_utils.rs | 6 +- near/src/validator_manager.rs | 66 +++++----- near/tests/run_nodes.rs | 5 +- near/tests/sync_nodes.rs | 12 +- near/tests/sync_state_nodes.rs | 2 +- runtime/runtime/src/lib.rs | 21 ++-- runtime/runtime/src/store.rs | 15 +-- test-utils/loadtester/Cargo.toml | 1 + test-utils/loadtester/src/remote_node.rs | 7 +- test-utils/state-viewer/Cargo.toml | 1 + test-utils/state-viewer/src/main.rs | 19 ++- test-utils/testlib/Cargo.toml | 1 + test-utils/testlib/src/user/mod.rs | 7 +- test-utils/testlib/src/user/rpc_user.rs | 12 +- test-utils/testlib/src/user/runtime_user.rs | 7 +- 56 files changed, 774 insertions(+), 474 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 45f2127fdb1..245f5d212b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1810,6 +1810,7 @@ dependencies = [ "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nbor 0.1.0", "near 0.2.7", "near-primitives 0.1.0", "node-runtime 0.0.1", @@ -2048,6 +2049,7 @@ dependencies = [ "kvdb-memorydb 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nbor 0.1.0", "near-chain 0.1.0", "near-client 0.1.0", "near-jsonrpc 0.1.0", @@ -2076,6 +2078,7 @@ dependencies = [ "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "kvdb 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nbor 0.1.0", "near-primitives 0.1.0", "near-store 0.1.0", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2120,6 +2123,7 @@ dependencies = [ "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "futures-preview 0.3.0-alpha.16 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nbor 0.1.0", "near-chain 0.1.0", "near-client 0.1.0", "near-jsonrpc-client 0.1.0", @@ -2156,6 +2160,7 @@ dependencies = [ "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nbor 0.1.0", "near-chain 0.1.0", "near-client 0.1.0", "near-primitives 0.1.0", @@ -2196,6 +2201,7 @@ dependencies = [ "jemallocator 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nbor 0.1.0", "pairing 0.14.1 (git+https://github.com/nearprotocol/pairing.git?rev=f009a9f54c1c1149cea4ee3e6e58ed71d72bb2e9)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2221,6 +2227,7 @@ dependencies = [ "kvdb-memorydb 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "kvdb-rocksdb 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nbor 0.1.0", "near-primitives 0.1.0", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3317,6 +3324,7 @@ version = "0.1.0" dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "nbor 0.1.0", "near 0.2.7", "near-chain 0.1.0", "near-network 0.1.0", @@ -3448,6 +3456,7 @@ dependencies = [ "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nbor 0.1.0", "near 0.2.7", "near-chain 0.1.0", "near-client 0.1.0", diff --git a/chain/chain/Cargo.toml b/chain/chain/Cargo.toml index 0ee36d49ed0..32438c5d7c7 100644 --- a/chain/chain/Cargo.toml +++ b/chain/chain/Cargo.toml @@ -14,5 +14,6 @@ serde = "1.0" serde_derive = "1.0" cached = { git = "https://github.com/nearprotocol/cached", rev = "7e472eddef68607e344d5a106a0e6705d92e55be" } +nbor = { path = "../../core/nbor" } near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } diff --git a/chain/chain/src/chain.rs b/chain/chain/src/chain.rs index d13c5a1edf7..af7f6856934 100644 --- a/chain/chain/src/chain.rs +++ b/chain/chain/src/chain.rs @@ -817,8 +817,8 @@ impl<'a> ChainUpdate<'a> { provenance: &Provenance, ) -> Result<(), Error> { // Refuse blocks from the too distant future. - if header.inner.timestamp > Utc::now() + Duration::seconds(ACCEPTABLE_TIME_DIFFERENCE) { - return Err(ErrorKind::InvalidBlockFutureTime(header.inner.timestamp).into()); + if header.timestamp() > Utc::now() + Duration::seconds(ACCEPTABLE_TIME_DIFFERENCE) { + return Err(ErrorKind::InvalidBlockFutureTime(header.timestamp()).into()); } // First I/O cost, delay as much as possible. @@ -830,8 +830,8 @@ impl<'a> ChainUpdate<'a> { // time progression. if header.inner.timestamp <= prev_header.inner.timestamp { return Err(ErrorKind::InvalidBlockPastTime( - prev_header.inner.timestamp, - header.inner.timestamp, + prev_header.timestamp(), + header.timestamp(), ) .into()); } diff --git a/chain/chain/src/lib.rs b/chain/chain/src/lib.rs index 1e94010c356..75583306678 100644 --- a/chain/chain/src/lib.rs +++ b/chain/chain/src/lib.rs @@ -1,6 +1,3 @@ -#[macro_use] -extern crate serde_derive; - pub use chain::{Chain, MAX_ORPHAN_SIZE}; pub use error::{Error, ErrorKind}; pub use store::{ChainStore, ChainStoreAccess}; diff --git a/chain/chain/src/types.rs b/chain/chain/src/types.rs index 419390d3a66..91d9636e974 100644 --- a/chain/chain/src/types.rs +++ b/chain/chain/src/types.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use nbor::nbor; pub use near_primitives::block::{Block, BlockHeader, Weight}; use near_primitives::crypto::signature::Signature; use near_primitives::crypto::signer::EDSigner; @@ -166,7 +167,7 @@ pub trait RuntimeAdapter: Send + Sync { /// The tip of a fork. A handle to the fork ancestry from its leaf in the /// blockchain tree. References the max height and the latest and previous /// blocks for convenience and the total weight. -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(nbor, Debug, Clone, PartialEq)] pub struct Tip { /// Height of the tip (max height of the fork) pub height: BlockIndex, diff --git a/chain/client/src/client.rs b/chain/client/src/client.rs index cb8b9d6a359..8f11632b002 100644 --- a/chain/client/src/client.rs +++ b/chain/client/src/client.rs @@ -33,6 +33,7 @@ use near_primitives::rpc::ValidatorInfo; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, BlockIndex, ShardId}; use near_primitives::unwrap_or_return; +use near_primitives::utils::from_timestamp; use near_store::Store; use near_telemetry::TelemetryActor; @@ -346,7 +347,7 @@ impl Handler for ClientActor { latest_block_hash: head.last_block_hash.into(), latest_block_height: head.height, latest_state_root: state_root.clone().into(), - latest_block_time, + latest_block_time: from_timestamp(latest_block_time), syncing: self.sync_status.is_syncing(), }, }) diff --git a/chain/client/src/types.rs b/chain/client/src/types.rs index 05e1bfa463d..f40481f8183 100644 --- a/chain/client/src/types.rs +++ b/chain/client/src/types.rs @@ -5,10 +5,9 @@ use std::time::Duration; use actix::Message; use chrono::{DateTime, Utc}; -use near_chain::Block; use near_primitives::crypto::signer::{AccountSigner, EDSigner, InMemorySigner}; use near_primitives::hash::CryptoHash; -use near_primitives::rpc::QueryResponse; +use near_primitives::rpc::{BlockView, QueryResponse}; pub use near_primitives::rpc::{StatusResponse, StatusSyncInfo}; use near_primitives::transaction::{FinalTransactionResult, TransactionResult}; use near_primitives::types::{AccountId, BlockIndex, ShardId, Version}; @@ -228,7 +227,7 @@ pub enum GetBlock { } impl Message for GetBlock { - type Result = Result; + type Result = Result; } /// Queries client for given path / data. diff --git a/chain/client/src/view_client.rs b/chain/client/src/view_client.rs index 2ed890581fa..025db9e8018 100644 --- a/chain/client/src/view_client.rs +++ b/chain/client/src/view_client.rs @@ -6,9 +6,9 @@ use std::sync::Arc; use actix::{Actor, Context, Handler}; use chrono::{DateTime, Utc}; -use near_chain::{Block, Chain, ErrorKind, RuntimeAdapter}; +use near_chain::{Chain, ErrorKind, RuntimeAdapter}; use near_primitives::hash::CryptoHash; -use near_primitives::rpc::QueryResponse; +use near_primitives::rpc::{BlockView, QueryResponse}; use near_primitives::transaction::{ FinalTransactionResult, FinalTransactionStatus, TransactionLog, TransactionResult, TransactionStatus, @@ -108,7 +108,7 @@ impl Handler for ViewClientActor { /// Handles retrieving block from the chain. impl Handler for ViewClientActor { - type Result = Result; + type Result = Result; fn handle(&mut self, msg: GetBlock, _: &mut Context) -> Self::Result { match msg { @@ -119,6 +119,7 @@ impl Handler for ViewClientActor { GetBlock::Height(height) => self.chain.get_block_by_height(height).map(Clone::clone), GetBlock::Hash(hash) => self.chain.get_block(&hash).map(Clone::clone), } + .map(|block| block.into()) .map_err(|err| err.to_string()) } } diff --git a/chain/client/tests/process_blocks.rs b/chain/client/tests/process_blocks.rs index 89fecec4dcc..f280e20a1dd 100644 --- a/chain/client/tests/process_blocks.rs +++ b/chain/client/tests/process_blocks.rs @@ -98,8 +98,8 @@ fn receive_network_block() { let last_block = res.unwrap().unwrap(); let signer = Arc::new(InMemorySigner::from_seed("test1", "test1")); let block = Block::produce( - &last_block.header, - last_block.header.inner.height + 1, + &last_block.header.clone().into(), + last_block.header.height + 1, MerkleHash::default(), CryptoHash::default(), vec![], @@ -147,8 +147,8 @@ fn receive_network_block_header() { let last_block = res.unwrap().unwrap(); let signer = Arc::new(InMemorySigner::from_seed("test", "test")); let block = Block::produce( - &last_block.header, - last_block.header.inner.height + 1, + &last_block.header.clone().into(), + last_block.header.height + 1, MerkleHash::default(), CryptoHash::default(), vec![], @@ -189,8 +189,8 @@ fn produce_block_with_approvals() { let signer1 = Arc::new(InMemorySigner::from_seed("test1", "test1")); let signer3 = Arc::new(InMemorySigner::from_seed("test3", "test3")); let block = Block::produce( - &last_block.header, - last_block.header.inner.height + 1, + &last_block.header.clone().into(), + last_block.header.height + 1, MerkleHash::default(), CryptoHash::default(), vec![], @@ -238,8 +238,8 @@ fn invalid_blocks() { let signer = Arc::new(InMemorySigner::from_seed("test", "test")); // Send invalid state root. let block = Block::produce( - &last_block.header, - last_block.header.inner.height + 1, + &last_block.header.clone().into(), + last_block.header.height + 1, hash(&[0]), CryptoHash::default(), vec![], @@ -254,7 +254,7 @@ fn invalid_blocks() { )); // Send block that builds on invalid one. let block2 = Block::produce( - &block.header, + &block.header.clone().into(), block.header.inner.height + 1, hash(&[1]), CryptoHash::default(), @@ -266,8 +266,8 @@ fn invalid_blocks() { client.do_send(NetworkClientMessages::Block(block2, PeerInfo::random().id, false)); // Send proper block. let block3 = Block::produce( - &last_block.header, - last_block.header.inner.height + 1, + &last_block.header.clone().into(), + last_block.header.height + 1, MerkleHash::default(), CryptoHash::default(), vec![], diff --git a/chain/jsonrpc/Cargo.toml b/chain/jsonrpc/Cargo.toml index b3a530fe5c4..8f585eb385b 100644 --- a/chain/jsonrpc/Cargo.toml +++ b/chain/jsonrpc/Cargo.toml @@ -22,6 +22,7 @@ tokio = "0.1.15" uuid = { version = "~0.6", features = ["v4"] } async-utils = { path = "../../async-utils" } +nbor = { path = "../../core/nbor" } near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } near-chain = { path = "../chain" } diff --git a/chain/jsonrpc/client/src/lib.rs b/chain/jsonrpc/client/src/lib.rs index 2e4b893c8b6..2761aaf97a8 100644 --- a/chain/jsonrpc/client/src/lib.rs +++ b/chain/jsonrpc/client/src/lib.rs @@ -4,8 +4,7 @@ use actix_web::client::Client; use futures::Future; use serde::Serialize; -use near_primitives::block::Block; -use near_primitives::rpc::{QueryResponse, StatusResponse}; +use near_primitives::rpc::{BlockView, QueryResponse, StatusResponse}; use near_primitives::transaction::{FinalTransactionResult, TransactionResult}; use near_primitives::types::BlockIndex; @@ -169,7 +168,7 @@ jsonrpc_client!(pub struct JsonRpcClient { pub fn health(&mut self) -> RpcRequest<()>; pub fn tx(&mut self, hash: String) -> RpcRequest; pub fn tx_details(&mut self, hash: String) -> RpcRequest; - pub fn block(&mut self, height: BlockIndex) -> RpcRequest; + pub fn block(&mut self, height: BlockIndex) -> RpcRequest; }); /// Create new JSON RPC client that connects to the given address. diff --git a/chain/jsonrpc/src/lib.rs b/chain/jsonrpc/src/lib.rs index e4243265a30..d1576eff8ca 100644 --- a/chain/jsonrpc/src/lib.rs +++ b/chain/jsonrpc/src/lib.rs @@ -15,12 +15,13 @@ use serde_json::Value; use async_utils::{delay, timeout}; use message::{Request, RpcError}; use message::Message; +use nbor::Deserializable; use near_client::{ClientActor, GetBlock, Query, Status, TxDetails, TxStatus, ViewClientActor}; pub use near_jsonrpc_client as client; use near_jsonrpc_client::message as message; use near_network::{NetworkClientMessages, NetworkClientResponses}; use near_primitives::hash::CryptoHash; -use near_primitives::serialize::{BaseEncode, Decode, from_base, from_base64}; +use near_primitives::serialize::{BaseEncode, from_base, from_base64}; use near_primitives::transaction::{FinalTransactionStatus, SignedTransaction}; use near_primitives::types::BlockIndex; @@ -98,13 +99,7 @@ fn jsonify( fn parse_tx(params: Option) -> Result { let (encoded,) = parse_params::<(String,)>(params)?; let bytes = from_base64_or_parse_err(encoded)?; - Decode::decode(&bytes).map_err(|e| RpcError::invalid_params(Some(format!("Failed to decode transaction: {}", e)))) -// let tx: transaction_proto::SignedTransaction = parse_from_bytes(&bytes).map_err(|e| { -// RpcError::invalid_params(Some(format!("Failed to decode transaction proto: {}", e))) -// })?; -// Ok(tx.try_into().map_err(|e| { -// RpcError::invalid_params(Some(format!("Failed to decode transaction: {}", e))) -// })?) + SignedTransaction::from_slice(&bytes).map_err(|e| RpcError::invalid_params(Some(format!("Failed to decode transaction: {}", e)))) } fn parse_hash(params: Option) -> Result { diff --git a/chain/jsonrpc/tests/rpc_query.rs b/chain/jsonrpc/tests/rpc_query.rs index acac7ae3cfb..4a533539102 100644 --- a/chain/jsonrpc/tests/rpc_query.rs +++ b/chain/jsonrpc/tests/rpc_query.rs @@ -16,7 +16,7 @@ fn test_block() { let mut client = new_client(&format!("http://{}", addr)); actix::spawn(client.block(0).then(|res| { - assert_eq!(res.unwrap().header.inner.height, 0); + assert_eq!(res.unwrap().header.height, 0); System::current().stop(); future::result(Ok(())) })); diff --git a/chain/jsonrpc/tests/rpc_transactions.rs b/chain/jsonrpc/tests/rpc_transactions.rs index c529035dd83..cf0d0e81f2f 100644 --- a/chain/jsonrpc/tests/rpc_transactions.rs +++ b/chain/jsonrpc/tests/rpc_transactions.rs @@ -3,11 +3,12 @@ use std::sync::Arc; use actix::{Actor, System}; use futures::future::Future; +use nbor::Serializable; use near_jsonrpc::client::new_client; use near_jsonrpc::test_utils::start_all; use near_network::test_utils::{wait_or_panic, WaitOrTimeout}; use near_primitives::crypto::signer::InMemorySigner; -use near_primitives::serialize::{to_base64, Encode}; +use near_primitives::serialize::to_base64; use near_primitives::test_utils::init_test_logger; use near_primitives::transaction::{FinalTransactionStatus, SignedTransaction}; @@ -30,7 +31,7 @@ fn test_send_tx_async() { ); let tx_hash: String = (&tx.get_hash()).into(); let tx_hash2 = tx_hash.clone(); - let bytes = tx.encode().unwrap(); + let bytes = tx.to_vec().unwrap(); actix::spawn( client .broadcast_tx_async(to_base64(&bytes)) @@ -74,7 +75,7 @@ fn test_send_tx_commit() { Arc::new(signer), 100, ); - let bytes = tx.encode().unwrap(); + let bytes = tx.to_vec().unwrap(); actix::spawn( client .broadcast_tx_commit(to_base64(&bytes)) diff --git a/chain/network/Cargo.toml b/chain/network/Cargo.toml index 30b9aece9f9..f92f1cc17db 100644 --- a/chain/network/Cargo.toml +++ b/chain/network/Cargo.toml @@ -15,6 +15,7 @@ serde = "1.0" serde_derive = "1.0" rand = "0.6.5" +nbor = { path = "../../core/nbor" } near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } near-chain = { path = "../chain" } diff --git a/chain/network/src/codec.rs b/chain/network/src/codec.rs index 2bb6f634ddd..ecf273362b6 100644 --- a/chain/network/src/codec.rs +++ b/chain/network/src/codec.rs @@ -3,8 +3,9 @@ use std::io::{Error, ErrorKind}; use bytes::{BufMut, BytesMut}; use tokio::codec::{Decoder, Encoder}; +use nbor::{Deserializable, Serializable}; + use crate::types::PeerMessage; -use near_primitives::serialize::{Decode, Encode}; pub struct Codec { max_length: u32, @@ -57,13 +58,11 @@ impl Decoder for Codec { } pub fn peer_message_to_bytes(peer_message: PeerMessage) -> Result, std::io::Error> { - // MOO - Encode::encode(&peer_message) + peer_message.to_vec() } pub fn bytes_to_peer_message(bytes: &[u8]) -> Result { - // MOO - Decode::decode(bytes) + PeerMessage::from_slice(bytes) } #[cfg(test)] diff --git a/chain/network/src/peer_manager.rs b/chain/network/src/peer_manager.rs index 0dbaaaae6a9..ae6972b202e 100644 --- a/chain/network/src/peer_manager.rs +++ b/chain/network/src/peer_manager.rs @@ -20,6 +20,7 @@ use tokio::io::AsyncRead; use tokio::net::{TcpListener, TcpStream}; use near_primitives::types::AccountId; +use near_primitives::utils::from_timestamp; use near_store::Store; use crate::codec::Codec; @@ -330,7 +331,7 @@ impl PeerManagerActor { match peer_state.status { KnownPeerStatus::Banned(_, last_banned) => { let interval = unwrap_or_error!( - (Utc::now() - last_banned).to_std(), + (Utc::now() - from_timestamp(last_banned)).to_std(), "Failed to convert time" ); if interval > self.config.ban_window { diff --git a/chain/network/src/peer_store.rs b/chain/network/src/peer_store.rs index 904db00daf4..186b3687aad 100644 --- a/chain/network/src/peer_store.rs +++ b/chain/network/src/peer_store.rs @@ -32,7 +32,7 @@ impl PeerStore { let mut peer_state: KnownPeerState = value.try_into()?; match peer_state.status { KnownPeerStatus::Banned(_, _) => {} - _ => peer_state.status = KnownPeerStatus::NotConnected + _ => peer_state.status = KnownPeerStatus::NotConnected, }; peer_states.insert(peer_id, peer_state); } @@ -56,7 +56,7 @@ impl PeerStore { .peer_states .entry(peer_info.peer_info.id) .or_insert(KnownPeerState::new(peer_info.peer_info.clone())); - entry.last_seen = Utc::now(); + entry.last_seen = Utc::now().timestamp_millis() as u64; entry.status = KnownPeerStatus::Connected; let mut store_update = self.store.store_update(); store_update.set_ser(COL_PEERS, peer_info.peer_info.id.as_ref(), entry)?; @@ -68,7 +68,7 @@ impl PeerStore { peer_id: &PeerId, ) -> Result<(), Box> { if let Some(peer_state) = self.peer_states.get_mut(peer_id) { - peer_state.last_seen = Utc::now(); + peer_state.last_seen = Utc::now().timestamp_millis() as u64; peer_state.status = KnownPeerStatus::NotConnected; let mut store_update = self.store.store_update(); store_update.set_ser(COL_PEERS, peer_id.as_ref(), peer_state)?; @@ -84,8 +84,9 @@ impl PeerStore { ban_reason: ReasonForBan, ) -> Result<(), Box> { if let Some(peer_state) = self.peer_states.get_mut(peer_id) { - peer_state.last_seen = Utc::now(); - peer_state.status = KnownPeerStatus::Banned(ban_reason, Utc::now()); + peer_state.last_seen = Utc::now().timestamp_millis() as u64; + peer_state.status = + KnownPeerStatus::Banned(ban_reason, Utc::now().timestamp_millis() as u64); let mut store_update = self.store.store_update(); store_update.set_ser(COL_PEERS, peer_id.as_ref(), peer_state)?; store_update.commit().map_err(|err| err.into()) @@ -157,7 +158,7 @@ impl PeerStore { let now = Utc::now(); let mut to_remove = vec![]; for (peer_id, peer_status) in self.peer_states.iter() { - let diff = (now - peer_status.last_seen).to_std()?; + let diff = (now - peer_status.last_seen()).to_std()?; if peer_status.status != KnownPeerStatus::Connected && diff > config.peer_expiration_duration { @@ -186,15 +187,11 @@ impl PeerStore { mod test { extern crate tempdir; use super::*; - use near_store::{create_store}; use near_primitives::crypto::signature::get_key_pair; + use near_store::create_store; fn gen_peer_info() -> PeerInfo { - PeerInfo{ - id: PeerId::from(get_key_pair().0), - addr: None, - account_id: None, - } + PeerInfo { id: PeerId::from(get_key_pair().0), addr: None, account_id: None } } #[test] diff --git a/chain/network/src/types.rs b/chain/network/src/types.rs index 1840072b9b6..15bda8546f4 100644 --- a/chain/network/src/types.rs +++ b/chain/network/src/types.rs @@ -9,25 +9,26 @@ use std::time::Duration; use actix::dev::{MessageResponse, ResponseChannel}; use actix::{Actor, Addr, Message}; use chrono::{DateTime, Utc}; -use serde_derive::{Deserialize, Serialize}; use tokio::net::TcpStream; +use nbor::{nbor, Deserializable}; use near_chain::{Block, BlockApproval, BlockHeader, Weight}; use near_primitives::crypto::signature::{sign, PublicKey, SecretKey, Signature}; use near_primitives::hash::{hash, CryptoHash}; use near_primitives::logging::pretty_str; use near_primitives::receipt::Receipt; -use near_primitives::serialize::{BaseEncode, Decode}; +use near_primitives::serialize::BaseEncode; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, BlockIndex, ShardId}; use crate::peer::Peer; +use near_primitives::utils::from_timestamp; /// Current latest version of the protocol pub const PROTOCOL_VERSION: u32 = 2; /// Peer id is the public key. -#[derive(Copy, Clone, Eq, PartialOrd, Ord, PartialEq, Serialize, Deserialize)] +#[derive(nbor, Copy, Clone, Eq, PartialOrd, Ord, PartialEq)] pub struct PeerId(PublicKey); impl PeerId { @@ -81,7 +82,7 @@ impl fmt::Debug for PeerId { } /// Peer information. -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[derive(nbor, Clone, Debug, Eq, PartialEq)] pub struct PeerInfo { pub id: PeerId, pub addr: Option, @@ -131,7 +132,7 @@ impl TryFrom<&str> for PeerInfo { } /// Peer chain information. -#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)] +#[derive(nbor, Copy, Clone, Debug, Eq, PartialEq, Default)] pub struct PeerChainInfo { /// Genesis hash. pub genesis: CryptoHash, @@ -161,7 +162,7 @@ pub enum PeerStatus { Banned(ReasonForBan), } -#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] +#[derive(nbor, PartialEq, Eq, Clone, Debug)] pub struct Handshake { /// Protocol version. pub version: u32, @@ -180,7 +181,7 @@ impl Handshake { } /// Account route description -#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] +#[derive(nbor, PartialEq, Eq, Clone, Debug)] pub struct AnnounceAccountRoute { pub peer_id: PeerId, pub hash: CryptoHash, @@ -188,7 +189,7 @@ pub struct AnnounceAccountRoute { } /// Account announcement information -#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] +#[derive(nbor, PartialEq, Eq, Clone, Debug)] pub struct AnnounceAccount { /// AccountId to be announced pub account_id: AccountId, @@ -254,7 +255,7 @@ impl AnnounceAccount { } } -#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] +#[derive(nbor, PartialEq, Eq, Clone, Debug)] pub enum PeerMessage { Handshake(Handshake), @@ -320,21 +321,21 @@ pub struct NetworkConfig { } /// Status of the known peers. -#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)] +#[derive(nbor, Eq, PartialEq, Debug)] pub enum KnownPeerStatus { Unknown, NotConnected, Connected, - Banned(ReasonForBan, DateTime), + Banned(ReasonForBan, u64), } /// Information node stores about known peers. -#[derive(Serialize, Deserialize, Debug)] +#[derive(nbor, Debug)] pub struct KnownPeerState { pub peer_info: PeerInfo, pub status: KnownPeerStatus, - pub first_seen: DateTime, - pub last_seen: DateTime, + pub first_seen: u64, + pub last_seen: u64, } impl KnownPeerState { @@ -342,17 +343,25 @@ impl KnownPeerState { KnownPeerState { peer_info, status: KnownPeerStatus::Unknown, - first_seen: Utc::now(), - last_seen: Utc::now(), + first_seen: Utc::now().timestamp_millis() as u64, + last_seen: Utc::now().timestamp_millis() as u64, } } + + pub fn first_seen(&self) -> DateTime { + from_timestamp(self.first_seen) + } + + pub fn last_seen(&self) -> DateTime { + from_timestamp(self.last_seen) + } } impl TryFrom> for KnownPeerState { type Error = Box; fn try_from(bytes: Vec) -> Result { - Decode::decode(&bytes).map_err(|err| err.into()) + KnownPeerState::from_slice(&bytes).map_err(|err| err.into()) } } @@ -431,7 +440,7 @@ where } /// Ban reason. -#[derive(Debug, Clone, PartialEq, Eq, Copy, Serialize, Deserialize)] +#[derive(nbor, Debug, Clone, PartialEq, Eq, Copy)] pub enum ReasonForBan { None = 0, BadBlock = 1, diff --git a/core/nbor-derive/src/lib.rs b/core/nbor-derive/src/lib.rs index c6ae22d7769..af965f67659 100644 --- a/core/nbor-derive/src/lib.rs +++ b/core/nbor-derive/src/lib.rs @@ -280,7 +280,7 @@ fn nbor_enum_de(input: &ItemEnum) -> TokenStream2 { #variant_idx let mut return_value = match variant_idx { #variant_arms - _ => panic!("Unexpeted variant index"), + _ => panic!(format!("Unexpeted variant index: {:?}", variant_idx)), }; return_value.#method_ident(); Ok(return_value) @@ -294,7 +294,7 @@ fn nbor_enum_de(input: &ItemEnum) -> TokenStream2 { #variant_idx let return_value = match variant_idx { #variant_arms - _ => panic!("Unexpeted variant index"), + _ => panic!(format!("Unexpeted variant index: {:?}", variant_idx)), }; Ok(return_value) } diff --git a/core/nbor/Cargo.toml b/core/nbor/Cargo.toml index 7e083e9a04b..457e1c531ea 100644 --- a/core/nbor/Cargo.toml +++ b/core/nbor/Cargo.toml @@ -8,3 +8,10 @@ description = """Near Binary Object Representation [dependencies] nbor-derive = { path = "../nbor-derive" } + +[features] +default = ["std"] + +# Provides impls for common standard library types like Vec. +# Requires a dependency on the Rust standard library. +std = [] diff --git a/core/nbor/src/de/mod.rs b/core/nbor/src/de/mod.rs index 3087a04b780..e097aa671c6 100644 --- a/core/nbor/src/de/mod.rs +++ b/core/nbor/src/de/mod.rs @@ -1,3 +1,4 @@ +use std::collections::{BTreeMap, HashMap, HashSet}; use std::io::{Cursor, Error, Read}; use std::mem::size_of; @@ -59,6 +60,14 @@ macro_rules! impl_for_float { impl_for_float!(f32, u32); impl_for_float!(f64, u64); +impl Deserializable for bool { + fn read(reader: &mut R) -> Result { + let mut buf = [0u8]; + reader.read(&mut buf)?; + Ok(buf[0] == 1) + } +} + impl Deserializable for Option where T: Deserializable, @@ -73,3 +82,123 @@ where } } } + +impl Deserializable for String { + fn read(reader: &mut R) -> Result { + let len = u32::read(reader)?; + let mut result = vec![0; len as usize]; + reader.read_exact(&mut result)?; + String::from_utf8(result) + .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string())) + } +} + +#[cfg(feature = "std")] +impl Deserializable for Vec +where + T: Deserializable, +{ + fn read(reader: &mut R) -> Result { + let len = u32::read(reader)?; + let mut result = vec![]; + for _ in 0..len { + result.push(T::read(reader)?); + } + Ok(result) + } +} + +#[cfg(feature = "std")] +impl Deserializable for HashSet +where + T: Deserializable + Eq + std::hash::Hash, +{ + fn read(reader: &mut R) -> Result { + let mut vec = >::read(reader)?; + Ok(vec.drain(..).collect::>()) + } +} + +#[cfg(feature = "std")] +impl Deserializable for HashMap +where + K: Deserializable + Eq + std::hash::Hash, + V: Deserializable, +{ + fn read(reader: &mut R) -> Result { + let len = u32::read(reader)?; + let mut result = HashMap::default(); + for _ in 0..len { + let key = K::read(reader)?; + let value = V::read(reader)?; + result.insert(key, value); + } + Ok(result) + } +} + +#[cfg(feature = "std")] +impl Deserializable for BTreeMap +where + K: Deserializable + Ord + std::hash::Hash, + V: Deserializable, +{ + fn read(reader: &mut R) -> Result { + let len = u32::read(reader)?; + let mut result = BTreeMap::default(); + for _ in 0..len { + let key = K::read(reader)?; + let value = V::read(reader)?; + result.insert(key, value); + } + Ok(result) + } +} + +#[cfg(feature = "std")] +impl Deserializable for std::net::SocketAddr { + fn read(reader: &mut R) -> Result { + let kind = u8::read(reader)?; + match kind { + 0 => std::net::SocketAddrV4::read(reader).map(|addr| std::net::SocketAddr::V4(addr)), + 1 => std::net::SocketAddrV6::read(reader).map(|addr| std::net::SocketAddr::V6(addr)), + value => panic!(format!("Invalid SocketAddr variant: {}", value)), + } + } +} + +#[cfg(feature = "std")] +impl Deserializable for std::net::SocketAddrV4 { + fn read(reader: &mut R) -> Result { + let ip = std::net::Ipv4Addr::read(reader)?; + let port = u16::read(reader)?; + Ok(std::net::SocketAddrV4::new(ip, port)) + } +} + +#[cfg(feature = "std")] +impl Deserializable for std::net::SocketAddrV6 { + fn read(reader: &mut R) -> Result { + let ip = std::net::Ipv6Addr::read(reader)?; + let port = u16::read(reader)?; + Ok(std::net::SocketAddrV6::new(ip, port, 0, 0)) + } +} + +#[cfg(feature = "std")] +impl Deserializable for std::net::Ipv4Addr { + fn read(reader: &mut R) -> Result { + let mut buf = [0u8; 4]; + reader.read(&mut buf)?; + Ok(std::net::Ipv4Addr::from(buf)) + } +} + +#[cfg(feature = "std")] +impl Deserializable for std::net::Ipv6Addr { + fn read(reader: &mut R) -> Result { + let mut buf = [0u8; 16]; + reader.read(&mut buf)?; + Ok(std::net::Ipv6Addr::from(buf)) + } +} diff --git a/core/nbor/src/ser/mod.rs b/core/nbor/src/ser/mod.rs index 8d552fe0ad2..234e89cfce7 100644 --- a/core/nbor/src/ser/mod.rs +++ b/core/nbor/src/ser/mod.rs @@ -1,3 +1,4 @@ +use std::collections::{BTreeMap, HashMap, HashSet}; use std::io::{Error, Write}; /// A data-structure that can be serialized into binary format by NBOR. @@ -54,17 +55,141 @@ macro_rules! impl_for_float { impl_for_float!(f32); impl_for_float!(f64); +impl Serializable for bool { + fn write(&self, writer: &mut W) -> Result<(), Error> { + writer.write(if *self { &[0u8] } else { &[1u8] }).map(|_| ()) + } +} + impl Serializable for Option where T: Serializable, { fn write(&self, writer: &mut W) -> Result<(), Error> { match self { - None => writer.write(&[0u8]).map(|_| ()), + None => 0u8.write(writer).map(|_| ()), Some(value) => { - writer.write(&[1u8])?; + 1u8.write(writer)?; value.write(writer) } } } } + +impl Serializable for String { + fn write(&self, writer: &mut W) -> Result<(), Error> { + (self.len() as u32).write(writer)?; + writer.write(self.as_bytes())?; + Ok(()) + } +} + +#[cfg(feature = "std")] +impl Serializable for Vec +where + T: Serializable, +{ + fn write(&self, writer: &mut W) -> Result<(), Error> { + (self.len() as u32).write(writer)?; + for item in self { + item.write(writer)?; + } + Ok(()) + } +} + +#[cfg(feature = "std")] +impl Serializable for HashSet +where + T: Serializable + PartialOrd, +{ + fn write(&self, writer: &mut W) -> Result<(), Error> { + let mut vec = self.iter().collect::>(); + vec.sort_by(|a, b| a.partial_cmp(b).unwrap()); + (vec.len() as u32).write(writer)?; + for item in vec { + item.write(writer)?; + } + Ok(()) + } +} + +#[cfg(feature = "std")] +impl Serializable for HashMap +where + K: Serializable + PartialOrd, + V: Serializable, +{ + fn write(&self, writer: &mut W) -> Result<(), Error> { + let mut vec = self.iter().collect::>(); + vec.sort_by(|(a, _), (b, _)| a.partial_cmp(b).unwrap()); + (vec.len() as u32).write(writer)?; + for (key, value) in vec { + key.write(writer)?; + value.write(writer)?; + } + Ok(()) + } +} + +#[cfg(feature = "std")] +impl Serializable for BTreeMap +where + K: Serializable + PartialOrd, + V: Serializable, +{ + fn write(&self, writer: &mut W) -> Result<(), Error> { + (self.len() as u32).write(writer)?; + for (key, value) in self.iter() { + key.write(writer)?; + value.write(writer)?; + } + Ok(()) + } +} + +#[cfg(feature = "std")] +impl Serializable for std::net::SocketAddr { + fn write(&self, writer: &mut W) -> Result<(), Error> { + match *self { + std::net::SocketAddr::V4(ref addr) => { + 0u8.write(writer)?; + addr.write(writer) + } + std::net::SocketAddr::V6(ref addr) => { + 1u8.write(writer)?; + addr.write(writer) + } + } + } +} + +#[cfg(feature = "std")] +impl Serializable for std::net::SocketAddrV4 { + fn write(&self, writer: &mut W) -> Result<(), Error> { + self.ip().write(writer)?; + self.port().write(writer).map(|_| ()) + } +} + +#[cfg(feature = "std")] +impl Serializable for std::net::SocketAddrV6 { + fn write(&self, writer: &mut W) -> Result<(), Error> { + self.ip().write(writer)?; + self.port().write(writer).map(|_| ()) + } +} + +#[cfg(feature = "std")] +impl Serializable for std::net::Ipv4Addr { + fn write(&self, writer: &mut W) -> Result<(), Error> { + writer.write(&self.octets()).map(|_| ()) + } +} + +#[cfg(feature = "std")] +impl Serializable for std::net::Ipv6Addr { + fn write(&self, writer: &mut W) -> Result<(), Error> { + writer.write(&self.octets()).map(|_| ()) + } +} diff --git a/core/nbor/tests/test_simple_structs.rs b/core/nbor/tests/test_simple_structs.rs index cef37efb3d2..3177d1d148c 100644 --- a/core/nbor/tests/test_simple_structs.rs +++ b/core/nbor/tests/test_simple_structs.rs @@ -4,8 +4,10 @@ use nbor::{nbor, Deserializable, Serializable}; #[nbor_init(init)] struct A { x: u64, - b: B, + // b: B, y: f32, + z: String, + v: Vec, lazy: Option, #[nbor_skip] skipped: Option, @@ -44,8 +46,10 @@ struct D { fn test_simple_struct() { let a = A { x: 1, - b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, + // b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, y: 4.0, + z: "123".to_string(), + v: vec!["qwe".to_string(), "zxc".to_string()], lazy: Some(5), skipped: Some(6), }; @@ -53,8 +57,10 @@ fn test_simple_struct() { let decoded_a = A::from_slice(&encoded_a).unwrap(); let expected_a = A { x: 1, - b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, + // b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, y: 4.0, + z: a.z, + v: a.v, lazy: Some(50), skipped: None, }; diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index 1f53fe7cb51..7a3efbaf0f6 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -26,6 +26,8 @@ log = "0.4" reed-solomon-erasure = "3.1.1" jemallocator = { version = "0.3.0", optional = true } +nbor = { path = "../nbor" } + [features] default = ["jemallocator"] diff --git a/core/primitives/benches/serialization.rs b/core/primitives/benches/serialization.rs index 4f81397dc0f..08f5c7ed8ea 100644 --- a/core/primitives/benches/serialization.rs +++ b/core/primitives/benches/serialization.rs @@ -12,7 +12,8 @@ use near_primitives::block::Block; use near_primitives::crypto::signature::{PublicKey, DEFAULT_SIGNATURE}; use near_primitives::crypto::signer::InMemorySigner; use near_primitives::hash::CryptoHash; -use near_primitives::serialize::{Decode, Encode}; +//use near_primitives::serialize::{Decode, Encode}; +use nbor::{Deserializable, Serializable}; use near_primitives::transaction::{Action, SignedTransaction, Transaction, TransferAction}; use near_primitives::types::MerkleHash; @@ -56,50 +57,50 @@ fn create_account() -> Account { fn serialize_tx(bench: &mut Bencher) { let t = create_transaction(); bench.iter(|| { - let bytes = t.encode().unwrap(); + let bytes = t.to_vec().unwrap(); assert!(bytes.len() > 0); }); } fn deserialize_tx(bench: &mut Bencher) { let t = create_transaction(); - let bytes = t.encode().unwrap(); + let bytes = t.to_vec().unwrap(); bench.iter(|| { - let nt = SignedTransaction::decode(&bytes).unwrap(); - // assert_eq!(nt, t); + let nt = SignedTransaction::from_slice(&bytes).unwrap(); + assert_eq!(nt, t); }); } fn serialize_block(bench: &mut Bencher) { let b = create_block(); bench.iter(|| { - let bytes = b.encode().unwrap(); + let bytes = b.to_vec().unwrap(); assert!(bytes.len() > 0); }); } fn deserialize_block(bench: &mut Bencher) { let b = create_block(); - let bytes = b.encode().unwrap(); + let bytes = b.to_vec().unwrap(); bench.iter(|| { - let nb = Block::decode(&bytes).unwrap(); - // assert_eq!(nb, b); + let nb = Block::from_slice(&bytes).unwrap(); + assert_eq!(nb, b); }); } fn serialize_account(bench: &mut Bencher) { let acc = create_account(); bench.iter(|| { - let bytes = acc.encode().unwrap(); + let bytes = acc.to_vec().unwrap(); assert!(bytes.len() > 0); }); } fn deserialize_account(bench: &mut Bencher) { let acc = create_account(); - let bytes = acc.encode().unwrap(); + let bytes = acc.to_vec().unwrap(); bench.iter(|| { - let nacc = Account::decode(&bytes).unwrap(); + let nacc = Account::from_slice(&bytes).unwrap(); assert_eq!(nacc, acc); }); } diff --git a/core/primitives/src/account.rs b/core/primitives/src/account.rs index d44155a6267..c9856d045a2 100644 --- a/core/primitives/src/account.rs +++ b/core/primitives/src/account.rs @@ -1,13 +1,14 @@ use std::fmt; +use nbor::nbor; + use crate::crypto::signature::PublicKey; use crate::hash::CryptoHash; use crate::logging; -use crate::serialize::{Encode, EncodeResult}; use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage}; /// Per account information stored in the state. -#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] +#[derive(nbor, PartialEq, Eq, Debug, Clone)] pub struct Account { pub public_keys: Vec, pub nonce: Nonce, @@ -54,7 +55,7 @@ impl Account { /// Limited Access key to use owner's account with the fixed public_key. /// Access Key is stored under the key of owner's `account_id` and the `public_key`. -#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone)] +#[derive(nbor, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)] pub struct AccessKey { /// Balance amount on this Access Key. Can be used to pay for the transactions. pub amount: Balance, diff --git a/core/primitives/src/block.rs b/core/primitives/src/block.rs index 45565afd520..cce2edff79b 100644 --- a/core/primitives/src/block.rs +++ b/core/primitives/src/block.rs @@ -2,17 +2,17 @@ use std::collections::HashMap; use std::sync::Arc; use chrono::prelude::{DateTime, Utc}; -use chrono::serde::ts_nanoseconds; + +use nbor::{nbor, Serializable}; use crate::crypto::signature::{verify, PublicKey, Signature, DEFAULT_SIGNATURE}; use crate::crypto::signer::EDSigner; use crate::hash::{hash, CryptoHash}; -use crate::serialize::{vec_base_format, Encode}; use crate::transaction::SignedTransaction; use crate::types::{BlockIndex, MerkleHash, ValidatorStake}; -use serde::{Deserialize, Deserializer}; +use crate::utils::from_timestamp; -#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] +#[derive(nbor, Debug, Clone, Eq, PartialEq)] pub struct BlockHeaderInner { /// Height of this block since the genesis block (height 0). pub height: BlockIndex, @@ -26,8 +26,7 @@ pub struct BlockHeaderInner { /// Root hash of the transactions in the given block. pub tx_root: MerkleHash, /// Timestamp at which the block was built. - #[serde(with = "ts_nanoseconds")] - pub timestamp: DateTime, + pub timestamp: u64, /// Approval mask, given current block producers. pub approval_mask: Vec, /// Approval signatures. @@ -57,7 +56,7 @@ impl BlockHeaderInner { prev_hash, prev_state_root, tx_root, - timestamp, + timestamp: timestamp.timestamp_millis() as u64, approval_mask, approval_sigs, total_weight, @@ -66,7 +65,8 @@ impl BlockHeaderInner { } } -#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] +#[derive(nbor, Debug, Clone, Eq, PartialEq)] +#[nbor_init(init)] pub struct BlockHeader { /// Inner part of the block header that gets hashed. pub inner: BlockHeaderInner, @@ -75,11 +75,15 @@ pub struct BlockHeader { pub signature: Signature, /// Cached value of hash for this block. - #[serde(skip)] - hash: CryptoHash, + #[nbor_skip] + pub hash: CryptoHash, } impl BlockHeader { + pub fn init(&mut self) { + self.hash = hash(&self.inner.to_vec().expect("Failed to serialize")); + } + pub fn new( height: BlockIndex, prev_hash: CryptoHash, @@ -105,9 +109,8 @@ impl BlockHeader { total_weight, validator_proposal, ); - let bytes = inner.encode().expect("Failed to serialze block header"); - let hash = hash(&bytes); - Self { inner, signature: signer.sign(hash.as_ref()).into(), hash } + let hash = hash(&inner.to_vec().expect("Failed to serialize")); + Self { inner, signature: signer.sign(hash.as_ref()), hash } } pub fn genesis(state_root: MerkleHash, timestamp: DateTime) -> Self { @@ -123,9 +126,8 @@ impl BlockHeader { 0.into(), vec![], ); - let bytes = inner.encode().expect("Failed to serialze block header"); - let hash = hash(&bytes); - Self { inner, signature: DEFAULT_SIGNATURE.into(), hash } + let hash = hash(&inner.to_vec().expect("Failed to serialize")); + Self { inner, signature: DEFAULT_SIGNATURE, hash } } pub fn hash(&self) -> CryptoHash { @@ -136,23 +138,13 @@ impl BlockHeader { pub fn verify_block_producer(&self, public_key: &PublicKey) -> bool { verify(self.hash.as_ref(), &self.signature, public_key) } + + pub fn timestamp(&self) -> DateTime { + from_timestamp(self.inner.timestamp) + } } -//impl<'de> Deserialize<'de> for BlockHeader { -// fn deserialize(deserializer: D) -> Result>::Error> -// where -// D: Deserializer<'de>, -// { -// let mut header = BlockHeader::deserialize(deserializer)?; -// header.hash = hash(&header.inner.encode().expect("Failed serializing header")); -// Ok(header) -// } -//} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Bytes(Vec); - -#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] +#[derive(nbor, Debug, Clone, Eq, PartialEq)] pub struct Block { pub header: BlockHeader, pub transactions: Vec, @@ -225,7 +217,7 @@ impl Block { } /// The weight is defined as the number of unique validators approving this fork. -#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Serialize, Deserialize, Default)] +#[derive(nbor, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Default)] pub struct Weight { num: u64, } diff --git a/core/primitives/src/crypto/signature.rs b/core/primitives/src/crypto/signature.rs index 46bc00af00b..ba4b70f71f0 100644 --- a/core/primitives/src/crypto/signature.rs +++ b/core/primitives/src/crypto/signature.rs @@ -3,13 +3,13 @@ extern crate exonum_sodiumoxide as sodiumoxide; use std::convert::TryFrom; use std::fmt; use std::hash::{Hash, Hasher}; +use std::io::Read; pub use exonum_sodiumoxide::crypto::sign::ed25519::Seed; use crate::logging::pretty_hash; -use crate::serialize::{from_base, to_base, BaseDecode, BaseEncode, Writable, WritableResult}; +use crate::serialize::{from_base, to_base, BaseDecode}; use crate::types::ReadablePublicKey; -use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialOrd, Ord, PartialEq)] pub struct PublicKey(pub sodiumoxide::crypto::sign::ed25519::PublicKey); @@ -17,7 +17,7 @@ pub struct PublicKey(pub sodiumoxide::crypto::sign::ed25519::PublicKey); #[derive(Serialize, Deserialize, Clone, Eq, PartialEq)] pub struct SecretKey(pub sodiumoxide::crypto::sign::ed25519::SecretKey); -#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Hash)] +#[derive(Clone, Eq, PartialEq, Hash)] pub struct Signature(pub sodiumoxide::crypto::sign::ed25519::Signature); pub fn sign(data: &[u8], secret_key: &SecretKey) -> Signature { @@ -119,15 +119,27 @@ impl TryFrom<&str> for PublicKey { } } -impl Writable for PublicKey { - fn write_into(&self, out: &mut Vec) -> WritableResult { - // 1 byte for kind. - out.extend_from_slice(&vec![0]); - out.extend_from_slice(&(self.0).0); +impl nbor::Serializable for PublicKey { + fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { + writer.write(&vec![0])?; + writer.write(&(self.0).0)?; Ok(()) } } +impl nbor::Deserializable for PublicKey { + fn read(reader: &mut R) -> Result { + let mut bytes = [0; sodiumoxide::crypto::sign::ed25519::PUBLICKEYBYTES + 1]; + reader.read(&mut bytes)?; + // TODO: support other curves here. + assert_eq!(bytes[0], 0); + let mut array = [0; sodiumoxide::crypto::sign::ed25519::PUBLICKEYBYTES]; + array.copy_from_slice(&bytes[1..]); + let public_key = sodiumoxide::crypto::sign::ed25519::PublicKey(array); + Ok(PublicKey(public_key)) + } +} + impl TryFrom<&[u8]> for SecretKey { type Error = Box; @@ -201,6 +213,21 @@ impl TryFrom<&str> for Signature { } } +impl nbor::Serializable for Signature { + fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { + writer.write(&(self.0).0)?; + Ok(()) + } +} + +impl nbor::Deserializable for Signature { + fn read(reader: &mut R) -> Result { + let mut bytes = [0; sodiumoxide::crypto::sign::ed25519::SIGNATUREBYTES]; + reader.read(&mut bytes)?; + Ok(Signature(sodiumoxide::crypto::sign::ed25519::Signature(bytes))) + } +} + impl std::convert::AsRef<[u8]> for PublicKey { fn as_ref(&self) -> &[u8] { &self.0[..] diff --git a/core/primitives/src/hash.rs b/core/primitives/src/hash.rs index c996a874121..c7887c43987 100644 --- a/core/primitives/src/hash.rs +++ b/core/primitives/src/hash.rs @@ -6,8 +6,9 @@ use exonum_sodiumoxide as sodiumoxide; use exonum_sodiumoxide::crypto::hash::sha256::Digest; use crate::logging::pretty_hash; -use crate::serialize::{from_base, to_base, BaseDecode, BaseEncode, Encode}; +use crate::serialize::{from_base, to_base, BaseDecode, Encode}; use serde::{Deserialize, Serialize}; +use std::io::Read; #[derive(Copy, Clone, PartialOrd, Ord, Serialize, Deserialize)] pub struct CryptoHash(pub Digest); @@ -47,6 +48,21 @@ impl AsMut<[u8]> for CryptoHash { impl BaseDecode for CryptoHash {} +impl nbor::Serializable for CryptoHash { + fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { + writer.write(&(self.0).0)?; + Ok(()) + } +} + +impl nbor::Deserializable for CryptoHash { + fn read(reader: &mut R) -> Result { + let mut bytes = [0; 32]; + reader.read(&mut bytes)?; + Ok(CryptoHash(Digest(bytes))) + } +} + impl TryFrom<&[u8]> for CryptoHash { type Error = Box; diff --git a/core/primitives/src/receipt.rs b/core/primitives/src/receipt.rs index 9e9a1e6afbe..a9b1aa048da 100644 --- a/core/primitives/src/receipt.rs +++ b/core/primitives/src/receipt.rs @@ -1,22 +1,23 @@ use std::borrow::Borrow; use std::fmt; +use nbor::nbor; + use crate::crypto::signature::PublicKey; use crate::hash::CryptoHash; use crate::logging; -use crate::serialize::{option_bytes_format, u128_dec_format}; use crate::transaction::{Action, TransactionResult, TransferAction}; use crate::types::{AccountId, Balance}; use crate::utils::system_account; -#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct ReceiptInfo { pub receipt: Receipt, pub block_index: u64, pub result: TransactionResult, } -#[derive(Hash, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] +#[derive(nbor, Hash, Debug, PartialEq, Eq, Clone)] pub struct Receipt { pub predecessor_id: AccountId, pub receiver_id: AccountId, @@ -56,18 +57,17 @@ impl Receipt { } } -#[derive(Hash, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] +#[derive(nbor, Hash, Clone, Debug, PartialEq, Eq)] pub enum ReceiptEnum { Action(ActionReceipt), Data(DataReceipt), } -#[derive(Hash, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] +#[derive(nbor, Hash, Debug, PartialEq, Eq, Clone)] pub struct ActionReceipt { pub signer_id: AccountId, pub signer_public_key: PublicKey, - #[serde(with = "u128_dec_format")] pub gas_price: Balance, /// If present, where to route the output data @@ -78,16 +78,15 @@ pub struct ActionReceipt { pub actions: Vec, } -#[derive(Hash, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] +#[derive(nbor, Hash, Clone, Debug, PartialEq, Eq)] pub struct DataReceiver { pub data_id: CryptoHash, pub receiver_id: AccountId, } -#[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[derive(nbor, Hash, PartialEq, Eq, Clone)] pub struct DataReceipt { pub data_id: CryptoHash, - #[serde(with = "option_bytes_format")] pub data: Option>, } diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index e8d94c661b0..64904f12707 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -5,15 +5,13 @@ use chrono::{DateTime, Utc}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::account::{AccessKey, Account}; -use crate::block::BlockHeader; +use crate::block::{Block, BlockHeader, BlockHeaderInner}; use crate::crypto::signature::{PublicKey, SecretKey, Signature}; use crate::hash::CryptoHash; -use crate::serialize::{from_base, to_base, u128_dec_format, vec_base_format}; +use crate::serialize::{from_base, to_base, u128_dec_format}; +use crate::transaction::SignedTransaction; use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage, ValidatorStake, Version}; -/// Number of nano seconds in one second. -//const NS_IN_SECOND: u64 = 1_000_000_000; - #[derive(Debug, Eq, PartialEq, Clone)] pub struct PublicKeyView(Vec); @@ -44,6 +42,12 @@ impl From for PublicKeyView { } } +impl From for PublicKey { + fn from(view: PublicKeyView) -> Self { + Self::try_from(view.0).expect("Failed to get PublicKey from PublicKeyView") + } +} + #[derive(Debug)] pub struct SecretKeyView(Vec); @@ -74,7 +78,7 @@ impl From for SecretKeyView { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct SignatureView(Vec); impl Serialize for SignatureView { @@ -104,7 +108,13 @@ impl From for SignatureView { } } -#[derive(Debug, PartialEq, Eq)] +impl From for Signature { + fn from(view: SignatureView) -> Self { + Signature::try_from(view.0).expect("Failed to get Signature from SignatureView") + } +} + +#[derive(Debug, PartialEq, Eq, Clone)] pub struct CryptoHashView(Vec); impl Serialize for CryptoHashView { @@ -134,8 +144,14 @@ impl From for CryptoHashView { } } +impl From for CryptoHash { + fn from(view: CryptoHashView) -> Self { + CryptoHash::try_from(view.0).expect("Failed to convert CryptoHashView to CryptoHash") + } +} + /// A view of the account -#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] pub struct AccountView { pub public_keys: Vec, pub nonce: Nonce, @@ -166,7 +182,21 @@ impl From for AccountView { } } -#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] +impl From for Account { + fn from(mut view: AccountView) -> Self { + Self { + public_keys: view.public_keys.drain(..).map(|public_key| public_key.into()).collect(), + nonce: view.nonce, + amount: view.amount, + staked: view.staked, + code_hash: view.code_hash.into(), + storage_usage: view.storage_usage, + storage_paid_at: view.storage_paid_at, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] pub struct AccessKeyView { #[serde(with = "u128_dec_format")] pub amount: Balance, @@ -186,6 +216,17 @@ impl From for AccessKeyView { } } +impl From for AccessKey { + fn from(view: AccessKeyView) -> Self { + Self { + amount: view.amount, + balance_owner: view.balance_owner, + contract_id: view.contract_id, + method_name: view.method_name, + } + } +} + #[derive(Serialize, Deserialize, Debug)] pub struct ViewStateResult { pub values: HashMap, Vec>, @@ -284,7 +325,8 @@ impl TryFrom for Option { } } -struct BlockHeaderView { +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct BlockHeaderView { pub height: BlockIndex, pub epoch_hash: CryptoHash, pub prev_hash: CryptoHashView, @@ -295,7 +337,7 @@ struct BlockHeaderView { pub approval_sigs: Vec, pub total_weight: u64, pub validator_proposals: Vec, - pub signature: Signature, + pub signature: SignatureView, } impl From for BlockHeaderView { @@ -306,7 +348,7 @@ impl From for BlockHeaderView { prev_hash: header.inner.prev_hash.into(), prev_state_root: header.inner.prev_state_root.into(), tx_root: header.inner.tx_root.into(), - timestamp: header.inner.timestamp.timestamp() as u64, + timestamp: header.inner.timestamp, approval_mask: header.inner.approval_mask, approval_sigs: header .inner @@ -320,3 +362,55 @@ impl From for BlockHeaderView { } } } + +impl From for BlockHeader { + fn from(mut view: BlockHeaderView) -> Self { + let mut header = Self { + inner: BlockHeaderInner { + height: view.height, + epoch_hash: view.epoch_hash.into(), + prev_hash: view.prev_hash.into(), + prev_state_root: view.prev_state_root.into(), + tx_root: view.tx_root.into(), + timestamp: view.timestamp, + approval_mask: view.approval_mask, + approval_sigs: view + .approval_sigs + .drain(..) + .map(|signature| signature.into()) + .collect(), + total_weight: view.total_weight.into(), + validator_proposals: view.validator_proposals, + }, + signature: view.signature.into(), + hash: CryptoHash::default(), + }; + header.init(); + header + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct BlockView { + pub header: BlockHeaderView, + pub transactions: Vec, +} + +impl From for BlockView { + fn from(mut block: Block) -> Self { + BlockView { + header: block.header.into(), + transactions: block.transactions.drain(..).map(|tx| tx.into()).collect(), + } + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SignedTransactionView {} + +impl From for SignedTransactionView { + fn from(signed_tx: SignedTransaction) -> Self { + // MOO + SignedTransactionView {} + } +} diff --git a/core/primitives/src/serialize.rs b/core/primitives/src/serialize.rs index 1949eecf04e..c996a480346 100644 --- a/core/primitives/src/serialize.rs +++ b/core/primitives/src/serialize.rs @@ -1,109 +1,11 @@ use std::convert::TryFrom; use std::io; -use byteorder::{LittleEndian, ReadBytesExt}; use serde::{de::DeserializeOwned, Serialize}; -use std::io::Cursor; pub type EncodeResult = Result, io::Error>; pub type DecodeResult = Result; -pub type WritableResult = Result<(), std::io::Error>; -pub type ReadableResult = Result; - -pub trait Writable { - fn write(&self) -> Result, std::io::Error> { - let mut out = vec![]; - self.write_into(&mut out)?; - Ok(out) - } - fn write_into(&self, out: &mut Vec) -> WritableResult; -} - -macro_rules! impl_writable_for_uint { - ($type: ident) => { - impl Writable for $type { - fn write_into(&self, out: &mut Vec) -> WritableResult { - out.extend_from_slice(&self.to_le_bytes()); - Ok(()) - } - } - }; -} - -impl_writable_for_uint!(u8); -impl_writable_for_uint!(u16); -impl_writable_for_uint!(u32); -impl_writable_for_uint!(u64); -impl_writable_for_uint!(u128); - -impl Writable for String { - fn write_into(&self, out: &mut Vec) -> WritableResult { - (self.len() as u32).write_into(out)?; - out.extend_from_slice(self.as_bytes()); - Ok(()) - } -} - -impl Writable for Vec -where - T: Writable, -{ - fn write_into(&self, out: &mut Vec) -> WritableResult { - (self.len() as u32).write_into(out)?; - for item in self.iter() { - item.write_into(out)?; - } - Ok(()) - } -} - -pub trait Readable: Sized { - fn read(bytes: &[u8]) -> ReadableResult { - let mut cursor = Cursor::new(bytes); - Self::read_from_cursor(&mut cursor) - } - - fn read_from_cursor(cursor: &mut Cursor<&[u8]>) -> ReadableResult; -} - -macro_rules! impl_readable_for_primitive { - ($type: ident, $func: ident) => { - impl Readable for $type { - fn read_from_cursor(cursor: &mut Cursor<&[u8]>) -> ReadableResult { - cursor.$func() - } - } - }; - ($type: ident, $func: ident, $qual: ident) => { - impl Readable for $type { - fn read_from_cursor(cursor: &mut Cursor<&[u8]>) -> ReadableResult { - cursor.$func::<$qual>() - } - } - }; -} - -impl_readable_for_primitive!(u8, read_u8); -impl_readable_for_primitive!(u16, read_u16, LittleEndian); -impl_readable_for_primitive!(u32, read_u32, LittleEndian); -impl_readable_for_primitive!(u64, read_u64, LittleEndian); -impl_readable_for_primitive!(u128, read_u128, LittleEndian); - -impl Readable for Vec -where - T: Readable, -{ - fn read_from_cursor(mut cursor: &mut Cursor<&[u8]>) -> ReadableResult { - let len = u32::read_from_cursor(&mut cursor)?; - let mut result = vec![]; - for _ in 0..len { - result.push(T::read_from_cursor(&mut cursor)?); - } - Ok(result) - } -} - // encode a type to byte array pub trait Encode { fn encode(&self) -> EncodeResult; @@ -331,47 +233,12 @@ pub mod u128_dec_format { mod tests { use super::*; - #[derive(Debug, Eq, PartialEq)] - struct SomeStruct { - a: u8, - b: u32, - c: u128, - x: Vec, - } - #[derive(Deserialize, Serialize)] struct OptionBytesStruct { #[serde(with = "option_bytes_format")] data: Option>, } - impl Writable for SomeStruct { - fn write_into(&self, out: &mut Vec) -> WritableResult { - self.a.write_into(out)?; - self.b.write_into(out)?; - self.c.write_into(out)?; - self.x.write_into(out) - } - } - - impl Readable for SomeStruct { - fn read_from_cursor(mut cursor: &mut Cursor<&[u8]>) -> ReadableResult { - let a = u8::read_from_cursor(&mut cursor)?; - let b = u32::read_from_cursor(&mut cursor)?; - let c = u128::read_from_cursor(&mut cursor)?; - let x = >::read_from_cursor(&mut cursor)?; - Ok(Self { a, b, c, x }) - } - } - - #[test] - fn test_serialize_struct() { - let s = SomeStruct { a: 1, b: 2, c: 3, x: vec![1, 2, 3] }; - let bytes = s.write().unwrap(); - let ns = SomeStruct::read(&bytes).unwrap(); - assert_eq!(s, ns); - } - #[test] fn test_serialize_some() { let s = OptionBytesStruct { data: Some(vec![10, 20, 30]) }; diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index 06fcd7ed0ff..33350d77e2a 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -1,23 +1,19 @@ -use std::convert::{TryFrom, TryInto}; use std::fmt; use std::hash::{Hash, Hasher}; -use std::iter::FromIterator; +use std::sync::Arc; + +use nbor::{nbor, Serializable}; use crate::account::AccessKey; -use crate::crypto::signature::{verify, PublicKey, Signature}; +use crate::crypto::signature::{verify, PublicKey}; use crate::crypto::signer::EDSigner; use crate::hash::{hash, CryptoHash}; use crate::logging; -use crate::serialize::{ - base_bytes_format, u128_dec_format, Decode, DecodeResult, Encode, Writable, WritableResult, -}; use crate::types::{AccountId, Balance, Gas, Nonce, ShardId, StructSignature}; -use serde::{Deserialize, Deserializer}; -use std::sync::Arc; pub type LogEntry = String; -#[derive(Hash, PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] +#[derive(nbor, Hash, PartialEq, Eq, Debug, Clone)] pub struct Transaction { pub signer_id: AccountId, pub public_key: PublicKey, @@ -29,23 +25,12 @@ pub struct Transaction { impl Transaction { pub fn get_hash(&self) -> CryptoHash { - let bytes = self.write().expect("Failed to deserialize"); + let bytes = self.to_vec().expect("Failed to deserialize"); hash(&bytes) } } -impl Writable for Transaction { - fn write_into(&self, out: &mut Vec) -> WritableResult { - self.signer_id.write_into(out)?; - self.public_key.write_into(out)?; - self.nonce.write_into(out)?; - self.receiver_id.write_into(out)?; - self.actions.write_into(out)?; - Ok(()) - } -} - -#[derive(Hash, PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] +#[derive(nbor, Hash, PartialEq, Eq, Debug, Clone)] pub enum Action { CreateAccount(CreateAccountAction), DeployContract(DeployContractAction), @@ -73,12 +58,11 @@ impl Action { } } -#[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] +#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] pub struct CreateAccountAction {} -#[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[derive(nbor, Hash, PartialEq, Eq, Clone)] pub struct DeployContractAction { - #[serde(with = "base_bytes_format")] pub code: Vec, } @@ -90,13 +74,11 @@ impl fmt::Debug for DeployContractAction { } } -#[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[derive(nbor, Hash, PartialEq, Eq, Clone)] pub struct FunctionCallAction { pub method_name: String, - #[serde(with = "base_bytes_format")] pub args: Vec, pub gas: Gas, - #[serde(with = "u128_dec_format")] pub deposit: Balance, } @@ -111,47 +93,51 @@ impl fmt::Debug for FunctionCallAction { } } -#[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] +#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] pub struct TransferAction { - #[serde(with = "u128_dec_format")] pub deposit: Balance, } -#[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] +#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] pub struct StakeAction { - #[serde(with = "u128_dec_format")] pub stake: Balance, pub public_key: PublicKey, } -#[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] +#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] pub struct AddKeyAction { pub public_key: PublicKey, pub access_key: AccessKey, } -#[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] +#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] pub struct DeleteKeyAction { pub public_key: PublicKey, } -#[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] +#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] pub struct DeleteAccountAction { pub beneficiary_id: AccountId, } -#[derive(Eq, Debug, Clone, Serialize, Deserialize)] +#[derive(nbor, Eq, Debug, Clone)] +#[nbor_init(init)] pub struct SignedTransaction { pub signature: StructSignature, pub transaction: Transaction, - #[serde(skip)] + #[nbor_skip] hash: CryptoHash, } impl SignedTransaction { pub fn new(signature: StructSignature, transaction: Transaction) -> Self { - let hash = transaction.get_hash(); - Self { signature, transaction, hash } + let mut signed_tx = Self { signature, transaction, hash: CryptoHash::default() }; + signed_tx.init(); + signed_tx + } + + pub fn init(&mut self) { + self.hash = self.transaction.get_hash(); } pub fn get_hash(&self) -> CryptoHash { @@ -198,7 +184,7 @@ impl PartialEq for SignedTransaction { } } -#[derive(Hash, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[derive(nbor, Hash, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub enum TransactionStatus { Unknown, Completed, @@ -236,7 +222,7 @@ impl Default for TransactionStatus { } } -#[derive(PartialEq, Clone, Serialize, Deserialize, Default)] +#[derive(nbor, PartialEq, Clone, Serialize, Deserialize, Default)] pub struct TransactionResult { /// Transaction status. pub status: TransactionStatus, @@ -329,10 +315,14 @@ pub fn verify_transaction_signature( #[cfg(test)] mod tests { + use std::convert::TryFrom; + + use nbor::de::Deserializable; + use crate::crypto::signature::{get_key_pair, sign, DEFAULT_SIGNATURE}; + use crate::serialize::to_base; use super::*; - use crate::serialize::{to_base, Decode, Encode}; #[test] fn test_verify_transaction() { @@ -355,24 +345,52 @@ mod tests { let invalid_keys = vec![wrong_public_key]; assert!(!verify_transaction_signature(&transaction, &invalid_keys)); - let bytes = transaction.encode().unwrap(); - let new_tx = SignedTransaction::decode(&bytes).unwrap().into(); - assert!(verify_transaction_signature(&new_tx, &valid_keys)); + //let bytes = transaction.encode().unwrap(); + // let new_tx = SignedTransaction::decode(&bytes).unwrap().into(); + //assert!(verify_transaction_signature(&new_tx, &valid_keys)); } /// This test is change checker for a reason - we don't expect transaction format to change. - /// If it does - you MUST update all of the dependencies: like nearlib. + /// If it does - you MUST update all of the dependencies: like nearlib and other clients. #[test] fn test_serialize_transaction() { + let public_key = + PublicKey::try_from("22skMptHjFWNyuEWY22ftn2AbLPSYpmYwGJRGwpNHbTV").unwrap(); let transaction = Transaction { signer_id: "test.near".to_string(), - public_key: PublicKey::try_from("22skMptHjFWNyuEWY22ftn2AbLPSYpmYwGJRGwpNHbTV") - .unwrap(), + public_key, nonce: 1, receiver_id: "123".to_string(), - actions: vec![], + actions: vec![ + // Action::CreateAccount(CreateAccountAction {}), + // Action::DeployContract(DeployContractAction { code: vec![1, 2, 3] }), + // Action::FunctionCall(FunctionCallAction { + // method_name: "qqq".to_string(), + // args: vec![1, 2, 3], + // gas: 1_000, + // deposit: 1_000_000, + // }), + Action::Transfer(TransferAction { deposit: 1_000_000_000 }), + // Action::Stake(StakeAction { public_key, stake: 1_000_000 }), + // Action::AddKey(AddKeyAction { + // public_key, + // access_key: AccessKey { + // amount: 1, + // balance_owner: Some("123".to_string()), + // contract_id: Some("321".to_string()), + // method_name: None, + // }, + // }), + // Action::DeleteKey(DeleteKeyAction { public_key }), + // Action::DeleteAccount(DeleteAccountAction { beneficiary_id: "123".to_string() }), + ], }; - let hash = to_base(&transaction.get_hash()); - assert_eq!(hash, "EsTRpLernDsH2hzznZ6wKMu1XYdyT4ynKK2H13hbMyzb"); + let signed_tx = SignedTransaction::new(DEFAULT_SIGNATURE, transaction); + let new_signed_tx = SignedTransaction::from_slice(&signed_tx.to_vec().unwrap()).unwrap(); + + assert_eq!( + to_base(&new_signed_tx.get_hash()), + "EsTRpLernDsH2hzznZ6wKMu1XYdyT4ynKK2H13hbMyzb" + ); } } diff --git a/core/primitives/src/types.rs b/core/primitives/src/types.rs index 5b99325938c..a1eae12cf3e 100644 --- a/core/primitives/src/types.rs +++ b/core/primitives/src/types.rs @@ -1,10 +1,8 @@ -use std::convert::TryFrom; +use nbor::nbor; -// pub use crate::balance::Balance; use crate::crypto::aggregate_signature::BlsSignature; use crate::crypto::signature::{PublicKey, Signature}; use crate::hash::CryptoHash; -use crate::serialize::u128_dec_format; /// Public key alias. Used to human readable public key. #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)] @@ -51,14 +49,13 @@ pub enum BlockId { } /// Stores validator and its stake. -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(nbor, Debug, Serialize, Deserialize, Clone)] pub struct ValidatorStake { /// Account that stakes money. pub account_id: AccountId, /// Public key of the proposed validator. pub public_key: PublicKey, /// Stake / weight of the validator. - #[serde(with = "u128_dec_format")] pub amount: Balance, } diff --git a/core/primitives/src/utils.rs b/core/primitives/src/utils.rs index 25681b88798..3a1eb0bf9ac 100644 --- a/core/primitives/src/utils.rs +++ b/core/primitives/src/utils.rs @@ -1,4 +1,4 @@ -use std::convert::{AsRef, TryFrom, TryInto}; +use std::convert::AsRef; use std::fmt; use byteorder::{LittleEndian, WriteBytesExt}; @@ -9,6 +9,7 @@ use lazy_static::lazy_static; use crate::crypto::signature::PublicKey; use crate::hash::{hash, CryptoHash}; use crate::types::{AccountId, ShardId}; +use chrono::{DateTime, NaiveDateTime, Utc}; pub const ACCOUNT_DATA_SEPARATOR: &[u8; 1] = b","; @@ -165,3 +166,14 @@ macro_rules! unwrap_or_return(($obj: expr, $ret: expr) => (match $obj { return $ret; } })); + +/// Converts timestamp in millis into DateTime UTC time. +pub fn from_timestamp(timestamp: u64) -> DateTime { + DateTime::from_utc( + NaiveDateTime::from_timestamp( + (timestamp / 1000) as i64, + ((timestamp % 1000) * 1_000_000) as u32, + ), + Utc, + ) +} diff --git a/core/store/Cargo.toml b/core/store/Cargo.toml index ee92c848d84..24f0d843558 100644 --- a/core/store/Cargo.toml +++ b/core/store/Cargo.toml @@ -15,6 +15,7 @@ serde_derive = "1.0" cached = { git = "https://github.com/nearprotocol/cached", rev = "7e472eddef68607e344d5a106a0e6705d92e55be" } log = "0.4" +nbor = { path = "../nbor" } near-primitives = { path = "../primitives" } [dev-dependencies] diff --git a/core/store/src/lib.rs b/core/store/src/lib.rs index df368257c82..2cc8f1b56df 100644 --- a/core/store/src/lib.rs +++ b/core/store/src/lib.rs @@ -5,13 +5,12 @@ use cached::{Cached, SizedCache}; pub use kvdb::DBValue; use kvdb::{DBOp, DBTransaction, KeyValueDB}; use kvdb_rocksdb::{Database, DatabaseConfig}; -use serde::de::DeserializeOwned; -use serde::Serialize; +use nbor::{Deserializable, Serializable}; use near_primitives::account::{AccessKey, Account}; use near_primitives::contract::ContractCode; use near_primitives::crypto::signature::PublicKey; -use near_primitives::serialize::{to_base, Decode, Encode}; +use near_primitives::serialize::to_base; use near_primitives::types::{AccountId, StorageUsage}; use near_primitives::utils::{ key_for_access_key, key_for_account, key_for_code, prefix_for_access_key, prefix_for_data, @@ -53,13 +52,13 @@ impl Store { self.storage.get(column, key).map(|a| a.map(|b| b.to_vec())) } - pub fn get_ser( + pub fn get_ser( &self, column: Option, key: &[u8], ) -> Result, io::Error> { match self.storage.get(column, key) { - Ok(Some(bytes)) => match Decode::decode(bytes.as_ref()) { + Ok(Some(bytes)) => match T::from_slice(bytes.as_ref()) { Ok(result) => Ok(Some(result)), Err(e) => Err(e), }, @@ -107,13 +106,13 @@ impl StoreUpdate { self.transaction.put(column, key, value) } - pub fn set_ser( + pub fn set_ser( &mut self, column: Option, key: &[u8], value: &T, ) -> Result<(), io::Error> { - let data = Encode::encode(value)?; + let data = value.to_vec()?; self.set(column, key, &data); Ok(()) } @@ -165,7 +164,7 @@ impl fmt::Debug for StoreUpdate { } } -pub fn read_with_cache<'a, T: Decode + DeserializeOwned + 'a>( +pub fn read_with_cache<'a, T: Deserializable + 'a>( storage: &Store, col: Option, cache: &'a mut SizedCache, T>, @@ -189,18 +188,18 @@ pub fn create_store(path: &str) -> Arc { } /// Reads an object from Trie. -pub fn get(state_update: &TrieUpdate, key: &[u8]) -> Option { - state_update.get(key).and_then(|data| Decode::decode(&data).ok()) +pub fn get(state_update: &TrieUpdate, key: &[u8]) -> Option { + state_update.get(key).and_then(|data| T::from_slice(&data).ok()) } /// Writes an object into Trie. -pub fn set(state_update: &mut TrieUpdate, key: Vec, value: &T) { - value.encode().ok().map(|data| state_update.set(key, DBValue::from_vec(data))).or_else(|| None); +pub fn set(state_update: &mut TrieUpdate, key: Vec, value: &T) { + value.to_vec().ok().map(|data| state_update.set(key, DBValue::from_vec(data))).or_else(|| None); } /// Number of bytes the account data structure occupies in the storage. pub fn account_storage_size(account: &Account) -> StorageUsage { - account.encode().map(|bytes| bytes.len() as StorageUsage).unwrap_or(0) + account.to_vec().map(|bytes| bytes.len() as StorageUsage).unwrap_or(0) } /// Number of bytes account and all of it's other data occupies in the storage. @@ -212,7 +211,7 @@ pub fn total_account_storage(account_id: &AccountId, account: &Account) -> Stora } pub fn set_account(state_update: &mut TrieUpdate, key: &AccountId, account: &Account) { - set(state_update, key_for_account(key), &account) + set(state_update, key_for_account(key), account) } pub fn get_account(state_update: &TrieUpdate, key: &AccountId) -> Option { @@ -225,7 +224,7 @@ pub fn set_access_key( public_key: &PublicKey, access_key: &AccessKey, ) { - set(state_update, key_for_access_key(account_id, public_key), &access_key); + set(state_update, key_for_access_key(account_id, public_key), access_key); } pub fn get_access_key( diff --git a/near/Cargo.toml b/near/Cargo.toml index 8f1988b20be..3bbd0985c2a 100644 --- a/near/Cargo.toml +++ b/near/Cargo.toml @@ -22,6 +22,7 @@ serde_json = "1.0" dirs = "1.0.5" lazy_static = "1.3" +nbor = { path = "../core/nbor" } near-primitives = { path = "../core/primitives" } near-store = { path = "../core/store" } node-runtime = { path = "../runtime/runtime" } diff --git a/near/src/config.rs b/near/src/config.rs index dfe6fa37ea6..b237c255324 100644 --- a/near/src/config.rs +++ b/near/src/config.rs @@ -19,9 +19,9 @@ use near_jsonrpc::RpcConfig; use near_network::test_utils::open_port; use near_network::types::PROTOCOL_VERSION; use near_network::NetworkConfig; -use near_primitives::account::Account; use near_primitives::crypto::signer::{EDSigner, InMemorySigner, KeyFile}; use near_primitives::hash::hash; +use near_primitives::rpc::AccountView; use near_primitives::serialize::{to_base64, u128_dec_format}; use near_primitives::types::{AccountId, Balance, BlockIndex, ReadablePublicKey, ValidatorId}; use near_telemetry::TelemetryConfig; @@ -338,12 +338,12 @@ impl GenesisConfig { } records[0].push(StateRecord::Account { account_id: account.to_string(), - account: Account { + account: AccountView { nonce: 0, amount: TESTING_INIT_BALANCE - if i < num_validators { TESTING_INIT_STAKE } else { 0 }, - public_keys: vec![signer.public_key], - code_hash, + public_keys: vec![signer.public_key.into()], + code_hash: code_hash.into(), staked: if i < num_validators { TESTING_INIT_STAKE } else { 0 }, storage_usage: 0, storage_paid_at: 0, diff --git a/near/src/runtime.rs b/near/src/runtime.rs index df608e911e0..190acbeec0d 100644 --- a/near/src/runtime.rs +++ b/near/src/runtime.rs @@ -169,20 +169,12 @@ impl RuntimeAdapter for NightshadeRuntime { height: BlockIndex, ) -> Result> { let mut vm = self.validator_manager.write().expect(POISONED_LOCK_ERR); - let (epoch_hash, idx) = vm.get_epoch_offset(parent_hash, height)?; + let (epoch_hash, _idx) = vm.get_epoch_offset(parent_hash, height)?; let validator_assignemnt = vm.get_validators(epoch_hash)?; - let total_seats: u64 = validator_assignemnt.chunk_producers[shard_id as usize] - .iter() - .map(|(_, seats)| seats) - .sum(); - let mut cur_seats = 0; - for (index, seats) in validator_assignemnt.chunk_producers[shard_id as usize].iter() { - if cur_seats + *seats > idx % total_seats { - return Ok(validator_assignemnt.validators[*index].account_id.clone()); - } - cur_seats += *seats; - } - unreachable!() + let total_seats: u64 = validator_assignemnt.chunk_producers[shard_id as usize].len() as u64; + let validator_id = validator_assignemnt.chunk_producers[shard_id as usize] + [(height % total_seats) as usize]; + Ok(validator_assignemnt.validators[validator_id].account_id.clone()) } fn check_validator_signature( @@ -640,7 +632,7 @@ mod test { &assignment( vec![("test3", TESTING_INIT_STAKE * 2), ("test1", TESTING_INIT_STAKE * 2)], vec![1, 0], - vec![vec![(1, 1), (0, 1)]], + vec![vec![1, 0]], vec![], 6, change_stake(vec![ diff --git a/near/src/test_utils.rs b/near/src/test_utils.rs index e6ea79714b1..95b542ca10c 100644 --- a/near/src/test_utils.rs +++ b/near/src/test_utils.rs @@ -10,8 +10,8 @@ pub fn change_stake(stake_changes: Vec<(&str, Balance)>) -> BTreeMap, block_producers: Vec, - chunk_producers: Vec>, - fishermen: Vec<(usize, u64)>, + chunk_producers: Vec>, + mut fishermen: Vec<(usize, u64)>, expected_epoch_start: BlockIndex, stake_change: BTreeMap, ) -> ValidatorAssignment { @@ -32,7 +32,7 @@ pub fn assignment( validator_to_index, block_producers, chunk_producers, - fishermen, + fishermen: fishermen.drain(..).collect(), expected_epoch_start, stake_change, } diff --git a/near/src/validator_manager.rs b/near/src/validator_manager.rs index 11a6d05a413..37a2425d7eb 100644 --- a/near/src/validator_manager.rs +++ b/near/src/validator_manager.rs @@ -6,8 +6,8 @@ use std::sync::Arc; use rand::seq::SliceRandom; use rand::{rngs::StdRng, SeedableRng}; -use serde_derive::{Deserialize, Serialize}; +use nbor::nbor; use near_primitives::hash::CryptoHash; use near_primitives::types::{ AccountId, Balance, BlockIndex, ShardId, ValidatorId, ValidatorStake, @@ -188,19 +188,13 @@ fn proposals_to_assignments( let block_producers = dup_proposals[..epoch_config.num_block_producers].to_vec(); // Collect proposals into block producer assignments. - let mut chunk_producers: Vec> = vec![]; + let mut chunk_producers: Vec> = vec![]; let mut last_index: usize = 0; for num_seats in epoch_config.block_producers_per_shard.iter() { - let mut cp_to_index: HashMap = HashMap::default(); - let mut cp: Vec<(ValidatorId, u64)> = vec![]; + let mut cp: Vec = vec![]; for i in 0..*num_seats { let proposal_index = dup_proposals[(i + last_index) % epoch_config.num_block_producers]; - if let Some(j) = cp_to_index.get(&proposal_index) { - cp[*j as usize].1 += 1; - } else { - cp_to_index.insert(proposal_index, cp.len()); - cp.push((proposal_index, 1)); - } + cp.push(proposal_index); } chunk_producers.push(cp); last_index = (last_index + num_seats) % epoch_config.num_block_producers; @@ -226,7 +220,7 @@ fn proposals_to_assignments( validator_to_index, block_producers, chunk_producers, - fishermen: vec![], + fishermen: HashMap::default(), expected_epoch_start, stake_change: final_stake_change, }) @@ -270,7 +264,7 @@ pub struct ValidatorEpochConfig { } /// Information about validator seat assignments. -#[derive(Default, Serialize, Deserialize, Clone, Debug)] +#[derive(nbor, Default, Clone, Debug)] pub struct ValidatorAssignment { /// List of current validators. pub validators: Vec, @@ -279,9 +273,9 @@ pub struct ValidatorAssignment { /// Weights for each of the validators responsible for block production. pub block_producers: Vec, /// Per each shard, ids and seats of validators that are responsible. - pub chunk_producers: Vec>, + pub chunk_producers: Vec>, /// Weight of given validator used to determine how many shards they will validate. - pub fishermen: Vec<(ValidatorId, u64)>, + pub fishermen: HashMap, /// Expected epoch start index: previous expected epoch start + epoch_length pub expected_epoch_start: BlockIndex, /// New stake for validators @@ -323,7 +317,7 @@ impl PartialEq for ValidatorAssignment { impl Eq for ValidatorAssignment {} /// Information per each index about validators. -#[derive(Default, Serialize, Deserialize, Clone, Debug)] +#[derive(nbor, Default, Clone, Debug)] pub struct ValidatorIndexInfo { pub index: BlockIndex, pub prev_hash: CryptoHash, @@ -583,7 +577,7 @@ impl ValidatorManager { self.last_epoch = *new_hash; self.set_validators(new_hash, assignment, &mut store_update)?; - store_update.set_ser(COL_PROPOSALS, LAST_EPOCH_KEY, &epoch_hash)?; + store_update.set_ser(COL_PROPOSALS, LAST_EPOCH_KEY, epoch_hash)?; store_update.set_ser(COL_LAST_EPOCH_PROPOSALS, new_hash.as_ref(), &cur_proposals)?; store_update.commit().map_err(|err| ValidatorError::Other(err.to_string()))?; Ok(()) @@ -734,7 +728,7 @@ mod test { assignment( vec![("test1", 1_000_000)], vec![0], - vec![vec![(0, 1)], vec![(0, 1)]], + vec![vec![0, 0]], vec![], 0, change_stake(vec![("test1", 1_000_000)]) @@ -765,11 +759,11 @@ mod test { vec![0, 1, 0, 0, 1, 2], vec![ // Shard 0 is block produced / validated by all block producers & fisherman. - vec![(0, 3), (1, 2), (2, 1)], - vec![(0, 1), (1, 1)], - vec![(0, 2)], - vec![(1, 1), (2, 1)], - vec![(0, 1), (1, 1)] + vec![0, 1, 2], + vec![0, 1], + vec![0, 2], + vec![1, 2], + vec![0, 1] ], vec![], 0, @@ -800,7 +794,7 @@ mod test { let expected0 = assignment( vec![("test1", amount_staked)], vec![0, 0], - vec![vec![(0, 2)]], + vec![vec![0]], vec![], 1, change_stake(vec![("test1", amount_staked)]), @@ -821,7 +815,7 @@ mod test { let expected2 = assignment( vec![("test1", amount_staked), ("test2", amount_staked)], vec![0, 1], - vec![vec![(0, 1), (1, 1)]], + vec![vec![0, 1]], vec![], 3, change_stake(vec![("test1", amount_staked), ("test2", amount_staked)]), @@ -914,7 +908,7 @@ mod test { &assignment( vec![("test1", amount_staked), ("test2", amount_staked), ("test3", amount_staked)], vec![2, 1, 0], - vec![vec![(2, 1), (1, 1), (0, 1)]], + vec![vec![2, 1, 0]], vec![], 3, change_stake(vec![ @@ -931,7 +925,7 @@ mod test { &assignment( vec![("test4", amount_staked), ("test3", amount_staked), ("test2", amount_staked)], vec![2, 1, 0], - vec![vec![(2, 1), (1, 1), (0, 1)]], + vec![vec![2, 1, 0]], vec![], 6, change_stake(vec![ @@ -949,7 +943,7 @@ mod test { &assignment( vec![("test1", amount_staked), ("test3", amount_staked)], vec![0, 1, 0], - vec![vec![(0, 2), (1, 1)]], + vec![vec![0, 1]], vec![], 9, change_stake(vec![ @@ -969,7 +963,7 @@ mod test { &assignment( vec![("test4", amount_staked), ("test2", amount_staked)], vec![0, 1, 0], - vec![vec![(0, 2), (1, 1)]], + vec![vec![0, 1]], vec![], 9, change_stake(vec![ @@ -1018,7 +1012,7 @@ mod test { &assignment( vec![("test1", amount_staked)], vec![0], - vec![vec![(0, 1)]], + vec![vec![0]], vec![], 4, change_stake(vec![("test1", amount_staked)]), @@ -1052,7 +1046,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![(0, 2)]], + vec![vec![0]], vec![], 4, change_stake(vec![("test1", 0), ("test2", amount_staked)]) @@ -1063,7 +1057,7 @@ mod test { &assignment( vec![("test1", amount_staked)], vec![0, 0], - vec![vec![(0, 2)]], + vec![vec![0]], vec![], 4, change_stake(vec![("test1", amount_staked), ("test2", 0)]) @@ -1096,7 +1090,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![(0, 2)]], + vec![vec![0]], vec![], 4, change_stake(vec![("test1", 0), ("test2", amount_staked)]) @@ -1110,7 +1104,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![(0, 2)]], + vec![vec![0]], vec![], 6, change_stake(vec![("test1", 0), ("test2", amount_staked)]) @@ -1143,7 +1137,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![(0, 2)]], + vec![vec![0]], vec![], 4, change_stake(vec![("test1", 0), ("test2", amount_staked)]) @@ -1213,7 +1207,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![(0, 2)]], + vec![vec![0]], vec![], 4, change_stake(vec![("test1", 0), ("test2", amount_staked)]) @@ -1233,7 +1227,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![(0, 2)]], + vec![vec![0]], vec![], 6, change_stake(vec![("test1", 0), ("test2", amount_staked)]) diff --git a/near/tests/run_nodes.rs b/near/tests/run_nodes.rs index 9ca30094158..15d17359e79 100644 --- a/near/tests/run_nodes.rs +++ b/near/tests/run_nodes.rs @@ -43,10 +43,7 @@ fn run_nodes(num_nodes: usize) { Box::new(move |_ctx| { actix::spawn(view_client.send(GetBlock::Best).then(|res| { match &res { - Ok(Ok(b)) - if b.header.inner.height > 4 - && b.header.inner.total_weight.to_num() > 6 => - { + Ok(Ok(b)) if b.header.height > 4 && b.header.total_weight > 6 => { System::current().stop() } Err(_) => return futures::future::err(()), diff --git a/near/tests/sync_nodes.rs b/near/tests/sync_nodes.rs index 3a153e821f1..1101b95ac51 100644 --- a/near/tests/sync_nodes.rs +++ b/near/tests/sync_nodes.rs @@ -80,7 +80,7 @@ fn sync_nodes() { Box::new(move |_ctx| { actix::spawn(view_client2.send(GetBlock::Best).then(|res| { match &res { - Ok(Ok(b)) if b.header.inner.height == 11 => System::current().stop(), + Ok(Ok(b)) if b.header.height == 11 => System::current().stop(), Err(_) => return futures::future::err(()), _ => {} }; @@ -130,13 +130,13 @@ fn sync_after_sync_nodes() { let next_step1 = next_step.clone(); actix::spawn(view_client2.send(GetBlock::Best).then(move |res| { match &res { - Ok(Ok(b)) if b.header.inner.height == 11 => { + Ok(Ok(b)) if b.header.height == 11 => { if !next_step1.load(Ordering::Relaxed) { let _ = add_blocks(&last_block1, client11, 11, signer1); next_step1.store(true, Ordering::Relaxed); } } - Ok(Ok(b)) if b.header.inner.height > 20 => System::current().stop(), + Ok(Ok(b)) if b.header.height > 20 => System::current().stop(), Err(_) => return futures::future::err(()), _ => {} }; @@ -202,7 +202,7 @@ fn sync_state_stake_change() { let near2_copy = near2.clone(); let dir2_path_copy = dir2_path.clone(); actix::spawn(view_client1.send(GetBlock::Best).then(move |res| { - let latest_block_height = res.unwrap().unwrap().header.inner.height; + let latest_block_height = res.unwrap().unwrap().header.height; if !started_copy.load(Ordering::SeqCst) && latest_block_height > 10 { started_copy.store(true, Ordering::SeqCst); let (_, view_client2) = start_with_config(&dir2_path_copy, near2_copy); @@ -210,9 +210,7 @@ fn sync_state_stake_change() { WaitOrTimeout::new( Box::new(move |_ctx| { actix::spawn(view_client2.send(GetBlock::Best).then(move |res| { - if res.unwrap().unwrap().header.inner.height - > latest_block_height + 1 - { + if res.unwrap().unwrap().header.height > latest_block_height + 1 { System::current().stop() } future::result::<_, ()>(Ok(())) diff --git a/near/tests/sync_state_nodes.rs b/near/tests/sync_state_nodes.rs index 7dfbd328f6a..98857065662 100644 --- a/near/tests/sync_state_nodes.rs +++ b/near/tests/sync_state_nodes.rs @@ -65,7 +65,7 @@ fn sync_state_nodes() { Box::new(move |_ctx| { actix::spawn(view_client2.send(GetBlock::Best).then(|res| { match &res { - Ok(Ok(b)) if b.header.inner.height >= 101 => System::current().stop(), + Ok(Ok(b)) if b.header.height >= 101 => System::current().stop(), Err(_) => return futures::future::err(()), _ => {} }; diff --git a/runtime/runtime/src/lib.rs b/runtime/runtime/src/lib.rs index 97b091d7cbc..8640207a2e4 100644 --- a/runtime/runtime/src/lib.rs +++ b/runtime/runtime/src/lib.rs @@ -4,14 +4,12 @@ extern crate log; extern crate serde_derive; use std::collections::{hash_map::Entry, HashMap}; -use std::convert::TryFrom; use std::sync::{Arc, Mutex}; use kvdb::DBValue; use near_primitives::account::Account; use near_primitives::contract::ContractCode; -use near_primitives::crypto::signature::PublicKey; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{ActionReceipt, DataReceipt, Receipt, ReceiptEnum}; use near_primitives::serialize::from_base64; @@ -683,29 +681,28 @@ impl Runtime { records: &[StateRecord], ) -> (StoreUpdate, MerkleHash) { for record in records { - match record { + match record.clone() { StateRecord::Account { account_id, account } => { - set_account(&mut state_update, &account_id, account); + set_account(&mut state_update, &account_id, &account.into()); } StateRecord::Data { key, value } => { state_update.set( - from_base64(key).expect("Failed to decode key"), - DBValue::from_vec(from_base64(value).expect("Failed to decode value")), + from_base64(&key).expect("Failed to decode key"), + DBValue::from_vec(from_base64(&value).expect("Failed to decode value")), ); } StateRecord::Contract { account_id, code } => { let code = ContractCode::new( - from_base64(code).expect("Failed to decode wasm from base64"), + from_base64(&code).expect("Failed to decode wasm from base64"), ); - set_code(&mut state_update, account_id, &code); + set_code(&mut state_update, &account_id, &code); } StateRecord::AccessKey { account_id, public_key, access_key } => { set_access_key( &mut state_update, - account_id, - &PublicKey::try_from(public_key.0.as_str()) - .expect("Failed to decode public key"), - access_key, + &account_id, + &public_key.into(), + &access_key.into(), ); } } diff --git a/runtime/runtime/src/store.rs b/runtime/runtime/src/store.rs index 0005edadf68..6eb3972bb37 100644 --- a/runtime/runtime/src/store.rs +++ b/runtime/runtime/src/store.rs @@ -1,20 +1,21 @@ use std::convert::TryFrom; -use near_primitives::account::{AccessKey, Account}; use near_primitives::crypto::signature::PublicKey; -use near_primitives::types::{AccountId, ReadablePublicKey}; +use near_primitives::hash::CryptoHash; +use near_primitives::rpc::{AccessKeyView, AccountView, PublicKeyView}; +use near_primitives::types::AccountId; /// Record in the state storage. #[derive(Serialize, Deserialize, Clone, Debug)] pub enum StateRecord { /// Account information. - Account { account_id: AccountId, account: Account }, + Account { account_id: AccountId, account: AccountView }, /// Data records inside the contract, encoded in base64. Data { key: String, value: String }, /// Contract code encoded in base64. Contract { account_id: AccountId, code: String }, /// Access key associated with some account. - AccessKey { account_id: AccountId, public_key: ReadablePublicKey, access_key: AccessKey }, + AccessKey { account_id: AccountId, public_key: PublicKeyView, access_key: AccessKeyView }, // TODO: DATA } @@ -22,12 +23,12 @@ impl StateRecord { pub fn account(account_id: &str, public_key: &str, amount: u128, staked: u128) -> Self { StateRecord::Account { account_id: account_id.to_string(), - account: Account { - public_keys: vec![PublicKey::try_from(public_key).unwrap()], + account: AccountView { + public_keys: vec![PublicKey::try_from(public_key).unwrap().into()], nonce: 0, amount, staked, - code_hash: Default::default(), + code_hash: CryptoHash::default().into(), storage_usage: 0, storage_paid_at: 0, }, diff --git a/test-utils/loadtester/Cargo.toml b/test-utils/loadtester/Cargo.toml index 37fc914140b..6079cd3a480 100644 --- a/test-utils/loadtester/Cargo.toml +++ b/test-utils/loadtester/Cargo.toml @@ -15,6 +15,7 @@ futures = "0.1.25" tokio = "0.1" serde_json = "1.0.0" +nbor = { path = "../../core/nbor" } near-primitives = { path = "../../core/primitives" } node-runtime = { path = "../../runtime/runtime" } testlib = { path = "../testlib" } diff --git a/test-utils/loadtester/src/remote_node.rs b/test-utils/loadtester/src/remote_node.rs index e833814faf7..94199fabc55 100644 --- a/test-utils/loadtester/src/remote_node.rs +++ b/test-utils/loadtester/src/remote_node.rs @@ -7,9 +7,10 @@ use futures::Future; use reqwest::r#async::Client as AsyncClient; use reqwest::Client as SyncClient; +use nbor::Serializable; use near_primitives::crypto::signer::InMemorySigner; use near_primitives::rpc::AccountView; -use near_primitives::serialize::{from_base, Encode}; +use near_primitives::serialize::from_base; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, Nonce}; @@ -158,7 +159,7 @@ impl RemoteNode { &self, transaction: SignedTransaction, ) -> Box + Send> { - let bytes = transaction.encode().unwrap(); + let bytes = transaction.to_vec().unwrap(); let url = format!("{}{}", self.url, "/broadcast_tx_sync"); let response = self .async_client @@ -176,7 +177,7 @@ impl RemoteNode { &self, transaction: SignedTransaction, ) -> Result> { - let bytes = transaction.encode().unwrap(); + let bytes = transaction.to_vec().unwrap(); let url = format!("{}{}", self.url, "/broadcast_tx_sync"); let result: serde_json::Value = self .sync_client diff --git a/test-utils/state-viewer/Cargo.toml b/test-utils/state-viewer/Cargo.toml index 16265bef740..5accd838e63 100644 --- a/test-utils/state-viewer/Cargo.toml +++ b/test-utils/state-viewer/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" clap = "2.32.0" hex = "0.3" +nbor = { path = "../../core/nbor" } near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } node-runtime = { path = "../../runtime/runtime" } diff --git a/test-utils/state-viewer/src/main.rs b/test-utils/state-viewer/src/main.rs index 87354259ae0..076b678c421 100644 --- a/test-utils/state-viewer/src/main.rs +++ b/test-utils/state-viewer/src/main.rs @@ -4,13 +4,15 @@ use std::sync::Arc; use clap::{App, Arg, SubCommand}; +use nbor::Deserializable; + use near::{get_default_home, get_store_path, load_config, NearConfig, NightshadeRuntime}; use near_chain::{ChainStore, ChainStoreAccess}; use near_network::peer_store::PeerStore; use near_primitives::account::{AccessKey, Account}; use near_primitives::crypto::signature::PublicKey; use near_primitives::hash::{hash, CryptoHash}; -use near_primitives::serialize::{from_base64, to_base64, Decode}; +use near_primitives::serialize::{from_base64, to_base64}; use near_primitives::test_utils::init_integration_logger; use near_primitives::types::BlockIndex; use near_primitives::utils::{col, ACCOUNT_DATA_SEPARATOR}; @@ -40,8 +42,11 @@ fn kv_to_state_record(key: Vec, value: DBValue) -> StateRecord { if separator.is_some() { StateRecord::Data { key: to_base64(&key), value: to_base64(&value) } } else { - let account = Account::decode(&value).unwrap(); - StateRecord::Account { account_id: to_printable(&key[1..]), account } + let account = Account::from_slice(&value).unwrap(); + StateRecord::Account { + account_id: to_printable(&key[1..]), + account: account.into(), + } } } /* @@ -55,10 +60,14 @@ fn kv_to_state_record(key: Vec, value: DBValue) -> StateRecord { } col::ACCESS_KEY => { let separator = (1..key.len()).find(|&x| key[x] == col::ACCESS_KEY[0]).unwrap(); - let access_key = AccessKey::decode(&value).unwrap(); + let access_key = AccessKey::from_slice(&value).unwrap(); let account_id = to_printable(&key[1..separator]); let public_key = PublicKey::try_from(&key[(separator + 1)..]).unwrap(); - StateRecord::AccessKey { account_id, public_key: public_key.to_readable(), access_key } + StateRecord::AccessKey { + account_id, + public_key: public_key.into(), + access_key: access_key.into(), + } } _ => unreachable!(), } diff --git a/test-utils/testlib/Cargo.toml b/test-utils/testlib/Cargo.toml index 883455a2dd4..faa5603c50e 100644 --- a/test-utils/testlib/Cargo.toml +++ b/test-utils/testlib/Cargo.toml @@ -22,6 +22,7 @@ byteorder = "1.2" tempdir = "0.3" tokio-signal = "0.2" +nbor = { path = "../../core/nbor" } near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } node-runtime = { path = "../../runtime/runtime" } diff --git a/test-utils/testlib/src/user/mod.rs b/test-utils/testlib/src/user/mod.rs index 5da44d49609..c73a79d417e 100644 --- a/test-utils/testlib/src/user/mod.rs +++ b/test-utils/testlib/src/user/mod.rs @@ -2,13 +2,14 @@ use std::sync::Arc; use futures::Future; -use near_chain::Block; use near_primitives::account::AccessKey; use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; -use near_primitives::rpc::{AccessKeyView, AccountView, CryptoHashView, ViewStateResult}; +use near_primitives::rpc::{ + AccessKeyView, AccountView, BlockView, CryptoHashView, ViewStateResult, +}; use near_primitives::transaction::{ Action, AddKeyAction, CreateAccountAction, DeleteAccountAction, DeleteKeyAction, DeployContractAction, FinalTransactionResult, FunctionCallAction, SignedTransaction, @@ -45,7 +46,7 @@ pub trait User { fn get_best_block_index(&self) -> Option; - fn get_block(&self, index: u64) -> Option; + fn get_block(&self, index: u64) -> Option; fn get_transaction_result(&self, hash: &CryptoHash) -> TransactionResult; diff --git a/test-utils/testlib/src/user/rpc_user.rs b/test-utils/testlib/src/user/rpc_user.rs index acc88ddd224..b0c36b9ed7e 100644 --- a/test-utils/testlib/src/user/rpc_user.rs +++ b/test-utils/testlib/src/user/rpc_user.rs @@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock}; use actix::System; -use near_chain::Block; +use nbor::Serializable; use near_client::StatusResponse; use near_jsonrpc::client::{new_client, JsonRpcClient}; use near_primitives::crypto::signature::PublicKey; @@ -11,9 +11,9 @@ use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; use near_primitives::rpc::{ - AccessKeyView, AccountView, CryptoHashView, QueryResponse, ViewStateResult, + AccessKeyView, AccountView, BlockView, CryptoHashView, QueryResponse, ViewStateResult, }; -use near_primitives::serialize::{to_base, to_base64, BaseEncode, Encode}; +use near_primitives::serialize::{to_base, to_base64, BaseEncode}; use near_primitives::transaction::{FinalTransactionResult, SignedTransaction, TransactionResult}; use near_primitives::types::AccountId; @@ -48,7 +48,7 @@ impl User for RpcUser { } fn add_transaction(&self, transaction: SignedTransaction) -> Result<(), String> { - let bytes = transaction.encode().unwrap(); + let bytes = transaction.to_vec().unwrap(); let _ = System::new("actix") .block_on(self.client.write().unwrap().broadcast_tx_async(to_base64(&bytes)))?; Ok(()) @@ -58,7 +58,7 @@ impl User for RpcUser { &self, transaction: SignedTransaction, ) -> Result { - let bytes = transaction.encode().unwrap(); + let bytes = transaction.to_vec().unwrap(); System::new("actix") .block_on(self.client.write().unwrap().broadcast_tx_commit(to_base64(&bytes))) } @@ -76,7 +76,7 @@ impl User for RpcUser { self.get_status().map(|status| status.sync_info.latest_block_height) } - fn get_block(&self, index: u64) -> Option { + fn get_block(&self, index: u64) -> Option { System::new("actix").block_on(self.client.write().unwrap().block(index)).ok() } diff --git a/test-utils/testlib/src/user/runtime_user.rs b/test-utils/testlib/src/user/runtime_user.rs index 082e16cb8fa..4fba3f0864e 100644 --- a/test-utils/testlib/src/user/runtime_user.rs +++ b/test-utils/testlib/src/user/runtime_user.rs @@ -5,12 +5,13 @@ use std::sync::{Arc, Mutex, RwLock}; use tempdir::TempDir; use lazy_static::lazy_static; -use near_chain::Block; use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; -use near_primitives::rpc::{AccessKeyView, AccountView, CryptoHashView, ViewStateResult}; +use near_primitives::rpc::{ + AccessKeyView, AccountView, BlockView, CryptoHashView, ViewStateResult, +}; use near_primitives::transaction::{ FinalTransactionResult, FinalTransactionStatus, SignedTransaction, TransactionLog, TransactionResult, TransactionStatus, @@ -198,7 +199,7 @@ impl User for RuntimeUser { unimplemented!("get_best_block_index should not be implemented for RuntimeUser"); } - fn get_block(&self, _index: u64) -> Option { + fn get_block(&self, _index: u64) -> Option { unimplemented!("get_block should not be implemented for RuntimeUser"); } From 1ab221550d856bf7d27f7c991887a0412ad775d8 Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Thu, 15 Aug 2019 01:09:58 -0700 Subject: [PATCH 11/25] Fix issue with unnamed fields in enums in NBOR --- core/nbor-derive/src/lib.rs | 7 +++---- core/nbor/tests/test_simple_structs.rs | 6 +++--- core/primitives/src/transaction.rs | 7 +++++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/core/nbor-derive/src/lib.rs b/core/nbor-derive/src/lib.rs index af965f67659..86d999c6dc0 100644 --- a/core/nbor-derive/src/lib.rs +++ b/core/nbor-derive/src/lib.rs @@ -132,10 +132,10 @@ fn nbor_enum_ser(input: &ItemEnum) -> TokenStream2 { let field_ident = Ident::new(format!("id{}", field_idx).as_str(), Span::call_site()); variant_header.extend(quote! { #field_ident, }); + variant_body.extend(quote! { + nbor::Serializable::write(#field_ident, writer)?; + }) } - variant_body.extend(quote! { - nbor::Serializable::write(&#field_idx, writer)?; - }) } variant_header = quote! { ( #variant_header )}; } @@ -159,7 +159,6 @@ fn nbor_enum_ser(input: &ItemEnum) -> TokenStream2 { } } }; - println!("{}", res.to_string()); res } diff --git a/core/nbor/tests/test_simple_structs.rs b/core/nbor/tests/test_simple_structs.rs index 3177d1d148c..997f7c8b5bd 100644 --- a/core/nbor/tests/test_simple_structs.rs +++ b/core/nbor/tests/test_simple_structs.rs @@ -4,7 +4,7 @@ use nbor::{nbor, Deserializable, Serializable}; #[nbor_init(init)] struct A { x: u64, - // b: B, + b: B, y: f32, z: String, v: Vec, @@ -46,7 +46,7 @@ struct D { fn test_simple_struct() { let a = A { x: 1, - // b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, + b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, y: 4.0, z: "123".to_string(), v: vec!["qwe".to_string(), "zxc".to_string()], @@ -57,7 +57,7 @@ fn test_simple_struct() { let decoded_a = A::from_slice(&encoded_a).unwrap(); let expected_a = A { x: 1, - // b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, + b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, y: 4.0, z: a.z, v: a.v, diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index 33350d77e2a..ed666be0753 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -123,8 +123,8 @@ pub struct DeleteAccountAction { #[derive(nbor, Eq, Debug, Clone)] #[nbor_init(init)] pub struct SignedTransaction { - pub signature: StructSignature, pub transaction: Transaction, + pub signature: StructSignature, #[nbor_skip] hash: CryptoHash, } @@ -370,7 +370,7 @@ mod tests { // gas: 1_000, // deposit: 1_000_000, // }), - Action::Transfer(TransferAction { deposit: 1_000_000_000 }), + Action::Transfer(TransferAction { deposit: 123 }), // Action::Stake(StakeAction { public_key, stake: 1_000_000 }), // Action::AddKey(AddKeyAction { // public_key, @@ -386,6 +386,9 @@ mod tests { ], }; let signed_tx = SignedTransaction::new(DEFAULT_SIGNATURE, transaction); + println!("{:?}", signed_tx.to_vec().unwrap()); + // [9, 0, 0, 0, 116, 101, 115, 116, 46, 110, 101, 97, 114, 0, 15, 86, 165, 240, 40, 223, 192, 137, 236, 124, 57, 193, 24, 59, 50, 27, 77, 143, 137, 186, 91, 236, 158, 23, 98, 128, 60, 194, 73, 31, 110, 248, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + // [9, 0, 0, 0, 116, 101, 115, 116, 46, 110, 101, 97, 114, 0, 15, 86, 165, 240, 40, 223, 192, 137, 236, 124, 57, 193, 24, 59, 50, 27, 77, 143, 137, 186, 91, 236, 158, 23, 98, 128, 60, 194, 73, 31, 110, 248, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 49, 50, 51, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] let new_signed_tx = SignedTransaction::from_slice(&signed_tx.to_vec().unwrap()).unwrap(); assert_eq!( From a2ebf549f24ff0fbf4c51d77498902c84593d02f Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Thu, 15 Aug 2019 09:30:47 -0700 Subject: [PATCH 12/25] Clean up, making tests pass --- chain/chain/src/chain.rs | 4 +- chain/network/src/peer_store.rs | 10 ++--- chain/network/src/types.rs | 6 +-- core/nbor/src/de/mod.rs | 8 ++-- core/primitives/benches/serialization.rs | 5 +-- core/primitives/src/block.rs | 6 +-- core/primitives/src/hash.rs | 11 ++--- core/primitives/src/transaction.rs | 45 +++++++++---------- core/primitives/src/utils.rs | 14 ++++-- near/src/validator_manager.rs | 26 +++++------ test-utils/testlib/src/standard_test_cases.rs | 2 +- 11 files changed, 71 insertions(+), 66 deletions(-) diff --git a/chain/chain/src/chain.rs b/chain/chain/src/chain.rs index af7f6856934..70b5242f638 100644 --- a/chain/chain/src/chain.rs +++ b/chain/chain/src/chain.rs @@ -419,8 +419,8 @@ impl Chain { maybe_new_head = maybe_tip; queue.push(block_hash); } - Err(_) => { - debug!(target: "chain", "Orphan declined"); + Err(err) => { + debug!(target: "chain", "Orphan declined: {:?}", err); } } } diff --git a/chain/network/src/peer_store.rs b/chain/network/src/peer_store.rs index 186b3687aad..4a0f6ba2b37 100644 --- a/chain/network/src/peer_store.rs +++ b/chain/network/src/peer_store.rs @@ -12,6 +12,7 @@ use near_store::{Store, COL_PEERS}; use crate::types::{ FullPeerInfo, KnownPeerState, KnownPeerStatus, NetworkConfig, PeerId, PeerInfo, ReasonForBan, }; +use near_primitives::utils::to_timestamp; /// Known peers store, maintaining cache of known peers and connection to storage to save/load them. pub struct PeerStore { @@ -56,7 +57,7 @@ impl PeerStore { .peer_states .entry(peer_info.peer_info.id) .or_insert(KnownPeerState::new(peer_info.peer_info.clone())); - entry.last_seen = Utc::now().timestamp_millis() as u64; + entry.last_seen = to_timestamp(Utc::now()); entry.status = KnownPeerStatus::Connected; let mut store_update = self.store.store_update(); store_update.set_ser(COL_PEERS, peer_info.peer_info.id.as_ref(), entry)?; @@ -68,7 +69,7 @@ impl PeerStore { peer_id: &PeerId, ) -> Result<(), Box> { if let Some(peer_state) = self.peer_states.get_mut(peer_id) { - peer_state.last_seen = Utc::now().timestamp_millis() as u64; + peer_state.last_seen = to_timestamp(Utc::now()); peer_state.status = KnownPeerStatus::NotConnected; let mut store_update = self.store.store_update(); store_update.set_ser(COL_PEERS, peer_id.as_ref(), peer_state)?; @@ -84,9 +85,8 @@ impl PeerStore { ban_reason: ReasonForBan, ) -> Result<(), Box> { if let Some(peer_state) = self.peer_states.get_mut(peer_id) { - peer_state.last_seen = Utc::now().timestamp_millis() as u64; - peer_state.status = - KnownPeerStatus::Banned(ban_reason, Utc::now().timestamp_millis() as u64); + peer_state.last_seen = to_timestamp(Utc::now()); + peer_state.status = KnownPeerStatus::Banned(ban_reason, to_timestamp(Utc::now())); let mut store_update = self.store.store_update(); store_update.set_ser(COL_PEERS, peer_id.as_ref(), peer_state)?; store_update.commit().map_err(|err| err.into()) diff --git a/chain/network/src/types.rs b/chain/network/src/types.rs index 15bda8546f4..e47cb59d063 100644 --- a/chain/network/src/types.rs +++ b/chain/network/src/types.rs @@ -22,7 +22,7 @@ use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, BlockIndex, ShardId}; use crate::peer::Peer; -use near_primitives::utils::from_timestamp; +use near_primitives::utils::{from_timestamp, to_timestamp}; /// Current latest version of the protocol pub const PROTOCOL_VERSION: u32 = 2; @@ -343,8 +343,8 @@ impl KnownPeerState { KnownPeerState { peer_info, status: KnownPeerStatus::Unknown, - first_seen: Utc::now().timestamp_millis() as u64, - last_seen: Utc::now().timestamp_millis() as u64, + first_seen: to_timestamp(Utc::now()), + last_seen: to_timestamp(Utc::now()), } } diff --git a/core/nbor/src/de/mod.rs b/core/nbor/src/de/mod.rs index e097aa671c6..975c3406ce3 100644 --- a/core/nbor/src/de/mod.rs +++ b/core/nbor/src/de/mod.rs @@ -87,7 +87,7 @@ impl Deserializable for String { fn read(reader: &mut R) -> Result { let len = u32::read(reader)?; let mut result = vec![0; len as usize]; - reader.read_exact(&mut result)?; + reader.read(&mut result)?; String::from_utf8(result) .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string())) } @@ -100,7 +100,7 @@ where { fn read(reader: &mut R) -> Result { let len = u32::read(reader)?; - let mut result = vec![]; + let mut result = Vec::with_capacity(len as usize); for _ in 0..len { result.push(T::read(reader)?); } @@ -127,7 +127,7 @@ where { fn read(reader: &mut R) -> Result { let len = u32::read(reader)?; - let mut result = HashMap::default(); + let mut result = HashMap::with_capacity(len as usize); for _ in 0..len { let key = K::read(reader)?; let value = V::read(reader)?; @@ -145,7 +145,7 @@ where { fn read(reader: &mut R) -> Result { let len = u32::read(reader)?; - let mut result = BTreeMap::default(); + let mut result = BTreeMap::new(); for _ in 0..len { let key = K::read(reader)?; let value = V::read(reader)?; diff --git a/core/primitives/benches/serialization.rs b/core/primitives/benches/serialization.rs index 08f5c7ed8ea..405b1406801 100644 --- a/core/primitives/benches/serialization.rs +++ b/core/primitives/benches/serialization.rs @@ -7,13 +7,12 @@ use std::sync::Arc; use bencher::Bencher; use chrono::Utc; +use nbor::{Deserializable, Serializable}; use near_primitives::account::Account; use near_primitives::block::Block; use near_primitives::crypto::signature::{PublicKey, DEFAULT_SIGNATURE}; use near_primitives::crypto::signer::InMemorySigner; use near_primitives::hash::CryptoHash; -//use near_primitives::serialize::{Decode, Encode}; -use nbor::{Deserializable, Serializable}; use near_primitives::transaction::{Action, SignedTransaction, Transaction, TransferAction}; use near_primitives::types::MerkleHash; @@ -108,8 +107,8 @@ fn deserialize_account(bench: &mut Bencher) { benchmark_group!( benches, serialize_tx, - serialize_block, deserialize_tx, + serialize_block, deserialize_block, serialize_account, deserialize_account diff --git a/core/primitives/src/block.rs b/core/primitives/src/block.rs index cce2edff79b..af7b22a9712 100644 --- a/core/primitives/src/block.rs +++ b/core/primitives/src/block.rs @@ -10,7 +10,7 @@ use crate::crypto::signer::EDSigner; use crate::hash::{hash, CryptoHash}; use crate::transaction::SignedTransaction; use crate::types::{BlockIndex, MerkleHash, ValidatorStake}; -use crate::utils::from_timestamp; +use crate::utils::{from_timestamp, to_timestamp}; #[derive(nbor, Debug, Clone, Eq, PartialEq)] pub struct BlockHeaderInner { @@ -44,7 +44,7 @@ impl BlockHeaderInner { prev_hash: CryptoHash, prev_state_root: MerkleHash, tx_root: MerkleHash, - timestamp: DateTime, + time: DateTime, approval_mask: Vec, approval_sigs: Vec, total_weight: Weight, @@ -56,7 +56,7 @@ impl BlockHeaderInner { prev_hash, prev_state_root, tx_root, - timestamp: timestamp.timestamp_millis() as u64, + timestamp: to_timestamp(time), approval_mask, approval_sigs, total_weight, diff --git a/core/primitives/src/hash.rs b/core/primitives/src/hash.rs index c7887c43987..6d79d38317a 100644 --- a/core/primitives/src/hash.rs +++ b/core/primitives/src/hash.rs @@ -143,23 +143,24 @@ pub fn hash_struct(obj: &T) -> CryptoHash { #[cfg(test)] mod tests { use super::*; + use crate::rpc::CryptoHashView; #[derive(Deserialize, Serialize)] struct Struct { - hash: CryptoHash, + hash: CryptoHashView, } #[test] fn test_serialize_success() { let hash = hash(&[0, 1, 2]); - let s = Struct { hash }; + let s = Struct { hash: hash.into() }; let encoded = serde_json::to_string(&s).unwrap(); assert_eq!(encoded, "{\"hash\":\"CjNSmWXTWhC3EhRVtqLhRmWMTkRbU96wUACqxMtV1uGf\"}"); } #[test] fn test_serialize_default() { - let s = Struct { hash: CryptoHash::default() }; + let s = Struct { hash: CryptoHash::default().into() }; let encoded = serde_json::to_string(&s).unwrap(); assert_eq!(encoded, "{\"hash\":\"11111111111111111111111111111111\"}"); } @@ -168,14 +169,14 @@ mod tests { fn test_deserialize_default() { let encoded = "{\"hash\":\"11111111111111111111111111111111\"}"; let decoded: Struct = serde_json::from_str(&encoded).unwrap(); - assert_eq!(decoded.hash, CryptoHash::default()); + assert_eq!(decoded.hash, CryptoHash::default().into()); } #[test] fn test_deserialize_success() { let encoded = "{\"hash\":\"CjNSmWXTWhC3EhRVtqLhRmWMTkRbU96wUACqxMtV1uGf\"}"; let decoded: Struct = serde_json::from_str(&encoded).unwrap(); - assert_eq!(decoded.hash, hash(&[0, 1, 2])); + assert_eq!(decoded.hash, hash(&[0, 1, 2]).into()); } #[test] diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index ed666be0753..d697aae9e83 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -362,38 +362,35 @@ mod tests { nonce: 1, receiver_id: "123".to_string(), actions: vec![ - // Action::CreateAccount(CreateAccountAction {}), - // Action::DeployContract(DeployContractAction { code: vec![1, 2, 3] }), - // Action::FunctionCall(FunctionCallAction { - // method_name: "qqq".to_string(), - // args: vec![1, 2, 3], - // gas: 1_000, - // deposit: 1_000_000, - // }), + Action::CreateAccount(CreateAccountAction {}), + Action::DeployContract(DeployContractAction { code: vec![1, 2, 3] }), + Action::FunctionCall(FunctionCallAction { + method_name: "qqq".to_string(), + args: vec![1, 2, 3], + gas: 1_000, + deposit: 1_000_000, + }), Action::Transfer(TransferAction { deposit: 123 }), - // Action::Stake(StakeAction { public_key, stake: 1_000_000 }), - // Action::AddKey(AddKeyAction { - // public_key, - // access_key: AccessKey { - // amount: 1, - // balance_owner: Some("123".to_string()), - // contract_id: Some("321".to_string()), - // method_name: None, - // }, - // }), - // Action::DeleteKey(DeleteKeyAction { public_key }), - // Action::DeleteAccount(DeleteAccountAction { beneficiary_id: "123".to_string() }), + Action::Stake(StakeAction { public_key, stake: 1_000_000 }), + Action::AddKey(AddKeyAction { + public_key, + access_key: AccessKey { + amount: 1, + balance_owner: Some("123".to_string()), + contract_id: Some("321".to_string()), + method_name: None, + }, + }), + Action::DeleteKey(DeleteKeyAction { public_key }), + Action::DeleteAccount(DeleteAccountAction { beneficiary_id: "123".to_string() }), ], }; let signed_tx = SignedTransaction::new(DEFAULT_SIGNATURE, transaction); - println!("{:?}", signed_tx.to_vec().unwrap()); - // [9, 0, 0, 0, 116, 101, 115, 116, 46, 110, 101, 97, 114, 0, 15, 86, 165, 240, 40, 223, 192, 137, 236, 124, 57, 193, 24, 59, 50, 27, 77, 143, 137, 186, 91, 236, 158, 23, 98, 128, 60, 194, 73, 31, 110, 248, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] - // [9, 0, 0, 0, 116, 101, 115, 116, 46, 110, 101, 97, 114, 0, 15, 86, 165, 240, 40, 223, 192, 137, 236, 124, 57, 193, 24, 59, 50, 27, 77, 143, 137, 186, 91, 236, 158, 23, 98, 128, 60, 194, 73, 31, 110, 248, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 49, 50, 51, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] let new_signed_tx = SignedTransaction::from_slice(&signed_tx.to_vec().unwrap()).unwrap(); assert_eq!( to_base(&new_signed_tx.get_hash()), - "EsTRpLernDsH2hzznZ6wKMu1XYdyT4ynKK2H13hbMyzb" + "8QXFE2Kes67MYTMuqeKHW5jGQPD2t7Ue9vRMo2oB8Cw" ); } } diff --git a/core/primitives/src/utils.rs b/core/primitives/src/utils.rs index 3a1eb0bf9ac..8af30e52641 100644 --- a/core/primitives/src/utils.rs +++ b/core/primitives/src/utils.rs @@ -13,6 +13,9 @@ use chrono::{DateTime, NaiveDateTime, Utc}; pub const ACCOUNT_DATA_SEPARATOR: &[u8; 1] = b","; +/// Number of nano seconds in a second. +const NS_IN_SECOND: u64 = 1_000_000_000; + pub mod col { pub const ACCOUNT: &[u8] = &[0]; pub const CALLBACK: &[u8] = &[1]; @@ -167,13 +170,18 @@ macro_rules! unwrap_or_return(($obj: expr, $ret: expr) => (match $obj { } })); -/// Converts timestamp in millis into DateTime UTC time. +/// Converts timestamp in ns into DateTime UTC time. pub fn from_timestamp(timestamp: u64) -> DateTime { DateTime::from_utc( NaiveDateTime::from_timestamp( - (timestamp / 1000) as i64, - ((timestamp % 1000) * 1_000_000) as u32, + (timestamp / NS_IN_SECOND) as i64, + (timestamp % NS_IN_SECOND) as u32, ), Utc, ) } + +/// Converts DateTime UTC time into timestamp in ns. +pub fn to_timestamp(time: DateTime) -> u64 { + time.timestamp_nanos() as u64 +} diff --git a/near/src/validator_manager.rs b/near/src/validator_manager.rs index 37a2425d7eb..f613a72b7d6 100644 --- a/near/src/validator_manager.rs +++ b/near/src/validator_manager.rs @@ -728,7 +728,7 @@ mod test { assignment( vec![("test1", 1_000_000)], vec![0], - vec![vec![0, 0]], + vec![vec![0], vec![0]], vec![], 0, change_stake(vec![("test1", 1_000_000)]) @@ -759,9 +759,9 @@ mod test { vec![0, 1, 0, 0, 1, 2], vec![ // Shard 0 is block produced / validated by all block producers & fisherman. - vec![0, 1, 2], + vec![0, 1, 0, 0, 1, 2], vec![0, 1], - vec![0, 2], + vec![0, 0], vec![1, 2], vec![0, 1] ], @@ -794,7 +794,7 @@ mod test { let expected0 = assignment( vec![("test1", amount_staked)], vec![0, 0], - vec![vec![0]], + vec![vec![0, 0]], vec![], 1, change_stake(vec![("test1", amount_staked)]), @@ -943,7 +943,7 @@ mod test { &assignment( vec![("test1", amount_staked), ("test3", amount_staked)], vec![0, 1, 0], - vec![vec![0, 1]], + vec![vec![0, 1, 0]], vec![], 9, change_stake(vec![ @@ -963,7 +963,7 @@ mod test { &assignment( vec![("test4", amount_staked), ("test2", amount_staked)], vec![0, 1, 0], - vec![vec![0, 1]], + vec![vec![0, 1, 0]], vec![], 9, change_stake(vec![ @@ -1046,7 +1046,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![0]], + vec![vec![0, 0]], vec![], 4, change_stake(vec![("test1", 0), ("test2", amount_staked)]) @@ -1057,7 +1057,7 @@ mod test { &assignment( vec![("test1", amount_staked)], vec![0, 0], - vec![vec![0]], + vec![vec![0, 0]], vec![], 4, change_stake(vec![("test1", amount_staked), ("test2", 0)]) @@ -1090,7 +1090,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![0]], + vec![vec![0, 0]], vec![], 4, change_stake(vec![("test1", 0), ("test2", amount_staked)]) @@ -1104,7 +1104,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![0]], + vec![vec![0, 0]], vec![], 6, change_stake(vec![("test1", 0), ("test2", amount_staked)]) @@ -1137,7 +1137,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![0]], + vec![vec![0, 0]], vec![], 4, change_stake(vec![("test1", 0), ("test2", amount_staked)]) @@ -1207,7 +1207,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![0]], + vec![vec![0, 0]], vec![], 4, change_stake(vec![("test1", 0), ("test2", amount_staked)]) @@ -1227,7 +1227,7 @@ mod test { &assignment( vec![("test2", amount_staked)], vec![0, 0], - vec![vec![0]], + vec![vec![0, 0]], vec![], 6, change_stake(vec![("test1", 0), ("test2", amount_staked)]) diff --git a/test-utils/testlib/src/standard_test_cases.rs b/test-utils/testlib/src/standard_test_cases.rs index d0be7fbc1b8..3f7d94c4698 100644 --- a/test-utils/testlib/src/standard_test_cases.rs +++ b/test-utils/testlib/src/standard_test_cases.rs @@ -774,7 +774,7 @@ pub fn test_stake_fail_not_enough_rent(node: impl Node) { alice_account(), eve_account(), node.signer().public_key(), - 119_000_000_000_000_010, + 134_000_000_000_000_010, ); let transaction_result = node_user.stake(eve_account(), node.signer().public_key(), 5); assert_eq!(transaction_result.status, FinalTransactionStatus::Failed); From c0e7ea3b69ff683f1798133a81d31b9c33dccbcd Mon Sep 17 00:00:00 2001 From: Maksym Zavershynskyi Date: Thu, 15 Aug 2019 11:27:15 -0700 Subject: [PATCH 13/25] Fix usize suffix warning --- core/nbor-derive/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/nbor-derive/src/lib.rs b/core/nbor-derive/src/lib.rs index 86d999c6dc0..7408a8206cb 100644 --- a/core/nbor-derive/src/lib.rs +++ b/core/nbor-derive/src/lib.rs @@ -5,7 +5,7 @@ use proc_macro::TokenStream; use proc_macro2::Span; use quote::quote; use syn::export::TokenStream2; -use syn::{Attribute, Fields, Ident, ItemEnum, ItemStruct, ItemUnion, Meta, NestedMeta}; +use syn::{Attribute, Fields, Ident, Index, ItemEnum, ItemStruct, ItemUnion, Meta, NestedMeta}; #[proc_macro_derive(nbor, attributes(nbor_skip, nbor_init))] pub fn nbor(input: TokenStream) -> TokenStream { @@ -78,6 +78,7 @@ fn nbor_struct_ser(input: &ItemStruct) -> TokenStream2 { } Fields::Unnamed(fields) => { for field_idx in 0..fields.unnamed.len() { + let field_idx = Index { index: field_idx as u32, span: Span::call_site() }; let delta = quote! { nbor::Serializable::write(&self.#field_idx, writer)?; }; From a78a24653094ee7ece9e06fe49404e5c754ff291 Mon Sep 17 00:00:00 2001 From: Maksym Zavershynskyi Date: Thu, 15 Aug 2019 14:14:30 -0700 Subject: [PATCH 14/25] Use u8 instead of u32 for enum variants --- core/nbor-derive/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/nbor-derive/src/lib.rs b/core/nbor-derive/src/lib.rs index 7408a8206cb..bd7676cf1d6 100644 --- a/core/nbor-derive/src/lib.rs +++ b/core/nbor-derive/src/lib.rs @@ -101,7 +101,7 @@ fn nbor_enum_ser(input: &ItemEnum) -> TokenStream2 { let name = &input.ident; let mut body = TokenStream2::new(); for (variant_idx, variant) in input.variants.iter().enumerate() { - let variant_idx = variant_idx as u32; + let variant_idx = variant_idx as u8; let variant_ident = &variant.ident; let mut variant_header = TokenStream2::new(); let mut variant_body = TokenStream2::new(); @@ -144,7 +144,7 @@ fn nbor_enum_ser(input: &ItemEnum) -> TokenStream2 { } body.extend(quote!( #name::#variant_ident #variant_header => { - let variant_idx: u32 = #variant_idx; + let variant_idx: u8 = #variant_idx; writer.write(&variant_idx.to_le_bytes())?; #variant_body } @@ -233,7 +233,7 @@ fn nbor_enum_de(input: &ItemEnum) -> TokenStream2 { let init_method = contains_initialize_with(&input.attrs); let mut variant_arms = TokenStream2::new(); for (variant_idx, variant) in input.variants.iter().enumerate() { - let variant_idx = variant_idx as u32; + let variant_idx = variant_idx as u8; let variant_ident = &variant.ident; let mut variant_header = TokenStream2::new(); match &variant.fields { @@ -269,9 +269,9 @@ fn nbor_enum_de(input: &ItemEnum) -> TokenStream2 { }); } let variant_idx = quote! { - let mut variant_idx = [0u8; std::mem::size_of::()]; + let mut variant_idx = [0u8; std::mem::size_of::()]; reader.read(&mut variant_idx)?; - let variant_idx = u32::from_le_bytes(variant_idx); + let variant_idx = u8::from_le_bytes(variant_idx); }; if let Some(method_ident) = init_method { quote! { From 8c155a144a2e22d9260831a441275e38df803a21 Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Thu, 15 Aug 2019 15:13:30 -0700 Subject: [PATCH 15/25] Add transaction view for RPC and a bit of KeyFile changes to make serialize properly --- core/primitives/src/crypto/signer.rs | 67 ++++++++++++---- core/primitives/src/rpc.rs | 111 +++++++++++++++++++++++++-- core/primitives/src/transaction.rs | 2 +- near/src/config.rs | 6 +- test-utils/keystore/src/main.rs | 2 +- test-utils/testlib/src/node/mod.rs | 2 +- 6 files changed, 161 insertions(+), 29 deletions(-) diff --git a/core/primitives/src/crypto/signer.rs b/core/primitives/src/crypto/signer.rs index c8550ffc59c..a87afe6a8c9 100644 --- a/core/primitives/src/crypto/signer.rs +++ b/core/primitives/src/crypto/signer.rs @@ -11,6 +11,7 @@ use rand::Rng; use crate::crypto::aggregate_signature::BlsPublicKey; use crate::crypto::signature::{get_key_pair, sign, verify, PublicKey, SecretKey, Signature}; +use crate::rpc::{PublicKeyView, SecretKeyView}; use crate::types::{AccountId, PartialSignature}; /// Trait to abstract the signer account. @@ -39,8 +40,26 @@ pub trait BLSSigner: Sync + Send { #[derive(Serialize, Deserialize)] pub struct KeyFile { - pub public_key: PublicKey, - pub secret_key: SecretKey, + pub account_id: AccountId, + pub public_key: PublicKeyView, + pub secret_key: SecretKeyView, +} + +impl KeyFile { + fn write_to_file(&self, path: &Path) { + let mut file = File::create(path).expect("Failed to create / write a key file."); + let str = serde_json::to_string_pretty(self).expect("Error serializing the key file."); + if let Err(err) = file.write_all(str.as_bytes()) { + panic!("Failed to write a key file {}", err); + } + } + + fn from_file(path: &Path) -> Self { + let mut file = File::open(path).expect("Could not open key file."); + let mut content = String::new(); + file.read_to_string(&mut content).expect("Could not read from key file."); + serde_json::from_str(&content).expect("Failed to deserialize KeyFile") + } } pub fn write_key_file( @@ -52,7 +71,11 @@ pub fn write_key_file( fs::create_dir_all(key_store_path).unwrap(); } - let key_file = KeyFile { public_key, secret_key }; + let key_file = KeyFile { + account_id: "".to_string(), + public_key: public_key.into(), + secret_key: secret_key.into(), + }; let key_file_path = key_store_path.join(Path::new(&public_key.to_string())); let serialized = serde_json::to_string(&key_file).unwrap(); fs::write(key_file_path, serialized).unwrap(); @@ -187,10 +210,7 @@ impl InMemorySigner { /// Read key file into signer. pub fn from_file(path: &Path) -> Self { - let mut file = File::open(path).expect("Could not open key file."); - let mut content = String::new(); - file.read_to_string(&mut content).expect("Could not read from key file."); - InMemorySigner::from(content.as_str()) + KeyFile::from_file(path).into() } /// Initialize `InMemorySigner` with a random ED25519 and BLS keys, and random account id. Used @@ -210,15 +230,33 @@ impl From<&str> for InMemorySigner { } } -impl From for KeyFile { - fn from(signer: InMemorySigner) -> KeyFile { - KeyFile { public_key: signer.public_key, secret_key: signer.secret_key.clone() } +impl From for InMemorySigner { + fn from(key_file: KeyFile) -> Self { + Self { + account_id: key_file.account_id, + public_key: key_file.public_key.into(), + secret_key: key_file.secret_key.into(), + } + } +} + +impl From<&InMemorySigner> for KeyFile { + fn from(signer: &InMemorySigner) -> KeyFile { + KeyFile { + account_id: signer.account_id.clone(), + public_key: signer.public_key.into(), + secret_key: signer.secret_key.clone().into(), + } } } impl From> for KeyFile { fn from(signer: Arc) -> KeyFile { - KeyFile { public_key: signer.public_key, secret_key: signer.secret_key.clone() } + KeyFile { + account_id: signer.account_id.clone(), + public_key: signer.public_key.into(), + secret_key: signer.secret_key.clone().into(), + } } } @@ -245,10 +283,7 @@ impl EDSigner for InMemorySigner { /// Save signer into key file. fn write_to_file(&self, path: &Path) { - let mut file = File::create(path).expect("Failed to create / write a key file."); - let str = serde_json::to_string_pretty(self).expect("Error serializing the key file."); - if let Err(err) = file.write_all(str.as_bytes()) { - panic!("Failed to write a key file {}", err); - } + let key_file: KeyFile = self.into(); + key_file.write_to_file(path); } } diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index 64904f12707..d0be53dc9d2 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::convert::TryFrom; +use std::fmt; use chrono::{DateTime, Utc}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -8,9 +9,11 @@ use crate::account::{AccessKey, Account}; use crate::block::{Block, BlockHeader, BlockHeaderInner}; use crate::crypto::signature::{PublicKey, SecretKey, Signature}; use crate::hash::CryptoHash; -use crate::serialize::{from_base, to_base, u128_dec_format}; -use crate::transaction::SignedTransaction; -use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage, ValidatorStake, Version}; +use crate::serialize::{from_base, to_base, to_base64, u128_dec_format}; +use crate::transaction::{Action, SignedTransaction}; +use crate::types::{ + AccountId, Balance, BlockIndex, Gas, Nonce, StorageUsage, ValidatorStake, Version, +}; #[derive(Debug, Eq, PartialEq, Clone)] pub struct PublicKeyView(Vec); @@ -36,6 +39,12 @@ impl<'de> Deserialize<'de> for PublicKeyView { } } +impl fmt::Display for PublicKeyView { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", to_base(&self.0)) + } +} + impl From for PublicKeyView { fn from(public_key: PublicKey) -> Self { Self(public_key.0.as_ref().to_vec()) @@ -78,6 +87,13 @@ impl From for SecretKeyView { } } +impl From for SecretKey { + fn from(view: SecretKeyView) -> Self { + TryFrom::<&[u8]>::try_from(view.0.as_ref()) + .expect("Failed to get SecretKeyView from SecretKey") + } +} + #[derive(Debug, Clone)] pub struct SignatureView(Vec); @@ -406,11 +422,92 @@ impl From for BlockView { } #[derive(Serialize, Deserialize, Debug)] -pub struct SignedTransactionView {} +pub enum ActionView { + CreateAccount, + DeployContract { + code: String, + }, + FunctionCall { + method_name: String, + args: String, + gas: Gas, + #[serde(with = "u128_dec_format")] + deposit: Balance, + }, + Transfer { + #[serde(with = "u128_dec_format")] + deposit: Balance, + }, + Stake { + #[serde(with = "u128_dec_format")] + stake: Balance, + public_key: PublicKeyView, + }, + AddKey { + public_key: PublicKeyView, + access_key: AccessKeyView, + }, + DeleteKey { + public_key: PublicKey, + }, + DeleteAccount { + beneficiary_id: AccountId, + }, +} + +impl From for ActionView { + fn from(action: Action) -> Self { + match action { + Action::CreateAccount(_) => ActionView::CreateAccount, + Action::DeployContract(action) => { + ActionView::DeployContract { code: to_base64(&action.code) } + } + Action::FunctionCall(action) => ActionView::FunctionCall { + method_name: action.method_name, + args: to_base(&action.args), + gas: action.gas, + deposit: action.deposit, + }, + Action::Transfer(action) => ActionView::Transfer { deposit: action.deposit }, + Action::Stake(action) => { + ActionView::Stake { stake: action.stake, public_key: action.public_key.into() } + } + Action::AddKey(action) => ActionView::AddKey { + public_key: action.public_key.into(), + access_key: action.access_key.into(), + }, + Action::DeleteKey(action) => { + ActionView::DeleteKey { public_key: action.public_key.into() } + } + Action::DeleteAccount(action) => { + ActionView::DeleteAccount { beneficiary_id: action.beneficiary_id } + } + } + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SignedTransactionView { + signer_id: AccountId, + public_key: PublicKeyView, + nonce: Nonce, + receiver_id: AccountId, + actions: Vec, + signature: SignatureView, + hash: CryptoHashView, +} impl From for SignedTransactionView { - fn from(signed_tx: SignedTransaction) -> Self { - // MOO - SignedTransactionView {} + fn from(mut signed_tx: SignedTransaction) -> Self { + let hash = signed_tx.get_hash().into(); + SignedTransactionView { + signer_id: signed_tx.transaction.signer_id, + public_key: signed_tx.transaction.public_key.into(), + nonce: signed_tx.transaction.nonce, + receiver_id: signed_tx.transaction.receiver_id, + actions: signed_tx.transaction.actions.drain(..).map(|action| action.into()).collect(), + signature: signed_tx.signature.into(), + hash, + } } } diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index d697aae9e83..766f2606c0f 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -390,7 +390,7 @@ mod tests { assert_eq!( to_base(&new_signed_tx.get_hash()), - "8QXFE2Kes67MYTMuqeKHW5jGQPD2t7Ue9vRMo2oB8Cw" + "6jaty3HYh35hQUj2PSN4rhVoZXx5EZ1xUBhBgXjko8fa" ); } } diff --git a/near/src/config.rs b/near/src/config.rs index b237c255324..5e62c43fc01 100644 --- a/near/src/config.rs +++ b/near/src/config.rs @@ -222,8 +222,8 @@ impl NearConfig { block_header_fetch_horizon: 50, }, network_config: NetworkConfig { - public_key: network_key_pair.public_key, - secret_key: network_key_pair.secret_key, + public_key: network_key_pair.public_key.into(), + secret_key: network_key_pair.secret_key.into(), account_id: block_producer.clone().map(|bp| bp.account_id.clone()), addr: if config.network.addr.is_empty() { None @@ -672,7 +672,7 @@ pub fn load_config(dir: &Path) -> NearConfig { None }; let network_signer = InMemorySigner::from_file(&dir.join(config.node_key_file.clone())); - NearConfig::new(config, &genesis_config, network_signer.into(), block_producer) + NearConfig::new(config, &genesis_config, (&network_signer).into(), block_producer) } pub fn load_test_config(seed: &str, port: u16, genesis_config: &GenesisConfig) -> NearConfig { diff --git a/test-utils/keystore/src/main.rs b/test-utils/keystore/src/main.rs index 70771b5c3ff..dcabefdb2c0 100644 --- a/test-utils/keystore/src/main.rs +++ b/test-utils/keystore/src/main.rs @@ -63,7 +63,7 @@ fn sign_data(matches: &ArgMatches) { let data = matches.value_of("data").unwrap(); let bytes = from_base(data).unwrap(); - let signature = sign(&bytes, &key_file.secret_key); + let signature = sign(&bytes, &key_file.secret_key.into()); let encoded = to_base(&signature); print!("{}", encoded); } diff --git a/test-utils/testlib/src/node/mod.rs b/test-utils/testlib/src/node/mod.rs index 3f9adffa740..f7c88b4e4d8 100644 --- a/test-utils/testlib/src/node/mod.rs +++ b/test-utils/testlib/src/node/mod.rs @@ -125,7 +125,7 @@ fn near_configs_to_node_configs( result.push(NodeConfig::Thread(NearConfig::new( configs[i].clone(), &genesis_config, - network_signers[i].clone().into(), + (&network_signers[i]).into(), Some(signers[i].clone().into()), ))) } From 6320ad90dddcd2129cfe2a31c8d993dded7b64dc Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Tue, 13 Aug 2019 17:48:10 -0700 Subject: [PATCH 16/25] Remove #update-as from dependencies (#1168) --- tests/hello/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/hello/package.json b/tests/hello/package.json index 93929bd5a8d..8c77d010070 100644 --- a/tests/hello/package.json +++ b/tests/hello/package.json @@ -2,8 +2,8 @@ "name": "hello-wasm", "version": "0.1.0", "dependencies": { - "assemblyscript-json": "github:nearprotocol/assemblyscript-json#update-as", - "near-runtime-ts": "github:nearprotocol/near-runtime-ts#update-as" + "assemblyscript-json": "github:nearprotocol/assemblyscript-json", + "near-runtime-ts": "github:nearprotocol/near-runtime-ts" }, "scripts": { "build": "mkdir -p ./out && near build && cp ./out/main.wasm ../hello.wasm" From 4eb45d4f247a1b8ba14a619517cc5bd0c38af509 Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Tue, 13 Aug 2019 18:09:39 -0700 Subject: [PATCH 17/25] Remove #nightshade from near-shell dependency (#1169) --- tests/hello/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/hello/package.json b/tests/hello/package.json index 8c77d010070..5d26c5fc66d 100644 --- a/tests/hello/package.json +++ b/tests/hello/package.json @@ -10,7 +10,7 @@ }, "devDependencies": { "gulp": "^3.0.0", - "near-shell": "github:nearprotocol/near-shell#nightshade", + "near-shell": "github:nearprotocol/near-shell", "bignum": "github:MaxGraey/bignum.wasm" } } From 9cb1f079471c2a7e4879d75287cb3b8df4c3ec43 Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Thu, 15 Aug 2019 18:20:06 -0700 Subject: [PATCH 18/25] AccessKey changes are done --- chain/client/src/types.rs | 4 +- chain/client/src/view_client.rs | 14 +-- chain/jsonrpc/client/src/lib.rs | 4 +- chain/jsonrpc/src/lib.rs | 3 +- chain/jsonrpc/tests/rpc_transactions.rs | 3 +- core/primitives/src/rpc.rs | 79 ++++++++++++++++- core/primitives/src/transaction.rs | 87 ++----------------- runtime/runtime/src/store.rs | 5 +- runtime/runtime/tests/test_evil_contracts.rs | 2 +- scripts/test_nearlib.sh | 2 +- test-utils/testlib/src/standard_test_cases.rs | 2 +- test-utils/testlib/src/user/mod.rs | 5 +- test-utils/testlib/src/user/rpc_user.rs | 5 +- test-utils/testlib/src/user/runtime_user.rs | 11 ++- tests/test_errors.rs | 4 +- 15 files changed, 118 insertions(+), 112 deletions(-) diff --git a/chain/client/src/types.rs b/chain/client/src/types.rs index f40481f8183..eeec3d6b551 100644 --- a/chain/client/src/types.rs +++ b/chain/client/src/types.rs @@ -7,9 +7,9 @@ use chrono::{DateTime, Utc}; use near_primitives::crypto::signer::{AccountSigner, EDSigner, InMemorySigner}; use near_primitives::hash::CryptoHash; -use near_primitives::rpc::{BlockView, QueryResponse}; +use near_primitives::rpc::{BlockView, FinalTransactionResult, QueryResponse}; pub use near_primitives::rpc::{StatusResponse, StatusSyncInfo}; -use near_primitives::transaction::{FinalTransactionResult, TransactionResult}; +use near_primitives::transaction::TransactionResult; use near_primitives::types::{AccountId, BlockIndex, ShardId, Version}; /// Combines errors coming from chain, tx pool and block producer. diff --git a/chain/client/src/view_client.rs b/chain/client/src/view_client.rs index 025db9e8018..aa005b9fdec 100644 --- a/chain/client/src/view_client.rs +++ b/chain/client/src/view_client.rs @@ -8,11 +8,10 @@ use chrono::{DateTime, Utc}; use near_chain::{Chain, ErrorKind, RuntimeAdapter}; use near_primitives::hash::CryptoHash; -use near_primitives::rpc::{BlockView, QueryResponse}; -use near_primitives::transaction::{ - FinalTransactionResult, FinalTransactionStatus, TransactionLog, TransactionResult, - TransactionStatus, +use near_primitives::rpc::{ + BlockView, FinalTransactionResult, FinalTransactionStatus, QueryResponse, }; +use near_primitives::transaction::{TransactionLog, TransactionResult, TransactionStatus}; use near_store::Store; use crate::types::{Error, GetBlock, Query, TxStatus}; @@ -68,7 +67,7 @@ impl ViewClientActor { &mut self, hash: &CryptoHash, ) -> Result { - let transactions = self.get_recursive_transaction_results(hash)?; + let mut transactions = self.get_recursive_transaction_results(hash)?; let status = if transactions .iter() .find(|t| &t.result.status == &TransactionStatus::Failed) @@ -84,7 +83,10 @@ impl ViewClientActor { } else { FinalTransactionStatus::Completed }; - Ok(FinalTransactionResult { status, transactions }) + Ok(FinalTransactionResult { + status, + transactions: transactions.drain(..).map(|t| t.into()).collect(), + }) } } diff --git a/chain/jsonrpc/client/src/lib.rs b/chain/jsonrpc/client/src/lib.rs index 2761aaf97a8..097067268ec 100644 --- a/chain/jsonrpc/client/src/lib.rs +++ b/chain/jsonrpc/client/src/lib.rs @@ -4,8 +4,8 @@ use actix_web::client::Client; use futures::Future; use serde::Serialize; -use near_primitives::rpc::{BlockView, QueryResponse, StatusResponse}; -use near_primitives::transaction::{FinalTransactionResult, TransactionResult}; +use near_primitives::rpc::{BlockView, FinalTransactionResult, QueryResponse, StatusResponse}; +use near_primitives::transaction::TransactionResult; use near_primitives::types::BlockIndex; pub mod message; diff --git a/chain/jsonrpc/src/lib.rs b/chain/jsonrpc/src/lib.rs index d1576eff8ca..79bfe681bd5 100644 --- a/chain/jsonrpc/src/lib.rs +++ b/chain/jsonrpc/src/lib.rs @@ -21,8 +21,9 @@ pub use near_jsonrpc_client as client; use near_jsonrpc_client::message as message; use near_network::{NetworkClientMessages, NetworkClientResponses}; use near_primitives::hash::CryptoHash; +use near_primitives::rpc::FinalTransactionStatus; use near_primitives::serialize::{BaseEncode, from_base, from_base64}; -use near_primitives::transaction::{FinalTransactionStatus, SignedTransaction}; +use near_primitives::transaction::SignedTransaction; use near_primitives::types::BlockIndex; pub mod test_utils; diff --git a/chain/jsonrpc/tests/rpc_transactions.rs b/chain/jsonrpc/tests/rpc_transactions.rs index cf0d0e81f2f..465713c6799 100644 --- a/chain/jsonrpc/tests/rpc_transactions.rs +++ b/chain/jsonrpc/tests/rpc_transactions.rs @@ -8,9 +8,10 @@ use near_jsonrpc::client::new_client; use near_jsonrpc::test_utils::start_all; use near_network::test_utils::{wait_or_panic, WaitOrTimeout}; use near_primitives::crypto::signer::InMemorySigner; +use near_primitives::rpc::FinalTransactionStatus; use near_primitives::serialize::to_base64; use near_primitives::test_utils::init_test_logger; -use near_primitives::transaction::{FinalTransactionStatus, SignedTransaction}; +use near_primitives::transaction::SignedTransaction; /// Test sending transaction via json rpc without waiting. #[test] diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index 30cb76ba874..07182b82442 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -9,8 +9,9 @@ use crate::account::{AccessKey, AccessKeyPermission, Account, FunctionCallPermis use crate::block::{Block, BlockHeader, BlockHeaderInner}; use crate::crypto::signature::{PublicKey, SecretKey, Signature}; use crate::hash::CryptoHash; +use crate::logging; use crate::serialize::{from_base, option_u128_dec_format, to_base, to_base64, u128_dec_format}; -use crate::transaction::{Action, SignedTransaction}; +use crate::transaction::{Action, SignedTransaction, TransactionLog, TransactionResult}; use crate::types::{ AccountId, Balance, BlockIndex, Gas, Nonce, StorageUsage, ValidatorStake, Version, }; @@ -527,3 +528,79 @@ impl From for SignedTransactionView { } } } + +#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] +pub enum FinalTransactionStatus { + Unknown, + Started, + Failed, + Completed, +} + +impl Default for FinalTransactionStatus { + fn default() -> Self { + FinalTransactionStatus::Unknown + } +} + +impl FinalTransactionStatus { + pub fn to_code(&self) -> u64 { + match self { + FinalTransactionStatus::Completed => 0, + FinalTransactionStatus::Failed => 1, + FinalTransactionStatus::Started => 2, + FinalTransactionStatus::Unknown => std::u64::MAX, + } + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct TransactionLogView { + pub hash: CryptoHashView, + pub result: TransactionResult, +} + +impl From for TransactionLogView { + fn from(log: TransactionLog) -> Self { + Self { hash: log.hash.into(), result: log.result } + } +} + +/// Result of transaction and all of subsequent the receipts. +#[derive(Serialize, Deserialize)] +pub struct FinalTransactionResult { + /// Status of the whole transaction and it's receipts. + pub status: FinalTransactionStatus, + /// Transaction results. + pub transactions: Vec, +} + +impl fmt::Debug for FinalTransactionResult { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("FinalTransactionResult") + .field("status", &self.status) + .field("transactions", &format_args!("{}", logging::pretty_vec(&self.transactions))) + .finish() + } +} + +impl FinalTransactionResult { + pub fn final_log(&self) -> String { + let mut logs = vec![]; + for transaction in &self.transactions { + for line in &transaction.result.logs { + logs.push(line.clone()); + } + } + logs.join("\n") + } + + pub fn last_result(&self) -> Vec { + for transaction in self.transactions.iter().rev() { + if let Some(r) = &transaction.result.result { + return r.clone(); + } + } + vec![] + } +} diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index e93b2582099..8af8609071d 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -9,7 +9,7 @@ use crate::crypto::signature::{verify, PublicKey}; use crate::crypto::signer::EDSigner; use crate::hash::{hash, CryptoHash}; use crate::logging; -use crate::types::{AccountId, Balance, Gas, Nonce, ShardId, StructSignature}; +use crate::types::{AccountId, Balance, Gas, Nonce, StructSignature}; pub type LogEntry = String; @@ -191,38 +191,13 @@ pub enum TransactionStatus { Failed, } -#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] -pub enum FinalTransactionStatus { - Unknown, - Started, - Failed, - Completed, -} - -impl Default for FinalTransactionStatus { - fn default() -> Self { - FinalTransactionStatus::Unknown - } -} - -impl FinalTransactionStatus { - pub fn to_code(&self) -> u64 { - match self { - FinalTransactionStatus::Completed => 0, - FinalTransactionStatus::Failed => 1, - FinalTransactionStatus::Started => 2, - FinalTransactionStatus::Unknown => std::u64::MAX, - } - } -} - impl Default for TransactionStatus { fn default() -> Self { TransactionStatus::Unknown } } -#[derive(nbor, PartialEq, Clone, Serialize, Deserialize, Default)] +#[derive(nbor, PartialEq, Clone, Default, Serialize, Deserialize)] pub struct TransactionResult { /// Transaction status. pub status: TransactionStatus, @@ -245,65 +220,13 @@ impl fmt::Debug for TransactionResult { } } -#[derive(PartialEq, Clone, Serialize, Deserialize, Default, Debug)] +#[derive(PartialEq, Clone, Default, Debug)] pub struct TransactionLog { /// Hash of a transaction or a receipt that generated this result. pub hash: CryptoHash, pub result: TransactionResult, } -/// Result of transaction and all of subsequent the receipts. -#[derive(PartialEq, Clone, Serialize, Deserialize, Default)] -pub struct FinalTransactionResult { - /// Status of the whole transaction and it's receipts. - pub status: FinalTransactionStatus, - /// Transaction results. - pub transactions: Vec, -} - -impl fmt::Debug for FinalTransactionResult { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("FinalTransactionResult") - .field("status", &self.status) - .field("transactions", &format_args!("{}", logging::pretty_vec(&self.transactions))) - .finish() - } -} - -impl FinalTransactionResult { - pub fn final_log(&self) -> String { - let mut logs = vec![]; - for transaction in &self.transactions { - for line in &transaction.result.logs { - logs.push(line.clone()); - } - } - logs.join("\n") - } - - pub fn last_result(&self) -> Vec { - for transaction in self.transactions.iter().rev() { - if let Some(r) = &transaction.result.result { - return r.clone(); - } - } - vec![] - } -} - -/// Represents address of certain transaction within block -#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] -pub struct TransactionAddress { - /// Block hash - pub block_hash: CryptoHash, - /// Transaction index within the block. If it is a receipt, - /// index is the index in the receipt block. - pub index: usize, - /// Only for receipts. The shard that the receipt - /// block is supposed to go - pub shard_id: Option, -} - pub fn verify_transaction_signature( transaction: &SignedTransaction, public_keys: &[PublicKey], @@ -376,7 +299,7 @@ mod tests { Action::AddKey(AddKeyAction { public_key, access_key: AccessKey { - nonce: 2, + nonce: 0, permission: AccessKeyPermission::FunctionCall(FunctionCallPermission { allowance: None, receiver_id: "zzz".to_string(), @@ -393,7 +316,7 @@ mod tests { assert_eq!( to_base(&new_signed_tx.get_hash()), - "6jaty3HYh35hQUj2PSN4rhVoZXx5EZ1xUBhBgXjko8fa" + "244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM" ); } } diff --git a/runtime/runtime/src/store.rs b/runtime/runtime/src/store.rs index 99c0fd96699..faf57a9cef1 100644 --- a/runtime/runtime/src/store.rs +++ b/runtime/runtime/src/store.rs @@ -1,6 +1,3 @@ -use std::convert::TryFrom; - -use near_primitives::crypto::signature::PublicKey; use near_primitives::hash::CryptoHash; use near_primitives::rpc::{AccessKeyView, AccountView, PublicKeyView}; use near_primitives::types::AccountId; @@ -20,7 +17,7 @@ pub enum StateRecord { } impl StateRecord { - pub fn account(account_id: &str, public_key: &str, amount: u128, staked: u128) -> Self { + pub fn account(account_id: &str, amount: u128, staked: u128) -> Self { StateRecord::Account { account_id: account_id.to_string(), account: AccountView { diff --git a/runtime/runtime/tests/test_evil_contracts.rs b/runtime/runtime/tests/test_evil_contracts.rs index 5706f3dcb2a..fd8228ca666 100644 --- a/runtime/runtime/tests/test_evil_contracts.rs +++ b/runtime/runtime/tests/test_evil_contracts.rs @@ -1,4 +1,4 @@ -use near_primitives::transaction::FinalTransactionStatus; +use near_primitives::rpc::FinalTransactionStatus; use near_primitives::types::Gas; use testlib::node::{Node, RuntimeNode}; diff --git a/scripts/test_nearlib.sh b/scripts/test_nearlib.sh index a7994f14f80..51aab53252e 100755 --- a/scripts/test_nearlib.sh +++ b/scripts/test_nearlib.sh @@ -9,7 +9,7 @@ trap 'pkill -15 -P $NEAR_PID' 0 # Run nearlib tests rm -rf nearlib -git clone --single-branch --branch master https://github.com/nearprotocol/nearlib.git nearlib +git clone --single-branch --branch nep8 https://github.com/nearprotocol/nearlib.git nearlib cd nearlib export NEAR_PROTOS_DIR="../core/protos/protos" export HELLO_WASM_PATH="../tests/hello.wasm" diff --git a/test-utils/testlib/src/standard_test_cases.rs b/test-utils/testlib/src/standard_test_cases.rs index 1add073e022..872b9f5be99 100644 --- a/test-utils/testlib/src/standard_test_cases.rs +++ b/test-utils/testlib/src/standard_test_cases.rs @@ -2,7 +2,7 @@ use near::config::{TESTING_INIT_BALANCE, TESTING_INIT_STAKE}; use near_primitives::account::{AccessKey, AccessKeyPermission, FunctionCallPermission}; use near_primitives::crypto::signer::InMemorySigner; use near_primitives::hash::hash; -use near_primitives::transaction::FinalTransactionStatus; +use near_primitives::rpc::FinalTransactionStatus; use near_primitives::types::Balance; use crate::node::Node; diff --git a/test-utils/testlib/src/user/mod.rs b/test-utils/testlib/src/user/mod.rs index 93bdffe38ee..735d9a05e53 100644 --- a/test-utils/testlib/src/user/mod.rs +++ b/test-utils/testlib/src/user/mod.rs @@ -7,13 +7,14 @@ use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; +use near_primitives::rpc::FinalTransactionResult; use near_primitives::rpc::{ AccessKeyView, AccountView, BlockView, CryptoHashView, ViewStateResult, }; use near_primitives::transaction::{ Action, AddKeyAction, CreateAccountAction, DeleteAccountAction, DeleteKeyAction, - DeployContractAction, FinalTransactionResult, FunctionCallAction, SignedTransaction, - StakeAction, TransactionResult, TransferAction, + DeployContractAction, FunctionCallAction, SignedTransaction, StakeAction, TransactionResult, + TransferAction, }; use near_primitives::types::{AccountId, Balance, Gas, MerkleHash}; diff --git a/test-utils/testlib/src/user/rpc_user.rs b/test-utils/testlib/src/user/rpc_user.rs index 3d0043977cf..655b49d1b58 100644 --- a/test-utils/testlib/src/user/rpc_user.rs +++ b/test-utils/testlib/src/user/rpc_user.rs @@ -11,10 +11,11 @@ use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; use near_primitives::rpc::{ - AccessKeyView, AccountView, BlockView, CryptoHashView, QueryResponse, ViewStateResult, + AccessKeyView, AccountView, BlockView, CryptoHashView, FinalTransactionResult, QueryResponse, + ViewStateResult, }; use near_primitives::serialize::{to_base, to_base64, BaseEncode}; -use near_primitives::transaction::{FinalTransactionResult, SignedTransaction, TransactionResult}; +use near_primitives::transaction::{SignedTransaction, TransactionResult}; use near_primitives::types::AccountId; use crate::user::User; diff --git a/test-utils/testlib/src/user/runtime_user.rs b/test-utils/testlib/src/user/runtime_user.rs index b07b6392f1f..1a5cc2b4591 100644 --- a/test-utils/testlib/src/user/runtime_user.rs +++ b/test-utils/testlib/src/user/runtime_user.rs @@ -12,9 +12,9 @@ use near_primitives::receipt::{Receipt, ReceiptInfo}; use near_primitives::rpc::{ AccessKeyView, AccountView, BlockView, CryptoHashView, ViewStateResult, }; +use near_primitives::rpc::{FinalTransactionResult, FinalTransactionStatus}; use near_primitives::transaction::{ - FinalTransactionResult, FinalTransactionStatus, SignedTransaction, TransactionLog, - TransactionResult, TransactionStatus, + SignedTransaction, TransactionLog, TransactionResult, TransactionStatus, }; use near_primitives::types::{AccountId, BlockIndex, MerkleHash}; use near_store::{Trie, TrieUpdate}; @@ -137,7 +137,7 @@ impl RuntimeUser { } fn get_final_transaction_result(&self, hash: &CryptoHash) -> FinalTransactionResult { - let transactions = self.get_recursive_transaction_results(hash); + let mut transactions = self.get_recursive_transaction_results(hash); let status = if transactions .iter() .find(|t| &t.result.status == &TransactionStatus::Failed) @@ -153,7 +153,10 @@ impl RuntimeUser { } else { FinalTransactionStatus::Completed }; - FinalTransactionResult { status, transactions } + FinalTransactionResult { + status, + transactions: transactions.drain(..).map(|t| t.into()).collect(), + } } } diff --git a/tests/test_errors.rs b/tests/test_errors.rs index 24b7085c719..827036bc188 100644 --- a/tests/test_errors.rs +++ b/tests/test_errors.rs @@ -40,10 +40,10 @@ fn test_check_tx_error_log() { ], ); - let tx_result = node.user().commit_transaction(tx); + let tx_result = node.user().commit_transaction(tx).unwrap_err(); assert_eq!( tx_result, - Err("RpcError { code: -32000, message: \"Server error\", data: Some(String(\"Signer \\\"bob.near\\\" doesn\\'t have access key with the given public_key `22skMptHjFWNyuEWY22ftn2AbLPSYpmYwGJRGwpNHbTV`\")) }".to_string()) + "RpcError { code: -32000, message: \"Server error\", data: Some(String(\"Signer \\\"bob.near\\\" doesn\\'t have access key with the given public_key `22skMptHjFWNyuEWY22ftn2AbLPSYpmYwGJRGwpNHbTV`\")) }".to_string() ); } From 9a5ca9cd99310793d9a7f60598448ec9d3e0efac Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Fri, 16 Aug 2019 10:32:36 -0700 Subject: [PATCH 19/25] Replace drain with into_iter --- chain/client/src/sync.rs | 4 ++-- chain/client/src/view_client.rs | 4 ++-- chain/network/src/peer_manager.rs | 4 ++-- chain/network/src/peer_store.rs | 4 ++-- core/nbor/src/de/mod.rs | 4 ++-- core/primitives/src/rpc.rs | 21 +++++++++++++-------- near/src/test_utils.rs | 4 ++-- near/tests/run_nodes.rs | 2 +- runtime/runtime/src/adapter.rs | 4 ++-- test-utils/testlib/src/run_nodes.rs | 15 +++++++++++---- test-utils/testlib/src/user/runtime_user.rs | 4 ++-- tests/test_cases_testnet_rpc.rs | 2 +- tests/test_catchup.rs | 2 +- tests/test_simple.rs | 2 +- tests/test_tps_regression.rs | 2 +- 15 files changed, 45 insertions(+), 33 deletions(-) diff --git a/chain/client/src/sync.rs b/chain/client/src/sync.rs index 9f79c325737..bc8b8343982 100644 --- a/chain/client/src/sync.rs +++ b/chain/client/src/sync.rs @@ -320,7 +320,7 @@ impl BlockSync { near_chain::MAX_ORPHAN_SIZE.saturating_sub(chain.orphans_len()) + 1, ); - let mut hashes_to_request = hashes + let hashes_to_request = hashes .iter() .filter(|x| !chain.get_block(x).is_ok() && !chain.is_orphan(x)) .take(block_count) @@ -335,7 +335,7 @@ impl BlockSync { self.receive_timeout = Utc::now() + Duration::seconds(BLOCK_REQUEST_TIMEOUT); let mut peers_iter = most_weight_peers.iter().cycle(); - for hash in hashes_to_request.drain(..) { + for hash in hashes_to_request.into_iter() { if let Some(peer) = peers_iter.next() { if self .network_recipient diff --git a/chain/client/src/view_client.rs b/chain/client/src/view_client.rs index aa005b9fdec..25be95f7c84 100644 --- a/chain/client/src/view_client.rs +++ b/chain/client/src/view_client.rs @@ -67,7 +67,7 @@ impl ViewClientActor { &mut self, hash: &CryptoHash, ) -> Result { - let mut transactions = self.get_recursive_transaction_results(hash)?; + let transactions = self.get_recursive_transaction_results(hash)?; let status = if transactions .iter() .find(|t| &t.result.status == &TransactionStatus::Failed) @@ -85,7 +85,7 @@ impl ViewClientActor { }; Ok(FinalTransactionResult { status, - transactions: transactions.drain(..).map(|t| t.into()).collect(), + transactions: transactions.into_iter().map(|t| t.into()).collect(), }) } } diff --git a/chain/network/src/peer_manager.rs b/chain/network/src/peer_manager.rs index ae6972b202e..74d6be50518 100644 --- a/chain/network/src/peer_manager.rs +++ b/chain/network/src/peer_manager.rs @@ -624,9 +624,9 @@ impl Handler for PeerManagerActor { impl Handler for PeerManagerActor { type Result = (); - fn handle(&mut self, mut msg: PeersResponse, _ctx: &mut Self::Context) { + fn handle(&mut self, msg: PeersResponse, _ctx: &mut Self::Context) { self.peer_store.add_peers( - msg.peers.drain(..).filter(|peer_info| peer_info.id != self.peer_id).collect(), + msg.peers.into_iter().filter(|peer_info| peer_info.id != self.peer_id).collect(), ); } } diff --git a/chain/network/src/peer_store.rs b/chain/network/src/peer_store.rs index 4a0f6ba2b37..6dd7e826bf3 100644 --- a/chain/network/src/peer_store.rs +++ b/chain/network/src/peer_store.rs @@ -174,8 +174,8 @@ impl PeerStore { store_update.commit().map_err(|err| err.into()) } - pub fn add_peers(&mut self, mut peers: Vec) { - for peer_info in peers.drain(..) { + pub fn add_peers(&mut self, peers: Vec) { + for peer_info in peers.into_iter() { if !self.peer_states.contains_key(&peer_info.id) { self.peer_states.insert(peer_info.id, KnownPeerState::new(peer_info)); } diff --git a/core/nbor/src/de/mod.rs b/core/nbor/src/de/mod.rs index 975c3406ce3..ef7be6d73b9 100644 --- a/core/nbor/src/de/mod.rs +++ b/core/nbor/src/de/mod.rs @@ -114,8 +114,8 @@ where T: Deserializable + Eq + std::hash::Hash, { fn read(reader: &mut R) -> Result { - let mut vec = >::read(reader)?; - Ok(vec.drain(..).collect::>()) + let vec = >::read(reader)?; + Ok(vec.into_iter().collect::>()) } } diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index 07182b82442..5aa2e956e0c 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -374,7 +374,7 @@ pub struct BlockHeaderView { } impl From for BlockHeaderView { - fn from(mut header: BlockHeader) -> Self { + fn from(header: BlockHeader) -> Self { Self { height: header.inner.height, epoch_hash: header.inner.epoch_hash, @@ -386,7 +386,7 @@ impl From for BlockHeaderView { approval_sigs: header .inner .approval_sigs - .drain(..) + .into_iter() .map(|signature| signature.into()) .collect(), total_weight: header.inner.total_weight.to_num(), @@ -397,7 +397,7 @@ impl From for BlockHeaderView { } impl From for BlockHeader { - fn from(mut view: BlockHeaderView) -> Self { + fn from(view: BlockHeaderView) -> Self { let mut header = Self { inner: BlockHeaderInner { height: view.height, @@ -409,7 +409,7 @@ impl From for BlockHeader { approval_mask: view.approval_mask, approval_sigs: view .approval_sigs - .drain(..) + .into_iter() .map(|signature| signature.into()) .collect(), total_weight: view.total_weight.into(), @@ -430,10 +430,10 @@ pub struct BlockView { } impl From for BlockView { - fn from(mut block: Block) -> Self { + fn from(block: Block) -> Self { BlockView { header: block.header.into(), - transactions: block.transactions.drain(..).map(|tx| tx.into()).collect(), + transactions: block.transactions.into_iter().map(|tx| tx.into()).collect(), } } } @@ -515,14 +515,19 @@ pub struct SignedTransactionView { } impl From for SignedTransactionView { - fn from(mut signed_tx: SignedTransaction) -> Self { + fn from(signed_tx: SignedTransaction) -> Self { let hash = signed_tx.get_hash().into(); SignedTransactionView { signer_id: signed_tx.transaction.signer_id, public_key: signed_tx.transaction.public_key.into(), nonce: signed_tx.transaction.nonce, receiver_id: signed_tx.transaction.receiver_id, - actions: signed_tx.transaction.actions.drain(..).map(|action| action.into()).collect(), + actions: signed_tx + .transaction + .actions + .into_iter() + .map(|action| action.into()) + .collect(), signature: signed_tx.signature.into(), hash, } diff --git a/near/src/test_utils.rs b/near/src/test_utils.rs index 95b542ca10c..d28a61de35a 100644 --- a/near/src/test_utils.rs +++ b/near/src/test_utils.rs @@ -11,7 +11,7 @@ pub fn assignment( mut accounts: Vec<(&str, Balance)>, block_producers: Vec, chunk_producers: Vec>, - mut fishermen: Vec<(usize, u64)>, + fishermen: Vec<(usize, u64)>, expected_epoch_start: BlockIndex, stake_change: BTreeMap, ) -> ValidatorAssignment { @@ -32,7 +32,7 @@ pub fn assignment( validator_to_index, block_producers, chunk_producers, - fishermen: fishermen.drain(..).collect(), + fishermen: fishermen.into_iter().collect(), expected_epoch_start, stake_change, } diff --git a/near/tests/run_nodes.rs b/near/tests/run_nodes.rs index 15d17359e79..7bc98064cb8 100644 --- a/near/tests/run_nodes.rs +++ b/near/tests/run_nodes.rs @@ -32,7 +32,7 @@ fn run_nodes(num_nodes: usize) { let system = System::new("NEAR"); let mut view_clients = vec![]; - for (i, near_config) in near_configs.drain(..).enumerate() { + for (i, near_config) in near_configs.into_iter().enumerate() { let dir = TempDir::new(&format!("two_nodes_{}", i)).unwrap(); let (_client, view_client) = start_with_config(dir.path(), near_config); view_clients.push(view_client) diff --git a/runtime/runtime/src/adapter.rs b/runtime/runtime/src/adapter.rs index 315d57303d3..638222cf0b9 100644 --- a/runtime/runtime/src/adapter.rs +++ b/runtime/runtime/src/adapter.rs @@ -83,9 +83,9 @@ pub fn query_client( }, "access_key" => { let result = if path_parts.len() == 2 { - adapter.view_access_keys(state_root, &AccountId::from(path_parts[1])).map(|mut r| { + adapter.view_access_keys(state_root, &AccountId::from(path_parts[1])).map(|r| { QueryResponse::AccessKeyList( - r.drain(..) + r.into_iter() .map(|(public_key, access_key)| AccessKeyInfo { public_key: public_key.into(), access_key: access_key.into(), diff --git a/test-utils/testlib/src/run_nodes.rs b/test-utils/testlib/src/run_nodes.rs index e149e1043b9..cd8ca173bdf 100644 --- a/test-utils/testlib/src/run_nodes.rs +++ b/test-utils/testlib/src/run_nodes.rs @@ -1,5 +1,4 @@ ///! Runs given number of nodes from scratch for testing / integration / load testing purposes. - use std::thread; use std::time::Duration; @@ -12,12 +11,20 @@ fn main() { init_integration_logger(); let matches = App::new("run-nodes") - .arg(Arg::with_name("num_nodes").short("n").long("num-nodes").value_name("NUM_NODES").required(true).default_value("7").takes_value(true)) + .arg( + Arg::with_name("num_nodes") + .short("n") + .long("num-nodes") + .value_name("NUM_NODES") + .required(true) + .default_value("7") + .takes_value(true), + ) .get_matches(); let num_nodes = matches.value_of("num_nodes").map(|x| x.parse::().unwrap()).unwrap(); - let mut nodes = create_nodes(num_nodes, "test"); + let nodes = create_nodes(num_nodes, "test"); print!("Connect via RPC to: "); for i in 0..num_nodes { @@ -28,7 +35,7 @@ fn main() { } println!(); - let nodes: Vec<_> = nodes.drain(..).map(|cfg| Node::new_sharable(cfg)).collect(); + let nodes: Vec<_> = nodes.into_iter().map(|cfg| Node::new_sharable(cfg)).collect(); // Start nodes. for i in 0..num_nodes { diff --git a/test-utils/testlib/src/user/runtime_user.rs b/test-utils/testlib/src/user/runtime_user.rs index 1a5cc2b4591..8670a927cdf 100644 --- a/test-utils/testlib/src/user/runtime_user.rs +++ b/test-utils/testlib/src/user/runtime_user.rs @@ -137,7 +137,7 @@ impl RuntimeUser { } fn get_final_transaction_result(&self, hash: &CryptoHash) -> FinalTransactionResult { - let mut transactions = self.get_recursive_transaction_results(hash); + let transactions = self.get_recursive_transaction_results(hash); let status = if transactions .iter() .find(|t| &t.result.status == &TransactionStatus::Failed) @@ -155,7 +155,7 @@ impl RuntimeUser { }; FinalTransactionResult { status, - transactions: transactions.drain(..).map(|t| t.into()).collect(), + transactions: transactions.into_iter().map(|t| t.into()).collect(), } } } diff --git a/tests/test_cases_testnet_rpc.rs b/tests/test_cases_testnet_rpc.rs index 9308f79f99e..6ec9649f770 100644 --- a/tests/test_cases_testnet_rpc.rs +++ b/tests/test_cases_testnet_rpc.rs @@ -21,7 +21,7 @@ mod test { "dan.near".to_string(), ]); let mut nodes: Vec<_> = nodes - .drain(..) + .into_iter() .map(|cfg| match cfg { NodeConfig::Thread(config) => ThreadNode::new(config), _ => unreachable!(), diff --git a/tests/test_catchup.rs b/tests/test_catchup.rs index e1b30632ee7..8d75783e2bc 100644 --- a/tests/test_catchup.rs +++ b/tests/test_catchup.rs @@ -21,7 +21,7 @@ fn test_catchup() { let mut nodes = create_nodes(num_nodes, test_prefix); let mut nodes: Vec>> = - nodes.drain(..).map(|cfg| Node::new_sharable(cfg)).collect(); + nodes.into_iter().map(|cfg| Node::new_sharable(cfg)).collect(); let late_node = nodes.pop().unwrap(); // Start all but one. diff --git a/tests/test_simple.rs b/tests/test_simple.rs index d23dd2860cf..7ff9aa1b438 100644 --- a/tests/test_simple.rs +++ b/tests/test_simple.rs @@ -11,7 +11,7 @@ mod test { init_integration_logger(); let mut nodes = create_nodes(num_nodes, test_prefix); - let nodes: Vec<_> = nodes.drain(..).map(|cfg| Node::new_sharable(cfg)).collect(); + let nodes: Vec<_> = nodes.into_iter().map(|cfg| Node::new_sharable(cfg)).collect(); let account_names: Vec<_> = nodes.iter().map(|node| node.read().unwrap().account_id().unwrap()).collect(); diff --git a/tests/test_tps_regression.rs b/tests/test_tps_regression.rs index 36103cfb433..f5868fbc75f 100644 --- a/tests/test_tps_regression.rs +++ b/tests/test_tps_regression.rs @@ -85,7 +85,7 @@ mod test { let mut nodes = create_nodes(num_nodes, test_prefix); let nodes: Vec>> = - nodes.drain(..).map(|cfg| Node::new_sharable(cfg)).collect(); + nodes.into_iter().map(|cfg| Node::new_sharable(cfg)).collect(); for i in 0..num_nodes { nodes[i].write().unwrap().start(); } From 554d4bb43e305893961348ca0ccb8b52b3d1d9ba Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Fri, 16 Aug 2019 10:45:14 -0700 Subject: [PATCH 20/25] Rename to_vec/from_slice -> try_to_vec/try_from_slice --- chain/jsonrpc/src/lib.rs | 2 +- chain/jsonrpc/tests/rpc_transactions.rs | 4 ++-- chain/network/src/codec.rs | 4 ++-- chain/network/src/types.rs | 2 +- core/nbor/src/de/mod.rs | 2 +- core/nbor/src/ser/mod.rs | 6 ++++-- core/nbor/tests/test_simple_structs.rs | 4 ++-- core/primitives/benches/serialization.rs | 18 +++++++++--------- core/primitives/src/block.rs | 6 +++--- core/primitives/src/transaction.rs | 5 +++-- core/store/src/lib.rs | 14 +++++++++----- test-utils/loadtester/src/remote_node.rs | 4 ++-- test-utils/state-viewer/src/main.rs | 4 ++-- test-utils/testlib/src/user/rpc_user.rs | 4 ++-- 14 files changed, 43 insertions(+), 36 deletions(-) diff --git a/chain/jsonrpc/src/lib.rs b/chain/jsonrpc/src/lib.rs index 79bfe681bd5..6e4e007d684 100644 --- a/chain/jsonrpc/src/lib.rs +++ b/chain/jsonrpc/src/lib.rs @@ -100,7 +100,7 @@ fn jsonify( fn parse_tx(params: Option) -> Result { let (encoded,) = parse_params::<(String,)>(params)?; let bytes = from_base64_or_parse_err(encoded)?; - SignedTransaction::from_slice(&bytes).map_err(|e| RpcError::invalid_params(Some(format!("Failed to decode transaction: {}", e)))) + SignedTransaction::try_from_slice(&bytes).map_err(|e| RpcError::invalid_params(Some(format!("Failed to decode transaction: {}", e)))) } fn parse_hash(params: Option) -> Result { diff --git a/chain/jsonrpc/tests/rpc_transactions.rs b/chain/jsonrpc/tests/rpc_transactions.rs index 465713c6799..6219a9ce753 100644 --- a/chain/jsonrpc/tests/rpc_transactions.rs +++ b/chain/jsonrpc/tests/rpc_transactions.rs @@ -32,7 +32,7 @@ fn test_send_tx_async() { ); let tx_hash: String = (&tx.get_hash()).into(); let tx_hash2 = tx_hash.clone(); - let bytes = tx.to_vec().unwrap(); + let bytes = tx.try_to_vec().unwrap(); actix::spawn( client .broadcast_tx_async(to_base64(&bytes)) @@ -76,7 +76,7 @@ fn test_send_tx_commit() { Arc::new(signer), 100, ); - let bytes = tx.to_vec().unwrap(); + let bytes = tx.try_to_vec().unwrap(); actix::spawn( client .broadcast_tx_commit(to_base64(&bytes)) diff --git a/chain/network/src/codec.rs b/chain/network/src/codec.rs index ecf273362b6..46256825f12 100644 --- a/chain/network/src/codec.rs +++ b/chain/network/src/codec.rs @@ -58,11 +58,11 @@ impl Decoder for Codec { } pub fn peer_message_to_bytes(peer_message: PeerMessage) -> Result, std::io::Error> { - peer_message.to_vec() + peer_message.try_to_vec() } pub fn bytes_to_peer_message(bytes: &[u8]) -> Result { - PeerMessage::from_slice(bytes) + PeerMessage::try_from_slice(bytes) } #[cfg(test)] diff --git a/chain/network/src/types.rs b/chain/network/src/types.rs index e47cb59d063..bd24bbfbf86 100644 --- a/chain/network/src/types.rs +++ b/chain/network/src/types.rs @@ -361,7 +361,7 @@ impl TryFrom> for KnownPeerState { type Error = Box; fn try_from(bytes: Vec) -> Result { - KnownPeerState::from_slice(&bytes).map_err(|err| err.into()) + KnownPeerState::try_from_slice(&bytes).map_err(|err| err.into()) } } diff --git a/core/nbor/src/de/mod.rs b/core/nbor/src/de/mod.rs index ef7be6d73b9..01cf3a6e83e 100644 --- a/core/nbor/src/de/mod.rs +++ b/core/nbor/src/de/mod.rs @@ -7,7 +7,7 @@ pub trait Deserializable: Sized { fn read(reader: &mut R) -> Result; /// Deserialize this instance from a slice of bytes. - fn from_slice(v: &[u8]) -> Result { + fn try_from_slice(v: &[u8]) -> Result { let mut c = Cursor::new(v); Self::read(&mut c) } diff --git a/core/nbor/src/ser/mod.rs b/core/nbor/src/ser/mod.rs index 234e89cfce7..932d627bc42 100644 --- a/core/nbor/src/ser/mod.rs +++ b/core/nbor/src/ser/mod.rs @@ -1,13 +1,15 @@ use std::collections::{BTreeMap, HashMap, HashSet}; use std::io::{Error, Write}; +const DEFAULT_SERIALIZER_CAPACITY: usize = 1024; + /// A data-structure that can be serialized into binary format by NBOR. pub trait Serializable { fn write(&self, writer: &mut W) -> Result<(), Error>; /// Serialize this instance into a vector of bytes. - fn to_vec(&self) -> Result, Error> { - let mut result = vec![]; + fn try_to_vec(&self) -> Result, Error> { + let mut result = Vec::with_capacity(DEFAULT_SERIALIZER_CAPACITY); self.write(&mut result)?; Ok(result) } diff --git a/core/nbor/tests/test_simple_structs.rs b/core/nbor/tests/test_simple_structs.rs index 997f7c8b5bd..c5c8530ae81 100644 --- a/core/nbor/tests/test_simple_structs.rs +++ b/core/nbor/tests/test_simple_structs.rs @@ -53,8 +53,8 @@ fn test_simple_struct() { lazy: Some(5), skipped: Some(6), }; - let encoded_a = a.to_vec().unwrap(); - let decoded_a = A::from_slice(&encoded_a).unwrap(); + let encoded_a = a.try_to_vec().unwrap(); + let decoded_a = A::try_from_slice(&encoded_a).unwrap(); let expected_a = A { x: 1, b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, diff --git a/core/primitives/benches/serialization.rs b/core/primitives/benches/serialization.rs index fb3d18f4a37..5b9a90210b7 100644 --- a/core/primitives/benches/serialization.rs +++ b/core/primitives/benches/serialization.rs @@ -56,16 +56,16 @@ fn create_account() -> Account { fn serialize_tx(bench: &mut Bencher) { let t = create_transaction(); bench.iter(|| { - let bytes = t.to_vec().unwrap(); + let bytes = t.try_to_vec().unwrap(); assert!(bytes.len() > 0); }); } fn deserialize_tx(bench: &mut Bencher) { let t = create_transaction(); - let bytes = t.to_vec().unwrap(); + let bytes = t.try_to_vec().unwrap(); bench.iter(|| { - let nt = SignedTransaction::from_slice(&bytes).unwrap(); + let nt = SignedTransaction::try_from_slice(&bytes).unwrap(); assert_eq!(nt, t); }); } @@ -73,16 +73,16 @@ fn deserialize_tx(bench: &mut Bencher) { fn serialize_block(bench: &mut Bencher) { let b = create_block(); bench.iter(|| { - let bytes = b.to_vec().unwrap(); + let bytes = b.try_to_vec().unwrap(); assert!(bytes.len() > 0); }); } fn deserialize_block(bench: &mut Bencher) { let b = create_block(); - let bytes = b.to_vec().unwrap(); + let bytes = b.try_to_vec().unwrap(); bench.iter(|| { - let nb = Block::from_slice(&bytes).unwrap(); + let nb = Block::try_from_slice(&bytes).unwrap(); assert_eq!(nb, b); }); } @@ -90,16 +90,16 @@ fn deserialize_block(bench: &mut Bencher) { fn serialize_account(bench: &mut Bencher) { let acc = create_account(); bench.iter(|| { - let bytes = acc.to_vec().unwrap(); + let bytes = acc.try_to_vec().unwrap(); assert!(bytes.len() > 0); }); } fn deserialize_account(bench: &mut Bencher) { let acc = create_account(); - let bytes = acc.to_vec().unwrap(); + let bytes = acc.try_to_vec().unwrap(); bench.iter(|| { - let nacc = Account::from_slice(&bytes).unwrap(); + let nacc = Account::try_from_slice(&bytes).unwrap(); assert_eq!(nacc, acc); }); } diff --git a/core/primitives/src/block.rs b/core/primitives/src/block.rs index af7b22a9712..5e22df3bc58 100644 --- a/core/primitives/src/block.rs +++ b/core/primitives/src/block.rs @@ -81,7 +81,7 @@ pub struct BlockHeader { impl BlockHeader { pub fn init(&mut self) { - self.hash = hash(&self.inner.to_vec().expect("Failed to serialize")); + self.hash = hash(&self.inner.try_to_vec().expect("Failed to serialize")); } pub fn new( @@ -109,7 +109,7 @@ impl BlockHeader { total_weight, validator_proposal, ); - let hash = hash(&inner.to_vec().expect("Failed to serialize")); + let hash = hash(&inner.try_to_vec().expect("Failed to serialize")); Self { inner, signature: signer.sign(hash.as_ref()), hash } } @@ -126,7 +126,7 @@ impl BlockHeader { 0.into(), vec![], ); - let hash = hash(&inner.to_vec().expect("Failed to serialize")); + let hash = hash(&inner.try_to_vec().expect("Failed to serialize")); Self { inner, signature: DEFAULT_SIGNATURE, hash } } diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index 8af8609071d..7b1e6637fb9 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -25,7 +25,7 @@ pub struct Transaction { impl Transaction { pub fn get_hash(&self) -> CryptoHash { - let bytes = self.to_vec().expect("Failed to deserialize"); + let bytes = self.try_to_vec().expect("Failed to deserialize"); hash(&bytes) } } @@ -312,7 +312,8 @@ mod tests { ], }; let signed_tx = SignedTransaction::new(DEFAULT_SIGNATURE, transaction); - let new_signed_tx = SignedTransaction::from_slice(&signed_tx.to_vec().unwrap()).unwrap(); + let new_signed_tx = + SignedTransaction::try_from_slice(&signed_tx.try_to_vec().unwrap()).unwrap(); assert_eq!( to_base(&new_signed_tx.get_hash()), diff --git a/core/store/src/lib.rs b/core/store/src/lib.rs index 2cc8f1b56df..26bf0a928cd 100644 --- a/core/store/src/lib.rs +++ b/core/store/src/lib.rs @@ -58,7 +58,7 @@ impl Store { key: &[u8], ) -> Result, io::Error> { match self.storage.get(column, key) { - Ok(Some(bytes)) => match T::from_slice(bytes.as_ref()) { + Ok(Some(bytes)) => match T::try_from_slice(bytes.as_ref()) { Ok(result) => Ok(Some(result)), Err(e) => Err(e), }, @@ -112,7 +112,7 @@ impl StoreUpdate { key: &[u8], value: &T, ) -> Result<(), io::Error> { - let data = value.to_vec()?; + let data = value.try_to_vec()?; self.set(column, key, &data); Ok(()) } @@ -189,17 +189,21 @@ pub fn create_store(path: &str) -> Arc { /// Reads an object from Trie. pub fn get(state_update: &TrieUpdate, key: &[u8]) -> Option { - state_update.get(key).and_then(|data| T::from_slice(&data).ok()) + state_update.get(key).and_then(|data| T::try_from_slice(&data).ok()) } /// Writes an object into Trie. pub fn set(state_update: &mut TrieUpdate, key: Vec, value: &T) { - value.to_vec().ok().map(|data| state_update.set(key, DBValue::from_vec(data))).or_else(|| None); + value + .try_to_vec() + .ok() + .map(|data| state_update.set(key, DBValue::from_vec(data))) + .or_else(|| None); } /// Number of bytes the account data structure occupies in the storage. pub fn account_storage_size(account: &Account) -> StorageUsage { - account.to_vec().map(|bytes| bytes.len() as StorageUsage).unwrap_or(0) + account.try_to_vec().map(|bytes| bytes.len() as StorageUsage).unwrap_or(0) } /// Number of bytes account and all of it's other data occupies in the storage. diff --git a/test-utils/loadtester/src/remote_node.rs b/test-utils/loadtester/src/remote_node.rs index df7a1030483..ae460cd36cd 100644 --- a/test-utils/loadtester/src/remote_node.rs +++ b/test-utils/loadtester/src/remote_node.rs @@ -166,7 +166,7 @@ impl RemoteNode { &self, transaction: SignedTransaction, ) -> Box + Send> { - let bytes = transaction.to_vec().unwrap(); + let bytes = transaction.try_to_vec().unwrap(); let url = format!("{}{}", self.url, "/broadcast_tx_sync"); let response = self .async_client @@ -184,7 +184,7 @@ impl RemoteNode { &self, transaction: SignedTransaction, ) -> Result> { - let bytes = transaction.to_vec().unwrap(); + let bytes = transaction.try_to_vec().unwrap(); let url = format!("{}{}", self.url, "/broadcast_tx_sync"); let result: serde_json::Value = self .sync_client diff --git a/test-utils/state-viewer/src/main.rs b/test-utils/state-viewer/src/main.rs index 076b678c421..53195e6cd07 100644 --- a/test-utils/state-viewer/src/main.rs +++ b/test-utils/state-viewer/src/main.rs @@ -42,7 +42,7 @@ fn kv_to_state_record(key: Vec, value: DBValue) -> StateRecord { if separator.is_some() { StateRecord::Data { key: to_base64(&key), value: to_base64(&value) } } else { - let account = Account::from_slice(&value).unwrap(); + let account = Account::try_from_slice(&value).unwrap(); StateRecord::Account { account_id: to_printable(&key[1..]), account: account.into(), @@ -60,7 +60,7 @@ fn kv_to_state_record(key: Vec, value: DBValue) -> StateRecord { } col::ACCESS_KEY => { let separator = (1..key.len()).find(|&x| key[x] == col::ACCESS_KEY[0]).unwrap(); - let access_key = AccessKey::from_slice(&value).unwrap(); + let access_key = AccessKey::try_from_slice(&value).unwrap(); let account_id = to_printable(&key[1..separator]); let public_key = PublicKey::try_from(&key[(separator + 1)..]).unwrap(); StateRecord::AccessKey { diff --git a/test-utils/testlib/src/user/rpc_user.rs b/test-utils/testlib/src/user/rpc_user.rs index 655b49d1b58..f127d9d2c30 100644 --- a/test-utils/testlib/src/user/rpc_user.rs +++ b/test-utils/testlib/src/user/rpc_user.rs @@ -49,7 +49,7 @@ impl User for RpcUser { } fn add_transaction(&self, transaction: SignedTransaction) -> Result<(), String> { - let bytes = transaction.to_vec().unwrap(); + let bytes = transaction.try_to_vec().unwrap(); let _ = System::new("actix") .block_on(self.client.write().unwrap().broadcast_tx_async(to_base64(&bytes)))?; Ok(()) @@ -59,7 +59,7 @@ impl User for RpcUser { &self, transaction: SignedTransaction, ) -> Result { - let bytes = transaction.to_vec().unwrap(); + let bytes = transaction.try_to_vec().unwrap(); System::new("actix") .block_on(self.client.write().unwrap().broadcast_tx_commit(to_base64(&bytes))) } From 9b0224cbf186cc361d74e8bed023c8afb213ff38 Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Fri, 16 Aug 2019 13:32:29 -0700 Subject: [PATCH 21/25] Fix comments --- chain/client/src/types.rs | 7 +- chain/client/src/view_client.rs | 21 +-- chain/jsonrpc/client/src/lib.rs | 7 +- core/primitives/src/contract.rs | 3 +- core/primitives/src/crypto/signature.rs | 9 +- core/primitives/src/crypto/signer.rs | 87 +--------- core/primitives/src/hash.rs | 7 +- core/primitives/src/rpc.rs | 167 ++++++++++++-------- core/primitives/src/sharding.rs | 9 +- core/primitives/src/transaction.rs | 2 +- core/primitives/src/types.rs | 9 +- runtime/runtime/src/adapter.rs | 6 +- runtime/runtime/src/state_viewer.rs | 6 +- test-utils/keystore/src/main.rs | 91 ++--------- test-utils/state-viewer/src/main.rs | 4 +- test-utils/testlib/src/user/mod.rs | 4 +- test-utils/testlib/src/user/rpc_user.rs | 6 +- test-utils/testlib/src/user/runtime_user.rs | 26 +-- 18 files changed, 187 insertions(+), 284 deletions(-) diff --git a/chain/client/src/types.rs b/chain/client/src/types.rs index eeec3d6b551..0635795f473 100644 --- a/chain/client/src/types.rs +++ b/chain/client/src/types.rs @@ -7,9 +7,10 @@ use chrono::{DateTime, Utc}; use near_primitives::crypto::signer::{AccountSigner, EDSigner, InMemorySigner}; use near_primitives::hash::CryptoHash; -use near_primitives::rpc::{BlockView, FinalTransactionResult, QueryResponse}; +use near_primitives::rpc::{ + BlockView, FinalTransactionResult, QueryResponse, TransactionResultView, +}; pub use near_primitives::rpc::{StatusResponse, StatusSyncInfo}; -use near_primitives::transaction::TransactionResult; use near_primitives::types::{AccountId, BlockIndex, ShardId, Version}; /// Combines errors coming from chain, tx pool and block producer. @@ -261,5 +262,5 @@ pub struct TxDetails { } impl Message for TxDetails { - type Result = Result; + type Result = Result; } diff --git a/chain/client/src/view_client.rs b/chain/client/src/view_client.rs index 25be95f7c84..7097084f45b 100644 --- a/chain/client/src/view_client.rs +++ b/chain/client/src/view_client.rs @@ -9,9 +9,10 @@ use chrono::{DateTime, Utc}; use near_chain::{Chain, ErrorKind, RuntimeAdapter}; use near_primitives::hash::CryptoHash; use near_primitives::rpc::{ - BlockView, FinalTransactionResult, FinalTransactionStatus, QueryResponse, + BlockView, FinalTransactionResult, FinalTransactionStatus, QueryResponse, TransactionLogView, + TransactionResultView, }; -use near_primitives::transaction::{TransactionLog, TransactionResult, TransactionStatus}; +use near_primitives::transaction::{TransactionResult, TransactionStatus}; use near_store::Store; use crate::types::{Error, GetBlock, Query, TxStatus}; @@ -37,14 +38,15 @@ impl ViewClientActor { pub fn get_transaction_result( &mut self, hash: &CryptoHash, - ) -> Result { + ) -> Result { match self.chain.get_transaction_result(hash) { - Ok(result) => Ok(result.clone()), + Ok(result) => Ok(result.clone().into()), Err(err) => match err.kind() { ErrorKind::DBNotFoundErr(_) => Ok(TransactionResult { status: TransactionStatus::Unknown, ..Default::default() - }), + } + .into()), _ => Err(err.to_string()), }, } @@ -53,12 +55,13 @@ impl ViewClientActor { fn get_recursive_transaction_results( &mut self, hash: &CryptoHash, - ) -> Result, String> { + ) -> Result, String> { let result = self.get_transaction_result(hash)?; let receipt_ids = result.receipts.clone(); - let mut transactions = vec![TransactionLog { hash: *hash, result }]; + let mut transactions = vec![TransactionLogView { hash: hash.clone().into(), result }]; for hash in &receipt_ids { - transactions.extend(self.get_recursive_transaction_results(hash)?.into_iter()); + transactions + .extend(self.get_recursive_transaction_results(&hash.clone().into())?.into_iter()); } Ok(transactions) } @@ -135,7 +138,7 @@ impl Handler for ViewClientActor { } impl Handler for ViewClientActor { - type Result = Result; + type Result = Result; fn handle(&mut self, msg: TxDetails, _: &mut Context) -> Self::Result { self.get_transaction_result(&msg.tx_hash) diff --git a/chain/jsonrpc/client/src/lib.rs b/chain/jsonrpc/client/src/lib.rs index 097067268ec..d000f654623 100644 --- a/chain/jsonrpc/client/src/lib.rs +++ b/chain/jsonrpc/client/src/lib.rs @@ -4,8 +4,9 @@ use actix_web::client::Client; use futures::Future; use serde::Serialize; -use near_primitives::rpc::{BlockView, FinalTransactionResult, QueryResponse, StatusResponse}; -use near_primitives::transaction::TransactionResult; +use near_primitives::rpc::{ + BlockView, FinalTransactionResult, QueryResponse, StatusResponse, TransactionResultView, +}; use near_primitives::types::BlockIndex; pub mod message; @@ -167,7 +168,7 @@ jsonrpc_client!(pub struct JsonRpcClient { pub fn status(&mut self) -> RpcRequest; pub fn health(&mut self) -> RpcRequest<()>; pub fn tx(&mut self, hash: String) -> RpcRequest; - pub fn tx_details(&mut self, hash: String) -> RpcRequest; + pub fn tx_details(&mut self, hash: String) -> RpcRequest; pub fn block(&mut self, height: BlockIndex) -> RpcRequest; }); diff --git a/core/primitives/src/contract.rs b/core/primitives/src/contract.rs index 8c8df394b2a..bfc20b5ff07 100644 --- a/core/primitives/src/contract.rs +++ b/core/primitives/src/contract.rs @@ -1,6 +1,5 @@ -use crate::hash::{CryptoHash, hash}; +use crate::hash::{hash, CryptoHash}; -#[derive(Serialize, Deserialize)] pub struct ContractCode { pub code: Vec, pub hash: CryptoHash, diff --git a/core/primitives/src/crypto/signature.rs b/core/primitives/src/crypto/signature.rs index ba4b70f71f0..11bc8a4ed87 100644 --- a/core/primitives/src/crypto/signature.rs +++ b/core/primitives/src/crypto/signature.rs @@ -11,10 +11,10 @@ use crate::logging::pretty_hash; use crate::serialize::{from_base, to_base, BaseDecode}; use crate::types::ReadablePublicKey; -#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialOrd, Ord, PartialEq)] +#[derive(Copy, Clone, Eq, PartialOrd, Ord, PartialEq)] pub struct PublicKey(pub sodiumoxide::crypto::sign::ed25519::PublicKey); -#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)] +#[derive(Clone, Eq, PartialEq)] pub struct SecretKey(pub sodiumoxide::crypto::sign::ed25519::SecretKey); #[derive(Clone, Eq, PartialEq, Hash)] @@ -121,6 +121,7 @@ impl TryFrom<&str> for PublicKey { impl nbor::Serializable for PublicKey { fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { + // TODO: support other curves here. writer.write(&vec![0])?; writer.write(&(self.0).0)?; Ok(()) @@ -132,7 +133,9 @@ impl nbor::Deserializable for PublicKey { let mut bytes = [0; sodiumoxide::crypto::sign::ed25519::PUBLICKEYBYTES + 1]; reader.read(&mut bytes)?; // TODO: support other curves here. - assert_eq!(bytes[0], 0); + if bytes[0] != 0 { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "Unsupported curve")); + } let mut array = [0; sodiumoxide::crypto::sign::ed25519::PUBLICKEYBYTES]; array.copy_from_slice(&bytes[1..]); let public_key = sodiumoxide::crypto::sign::ed25519::PublicKey(array); diff --git a/core/primitives/src/crypto/signer.rs b/core/primitives/src/crypto/signer.rs index a87afe6a8c9..9835736860a 100644 --- a/core/primitives/src/crypto/signer.rs +++ b/core/primitives/src/crypto/signer.rs @@ -108,86 +108,7 @@ pub fn get_key_file(key_store_path: &Path, public_key: Option) -> KeyFil serde_json::from_str(&key_file_string).unwrap() } -#[derive(Serialize, Deserialize)] -pub struct BlockProducerKeyFile { - pub public_key: PublicKey, - pub secret_key: SecretKey, -} - -pub fn write_block_producer_key_file( - key_store_path: &Path, - public_key: PublicKey, - secret_key: SecretKey, -) -> String { - if !key_store_path.exists() { - fs::create_dir_all(key_store_path).unwrap(); - } - - let key_file = BlockProducerKeyFile { public_key, secret_key }; - let key_file_path = key_store_path.join(Path::new(&key_file.public_key.to_string())); - let serialized = serde_json::to_string(&key_file).unwrap(); - fs::write(key_file_path, serialized).unwrap(); - key_file.public_key.to_string() -} - -pub fn get_block_producer_key_file( - key_store_path: &Path, - public_key: Option, -) -> BlockProducerKeyFile { - if !key_store_path.exists() { - println!("Key store path does not exist: {:?}", &key_store_path); - process::exit(3); - } - - let mut key_files = fs::read_dir(key_store_path).unwrap(); - let key_file = key_files.next(); - let key_files_count = key_files.count(); - if key_files_count == 0 && key_file.is_none() { - panic!("No key file found in {:?}. Run `cargo run --package keystore -- keygen --test-seed alice.near` to set up testing keys.", key_store_path); - } - let key_file_string = if key_files_count > 0 { - if let Some(p) = public_key { - let key_file_path = key_store_path.join(Path::new(&p)); - match fs::read_to_string(key_file_path.clone()) { - Ok(content) => content, - Err(err) => { - panic!("Failed to read key file {:?} with error: {}", key_file_path, err); - } - } - } else { - println!( - "Public key must be specified when there is more than one \ - file in the keystore" - ); - process::exit(4); - } - } else { - let path = key_file.unwrap().unwrap().path(); - match fs::read_to_string(path.clone()) { - Ok(content) => content, - Err(err) => { - panic!("Failed to read key file {:?} with error: {}", path, err); - } - } - }; - - serde_json::from_str(&key_file_string).unwrap() -} - -pub fn get_or_create_key_file( - key_store_path: &Path, - public_key: Option, -) -> BlockProducerKeyFile { - if !key_store_path.exists() { - let (public_key, secret_key) = get_key_pair(); - let new_public_key = write_block_producer_key_file(key_store_path, public_key, secret_key); - get_block_producer_key_file(key_store_path, Some(new_public_key)) - } else { - get_block_producer_key_file(key_store_path, public_key) - } -} - -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone)] pub struct InMemorySigner { pub account_id: AccountId, pub public_key: PublicKey, @@ -224,12 +145,6 @@ impl InMemorySigner { } } -impl From<&str> for InMemorySigner { - fn from(key_file: &str) -> Self { - serde_json::from_str(key_file).expect("Failed to deserialize the key file.") - } -} - impl From for InMemorySigner { fn from(key_file: KeyFile) -> Self { Self { diff --git a/core/primitives/src/hash.rs b/core/primitives/src/hash.rs index 6d79d38317a..54abfbce416 100644 --- a/core/primitives/src/hash.rs +++ b/core/primitives/src/hash.rs @@ -7,10 +7,9 @@ use exonum_sodiumoxide::crypto::hash::sha256::Digest; use crate::logging::pretty_hash; use crate::serialize::{from_base, to_base, BaseDecode, Encode}; -use serde::{Deserialize, Serialize}; use std::io::Read; -#[derive(Copy, Clone, PartialOrd, Ord, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialOrd, Ord)] pub struct CryptoHash(pub Digest); impl<'a> From<&'a CryptoHash> for String { @@ -183,7 +182,7 @@ mod tests { fn test_deserialize_not_base64() { let encoded = "\"---\""; match serde_json::from_str(&encoded) { - Ok(CryptoHash(_)) => assert!(false, "should have failed"), + Ok(CryptoHashView(_)) => assert!(false, "should have failed"), Err(_) => (), } } @@ -192,7 +191,7 @@ mod tests { fn test_deserialize_not_crypto_hash() { let encoded = "\"CjNSmWXTWhC3ELhRmWMTkRbU96wUACqxMtV1uGf\""; match serde_json::from_str(&encoded) { - Ok(CryptoHash(_)) => assert!(false, "should have failed"), + Ok(CryptoHashView(_)) => assert!(false, "should have failed"), Err(_) => (), } } diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index 5aa2e956e0c..4ead6190fc5 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -10,87 +10,63 @@ use crate::block::{Block, BlockHeader, BlockHeaderInner}; use crate::crypto::signature::{PublicKey, SecretKey, Signature}; use crate::hash::CryptoHash; use crate::logging; -use crate::serialize::{from_base, option_u128_dec_format, to_base, to_base64, u128_dec_format}; -use crate::transaction::{Action, SignedTransaction, TransactionLog, TransactionResult}; +use crate::serialize::{ + base_bytes_format, from_base, from_base64, option_u128_dec_format, to_base, to_base64, + u128_dec_format, +}; +use crate::transaction::{ + Action, LogEntry, SignedTransaction, TransactionLog, TransactionResult, TransactionStatus, +}; use crate::types::{ AccountId, Balance, BlockIndex, Gas, Nonce, StorageUsage, ValidatorStake, Version, }; -#[derive(Debug, Eq, PartialEq, Clone)] -pub struct PublicKeyView(Vec); - -impl Serialize for PublicKeyView { - fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> - where - S: Serializer, - { - serializer.serialize_str(&to_base(&self.0)) - } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +pub enum KeyType { + ED25519 = 0, } -impl<'de> Deserialize<'de> for PublicKeyView { - fn deserialize(deserializer: D) -> Result>::Error> - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - from_base(&s) - .map(|v| PublicKeyView(v)) - .map_err(|err| serde::de::Error::custom(err.to_string())) - } +#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)] +pub struct PublicKeyView { + key_type: KeyType, + #[serde(with = "base_bytes_format")] + data: Vec, } impl fmt::Display for PublicKeyView { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", to_base(&self.0)) + write!(f, "{:?}:{}", self.key_type, to_base(&self.data)) } } impl From for PublicKeyView { fn from(public_key: PublicKey) -> Self { - Self(public_key.0.as_ref().to_vec()) + Self { key_type: KeyType::ED25519, data: public_key.0.as_ref().to_vec() } } } impl From for PublicKey { fn from(view: PublicKeyView) -> Self { - Self::try_from(view.0).expect("Failed to get PublicKey from PublicKeyView") - } -} - -#[derive(Debug)] -pub struct SecretKeyView(Vec); - -impl Serialize for SecretKeyView { - fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> - where - S: Serializer, - { - serializer.serialize_str(&to_base(&self.0)) + Self::try_from(view.data).expect("Failed to get PublicKey from PublicKeyView") } } -impl<'de> Deserialize<'de> for SecretKeyView { - fn deserialize(deserializer: D) -> Result>::Error> - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - from_base(&s) - .map(|v| SecretKeyView(v)) - .map_err(|err| serde::de::Error::custom(err.to_string())) - } +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] +pub struct SecretKeyView { + key_type: KeyType, + #[serde(with = "base_bytes_format")] + data: Vec, } impl From for SecretKeyView { fn from(secret_key: SecretKey) -> Self { - Self(secret_key.0[..].to_vec()) + Self { key_type: KeyType::ED25519, data: secret_key.0[..].to_vec() } } } impl From for SecretKey { fn from(view: SecretKeyView) -> Self { - TryFrom::<&[u8]>::try_from(view.0.as_ref()) + TryFrom::<&[u8]>::try_from(view.data.as_ref()) .expect("Failed to get SecretKeyView from SecretKey") } } @@ -132,7 +108,7 @@ impl From for Signature { } #[derive(Debug, PartialEq, Eq, Clone)] -pub struct CryptoHashView(Vec); +pub struct CryptoHashView(pub Vec); impl Serialize for CryptoHashView { fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> @@ -278,7 +254,7 @@ pub struct QueryError { } #[derive(Serialize, Deserialize, Debug)] -pub struct AccessKeyInfo { +pub struct AccessKeyInfoView { pub public_key: PublicKeyView, pub access_key: AccessKeyView, } @@ -291,7 +267,7 @@ pub enum QueryResponse { CallResult(CallResult), Error(QueryError), AccessKey(Option), - AccessKeyList(Vec), + AccessKeyList(Vec), } #[derive(Serialize, Deserialize, Debug)] @@ -361,7 +337,7 @@ impl TryFrom for Option { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct BlockHeaderView { pub height: BlockIndex, - pub epoch_hash: CryptoHash, + pub epoch_hash: CryptoHashView, pub prev_hash: CryptoHashView, pub prev_state_root: CryptoHashView, pub tx_root: CryptoHashView, @@ -369,7 +345,7 @@ pub struct BlockHeaderView { pub approval_mask: Vec, pub approval_sigs: Vec, pub total_weight: u64, - pub validator_proposals: Vec, + pub validator_proposals: Vec, pub signature: SignatureView, } @@ -377,7 +353,7 @@ impl From for BlockHeaderView { fn from(header: BlockHeader) -> Self { Self { height: header.inner.height, - epoch_hash: header.inner.epoch_hash, + epoch_hash: header.inner.epoch_hash.into(), prev_hash: header.inner.prev_hash.into(), prev_state_root: header.inner.prev_state_root.into(), tx_root: header.inner.tx_root.into(), @@ -390,7 +366,12 @@ impl From for BlockHeaderView { .map(|signature| signature.into()) .collect(), total_weight: header.inner.total_weight.to_num(), - validator_proposals: header.inner.validator_proposals, + validator_proposals: header + .inner + .validator_proposals + .into_iter() + .map(|v| v.into()) + .collect(), signature: header.signature.into(), } } @@ -413,7 +394,11 @@ impl From for BlockHeader { .map(|signature| signature.into()) .collect(), total_weight: view.total_weight.into(), - validator_proposals: view.validator_proposals, + validator_proposals: view + .validator_proposals + .into_iter() + .map(|v| v.into()) + .collect(), }, signature: view.signature.into(), hash: CryptoHash::default(), @@ -465,7 +450,7 @@ pub enum ActionView { access_key: AccessKeyView, }, DeleteKey { - public_key: PublicKey, + public_key: PublicKeyView, }, DeleteAccount { beneficiary_id: AccountId, @@ -559,15 +544,45 @@ impl FinalTransactionStatus { } } +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct TransactionResultView { + pub status: TransactionStatus, + pub logs: Vec, + pub receipts: Vec, + pub result: Option, +} + +impl From for TransactionResultView { + fn from(result: TransactionResult) -> Self { + Self { + status: result.status, + logs: result.logs, + receipts: result.receipts.into_iter().map(|h| h.into()).collect(), + result: result.result.map(|v| to_base64(&v)), + } + } +} + +impl From for TransactionResult { + fn from(view: TransactionResultView) -> Self { + Self { + status: view.status, + logs: view.logs, + receipts: view.receipts.into_iter().map(|h| h.into()).collect(), + result: view.result.map(|v| from_base64(&v).unwrap()), + } + } +} + #[derive(Serialize, Deserialize, Debug)] pub struct TransactionLogView { pub hash: CryptoHashView, - pub result: TransactionResult, + pub result: TransactionResultView, } impl From for TransactionLogView { fn from(log: TransactionLog) -> Self { - Self { hash: log.hash.into(), result: log.result } + Self { hash: log.hash.into(), result: log.result.into() } } } @@ -600,12 +615,40 @@ impl FinalTransactionResult { logs.join("\n") } - pub fn last_result(&self) -> Vec { + pub fn last_result(&self) -> String { for transaction in self.transactions.iter().rev() { if let Some(r) = &transaction.result.result { return r.clone(); } } - vec![] + "".to_string() + } +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct ValidatorStakeView { + pub account_id: AccountId, + pub public_key: PublicKeyView, + #[serde(with = "u128_dec_format")] + pub amount: Balance, +} + +impl From for ValidatorStakeView { + fn from(stake: ValidatorStake) -> Self { + Self { + account_id: stake.account_id, + public_key: stake.public_key.into(), + amount: stake.amount, + } + } +} + +impl From for ValidatorStake { + fn from(view: ValidatorStakeView) -> Self { + Self { + account_id: view.account_id, + public_key: view.public_key.into(), + amount: view.amount, + } } } diff --git a/core/primitives/src/sharding.rs b/core/primitives/src/sharding.rs index 008902fc60d..7dc5101f466 100644 --- a/core/primitives/src/sharding.rs +++ b/core/primitives/src/sharding.rs @@ -1,5 +1,5 @@ use crate::crypto::group_signature::GroupSignature; -use crate::hash::{hash_struct, CryptoHash}; +use crate::hash::{hash, CryptoHash}; use crate::merkle::{merklize, MerklePath}; use crate::types::MerkleHash; use reed_solomon_erasure::{ReedSolomon, Shard}; @@ -19,19 +19,18 @@ pub struct MainChainLocalBlock { pub body: Option, } -#[derive(Serialize, Clone)] +#[derive(Clone)] pub struct ShardChunkHeader { pub prev_block_hash: CryptoHash, pub encoded_merkle_root: CryptoHash, pub height: u64, } -#[derive(Default, Serialize)] +#[derive(Default)] pub struct EncodedShardChunkBody { pub parts: Vec>, } -#[derive(Serialize)] pub struct EncodedShardChunk { pub header: ShardChunkHeader, pub content: EncodedShardChunkBody, @@ -82,6 +81,6 @@ impl EncodedShardChunk { } pub fn chunk_hash(&self) -> CryptoHash { - hash_struct(self) + hash(&[0]) } } diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index 7b1e6637fb9..ba40ed9a191 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -197,7 +197,7 @@ impl Default for TransactionStatus { } } -#[derive(nbor, PartialEq, Clone, Default, Serialize, Deserialize)] +#[derive(nbor, PartialEq, Clone, Default)] pub struct TransactionResult { /// Transaction status. pub status: TransactionStatus, diff --git a/core/primitives/src/types.rs b/core/primitives/src/types.rs index a1eae12cf3e..11ae4cd1314 100644 --- a/core/primitives/src/types.rs +++ b/core/primitives/src/types.rs @@ -41,15 +41,8 @@ pub type Gas = u64; pub type ReceiptIndex = usize; pub type PromiseId = Vec; -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hash, Clone)] -pub enum BlockId { - Best, - Number(BlockIndex), - Hash(CryptoHash), -} - /// Stores validator and its stake. -#[derive(nbor, Debug, Serialize, Deserialize, Clone)] +#[derive(nbor, Debug, Clone)] pub struct ValidatorStake { /// Account that stakes money. pub account_id: AccountId, diff --git a/runtime/runtime/src/adapter.rs b/runtime/runtime/src/adapter.rs index 638222cf0b9..dfca223a707 100644 --- a/runtime/runtime/src/adapter.rs +++ b/runtime/runtime/src/adapter.rs @@ -1,6 +1,8 @@ use near_primitives::account::{AccessKey, Account}; use near_primitives::crypto::signature::PublicKey; -use near_primitives::rpc::{AccessKeyInfo, CallResult, QueryError, QueryResponse, ViewStateResult}; +use near_primitives::rpc::{ + AccessKeyInfoView, CallResult, QueryError, QueryResponse, ViewStateResult, +}; use near_primitives::serialize::BaseDecode; use near_primitives::types::{AccountId, BlockIndex, MerkleHash}; @@ -86,7 +88,7 @@ pub fn query_client( adapter.view_access_keys(state_root, &AccountId::from(path_parts[1])).map(|r| { QueryResponse::AccessKeyList( r.into_iter() - .map(|(public_key, access_key)| AccessKeyInfo { + .map(|(public_key, access_key)| AccessKeyInfoView { public_key: public_key.into(), access_key: access_key.into(), }) diff --git a/runtime/runtime/src/state_viewer.rs b/runtime/runtime/src/state_viewer.rs index a0134335991..30434805259 100644 --- a/runtime/runtime/src/state_viewer.rs +++ b/runtime/runtime/src/state_viewer.rs @@ -35,10 +35,8 @@ impl TrieViewer { return Err(format!("Account ID '{}' is not valid", account_id).into()); } - match get_account(state_update, &account_id) { - Some(account) => Ok(account), - _ => Err(format!("account {} does not exist while viewing", account_id).into()), - } + get_account(state_update, &account_id) + .ok_or_else(|| format!("account {} does not exist while viewing", account_id).into()) } pub fn view_access_key( diff --git a/test-utils/keystore/src/main.rs b/test-utils/keystore/src/main.rs index dcabefdb2c0..a6cc690eb33 100644 --- a/test-utils/keystore/src/main.rs +++ b/test-utils/keystore/src/main.rs @@ -1,55 +1,11 @@ -#[macro_use] -extern crate serde_derive; - -use std::fs; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::process; use clap::{App, Arg, ArgMatches, SubCommand}; -use near_primitives::crypto::signature::{sign, PublicKey, SecretKey}; -use near_primitives::crypto::signer::{ - get_key_file, write_block_producer_key_file, InMemorySigner, -}; -use near_primitives::hash::hash; -use near_primitives::serialize::{from_base, to_base, BaseEncode}; - -#[derive(Serialize)] -struct TypeValue { - #[serde(rename = "type")] - type_field: String, - value: String, -} - -#[derive(Serialize)] -struct TendermintKeyFile { - address: String, - pub_key: TypeValue, - priv_key: TypeValue, -} - -fn write_tendermint_key_file(key_store_path: &Path, public_key: PublicKey, secret_key: SecretKey) { - if !key_store_path.exists() { - fs::create_dir_all(key_store_path).unwrap(); - } - - let address_bytes = hash(public_key.as_ref()).as_ref()[..20].to_vec(); - let address = hex::encode(&address_bytes); - let key_file = TendermintKeyFile { - address, - pub_key: TypeValue { - type_field: "tendermint/PubKeyEd25519".to_string(), - value: public_key.to_base(), - }, - priv_key: TypeValue { - type_field: "tendermint/PrivKeyEd25519".to_string(), - value: secret_key.to_base(), - }, - }; - let key_file_path = key_store_path.join(Path::new("priv_validator_key.json")); - let serialized = serde_json::to_string(&key_file).unwrap(); - fs::write(key_file_path, serialized).unwrap(); -} +use near_primitives::crypto::signature::sign; +use near_primitives::crypto::signer::{get_key_file, write_key_file, InMemorySigner}; +use near_primitives::serialize::{from_base, to_base}; fn get_key_store_path(matches: &ArgMatches) -> PathBuf { matches.value_of("key_store_path").map(PathBuf::from).unwrap() @@ -71,13 +27,7 @@ fn sign_data(matches: &ArgMatches) { fn generate_key(matches: &ArgMatches) { let key_store_path = get_key_store_path(matches); let signer = InMemorySigner::from_seed("not_used", matches.value_of("test_seed").unwrap()); - write_block_producer_key_file(&key_store_path.as_path(), signer.public_key, signer.secret_key); -} - -fn generate_tendermint_key(matches: &ArgMatches) { - let key_store_path = get_key_store_path(matches); - let signer = InMemorySigner::from_seed("not_used", matches.value_of("test_seed").unwrap()); - write_tendermint_key_file(&key_store_path.as_path(), signer.public_key, signer.secret_key); + write_key_file(&key_store_path.as_path(), signer.public_key, signer.secret_key); } fn get_public_key(matches: &ArgMatches) { @@ -98,20 +48,17 @@ fn main() { .takes_value(true); let matches = App::new("keystore") .subcommand( - SubCommand::with_name("keygen") - .arg(key_store_path_arg) - .arg( - Arg::with_name("test_seed") - .long("test-seed") - .value_name("TEST_SEED") - .help( - "Specify a seed for generating a key pair.\ - This should only be used for deterministically \ - creating key pairs during tests.", - ) - .takes_value(true), - ) - .arg(Arg::with_name("tendermint").long("tendermint").takes_value(false)), + SubCommand::with_name("keygen").arg(key_store_path_arg).arg( + Arg::with_name("test_seed") + .long("test-seed") + .value_name("TEST_SEED") + .help( + "Specify a seed for generating a key pair.\ + This should only be used for deterministically \ + creating key pairs during tests.", + ) + .takes_value(true), + ), ) .subcommand(SubCommand::with_name("get_public_key").arg(key_store_path_arg)) .subcommand( @@ -141,11 +88,7 @@ fn main() { .get_matches(); if let Some(sub) = matches.subcommand_matches("keygen") { - if sub.is_present("tendermint") { - generate_tendermint_key(sub); - } else { - generate_key(sub); - } + generate_key(sub); } else if let Some(sub) = matches.subcommand_matches("sign") { sign_data(sub); } else if let Some(sub) = matches.subcommand_matches("get_public_key") { diff --git a/test-utils/state-viewer/src/main.rs b/test-utils/state-viewer/src/main.rs index 53195e6cd07..117f643844e 100644 --- a/test-utils/state-viewer/src/main.rs +++ b/test-utils/state-viewer/src/main.rs @@ -44,7 +44,7 @@ fn kv_to_state_record(key: Vec, value: DBValue) -> StateRecord { } else { let account = Account::try_from_slice(&value).unwrap(); StateRecord::Account { - account_id: to_printable(&key[1..]), + account_id: String::from_utf8(key[1..].to_vec()).unwrap(), account: account.into(), } } @@ -61,7 +61,7 @@ fn kv_to_state_record(key: Vec, value: DBValue) -> StateRecord { col::ACCESS_KEY => { let separator = (1..key.len()).find(|&x| key[x] == col::ACCESS_KEY[0]).unwrap(); let access_key = AccessKey::try_from_slice(&value).unwrap(); - let account_id = to_printable(&key[1..separator]); + let account_id = String::from_utf8(key[1..separator].to_vec()).unwrap(); let public_key = PublicKey::try_from(&key[(separator + 1)..]).unwrap(); StateRecord::AccessKey { account_id, diff --git a/test-utils/testlib/src/user/mod.rs b/test-utils/testlib/src/user/mod.rs index 735d9a05e53..e8d39fcd0e5 100644 --- a/test-utils/testlib/src/user/mod.rs +++ b/test-utils/testlib/src/user/mod.rs @@ -7,10 +7,10 @@ use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; -use near_primitives::rpc::FinalTransactionResult; use near_primitives::rpc::{ AccessKeyView, AccountView, BlockView, CryptoHashView, ViewStateResult, }; +use near_primitives::rpc::{FinalTransactionResult, TransactionResultView}; use near_primitives::transaction::{ Action, AddKeyAction, CreateAccountAction, DeleteAccountAction, DeleteKeyAction, DeployContractAction, FunctionCallAction, SignedTransaction, StakeAction, TransactionResult, @@ -53,7 +53,7 @@ pub trait User { fn get_block(&self, index: u64) -> Option; - fn get_transaction_result(&self, hash: &CryptoHash) -> TransactionResult; + fn get_transaction_result(&self, hash: &CryptoHash) -> TransactionResultView; fn get_transaction_final_result(&self, hash: &CryptoHash) -> FinalTransactionResult; diff --git a/test-utils/testlib/src/user/rpc_user.rs b/test-utils/testlib/src/user/rpc_user.rs index f127d9d2c30..443cd30984b 100644 --- a/test-utils/testlib/src/user/rpc_user.rs +++ b/test-utils/testlib/src/user/rpc_user.rs @@ -12,10 +12,10 @@ use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; use near_primitives::rpc::{ AccessKeyView, AccountView, BlockView, CryptoHashView, FinalTransactionResult, QueryResponse, - ViewStateResult, + TransactionResultView, ViewStateResult, }; use near_primitives::serialize::{to_base, to_base64, BaseEncode}; -use near_primitives::transaction::{SignedTransaction, TransactionResult}; +use near_primitives::transaction::SignedTransaction; use near_primitives::types::AccountId; use crate::user::User; @@ -77,7 +77,7 @@ impl User for RpcUser { System::new("actix").block_on(self.client.write().unwrap().block(index)).ok() } - fn get_transaction_result(&self, hash: &CryptoHash) -> TransactionResult { + fn get_transaction_result(&self, hash: &CryptoHash) -> TransactionResultView { System::new("actix").block_on(self.client.write().unwrap().tx_details(hash.into())).unwrap() } diff --git a/test-utils/testlib/src/user/runtime_user.rs b/test-utils/testlib/src/user/runtime_user.rs index 8670a927cdf..2bb09d976f4 100644 --- a/test-utils/testlib/src/user/runtime_user.rs +++ b/test-utils/testlib/src/user/runtime_user.rs @@ -10,12 +10,11 @@ use near_primitives::crypto::signer::EDSigner; use near_primitives::hash::CryptoHash; use near_primitives::receipt::{Receipt, ReceiptInfo}; use near_primitives::rpc::{ - AccessKeyView, AccountView, BlockView, CryptoHashView, ViewStateResult, + AccessKeyView, AccountView, BlockView, CryptoHashView, TransactionLogView, + TransactionResultView, ViewStateResult, }; use near_primitives::rpc::{FinalTransactionResult, FinalTransactionStatus}; -use near_primitives::transaction::{ - SignedTransaction, TransactionLog, TransactionResult, TransactionStatus, -}; +use near_primitives::transaction::{SignedTransaction, TransactionStatus}; use near_primitives::types::{AccountId, BlockIndex, MerkleHash}; use near_store::{Trie, TrieUpdate}; use node_runtime::ethereum::EthashProvider; @@ -46,7 +45,7 @@ pub struct RuntimeUser { pub trie_viewer: TrieViewer, pub client: Arc>, // Store results of applying transactions/receipts - pub transaction_results: RefCell>, + pub transaction_results: RefCell>, // store receipts generated when applying transactions pub receipts: RefCell>, } @@ -91,7 +90,7 @@ impl RuntimeUser { for transaction_result in apply_result.tx_result.into_iter() { self.transaction_results .borrow_mut() - .insert(transaction_result.hash, transaction_result.result); + .insert(transaction_result.hash, transaction_result.result.into()); } apply_result.trie_changes.into(client.trie.clone()).unwrap().0.commit().unwrap(); if apply_result.new_receipts.is_empty() { @@ -126,12 +125,13 @@ impl RuntimeUser { } } - fn get_recursive_transaction_results(&self, hash: &CryptoHash) -> Vec { + fn get_recursive_transaction_results(&self, hash: &CryptoHash) -> Vec { let result = self.get_transaction_result(hash); let receipt_ids = result.receipts.clone(); - let mut transactions = vec![TransactionLog { hash: *hash, result }]; + let mut transactions = vec![TransactionLogView { hash: hash.clone().into(), result }]; for hash in &receipt_ids { - transactions.extend(self.get_recursive_transaction_results(hash).into_iter()); + transactions + .extend(self.get_recursive_transaction_results(&hash.clone().into()).into_iter()); } transactions } @@ -202,7 +202,7 @@ impl User for RuntimeUser { unimplemented!("get_block should not be implemented for RuntimeUser"); } - fn get_transaction_result(&self, hash: &CryptoHash) -> TransactionResult { + fn get_transaction_result(&self, hash: &CryptoHash) -> TransactionResultView { self.transaction_results.borrow().get(hash).cloned().unwrap() } @@ -217,7 +217,11 @@ impl User for RuntimeUser { fn get_receipt_info(&self, hash: &CryptoHash) -> Option { let receipt = self.receipts.borrow().get(hash).cloned()?; let transaction_result = self.transaction_results.borrow().get(hash).cloned()?; - Some(ReceiptInfo { receipt, result: transaction_result, block_index: Default::default() }) + Some(ReceiptInfo { + receipt, + result: transaction_result.into(), + block_index: Default::default(), + }) } fn get_access_key( From ae720a554bce9c38149b82f88985a447823cfeb7 Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Fri, 16 Aug 2019 13:55:44 -0700 Subject: [PATCH 22/25] Add verification for CryptoHash --- core/primitives/src/rpc.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index 4ead6190fc5..78e1041faef 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -125,9 +125,13 @@ impl<'de> Deserialize<'de> for CryptoHashView { D: Deserializer<'de>, { let s = String::deserialize(deserializer)?; - from_base(&s) + let view = from_base(&s) .map(|v| CryptoHashView(v)) - .map_err(|err| serde::de::Error::custom(err.to_string())) + .map_err(|err| serde::de::Error::custom(err.to_string()))?; + if let Err(err) = CryptoHash::try_from(view.0.clone()) { + return Err(serde::de::Error::custom(err.to_string())); + } + Ok(view) } } From e982ef1e222cc33a8fa6d14a59c0423cc13eaf4e Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Fri, 16 Aug 2019 14:53:41 -0700 Subject: [PATCH 23/25] Switch to external repo borsh --- Cargo.lock | 48 ++++++++++++++++++++----- chain/chain/Cargo.toml | 3 +- chain/chain/src/types.rs | 5 +-- chain/jsonrpc/Cargo.toml | 3 +- chain/network/Cargo.toml | 3 +- chain/network/src/types.rs | 20 +++++------ core/primitives/Cargo.toml | 2 +- core/primitives/src/account.rs | 33 ++++++++++++++--- core/primitives/src/block.rs | 16 +++++---- core/primitives/src/crypto/signature.rs | 20 +++++++---- core/primitives/src/hash.rs | 4 +-- core/primitives/src/receipt.rs | 12 +++---- core/primitives/src/rpc.rs | 31 ++++------------ core/primitives/src/transaction.rs | 36 ++++++++++--------- core/primitives/src/types.rs | 4 +-- core/store/Cargo.toml | 2 +- near/Cargo.toml | 2 +- near/src/validator_manager.rs | 9 ++--- test-utils/loadtester/Cargo.toml | 3 +- test-utils/state-viewer/Cargo.toml | 3 +- test-utils/testlib/Cargo.toml | 3 +- 21 files changed, 159 insertions(+), 103 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 245f5d212b1..3d7ce9d254b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -503,6 +503,33 @@ name = "blockchain" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "borsh" +version = "0.1.0" +source = "git+https://github.com/nearprotocol/borsh.git#1331099024ad28a4541aa453b75867ebd30123f9" +dependencies = [ + "borsh-derive 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", +] + +[[package]] +name = "borsh-derive" +version = "0.1.0" +source = "git+https://github.com/nearprotocol/borsh.git#1331099024ad28a4541aa453b75867ebd30123f9" +dependencies = [ + "borsh-derive-internal 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", + "syn 0.15.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "borsh-derive-internal" +version = "0.1.0" +source = "git+https://github.com/nearprotocol/borsh.git#1331099024ad28a4541aa453b75867ebd30123f9" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "brotli-sys" version = "0.3.2" @@ -1805,12 +1832,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "loadtester" version = "0.0.1" dependencies = [ + "borsh 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "nbor 0.1.0", "near 0.2.7", "near-primitives 0.1.0", "node-runtime 0.0.1", @@ -2038,6 +2065,7 @@ name = "near" version = "0.2.7" dependencies = [ "actix 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", + "borsh 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2049,7 +2077,6 @@ dependencies = [ "kvdb-memorydb 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "nbor 0.1.0", "near-chain 0.1.0", "near-client 0.1.0", "near-jsonrpc 0.1.0", @@ -2072,13 +2099,13 @@ dependencies = [ name = "near-chain" version = "0.1.0" dependencies = [ + "borsh 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", "cached 0.8.0 (git+https://github.com/nearprotocol/cached?rev=7e472eddef68607e344d5a106a0e6705d92e55be)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "kvdb 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "nbor 0.1.0", "near-primitives 0.1.0", "near-store 0.1.0", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2118,12 +2145,12 @@ dependencies = [ "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "async-utils 0.1.0", "base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "borsh 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "futures-preview 0.3.0-alpha.16 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "nbor 0.1.0", "near-chain 0.1.0", "near-client 0.1.0", "near-jsonrpc-client 0.1.0", @@ -2156,11 +2183,11 @@ name = "near-network" version = "0.1.0" dependencies = [ "actix 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", + "borsh 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "nbor 0.1.0", "near-chain 0.1.0", "near-client 0.1.0", "near-primitives 0.1.0", @@ -2193,6 +2220,7 @@ dependencies = [ "base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "bencher 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "bincode 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "borsh 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", "bs58 0.2.3 (git+https://github.com/ilblackdragon/bs58-rs?rev=46a818c93cd2ba19c2d5d9aefa8e3062ffb98d9b)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2201,7 +2229,6 @@ dependencies = [ "jemallocator 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "nbor 0.1.0", "pairing 0.14.1 (git+https://github.com/nearprotocol/pairing.git?rev=f009a9f54c1c1149cea4ee3e6e58ed71d72bb2e9)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2219,6 +2246,7 @@ name = "near-store" version = "0.1.0" dependencies = [ "bencher 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "borsh 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "cached 0.8.0 (git+https://github.com/nearprotocol/cached?rev=7e472eddef68607e344d5a106a0e6705d92e55be)", "elastic-array 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2227,7 +2255,6 @@ dependencies = [ "kvdb-memorydb 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "kvdb-rocksdb 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "nbor 0.1.0", "near-primitives 0.1.0", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3322,9 +3349,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "state-viewer" version = "0.1.0" dependencies = [ + "borsh 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", - "nbor 0.1.0", "near 0.2.7", "near-chain 0.1.0", "near-network 0.1.0", @@ -3451,12 +3478,12 @@ name = "testlib" version = "0.1.0" dependencies = [ "actix 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", + "borsh 0.1.0 (git+https://github.com/nearprotocol/borsh.git)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "nbor 0.1.0", "near 0.2.7", "near-chain 0.1.0", "near-client 0.1.0", @@ -4233,6 +4260,9 @@ dependencies = [ "checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" "checksum block-padding 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6d4dc3af3ee2e12f3e5d224e5e1e3d73668abbeb69e566d361f7d5563a4fdf09" "checksum blockchain 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b25c0f727a410bef0ea87d7437aa403f669ec4d7de1b302f4d819f83d3b4c561" +"checksum borsh 0.1.0 (git+https://github.com/nearprotocol/borsh.git)" = "" +"checksum borsh-derive 0.1.0 (git+https://github.com/nearprotocol/borsh.git)" = "" +"checksum borsh-derive-internal 0.1.0 (git+https://github.com/nearprotocol/borsh.git)" = "" "checksum brotli-sys 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4445dea95f4c2b41cde57cc9fee236ae4dbae88d8fcbdb4750fc1bb5d86aaecd" "checksum brotli2 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0cb036c3eade309815c15ddbacec5b22c4d1f3983a774ab2eac2e3e9ea85568e" "checksum bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0de79cfb98e7aa9988188784d8664b4b5dad6eaaa0863b91d9a4ed871d4f7a42" diff --git a/chain/chain/Cargo.toml b/chain/chain/Cargo.toml index 32438c5d7c7..fa6c1608c1d 100644 --- a/chain/chain/Cargo.toml +++ b/chain/chain/Cargo.toml @@ -14,6 +14,7 @@ serde = "1.0" serde_derive = "1.0" cached = { git = "https://github.com/nearprotocol/cached", rev = "7e472eddef68607e344d5a106a0e6705d92e55be" } -nbor = { path = "../../core/nbor" } +borsh = { git = "https://github.com/nearprotocol/borsh.git" } + near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } diff --git a/chain/chain/src/types.rs b/chain/chain/src/types.rs index 91d9636e974..9b2d2cdee59 100644 --- a/chain/chain/src/types.rs +++ b/chain/chain/src/types.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; -use nbor::nbor; +use borsh::{BorshDeserialize, BorshSerialize}; + pub use near_primitives::block::{Block, BlockHeader, Weight}; use near_primitives::crypto::signature::Signature; use near_primitives::crypto::signer::EDSigner; @@ -167,7 +168,7 @@ pub trait RuntimeAdapter: Send + Sync { /// The tip of a fork. A handle to the fork ancestry from its leaf in the /// blockchain tree. References the max height and the latest and previous /// blocks for convenience and the total weight. -#[derive(nbor, Debug, Clone, PartialEq)] +#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq)] pub struct Tip { /// Height of the tip (max height of the fork) pub height: BlockIndex, diff --git a/chain/jsonrpc/Cargo.toml b/chain/jsonrpc/Cargo.toml index 8f585eb385b..d324f0e4e8d 100644 --- a/chain/jsonrpc/Cargo.toml +++ b/chain/jsonrpc/Cargo.toml @@ -21,8 +21,9 @@ serde = { version = "1.0", features = ["derive"] } tokio = "0.1.15" uuid = { version = "~0.6", features = ["v4"] } +borsh = { git = "https://github.com/nearprotocol/borsh.git" } + async-utils = { path = "../../async-utils" } -nbor = { path = "../../core/nbor" } near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } near-chain = { path = "../chain" } diff --git a/chain/network/Cargo.toml b/chain/network/Cargo.toml index f92f1cc17db..aee8596ee4f 100644 --- a/chain/network/Cargo.toml +++ b/chain/network/Cargo.toml @@ -15,7 +15,8 @@ serde = "1.0" serde_derive = "1.0" rand = "0.6.5" -nbor = { path = "../../core/nbor" } +borsh = { git = "https://github.com/nearprotocol/borsh.git" } + near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } near-chain = { path = "../chain" } diff --git a/chain/network/src/types.rs b/chain/network/src/types.rs index bd24bbfbf86..0259079540f 100644 --- a/chain/network/src/types.rs +++ b/chain/network/src/types.rs @@ -28,7 +28,7 @@ use near_primitives::utils::{from_timestamp, to_timestamp}; pub const PROTOCOL_VERSION: u32 = 2; /// Peer id is the public key. -#[derive(nbor, Copy, Clone, Eq, PartialOrd, Ord, PartialEq)] +#[derive(BorshSerialize, BorshDeserialize, Copy, Clone, Eq, PartialOrd, Ord, PartialEq)] pub struct PeerId(PublicKey); impl PeerId { @@ -82,7 +82,7 @@ impl fmt::Debug for PeerId { } /// Peer information. -#[derive(nbor, Clone, Debug, Eq, PartialEq)] +#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)] pub struct PeerInfo { pub id: PeerId, pub addr: Option, @@ -132,7 +132,7 @@ impl TryFrom<&str> for PeerInfo { } /// Peer chain information. -#[derive(nbor, Copy, Clone, Debug, Eq, PartialEq, Default)] +#[derive(BorshSerialize, BorshDeserialize, Copy, Clone, Debug, Eq, PartialEq, Default)] pub struct PeerChainInfo { /// Genesis hash. pub genesis: CryptoHash, @@ -162,7 +162,7 @@ pub enum PeerStatus { Banned(ReasonForBan), } -#[derive(nbor, PartialEq, Eq, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)] pub struct Handshake { /// Protocol version. pub version: u32, @@ -181,7 +181,7 @@ impl Handshake { } /// Account route description -#[derive(nbor, PartialEq, Eq, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)] pub struct AnnounceAccountRoute { pub peer_id: PeerId, pub hash: CryptoHash, @@ -189,7 +189,7 @@ pub struct AnnounceAccountRoute { } /// Account announcement information -#[derive(nbor, PartialEq, Eq, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)] pub struct AnnounceAccount { /// AccountId to be announced pub account_id: AccountId, @@ -255,7 +255,7 @@ impl AnnounceAccount { } } -#[derive(nbor, PartialEq, Eq, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)] pub enum PeerMessage { Handshake(Handshake), @@ -321,7 +321,7 @@ pub struct NetworkConfig { } /// Status of the known peers. -#[derive(nbor, Eq, PartialEq, Debug)] +#[derive(BorshSerialize, BorshDeserialize, Eq, PartialEq, Debug)] pub enum KnownPeerStatus { Unknown, NotConnected, @@ -330,7 +330,7 @@ pub enum KnownPeerStatus { } /// Information node stores about known peers. -#[derive(nbor, Debug)] +#[derive(BorshSerialize, BorshDeserialize, Debug)] pub struct KnownPeerState { pub peer_info: PeerInfo, pub status: KnownPeerStatus, @@ -440,7 +440,7 @@ where } /// Ban reason. -#[derive(nbor, Debug, Clone, PartialEq, Eq, Copy)] +#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq, Copy)] pub enum ReasonForBan { None = 0, BadBlock = 1, diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index 7a3efbaf0f6..b8bff228703 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -26,7 +26,7 @@ log = "0.4" reed-solomon-erasure = "3.1.1" jemallocator = { version = "0.3.0", optional = true } -nbor = { path = "../nbor" } +borsh = { git = "https://github.com/nearprotocol/borsh.git" } [features] default = ["jemallocator"] diff --git a/core/primitives/src/account.rs b/core/primitives/src/account.rs index 1ca6172eb58..329031e5f11 100644 --- a/core/primitives/src/account.rs +++ b/core/primitives/src/account.rs @@ -1,10 +1,10 @@ -use nbor::nbor; +use borsh::{BorshDeserialize, BorshSerialize}; use crate::hash::CryptoHash; use crate::types::{AccountId, Balance, BlockIndex, Nonce, StorageUsage}; /// Per account information stored in the state. -#[derive(nbor, PartialEq, Eq, Debug, Clone)] +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Debug, Clone)] pub struct Account { /// The sum of `amount` and `staked` is the total value of the account. pub amount: Balance, @@ -40,7 +40,7 @@ impl Account { /// access keys. Access keys allow to act on behalf of the account by restricting transactions /// that can be issued. /// `account_id,public_key` is a key in the state -#[derive(nbor, PartialEq, Eq, Hash, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Hash, Clone, Debug)] pub struct AccessKey { /// The nonce for this access key. /// NOTE: In some cases the access key needs to be recreated. If the new access key reuses the @@ -59,7 +59,7 @@ impl AccessKey { } /// Defines permissions for AccessKey -#[derive(nbor, PartialEq, Eq, Hash, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Hash, Clone, Debug)] pub enum AccessKeyPermission { FunctionCall(FunctionCallPermission), @@ -73,7 +73,7 @@ pub enum AccessKeyPermission { /// The permission can limit the allowed balance to be spent on the prepaid gas. /// It also restrict the account ID of the receiver for this function call. /// It also can restrict the method name for the allowed function calls. -#[derive(nbor, PartialEq, Eq, Hash, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Hash, Clone, Debug)] pub struct FunctionCallPermission { /// Allowance is a balance limit to use by this access key to pay for function call gas and /// transaction fees. When this access key is used, both account balance and the allowance is @@ -91,3 +91,26 @@ pub struct FunctionCallPermission { /// Empty list means any method name can be used. pub method_names: Vec, } + +#[cfg(test)] +mod tests { + use borsh::Serializable; + + use crate::hash::hash; + use crate::serialize::to_base; + + use super::*; + + #[test] + fn test_account_serialization() { + let acc = Account { + amount: 1_000_000, + staked: 1_000_000, + code_hash: CryptoHash::default(), + storage_usage: 100, + storage_paid_at: 1_123_321, + }; + let bytes = acc.try_to_vec().unwrap(); + assert_eq!(to_base(&hash(&bytes)), "DzpbYEwBoiKa3DRTgK2L8fBq3QRfGSoUkTXrTYxwBt17"); + } +} diff --git a/core/primitives/src/block.rs b/core/primitives/src/block.rs index 5e22df3bc58..abc6acdfb21 100644 --- a/core/primitives/src/block.rs +++ b/core/primitives/src/block.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use chrono::prelude::{DateTime, Utc}; -use nbor::{nbor, Serializable}; +use borsh::{BorshDeserialize, BorshSerialize, Serializable}; use crate::crypto::signature::{verify, PublicKey, Signature, DEFAULT_SIGNATURE}; use crate::crypto::signer::EDSigner; @@ -12,7 +12,7 @@ use crate::transaction::SignedTransaction; use crate::types::{BlockIndex, MerkleHash, ValidatorStake}; use crate::utils::{from_timestamp, to_timestamp}; -#[derive(nbor, Debug, Clone, Eq, PartialEq)] +#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Eq, PartialEq)] pub struct BlockHeaderInner { /// Height of this block since the genesis block (height 0). pub height: BlockIndex, @@ -65,8 +65,8 @@ impl BlockHeaderInner { } } -#[derive(nbor, Debug, Clone, Eq, PartialEq)] -#[nbor_init(init)] +#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Eq, PartialEq)] +#[borsh_init(init)] pub struct BlockHeader { /// Inner part of the block header that gets hashed. pub inner: BlockHeaderInner, @@ -75,7 +75,7 @@ pub struct BlockHeader { pub signature: Signature, /// Cached value of hash for this block. - #[nbor_skip] + #[borsh_skip] pub hash: CryptoHash, } @@ -144,7 +144,7 @@ impl BlockHeader { } } -#[derive(nbor, Debug, Clone, Eq, PartialEq)] +#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Eq, PartialEq)] pub struct Block { pub header: BlockHeader, pub transactions: Vec, @@ -217,7 +217,9 @@ impl Block { } /// The weight is defined as the number of unique validators approving this fork. -#[derive(nbor, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Default)] +#[derive( + BorshSerialize, BorshDeserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Default, +)] pub struct Weight { num: u64, } diff --git a/core/primitives/src/crypto/signature.rs b/core/primitives/src/crypto/signature.rs index 11bc8a4ed87..6f3e17ae6d8 100644 --- a/core/primitives/src/crypto/signature.rs +++ b/core/primitives/src/crypto/signature.rs @@ -119,7 +119,7 @@ impl TryFrom<&str> for PublicKey { } } -impl nbor::Serializable for PublicKey { +impl borsh::Serializable for PublicKey { fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { // TODO: support other curves here. writer.write(&vec![0])?; @@ -128,7 +128,7 @@ impl nbor::Serializable for PublicKey { } } -impl nbor::Deserializable for PublicKey { +impl borsh::Deserializable for PublicKey { fn read(reader: &mut R) -> Result { let mut bytes = [0; sodiumoxide::crypto::sign::ed25519::PUBLICKEYBYTES + 1]; reader.read(&mut bytes)?; @@ -216,18 +216,26 @@ impl TryFrom<&str> for Signature { } } -impl nbor::Serializable for Signature { +impl borsh::Serializable for Signature { fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { + // TODO: add crypto curve. + writer.write(&vec![0])?; writer.write(&(self.0).0)?; Ok(()) } } -impl nbor::Deserializable for Signature { +impl borsh::Deserializable for Signature { fn read(reader: &mut R) -> Result { - let mut bytes = [0; sodiumoxide::crypto::sign::ed25519::SIGNATUREBYTES]; + // TODO: add crypto curve. + let mut bytes = [0; sodiumoxide::crypto::sign::ed25519::SIGNATUREBYTES + 1]; reader.read(&mut bytes)?; - Ok(Signature(sodiumoxide::crypto::sign::ed25519::Signature(bytes))) + if bytes[0] != 0 { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "Unsupported curve")); + } + let mut array = [0; sodiumoxide::crypto::sign::ed25519::SIGNATUREBYTES]; + array.copy_from_slice(&bytes[1..]); + Ok(Signature(sodiumoxide::crypto::sign::ed25519::Signature(array))) } } diff --git a/core/primitives/src/hash.rs b/core/primitives/src/hash.rs index 54abfbce416..92c83034782 100644 --- a/core/primitives/src/hash.rs +++ b/core/primitives/src/hash.rs @@ -47,14 +47,14 @@ impl AsMut<[u8]> for CryptoHash { impl BaseDecode for CryptoHash {} -impl nbor::Serializable for CryptoHash { +impl borsh::Serializable for CryptoHash { fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { writer.write(&(self.0).0)?; Ok(()) } } -impl nbor::Deserializable for CryptoHash { +impl borsh::Deserializable for CryptoHash { fn read(reader: &mut R) -> Result { let mut bytes = [0; 32]; reader.read(&mut bytes)?; diff --git a/core/primitives/src/receipt.rs b/core/primitives/src/receipt.rs index a9b1aa048da..fe50304f68a 100644 --- a/core/primitives/src/receipt.rs +++ b/core/primitives/src/receipt.rs @@ -1,7 +1,7 @@ use std::borrow::Borrow; use std::fmt; -use nbor::nbor; +use borsh::{BorshDeserialize, BorshSerialize}; use crate::crypto::signature::PublicKey; use crate::hash::CryptoHash; @@ -17,7 +17,7 @@ pub struct ReceiptInfo { pub result: TransactionResult, } -#[derive(nbor, Hash, Debug, PartialEq, Eq, Clone)] +#[derive(BorshSerialize, BorshDeserialize, Hash, Debug, PartialEq, Eq, Clone)] pub struct Receipt { pub predecessor_id: AccountId, pub receiver_id: AccountId, @@ -57,13 +57,13 @@ impl Receipt { } } -#[derive(nbor, Hash, Clone, Debug, PartialEq, Eq)] +#[derive(BorshSerialize, BorshDeserialize, Hash, Clone, Debug, PartialEq, Eq)] pub enum ReceiptEnum { Action(ActionReceipt), Data(DataReceipt), } -#[derive(nbor, Hash, Debug, PartialEq, Eq, Clone)] +#[derive(BorshSerialize, BorshDeserialize, Hash, Debug, PartialEq, Eq, Clone)] pub struct ActionReceipt { pub signer_id: AccountId, pub signer_public_key: PublicKey, @@ -78,13 +78,13 @@ pub struct ActionReceipt { pub actions: Vec, } -#[derive(nbor, Hash, Clone, Debug, PartialEq, Eq)] +#[derive(BorshSerialize, BorshDeserialize, Hash, Clone, Debug, PartialEq, Eq)] pub struct DataReceiver { pub data_id: CryptoHash, pub receiver_id: AccountId, } -#[derive(nbor, Hash, PartialEq, Eq, Clone)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone)] pub struct DataReceipt { pub data_id: CryptoHash, pub data: Option>, diff --git a/core/primitives/src/rpc.rs b/core/primitives/src/rpc.rs index 78e1041faef..024e1dae579 100644 --- a/core/primitives/src/rpc.rs +++ b/core/primitives/src/rpc.rs @@ -71,39 +71,22 @@ impl From for SecretKey { } } -#[derive(Debug, Clone)] -pub struct SignatureView(Vec); - -impl Serialize for SignatureView { - fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> - where - S: Serializer, - { - serializer.serialize_str(&to_base(&self.0)) - } -} - -impl<'de> Deserialize<'de> for SignatureView { - fn deserialize(deserializer: D) -> Result>::Error> - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - from_base(&s) - .map(|v| SignatureView(v)) - .map_err(|err| serde::de::Error::custom(err.to_string())) - } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct SignatureView { + key_type: KeyType, + #[serde(with = "base_bytes_format")] + data: Vec, } impl From for SignatureView { fn from(signature: Signature) -> Self { - Self(signature.0.as_ref().to_vec()) + Self { key_type: KeyType::ED25519, data: signature.0.as_ref().to_vec() } } } impl From for Signature { fn from(view: SignatureView) -> Self { - Signature::try_from(view.0).expect("Failed to get Signature from SignatureView") + Signature::try_from(view.data).expect("Failed to get Signature from SignatureView") } } diff --git a/core/primitives/src/transaction.rs b/core/primitives/src/transaction.rs index ba40ed9a191..2e1b26e73e7 100644 --- a/core/primitives/src/transaction.rs +++ b/core/primitives/src/transaction.rs @@ -2,7 +2,7 @@ use std::fmt; use std::hash::{Hash, Hasher}; use std::sync::Arc; -use nbor::{nbor, Serializable}; +use borsh::{BorshDeserialize, BorshSerialize, Serializable}; use crate::account::AccessKey; use crate::crypto::signature::{verify, PublicKey}; @@ -13,7 +13,7 @@ use crate::types::{AccountId, Balance, Gas, Nonce, StructSignature}; pub type LogEntry = String; -#[derive(nbor, Hash, PartialEq, Eq, Debug, Clone)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Debug, Clone)] pub struct Transaction { pub signer_id: AccountId, pub public_key: PublicKey, @@ -30,7 +30,7 @@ impl Transaction { } } -#[derive(nbor, Hash, PartialEq, Eq, Debug, Clone)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Debug, Clone)] pub enum Action { CreateAccount(CreateAccountAction), DeployContract(DeployContractAction), @@ -58,10 +58,10 @@ impl Action { } } -#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone, Debug)] pub struct CreateAccountAction {} -#[derive(nbor, Hash, PartialEq, Eq, Clone)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone)] pub struct DeployContractAction { pub code: Vec, } @@ -74,7 +74,7 @@ impl fmt::Debug for DeployContractAction { } } -#[derive(nbor, Hash, PartialEq, Eq, Clone)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone)] pub struct FunctionCallAction { pub method_name: String, pub args: Vec, @@ -93,39 +93,39 @@ impl fmt::Debug for FunctionCallAction { } } -#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone, Debug)] pub struct TransferAction { pub deposit: Balance, } -#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone, Debug)] pub struct StakeAction { pub stake: Balance, pub public_key: PublicKey, } -#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone, Debug)] pub struct AddKeyAction { pub public_key: PublicKey, pub access_key: AccessKey, } -#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone, Debug)] pub struct DeleteKeyAction { pub public_key: PublicKey, } -#[derive(nbor, Hash, PartialEq, Eq, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone, Debug)] pub struct DeleteAccountAction { pub beneficiary_id: AccountId, } -#[derive(nbor, Eq, Debug, Clone)] -#[nbor_init(init)] +#[derive(BorshSerialize, BorshDeserialize, Eq, Debug, Clone)] +#[borsh_init(init)] pub struct SignedTransaction { pub transaction: Transaction, pub signature: StructSignature, - #[nbor_skip] + #[borsh_skip] hash: CryptoHash, } @@ -184,7 +184,9 @@ impl PartialEq for SignedTransaction { } } -#[derive(nbor, Hash, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[derive( + BorshSerialize, BorshDeserialize, Hash, Debug, PartialEq, Eq, Clone, Serialize, Deserialize, +)] pub enum TransactionStatus { Unknown, Completed, @@ -197,7 +199,7 @@ impl Default for TransactionStatus { } } -#[derive(nbor, PartialEq, Clone, Default)] +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Clone, Default)] pub struct TransactionResult { /// Transaction status. pub status: TransactionStatus, @@ -240,7 +242,7 @@ pub fn verify_transaction_signature( mod tests { use std::convert::TryFrom; - use nbor::de::Deserializable; + use borsh::Deserializable; use crate::crypto::signature::{get_key_pair, sign, DEFAULT_SIGNATURE}; use crate::serialize::to_base; diff --git a/core/primitives/src/types.rs b/core/primitives/src/types.rs index 11ae4cd1314..acf333831ad 100644 --- a/core/primitives/src/types.rs +++ b/core/primitives/src/types.rs @@ -1,4 +1,4 @@ -use nbor::nbor; +use borsh::{BorshDeserialize, BorshSerialize}; use crate::crypto::aggregate_signature::BlsSignature; use crate::crypto::signature::{PublicKey, Signature}; @@ -42,7 +42,7 @@ pub type ReceiptIndex = usize; pub type PromiseId = Vec; /// Stores validator and its stake. -#[derive(nbor, Debug, Clone)] +#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)] pub struct ValidatorStake { /// Account that stakes money. pub account_id: AccountId, diff --git a/core/store/Cargo.toml b/core/store/Cargo.toml index 24f0d843558..716e71f4806 100644 --- a/core/store/Cargo.toml +++ b/core/store/Cargo.toml @@ -15,7 +15,7 @@ serde_derive = "1.0" cached = { git = "https://github.com/nearprotocol/cached", rev = "7e472eddef68607e344d5a106a0e6705d92e55be" } log = "0.4" -nbor = { path = "../nbor" } +borsh = { git = "https://github.com/nearprotocol/borsh.git" } near-primitives = { path = "../primitives" } [dev-dependencies] diff --git a/near/Cargo.toml b/near/Cargo.toml index 3bbd0985c2a..c3e43998a56 100644 --- a/near/Cargo.toml +++ b/near/Cargo.toml @@ -22,7 +22,7 @@ serde_json = "1.0" dirs = "1.0.5" lazy_static = "1.3" -nbor = { path = "../core/nbor" } +borsh = { git = "https://github.com/nearprotocol/borsh.git" } near-primitives = { path = "../core/primitives" } near-store = { path = "../core/store" } node-runtime = { path = "../runtime/runtime" } diff --git a/near/src/validator_manager.rs b/near/src/validator_manager.rs index f613a72b7d6..6c07f62f886 100644 --- a/near/src/validator_manager.rs +++ b/near/src/validator_manager.rs @@ -4,10 +4,10 @@ use std::fmt; use std::iter; use std::sync::Arc; +use borsh::{BorshDeserialize, BorshSerialize}; use rand::seq::SliceRandom; use rand::{rngs::StdRng, SeedableRng}; -use nbor::nbor; use near_primitives::hash::CryptoHash; use near_primitives::types::{ AccountId, Balance, BlockIndex, ShardId, ValidatorId, ValidatorStake, @@ -264,7 +264,7 @@ pub struct ValidatorEpochConfig { } /// Information about validator seat assignments. -#[derive(nbor, Default, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, Default, Clone, Debug)] pub struct ValidatorAssignment { /// List of current validators. pub validators: Vec, @@ -317,7 +317,7 @@ impl PartialEq for ValidatorAssignment { impl Eq for ValidatorAssignment {} /// Information per each index about validators. -#[derive(nbor, Default, Clone, Debug)] +#[derive(BorshSerialize, BorshDeserialize, Default, Clone, Debug)] pub struct ValidatorIndexInfo { pub index: BlockIndex, pub prev_hash: CryptoHash, @@ -676,11 +676,12 @@ impl ValidatorManager { #[cfg(test)] mod test { - use crate::test_utils::*; use near_primitives::hash::hash; use near_primitives::test_utils::get_key_pair_from_seed; use near_store::test_utils::create_test_store; + use crate::test_utils::*; + use super::*; fn stake(account_id: &str, amount: Balance) -> ValidatorStake { diff --git a/test-utils/loadtester/Cargo.toml b/test-utils/loadtester/Cargo.toml index 6079cd3a480..fec3315abaf 100644 --- a/test-utils/loadtester/Cargo.toml +++ b/test-utils/loadtester/Cargo.toml @@ -15,7 +15,8 @@ futures = "0.1.25" tokio = "0.1" serde_json = "1.0.0" -nbor = { path = "../../core/nbor" } +borsh = { git = "https://github.com/nearprotocol/borsh.git" } + near-primitives = { path = "../../core/primitives" } node-runtime = { path = "../../runtime/runtime" } testlib = { path = "../testlib" } diff --git a/test-utils/state-viewer/Cargo.toml b/test-utils/state-viewer/Cargo.toml index 5accd838e63..31e067f4379 100644 --- a/test-utils/state-viewer/Cargo.toml +++ b/test-utils/state-viewer/Cargo.toml @@ -8,7 +8,8 @@ edition = "2018" clap = "2.32.0" hex = "0.3" -nbor = { path = "../../core/nbor" } +borsh = { git = "https://github.com/nearprotocol/borsh.git" } + near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } node-runtime = { path = "../../runtime/runtime" } diff --git a/test-utils/testlib/Cargo.toml b/test-utils/testlib/Cargo.toml index faa5603c50e..f0ceadcb7db 100644 --- a/test-utils/testlib/Cargo.toml +++ b/test-utils/testlib/Cargo.toml @@ -22,7 +22,8 @@ byteorder = "1.2" tempdir = "0.3" tokio-signal = "0.2" -nbor = { path = "../../core/nbor" } +borsh = { git = "https://github.com/nearprotocol/borsh.git" } + near-primitives = { path = "../../core/primitives" } near-store = { path = "../../core/store" } node-runtime = { path = "../../runtime/runtime" } From ca75b513f323b937adba9e54fc9c736bdbe43dae Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Fri, 16 Aug 2019 14:54:42 -0700 Subject: [PATCH 24/25] Remove nbor --- core/nbor-derive/Cargo.toml | 14 -- core/nbor-derive/src/lib.rs | 308 ------------------------- core/nbor/Cargo.toml | 17 -- core/nbor/src/de/mod.rs | 204 ---------------- core/nbor/src/lib.rs | 7 - core/nbor/src/ser/mod.rs | 197 ---------------- core/nbor/tests/test_simple_structs.rs | 69 ------ 7 files changed, 816 deletions(-) delete mode 100644 core/nbor-derive/Cargo.toml delete mode 100644 core/nbor-derive/src/lib.rs delete mode 100644 core/nbor/Cargo.toml delete mode 100644 core/nbor/src/de/mod.rs delete mode 100644 core/nbor/src/lib.rs delete mode 100644 core/nbor/src/ser/mod.rs delete mode 100644 core/nbor/tests/test_simple_structs.rs diff --git a/core/nbor-derive/Cargo.toml b/core/nbor-derive/Cargo.toml deleted file mode 100644 index e019d76a3d7..00000000000 --- a/core/nbor-derive/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "nbor-derive" -version = "0.1.0" -authors = ["Near Inc "] -edition = "2018" - -[lib] -proc-macro = true - -[dependencies] -proc-macro2 = "0.4.30" -syn = {version = "0.15.34", features = ["full", "fold"] } -quote = "0.6.12" - diff --git a/core/nbor-derive/src/lib.rs b/core/nbor-derive/src/lib.rs deleted file mode 100644 index bd7676cf1d6..00000000000 --- a/core/nbor-derive/src/lib.rs +++ /dev/null @@ -1,308 +0,0 @@ -#![recursion_limit = "128"] -extern crate proc_macro; - -use proc_macro::TokenStream; -use proc_macro2::Span; -use quote::quote; -use syn::export::TokenStream2; -use syn::{Attribute, Fields, Ident, Index, ItemEnum, ItemStruct, ItemUnion, Meta, NestedMeta}; - -#[proc_macro_derive(nbor, attributes(nbor_skip, nbor_init))] -pub fn nbor(input: TokenStream) -> TokenStream { - if let Ok(input) = syn::parse::(input.clone()) { - let mut res = nbor_struct_ser(&input); - res.extend(nbor_struct_de(&input)); - TokenStream::from(res) - } else if let Ok(input) = syn::parse::(input.clone()) { - let mut res = nbor_enum_ser(&input); - res.extend(nbor_enum_de(&input)); - TokenStream::from(res) - } else if let Ok(input) = syn::parse::(input.clone()) { - let mut res = nbor_union_ser(&input); - res.extend(nbor_union_de(&input)); - TokenStream::from(res) - } else { - // Derive macros can only be defined on structs, enums, and unions. - unreachable!() - } -} - -// Check if the field is marked with `#[nbor(skip)]` or `#[.., nbor(.., skip, ...), ...]`. -fn contains_skip(attrs: &[Attribute]) -> bool { - for attr in attrs.iter() { - if let Ok(Meta::Word(ident)) = attr.parse_meta() { - if ident.to_string().as_str() == "nbor_skip" { - return true; - } - } - } - false -} - -// Checks if struct should be initialized with some method after the object is constructed. -// Should be marked as #[nbor_init()] -fn contains_initialize_with(attrs: &[Attribute]) -> Option { - for attr in attrs.iter() { - if let Ok(Meta::List(meta_list)) = attr.parse_meta() { - if meta_list.ident.to_string().as_str() == "nbor_init" { - assert_eq!( - meta_list.nested.len(), - 1, - "nbor_init requires exactly one initialization method." - ); - let nested_meta = meta_list.nested.iter().next().unwrap(); - if let NestedMeta::Meta(Meta::Word(ident)) = nested_meta { - return Some(ident.clone()); - } - } - } - } - None -} - -fn nbor_struct_ser(input: &ItemStruct) -> TokenStream2 { - let name = &input.ident; - let mut body = TokenStream2::new(); - match &input.fields { - Fields::Named(fields) => { - for field in &fields.named { - if contains_skip(&field.attrs) { - continue; - } - let field_name = field.ident.as_ref().unwrap(); - let delta = quote! { - nbor::Serializable::write(&self.#field_name, writer)?; - }; - body.extend(delta); - } - } - Fields::Unnamed(fields) => { - for field_idx in 0..fields.unnamed.len() { - let field_idx = Index { index: field_idx as u32, span: Span::call_site() }; - let delta = quote! { - nbor::Serializable::write(&self.#field_idx, writer)?; - }; - body.extend(delta); - } - } - Fields::Unit => {} - } - quote! { - impl nbor::ser::Serializable for #name { - fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { - #body - Ok(()) - } - } - } -} - -fn nbor_enum_ser(input: &ItemEnum) -> TokenStream2 { - let name = &input.ident; - let mut body = TokenStream2::new(); - for (variant_idx, variant) in input.variants.iter().enumerate() { - let variant_idx = variant_idx as u8; - let variant_ident = &variant.ident; - let mut variant_header = TokenStream2::new(); - let mut variant_body = TokenStream2::new(); - match &variant.fields { - Fields::Named(fields) => { - for field in &fields.named { - let field_name = field.ident.as_ref().unwrap(); - if contains_skip(&field.attrs) { - variant_header.extend(quote! { _#field_name, }); - continue; - } else { - variant_header.extend(quote! { #field_name, }); - } - variant_body.extend(quote! { - nbor::Serializable::write(#field_name, writer)?; - }) - } - variant_header = quote! { { #variant_header }}; - } - Fields::Unnamed(fields) => { - for (field_idx, field) in fields.unnamed.iter().enumerate() { - let field_idx = field_idx as u32; - if contains_skip(&field.attrs) { - let field_ident = - Ident::new(format!("_id{}", field_idx).as_str(), Span::call_site()); - variant_header.extend(quote! { #field_ident, }); - continue; - } else { - let field_ident = - Ident::new(format!("id{}", field_idx).as_str(), Span::call_site()); - variant_header.extend(quote! { #field_ident, }); - variant_body.extend(quote! { - nbor::Serializable::write(#field_ident, writer)?; - }) - } - } - variant_header = quote! { ( #variant_header )}; - } - Fields::Unit => {} - } - body.extend(quote!( - #name::#variant_ident #variant_header => { - let variant_idx: u8 = #variant_idx; - writer.write(&variant_idx.to_le_bytes())?; - #variant_body - } - )) - } - let res = quote! { - impl nbor::ser::Serializable for #name { - fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { - match self { - #body - } - Ok(()) - } - } - }; - res -} - -fn nbor_union_ser(_input: &ItemUnion) -> TokenStream2 { - unimplemented!() -} - -fn nbor_struct_de(input: &ItemStruct) -> TokenStream2 { - let name = &input.ident; - let init_method = contains_initialize_with(&input.attrs); - let return_value = match &input.fields { - Fields::Named(fields) => { - let mut body = TokenStream2::new(); - for field in &fields.named { - let field_name = field.ident.as_ref().unwrap(); - let delta = if contains_skip(&field.attrs) { - quote! { - #field_name: Default::default(), - } - } else { - quote! { - #field_name: nbor::Deserializable::read(reader)?, - } - }; - body.extend(delta); - } - quote! { - Self { #body } - } - } - Fields::Unnamed(fields) => { - let mut body = TokenStream2::new(); - for _ in 0..fields.unnamed.len() { - let delta = quote! { - nbor::Deserializable::read(reader)?, - }; - body.extend(delta); - } - quote! { - Self( #body ) - } - } - Fields::Unit => { - quote! { - Self {} - } - } - }; - if let Some(method_ident) = init_method { - quote! { - impl nbor::de::Deserializable for #name { - fn read(reader: &mut R) -> Result { - let mut return_value = #return_value; - return_value.#method_ident(); - Ok(return_value) - } - } - } - } else { - quote! { - impl nbor::de::Deserializable for #name { - fn read(reader: &mut R) -> Result { - Ok(#return_value) - } - } - } - } -} -fn nbor_enum_de(input: &ItemEnum) -> TokenStream2 { - let name = &input.ident; - let init_method = contains_initialize_with(&input.attrs); - let mut variant_arms = TokenStream2::new(); - for (variant_idx, variant) in input.variants.iter().enumerate() { - let variant_idx = variant_idx as u8; - let variant_ident = &variant.ident; - let mut variant_header = TokenStream2::new(); - match &variant.fields { - Fields::Named(fields) => { - for field in &fields.named { - let field_name = field.ident.as_ref().unwrap(); - if contains_skip(&field.attrs) { - variant_header.extend(quote! { - #field_name: Default::default(), - }); - } else { - variant_header.extend(quote! { - #field_name: nbor::Deserializable::read(reader)?, - }); - } - } - variant_header = quote! { { #variant_header }}; - } - Fields::Unnamed(fields) => { - for field in fields.unnamed.iter() { - if contains_skip(&field.attrs) { - variant_header.extend(quote! { Default::default(), }); - } else { - variant_header.extend(quote! { nbor::Deserializable::read(reader)?, }); - } - } - variant_header = quote! { ( #variant_header )}; - } - Fields::Unit => {} - } - variant_arms.extend(quote! { - #variant_idx => #name::#variant_ident #variant_header , - }); - } - let variant_idx = quote! { - let mut variant_idx = [0u8; std::mem::size_of::()]; - reader.read(&mut variant_idx)?; - let variant_idx = u8::from_le_bytes(variant_idx); - }; - if let Some(method_ident) = init_method { - quote! { - impl nbor::de::Deserializable for #name { - fn read(reader: &mut R) -> Result { - #variant_idx - let mut return_value = match variant_idx { - #variant_arms - _ => panic!(format!("Unexpeted variant index: {:?}", variant_idx)), - }; - return_value.#method_ident(); - Ok(return_value) - } - } - } - } else { - quote! { - impl nbor::de::Deserializable for #name { - fn read(reader: &mut R) -> Result { - #variant_idx - let return_value = match variant_idx { - #variant_arms - _ => panic!(format!("Unexpeted variant index: {:?}", variant_idx)), - }; - Ok(return_value) - } - } - } - } -} - -fn nbor_union_de(_input: &ItemUnion) -> TokenStream2 { - unimplemented!() -} diff --git a/core/nbor/Cargo.toml b/core/nbor/Cargo.toml deleted file mode 100644 index 457e1c531ea..00000000000 --- a/core/nbor/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "nbor" -version = "0.1.0" -authors = ["Near Inc "] -edition = "2018" -description = """Near Binary Object Representation -""" - -[dependencies] -nbor-derive = { path = "../nbor-derive" } - -[features] -default = ["std"] - -# Provides impls for common standard library types like Vec. -# Requires a dependency on the Rust standard library. -std = [] diff --git a/core/nbor/src/de/mod.rs b/core/nbor/src/de/mod.rs deleted file mode 100644 index 01cf3a6e83e..00000000000 --- a/core/nbor/src/de/mod.rs +++ /dev/null @@ -1,204 +0,0 @@ -use std::collections::{BTreeMap, HashMap, HashSet}; -use std::io::{Cursor, Error, Read}; -use std::mem::size_of; - -/// A data-structure that can be de-serialized from binary format by NBOR. -pub trait Deserializable: Sized { - fn read(reader: &mut R) -> Result; - - /// Deserialize this instance from a slice of bytes. - fn try_from_slice(v: &[u8]) -> Result { - let mut c = Cursor::new(v); - Self::read(&mut c) - } -} - -macro_rules! impl_for_integer { - ($type: ident) => { - impl Deserializable for $type { - fn read(reader: &mut R) -> Result { - let mut data = [0u8; size_of::<$type>()]; - reader.read_exact(&mut data)?; - Ok($type::from_le_bytes(data)) - } - } - }; -} - -impl_for_integer!(i8); -impl_for_integer!(i16); -impl_for_integer!(i32); -impl_for_integer!(i64); -impl_for_integer!(i128); -impl_for_integer!(isize); -impl_for_integer!(u8); -impl_for_integer!(u16); -impl_for_integer!(u32); -impl_for_integer!(u64); -impl_for_integer!(u128); -impl_for_integer!(usize); - -// Note NaNs have a portability issue. Specifically, signalling NaNs on MIPS are quiet NaNs on x86, -// and vice-versa. We disallow NaNs to avoid this issue. -macro_rules! impl_for_float { - ($type: ident, $int_type: ident) => { - impl Deserializable for $type { - fn read(reader: &mut R) -> Result { - let mut data = [0u8; size_of::<$type>()]; - reader.read_exact(&mut data)?; - let res = $type::from_bits($int_type::from_le_bytes(data)); - assert!( - !res.is_nan(), - "For portability reasons we do not allow to deserialize NaNs." - ); - Ok(res) - } - } - }; -} - -impl_for_float!(f32, u32); -impl_for_float!(f64, u64); - -impl Deserializable for bool { - fn read(reader: &mut R) -> Result { - let mut buf = [0u8]; - reader.read(&mut buf)?; - Ok(buf[0] == 1) - } -} - -impl Deserializable for Option -where - T: Deserializable, -{ - fn read(reader: &mut R) -> Result { - let mut flag = [0u8]; - reader.read_exact(&mut flag)?; - if flag[0] == 0 { - Ok(None) - } else { - Ok(Some(T::read(reader)?)) - } - } -} - -impl Deserializable for String { - fn read(reader: &mut R) -> Result { - let len = u32::read(reader)?; - let mut result = vec![0; len as usize]; - reader.read(&mut result)?; - String::from_utf8(result) - .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string())) - } -} - -#[cfg(feature = "std")] -impl Deserializable for Vec -where - T: Deserializable, -{ - fn read(reader: &mut R) -> Result { - let len = u32::read(reader)?; - let mut result = Vec::with_capacity(len as usize); - for _ in 0..len { - result.push(T::read(reader)?); - } - Ok(result) - } -} - -#[cfg(feature = "std")] -impl Deserializable for HashSet -where - T: Deserializable + Eq + std::hash::Hash, -{ - fn read(reader: &mut R) -> Result { - let vec = >::read(reader)?; - Ok(vec.into_iter().collect::>()) - } -} - -#[cfg(feature = "std")] -impl Deserializable for HashMap -where - K: Deserializable + Eq + std::hash::Hash, - V: Deserializable, -{ - fn read(reader: &mut R) -> Result { - let len = u32::read(reader)?; - let mut result = HashMap::with_capacity(len as usize); - for _ in 0..len { - let key = K::read(reader)?; - let value = V::read(reader)?; - result.insert(key, value); - } - Ok(result) - } -} - -#[cfg(feature = "std")] -impl Deserializable for BTreeMap -where - K: Deserializable + Ord + std::hash::Hash, - V: Deserializable, -{ - fn read(reader: &mut R) -> Result { - let len = u32::read(reader)?; - let mut result = BTreeMap::new(); - for _ in 0..len { - let key = K::read(reader)?; - let value = V::read(reader)?; - result.insert(key, value); - } - Ok(result) - } -} - -#[cfg(feature = "std")] -impl Deserializable for std::net::SocketAddr { - fn read(reader: &mut R) -> Result { - let kind = u8::read(reader)?; - match kind { - 0 => std::net::SocketAddrV4::read(reader).map(|addr| std::net::SocketAddr::V4(addr)), - 1 => std::net::SocketAddrV6::read(reader).map(|addr| std::net::SocketAddr::V6(addr)), - value => panic!(format!("Invalid SocketAddr variant: {}", value)), - } - } -} - -#[cfg(feature = "std")] -impl Deserializable for std::net::SocketAddrV4 { - fn read(reader: &mut R) -> Result { - let ip = std::net::Ipv4Addr::read(reader)?; - let port = u16::read(reader)?; - Ok(std::net::SocketAddrV4::new(ip, port)) - } -} - -#[cfg(feature = "std")] -impl Deserializable for std::net::SocketAddrV6 { - fn read(reader: &mut R) -> Result { - let ip = std::net::Ipv6Addr::read(reader)?; - let port = u16::read(reader)?; - Ok(std::net::SocketAddrV6::new(ip, port, 0, 0)) - } -} - -#[cfg(feature = "std")] -impl Deserializable for std::net::Ipv4Addr { - fn read(reader: &mut R) -> Result { - let mut buf = [0u8; 4]; - reader.read(&mut buf)?; - Ok(std::net::Ipv4Addr::from(buf)) - } -} - -#[cfg(feature = "std")] -impl Deserializable for std::net::Ipv6Addr { - fn read(reader: &mut R) -> Result { - let mut buf = [0u8; 16]; - reader.read(&mut buf)?; - Ok(std::net::Ipv6Addr::from(buf)) - } -} diff --git a/core/nbor/src/lib.rs b/core/nbor/src/lib.rs deleted file mode 100644 index 2cbf47de7b1..00000000000 --- a/core/nbor/src/lib.rs +++ /dev/null @@ -1,7 +0,0 @@ -pub use nbor_derive::nbor; - -pub mod de; -pub mod ser; - -pub use de::Deserializable; -pub use ser::Serializable; diff --git a/core/nbor/src/ser/mod.rs b/core/nbor/src/ser/mod.rs deleted file mode 100644 index 932d627bc42..00000000000 --- a/core/nbor/src/ser/mod.rs +++ /dev/null @@ -1,197 +0,0 @@ -use std::collections::{BTreeMap, HashMap, HashSet}; -use std::io::{Error, Write}; - -const DEFAULT_SERIALIZER_CAPACITY: usize = 1024; - -/// A data-structure that can be serialized into binary format by NBOR. -pub trait Serializable { - fn write(&self, writer: &mut W) -> Result<(), Error>; - - /// Serialize this instance into a vector of bytes. - fn try_to_vec(&self) -> Result, Error> { - let mut result = Vec::with_capacity(DEFAULT_SERIALIZER_CAPACITY); - self.write(&mut result)?; - Ok(result) - } -} - -macro_rules! impl_for_integer { - ($type: ident) => { - impl Serializable for $type { - fn write(&self, writer: &mut W) -> Result<(), Error> { - writer.write(&self.to_le_bytes()).map(|_| ()) - } - } - }; -} - -impl_for_integer!(i8); -impl_for_integer!(i16); -impl_for_integer!(i32); -impl_for_integer!(i64); -impl_for_integer!(i128); -impl_for_integer!(isize); -impl_for_integer!(u8); -impl_for_integer!(u16); -impl_for_integer!(u32); -impl_for_integer!(u64); -impl_for_integer!(u128); -impl_for_integer!(usize); - -// Note NaNs have a portability issue. Specifically, signalling NaNs on MIPS are quiet NaNs on x86, -// and vice-versa. We disallow NaNs to avoid this issue. -macro_rules! impl_for_float { - ($type: ident) => { - impl Serializable for $type { - fn write(&self, writer: &mut W) -> Result<(), Error> { - assert!( - !self.is_nan(), - "For portability reasons we do not allow to serialize NaNs." - ); - writer.write(&self.to_bits().to_le_bytes()).map(|_| ()) - } - } - }; -} - -impl_for_float!(f32); -impl_for_float!(f64); - -impl Serializable for bool { - fn write(&self, writer: &mut W) -> Result<(), Error> { - writer.write(if *self { &[0u8] } else { &[1u8] }).map(|_| ()) - } -} - -impl Serializable for Option -where - T: Serializable, -{ - fn write(&self, writer: &mut W) -> Result<(), Error> { - match self { - None => 0u8.write(writer).map(|_| ()), - Some(value) => { - 1u8.write(writer)?; - value.write(writer) - } - } - } -} - -impl Serializable for String { - fn write(&self, writer: &mut W) -> Result<(), Error> { - (self.len() as u32).write(writer)?; - writer.write(self.as_bytes())?; - Ok(()) - } -} - -#[cfg(feature = "std")] -impl Serializable for Vec -where - T: Serializable, -{ - fn write(&self, writer: &mut W) -> Result<(), Error> { - (self.len() as u32).write(writer)?; - for item in self { - item.write(writer)?; - } - Ok(()) - } -} - -#[cfg(feature = "std")] -impl Serializable for HashSet -where - T: Serializable + PartialOrd, -{ - fn write(&self, writer: &mut W) -> Result<(), Error> { - let mut vec = self.iter().collect::>(); - vec.sort_by(|a, b| a.partial_cmp(b).unwrap()); - (vec.len() as u32).write(writer)?; - for item in vec { - item.write(writer)?; - } - Ok(()) - } -} - -#[cfg(feature = "std")] -impl Serializable for HashMap -where - K: Serializable + PartialOrd, - V: Serializable, -{ - fn write(&self, writer: &mut W) -> Result<(), Error> { - let mut vec = self.iter().collect::>(); - vec.sort_by(|(a, _), (b, _)| a.partial_cmp(b).unwrap()); - (vec.len() as u32).write(writer)?; - for (key, value) in vec { - key.write(writer)?; - value.write(writer)?; - } - Ok(()) - } -} - -#[cfg(feature = "std")] -impl Serializable for BTreeMap -where - K: Serializable + PartialOrd, - V: Serializable, -{ - fn write(&self, writer: &mut W) -> Result<(), Error> { - (self.len() as u32).write(writer)?; - for (key, value) in self.iter() { - key.write(writer)?; - value.write(writer)?; - } - Ok(()) - } -} - -#[cfg(feature = "std")] -impl Serializable for std::net::SocketAddr { - fn write(&self, writer: &mut W) -> Result<(), Error> { - match *self { - std::net::SocketAddr::V4(ref addr) => { - 0u8.write(writer)?; - addr.write(writer) - } - std::net::SocketAddr::V6(ref addr) => { - 1u8.write(writer)?; - addr.write(writer) - } - } - } -} - -#[cfg(feature = "std")] -impl Serializable for std::net::SocketAddrV4 { - fn write(&self, writer: &mut W) -> Result<(), Error> { - self.ip().write(writer)?; - self.port().write(writer).map(|_| ()) - } -} - -#[cfg(feature = "std")] -impl Serializable for std::net::SocketAddrV6 { - fn write(&self, writer: &mut W) -> Result<(), Error> { - self.ip().write(writer)?; - self.port().write(writer).map(|_| ()) - } -} - -#[cfg(feature = "std")] -impl Serializable for std::net::Ipv4Addr { - fn write(&self, writer: &mut W) -> Result<(), Error> { - writer.write(&self.octets()).map(|_| ()) - } -} - -#[cfg(feature = "std")] -impl Serializable for std::net::Ipv6Addr { - fn write(&self, writer: &mut W) -> Result<(), Error> { - writer.write(&self.octets()).map(|_| ()) - } -} diff --git a/core/nbor/tests/test_simple_structs.rs b/core/nbor/tests/test_simple_structs.rs deleted file mode 100644 index c5c8530ae81..00000000000 --- a/core/nbor/tests/test_simple_structs.rs +++ /dev/null @@ -1,69 +0,0 @@ -use nbor::{nbor, Deserializable, Serializable}; - -#[derive(nbor, PartialEq, Debug)] -#[nbor_init(init)] -struct A { - x: u64, - b: B, - y: f32, - z: String, - v: Vec, - lazy: Option, - #[nbor_skip] - skipped: Option, -} - -impl A { - pub fn init(&mut self) { - if let Some(v) = self.lazy.as_mut() { - *v *= 10; - } - } -} - -#[derive(nbor, PartialEq, Debug)] -struct B { - x: u64, - y: i32, - c: C, -} - -#[derive(nbor, PartialEq, Debug)] -enum C { - C1, - C2(u64), - C3(u64, u64), - C4 { x: u64, y: u64 }, - C5(D), -} - -#[derive(nbor, PartialEq, Debug)] -struct D { - x: u64, -} - -#[test] -fn test_simple_struct() { - let a = A { - x: 1, - b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, - y: 4.0, - z: "123".to_string(), - v: vec!["qwe".to_string(), "zxc".to_string()], - lazy: Some(5), - skipped: Some(6), - }; - let encoded_a = a.try_to_vec().unwrap(); - let decoded_a = A::try_from_slice(&encoded_a).unwrap(); - let expected_a = A { - x: 1, - b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) }, - y: 4.0, - z: a.z, - v: a.v, - lazy: Some(50), - skipped: None, - }; - - assert_eq!(expected_a, decoded_a); -} From 7941aefb0638759d05d8ad6661fb77420aaf3243 Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Fri, 16 Aug 2019 14:56:39 -0700 Subject: [PATCH 25/25] Replace the rest of nbor usage --- Cargo.lock | 16 ---------------- Cargo.toml | 2 -- chain/jsonrpc/src/lib.rs | 2 +- chain/jsonrpc/tests/rpc_transactions.rs | 2 +- chain/network/src/codec.rs | 2 +- chain/network/src/types.rs | 4 ++-- core/primitives/benches/serialization.rs | 2 +- core/store/src/lib.rs | 2 +- test-utils/loadtester/src/remote_node.rs | 2 +- test-utils/state-viewer/src/main.rs | 3 +-- test-utils/testlib/src/user/rpc_user.rs | 2 +- 11 files changed, 10 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3d7ce9d254b..0a1fef8a4bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2044,22 +2044,6 @@ dependencies = [ "tempfile 3.0.8 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "nbor" -version = "0.1.0" -dependencies = [ - "nbor-derive 0.1.0", -] - -[[package]] -name = "nbor-derive" -version = "0.1.0" -dependencies = [ - "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.36 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "near" version = "0.2.7" diff --git a/Cargo.toml b/Cargo.toml index 6d19e3b8897..de10be89db1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,6 @@ edition = "2018" [workspace] members = [ "async-utils/", - "core/nbor", - "core/nbor-derive", "core/primitives", "core/store", "runtime/runtime", diff --git a/chain/jsonrpc/src/lib.rs b/chain/jsonrpc/src/lib.rs index 6e4e007d684..4a76e58847f 100644 --- a/chain/jsonrpc/src/lib.rs +++ b/chain/jsonrpc/src/lib.rs @@ -6,6 +6,7 @@ use std::time::Duration; use actix::{Addr, MailboxError}; use actix_cors::Cors; use actix_web::{App, Error as HttpError, http, HttpResponse, HttpServer, middleware, web}; +use borsh::Deserializable; use futures03::{compat::Future01CompatExt as _, FutureExt as _, TryFutureExt as _}; use futures::future::Future; use serde::de::DeserializeOwned; @@ -15,7 +16,6 @@ use serde_json::Value; use async_utils::{delay, timeout}; use message::{Request, RpcError}; use message::Message; -use nbor::Deserializable; use near_client::{ClientActor, GetBlock, Query, Status, TxDetails, TxStatus, ViewClientActor}; pub use near_jsonrpc_client as client; use near_jsonrpc_client::message as message; diff --git a/chain/jsonrpc/tests/rpc_transactions.rs b/chain/jsonrpc/tests/rpc_transactions.rs index 6219a9ce753..1c1b13121e3 100644 --- a/chain/jsonrpc/tests/rpc_transactions.rs +++ b/chain/jsonrpc/tests/rpc_transactions.rs @@ -1,9 +1,9 @@ use std::sync::Arc; use actix::{Actor, System}; +use borsh::Serializable; use futures::future::Future; -use nbor::Serializable; use near_jsonrpc::client::new_client; use near_jsonrpc::test_utils::start_all; use near_network::test_utils::{wait_or_panic, WaitOrTimeout}; diff --git a/chain/network/src/codec.rs b/chain/network/src/codec.rs index 46256825f12..eca0d1f8f6d 100644 --- a/chain/network/src/codec.rs +++ b/chain/network/src/codec.rs @@ -3,7 +3,7 @@ use std::io::{Error, ErrorKind}; use bytes::{BufMut, BytesMut}; use tokio::codec::{Decoder, Encoder}; -use nbor::{Deserializable, Serializable}; +use borsh::{Deserializable, Serializable}; use crate::types::PeerMessage; diff --git a/chain/network/src/types.rs b/chain/network/src/types.rs index 0259079540f..bc4e82e6272 100644 --- a/chain/network/src/types.rs +++ b/chain/network/src/types.rs @@ -8,10 +8,10 @@ use std::time::Duration; use actix::dev::{MessageResponse, ResponseChannel}; use actix::{Actor, Addr, Message}; +use borsh::{BorshDeserialize, BorshSerialize, Deserializable}; use chrono::{DateTime, Utc}; use tokio::net::TcpStream; -use nbor::{nbor, Deserializable}; use near_chain::{Block, BlockApproval, BlockHeader, Weight}; use near_primitives::crypto::signature::{sign, PublicKey, SecretKey, Signature}; use near_primitives::hash::{hash, CryptoHash}; @@ -20,9 +20,9 @@ use near_primitives::receipt::Receipt; use near_primitives::serialize::BaseEncode; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, BlockIndex, ShardId}; +use near_primitives::utils::{from_timestamp, to_timestamp}; use crate::peer::Peer; -use near_primitives::utils::{from_timestamp, to_timestamp}; /// Current latest version of the protocol pub const PROTOCOL_VERSION: u32 = 2; diff --git a/core/primitives/benches/serialization.rs b/core/primitives/benches/serialization.rs index 5b9a90210b7..eca2e58e943 100644 --- a/core/primitives/benches/serialization.rs +++ b/core/primitives/benches/serialization.rs @@ -5,9 +5,9 @@ use std::collections::HashMap; use std::sync::Arc; use bencher::Bencher; +use borsh::{Deserializable, Serializable}; use chrono::Utc; -use nbor::{Deserializable, Serializable}; use near_primitives::account::Account; use near_primitives::block::Block; use near_primitives::crypto::signature::{PublicKey, DEFAULT_SIGNATURE}; diff --git a/core/store/src/lib.rs b/core/store/src/lib.rs index 26bf0a928cd..947c902ffcf 100644 --- a/core/store/src/lib.rs +++ b/core/store/src/lib.rs @@ -1,12 +1,12 @@ use std::sync::Arc; use std::{fmt, io}; +use borsh::{Deserializable, Serializable}; use cached::{Cached, SizedCache}; pub use kvdb::DBValue; use kvdb::{DBOp, DBTransaction, KeyValueDB}; use kvdb_rocksdb::{Database, DatabaseConfig}; -use nbor::{Deserializable, Serializable}; use near_primitives::account::{AccessKey, Account}; use near_primitives::contract::ContractCode; use near_primitives::crypto::signature::PublicKey; diff --git a/test-utils/loadtester/src/remote_node.rs b/test-utils/loadtester/src/remote_node.rs index ae460cd36cd..bc1a4d7ca97 100644 --- a/test-utils/loadtester/src/remote_node.rs +++ b/test-utils/loadtester/src/remote_node.rs @@ -3,11 +3,11 @@ use std::sync::{Arc, RwLock}; use std::thread; use std::time::{Duration, Instant}; +use borsh::Serializable; use futures::Future; use reqwest::r#async::Client as AsyncClient; use reqwest::Client as SyncClient; -use nbor::Serializable; use near_primitives::crypto::signature::PublicKey; use near_primitives::crypto::signer::InMemorySigner; use near_primitives::rpc::AccessKeyView; diff --git a/test-utils/state-viewer/src/main.rs b/test-utils/state-viewer/src/main.rs index 117f643844e..cd4f137f256 100644 --- a/test-utils/state-viewer/src/main.rs +++ b/test-utils/state-viewer/src/main.rs @@ -2,10 +2,9 @@ use std::convert::TryFrom; use std::path::Path; use std::sync::Arc; +use borsh::Deserializable; use clap::{App, Arg, SubCommand}; -use nbor::Deserializable; - use near::{get_default_home, get_store_path, load_config, NearConfig, NightshadeRuntime}; use near_chain::{ChainStore, ChainStoreAccess}; use near_network::peer_store::PeerStore; diff --git a/test-utils/testlib/src/user/rpc_user.rs b/test-utils/testlib/src/user/rpc_user.rs index 443cd30984b..26a345abf73 100644 --- a/test-utils/testlib/src/user/rpc_user.rs +++ b/test-utils/testlib/src/user/rpc_user.rs @@ -2,8 +2,8 @@ use std::convert::TryInto; use std::sync::{Arc, RwLock}; use actix::System; +use borsh::Serializable; -use nbor::Serializable; use near_client::StatusResponse; use near_jsonrpc::client::{new_client, JsonRpcClient}; use near_primitives::crypto::signature::PublicKey;