Skip to content

Commit

Permalink
feat: update gas price (#3028)
Browse files Browse the repository at this point in the history
Update gas price to 1_000_000_000 according to near/NEPs#92. 

Test plan
---------
* `upgradable.py` with transactions.
* spin up a node with this change and verify that it can sync to testnet and process blocks without any issue.
  • Loading branch information
bowenwang1996 authored Jul 25, 2020
1 parent 73c7f00 commit 9d7aaf5
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 172 deletions.
109 changes: 13 additions & 96 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ use std::sync::Arc;
use std::time::{Duration as TimeDuration, Instant};

use borsh::BorshSerialize;
use chrono::prelude::{DateTime, Utc};
use chrono::Duration;
use chrono::Utc;
use log::{debug, error, info};
use num_rational::Rational;
use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;

use near_chain_configs::GenesisConfig;
use near_primitives::block::{genesis_chunks, Tip};
use near_primitives::challenge::{
BlockDoubleSign, Challenge, ChallengeBody, ChallengesResult, ChunkProofs, ChunkState,
Expand All @@ -32,11 +30,10 @@ use near_primitives::syncing::{
};
use near_primitives::transaction::ExecutionOutcomeWithIdAndProof;
use near_primitives::types::{
AccountId, Balance, BlockExtra, BlockHeight, BlockHeightDelta, ChunkExtra, EpochId, Gas,
MerkleHash, NumBlocks, ShardId, ValidatorStake,
AccountId, Balance, BlockExtra, BlockHeight, BlockHeightDelta, ChunkExtra, EpochId, MerkleHash,
NumBlocks, ShardId, ValidatorStake,
};
use near_primitives::unwrap_or_return;
use near_primitives::version::ProtocolVersion;
use near_primitives::views::{
ExecutionOutcomeWithIdView, ExecutionStatusView, FinalExecutionOutcomeView,
FinalExecutionStatus, LightClientBlockView,
Expand All @@ -47,8 +44,9 @@ use crate::error::{Error, ErrorKind, LogTransientStorageError};
use crate::lightclient::get_epoch_block_producers_view;
use crate::store::{ChainStore, ChainStoreAccess, ChainStoreUpdate, GCMode};
use crate::types::{
AcceptedBlock, ApplyTransactionResult, Block, BlockHeader, BlockHeaderInfo, BlockStatus,
BlockSyncResponse, Provenance, ReceiptList, RuntimeAdapter,
AcceptedBlock, ApplyTransactionResult, Block, BlockEconomicsConfig, BlockHeader,
BlockHeaderInfo, BlockStatus, BlockSyncResponse, ChainGenesis, Provenance, ReceiptList,
RuntimeAdapter,
};
use crate::validate::{
validate_challenge, validate_chunk_proofs, validate_chunk_with_chunk_extra,
Expand All @@ -75,13 +73,6 @@ const NEAR_BASE: Balance = 1_000_000_000_000_000_000_000_000;
/// Number of epochs for which we keep store data
pub const NUM_EPOCHS_TO_KEEP_STORE_DATA: u64 = 5;

/// Block economics config taken from genesis config
pub struct BlockEconomicsConfig {
pub gas_price_adjustment_rate: Rational,
pub min_gas_price: Balance,
pub max_gas_price: Balance,
}

enum ApplyChunksMode {
ThisEpoch,
NextEpoch,
Expand Down Expand Up @@ -176,74 +167,6 @@ impl OrphanBlockPool {
}
}

/// Chain genesis configuration.
#[derive(Clone)]
pub struct ChainGenesis {
pub time: DateTime<Utc>,
pub height: BlockHeight,
pub gas_limit: Gas,
pub min_gas_price: Balance,
pub max_gas_price: Balance,
pub total_supply: Balance,
pub max_inflation_rate: Rational,
pub gas_price_adjustment_rate: Rational,
pub transaction_validity_period: NumBlocks,
pub epoch_length: BlockHeightDelta,
pub protocol_version: ProtocolVersion,
}

impl ChainGenesis {
pub fn new(
time: DateTime<Utc>,
height: BlockHeight,
gas_limit: Gas,
min_gas_price: Balance,
max_gas_price: Balance,
total_supply: Balance,
max_inflation_rate: Rational,
gas_price_adjustment_rate: Rational,
transaction_validity_period: NumBlocks,
epoch_length: BlockHeightDelta,
protocol_version: ProtocolVersion,
) -> Self {
Self {
time,
height,
gas_limit,
min_gas_price,
max_gas_price,
total_supply,
max_inflation_rate,
gas_price_adjustment_rate,
transaction_validity_period,
epoch_length,
protocol_version,
}
}
}

impl<T> From<T> for ChainGenesis
where
T: AsRef<GenesisConfig>,
{
fn from(genesis_config: T) -> Self {
let genesis_config = genesis_config.as_ref();
ChainGenesis::new(
genesis_config.genesis_time,
genesis_config.genesis_height,
genesis_config.gas_limit,
genesis_config.min_gas_price,
genesis_config.max_gas_price,
genesis_config.total_supply,
genesis_config.max_inflation_rate,
genesis_config.gas_price_adjustment_rate,
genesis_config.transaction_validity_period,
genesis_config.epoch_length,
genesis_config.protocol_version,
)
}
}

/// Facade to the blockchain block processing and storage.
/// Provides current view on the state according to the chain state.
pub struct Chain {
Expand Down Expand Up @@ -290,11 +213,7 @@ impl Chain {
genesis: genesis.header().clone(),
transaction_validity_period: chain_genesis.transaction_validity_period,
epoch_length: chain_genesis.epoch_length,
block_economics_config: BlockEconomicsConfig {
gas_price_adjustment_rate: chain_genesis.gas_price_adjustment_rate,
min_gas_price: chain_genesis.min_gas_price,
max_gas_price: chain_genesis.max_gas_price,
},
block_economics_config: BlockEconomicsConfig::from(chain_genesis),
doomslug_threshold_mode,
})
}
Expand Down Expand Up @@ -411,11 +330,7 @@ impl Chain {
genesis: genesis.header().clone(),
transaction_validity_period: chain_genesis.transaction_validity_period,
epoch_length: chain_genesis.epoch_length,
block_economics_config: BlockEconomicsConfig {
gas_price_adjustment_rate: chain_genesis.gas_price_adjustment_rate,
min_gas_price: chain_genesis.min_gas_price,
max_gas_price: chain_genesis.max_gas_price,
},
block_economics_config: BlockEconomicsConfig::from(chain_genesis),
doomslug_threshold_mode,
})
}
Expand Down Expand Up @@ -2976,11 +2891,13 @@ impl<'a> ChainUpdate<'a> {
return Err(ErrorKind::Other("Invalid block".into()).into());
}

let protocol_version =
self.runtime_adapter.get_epoch_protocol_version(&block.header().epoch_id())?;
if !block.verify_gas_price(
prev_gas_price,
self.block_economics_config.min_gas_price,
self.block_economics_config.max_gas_price,
self.block_economics_config.gas_price_adjustment_rate,
self.block_economics_config.min_gas_price(protocol_version),
self.block_economics_config.max_gas_price(protocol_version),
self.block_economics_config.gas_price_adjustment_rate(protocol_version),
) {
byzantine_assert!(false);
return Err(ErrorKind::InvalidGasPrice.into());
Expand Down
6 changes: 4 additions & 2 deletions chain/chain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#[macro_use]
extern crate lazy_static;

pub use chain::{collect_receipts, Chain, ChainGenesis, MAX_ORPHAN_SIZE};
pub use chain::{collect_receipts, Chain, MAX_ORPHAN_SIZE};
pub use doomslug::{Doomslug, DoomslugBlockProductionReadiness, DoomslugThresholdMode};
pub use error::{Error, ErrorKind};
pub use lightclient::create_light_client_block_view;
pub use store::{ChainStore, ChainStoreAccess, ChainStoreUpdate};
pub use store_validator::{ErrorMessage, StoreValidator};
pub use types::{Block, BlockHeader, BlockStatus, Provenance, ReceiptResult, RuntimeAdapter};
pub use types::{
Block, BlockHeader, BlockStatus, ChainGenesis, Provenance, ReceiptResult, RuntimeAdapter,
};

pub mod chain;
mod doomslug;
Expand Down
54 changes: 27 additions & 27 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ use near_store::{
WrappedTrieChanges,
};

use crate::chain::{Chain, ChainGenesis, NUM_EPOCHS_TO_KEEP_STORE_DATA};
use crate::chain::{Chain, NUM_EPOCHS_TO_KEEP_STORE_DATA};
use crate::error::{Error, ErrorKind};
use crate::store::ChainStoreAccess;
use crate::types::{ApplyTransactionResult, BlockHeaderInfo};
use crate::types::{ApplyTransactionResult, BlockHeaderInfo, ChainGenesis};
use crate::{BlockHeader, DoomslugThresholdMode, RuntimeAdapter};

#[derive(
Expand Down Expand Up @@ -957,19 +957,19 @@ pub fn setup_with_tx_validity_period(
let runtime = Arc::new(KeyValueRuntime::new(store));
let chain = Chain::new(
runtime.clone(),
&ChainGenesis::new(
Utc::now(),
0,
1_000_000,
100,
1_000_000_000,
1_000_000_000,
Rational::from_integer(0),
Rational::from_integer(0),
tx_validity_period,
10,
PROTOCOL_VERSION,
),
&ChainGenesis {
time: Utc::now(),
height: 0,
gas_limit: 1_000_000,
min_gas_price: 100,
max_gas_price: 1_000_000_000,
total_supply: 1_000_000_000,
max_inflation_rate: Rational::from_integer(0),
gas_price_adjustment_rate: Rational::from_integer(0),
transaction_validity_period: tx_validity_period,
epoch_length: 10,
protocol_version: PROTOCOL_VERSION,
},
DoomslugThresholdMode::NoApprovals,
)
.unwrap();
Expand Down Expand Up @@ -998,19 +998,19 @@ pub fn setup_with_validators(
));
let chain = Chain::new(
runtime.clone(),
&ChainGenesis::new(
Utc::now(),
0,
1_000_000,
100,
1_000_000_000,
1_000_000_000,
Rational::from_integer(0),
Rational::from_integer(0),
tx_validity_period,
&ChainGenesis {
time: Utc::now(),
height: 0,
gas_limit: 1_000_000,
min_gas_price: 100,
max_gas_price: 1_000_000_000,
total_supply: 1_000_000_000,
max_inflation_rate: Rational::from_integer(0),
gas_price_adjustment_rate: Rational::from_integer(0),
transaction_validity_period: tx_validity_period,
epoch_length,
PROTOCOL_VERSION,
),
protocol_version: PROTOCOL_VERSION,
},
DoomslugThresholdMode::NoApprovals,
)
.unwrap();
Expand Down
90 changes: 87 additions & 3 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ use near_primitives::receipt::Receipt;
use near_primitives::sharding::ShardChunkHeader;
use near_primitives::transaction::{ExecutionOutcomeWithId, SignedTransaction};
use near_primitives::types::{
AccountId, ApprovalStake, Balance, BlockHeight, EpochId, Gas, MerkleHash, ShardId, StateRoot,
StateRootNode, ValidatorStake,
AccountId, ApprovalStake, Balance, BlockHeight, BlockHeightDelta, EpochId, Gas, MerkleHash,
NumBlocks, ShardId, StateRoot, StateRootNode, ValidatorStake,
};
use near_primitives::version::{
ProtocolVersion, MIN_GAS_PRICE_NEP_92, MIN_PROTOCOL_VERSION_NEP_92,
};
use near_primitives::version::ProtocolVersion;
use near_primitives::views::{EpochValidatorInfo, QueryRequest, QueryResponse};
use near_store::{PartialStorage, ShardTries, Store, StoreUpdate, Trie, WrappedTrieChanges};

use crate::error::Error;
use chrono::{DateTime, Utc};
use near_chain_configs::GenesisConfig;
use num_rational::Rational;

#[derive(Eq, PartialEq, Debug, Clone)]
pub enum BlockStatus {
Expand Down Expand Up @@ -127,6 +132,85 @@ impl BlockHeaderInfo {
}
}

/// Block economics config taken from genesis config
pub struct BlockEconomicsConfig {
gas_price_adjustment_rate: Rational,
min_gas_price: Balance,
max_gas_price: Balance,
genesis_protocol_version: ProtocolVersion,
}

impl BlockEconomicsConfig {
/// Compute min gas price according to protocol version and chain id. We only upgrade gas price
/// for betanet, testnet and mainnet.
pub fn min_gas_price(&self, protocol_version: ProtocolVersion) -> Balance {
if self.genesis_protocol_version < MIN_PROTOCOL_VERSION_NEP_92
&& protocol_version >= MIN_PROTOCOL_VERSION_NEP_92
{
MIN_GAS_PRICE_NEP_92
} else {
self.min_gas_price
}
}

pub fn max_gas_price(&self, _protocol_version: ProtocolVersion) -> Balance {
self.max_gas_price
}

pub fn gas_price_adjustment_rate(&self, _protocol_version: ProtocolVersion) -> Rational {
self.gas_price_adjustment_rate
}
}

impl From<&ChainGenesis> for BlockEconomicsConfig {
fn from(chain_genesis: &ChainGenesis) -> Self {
BlockEconomicsConfig {
gas_price_adjustment_rate: chain_genesis.gas_price_adjustment_rate,
min_gas_price: chain_genesis.min_gas_price,
max_gas_price: chain_genesis.max_gas_price,
genesis_protocol_version: chain_genesis.protocol_version,
}
}
}

/// Chain genesis configuration.
#[derive(Clone)]
pub struct ChainGenesis {
pub time: DateTime<Utc>,
pub height: BlockHeight,
pub gas_limit: Gas,
pub min_gas_price: Balance,
pub max_gas_price: Balance,
pub total_supply: Balance,
pub max_inflation_rate: Rational,
pub gas_price_adjustment_rate: Rational,
pub transaction_validity_period: NumBlocks,
pub epoch_length: BlockHeightDelta,
pub protocol_version: ProtocolVersion,
}

impl<T> From<T> for ChainGenesis
where
T: AsRef<GenesisConfig>,
{
fn from(genesis_config: T) -> Self {
let genesis_config = genesis_config.as_ref();
Self {
time: genesis_config.genesis_time,
height: genesis_config.genesis_height,
gas_limit: genesis_config.gas_limit,
min_gas_price: genesis_config.min_gas_price,
max_gas_price: genesis_config.max_gas_price,
total_supply: genesis_config.total_supply,
max_inflation_rate: genesis_config.max_inflation_rate,
gas_price_adjustment_rate: genesis_config.gas_price_adjustment_rate,
transaction_validity_period: genesis_config.transaction_validity_period,
epoch_length: genesis_config.epoch_length,
protocol_version: genesis_config.protocol_version,
}
}
}

/// Bridge between the chain and the runtime.
/// Main function is to update state given transactions.
/// Additionally handles validators.
Expand Down
4 changes: 2 additions & 2 deletions chain/chain/tests/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
mod tests {
use std::sync::Arc;

use near_chain::chain::{Chain, ChainGenesis};
use near_chain::chain::Chain;
use near_chain::test_utils::KeyValueRuntime;
use near_chain::types::Tip;
use near_chain::types::{ChainGenesis, Tip};
use near_chain::DoomslugThresholdMode;
use near_crypto::KeyType;
use near_primitives::block::Block;
Expand Down
Loading

0 comments on commit 9d7aaf5

Please sign in to comment.