l2

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2022 License: MIT, MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DepositEventABI     = "TransactionDeposited(address,address,uint256,uint256,uint256,bool,bytes)"
	DepositEventABIHash = crypto.Keccak256Hash([]byte(DepositEventABI))
	DepositContractAddr = common.HexToAddress("0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001")
	L1InfoFuncSignature = "setL1BlockValues(uint256 _number, uint256 _timestamp, uint256 _basefee, bytes32 _hash)"
	L1InfoFuncBytes4    = crypto.Keccak256([]byte(L1InfoFuncSignature))[:4]
	L1InfoPredeployAddr = common.HexToAddress("0x4242424242424242424242424242424242424242")
)
View Source
var WrongChainErr = errors.New("wrong chain")

Functions

func CheckReceipts

func CheckReceipts(block ReceiptHash, receipts []*types.Receipt) bool

CheckReceipts sanity checks that the receipts are consistent with the block data.

func DeriveL1InfoDeposit

func DeriveL1InfoDeposit(block L1Info) *types.DepositTx

func DeriveUserDeposits

func DeriveUserDeposits(height uint64, receipts []*types.Receipt) ([]*types.DepositTx, error)

DeriveL2Transactions transforms a L1 block and corresponding receipts into the transaction inputs for a full L2 block

func DriverStep

func DriverStep(ctx context.Context, log log.Logger, rpc DriverAPI,
	dl Downloader, l1Input eth.BlockID, l2Parent eth.BlockID, l2Finalized common.Hash) (out eth.BlockID, err error)

func Execute

func Execute(ctx context.Context, rpc DriverAPI, payload *ExecutionPayload) error

func FindSyncStart

func FindSyncStart(ctx context.Context, reference SyncReference, genesis *Genesis) (nextRefL1, refL2 eth.BlockID, err error)

FindSyncStart finds nextRefL1: the L1 block needed next for sync, to derive into a L2 block on top of refL2. If the L1 reorgs then this will find the common history to build on top of and then follow the first step of the reorg.

func ForkchoiceUpdate

func ForkchoiceUpdate(ctx context.Context, rpc DriverAPI, l2BlockHash common.Hash, l2Finalized common.Hash) error

func NewDriverLoop

func NewDriverLoop(ctx context.Context, state StateMachine, log log.Logger, l1Heads <-chan eth.HeadSignal, driver Driver) func(quit <-chan struct{}) error

func ParseBlockReferences

func ParseBlockReferences(refL2Block Block, genesis *Genesis) (refL1 eth.BlockID, refL2 eth.BlockID, parentL2 common.Hash, err error)

ParseBlockReferences takes a L2 block and determines which L1 block it was derived from, and the L2 self and parent id.

func ParseL1InfoDepositTxData

func ParseL1InfoDepositTxData(data []byte) (nr uint64, time uint64, baseFee *big.Int, blockHash common.Hash, err error)

ParseL1InfoDepositTxData is the inverse of DeriveL1InfoDeposit, to see where the L2 chain is derived from

func UnmarshalLogEvent

func UnmarshalLogEvent(blockNum uint64, txIndex uint64, ev *types.Log) (*types.DepositTx, error)

UnmarshalLogEvent decodes an EVM log entry emitted by the deposit contract into typed deposit data.

parse log data for:

 event TransactionDeposited(
	 address indexed from,
	 address indexed to,
   uint256 mint,
	 uint256 value,
	 uint256 gasLimit,
	 bool isCreation,
	 data data
 );

Deposits additionally get:

  • blockNum matching the L1 block height
  • txIndex: matching the deposit index, not L1 transaction index, since there can be multiple deposits per L1 tx

Types

type Block

type Block interface {
	Hash() common.Hash
	NumberU64() uint64
	ParentHash() common.Hash
	Transactions() types.Transactions
}

type BlockInput

type BlockInput interface {
	ReceiptHash
	L1Info
	MixDigest() common.Hash
}

type BlockPreparer

type BlockPreparer interface {
	GetPayload(ctx context.Context, payloadId PayloadID) (*ExecutionPayload, error)
	ForkchoiceUpdated(ctx context.Context, state *ForkchoiceState, attr *PayloadAttributes) (ForkchoiceUpdatedResult, error)
}

type Bytes256

type Bytes256 [256]byte

func (Bytes256) MarshalText

func (b Bytes256) MarshalText() ([]byte, error)

func (Bytes256) String

func (b Bytes256) String() string

func (*Bytes256) UnmarshalJSON

func (b *Bytes256) UnmarshalJSON(text []byte) error

func (*Bytes256) UnmarshalText

func (b *Bytes256) UnmarshalText(text []byte) error

type Bytes32

type Bytes32 [32]byte

func (Bytes32) MarshalText

func (b Bytes32) MarshalText() ([]byte, error)

func (Bytes32) String

func (b Bytes32) String() string

func (*Bytes32) UnmarshalJSON

func (b *Bytes32) UnmarshalJSON(text []byte) error

func (*Bytes32) UnmarshalText

func (b *Bytes32) UnmarshalText(text []byte) error

type BytesMax32

type BytesMax32 []byte

func (BytesMax32) MarshalText

func (b BytesMax32) MarshalText() ([]byte, error)

func (BytesMax32) String

func (b BytesMax32) String() string

func (*BytesMax32) UnmarshalJSON

func (b *BytesMax32) UnmarshalJSON(text []byte) error

func (*BytesMax32) UnmarshalText

func (b *BytesMax32) UnmarshalText(text []byte) error

type Data

type Data = hexutil.Bytes

type Downloader

type Downloader interface {
	Fetch(ctx context.Context, id eth.BlockID) (*types.Block, []*types.Receipt, error)
}

type Driver

type Driver interface {
	// contains filtered or unexported methods
}

Driver exposes the driver functionality that maintains the external execution-engine.

type DriverAPI

type DriverAPI interface {
	EngineAPI
	EthBackend
}

type EngineAPI

type EngineAPI interface {
	GetPayload(ctx context.Context, payloadId PayloadID) (*ExecutionPayload, error)
	ExecutePayload(ctx context.Context, payload *ExecutionPayload) (*ExecutePayloadResult, error)
	ForkchoiceUpdated(ctx context.Context, state *ForkchoiceState, attr *PayloadAttributes) (ForkchoiceUpdatedResult, error)
	Close()
}

type EngineClient

type EngineClient struct {
	RPCBackend
	EthBackend
	Log log.Logger
}

func (*EngineClient) Close

func (el *EngineClient) Close()

func (*EngineClient) ExecutePayload

func (el *EngineClient) ExecutePayload(ctx context.Context, payload *ExecutionPayload) (*ExecutePayloadResult, error)

func (*EngineClient) ForkchoiceUpdated

func (el *EngineClient) ForkchoiceUpdated(ctx context.Context, state *ForkchoiceState, attr *PayloadAttributes) (ForkchoiceUpdatedResult, error)

func (*EngineClient) GetPayload

func (el *EngineClient) GetPayload(ctx context.Context, payloadId PayloadID) (*ExecutionPayload, error)

type EngineDriver

type EngineDriver struct {
	Log log.Logger
	// API bindings to execution engine
	RPC     DriverAPI
	DL      Downloader
	SyncRef SyncReference

	EngineDriverState
	// contains filtered or unexported fields
}

func (*EngineDriver) Close

func (e *EngineDriver) Close()

func (*EngineDriver) Drive

func (e *EngineDriver) Drive(ctx context.Context, l1Heads <-chan eth.HeadSignal) ethereum.Subscription

type EngineDriverState

type EngineDriverState struct {

	// Genesis starting point
	Genesis Genesis
	// contains filtered or unexported fields
}

func (*EngineDriverState) Head

func (e *EngineDriverState) Head() (l1Head eth.BlockID, l2Head eth.BlockID)

func (*EngineDriverState) L1Head

func (e *EngineDriverState) L1Head() eth.BlockID

L1Head returns the block-id (hash and number) of the last L1 block that was derived into the L2 block

func (*EngineDriverState) L2Head

func (e *EngineDriverState) L2Head() eth.BlockID

L2Head returns the block-id (hash and number) of the L2 chain head

func (*EngineDriverState) NotifyL1Head

func (e *EngineDriverState) NotifyL1Head(ctx context.Context, log log.Logger, l1HeadSig eth.HeadSignal, driver Driver) (l2Updated bool)

func (*EngineDriverState) RequestSync

func (e *EngineDriverState) RequestSync(ctx context.Context, log log.Logger, driver Driver) (l2Updated bool)

func (*EngineDriverState) RequestUpdate

func (e *EngineDriverState) RequestUpdate(ctx context.Context, log log.Logger, driver Driver) (l2Updated bool)

func (*EngineDriverState) UpdateHead

func (e *EngineDriverState) UpdateHead(l1Head eth.BlockID, l2Head eth.BlockID)

type ErrorCode

type ErrorCode int
const (
	UnavailablePayload ErrorCode = -32001
)

type ExecutePayloadResult

type ExecutePayloadResult struct {
	// the result of the payload execution
	Status ExecutePayloadStatus `json:"status"`
	// the hash of the most recent valid block in the branch defined by payload and its ancestors
	LatestValidHash common.Hash `json:"latestValidHash"`
	// additional details on the result
	ValidationError string `json:"validationError"`
}

type ExecutePayloadStatus

type ExecutePayloadStatus string
const (
	// given payload is valid
	ExecutionValid ExecutePayloadStatus = "VALID"
	// given payload is invalid
	ExecutionInvalid ExecutePayloadStatus = "INVALID"
	// sync process is in progress
	ExecutionSyncing ExecutePayloadStatus = "SYNCING"
)

type ExecutionPayload

type ExecutionPayload struct {
	ParentHash    common.Hash     `json:"parentHash"`
	FeeRecipient  common.Address  `json:"feeRecipient"`
	StateRoot     Bytes32         `json:"stateRoot"`
	ReceiptsRoot  Bytes32         `json:"receiptsRoot"`
	LogsBloom     Bytes256        `json:"logsBloom"`
	Random        Bytes32         `json:"random"`
	BlockNumber   Uint64Quantity  `json:"blockNumber"`
	GasLimit      Uint64Quantity  `json:"gasLimit"`
	GasUsed       Uint64Quantity  `json:"gasUsed"`
	Timestamp     Uint64Quantity  `json:"timestamp"`
	ExtraData     BytesMax32      `json:"extraData"`
	BaseFeePerGas Uint256Quantity `json:"baseFeePerGas"`
	BlockHash     common.Hash     `json:"blockHash"`
	// Array of transaction objects, each object is a byte list (DATA) representing
	// TransactionType || TransactionPayload or LegacyTransaction as defined in EIP-2718
	Transactions []Data `json:"transactions"`
}

func BlockToPayload

func BlockToPayload(bl *types.Block, random Bytes32) (*ExecutionPayload, error)

func DeriveBlockOutputs

func DeriveBlockOutputs(ctx context.Context, engine BlockPreparer, l2Parent common.Hash, l2Finalized common.Hash, attributes *PayloadAttributes) (*ExecutionPayload, error)

DeriveBlockOutputs uses the engine API to derive a full L2 block from the block inputs. The fcState does not affect the block production, but may inform the engine of finality and head changes to sync towards before block computation.

func (*ExecutionPayload) ID

func (payload *ExecutionPayload) ID() eth.BlockID

type ForkchoiceState

type ForkchoiceState struct {
	// block hash of the head of the canonical chain
	HeadBlockHash common.Hash `json:"headBlockHash"`
	// safe block hash in the canonical chain
	SafeBlockHash common.Hash `json:"safeBlockHash"`
	// block hash of the most recent finalized block
	FinalizedBlockHash common.Hash `json:"finalizedBlockHash"`
}

type ForkchoiceUpdatedResult

type ForkchoiceUpdatedResult struct {
	// the result of the payload execution
	Status ForkchoiceUpdatedStatus `json:"status"`
	// the payload id if requested
	PayloadID *PayloadID `json:"payloadId"`
}

type ForkchoiceUpdatedStatus

type ForkchoiceUpdatedStatus string
const (
	// given payload is valid
	UpdateSuccess ForkchoiceUpdatedStatus = "SUCCESS"
	// sync process is in progress
	UpdateSyncing ForkchoiceUpdatedStatus = "SYNCING"
)

type Genesis

type Genesis struct {
	L1 eth.BlockID
	L2 eth.BlockID
}

type L1Info

type L1Info interface {
	NumberU64() uint64
	Time() uint64
	Hash() common.Hash
	BaseFee() *big.Int
}

type PayloadAttributes

type PayloadAttributes struct {
	// value for the timestamp field of the new payload
	Timestamp Uint64Quantity `json:"timestamp"`
	// value for the random field of the new payload
	Random Bytes32 `json:"random"`
	// suggested value for the coinbase field of the new payload
	SuggestedFeeRecipient common.Address `json:"suggestedFeeRecipient"`
	// Transactions to build the block with, omitted if the local tx pool of the engine should be used instead
	Transactions []Data `json:"transactions,omitempty"`
}

func DeriveBlockInputs

func DeriveBlockInputs(block BlockInput, receipts []*types.Receipt) (*PayloadAttributes, error)

type PayloadID

type PayloadID = hexutil.Bytes

TODO: implement neat 8 byte typed payload ID and upstream it to geth api definitions

type RPCBackend

type RPCBackend interface {
	CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
	Close()
}

type ReceiptHash

type ReceiptHash interface {
	ReceiptHash() common.Hash
}

type StateMachine

type StateMachine interface {
	// RequestUpdate tries to update the state-machine with the driver head information.
	// If the state-machine changed, considering the engine L1 and L2 head, it will return true. False otherwise.
	RequestUpdate(ctx context.Context, log log.Logger, driver Driver) (l2Updated bool)
	// RequestSync tries to sync the provided driver towards the sync target of the state-machine.
	// If the L2 syncs a step, but is not finished yet, it will return true. False otherwise.
	RequestSync(ctx context.Context, log log.Logger, driver Driver) (l2Updated bool)
	// NotifyL1Head updates the state-machine with the L1 signal,
	// and attempts to sync the driver if the update extends the previous head.
	// Returns true if the driver successfully derived and synced the L2 block to match L1. False otherwise.
	NotifyL1Head(ctx context.Context, log log.Logger, l1HeadSig eth.HeadSignal, driver Driver) (l2Updated bool)
}

StateMachine provides control over the driver state, when given control over the Driver actions.

type SyncReference

type SyncReference interface {
	// RefByL1Num fetches the canonical L1 block hash and the parent for the given L1 block height.
	RefByL1Num(ctx context.Context, l1Num uint64) (self eth.BlockID, parent eth.BlockID, err error)

	// RefByL2Num fetches the L1 and L2 block IDs from the engine for the given L2 block height.
	// Use a nil height to fetch the head.
	RefByL2Num(ctx context.Context, l2Num *big.Int, genesis *Genesis) (refL1 eth.BlockID, refL2 eth.BlockID, parentL2 common.Hash, err error)

	// RefByL2Hash fetches the L1 and L2 block IDs from the engine for the given L2 block hash.
	RefByL2Hash(ctx context.Context, l2Hash common.Hash, genesis *Genesis) (refL1 eth.BlockID, refL2 eth.BlockID, parentL2 common.Hash, err error)
}

SyncReference helps inform the sync algorithm of the L2 sync-state and L1 canonical chain

type SyncSource

type SyncSource struct {
	L1 eth.BlockLinkByNumber
	L2 eth.BlockSource
}

SyncSource implements SyncReference with a L2 block sources and L1 hash-by-number source

func (SyncSource) RefByL1Num

func (src SyncSource) RefByL1Num(ctx context.Context, l1Num uint64) (self eth.BlockID, parent eth.BlockID, err error)

RefByL1Num fetches the canonical L1 block hash and the parent for the given L1 block height.

func (SyncSource) RefByL2Hash

func (src SyncSource) RefByL2Hash(ctx context.Context, l2Hash common.Hash, genesis *Genesis) (refL1 eth.BlockID, refL2 eth.BlockID, parentL2 common.Hash, err error)

RefByL2Hash fetches the L1 and L2 block IDs from the engine for the given L2 block hash.

func (SyncSource) RefByL2Num

func (src SyncSource) RefByL2Num(ctx context.Context, l2Num *big.Int, genesis *Genesis) (refL1 eth.BlockID, refL2 eth.BlockID, parentL2 common.Hash, err error)

RefByL2Num fetches the L1 and L2 block IDs from the engine for the given L2 block height. Use a nil height to fetch the head.

type Uint256Quantity

type Uint256Quantity = uint256.Int

type Uint64Quantity

type Uint64Quantity = hexutil.Uint64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL