Skip to content

Commit

Permalink
Merge branch 'master' into block-merkle-root
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenwang1996 committed May 18, 2020
2 parents 96b33e0 + 7283fb2 commit b1a235a
Show file tree
Hide file tree
Showing 71 changed files with 1,748 additions and 1,239 deletions.
4 changes: 2 additions & 2 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

/genesis-tools/ @nearmax

/core/ @nearmax @evgenykuzyakov @SkidanovAlex @bowenwang1996
/neard/ @nearmax @evgenykuzyakov @SkidanovAlex @bowenwang1996
/core/ @bowenwang1996 @frol @evgenykuzyakov @nearmax @SkidanovAlex
/neard/ @bowenwang1996 @frol @evgenykuzyakov @nearmax @SkidanovAlex
5 changes: 5 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ The following are the neccessary conditions for the reward:
* The vulnerability is not disclosed to anyone else except the finder and NEAR before it is fixed;
* The vulnerability is not exploited until it is fixed.

### Rewards platform

We are using https://gitcoin.co/ to reward tokens. Meaning that every security vulnerability that you submit to us will be processed like a general work-item by an external contributor. To receive the reward you would need to register on https://gitcoin.co/ and be able to receive the reward through it. Example of a reward for a security vulnerability finding: https://gitcoin.co/issue/near/community/5/4359


## Receive Security Updates

If you are must be informed about security vulnerabilities, please subscribe to the [NEAR Security Update newsletter](https://groups.google.com/a/nearprotocol.com/forum/#!forum/security-updates).
Expand Down
34 changes: 16 additions & 18 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use near_primitives::views::{
ExecutionOutcomeWithIdView, ExecutionStatusView, FinalExecutionOutcomeView,
FinalExecutionStatus, LightClientBlockView,
};
use near_store::{ColStateHeaders, ColStateParts, Trie};
use near_store::{ColStateHeaders, ColStateParts, ShardTries};

use crate::error::{Error, ErrorKind};
use crate::lightclient::get_epoch_block_producers_view;
Expand Down Expand Up @@ -320,7 +320,6 @@ impl Chain {
vec![],
vec![],
vec![],
0,
chain_genesis.total_supply,
)?;
store_update.save_block_header(genesis.header.clone())?;
Expand All @@ -342,7 +341,6 @@ impl Chain {
0,
chain_genesis.gas_limit,
0,
0,
),
);
}
Expand Down Expand Up @@ -541,7 +539,7 @@ impl Chain {
// and the Trie is updated with having only Genesis data.
// 4. State Sync Clearing happens in `reset_data_pre_state_sync()`.
//
pub fn clear_data(&mut self, trie: Arc<Trie>) -> Result<(), Error> {
pub fn clear_data(&mut self, tries: ShardTries) -> Result<(), Error> {
let head = self.store.head()?;
let tail = self.store.tail()?;
let mut gc_stop_height = self.runtime_adapter.get_gc_stop_height(&head.last_block_hash)?;
Expand All @@ -556,7 +554,7 @@ impl Chain {

// Forks Cleaning
for height in tail..gc_stop_height {
self.clear_forks_data(trie.clone(), height)?;
self.clear_forks_data(tries.clone(), height)?;
}

// Canonical Chain Clearing
Expand All @@ -576,7 +574,7 @@ impl Chain {
} else if prev_block_refcount == 1 {
debug_assert_eq!(blocks_current_height.len(), 1);
chain_store_update
.clear_block_data(*block_hash, GCMode::Canonical(trie.clone()))?;
.clear_block_data(*block_hash, GCMode::Canonical(tries.clone()))?;
} else {
return Err(ErrorKind::GCError(
"block on canonical chain shouldn't have refcount 0".into(),
Expand All @@ -591,7 +589,11 @@ impl Chain {
Ok(())
}

pub fn clear_forks_data(&mut self, trie: Arc<Trie>, height: BlockHeight) -> Result<(), Error> {
pub fn clear_forks_data(
&mut self,
tries: ShardTries,
height: BlockHeight,
) -> Result<(), Error> {
if let Ok(blocks_current_height) = self.store.get_all_block_hashes_by_height(height) {
let blocks_current_height =
blocks_current_height.values().flatten().cloned().collect::<Vec<_>>();
Expand All @@ -609,7 +611,7 @@ impl Chain {

// It's safe to call `clear_block_data` for prev data because it clears fork only here
chain_store_update
.clear_block_data(current_hash, GCMode::Fork(trie.clone()))?;
.clear_block_data(current_hash, GCMode::Fork(tries.clone()))?;
chain_store_update.commit()?;

current_hash = prev_hash;
Expand Down Expand Up @@ -795,7 +797,6 @@ impl Chain {
header.inner_rest.validator_proposals.clone(),
vec![],
header.inner_rest.chunk_mask.clone(),
header.inner_rest.validator_reward,
header.inner_rest.total_supply,
)?;
}
Expand Down Expand Up @@ -1431,7 +1432,7 @@ impl Chain {
}

let state_root_node =
self.runtime_adapter.get_state_root_node(&chunk_header.inner.prev_state_root);
self.runtime_adapter.get_state_root_node(shard_id, &chunk_header.inner.prev_state_root);

Ok(ShardStateSyncResponseHeader {
chunk,
Expand Down Expand Up @@ -1476,13 +1477,14 @@ impl Chain {
return Err(ErrorKind::InvalidStateRequest("shard_id out of bounds".into()).into());
}
let state_root = sync_prev_block.chunks[shard_id as usize].inner.prev_state_root.clone();
let state_root_node = self.runtime_adapter.get_state_root_node(&state_root);
let state_root_node = self.runtime_adapter.get_state_root_node(shard_id, &state_root);
let num_parts = Self::get_num_state_parts(state_root_node.memory_usage);

if part_id >= num_parts {
return Err(ErrorKind::InvalidStateRequest("part_id out of bound".to_string()).into());
}
let state_part = self.runtime_adapter.obtain_state_part(&state_root, part_id, num_parts);
let state_part =
self.runtime_adapter.obtain_state_part(shard_id, &state_root, part_id, num_parts);

Ok(state_part)
}
Expand Down Expand Up @@ -1725,7 +1727,7 @@ impl Chain {
}

// Confirm that state matches the parts we received
self.runtime_adapter.confirm_state(&state_root, &parts)?;
self.runtime_adapter.confirm_state(shard_id, &state_root, &parts)?;

// Applying the chunk starts here
let mut chain_update = ChainUpdate::new(
Expand Down Expand Up @@ -2533,7 +2535,6 @@ impl<'a> ChainUpdate<'a> {
apply_result.validator_proposals,
apply_result.total_gas_burnt,
gas_limit,
apply_result.total_validator_reward,
apply_result.total_balance_burnt,
),
);
Expand Down Expand Up @@ -2620,8 +2621,7 @@ impl<'a> ChainUpdate<'a> {

// Check that we know the epoch of the block before we try to get the header
// (so that a block from unknown epoch doesn't get marked as an orphan)
if let Err(_) = self.runtime_adapter.get_epoch_inflation(&block.header.inner_lite.epoch_id)
{
if !self.runtime_adapter.epoch_exists(&block.header.inner_lite.epoch_id) {
return Err(ErrorKind::EpochOutOfBounds.into());
}

Expand Down Expand Up @@ -2747,7 +2747,6 @@ impl<'a> ChainUpdate<'a> {
block.header.inner_rest.validator_proposals.clone(),
block.header.inner_rest.challenges_result.clone(),
block.header.inner_rest.chunk_mask.clone(),
block.header.inner_rest.validator_reward,
block.header.inner_rest.total_supply,
)?;

Expand Down Expand Up @@ -3198,7 +3197,6 @@ impl<'a> ChainUpdate<'a> {
apply_result.validator_proposals,
apply_result.total_gas_burnt,
gas_limit,
apply_result.total_validator_reward,
apply_result.total_balance_burnt,
);
self.chain_store_update.save_chunk_extra(&block_header.hash, shard_id, chunk_extra);
Expand Down
6 changes: 1 addition & 5 deletions chain/chain/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub enum ErrorKind {
/// Orphan block.
#[fail(display = "Orphan")]
Orphan,
/// Block is not availiable (e.g. garbage collected)
/// Block is not available (e.g. garbage collected)
#[fail(display = "Block Missing (unavailable on the node): {}", _0)]
BlockMissing(CryptoHash),
/// Chunk is missing.
Expand Down Expand Up @@ -128,9 +128,6 @@ pub enum ErrorKind {
/// Invalid Gas Used
#[fail(display = "Invalid Gas Used")]
InvalidGasUsed,
/// Invalid Validator Reward
#[fail(display = "Invalid Validator Reward")]
InvalidReward,
/// Invalid Balance Burnt
#[fail(display = "Invalid Balance Burnt")]
InvalidBalanceBurnt,
Expand Down Expand Up @@ -251,7 +248,6 @@ impl Error {
| ErrorKind::InvalidGasLimit
| ErrorKind::InvalidGasPrice
| ErrorKind::InvalidGasUsed
| ErrorKind::InvalidReward
| ErrorKind::InvalidBalanceBurnt
| ErrorKind::InvalidShardId(_)
| ErrorKind::InvalidStateRequest(_)
Expand Down
26 changes: 13 additions & 13 deletions chain/chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use near_store::{
ColIncomingReceipts, ColInvalidChunks, ColLastBlockWithNewChunk, ColNextBlockHashes,
ColNextBlockWithNewChunk, ColOutgoingReceipts, ColPartialChunks, ColReceiptIdToShardId,
ColState, ColStateChanges, ColStateDlInfos, ColStateHeaders, ColTransactionResult,
ColTransactions, ColTrieChanges, KeyForStateChanges, Store, StoreUpdate, Trie, TrieChanges,
WrappedTrieChanges,
ColTransactions, ColTrieChanges, KeyForStateChanges, ShardTries, Store, StoreUpdate,
TrieChanges, WrappedTrieChanges,
};

use crate::byzantine_assert;
Expand All @@ -58,8 +58,8 @@ pub struct ShardInfo(pub ShardId, pub ChunkHash);

#[derive(Clone)]
pub enum GCMode {
Fork(Arc<Trie>),
Canonical(Arc<Trie>),
Fork(ShardTries),
Canonical(ShardTries),
StateSync,
}

Expand Down Expand Up @@ -1803,24 +1803,24 @@ impl<'a> ChainStoreUpdate<'a> {

// 1. Apply revert insertions or deletions from ColTrieChanges for Trie
match gc_mode.clone() {
GCMode::Fork(trie) => {
GCMode::Fork(tries) => {
// If the block is on a fork, we delete the state that's the result of applying this block
self.store()
.get_ser(ColTrieChanges, block_hash.as_ref())?
.map(|trie_changes: TrieChanges| {
trie_changes
.revert_insertions_into(trie.clone(), &mut store_update)
tries
.revert_insertions(&trie_changes, 0, &mut store_update)
.map_err(|err| ErrorKind::Other(err.to_string()))
})
.unwrap_or(Ok(()))?;
}
GCMode::Canonical(trie) => {
GCMode::Canonical(tries) => {
// If the block is on canonical chain, we delete the state that's before applying this block
self.store()
.get_ser(ColTrieChanges, block_hash.as_ref())?
.map(|trie_changes: TrieChanges| {
trie_changes
.deletions_into(trie.clone(), &mut store_update)
tries
.apply_deletions(&trie_changes, 0, &mut store_update)
.map_err(|err| ErrorKind::Other(err.to_string()))
})
.unwrap_or(Ok(()))?;
Expand Down Expand Up @@ -2604,7 +2604,7 @@ mod tests {

assert!(check_refcount_map(&mut chain).is_ok());
chain.epoch_length = 1;
let trie = chain.runtime_adapter.get_trie();
let trie = chain.runtime_adapter.get_tries();
assert!(chain.clear_data(trie).is_ok());

assert!(chain.get_block(&blocks[0].hash()).is_ok());
Expand Down Expand Up @@ -2670,7 +2670,7 @@ mod tests {
);
assert!(chain.mut_store().get_next_block_hash(&blocks[5].hash()).is_ok());

let trie = chain.runtime_adapter.get_trie();
let trie = chain.runtime_adapter.get_tries();
let mut store_update = chain.mut_store().store_update();
assert!(store_update.clear_block_data(blocks[5].hash(), GCMode::Canonical(trie)).is_ok());
store_update.commit().unwrap();
Expand Down Expand Up @@ -2719,7 +2719,7 @@ mod tests {
}

assert!(check_refcount_map(&mut chain).is_ok());
let trie = chain.runtime_adapter.get_trie();
let trie = chain.runtime_adapter.get_tries();

for iter in 0..10 {
println!("ITERATION #{:?}", iter);
Expand Down
Loading

0 comments on commit b1a235a

Please sign in to comment.