10000 RPC admin_exportChain accepts first, last block numbers by blukat29 · Pull Request #1873 · klaytn/klaytn · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Aug 19, 2024. It is now read-only.

RPC admin_exportChain accepts first, last block numbers #1873

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions console/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ web3._extend({
new web3._extend.Method({
name: 'exportChain',
call: 'admin_exportChain',
params: 1,
inputFormatter: [null]
params: 3,
inputFormatter: [null, web3._extend.formatters.inputBlockNumberFormatter, web3._extend.formatters.inputBlockNumberFormatter],
}),
new web3._extend.Method({
name: 'importChain',
Expand Down
18 changes: 15 additions & 3 deletions node/cn/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,25 @@ func NewPrivateAdminAPI(cn *CN) *PrivateAdminAPI {
return &PrivateAdminAPI{cn: cn}
}

// 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, last *rpc.BlockNumber) (bool, error) {
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
return false, errors.New("location would overwrite an existing file")
}
if first == nil && last != nil {
return false, errors.New("last cannot be specified without first")
}
if first == nil {
zero := rpc.EarliestBlockNumber
first = &zero
}
if last == nil || *last == rpc.LatestBlockNumber {
head := rpc.BlockNumber(api.cn.BlockChain().CurrentBlock().NumberU64())
last = &head
}

// Make sure we can create the file to export into
out, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
Expand All @@ -93,7 +105,7 @@ func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) {
}

// Export the blockchain
if err := api.cn.BlockChain().Export(writer); err != nil {
if err := api.cn.BlockChain().ExportN(writer, first.Uint64(), last.Uint64()); err != nil {
return false, err
}
return true, nil
Expand Down
14 changes: 14 additions & 0 deletions work/mocks/blockchain_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions work/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ type BlockChain interface {
StateAtWithPersistent(root common.Hash) (*state.StateDB, error)
StateAtWithGCLock(root common.Hash) (*state.StateDB, error)
Export(w io.Writer) error
ExportN(w io.Writer, first, last uint64) error
Engine() consensus.Engine
GetTxLookupInfoAndReceipt(txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt)
GetTxAndLookupInfoInCache(hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64)
Expand Down
0