Skip to content

Commit

Permalink
SyncProgressMap
Browse files Browse the repository at this point in the history
  • Loading branch information
tsahee committed Jul 28, 2022
1 parent 703fb5a commit 5b27b2a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 26 deletions.
24 changes: 21 additions & 3 deletions arbitrum/apibackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"strings"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/log"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
Expand All @@ -35,6 +35,7 @@ type APIBackend struct {
b *Backend

fallbackClient types.FallbackClient
sync SyncProgressBackend
}

type ErrorFallbackClient struct {
Expand Down Expand Up @@ -79,14 +80,19 @@ func createFallbackClient(fallbackClientUrl string) (types.FallbackClient, error
return fallbackClient, nil
}

func createRegisterAPIBackend(backend *Backend, fallbackClientUrl string) error {
type SyncProgressBackend interface {
SyncProgressMap() map[string]interface{}
}

func createRegisterAPIBackend(backend *Backend, sync SyncProgressBackend, fallbackClientUrl string) error {
fallbackClient, err := createFallbackClient(fallbackClientUrl)
if err != nil {
return err
}
backend.apiBackend = &APIBackend{
b: backend,
fallbackClient: fallbackClient,
sync: sync,
}
backend.stack.RegisterAPIs(backend.apiBackend.GetAPIs())
return nil
Expand Down Expand Up @@ -130,8 +136,20 @@ func (a *APIBackend) GetArbitrumNode() interface{} {
}

// General Ethereum API
func (a *APIBackend) SyncProgressMap() map[string]interface{} {
return a.sync.SyncProgressMap()
}

func (a *APIBackend) SyncProgress() ethereum.SyncProgress {
panic("not implemented") // TODO: Implement
progress := a.sync.SyncProgressMap()

if progress == nil || len(progress) == 0 {
return ethereum.SyncProgress{}
}
return ethereum.SyncProgress{
CurrentBlock: 0,
HighestBlock: 1,
}
}

func (a *APIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
Expand Down
4 changes: 2 additions & 2 deletions arbitrum/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Backend struct {
chanNewBlock chan struct{} //create new L2 block unless empty
}

func NewBackend(stack *node.Node, config *Config, chainDb ethdb.Database, publisher ArbInterface) (*Backend, error) {
func NewBackend(stack *node.Node, config *Config, chainDb ethdb.Database, publisher ArbInterface, sync SyncProgressBackend) (*Backend, error) {
backend := &Backend{
arb: publisher,
stack: stack,
Expand All @@ -45,7 +45,7 @@ func NewBackend(stack *node.Node, config *Config, chainDb ethdb.Database, publis
}

backend.bloomIndexer.Start(backend.arb.BlockChain())
err := createRegisterAPIBackend(backend, config.ClassicRedirect)
err := createRegisterAPIBackend(backend, sync, config.ClassicRedirect)
if err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.S
return b.eth.TxPool().SubscribeNewTxsEvent(ch)
}

func (b *EthAPIBackend) SyncProgressMap() map[string]interface{} {
progress := b.eth.Downloader().Progress()
return progress.ToMap()
}

func (b *EthAPIBackend) SyncProgress() ethereum.SyncProgress {
return b.eth.Downloader().Progress()
}
Expand Down
25 changes: 25 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
)

Expand Down Expand Up @@ -124,6 +125,30 @@ type SyncProgress struct {
HealingBytecode uint64 // Number of bytecodes pending
}

func (progress SyncProgress) ToMap() map[string]interface{} {
if progress.CurrentBlock >= progress.HighestBlock {
return nil
}
// Otherwise gather the block sync stats
return map[string]interface{}{
"startingBlock": hexutil.Uint64(progress.StartingBlock),
"currentBlock": hexutil.Uint64(progress.CurrentBlock),
"highestBlock": hexutil.Uint64(progress.HighestBlock),
"syncedAccounts": hexutil.Uint64(progress.SyncedAccounts),
"syncedAccountBytes": hexutil.Uint64(progress.SyncedAccountBytes),
"syncedBytecodes": hexutil.Uint64(progress.SyncedBytecodes),
"syncedBytecodeBytes": hexutil.Uint64(progress.SyncedBytecodeBytes),
"syncedStorage": hexutil.Uint64(progress.SyncedStorage),
"syncedStorageBytes": hexutil.Uint64(progress.SyncedStorageBytes),
"healedTrienodes": hexutil.Uint64(progress.HealedTrienodes),
"healedTrienodeBytes": hexutil.Uint64(progress.HealedTrienodeBytes),
"healedBytecodes": hexutil.Uint64(progress.HealedBytecodes),
"healedBytecodeBytes": hexutil.Uint64(progress.HealedBytecodeBytes),
"healingTrienodes": hexutil.Uint64(progress.HealingTrienodes),
"healingBytecode": hexutil.Uint64(progress.HealingBytecode),
}
}

// ChainSyncReader wraps access to the node's current sync status. If there's no
// sync currently running, it returns nil.
type ChainSyncReader interface {
Expand Down
24 changes: 3 additions & 21 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,12 @@ func (s *PublicEthereumAPI) FeeHistory(ctx context.Context, blockCount rpc.Decim
// - pulledStates: number of state entries processed until now
// - knownStates: number of known state entries that still need to be pulled
func (s *PublicEthereumAPI) Syncing() (interface{}, error) {
progress := s.b.SyncProgress()
progress := s.b.SyncProgressMap()

// Return not syncing if the synchronisation already completed
if progress.CurrentBlock >= progress.HighestBlock {
if progress == nil || len(progress) == 0 {
return false, nil
}
// Otherwise gather the block sync stats
return map[string]interface{}{
"startingBlock": hexutil.Uint64(progress.StartingBlock),
"currentBlock": hexutil.Uint64(progress.CurrentBlock),
"highestBlock": hexutil.Uint64(progress.HighestBlock),
"syncedAccounts": hexutil.Uint64(progress.SyncedAccounts),
"syncedAccountBytes": hexutil.Uint64(progress.SyncedAccountBytes),
"syncedBytecodes": hexutil.Uint64(progress.SyncedBytecodes),
"syncedBytecodeBytes": hexutil.Uint64(progress.SyncedBytecodeBytes),
"syncedStorage": hexutil.Uint64(progress.SyncedStorage),
"syncedStorageBytes": hexutil.Uint64(progress.SyncedStorageBytes),
"healedTrienodes": hexutil.Uint64(progress.HealedTrienodes),
"healedTrienodeBytes": hexutil.Uint64(progress.HealedTrienodeBytes),
"healedBytecodes": hexutil.Uint64(progress.HealedBytecodes),
"healedBytecodeBytes": hexutil.Uint64(progress.HealedBytecodeBytes),
"healingTrienodes": hexutil.Uint64(progress.HealingTrienodes),
"healingBytecode": hexutil.Uint64(progress.HealingBytecode),
}, nil
return progress, nil
}

// PublicTxPoolAPI offers and API for the transaction pool. It only operates on data that is non confidential.
Expand Down
1 change: 1 addition & 0 deletions internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
type Backend interface {
// General Ethereum API
SyncProgress() ethereum.SyncProgress
SyncProgressMap() map[string]interface{}

SuggestGasTipCap(ctx context.Context) (*big.Int, error)
FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)
Expand Down
5 changes: 5 additions & 0 deletions les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEven
return b.eth.blockchain.SubscribeRemovedLogsEvent(ch)
}

func (b *LesApiBackend) SyncProgressMap() map[string]interface{} {
progress := b.eth.Downloader().Progress()
return progress.ToMap()
}

func (b *LesApiBackend) SyncProgress() ethereum.SyncProgress {
return b.eth.Downloader().Progress()
}
Expand Down

0 comments on commit 5b27b2a

Please sign in to comment.