-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic Gas Implementation #639
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
583906b
finally fixed compiler stuff
ec2 db4348b
accidentally committed some files
ec2 6086a0e
Merge branch 'main' into ec2/1559
ec2 a3b96cb
tests
ec2 eb25721
some gas api
ec2 594d98c
Merge branch 'main' into ec2/1559
ec2 a5f97d9
add gas api
ec2 f022fb8
base fee validation in sync
ec2 a39bebd
connect to rpc
ec2 7d21eda
fix clippy
ec2 3b99616
Merge branch 'main' into ec2/1559
ec2 7b70b5a
add license
ec2 c6a660d
Cleanup and fix tests
austinabell 1755d34
Cleanup bigint clones
austinabell d4575cb
suggestions
ec2 26faa7b
Merge branch 'main' into ec2/1559
ec2 ef6bdee
update basefee
ec2 99bd3c9
dont duplicate base fee computations
ec2 7a07b1a
fix more suggestions
ec2 d9afb2c
lint
ec2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
#[macro_use] | ||
extern crate lazy_static; | ||
|
||
mod store; | ||
|
||
pub use self::store::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use blocks::Tipset; | ||
use encoding::Cbor; | ||
use ipld_blockstore::BlockStore; | ||
use message::Message; | ||
use num_bigint::BigInt; | ||
use std::collections::HashSet; | ||
|
||
pub const BLOCK_GAS_LIMIT: i64 = 10_000_000_000; | ||
pub const BLOCK_GAS_TARGET: i64 = (BLOCK_GAS_LIMIT / 2) as i64; | ||
pub const BASE_FEE_MAX_CHANGE_DENOM: i64 = 8; // 12.5%; | ||
pub const INITIAL_BASE_FEE: i64 = 100000000; // Genesis base fee | ||
pub const PACKING_EFFICIENCY_DENOM: i64 = 5; | ||
pub const PACKING_EFFICIENCY_NUM: i64 = 4; | ||
lazy_static! { | ||
/// Cbor bytes of an empty array serialized. | ||
pub static ref MINIMUM_BASE_FEE: BigInt = 100.into(); | ||
|
||
} | ||
|
||
fn compute_next_base_fee(base_fee: &BigInt, gas_limit_used: i64, no_of_blocks: usize) -> BigInt { | ||
let mut delta = (PACKING_EFFICIENCY_DENOM * gas_limit_used | ||
/ (no_of_blocks as i64 * PACKING_EFFICIENCY_NUM)) | ||
- BLOCK_GAS_TARGET; | ||
// cap change at 12.5% (BaseFeeMaxChangeDenom) by capping delta | ||
if delta > BLOCK_GAS_TARGET { | ||
delta = BLOCK_GAS_TARGET | ||
} | ||
if delta < -BLOCK_GAS_TARGET { | ||
delta = -BLOCK_GAS_TARGET | ||
} | ||
|
||
let change: BigInt = ((base_fee * delta) / BLOCK_GAS_TARGET) / BASE_FEE_MAX_CHANGE_DENOM; | ||
let mut next_base_fee = base_fee + change; | ||
if next_base_fee < *MINIMUM_BASE_FEE { | ||
next_base_fee = MINIMUM_BASE_FEE.clone(); | ||
} | ||
next_base_fee | ||
} | ||
|
||
pub fn compute_base_fee<DB>(db: &DB, ts: &Tipset) -> Result<BigInt, crate::Error> | ||
where | ||
DB: BlockStore, | ||
{ | ||
let mut total_limit = 0; | ||
let mut seen = HashSet::new(); | ||
|
||
for b in ts.blocks() { | ||
let (msg1, msg2) = crate::block_messages(db, &b)?; | ||
for m in msg1 { | ||
let m_cid = m.cid()?; | ||
if !seen.contains(&m_cid) { | ||
total_limit += m.gas_limit(); | ||
seen.insert(m_cid); | ||
} | ||
} | ||
for m in msg2 { | ||
let m_cid = m.cid()?; | ||
if !seen.contains(&m_cid) { | ||
total_limit += m.gas_limit(); | ||
seen.insert(m_cid); | ||
} | ||
} | ||
} | ||
let parent_base_fee = ts.blocks()[0].parent_base_fee(); | ||
ec2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Ok(compute_next_base_fee( | ||
parent_base_fee, | ||
total_limit, | ||
ts.blocks().len(), | ||
)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
fn construct_tests() -> Vec<(i64, i64, usize, i64)> { | ||
// (base_fee, limit_used, no_of_blocks, output) | ||
let mut cases = Vec::new(); | ||
cases.push((100_000_000, 0, 1, 87_500_000)); | ||
cases.push((100_000_000, 0, 5, 87_500_000)); | ||
cases.push((100_000_000, BLOCK_GAS_TARGET, 1, 103_125_000)); | ||
cases.push((100_000_000, BLOCK_GAS_TARGET * 2, 2, 103_125_000)); | ||
cases.push((100_000_000, BLOCK_GAS_LIMIT * 2, 2, 112_500_000)); | ||
cases.push((100_000_000, BLOCK_GAS_LIMIT * 15 / 10, 2, 110_937_500)); | ||
cases | ||
} | ||
|
||
#[test] | ||
fn run_base_fee_tests() { | ||
let cases = construct_tests(); | ||
|
||
for case in cases { | ||
let output = compute_next_base_fee(&case.0.into(), case.1, case.2); | ||
assert_eq!(BigInt::from(case.3), output); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
pub mod base_fee; | ||
mod chain_store; | ||
mod errors; | ||
mod tip_index; | ||
|
||
pub use self::base_fee::*; | ||
pub use self::chain_store::*; | ||
pub use self::errors::*; | ||
pub use self::tip_index::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be nice to have a function like block_messages which returns ChainMessage because this is one of the cases where we don't care if it is a signed or unsigned message that is returned. We are just worrying about the gas_limit on that particular message.
Edit : This is not necessary to change btw. Was just a comment in passing