Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion consensus/dummy/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func verifyHeaderGasFields(config *extras.ChainConfig, header *types.Header, par
}

// Verify header.BaseFee matches the expected value.
expectedBaseFee, err := customheader.BaseFee(config, feeConfig, parent, header.Time)
timeMS := customtypes.HeaderTimeMilliseconds(header)
expectedBaseFee, err := customheader.BaseFee(config, feeConfig, parent, timeMS)
if err != nil {
return fmt.Errorf("failed to calculate base fee: %w", err)
}
Expand Down
9 changes: 5 additions & 4 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,18 @@ func GenerateChainWithGenesis(genesis *Genesis, engine consensus.Engine, n int,

func (cm *chainMaker) makeHeader(parent *types.Block, gap uint64, state *state.StateDB, engine consensus.Engine) *types.Header {
time := parent.Time() + gap // block time is fixed at [gap] seconds
timeMS := customtypes.HeaderTimeMilliseconds(parent.Header()) + gap*1000

feeConfig, _, err := cm.GetFeeConfigAt(parent.Header())
if err != nil {
panic(err)
}
config := params.GetExtra(cm.config)
gasLimit, err := customheader.GasLimit(config, feeConfig, parent.Header(), time)
gasLimit, err := customheader.GasLimit(config, feeConfig, parent.Header(), timeMS)
if err != nil {
panic(err)
}
baseFee, err := customheader.BaseFee(config, feeConfig, parent.Header(), time)
baseFee, err := customheader.BaseFee(config, feeConfig, parent.Header(), timeMS)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -420,8 +422,7 @@ func (cm *chainMaker) makeHeader(parent *types.Block, gap uint64, state *state.S

if config.IsGranite(header.Time) {
headerExtra := customtypes.GetHeaderExtra(header)
timeMilliseconds := header.Time * 1000 // convert to milliseconds
headerExtra.TimeMilliseconds = &timeMilliseconds
headerExtra.TimeMilliseconds = &timeMS
}
return header
}
Expand Down
19 changes: 10 additions & 9 deletions core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/ava-labs/subnet-evm/plugin/evm/customheader"
"github.com/ava-labs/subnet-evm/plugin/evm/customtypes"
"github.com/ava-labs/subnet-evm/plugin/evm/upgrade/legacy"
"github.com/ava-labs/subnet-evm/utils"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"
)
Expand Down Expand Up @@ -357,14 +358,16 @@ func TestStateProcessorErrors(t *testing.T) {
// - valid pow (fake), ancestry, difficulty, gaslimit etc
func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Transactions, config *params.ChainConfig) *types.Block {
fakeChainReader := newChainMaker(nil, config, engine)
time := parent.Time() + 10
feeConfig, _, err := fakeChainReader.GetFeeConfigAt(parent.Header())
if err != nil {
panic(err)
}
configExtra := params.GetExtra(config)
gasLimit, _ := customheader.GasLimit(configExtra, feeConfig, parent.Header(), time)
baseFee, _ := customheader.BaseFee(configExtra, feeConfig, parent.Header(), time)
gap := uint64(10) // 10 seconds
time := parent.Time() + gap
timeMS := customtypes.HeaderTimeMilliseconds(parent.Header()) + gap*1000
gasLimit, _ := customheader.GasLimit(configExtra, feeConfig, parent.Header(), timeMS)
baseFee, _ := customheader.BaseFee(configExtra, feeConfig, parent.Header(), timeMS)
header := &types.Header{
ParentHash: parent.Hash(),
Coinbase: parent.Coinbase(),
Expand All @@ -380,6 +383,10 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
UncleHash: types.EmptyUncleHash,
BaseFee: baseFee,
}
if configExtra.IsGranite(header.Time) {
headerExtra := customtypes.GetHeaderExtra(header)
headerExtra.TimeMilliseconds = utils.NewUint64(timeMS)
}

if params.GetExtra(config).IsSubnetEVM(header.Time) {
headerExtra := customtypes.GetHeaderExtra(header)
Expand Down Expand Up @@ -418,12 +425,6 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
header.ParentBeaconRoot = new(common.Hash)
}

if configExtra.IsGranite(header.Time) {
headerExtra := customtypes.GetHeaderExtra(header)
headerExtra.TimeMilliseconds = new(uint64)
*headerExtra.TimeMilliseconds = header.Time * 1000
}

// Assemble and return the final block for sealing
return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil))
}
15 changes: 13 additions & 2 deletions core/txpool/blobpool/blobpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"github.com/ava-labs/subnet-evm/core/txpool"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/plugin/evm/customheader"
"github.com/ava-labs/subnet-evm/plugin/evm/customtypes"
"github.com/ava-labs/subnet-evm/plugin/evm/upgrade/legacy"
"github.com/ava-labs/subnet-evm/plugin/evm/upgrade/subnetevm"
"github.com/holiman/billy"
Expand All @@ -76,6 +77,7 @@ var testChainConfig *params.ChainConfig

func init() {
params.RegisterExtras()
customtypes.Register()

testChainConfig = new(params.ChainConfig)
*testChainConfig = params.Copy(params.TestChainConfig)
Expand Down Expand Up @@ -123,8 +125,12 @@ func (bc *testBlockChain) CurrentBlock() *types.Header {
Extra: make([]byte, subnetevm.WindowSize),
}
config := params.GetExtra(bc.config)
timeMS := blockTime * 1000
if config.IsGranite(blockTime) {
customtypes.GetHeaderExtra(parent).TimeMilliseconds = &timeMS
}
baseFee, err := customheader.BaseFee(
config, config.FeeConfig, parent, blockTime,
config, config.FeeConfig, parent, timeMS,
)
if err != nil {
panic(err)
Expand Down Expand Up @@ -154,14 +160,19 @@ func (bc *testBlockChain) CurrentBlock() *types.Header {
}
excessBlobGas := lo.Uint64()

return &types.Header{
head := &types.Header{
Number: blockNumber,
Time: blockTime,
GasLimit: gasLimit,
BaseFee: baseFee,
ExcessBlobGas: &excessBlobGas,
Extra: make([]byte, subnetevm.WindowSize),
}
if params.GetExtra(bc.config).IsGranite(blockTime) {
timeMS := blockTime * 1000
customtypes.GetHeaderExtra(head).TimeMilliseconds = &timeMS
}
return head
}

func (bc *testBlockChain) CurrentFinalBlock() *types.Header {
Expand Down
8 changes: 7 additions & 1 deletion core/txpool/legacypool/legacypool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ import (
"github.com/ava-labs/subnet-evm/core"
"github.com/ava-labs/subnet-evm/core/txpool"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/plugin/evm/customtypes"
"github.com/holiman/uint256"
)

func TestMain(m *testing.M) {
params.RegisterExtras()
customtypes.Register()
os.Exit(m.Run())
}

Expand Down Expand Up @@ -110,10 +112,14 @@ func (bc *testBlockChain) CurrentBlock() *types.Header {
bc.lock.Lock()
defer bc.lock.Unlock()

return &types.Header{
header := &types.Header{
Number: new(big.Int),
GasLimit: bc.gasLimit.Load(),
}
if params.GetExtra(bc.config).IsGranite(0) {
customtypes.GetHeaderExtra(header).TimeMilliseconds = new(uint64)
}
return header
}

func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
Expand Down
7 changes: 4 additions & 3 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte
if err != nil {
return nil, err
}
gasLimit, err := customheader.GasLimit(chainExtra, feeConfig, parent, header.Time)
gasLimit, err := customheader.GasLimit(chainExtra, feeConfig, parent, timestampMS)
if err != nil {
return nil, fmt.Errorf("calculating new gas limit: %w", err)
}
header.GasLimit = gasLimit

baseFee, err := customheader.BaseFee(chainExtra, feeConfig, parent, header.Time)
baseFee, err := customheader.BaseFee(chainExtra, feeConfig, parent, timestampMS)
if err != nil {
return nil, fmt.Errorf("failed to calculate new base fee: %w", err)
}
Expand Down Expand Up @@ -287,7 +287,8 @@ func (w *worker) createCurrentEnvironment(predicateContext *precompileconfig.Pre
return nil, err
}
chainConfig := params.GetExtra(w.chainConfig)
capacity, err := customheader.GasCapacity(chainConfig, feeConfig, parent, header.Time)
timeMS := customtypes.HeaderTimeMilliseconds(header)
capacity, err := customheader.GasCapacity(chainConfig, feeConfig, parent, timeMS)
if err != nil {
return nil, fmt.Errorf("calculating gas capacity: %w", err)
}
Expand Down
6 changes: 4 additions & 2 deletions plugin/evm/customheader/base_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func BaseFee(
config *extras.ChainConfig,
feeConfig commontype.FeeConfig,
parent *types.Header,
timestamp uint64,
timeMS uint64,
) (*big.Int, error) {
timestamp := timeMS / 1000
switch {
case config.IsSubnetEVM(timestamp):
return baseFeeFromWindow(config, feeConfig, parent, timestamp)
Expand Down Expand Up @@ -53,5 +54,6 @@ func EstimateNextBaseFee(
}

timestamp = max(timestamp, parent.Time, *config.SubnetEVMTimestamp)
return BaseFee(config, feeConfig, parent, timestamp)
timeMS := timestamp * 1000
return BaseFee(config, feeConfig, parent, timeMS)
}
60 changes: 42 additions & 18 deletions plugin/evm/customheader/base_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func TestBaseFee(t *testing.T) {

func BaseFeeTest(t *testing.T, feeConfig commontype.FeeConfig) {
tests := []struct {
name string
upgrades extras.NetworkUpgrades
parent *types.Header
timestamp uint64
want *big.Int
wantErr error
name string
upgrades extras.NetworkUpgrades
parent *types.Header
timeMS uint64
want *big.Int
wantErr error
}{
{
name: "pre_subnet_evm",
Expand All @@ -53,8 +53,8 @@ func BaseFeeTest(t *testing.T, feeConfig commontype.FeeConfig) {
parent: &types.Header{
Number: big.NewInt(1),
},
timestamp: 1,
want: big.NewInt(feeConfig.MinBaseFee.Int64()),
timeMS: 1000,
want: big.NewInt(feeConfig.MinBaseFee.Int64()),
},
{
name: "subnet_evm_genesis_block",
Expand All @@ -80,8 +80,8 @@ func BaseFeeTest(t *testing.T, feeConfig commontype.FeeConfig) {
Time: 1,
Extra: (&subnetevm.Window{}).Bytes(),
},
timestamp: 0,
wantErr: errInvalidTimestamp,
timeMS: 0,
wantErr: errInvalidTimestamp,
},
{
name: "subnet_evm_no_change",
Expand All @@ -93,8 +93,8 @@ func BaseFeeTest(t *testing.T, feeConfig commontype.FeeConfig) {
Extra: (&subnetevm.Window{}).Bytes(),
BaseFee: big.NewInt(feeConfig.MinBaseFee.Int64() + 1),
},
timestamp: 1,
want: big.NewInt(feeConfig.MinBaseFee.Int64() + 1),
timeMS: 1000,
want: big.NewInt(feeConfig.MinBaseFee.Int64() + 1),
},
{
name: "subnet_evm_small_decrease",
Expand All @@ -104,7 +104,7 @@ func BaseFeeTest(t *testing.T, feeConfig commontype.FeeConfig) {
Extra: (&subnetevm.Window{}).Bytes(),
BaseFee: big.NewInt(maxBaseFee),
},
timestamp: 1,
timeMS: 1000,
want: func() *big.Int {
var (
gasTarget = feeConfig.TargetGas.Int64()
Expand All @@ -127,7 +127,7 @@ func BaseFeeTest(t *testing.T, feeConfig commontype.FeeConfig) {
Extra: (&subnetevm.Window{}).Bytes(),
BaseFee: big.NewInt(maxBaseFee),
},
timestamp: 2 * subnetevm.WindowLen,
timeMS: 2 * 1000 * subnetevm.WindowLen,
want: func() *big.Int {
var (
gasTarget = feeConfig.TargetGas.Int64()
Expand All @@ -152,7 +152,7 @@ func BaseFeeTest(t *testing.T, feeConfig commontype.FeeConfig) {
Extra: (&subnetevm.Window{}).Bytes(),
BaseFee: big.NewInt(feeConfig.MinBaseFee.Int64()),
},
timestamp: 1,
timeMS: 1000,
want: func() *big.Int {
var (
gasTarget = feeConfig.TargetGas.Int64()
Expand All @@ -176,8 +176,32 @@ func BaseFeeTest(t *testing.T, feeConfig commontype.FeeConfig) {
Extra: (&subnetevm.Window{}).Bytes(),
BaseFee: big.NewInt(1),
},
timestamp: 2 * subnetevm.WindowLen,
want: big.NewInt(feeConfig.MinBaseFee.Int64()),
timeMS: 2 * 1000 * subnetevm.WindowLen,
want: big.NewInt(feeConfig.MinBaseFee.Int64()),
},
{
name: "granite_rounds_seconds",
upgrades: extras.TestGraniteChainConfig.NetworkUpgrades,
parent: &types.Header{
Number: big.NewInt(1),
Extra: (&subnetevm.Window{}).Bytes(),
BaseFee: big.NewInt(maxBaseFee),
},
timeMS: 2*1000*subnetevm.WindowLen + 999,
want: func() *big.Int {
var (
gasTarget = feeConfig.TargetGas.Int64()
gasUsed = int64(0)
amountUnderTarget = gasTarget - gasUsed
parentBaseFee = int64(maxBaseFee)
smoothingFactor = feeConfig.BaseFeeChangeDenominator.Int64()
baseFeeFractionUnderTarget = amountUnderTarget * parentBaseFee / gasTarget
windowsElapsed = int64(2)
delta = windowsElapsed * baseFeeFractionUnderTarget / smoothingFactor
baseFee = parentBaseFee - delta
)
return big.NewInt(baseFee)
}(),
},
}
for _, test := range tests {
Expand All @@ -187,7 +211,7 @@ func BaseFeeTest(t *testing.T, feeConfig commontype.FeeConfig) {
config := &extras.ChainConfig{
NetworkUpgrades: test.upgrades,
}
got, err := BaseFee(config, feeConfig, test.parent, test.timestamp)
got, err := BaseFee(config, feeConfig, test.parent, test.timeMS)
require.ErrorIs(err, test.wantErr)
require.Equal(test.want, got)

Expand Down
11 changes: 7 additions & 4 deletions plugin/evm/customheader/gas_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ava-labs/subnet-evm/commontype"
"github.com/ava-labs/subnet-evm/params/extras"
"github.com/ava-labs/subnet-evm/plugin/evm/customtypes"

ethparams "github.com/ava-labs/libevm/params"
)
Expand All @@ -29,8 +30,9 @@ func GasLimit(
config *extras.ChainConfig,
feeConfig commontype.FeeConfig,
parent *types.Header,
timestamp uint64,
timeMS uint64,
) (uint64, error) {
timestamp := timeMS / 1000
switch {
case config.IsSubnetEVM(timestamp):
return feeConfig.GasLimit.Uint64(), nil
Expand All @@ -52,7 +54,8 @@ func VerifyGasUsed(
header *types.Header,
) error {
gasUsed := header.GasUsed
capacity, err := GasCapacity(config, feeConfig, parent, header.Time)
timeMS := customtypes.HeaderTimeMilliseconds(header)
capacity, err := GasCapacity(config, feeConfig, parent, timeMS)
if err != nil {
return fmt.Errorf("calculating gas capacity: %w", err)
}
Expand Down Expand Up @@ -114,7 +117,7 @@ func GasCapacity(
config *extras.ChainConfig,
feeConfig commontype.FeeConfig,
parent *types.Header,
timestamp uint64,
timeMS uint64,
) (uint64, error) {
return GasLimit(config, feeConfig, parent, timestamp)
return GasLimit(config, feeConfig, parent, timeMS)
}
Loading