Skip to content

Commit

Permalink
Merge pull request #1711 from nearprotocol/v0.4.6
Browse files Browse the repository at this point in the history
Release v0.4.6
  • Loading branch information
bowenwang1996 authored Nov 17, 2019
2 parents 54b597c + a9b6441 commit 7cd0ab6
Show file tree
Hide file tree
Showing 118 changed files with 5,161 additions and 2,795 deletions.
177 changes: 23 additions & 154 deletions Cargo.lock

Large diffs are not rendered by default.

188 changes: 143 additions & 45 deletions chain/chain/src/chain.rs

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion chain/chain/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ pub enum ErrorKind {
/// Invalid epoch hash
#[fail(display = "Invalid Epoch Hash")]
InvalidEpochHash,
/// Invalid quorum_pre_vote or quorum_pre_commit
#[fail(display = "Invalid Finality Info")]
InvalidFinalityInfo,
/// Invalid validator proposals in the block.
#[fail(display = "Invalid Validator Proposals")]
InvalidValidatorProposals,
Expand All @@ -109,6 +112,24 @@ pub enum ErrorKind {
/// Invalid Approvals
#[fail(display = "Invalid Approvals")]
InvalidApprovals,
/// Invalid Gas Limit
#[fail(display = "Invalid Gas Limit")]
InvalidGasLimit,
/// Invalid Gas Limit
#[fail(display = "Invalid Gas Price")]
InvalidGasPrice,
/// Invalid Gas Used
#[fail(display = "Invalid Gas Used")]
InvalidGasUsed,
/// Invalid Rent Paid
#[fail(display = "Invalid Rent Paid")]
InvalidRent,
/// Invalid Validator Reward
#[fail(display = "Invalid Validator Reward")]
InvalidReward,
/// Invalid Balance Burnt
#[fail(display = "Invalid Balance Burnt")]
InvalidBalanceBurnt,
/// Validator error.
#[fail(display = "Validator Error: {}", _0)]
ValidatorError(String),
Expand Down Expand Up @@ -199,9 +220,16 @@ impl Error {
| ErrorKind::MaliciousChallenge
| ErrorKind::IncorrectNumberOfChunkHeaders
| ErrorKind::InvalidEpochHash
| ErrorKind::InvalidFinalityInfo
| ErrorKind::InvalidValidatorProposals
| ErrorKind::InvalidSignature
| ErrorKind::InvalidApprovals => true,
| ErrorKind::InvalidApprovals
| ErrorKind::InvalidGasLimit
| ErrorKind::InvalidGasPrice
| ErrorKind::InvalidGasUsed
| ErrorKind::InvalidReward
| ErrorKind::InvalidBalanceBurnt
| ErrorKind::InvalidRent => true,
}
}

Expand Down
54 changes: 39 additions & 15 deletions chain/chain/src/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use crate::error::{Error, ErrorKind};
use crate::{ChainStoreAccess, ChainStoreUpdate};
use near_primitives::block::{Approval, BlockHeader, BlockHeaderInner, Weight};
use near_primitives::hash::CryptoHash;
use near_primitives::types::{AccountId, BlockIndex};
use near_primitives::types::{AccountId, BlockIndex, EpochId};
use std::collections::{HashMap, HashSet};

// How many blocks back to search for a new reference hash when the chain switches and the block
// producer cannot use the same reference hash as the last approval on chain
const REFERENCE_HASH_LOOKUP_DEPTH: usize = 10;

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct FinalityGadgetQuorums {
pub last_quorum_pre_vote: CryptoHash,
Expand All @@ -18,7 +22,6 @@ pub struct FinalityGadget {}

impl FinalityGadget {
pub fn process_approval(
&self,
me: &Option<AccountId>,
approval: &Approval,
chain_store_update: &mut ChainStoreUpdate,
Expand Down Expand Up @@ -62,7 +65,6 @@ impl FinalityGadget {
}

pub fn verify_approval_conditions(
&mut self,
_approval: &Approval,
_chain_store: &mut dyn ChainStoreAccess,
) -> Result<(), ApprovalVerificationError> {
Expand All @@ -71,7 +73,6 @@ impl FinalityGadget {
}

pub fn get_my_approval_reference_hash(
&self,
prev_hash: CryptoHash,
chain_store: &mut dyn ChainStoreAccess,
) -> Option<CryptoHash> {
Expand Down Expand Up @@ -101,7 +102,7 @@ impl FinalityGadget {
let last_approval_on_chain =
chain_store.get_my_last_approval(&prev_prev_hash).ok().cloned();

self.get_my_approval_reference_hash_inner(
FinalityGadget::get_my_approval_reference_hash_inner(
prev_hash,
last_approval_on_chain,
largest_weight_approved,
Expand All @@ -111,7 +112,6 @@ impl FinalityGadget {
}

pub fn get_my_approval_reference_hash_inner(
&self,
prev_hash: CryptoHash,
last_approval_on_chain: Option<Approval>,
largest_weight_approved: Weight,
Expand All @@ -121,15 +121,24 @@ impl FinalityGadget {
let default_f = |chain_store: &mut dyn ChainStoreAccess| match chain_store
.get_block_header(&prev_hash)
{
Ok(header) => {
if header.inner.total_weight > largest_weight_approved
&& (header.inner.score > largest_score_approved
|| largest_score_approved == 0.into())
{
Some(prev_hash)
} else {
None
Ok(mut header) => {
let mut candidate = None;
// Get the reference_hash up to `REFERENCE_HASH_LOOKUP_DEPTH` blocks into the past
for _ in 0..REFERENCE_HASH_LOOKUP_DEPTH {
if header.inner.total_weight > largest_weight_approved
&& header.inner.score >= largest_score_approved
{
candidate = Some(header.hash());
let prev_hash = header.inner.prev_hash;
match chain_store.get_block_header(&prev_hash) {
Ok(new_header) => header = new_header,
Err(_) => break,
}
} else {
break;
}
}
return candidate;
}
Err(_) => None,
};
Expand Down Expand Up @@ -162,8 +171,8 @@ impl FinalityGadget {
}

pub fn compute_quorums(
&self,
mut prev_hash: CryptoHash,
epoch_id: EpochId,
mut height: BlockIndex,
mut approvals: Vec<Approval>,
chain_store: &mut dyn ChainStoreAccess,
Expand Down Expand Up @@ -248,6 +257,21 @@ impl FinalityGadget {
approvals = last_block_header.inner.approvals.clone();
height = last_block_header.inner.height;

if last_block_header.inner.epoch_id != epoch_id {
// Do not cross the epoch boundary. It is safe to get the last quorums from the last
// block of the previous epoch, since no approval in the current epoch could
// have finalized anything else in the previous epoch (they would exit here),
// and if anything was finalized / had a prevote in this epoch, it would have
// been found in previous iterations of the surrounding loop
if quorum_pre_vote.is_none() {
quorum_pre_vote = Some(last_block_header.inner.last_quorum_pre_vote);
}
if quorum_pre_commit.is_none() {
quorum_pre_commit = Some(last_block_header.inner.last_quorum_pre_commit);
}
break;
}

// Move `highest_height_no_quorum` if needed
while accounts_surrounding_no_quroum > total_block_producers * 2 / 3 {
accounts_surrounding_no_quroum -= height_to_accounts_to_remove
Expand Down
8 changes: 8 additions & 0 deletions chain/chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,14 @@ impl ChainStore {
}
Err(InvalidTxError::Expired)
}

pub fn get_block_height(&mut self, hash: &CryptoHash) -> Result<BlockIndex, Error> {
if hash == &CryptoHash::default() {
Ok(0)
} else {
Ok(self.get_block_header(hash)?.inner.height)
}
}
}

impl ChainStoreAccess for ChainStore {
Expand Down
Loading

0 comments on commit 7cd0ab6

Please sign in to comment.