Skip to content

Commit

Permalink
Working toward supporting dynamic gas prices
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbertJordan committed Nov 15, 2024
1 parent 6a3e709 commit b43ec08
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions evmcore/dummy_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func ToEvmHeader(block *inter.Block, prevHash common.Hash, rules opera.Rules) *E
Root: block.StateRoot,
Number: big.NewInt(int64(block.Number)),
Time: block.Time,
Duration: time.Duration(block.Duration) * time.Nanosecond,
GasLimit: block.GasLimit,
GasUsed: block.GasUsed,
BaseFee: baseFee,
Expand Down
2 changes: 1 addition & 1 deletion gossip/apply_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *Store) ApplyGenesis(g genesis.Genesis) (err error) {
WithTime(evmcore.FakeGenesisTime-1). // TODO: extend genesis generator to provide time
WithGasLimit(gasLimit).
WithStateRoot(common.Hash{}). // TODO: get proper state root from genesis data
WithBaseFee(big.NewInt(0)). // TODO: set initial base fee according to the rules
WithBaseFee(gasprice.GetInitialBaseFee(rules.Economy)).
Build(),
)

Expand Down
6 changes: 3 additions & 3 deletions gossip/blockproc/evmmodule/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ func TestEvm_IgnoresGasPriceOfInternalTransactions(t *testing.T) {
nil,
nil,
opera.Rules{
Blocks: opera.BlocksRules{
MaxBlockGas: 1e10,
},
Economy: opera.EconomyRules{
MinGasPrice: big.NewInt(12), // > than 0 offered by the internal transactions
},
Upgrades: opera.Upgrades{
London: true,
},
Blocks: opera.BlocksRules{
MaxBlockGas: 1e12,
},
},
&params.ChainConfig{
LondonBlock: big.NewInt(0),
Expand Down
5 changes: 4 additions & 1 deletion gossip/c_block_callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,15 @@ func consensusCallbackBeginBlockFn(
number := uint64(blockCtx.Idx)
lastBlockHeader := evmStateReader.GetHeaderByNumber(number - 1)
maxBlockGas := es.Rules.Blocks.MaxBlockGas
blockDuration := time.Duration(blockCtx.Time - bs.LastBlock.Time)
blockBuilder := inter.NewBlockBuilder().
WithEpoch(es.Epoch).
WithNumber(number).
WithParentHash(lastBlockHeader.Hash).
WithTime(blockCtx.Time).
WithPrevRandao(prevRandao).
WithGasLimit(maxBlockGas)
WithGasLimit(maxBlockGas).
WithDuration(blockDuration)

for i := range preInternalTxs {
blockBuilder.AddTransaction(
Expand Down Expand Up @@ -348,6 +350,7 @@ func consensusCallbackBeginBlockFn(

block := blockBuilder.Build()
evmBlock.Hash = block.Hash()
evmBlock.Duration = blockDuration

for _, tx := range blockBuilder.GetTransactions() {
store.evm.SetTx(tx.Hash(), tx)
Expand Down
4 changes: 2 additions & 2 deletions gossip/c_llr_callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (s *Store) WriteFullBlockRecord(baseFee *big.Int, blobGasPrice *big.Int, ga
WithGasLimit(gasLimit).
WithGasUsed(br.GasUsed).
WithBaseFee(baseFee).
WithPrevRandao(common.Hash{1})
// TODO: add duration
WithPrevRandao(common.Hash{1}).
WithDuration(duration)

for i := range br.Txs {
copy := types.Receipt(*br.Receipts[i])
Expand Down
20 changes: 19 additions & 1 deletion inter/block.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package inter

import (
"encoding/binary"
"math/big"
"slices"
"time"
"unsafe"

"github.com/Fantom-foundation/lachesis-base/inter/idx"
Expand Down Expand Up @@ -43,6 +45,10 @@ type Block struct {
// Fields required for linking the block internally to a lachesis epoch.
Epoch idx.Epoch

// The duration of this block, being the difference between the predecessor
// block's timestamp and this block's timestamp, in nanoseconds.
Duration uint64

// The hash of this block, cached on first access.
hash common.Hash
}
Expand All @@ -57,6 +63,10 @@ func (b *Block) Hash() common.Hash {

// GetEthereumHeader returns the Ethereum header corresponding to this block.
func (b *Block) GetEthereumHeader() *types.Header {
// TODO: consider condensing the extra data into a single 8-byte field.
extra := make([]byte, 16)
binary.BigEndian.PutUint64(extra[:8], uint64(b.Time)) // < only nano-second part needed
binary.BigEndian.PutUint64(extra[8:], uint64(b.Duration)) // < could be measured in microseconds instead of nanoseconds
return &types.Header{
ParentHash: b.ParentHash,
UncleHash: types.EmptyUncleHash,
Expand All @@ -70,7 +80,7 @@ func (b *Block) GetEthereumHeader() *types.Header {
GasLimit: b.GasLimit,
GasUsed: b.GasUsed,
Time: uint64(b.Time.Time().Unix()),
Extra: nil, // TODO: fill in extra data required for gas computation
Extra: extra,
MixDigest: b.PrevRandao,
Nonce: types.BlockNonce{}, // constant 0 in Ethereum
BaseFee: b.BaseFee,
Expand Down Expand Up @@ -137,6 +147,14 @@ func (b *BlockBuilder) WithTime(time Timestamp) *BlockBuilder {
return b
}

func (b *BlockBuilder) WithDuration(duration time.Duration) *BlockBuilder {
if duration < 0 {
duration = 0
}
b.block.Duration = uint64(duration.Nanoseconds())
return b
}

func (b *BlockBuilder) WithDifficulty(difficulty uint64) *BlockBuilder {
b.block.Difficulty = difficulty
return b
Expand Down

0 comments on commit b43ec08

Please sign in to comment.