From 947c62b06c777bfe83423ac2a721d9e4201b192b Mon Sep 17 00:00:00 2001 From: Ryan Schneider Date: Fri, 20 Sep 2019 14:07:55 -0700 Subject: [PATCH] eth, internal/web3ext: add optional first and last arguments to the `admin_exportChain` RPC. --- eth/api.go | 18 +++++++++++++++--- internal/web3ext/web3ext.go | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/eth/api.go b/eth/api.go index f8c51c09bd..a874582e19 100644 --- a/eth/api.go +++ b/eth/api.go @@ -166,8 +166,16 @@ func NewPrivateAdminAPI(eth *Ethereum) *PrivateAdminAPI { return &PrivateAdminAPI{eth: eth} } -// ExportChain exports the current blockchain into a local file. -func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) { +// ExportChain exports the current blockchain into a local file, +// or a range of blocks if first and last are non-nil +func (api *PrivateAdminAPI) ExportChain(file string, first *uint64, last *uint64) (bool, error) { + if first == nil && last != nil { + return false, errors.New("last cannot be specified without first") + } + if first != nil && last == nil { + head := api.eth.BlockChain().CurrentHeader().Number.Uint64() + last = &head + } if _, err := os.Stat(file); err == nil { // File already exists. Allowing overwrite could be a DoS vecotor, // since the 'file' may point to arbitrary paths on the drive @@ -187,7 +195,11 @@ func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) { } // Export the blockchain - if err := api.eth.BlockChain().Export(writer); err != nil { + if first != nil { + if err := api.eth.BlockChain().ExportN(writer, *first, *last); err != nil { + return false, err + } + } else if err := api.eth.BlockChain().Export(writer); err != nil { return false, err } return true, nil diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 86e5754392..0a5e7946fc 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -167,8 +167,8 @@ web3._extend({ new web3._extend.Method({ name: 'exportChain', call: 'admin_exportChain', - params: 1, - inputFormatter: [null] + params: 3, + inputFormatter: [null, null, null] }), new web3._extend.Method({ name: 'importChain',