Skip to content

Commit

Permalink
Merge pull request ethereum#15 from OffchainLabs/blockchain_move
Browse files Browse the repository at this point in the history
arbitrum backend: blockchain is owned by publisher
  • Loading branch information
PlasmaPower authored Nov 2, 2021
2 parents 0fc21df + 1285209 commit 3c6c46a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
44 changes: 24 additions & 20 deletions arbitrum/apibackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (a *APIBackend) GetAPIs() []rpc.API {
return apis
}

func (a *APIBackend) blockChain() *core.BlockChain {
return a.b.publisher.BlockChain()
}

// General Ethereum API
func (a *APIBackend) SyncProgress() ethereum.SyncProgress {
panic("not implemented") // TODO: Implement
Expand Down Expand Up @@ -99,13 +103,13 @@ func (a *APIBackend) SetHead(number uint64) {

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.blockChain().CurrentBlock().Header(), nil
}
return a.b.blockChain.GetHeaderByNumber(uint64(number.Int64())), nil
return a.blockChain().GetHeaderByNumber(uint64(number.Int64())), nil
}

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

func (a *APIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
Expand All @@ -121,19 +125,19 @@ func (a *APIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc
}

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

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

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

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

func (a *APIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
Expand All @@ -155,7 +159,7 @@ func (a *APIBackend) stateAndHeaderFromHeader(header *types.Header, err error) (
if header == nil {
return nil, nil, errors.New("header not found")
}
state, err := a.b.blockChain.StateAt(header.Root)
state, err := a.blockChain().StateAt(header.Root)
return state, header, err
}

Expand All @@ -168,33 +172,33 @@ func (a *APIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOr
}

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

func (a *APIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
return a.b.blockChain.GetTdByHash(hash)
return a.blockChain().GetTdByHash(hash)
}

func (a *APIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
vmError := func() error { return nil }
if vmConfig == nil {
vmConfig = a.b.blockChain.GetVMConfig()
vmConfig = a.blockChain().GetVMConfig()
}
txContext := core.NewEVMTxContext(msg)
context := core.NewEVMBlockContext(header, a.b.blockChain, nil)
return vm.NewEVM(context, txContext, state, a.b.blockChain.Config(), *vmConfig), vmError, nil
context := core.NewEVMBlockContext(header, a.blockChain(), nil)
return vm.NewEVM(context, txContext, state, a.blockChain().Config(), *vmConfig), vmError, nil
}

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

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

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

// Transaction pool API
Expand Down Expand Up @@ -249,7 +253,7 @@ func (a *APIBackend) ServiceFilter(ctx context.Context, session *bloombits.Match
}

func (a *APIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
return a.b.blockChain.SubscribeLogsEvent(ch)
return a.blockChain().SubscribeLogsEvent(ch)
}

func (a *APIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
Expand All @@ -258,13 +262,13 @@ func (a *APIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Sub
}

func (a *APIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
return a.b.blockChain.SubscribeRemovedLogsEvent(ch)
return a.blockChain().SubscribeRemovedLogsEvent(ch)
}

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

func (a *APIBackend) Engine() consensus.Engine {
return a.b.blockChain.Engine()
return a.blockChain().Engine()
}
2 changes: 2 additions & 0 deletions arbitrum/arbos_interface.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package arbitrum

import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
)

type TransactionPublisher interface {
PublishTransaction(tx *types.Transaction) error
BlockChain() *core.BlockChain
}
19 changes: 6 additions & 13 deletions arbitrum/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethdb"
Expand All @@ -14,7 +13,6 @@ import (

type Backend struct {
publisher TransactionPublisher
blockChain *core.BlockChain
stack *node.Node
chainId *big.Int
apiBackend *APIBackend
Expand All @@ -32,7 +30,6 @@ type Backend struct {
func NewBackend(stack *node.Node, config *ethconfig.Config, ethDatabase ethdb.Database, blockChain *core.BlockChain, chainId *big.Int, publisher TransactionPublisher) (*Backend, error) {
backend := &Backend{
publisher: publisher,
blockChain: blockChain,
stack: stack,
chainId: chainId,
ethConfig: config,
Expand All @@ -59,15 +56,12 @@ func (b *Backend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscri
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
}
logs := make([]*types.Log, 0)
for _, receipt := range reciepts {
logs = append(logs, receipt.Logs...)
}
b.blockChain.WriteBlockWithState(block, reciepts, logs, state, true)
func (b *Backend) Stack() *node.Node {
return b.stack
}

func (b *Backend) Publisher() TransactionPublisher {
return b.publisher
}

//TODO: this is used when registering backend as lifecycle in stack
Expand All @@ -78,7 +72,6 @@ func (b *Backend) Start() error {
func (b *Backend) Stop() error {

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

return nil
}

0 comments on commit 3c6c46a

Please sign in to comment.