Skip to content
Merged
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
63 changes: 4 additions & 59 deletions op-node/l2/util.go → op-node/eth/account_proof.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
package l2
package eth

import (
"bytes"
"errors"
"fmt"
"math/big"

"github.com/ethereum-optimism/optimism/op-node/eth"

"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum/go-ethereum/core/types"

"github.com/ethereum/go-ethereum/ethdb/memorydb"

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

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
)

func ComputeL2OutputRoot(l2OutputRootVersion eth.Bytes32, blockHash common.Hash, blockRoot common.Hash, storageRoot common.Hash) eth.Bytes32 {
var buf bytes.Buffer
buf.Write(l2OutputRootVersion[:])
buf.Write(blockRoot.Bytes())
buf.Write(storageRoot[:])
buf.Write(blockHash.Bytes())
return eth.Bytes32(crypto.Keccak256Hash(buf.Bytes()))
}

type AccountResult struct {
AccountProof []hexutil.Bytes `json:"accountProof"`

Expand Down Expand Up @@ -82,40 +64,3 @@ func (res *AccountResult) Verify(stateRoot common.Hash) error {
}
return err
}

// BlockToBatch converts a L2 block to batch-data.
// Invalid L2 blocks may return an error.
func BlockToBatch(config *rollup.Config, block *types.Block) (*derive.BatchData, error) {
txs := block.Transactions()
if len(txs) == 0 {
return nil, errors.New("expected at least 1 transaction but found none")
}
if typ := txs[0].Type(); typ != types.DepositTxType {
return nil, fmt.Errorf("expected first tx to be a deposit of L1 info, but got type: %d", typ)
}

// encode non-deposit transactions
var opaqueTxs []hexutil.Bytes
for i, tx := range block.Transactions() {
if tx.Type() == types.DepositTxType {
continue
}
otx, err := tx.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("failed to encode tx %d in block: %v", i, err)
}
opaqueTxs = append(opaqueTxs, otx)
}

// figure out which L1 epoch this L2 block was derived from
l1Info, err := derive.L1InfoDepositTxData(txs[0].Data())
if err != nil {
return nil, fmt.Errorf("invalid L1 info deposit tx in block: %v", err)
}
return &derive.BatchData{BatchV1: derive.BatchV1{
EpochNum: rollup.Epoch(l1Info.Number), // the L1 block number equals the L2 epoch.
EpochHash: l1Info.BlockHash,
Timestamp: block.Time(),
Transactions: opaqueTxs,
}}, nil
}
13 changes: 11 additions & 2 deletions op-node/eth/l1info.go → op-node/eth/block_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@ import (
"github.com/ethereum/go-ethereum/common"
)

type L1Info interface {
type BlockInfo interface {
Hash() common.Hash
ParentHash() common.Hash
Coinbase() common.Address
Root() common.Hash // state-root
NumberU64() uint64
Time() uint64
// MixDigest field, reused for randomness after The Merge (Bellatrix hardfork)
MixDigest() common.Hash
BaseFee() *big.Int
ID() BlockID
BlockRef() L1BlockRef
ReceiptHash() common.Hash
}

func InfoToL1BlockRef(info BlockInfo) L1BlockRef {
return L1BlockRef{
Hash: info.Hash(),
Number: info.NumberU64(),
ParentHash: info.ParentHash(),
Time: info.Time(),
}
}
19 changes: 19 additions & 0 deletions op-node/eth/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package eth

type BlockLabel string

const (
// Unsafe is:
// - L1: absolute head of the chain
// - L2: absolute head of the chain, not confirmed on L1
Unsafe = "latest"
// Safe is:
// - L1: Justified checkpoint, beacon chain: 1 epoch of 2/3 of the validators attesting the epoch.
// - L2: Derived chain tip from L1 data
Safe = "safe"
// Finalized is:
// - L1: Finalized checkpoint, beacon chain: 2+ justified epochs with "supermajority link" (see FFG docs).
// More about FFG: https://ethereum.org/en/developers/docs/consensus-mechanisms/pos/gasper/
// - L2: Derived chain tip from finalized L1 data
Finalized = "finalized"
)
196 changes: 0 additions & 196 deletions op-node/l1/source_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions op-node/l2/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ func (s *ReadOnlySource) GetBlockHeader(ctx context.Context, blockTag string) (*
return head, err
}

func (s *ReadOnlySource) GetProof(ctx context.Context, address common.Address, blockTag string) (*AccountResult, error) {
var getProofResponse *AccountResult
func (s *ReadOnlySource) GetProof(ctx context.Context, address common.Address, blockTag string) (*eth.AccountResult, error) {
var getProofResponse *eth.AccountResult
err := s.rpc.CallContext(ctx, &getProofResponse, "eth_getProof", address, []common.Hash{}, blockTag)
if err == nil && getProofResponse == nil {
err = ethereum.NotFound
Expand Down
5 changes: 2 additions & 3 deletions op-node/node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/ethereum-optimism/optimism/op-bindings/predeploys"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/l2"
"github.com/ethereum-optimism/optimism/op-node/metrics"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/driver"
Expand All @@ -23,7 +22,7 @@ import (
type l2EthClient interface {
GetBlockHeader(ctx context.Context, blockTag string) (*types.Header, error)
// GetProof returns a proof of the account, it may return a nil result without error if the address was not found.
GetProof(ctx context.Context, address common.Address, blockTag string) (*l2.AccountResult, error)
GetProof(ctx context.Context, address common.Address, blockTag string) (*eth.AccountResult, error)

BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
L2BlockRefByNumber(ctx context.Context, l2Num *big.Int) (eth.L2BlockRef, error)
Expand Down Expand Up @@ -100,7 +99,7 @@ func (n *nodeAPI) OutputAtBlock(ctx context.Context, number rpc.BlockNumber) ([]
}

var l2OutputRootVersion eth.Bytes32 // it's zero for now
l2OutputRoot := l2.ComputeL2OutputRoot(l2OutputRootVersion, head.Hash(), head.Root, proof.StorageHash)
l2OutputRoot := rollup.ComputeL2OutputRoot(l2OutputRootVersion, head.Hash(), head.Root, proof.StorageHash)

return []eth.Bytes32{l2OutputRootVersion, l2OutputRoot}, nil
}
Expand Down
8 changes: 5 additions & 3 deletions op-node/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

"github.com/ethereum-optimism/optimism/op-node/client"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/l1"
"github.com/ethereum-optimism/optimism/op-node/l2"
"github.com/ethereum-optimism/optimism/op-node/metrics"
"github.com/ethereum-optimism/optimism/op-node/p2p"
"github.com/ethereum-optimism/optimism/op-node/rollup/driver"
"github.com/ethereum-optimism/optimism/op-node/sources"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/event"
Expand All @@ -27,7 +27,7 @@ type OpNode struct {
appVersion string
metrics *metrics.Metrics
l1HeadsSub ethereum.Subscription // Subscription to get L1 heads (automatically re-subscribes on error)
l1Source *l1.Source // Source to fetch data from (also implements the Downloader interface)
l1Source *sources.L1Client // L1 Client to fetch data from
l2Engine *driver.Driver // L2 Engine to Sync
l2Node client.RPC // L2 Execution Engine RPC connections to close at shutdown
l2Client client.Client // L2 client wrapper around eth namespace
Expand Down Expand Up @@ -110,7 +110,9 @@ func (n *OpNode) initL1(ctx context.Context, cfg *Config) error {
return fmt.Errorf("failed to get L1 RPC client: %w", err)
}

n.l1Source, err = l1.NewSource(client.NewInstrumentedRPC(l1Node, n.metrics), n.metrics.L1SourceCache, l1.DefaultConfig(&cfg.Rollup, trustRPC))
n.l1Source, err = sources.NewL1Client(
client.NewInstrumentedRPC(l1Node, n.metrics), n.log, n.metrics.L1SourceCache,
sources.L1ClientDefaultConfig(&cfg.Rollup, trustRPC))
if err != nil {
return fmt.Errorf("failed to create L1 source: %v", err)
}
Expand Down
Loading