Skip to content

Commit

Permalink
Merge pull request #21 from vulcanize/consensus_tests
Browse files Browse the repository at this point in the history
Consensus tests
  • Loading branch information
i-norden authored Dec 13, 2019
2 parents d15006b + 42fcd79 commit 5dd38c3
Show file tree
Hide file tree
Showing 31 changed files with 5,783 additions and 973 deletions.
29 changes: 16 additions & 13 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,24 +300,27 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.StateDB) ([]byte, uint64, bool, error) {
// Ensure message is initialized properly.
// EIP1559 guards
// If we have finalized EIP1559 and do not have a properly formed EIP1559 trx, sub in default values
if b.config.IsEIP1559Finalized(block.Number()) && (call.GasPremium == nil || call.FeeCap == nil || call.GasPrice != nil) {
return nil, 0, false, core.ErrTxNotEIP1559
call.GasPremium = big.NewInt(1)
call.FeeCap = big.NewInt(10)
call.GasPrice = nil
}
// If we have not activated EIP1559 and do not have a properly formed legacy trx, sub in default values
if !b.config.IsEIP1559(block.Number()) && (call.GasPremium != nil || call.FeeCap != nil || call.GasPrice == nil) {
return nil, 0, false, core.ErrTxIsEIP1559
}
if call.GasPrice != nil && (call.GasPremium != nil || call.FeeCap != nil) {
return nil, 0, false, core.ErrTxSetsLegacyAndEIP1559Fields
}
if call.FeeCap != nil && call.GasPremium == nil {
return nil, 0, false, errors.New("if FeeCap is set, GasPremium must be set")
}
if call.GasPremium != nil && call.FeeCap == nil {
return nil, 0, false, errors.New("if GasPremium is set, FeeCap must be set")
}
if call.GasPrice == nil && call.GasPremium == nil {
call.GasPremium = nil
call.FeeCap = nil
call.GasPrice = big.NewInt(1)
}
// If we are in between activation and finalization
if b.config.IsEIP1559(block.Number()) && !b.config.IsEIP1559Finalized(block.Number()) {
// and we have neither a properly formed legacy or EIP1559 transaction, sub in default legacy values
if (call.GasPremium == nil || call.FeeCap == nil && call.GasPrice == nil) || (call.GasPremium != nil || call.FeeCap != nil && call.GasPrice != nil) {
call.GasPremium = nil
call.FeeCap = nil
call.GasPrice = big.NewInt(1)
}
}
if call.Gas == 0 {
call.Gas = 50000000
}
Expand Down
4 changes: 2 additions & 2 deletions core/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func genValueTx(nbytes int) func(int, *BlockGen) {
toaddr := common.Address{}
data := make([]byte, nbytes)
gas, _ := IntrinsicGas(data, false, false, false)
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(benchRootAddr), toaddr, big.NewInt(1), gas, nil, data, nil, nil), types.HomesteadSigner{}, benchRootKey)
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(benchRootAddr), toaddr, big.NewInt(1), gas, new(big.Int), data, nil, nil), types.HomesteadSigner{}, benchRootKey)
gen.AddTx(tx)
}
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func genTxRing(naccounts int) func(int, *BlockGen) {
ringAddrs[to],
benchRootFunds,
params.TxGas,
nil,
new(big.Int),
nil,
nil,
nil,
Expand Down
25 changes: 11 additions & 14 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"

"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -148,14 +147,10 @@ func CalcGasLimitAndBaseFee(config *params.ChainConfig, parent *types.Block, gas
return calcGasLimitAndBaseFee(config, parent)
}

// start at 50 : 50 and then shift to 0 : 100
// calcGasLimitAndBaseFee returns the EIP1559GasLimit and the BaseFee
// Start at 50 : 50 and then shift to 0 : 100
// The GasLimit for the legacy pool is (params.MaxGasEIP1559 - EIP1559GasLimit)
func calcGasLimitAndBaseFee(config *params.ChainConfig, parent *types.Block) (uint64, *big.Int) {
// panic if we do not have a block number set for EIP1559 activation
if config.EIP1559Block == nil {
panic("chain config is missing EIP1559Block")
}
height := new(big.Int).Add(parent.Number(), common.Big1)

// If we are at the block of EIP1559 activation then the BaseFee is set to the initial value
Expand All @@ -164,31 +159,33 @@ func calcGasLimitAndBaseFee(config *params.ChainConfig, parent *types.Block) (ui
return params.MaxGasEIP1559 / 2, new(big.Int).SetUint64(params.EIP1559InitialBaseFee)
}

/*
Otherwise, calculate the BaseFee
As a default strategy, miners set BASEFEE as follows. Let delta = block.gas_used - TARGET_GASUSED (possibly negative).
Set BASEFEE = PARENT_BASEFEE + PARENT_BASEFEE * delta // TARGET_GASUSED // BASEFEE_MAX_CHANGE_DENOMINATOR,
clamping this result inside of the allowable bounds if needed (with the parameter setting above clamping will not be required).
*/
// Otherwise, calculate the BaseFee
// As a default strategy, miners set BASEFEE as follows. Let delta = block.gas_used - TARGET_GASUSED (possibly negative).
// Set BASEFEE = PARENT_BASEFEE + PARENT_BASEFEE * delta // TARGET_GASUSED // BASEFEE_MAX_CHANGE_DENOMINATOR,

delta := new(big.Int).Sub(new(big.Int).SetUint64(parent.GasUsed()), new(big.Int).SetUint64(params.TargetGasUsed))
mul := new(big.Int).Mul(parent.BaseFee(), delta)
div := new(big.Int).Div(mul, new(big.Int).SetUint64(params.TargetGasUsed))
div2 := new(big.Int).Div(div, new(big.Int).SetUint64(params.BaseFeeMaxChangeDenominator))
baseFee := new(big.Int).Add(parent.BaseFee(), div2)

// Panic is the BaseFee is not valid
// A valid BASEFEE is one such that abs(BASEFEE - PARENT_BASEFEE) <= max(1, PARENT_BASEFEE // BASEFEE_MAX_CHANGE_DENOMINATOR)
diff := new(big.Int).Sub(baseFee, parent.BaseFee())
neg := false
if diff.Sign() < 0 {
neg = true
diff.Neg(diff)
}
max := new(big.Int).Div(parent.BaseFee(), new(big.Int).SetUint64(params.BaseFeeMaxChangeDenominator))
if max.Cmp(common.Big1) < 0 {
max = common.Big1
}
// If derived BaseFee is not valid, restrict it within the bounds
if diff.Cmp(max) > 0 {
panic("invalid BaseFee")
if neg {
max.Neg(max)
}
baseFee.Set(new(big.Int).Add(parent.BaseFee(), max))
}

// If EIP1559 is finalized, our limit for the EIP1559 pool is the entire max limit
Expand Down
Loading

0 comments on commit 5dd38c3

Please sign in to comment.