Skip to content

arbitrum: initial create backend and API #5

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

Merged
merged 8 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
120 changes: 66 additions & 54 deletions arbitrum/apibackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"math/big"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
Expand All @@ -15,82 +16,91 @@ import (
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
)

type ArbAPIBackend struct {
b *ArbBackend
type APIBackend struct {
b *Backend
}

func createRegisterAPIBackend(backend *ArbBackend) {
backend.apiBackend = &ArbAPIBackend{
func createRegisterAPIBackend(backend *Backend) {
backend.apiBackend = &APIBackend{
b: backend,
}
backend.stack.RegisterAPIs(backend.apiBackend.GetAPIs())
}

func (a *ArbAPIBackend) GetAPIs() []rpc.API {
return ethapi.GetAPIs(a)
func (a *APIBackend) GetAPIs() []rpc.API {
apis := ethapi.GetAPIs(a)

apis = append(apis, rpc.API{
Namespace: "eth",
Version: "1.0",
Service: filters.NewPublicFilterAPI(a, false, 5*time.Minute),
Public: true,
})
return apis
}

// General Ethereum API
func (a *ArbAPIBackend) SyncProgress() ethereum.SyncProgress {
func (a *APIBackend) SyncProgress() ethereum.SyncProgress {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
func (a *APIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) {
func (a *APIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) ChainDb() ethdb.Database {
func (a *APIBackend) ChainDb() ethdb.Database {
return a.b.ethDatabase
}

func (a *ArbAPIBackend) AccountManager() *accounts.Manager {
func (a *APIBackend) AccountManager() *accounts.Manager {
return a.b.stack.AccountManager()
}

func (a *ArbAPIBackend) ExtRPCEnabled() bool {
func (a *APIBackend) ExtRPCEnabled() bool {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) RPCGasCap() uint64 {
func (a *APIBackend) RPCGasCap() uint64 {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) RPCTxFeeCap() float64 {
func (a *APIBackend) RPCTxFeeCap() float64 {
return a.b.ethConfig.RPCTxFeeCap
}

func (a *ArbAPIBackend) UnprotectedAllowed() bool {
func (a *APIBackend) UnprotectedAllowed() bool {
return true // TODO: is that true?
}

// Blockchain API
func (a *ArbAPIBackend) SetHead(number uint64) {
func (a *APIBackend) SetHead(number uint64) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
func (a *APIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
if number == rpc.LatestBlockNumber {
return a.b.blockChain.CurrentBlock().Header(), nil
}
return a.b.blockChain.GetHeaderByNumber(uint64(number.Int64())), nil
}

func (a *ArbAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
func (a *APIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
return a.b.blockChain.GetHeaderByHash(hash), nil
}

func (a *ArbAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
func (a *APIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
number, isnum := blockNrOrHash.Number()
if isnum {
return a.HeaderByNumber(ctx, number)
Expand All @@ -102,23 +112,23 @@ func (a *ArbAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash
return nil, errors.New("invalid arguments; neither block nor hash specified")
}

func (a *ArbAPIBackend) CurrentHeader() *types.Header {
func (a *APIBackend) CurrentHeader() *types.Header {
return a.b.blockChain.CurrentHeader()
}

func (a *ArbAPIBackend) CurrentBlock() *types.Block {
func (a *APIBackend) CurrentBlock() *types.Block {
return a.b.blockChain.CurrentBlock()
}

func (a *ArbAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
func (a *APIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
return a.b.blockChain.GetBlockByNumber(uint64(number.Int64())), nil
}

func (a *ArbAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
func (a *APIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
return a.b.blockChain.GetBlockByHash(hash), nil
}

func (a *ArbAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
func (a *APIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
number, isnum := blockNrOrHash.Number()
if isnum {
return a.BlockByNumber(ctx, number)
Expand All @@ -130,7 +140,7 @@ func (a *ArbAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash r
return nil, errors.New("invalid arguments; neither block nor hash specified")
}

func (a *ArbAPIBackend) stateAndHeaderFromHeader(header *types.Header, err error) (*state.StateDB, *types.Header, error) {
func (a *APIBackend) stateAndHeaderFromHeader(header *types.Header, err error) (*state.StateDB, *types.Header, error) {
if err != nil {
return nil, header, err
}
Expand All @@ -141,105 +151,107 @@ func (a *ArbAPIBackend) stateAndHeaderFromHeader(header *types.Header, err error
return state, header, err
}

func (a *ArbAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
func (a *APIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
return a.stateAndHeaderFromHeader(a.HeaderByNumber(ctx, number))
}

func (a *ArbAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
func (a *APIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
return a.stateAndHeaderFromHeader(a.HeaderByNumberOrHash(ctx, blockNrOrHash))
}

func (a *ArbAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
func (a *APIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
return a.b.blockChain.GetReceiptsByHash(hash), nil
}

func (a *ArbAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
func (a *APIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
func (a *APIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
func (a *APIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
return a.b.blockChain.SubscribeChainEvent(ch)
}

func (a *ArbAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
func (a *APIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
return a.b.blockChain.SubscribeChainHeadEvent(ch)
}

func (a *ArbAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
func (a *APIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
return a.b.blockChain.SubscribeChainSideEvent(ch)
}

// Transaction pool API
func (a *ArbAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
func (a *APIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
return a.b.EnqueueL2Message(signedTx)
}

func (a *ArbAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
func (a *APIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(a.b.ethDatabase, txHash)
return tx, blockHash, blockNumber, index, nil
}

func (a *ArbAPIBackend) GetPoolTransactions() (types.Transactions, error) {
func (a *APIBackend) GetPoolTransactions() (types.Transactions, error) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
func (a *APIBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
func (a *APIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) Stats() (pending int, queued int) {
func (a *APIBackend) Stats() (pending int, queued int) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
func (a *APIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
func (a *APIBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) SubscribeNewTxsEvent(_ chan<- core.NewTxsEvent) event.Subscription {
panic("not implemented") // TODO: Implement
func (a *APIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
return a.b.SubscribeNewTxsEvent(ch)
}

// Filter API
func (a *ArbAPIBackend) BloomStatus() (uint64, uint64) {
func (a *APIBackend) BloomStatus() (uint64, uint64) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
func (a *APIBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
func (a *APIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
panic("not implemented") // TODO: Implement
}

func (a *ArbAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
panic("not implemented") // TODO: Implement
func (a *APIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
return a.b.blockChain.SubscribeLogsEvent(ch)
}

func (a *ArbAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
panic("not implemented") // TODO: Implement
func (a *APIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
//TODO: this is a nop subscription
//Arbitrum doesn't really need pending logs. Logs are published as soon as we know them..
return event.NewSubscription(func(<-chan struct{}) error { return nil })
}

func (a *ArbAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
panic("not implemented") // TODO: Implement
func (a *APIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
return a.b.blockChain.SubscribeRemovedLogsEvent(ch)
}

func (a *ArbAPIBackend) ChainConfig() *params.ChainConfig {
func (a *APIBackend) ChainConfig() *params.ChainConfig {
return a.b.blockChain.Config()
}

func (a *ArbAPIBackend) Engine() consensus.Engine {
func (a *APIBackend) Engine() consensus.Engine {
return a.b.blockChain.Engine()
}
34 changes: 22 additions & 12 deletions arbitrum/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/node"
)

type ArbBackend struct {
type Backend struct {
arbos ArbosWrapper
blockChain *core.BlockChain
stack *node.Node
chainId *big.Int
apiBackend *ArbAPIBackend
apiBackend *APIBackend
ethConfig *ethconfig.Config
ethDatabase ethdb.Database

txFeed event.Feed
scope event.SubscriptionScope

chanTxs chan *types.Transaction
chanClose chan struct{} //close coroutine
chanNewBlock chan struct{} //create new L2 block unless empty
}

func NewBackend(stack *node.Node, config *ethconfig.Config, ethDatabase ethdb.Database, blockChain *core.BlockChain, chainId *big.Int, arbos ArbosWrapper) (*ArbBackend, error) {
backend := &ArbBackend{
func NewBackend(stack *node.Node, config *ethconfig.Config, ethDatabase ethdb.Database, blockChain *core.BlockChain, chainId *big.Int, arbos ArbosWrapper) (*Backend, error) {
backend := &Backend{
arbos: arbos,
blockChain: blockChain,
stack: stack,
Expand All @@ -38,26 +42,30 @@ func NewBackend(stack *node.Node, config *ethconfig.Config, ethDatabase ethdb.Da
chanNewBlock: make(chan struct{}, 1),
}
stack.RegisterLifecycle(backend)
go backend.segmentQueueRutine()
go backend.segmentQueueRoutine()

createRegisterAPIBackend(backend)
return backend, nil
}

func (b *ArbBackend) APIBackend() *ArbAPIBackend {
func (b *Backend) APIBackend() *APIBackend {
return b.apiBackend
}

func (b *ArbBackend) EnqueueL2Message(tx *types.Transaction) error {
func (b *Backend) EnqueueL2Message(tx *types.Transaction) error {
b.chanTxs <- tx
return nil
}

func (b *ArbBackend) CloseBlock() {
func (b *Backend) CloseBlock() {
b.chanNewBlock <- struct{}{}
}

func (b *ArbBackend) enqueueBlock(block *types.Block, reciepts types.Receipts, state *state.StateDB) {
func (b *Backend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
return b.scope.Track(b.txFeed.Subscribe(ch))
}

func (b *Backend) enqueueBlock(block *types.Block, reciepts types.Receipts, state *state.StateDB) {
if block == nil {
return
}
Expand All @@ -68,10 +76,11 @@ func (b *ArbBackend) enqueueBlock(block *types.Block, reciepts types.Receipts, s
b.blockChain.WriteBlockWithState(block, reciepts, logs, state, true)
}

func (b *ArbBackend) segmentQueueRutine() {
func (b *Backend) segmentQueueRoutine() {
for {
select {
case tx := <-b.chanTxs:
b.txFeed.Send(core.NewTxsEvent{Txs: []*types.Transaction{tx}})
b.arbos.EnqueueSequencerTx(tx)
case <-b.chanNewBlock:
b.enqueueBlock(b.arbos.BuildBlock(true))
Expand All @@ -82,12 +91,13 @@ func (b *ArbBackend) segmentQueueRutine() {
}

//TODO: this is used when registering backend as lifecycle in stack
func (b *ArbBackend) Start() error {
func (b *Backend) Start() error {
return nil
}

func (b *ArbBackend) Stop() error {
func (b *Backend) Stop() error {

b.scope.Close()
b.blockChain.Stop()

return nil
Expand Down