From d4e7603315cfb0df7c08e284983c5e937d2b2ff1 Mon Sep 17 00:00:00 2001 From: Manav Darji Date: Thu, 2 Jun 2022 16:23:38 +0530 Subject: [PATCH] add debug rpc endpoints for checkpoint whitelist service --- eth/api_backend.go | 8 ++++++++ eth/downloader/downloader.go | 2 ++ eth/downloader/downloader_test.go | 6 ++++++ eth/downloader/whitelist/service.go | 16 ++++++++++------ internal/ethapi/api.go | 11 +++++++++++ internal/ethapi/backend.go | 2 ++ internal/web3ext/web3ext.go | 10 ++++++++++ les/api_backend.go | 15 +++++++++++---- 8 files changed, 60 insertions(+), 10 deletions(-) diff --git a/eth/api_backend.go b/eth/api_backend.go index 6577ac1e1a..c33f3cf6f2 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -359,3 +359,11 @@ func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, re func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) { return b.eth.stateAtTransaction(block, txIndex, reexec) } + +func (b *EthAPIBackend) GetCheckpointWhitelist() map[uint64]common.Hash { + return b.eth.Downloader().ChainValidator.GetCheckpointWhitelist() +} + +func (b *EthAPIBackend) PurgeCheckpointWhitelist() { + b.eth.Downloader().ChainValidator.PurgeCheckpointWhitelist() +} diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 12b4a90dde..fa53fe39c0 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -157,6 +157,8 @@ type Downloader struct { type ChainValidator interface { IsValidChain(remoteHeader *types.Header, fetchHeadersByNumber func(number uint64, amount int, skip int, reverse bool) ([]*types.Header, []common.Hash, error)) (bool, error) ProcessCheckpoint(endBlockNum uint64, endBlockHash common.Hash) + GetCheckpointWhitelist() map[uint64]common.Hash + PurgeCheckpointWhitelist() } // LightChain encapsulates functions required to synchronise a light chain. diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go index cb0e656e39..3a188298dc 100644 --- a/eth/downloader/downloader_test.go +++ b/eth/downloader/downloader_test.go @@ -1420,6 +1420,12 @@ func (w *whitelistFake) IsValidChain(remoteHeader *types.Header, fetchHeadersByN func (w *whitelistFake) ProcessCheckpoint(endBlockNum uint64, endBlockHash common.Hash) {} +func (w *whitelistFake) GetCheckpointWhitelist() map[uint64]common.Hash { + return nil +} + +func (w *whitelistFake) PurgeCheckpointWhitelist() {} + // TestFakedSyncProgress66WhitelistMismatch tests if in case of whitelisted // checkpoint mismatch with opposite peer, the sync should fail. func TestFakedSyncProgress66WhitelistMismatch(t *testing.T) { diff --git a/eth/downloader/whitelist/service.go b/eth/downloader/whitelist/service.go index f12639454f..62bc842186 100644 --- a/eth/downloader/whitelist/service.go +++ b/eth/downloader/whitelist/service.go @@ -82,12 +82,16 @@ func (w *Service) ProcessCheckpoint(endBlockNum uint64, endBlockHash common.Hash } } -// PurgeWhitelistMap purges data from checkpoint whitelist map -func (w *Service) purgeWhitelistMap() error { - for k := range w.checkpointWhitelist { - delete(w.checkpointWhitelist, k) - } - return nil +// GetCheckpointWhitelist returns the existing whitelisted +// entries of checkpoint of the form block number -> block hash. +func (w *Service) GetCheckpointWhitelist() map[uint64]common.Hash { + return w.checkpointWhitelist +} + +// PurgeCheckpointWhitelist purges data from checkpoint whitelist map +func (w *Service) PurgeCheckpointWhitelist() { + w.checkpointWhitelist = make(map[uint64]common.Hash) + w.checkpointOrder = make([]uint64, 0) } // EnqueueWhitelistBlock enqueues blockNumber, blockHash to the checkpoint whitelist map diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 15536d5d72..59b6feba52 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -2156,6 +2156,17 @@ func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) { api.b.SetHead(uint64(number)) } +// GetCheckpointWhitelist retrieves the current checkpoint whitelist +// entries (of the form block number -> block hash) +func (api *PrivateDebugAPI) GetCheckpointWhitelist() map[uint64]common.Hash { + return api.b.GetCheckpointWhitelist() +} + +// PurgeCheckpointWhitelist purges the current checkpoint whitelist entries +func (api *PrivateDebugAPI) PurgeCheckpointWhitelist() { + api.b.PurgeCheckpointWhitelist() +} + // PublicNetAPI offers network related RPC methods type PublicNetAPI struct { net *p2p.Server diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index 97ce7c24cf..1287640b83 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -99,6 +99,8 @@ type Backend interface { GetBorBlockTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) GetBorBlockTransactionWithBlockHash(ctx context.Context, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) SubscribeChain2HeadEvent(ch chan<- core.Chain2HeadEvent) event.Subscription + GetCheckpointWhitelist() map[uint64]common.Hash + PurgeCheckpointWhitelist() ChainConfig() *params.ChainConfig Engine() consensus.Engine diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index dd8b34e025..dcdd5baf23 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -474,6 +474,16 @@ web3._extend({ params: 2, inputFormatter:[web3._extend.formatters.inputBlockNumberFormatter, web3._extend.formatters.inputBlockNumberFormatter], }), + new web3._extend.Method({ + name: 'getCheckpointWhitelist', + call: 'debug_getCheckpointWhitelist', + params: 0, + }), + new web3._extend.Method({ + name: 'purgeCheckpointWhitelist', + call: 'debug_purgeCheckpointWhitelist', + params: 0, + }), ], properties: [] }); diff --git a/les/api_backend.go b/les/api_backend.go index e9e51c4ecc..c716a3967f 100644 --- a/les/api_backend.go +++ b/les/api_backend.go @@ -337,17 +337,24 @@ func (b *LesApiBackend) StateAtTransaction(ctx context.Context, block *types.Blo // func (b *LesApiBackend) GetBorBlockReceipt(ctx context.Context, hash common.Hash) (*types.Receipt, error) { - return nil, errors.New("Not implemented") + return nil, errors.New("not implemented") } func (b *LesApiBackend) GetBorBlockLogs(ctx context.Context, hash common.Hash) ([]*types.Log, error) { - return nil, errors.New("Not implemented") + return nil, errors.New("not implemented") } func (b *LesApiBackend) GetBorBlockTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { - return nil, common.Hash{}, 0, 0, errors.New("Not implemented") + return nil, common.Hash{}, 0, 0, errors.New("not implemented") } func (b *LesApiBackend) GetBorBlockTransactionWithBlockHash(ctx context.Context, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { - return nil, common.Hash{}, 0, 0, errors.New("Not implemented") + return nil, common.Hash{}, 0, 0, errors.New("not implemented") +} + +func (b *LesApiBackend) GetCheckpointWhitelist() map[uint64]common.Hash { + return nil +} + +func (b *LesApiBackend) PurgeCheckpointWhitelist() { }