Skip to content
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

[Flow EVM] Genesis block update #6284

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 1 addition & 6 deletions fvm/evm/handler/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handler

import (
"fmt"
"time"

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

Expand Down Expand Up @@ -67,14 +66,10 @@ func (bs *BlockStore) BlockProposal() (*types.BlockProposal, error) {
return nil, err
}

// cadence block timestamp is unix nanoseconds but evm blocks
// expect timestamps in unix seconds so we convert here
timestamp := uint64(cadenceBlock.Timestamp / int64(time.Second))

blockProposal := types.NewBlockProposal(
parentHash,
lastExecutedBlock.Height+1,
timestamp,
uint64(cadenceBlock.Timestamp),
lastExecutedBlock.TotalSupply,
)
return blockProposal, nil
Expand Down
9 changes: 7 additions & 2 deletions fvm/evm/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"math/big"
"time"

"github.com/onflow/cadence/runtime/common"
gethCommon "github.com/onflow/go-ethereum/common"
Expand Down Expand Up @@ -461,10 +462,14 @@ func (h *ContractHandler) getBlockContext() (types.BlockContext, error) {
return types.BlockContext{}, err
}

// block timestamp is unix nanoseconds but evm blocks
// expect timestamps in unix seconds so we convert here
timestamp := bp.Timestamp / uint64(time.Second)

return types.BlockContext{
ChainID: types.EVMChainIDFromFlowChainID(h.flowChainID),
BlockNumber: bp.Height,
BlockTimestamp: bp.Timestamp,
BlockTimestamp: timestamp,
DirectCallBaseGasUsage: types.DefaultDirectCallBaseGasUsage,
GetHashFunc: func(n uint64) gethCommon.Hash {
hash, err := h.blockStore.BlockHash(n)
Expand All @@ -474,7 +479,7 @@ func (h *ContractHandler) getBlockContext() (types.BlockContext, error) {
ExtraPrecompiledContracts: h.precompiledContracts,
Random: rand,
Tracer: h.tracer.TxTracer(),
TxCountSoFar: uint(len(bp.TxHashes)),
TxCountSoFar: uint(bp.TransactionCount),
TotalGasUsedSoFar: bp.TotalGasUsed,
}, nil
}
Expand Down
66 changes: 54 additions & 12 deletions fvm/evm/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"bytes"
"math/big"
"time"

gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
Expand All @@ -20,7 +21,7 @@ type Block struct {
// Height returns the height of this block
Height uint64

// Timestamp is a Unix timestamp in seconds at which the block was created
// Timestamp is a Unix timestamp in nanoseconds at which the block was created
// Note that this value must be provided from the FVM Block
Timestamp uint64

Expand All @@ -41,8 +42,11 @@ type Block struct {
// values as node values. Proofs are still compatible but might require an extra hashing step.
TransactionHashRoot gethCommon.Hash

// stores gas used by all transactions included in the block.
// TotalGasUsed stores the sum of gas used by all transactions included in the block
TotalGasUsed uint64

// TransactionCount stores the total num of transactions
TransactionCount uint32
}

// ToBytes encodes the block into bytes
Expand Down Expand Up @@ -87,12 +91,18 @@ func NewBlockFromBytes(encoded []byte) (*Block, error) {
return res, nil
}

var GenesisTime = time.Date(2024, time.August, 1, 0, 0, 0, 0, time.UTC)

// GenesisBlock is the genesis block in the EVM environment
var GenesisBlock = &Block{
ParentBlockHash: gethCommon.Hash{},
Height: uint64(0),
TotalSupply: new(big.Int),
ReceiptRoot: gethTypes.EmptyRootHash,
ParentBlockHash: gethCommon.Hash{},
Height: uint64(0),
Timestamp: uint64(GenesisTime.Unix()),
TotalSupply: new(big.Int),
ReceiptRoot: gethTypes.EmptyRootHash,
TransactionHashRoot: gethTypes.EmptyRootHash,
TransactionCount: 0,
TotalGasUsed: 0,
}

var GenesisBlockHash, _ = GenesisBlock.Hash()
Expand Down Expand Up @@ -123,6 +133,7 @@ func (b *BlockProposal) AppendTransaction(res *Result) {
}
b.Receipts = append(b.Receipts, *r)
b.TotalGasUsed = r.CumulativeGasUsed
b.TransactionCount += 1
}

// PopulateRoots populates receiptRoot and transactionHashRoot
Expand Down Expand Up @@ -282,6 +293,23 @@ type blockV6 struct {
TotalGasUsed uint64
}

// removes TransactionHashes
// and adds TransactionHashRoot

type blockV7 struct {
ParentBlockHash gethCommon.Hash
Height uint64
Timestamp uint64
TotalSupply *big.Int
ReceiptRoot gethCommon.Hash
TransactionHashRoot gethCommon.Hash
TotalGasUsed uint64
}

func secondsToNanoSeconds(t uint64) uint64 {
return t * uint64(time.Second)
}

// decodeBlockBreakingChanges will try to decode the bytes into all
// previous versions of block type, if it succeeds it will return the
// migrated block, otherwise it will return nil.
Expand Down Expand Up @@ -341,7 +369,7 @@ func decodeBlockBreakingChanges(encoded []byte) *Block {
return &Block{
ParentBlockHash: b5.ParentBlockHash,
Height: b5.Height,
Timestamp: b5.Timestamp,
Timestamp: secondsToNanoSeconds(b5.Timestamp),
TotalSupply: b5.TotalSupply,
ReceiptRoot: b5.ReceiptRoot,
}
Expand All @@ -350,11 +378,25 @@ func decodeBlockBreakingChanges(encoded []byte) *Block {
b6 := &blockV6{}
if err := gethRLP.DecodeBytes(encoded, b6); err == nil {
return &Block{
ParentBlockHash: b5.ParentBlockHash,
Height: b5.Height,
Timestamp: b5.Timestamp,
TotalSupply: b5.TotalSupply,
ReceiptRoot: b5.ReceiptRoot,
ParentBlockHash: b6.ParentBlockHash,
Height: b6.Height,
Timestamp: secondsToNanoSeconds(b6.Timestamp),
TotalSupply: b6.TotalSupply,
ReceiptRoot: b6.ReceiptRoot,
TotalGasUsed: b6.TotalGasUsed,
}
}

b7 := &blockV7{}
if err := gethRLP.DecodeBytes(encoded, b7); err == nil {
return &Block{
ParentBlockHash: b7.ParentBlockHash,
Height: b7.Height,
Timestamp: secondsToNanoSeconds(b7.Timestamp),
TotalSupply: b7.TotalSupply,
ReceiptRoot: b7.ReceiptRoot,
TotalGasUsed: b7.TotalGasUsed,
TransactionHashRoot: b7.TransactionHashRoot,
}
}

Expand Down
Loading