Skip to content

Commit

Permalink
WIP. Rename ReceiptTransaction to Receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
evgenykuzyakov committed Aug 7, 2019
1 parent ab5babe commit 6f46173
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 209 deletions.
7 changes: 4 additions & 3 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use chrono::Duration;
use log::{debug, info};

use near_primitives::hash::CryptoHash;
use near_primitives::transaction::{ReceiptTransaction, TransactionResult};
use near_primitives::receipt::Receipt;
use near_primitives::transaction::TransactionResult;
use near_primitives::types::{BlockIndex, MerkleHash, ShardId};
use near_store::Store;

Expand Down Expand Up @@ -426,7 +427,7 @@ impl Chain {
shard_id: ShardId,
hash: CryptoHash,
payload: Vec<u8>,
receipts: Vec<ReceiptTransaction>,
receipts: Vec<Receipt>,
) -> Result<(), Error> {
// TODO(1046): update this with any required changes for chunks support.
let header = self.get_block_header(&hash)?;
Expand Down Expand Up @@ -519,7 +520,7 @@ impl Chain {

/// Get receipts stored for the given hash.
#[inline]
pub fn get_receipts(&mut self, hash: &CryptoHash) -> Result<&Vec<ReceiptTransaction>, Error> {
pub fn get_receipts(&mut self, hash: &CryptoHash) -> Result<&Vec<Receipt>, Error> {
self.store.get_receipts(hash)
}

Expand Down
15 changes: 8 additions & 7 deletions chain/chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use cached::SizedCache;
use log::debug;

use near_primitives::hash::CryptoHash;
use near_primitives::transaction::{ReceiptTransaction, TransactionResult};
use near_primitives::receipt::Receipt;
use near_primitives::transaction::TransactionResult;
use near_primitives::types::{BlockIndex, MerkleHash};
use near_primitives::utils::index_to_bytes;
use near_store::{
Expand Down Expand Up @@ -52,7 +53,7 @@ pub trait ChainStoreAccess {
/// Returns hash of the block on the main chain for given height.
fn get_block_hash_by_height(&mut self, height: BlockIndex) -> Result<CryptoHash, Error>;
/// Returns resulting receipt for given block.
fn get_receipts(&mut self, hash: &CryptoHash) -> Result<&Vec<ReceiptTransaction>, Error>;
fn get_receipts(&mut self, hash: &CryptoHash) -> Result<&Vec<Receipt>, Error>;
/// Returns transaction result for given tx hash.
fn get_transaction_result(&mut self, hash: &CryptoHash) -> Result<&TransactionResult, Error>;
}
Expand All @@ -69,7 +70,7 @@ pub struct ChainStore {
// Cache with index to hash on the main chain.
// block_index: SizedCache<Vec<u8>, CryptoHash>,
/// Cache with receipts.
receipts: SizedCache<Vec<u8>, Vec<ReceiptTransaction>>,
receipts: SizedCache<Vec<u8>, Vec<Receipt>>,
/// Cache transaction statuses.
transaction_results: SizedCache<Vec<u8>, TransactionResult>,
}
Expand Down Expand Up @@ -181,7 +182,7 @@ impl ChainStoreAccess for ChainStore {
// )
}

fn get_receipts(&mut self, hash: &CryptoHash) -> Result<&Vec<ReceiptTransaction>, Error> {
fn get_receipts(&mut self, hash: &CryptoHash) -> Result<&Vec<Receipt>, Error> {
option_to_not_found(
read_with_cache(&*self.store, COL_RECEIPTS, &mut self.receipts, hash.as_ref()),
&format!("RECEIPT: {}", hash),
Expand Down Expand Up @@ -212,7 +213,7 @@ pub struct ChainStoreUpdate<'a, T> {
headers: HashMap<CryptoHash, BlockHeader>,
post_state_roots: HashMap<CryptoHash, MerkleHash>,
block_index: HashMap<BlockIndex, Option<CryptoHash>>,
receipts: HashMap<CryptoHash, Vec<ReceiptTransaction>>,
receipts: HashMap<CryptoHash, Vec<Receipt>>,
transaction_results: HashMap<CryptoHash, TransactionResult>,
head: Option<Tip>,
tail: Option<Tip>,
Expand Down Expand Up @@ -330,7 +331,7 @@ impl<'a, T: ChainStoreAccess> ChainStoreAccess for ChainStoreUpdate<'a, T> {
}

/// Get receipts produced for block with givien hash.
fn get_receipts(&mut self, hash: &CryptoHash) -> Result<&Vec<ReceiptTransaction>, Error> {
fn get_receipts(&mut self, hash: &CryptoHash) -> Result<&Vec<Receipt>, Error> {
if let Some(receipts) = self.receipts.get(hash) {
Ok(receipts)
} else {
Expand Down Expand Up @@ -418,7 +419,7 @@ impl<'a, T: ChainStoreAccess> ChainStoreUpdate<'a, T> {
self.headers.insert(header.hash(), header);
}

pub fn save_receipt(&mut self, hash: &CryptoHash, receipt: Vec<ReceiptTransaction>) {
pub fn save_receipt(&mut self, hash: &CryptoHash, receipt: Vec<Receipt>) {
self.receipts.insert(*hash, receipt);
}

Expand Down
7 changes: 3 additions & 4 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use chrono::Utc;
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::test_utils::get_public_key_from_seed;
use near_primitives::transaction::{
ReceiptTransaction, SignedTransaction, TransactionResult, TransactionStatus,
};
use near_primitives::transaction::{SignedTransaction, TransactionResult, TransactionStatus};
use near_primitives::types::{AccountId, BlockIndex, MerkleHash, ShardId, ValidatorStake};
use near_store::test_utils::create_test_store;
use near_store::{Store, StoreUpdate, Trie, TrieChanges, WrappedTrieChanges};
Expand Down Expand Up @@ -158,7 +157,7 @@ impl RuntimeAdapter for KeyValueRuntime {
_block_index: BlockIndex,
_prev_block_hash: &CryptoHash,
_block_hash: &CryptoHash,
_receipts: &Vec<Vec<ReceiptTransaction>>,
_receipts: &Vec<Vec<Receipt>>,
transactions: &Vec<SignedTransaction>,
) -> Result<
(
Expand Down
7 changes: 4 additions & 3 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ pub use near_primitives::block::{Block, BlockHeader, Weight};
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::{ReceiptTransaction, SignedTransaction, TransactionResult};
use near_primitives::transaction::{SignedTransaction, TransactionResult};
use near_primitives::types::{AccountId, BlockIndex, MerkleHash, ShardId, ValidatorStake};
use near_store::{StoreUpdate, WrappedTrieChanges};

Expand Down Expand Up @@ -39,7 +40,7 @@ pub struct ValidTransaction {
}

/// Map of shard to list of receipts to send to it.
pub type ReceiptResult = HashMap<ShardId, Vec<ReceiptTransaction>>;
pub type ReceiptResult = HashMap<ShardId, Vec<Receipt>>;

/// Bridge between the chain and the runtime.
/// Main function is to update state given transactions.
Expand Down Expand Up @@ -129,7 +130,7 @@ pub trait RuntimeAdapter: Send + Sync {
block_index: BlockIndex,
prev_block_hash: &CryptoHash,
block_hash: &CryptoHash,
receipts: &Vec<Vec<ReceiptTransaction>>,
receipts: &Vec<Vec<Receipt>>,
transactions: &Vec<SignedTransaction>,
) -> Result<
(
Expand Down
4 changes: 2 additions & 2 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use near_network::{
use near_pool::TransactionPool;
use near_primitives::crypto::signature::{verify, Signature};
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::transaction::{ReceiptTransaction, SignedTransaction};
use near_primitives::transaction::{Receipt, SignedTransaction};
use near_primitives::types::{AccountId, BlockIndex, ShardId};
use near_primitives::unwrap_or_return;
use near_store::Store;
Expand Down Expand Up @@ -1097,7 +1097,7 @@ impl ClientActor {
&mut self,
shard_id: ShardId,
hash: CryptoHash,
) -> Result<(Vec<u8>, Vec<ReceiptTransaction>), near_chain::Error> {
) -> Result<(Vec<u8>, Vec<Receipt>), near_chain::Error> {
let header = self.chain.get_block_header(&hash)?;
let prev_hash = header.prev_hash;
let payload = self
Expand Down
13 changes: 4 additions & 9 deletions chain/network/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use near_primitives::crypto::signature::{sign, PublicKey, SecretKey, Signature};
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::logging::pretty_str;
use near_primitives::serialize::{BaseEncode, Decode};
use near_primitives::transaction::{ReceiptTransaction, SignedTransaction};
use near_primitives::transaction::{Receipt, 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;
Expand Down Expand Up @@ -403,7 +403,7 @@ pub enum PeerMessage {
Transaction(SignedTransaction),

StateRequest(ShardId, CryptoHash),
StateResponse(ShardId, CryptoHash, Vec<u8>, Vec<ReceiptTransaction>),
StateResponse(ShardId, CryptoHash, Vec<u8>, Vec<Receipt>),

AnnounceAccount(AnnounceAccount),
}
Expand Down Expand Up @@ -841,7 +841,7 @@ pub enum NetworkClientMessages {
/// State request.
StateRequest(ShardId, CryptoHash),
/// State response.
StateResponse(ShardId, CryptoHash, Vec<u8>, Vec<ReceiptTransaction>),
StateResponse(ShardId, CryptoHash, Vec<u8>, Vec<Receipt>),
/// Account announcement that needs to be validated before being processed
AnnounceAccount(AnnounceAccount),
}
Expand All @@ -862,12 +862,7 @@ pub enum NetworkClientResponses {
/// Headers response.
BlockHeaders(Vec<BlockHeader>),
/// Response to state request.
StateResponse {
shard_id: ShardId,
hash: CryptoHash,
payload: Vec<u8>,
receipts: Vec<ReceiptTransaction>,
},
StateResponse { shard_id: ShardId, hash: CryptoHash, payload: Vec<u8>, receipts: Vec<Receipt> },
}

impl<A, M> MessageResponse<A, M> for NetworkClientResponses
Expand Down
24 changes: 14 additions & 10 deletions chain/pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ impl TransactionPool {

/// Insert a valid transaction into the pool that passed validation.
pub fn insert_transaction(&mut self, valid_transaction: ValidTransaction) {
let account = valid_transaction.transaction.body.get_originator();
let nonce = valid_transaction.transaction.body.get_nonce();
let signer_id = valid_transaction.transaction.transaction.signer_id.clone();
let nonce = valid_transaction.transaction.transaction.nonce;
self.num_transactions += 1;
self.transactions
.entry(account)
.entry(signer_id)
.or_insert_with(BTreeMap::new)
.insert(nonce, valid_transaction.transaction);
}
Expand All @@ -52,17 +52,17 @@ impl TransactionPool {
/// Quick reconciliation step - evict all transactions that already in the block
/// or became invalid after it.
pub fn reconcile_block(&mut self, block: &Block) {
for transaction in block.transactions.iter() {
let account = transaction.body.get_originator();
let nonce = transaction.body.get_nonce();
for signed_transaction in block.transactions.iter() {
let signer_id = &signed_transaction.transaction.signer_id;
let nonce = signed_transaction.transaction.nonce;
let mut remove_map = false;
if let Some(map) = self.transactions.get_mut(&account) {
if let Some(map) = self.transactions.get_mut(signer_id) {
map.remove(&nonce);
remove_map = map.is_empty();
}
if remove_map {
self.num_transactions -= 1;
self.transactions.remove(&account);
self.transactions.remove(signer_id);
}
}
}
Expand All @@ -71,7 +71,7 @@ impl TransactionPool {
self.num_transactions
}
}

/*
#[cfg(test)]
mod tests {
use rand::seq::SliceRandom;
Expand All @@ -84,13 +84,16 @@ mod tests {
use crate::TransactionPool;
use near_primitives::types::Balance;
/// Add transactions of nonce from 1..10 in random order. Check that mempool
/// orders them correctly.
#[test]
fn test_order_nonce() {
let signer = InMemorySigner::from_seed("alice.near", "alice.near");
let mut transactions: Vec<_> = (1..10)
.map(|i| TransactionBody::send_money(i, "alice.near", "bob.near", i as Balance).sign(&signer))
.map(|i| {
TransactionBody::send_money(i, "alice.near", "bob.near", i as Balance).sign(&signer)
})
.collect();
let mut pool = TransactionPool::new();
let mut rng = thread_rng();
Expand All @@ -104,3 +107,4 @@ mod tests {
}
}
*/
9 changes: 3 additions & 6 deletions core/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ 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::transaction::Callback;
use near_primitives::types::{AccountId, StorageUsage};
use near_primitives::utils::{
key_for_access_key, key_for_account, key_for_callback, key_for_code, prefix_for_access_key,
prefix_for_data,
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;
use near_protos::receipt as receipt_proto;

pub use crate::trie::{
update::TrieUpdate, update::TrieUpdateIterator, Trie, TrieChanges, TrieIterator,
Expand Down Expand Up @@ -266,7 +263,7 @@ pub fn get_access_key_raw(state_update: &TrieUpdate, key: &[u8]) -> Option<Acces
get_proto(state_update, key)
.and_then(|value: access_key_proto::AccessKey| value.try_into().ok())
}

/*
pub fn set_callback(state_update: &mut TrieUpdate, id: &[u8], callback: &Callback) {
let proto: receipt_proto::Callback = callback.clone().into();
set_proto(state_update, key_for_callback(id), &proto);
Expand All @@ -276,7 +273,7 @@ pub fn get_callback(state_update: &TrieUpdate, id: &[u8]) -> Option<Callback> {
get_proto(state_update, &key_for_callback(id))
.and_then(|value: receipt_proto::Callback| value.try_into().ok())
}

*/
pub fn set_code(state_update: &mut TrieUpdate, account_id: &AccountId, code: &ContractCode) {
state_update
.set(key_for_code(account_id), DBValue::from_vec(code.code.clone()))
Expand Down
11 changes: 5 additions & 6 deletions near/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::rpc::{AccountViewCallResult, QueryResponse, ViewStateResult};
use near_primitives::transaction::{ReceiptTransaction, SignedTransaction, TransactionResult};
use near_primitives::transaction::{Receipt, SignedTransaction, TransactionResult};
use near_primitives::types::{AccountId, BlockIndex, MerkleHash, ShardId, ValidatorStake};
use near_primitives::utils::prefix_for_access_key;
use near_store::{
Expand Down Expand Up @@ -265,7 +265,7 @@ impl RuntimeAdapter for NightshadeRuntime {
block_index: BlockIndex,
prev_block_hash: &CryptoHash,
block_hash: &CryptoHash,
receipts: &Vec<Vec<ReceiptTransaction>>,
receipts: &Vec<Vec<Receipt>>,
transactions: &Vec<SignedTransaction>,
) -> Result<
(
Expand Down Expand Up @@ -466,8 +466,7 @@ mod test {
use near_primitives::rpc::AccountViewCallResult;
use near_primitives::serialize::BaseEncode;
use near_primitives::transaction::{
CreateAccountTransaction, ReceiptTransaction, SignedTransaction, StakeTransaction,
TransactionBody,
CreateAccountTransaction, Receipt, SignedTransaction, StakeTransaction, TransactionBody,
};
use near_primitives::types::{Balance, BlockIndex, Nonce, ValidatorStake};
use near_store::create_store;
Expand Down Expand Up @@ -495,9 +494,9 @@ mod test {
block_index: BlockIndex,
prev_block_hash: &CryptoHash,
block_hash: &CryptoHash,
receipts: &Vec<Vec<ReceiptTransaction>>,
receipts: &Vec<Vec<Receipt>>,
transactions: &Vec<SignedTransaction>,
) -> (CryptoHash, Vec<ValidatorStake>, Vec<Vec<ReceiptTransaction>>) {
) -> (CryptoHash, Vec<ValidatorStake>, Vec<Vec<Receipt>>) {
let mut root = *state_root;
let (wrapped_trie_changes, new_root, _tx_results, receipt_results, stakes) = self
.apply_transactions(
Expand Down
21 changes: 6 additions & 15 deletions runtime/runtime/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Settings of the parameters of the runtime.
use near_primitives::transaction::TransactionBody;
use near_primitives::transaction::Action;
use near_primitives::types::{Balance, BlockIndex};
use wasm::types::Config;

Expand All @@ -23,32 +23,23 @@ pub struct TransactionsCosts {
pub create_account: Balance,
pub deploy_contract: Balance,
pub function_call: Balance,
pub self_function_call: Balance,
pub send_money: Balance,
pub transfer: Balance,
pub stake: Balance,
pub swap_key: Balance,
pub add_key: Balance,
pub delete_key: Balance,
pub delete_account: Balance,
}

impl TransactionsCosts {
/// Get the cost of the given transaction.
pub fn cost(&self, transaction_body: &TransactionBody) -> Balance {
use TransactionBody::*;
match transaction_body {
pub fn cost(&self, action: &Action) -> Balance {
use Action::*;
match action {
CreateAccount(_) => self.create_account,
DeployContract(_) => self.deploy_contract,
FunctionCall(_)
if Some(transaction_body.get_originator())
== transaction_body.get_contract_id() =>
{
self.self_function_call
}
FunctionCall(_) => self.function_call,
SendMoney(_) => self.send_money,
Transfer(_) => self.transfer,
Stake(_) => self.stake,
SwapKey(_) => self.swap_key,
AddKey(_) => self.add_key,
DeleteKey(_) => self.delete_key,
DeleteAccount(_) => self.delete_account,
Expand Down
Loading

0 comments on commit 6f46173

Please sign in to comment.