fix api trace block & Change option pending block.

This commit is contained in:
olumuyiwadad 2021-11-11 18:49:57 +05:30
parent 420a589599
commit afe58d79fc
6 changed files with 21 additions and 16 deletions

View file

@ -572,6 +572,9 @@ func (XDCx *XDCX) GetTradingState(block *types.Block, author common.Address) (*t
} }
return tradingstate.New(root, XDCx.StateCache) return tradingstate.New(root, XDCx.StateCache)
} }
func (XDCX *XDCX) GetEmptyTradingState() (*tradingstate.TradingStateDB, error) {
return tradingstate.New(tradingstate.EmptyRoot, XDCX.StateCache)
}
func (XDCx *XDCX) GetStateCache() tradingstate.Database { func (XDCx *XDCX) GetStateCache() tradingstate.Database {
return XDCx.StateCache return XDCx.StateCache

View file

@ -22,6 +22,7 @@ type Masternode struct {
type TradingService interface { type TradingService interface {
GetTradingStateRoot(block *types.Block, author common.Address) (common.Hash, error) GetTradingStateRoot(block *types.Block, author common.Address) (common.Hash, error)
GetTradingState(block *types.Block, author common.Address) (*tradingstate.TradingStateDB, error) GetTradingState(block *types.Block, author common.Address) (*tradingstate.TradingStateDB, error)
GetEmptyTradingState() (*tradingstate.TradingStateDB, error)
HasTradingState(block *types.Block, author common.Address) bool HasTradingState(block *types.Block, author common.Address) bool
GetStateCache() tradingstate.Database GetStateCache() tradingstate.Database
GetTriegc() *prque.Prque GetTriegc() *prque.Prque

View file

@ -519,6 +519,13 @@ func (bc *BlockChain) OrderStateAt(block *types.Block) (*tradingstate.TradingSta
} else { } else {
return nil, err return nil, err
} }
} else {
XDCxState, err := XDCXService.GetEmptyTradingState()
if err == nil {
return XDCxState, nil
} else {
return nil, err
}
} }
} }
return nil, errors.New("Get XDCx state fail") return nil, errors.New("Get XDCx state fail")

View file

@ -306,11 +306,7 @@ func NewPublicDebugAPI(eth *Ethereum) *PublicDebugAPI {
// DumpBlock retrieves the entire state of the database at a given block. // DumpBlock retrieves the entire state of the database at a given block.
func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) { func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) {
if blockNr == rpc.PendingBlockNumber { if blockNr == rpc.PendingBlockNumber {
// If we're dumping the pending state, we need to request blockNr = rpc.LatestBlockNumber
// both the pending block as well as the pending state from
// the miner and operate on those
_, stateDb := api.eth.miner.Pending()
return stateDb.RawDump(), nil
} }
var block *types.Block var block *types.Block
if blockNr == rpc.LatestBlockNumber { if blockNr == rpc.LatestBlockNumber {

View file

@ -76,8 +76,7 @@ func (b *EthApiBackend) SetHead(number uint64) {
func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) { func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
// Pending block is only known by the miner // Pending block is only known by the miner
if blockNr == rpc.PendingBlockNumber { if blockNr == rpc.PendingBlockNumber {
block := b.eth.miner.PendingBlock() blockNr = rpc.LatestBlockNumber
return block.Header(), nil
} }
// Otherwise resolve and return the block // Otherwise resolve and return the block
if blockNr == rpc.LatestBlockNumber { if blockNr == rpc.LatestBlockNumber {
@ -89,8 +88,7 @@ func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNum
func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) { func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
// Pending block is only known by the miner // Pending block is only known by the miner
if blockNr == rpc.PendingBlockNumber { if blockNr == rpc.PendingBlockNumber {
block := b.eth.miner.PendingBlock() blockNr = rpc.LatestBlockNumber
return block, nil
} }
// Otherwise resolve and return the block // Otherwise resolve and return the block
if blockNr == rpc.LatestBlockNumber { if blockNr == rpc.LatestBlockNumber {
@ -102,8 +100,7 @@ func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumb
func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) { func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
// Pending state is only known by the miner // Pending state is only known by the miner
if blockNr == rpc.PendingBlockNumber { if blockNr == rpc.PendingBlockNumber {
block, state := b.eth.miner.Pending() blockNr = rpc.LatestBlockNumber
return state, block.Header(), nil
} }
// Otherwise resolve the block number and return its state // Otherwise resolve the block number and return its state
header, err := b.HeaderByNumber(ctx, blockNr) header, err := b.HeaderByNumber(ctx, blockNr)

View file

@ -21,13 +21,14 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"runtime" "runtime"
"sync" "sync"
"time" "time"
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/hexutil" "github.com/XinFinOrg/XDPoSChain/common/hexutil"
"github.com/XinFinOrg/XDPoSChain/core" "github.com/XinFinOrg/XDPoSChain/core"
@ -99,7 +100,7 @@ func (api *PrivateDebugAPI) TraceChain(ctx context.Context, start, end rpc.Block
switch start { switch start {
case rpc.PendingBlockNumber: case rpc.PendingBlockNumber:
from = api.eth.miner.PendingBlock() from = api.eth.blockchain.CurrentBlock()
case rpc.LatestBlockNumber: case rpc.LatestBlockNumber:
from = api.eth.blockchain.CurrentBlock() from = api.eth.blockchain.CurrentBlock()
default: default:
@ -107,7 +108,7 @@ func (api *PrivateDebugAPI) TraceChain(ctx context.Context, start, end rpc.Block
} }
switch end { switch end {
case rpc.PendingBlockNumber: case rpc.PendingBlockNumber:
to = api.eth.miner.PendingBlock() to = api.eth.blockchain.CurrentBlock()
case rpc.LatestBlockNumber: case rpc.LatestBlockNumber:
to = api.eth.blockchain.CurrentBlock() to = api.eth.blockchain.CurrentBlock()
default: default:
@ -353,7 +354,7 @@ func (api *PrivateDebugAPI) TraceBlockByNumber(ctx context.Context, number rpc.B
switch number { switch number {
case rpc.PendingBlockNumber: case rpc.PendingBlockNumber:
block = api.eth.miner.PendingBlock() block = api.eth.blockchain.CurrentBlock()
case rpc.LatestBlockNumber: case rpc.LatestBlockNumber:
block = api.eth.blockchain.CurrentBlock() block = api.eth.blockchain.CurrentBlock()
default: default:
@ -514,7 +515,7 @@ func (api *PrivateDebugAPI) computeStateDB(block *types.Block, reexec uint64) (*
break break
} }
if statedb, err = state.New(block.Root(), database); err == nil { if statedb, err = state.New(block.Root(), database); err == nil {
XDCxState, err = tradingstate.New(block.Root(), tradingstate.NewDatabase(api.eth.XDCX.GetLevelDB())) XDCxState, err = api.eth.blockchain.OrderStateAt(block)
if err == nil { if err == nil {
break break
} }