aboutsummaryrefslogtreecommitdiff
path: root/eth/api.go
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2020-09-16 17:00:11 -0400
committerDeterminant <[email protected]>2020-09-16 17:00:11 -0400
commitdba1cbd83254b0c8e4e99139f1f9748c38bf1537 (patch)
tree295d56e5fc972e866b5efb752e518cddaaf3a548 /eth/api.go
parentfec21d6c958264ff5ff729c0c91e4a882aecc23b (diff)
update more files
Diffstat (limited to 'eth/api.go')
-rw-r--r--eth/api.go128
1 files changed, 66 insertions, 62 deletions
diff --git a/eth/api.go b/eth/api.go
index b64090e..92ac928 100644
--- a/eth/api.go
+++ b/eth/api.go
@@ -34,10 +34,10 @@ import (
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/internal/ethapi"
"github.com/ava-labs/coreth/rpc"
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/rlp"
- "github.com/ava-labs/go-ethereum/trie"
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/common/hexutil"
+ "github.com/ethereum/go-ethereum/rlp"
+ "github.com/ethereum/go-ethereum/trie"
)
// PublicEthereumAPI provides an API to access Ethereum full node-related
@@ -166,8 +166,21 @@ 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
+ return false, errors.New("location would overwrite an existing file")
+ }
// 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)
if err != nil {
@@ -182,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
@@ -336,70 +353,52 @@ func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]*BadBlockArgs,
return results, nil
}
-// AccountRangeResult returns a mapping from the hash of an account addresses
-// to its preimage. It will return the JSON null if no preimage is found.
-// Since a query can return a limited amount of results, a "next" field is
-// also present for paging.
-type AccountRangeResult struct {
- Accounts map[common.Hash]*common.Address `json:"accounts"`
- Next common.Hash `json:"next"`
-}
-
-func accountRange(st state.Trie, start *common.Hash, maxResults int) (AccountRangeResult, error) {
- if start == nil {
- start = &common.Hash{0}
- }
- it := trie.NewIterator(st.NodeIterator(start.Bytes()))
- result := AccountRangeResult{Accounts: make(map[common.Hash]*common.Address), Next: common.Hash{}}
-
- if maxResults > AccountRangeMaxResults {
- maxResults = AccountRangeMaxResults
- }
-
- for i := 0; i < maxResults && it.Next(); i++ {
- if preimage := st.GetKey(it.Key); preimage != nil {
- addr := &common.Address{}
- addr.SetBytes(preimage)
- result.Accounts[common.BytesToHash(it.Key)] = addr
- } else {
- result.Accounts[common.BytesToHash(it.Key)] = nil
- }
- }
-
- if it.Next() {
- result.Next = common.BytesToHash(it.Key)
- }
-
- return result, nil
-}
-
// AccountRangeMaxResults is the maximum number of results to be returned per call
const AccountRangeMaxResults = 256
-// AccountRange enumerates all accounts in the latest state
-func (api *PrivateDebugAPI) AccountRange(ctx context.Context, start *common.Hash, maxResults int) (AccountRangeResult, error) {
- var statedb *state.StateDB
+// AccountRange enumerates all accounts in the given block and start point in paging request
+func (api *PublicDebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start []byte, maxResults int, nocode, nostorage, incompletes bool) (state.IteratorDump, error) {
+ var stateDb *state.StateDB
var err error
- block := api.eth.blockchain.CurrentBlock()
- if len(block.Transactions()) == 0 {
- statedb, err = api.computeStateDB(block, defaultTraceReexec)
- if err != nil {
- return AccountRangeResult{}, err
+ if number, ok := blockNrOrHash.Number(); ok {
+ if number == rpc.PendingBlockNumber {
+ // If we're dumping the pending state, we need to request
+ // both the pending block as well as the pending state from
+ // the miner and operate on those
+ _, stateDb = api.eth.miner.Pending()
+ } else {
+ var block *types.Block
+ if number == rpc.LatestBlockNumber {
+ block = api.eth.blockchain.CurrentBlock()
+ } else if blockNr == rpc.AcceptedBlockNumber {
+ block = api.eth.AcceptedBlock()
+ } else {
+ block = api.eth.blockchain.GetBlockByNumber(uint64(number))
+ }
+ if block == nil {
+ return state.IteratorDump{}, fmt.Errorf("block #%d not found", number)
+ }
+ stateDb, err = api.eth.BlockChain().StateAt(block.Root())
+ if err != nil {
+ return state.IteratorDump{}, err
+ }
}
- } else {
- _, _, statedb, err = api.computeTxEnv(block.Hash(), len(block.Transactions())-1, 0)
+ } else if hash, ok := blockNrOrHash.Hash(); ok {
+ block := api.eth.blockchain.GetBlockByHash(hash)
+ if block == nil {
+ return state.IteratorDump{}, fmt.Errorf("block %s not found", hash.Hex())
+ }
+ stateDb, err = api.eth.BlockChain().StateAt(block.Root())
if err != nil {
- return AccountRangeResult{}, err
+ return state.IteratorDump{}, err
}
}
- trie, err := statedb.Database().OpenTrie(block.Header().Root)
- if err != nil {
- return AccountRangeResult{}, err
+ if maxResults > AccountRangeMaxResults || maxResults <= 0 {
+ maxResults = AccountRangeMaxResults
}
-
- return accountRange(trie, start, maxResults)
+ return stateDb.IteratorDump(nocode, nostorage, incompletes, start, maxResults), nil
}
// StorageRangeResult is the result of a debug_storageRangeAt API call.
@@ -416,8 +415,13 @@ type storageEntry struct {
}
// StorageRangeAt returns the storage at the given block height and transaction index.
-func (api *PrivateDebugAPI) StorageRangeAt(ctx context.Context, blockHash common.Hash, txIndex int, contractAddress common.Address, keyStart hexutil.Bytes, maxResult int) (StorageRangeResult, error) {
- _, _, statedb, err := api.computeTxEnv(blockHash, txIndex, 0)
+func (api *PrivateDebugAPI) StorageRangeAt(blockHash common.Hash, txIndex int, contractAddress common.Address, keyStart hexutil.Bytes, maxResult int) (StorageRangeResult, error) {
+ // Retrieve the block
+ block := api.eth.blockchain.GetBlockByHash(blockHash)
+ if block == nil {
+ return StorageRangeResult{}, fmt.Errorf("block %#x not found", blockHash)
+ }
+ _, _, statedb, err := api.computeTxEnv(block, txIndex, 0)
if err != nil {
return StorageRangeResult{}, err
}