Skip to content

Commit

Permalink
Implement StateRecord for Action Receipts and Received Data (#1163)
Browse files Browse the repository at this point in the history
* Implement StateRecord for Action Receipts and Received Data
* Rename rpc.rs -> views.rs
  • Loading branch information
Evgeny Kuzyakov authored Aug 19, 2019
1 parent e044878 commit 2edba26
Show file tree
Hide file tree
Showing 35 changed files with 385 additions and 206 deletions.
2 changes: 1 addition & 1 deletion chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ 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::QueryResponse;
use near_primitives::test_utils::get_public_key_from_seed;
use near_primitives::transaction::{
SignedTransaction, TransactionLog, TransactionResult, TransactionStatus,
};
use near_primitives::types::{AccountId, BlockIndex, MerkleHash, ShardId, ValidatorStake};
use near_primitives::views::QueryResponse;
use near_store::test_utils::create_test_store;
use near_store::{PartialStorage, Store, StoreUpdate, Trie, TrieChanges, WrappedTrieChanges};

Expand Down
2 changes: 1 addition & 1 deletion chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use near_primitives::crypto::signature::Signature;
use near_primitives::crypto::signer::EDSigner;
use near_primitives::hash::CryptoHash;
use near_primitives::receipt::Receipt;
use near_primitives::rpc::QueryResponse;
use near_primitives::transaction::{SignedTransaction, TransactionLog};
use near_primitives::types::{AccountId, BlockIndex, MerkleHash, ShardId, ValidatorStake};
use near_primitives::views::QueryResponse;
use near_store::{PartialStorage, StoreUpdate, WrappedTrieChanges};

use crate::error::Error;
Expand Down
2 changes: 1 addition & 1 deletion chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ use near_pool::TransactionPool;
use near_primitives::crypto::signature::{verify, Signature};
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::receipt::Receipt;
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_primitives::views::ValidatorInfo;
use near_store::Store;
use near_telemetry::TelemetryActor;

Expand Down
6 changes: 3 additions & 3 deletions chain/client/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use chrono::{DateTime, Utc};

use near_primitives::crypto::signer::{AccountSigner, EDSigner, InMemorySigner};
use near_primitives::hash::CryptoHash;
use near_primitives::rpc::{
use near_primitives::types::{AccountId, BlockIndex, ShardId, Version};
use near_primitives::views::{
BlockView, FinalTransactionResult, QueryResponse, TransactionResultView,
};
pub use near_primitives::rpc::{StatusResponse, StatusSyncInfo};
use near_primitives::types::{AccountId, BlockIndex, ShardId, Version};
pub use near_primitives::views::{StatusResponse, StatusSyncInfo};

/// Combines errors coming from chain, tx pool and block producer.
#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions chain/client/src/view_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use chrono::{DateTime, Utc};

use near_chain::{Chain, ErrorKind, RuntimeAdapter};
use near_primitives::hash::CryptoHash;
use near_primitives::rpc::{
use near_primitives::transaction::{TransactionResult, TransactionStatus};
use near_primitives::views::{
BlockView, FinalTransactionResult, FinalTransactionStatus, QueryResponse, TransactionLogView,
TransactionResultView,
};
use near_primitives::transaction::{TransactionResult, TransactionStatus};
use near_store::Store;

use crate::types::{Error, GetBlock, Query, TxStatus};
Expand Down
2 changes: 1 addition & 1 deletion chain/client/tests/query_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use futures::future::Future;

use near_client::test_utils::setup_no_network;
use near_client::Query;
use near_primitives::rpc::QueryResponse;
use near_primitives::test_utils::init_test_logger;
use near_primitives::views::QueryResponse;

/// Query account from view client
#[test]
Expand Down
4 changes: 2 additions & 2 deletions chain/jsonrpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use actix_web::client::Client;
use futures::Future;
use serde::Serialize;

use near_primitives::rpc::{
use near_primitives::types::BlockIndex;
use near_primitives::views::{
BlockView, FinalTransactionResult, QueryResponse, StatusResponse, TransactionResultView,
};
use near_primitives::types::BlockIndex;

pub mod message;
use crate::message::{from_slice, Message};
Expand Down
2 changes: 1 addition & 1 deletion chain/jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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::views::FinalTransactionStatus;
use near_primitives::serialize::{BaseEncode, from_base, from_base64};
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::BlockIndex;
Expand Down
2 changes: 1 addition & 1 deletion chain/jsonrpc/tests/rpc_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +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::SignedTransaction;
use near_primitives::views::FinalTransactionStatus;

/// Test sending transaction via json rpc without waiting.
#[test]
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/crypto/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ 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};
use crate::views::{PublicKeyView, SecretKeyView};

/// Trait to abstract the signer account.
pub trait AccountSigner: Sync + Send {
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn hash_struct<T: Encode>(obj: &T) -> CryptoHash {
#[cfg(test)]
mod tests {
use super::*;
use crate::rpc::CryptoHashView;
use crate::views::CryptoHashView;

#[derive(Deserialize, Serialize)]
struct Struct {
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ pub mod hash;
pub mod logging;
pub mod merkle;
pub mod receipt;
pub mod rpc;
pub mod serialize;
pub mod sharding;
pub mod test_utils;
pub mod transaction;
pub mod types;
pub mod utils;
pub mod views;
13 changes: 13 additions & 0 deletions core/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,16 @@ impl fmt::Debug for DataReceipt {
.finish()
}
}

#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone)]
pub struct ReceivedData {
pub data: Option<Vec<u8>>,
}

impl fmt::Debug for ReceivedData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ReceivedData")
.field("data", &format_args!("{}", logging::pretty_result(&self.data)))
.finish()
}
}
14 changes: 7 additions & 7 deletions core/primitives/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,18 @@ pub mod vec_base_format {
}
}

pub mod option_bytes_format {
pub mod option_base64_format {
use serde::de;
use serde::{Deserialize, Deserializer, Serializer};

use super::{from_base, to_base};
use super::{from_base64, to_base64};

pub fn serialize<S>(data: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if let Some(ref bytes) = data {
serializer.serialize_str(&to_base(bytes))
serializer.serialize_str(&to_base64(bytes))
} else {
serializer.serialize_none()
}
Expand All @@ -180,7 +180,7 @@ pub mod option_bytes_format {
{
let s: Option<String> = Option::deserialize(deserializer)?;
if let Some(s) = s {
Ok(Some(from_base(&s).map_err(|err| de::Error::custom(err.to_string()))?))
Ok(Some(from_base64(&s).map_err(|err| de::Error::custom(err.to_string()))?))
} else {
Ok(None)
}
Expand Down Expand Up @@ -263,20 +263,20 @@ mod tests {

#[derive(Deserialize, Serialize)]
struct OptionBytesStruct {
#[serde(with = "option_bytes_format")]
#[serde(with = "option_base64_format")]
data: Option<Vec<u8>>,
}

#[test]
fn test_serialize_some() {
let s = OptionBytesStruct { data: Some(vec![10, 20, 30]) };
let encoded = serde_json::to_string(&s).unwrap();
assert_eq!(encoded, "{\"data\":\"4PM7\"}");
assert_eq!(encoded, "{\"data\":\"ChQe\"}");
}

#[test]
fn test_deserialize_some() {
let encoded = "{\"data\":\"4PM7\"}";
let encoded = "{\"data\":\"ChQe\"}";
let decoded: OptionBytesStruct = serde_json::from_str(&encoded).unwrap();
assert_eq!(decoded.data, Some(vec![10, 20, 30]));
}
Expand Down
13 changes: 6 additions & 7 deletions core/primitives/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ const NS_IN_SECOND: u64 = 1_000_000_000;

pub mod col {
pub const ACCOUNT: &[u8] = &[0];
pub const CALLBACK: &[u8] = &[1];
pub const CODE: &[u8] = &[2];
pub const ACCESS_KEY: &[u8] = &[3];
pub const RECEIVED_DATA: &[u8] = &[4];
pub const POSTPONED_RECEIPT_ID: &[u8] = &[5];
pub const PENDING_DATA_COUNT: &[u8] = &[6];
pub const POSTPONED_RECEIPT: &[u8] = &[7];
pub const CODE: &[u8] = &[1];
pub const ACCESS_KEY: &[u8] = &[2];
pub const RECEIVED_DATA: &[u8] = &[3];
pub const POSTPONED_RECEIPT_ID: &[u8] = &[4];
pub const PENDING_DATA_COUNT: &[u8] = &[5];
pub const POSTPONED_RECEIPT: &[u8] = &[6];
}

fn key_for_column_account_id(column: &[u8], account_key: &AccountId) -> Vec<u8> {
Expand Down
Loading

0 comments on commit 2edba26

Please sign in to comment.