Move stateAt func to XEth
This commit is contained in:
parent
6669ef5b70
commit
19360c0079
36
rpc/api.go
36
rpc/api.go
@ -10,11 +10,9 @@ import (
|
|||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/event"
|
"github.com/ethereum/go-ethereum/event"
|
||||||
"github.com/ethereum/go-ethereum/state"
|
|
||||||
"github.com/ethereum/go-ethereum/xeth"
|
"github.com/ethereum/go-ethereum/xeth"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -53,28 +51,6 @@ func (self *EthereumApi) xeth() *xeth.XEth {
|
|||||||
return self.eth
|
return self.eth
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *EthereumApi) xethWithStateNum(num int64) *xeth.XEth {
|
|
||||||
chain := self.xeth().Backend().ChainManager()
|
|
||||||
var block *types.Block
|
|
||||||
|
|
||||||
if num < 0 {
|
|
||||||
num = chain.CurrentBlock().Number().Int64() + num + 1
|
|
||||||
}
|
|
||||||
block = chain.GetBlockByNumber(uint64(num))
|
|
||||||
|
|
||||||
var st *state.StateDB
|
|
||||||
if block != nil {
|
|
||||||
st = state.New(block.Root(), self.xeth().Backend().StateDb())
|
|
||||||
} else {
|
|
||||||
st = chain.State()
|
|
||||||
}
|
|
||||||
return self.xeth().WithState(st)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *EthereumApi) getStateWithNum(num int64) *xeth.State {
|
|
||||||
return self.xethWithStateNum(num).State()
|
|
||||||
}
|
|
||||||
|
|
||||||
// func (self *EthereumApi) Register(args string, reply *interface{}) error {
|
// func (self *EthereumApi) Register(args string, reply *interface{}) error {
|
||||||
// self.regmut.Lock()
|
// self.regmut.Lock()
|
||||||
// defer self.regmut.Unlock()
|
// defer self.regmut.Unlock()
|
||||||
@ -152,7 +128,7 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) (err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *EthereumApi) Call(args *NewTxArgs, reply *interface{}) error {
|
func (p *EthereumApi) Call(args *NewTxArgs, reply *interface{}) error {
|
||||||
result, err := p.xethWithStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
|
result, err := p.xeth().AtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -166,7 +142,7 @@ func (p *EthereumApi) GetStorageAt(args *GetStorageAtArgs, reply *interface{}) e
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
state := p.xethWithStateNum(args.BlockNumber).State().SafeGet(args.Address)
|
state := p.xeth().AtStateNum(args.BlockNumber).State().SafeGet(args.Address)
|
||||||
value := state.StorageString(args.Key)
|
value := state.StorageString(args.Key)
|
||||||
|
|
||||||
var hx string
|
var hx string
|
||||||
@ -240,7 +216,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
*reply = common.ToHex(p.xethWithStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance().Bytes())
|
*reply = common.ToHex(p.xeth().AtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance().Bytes())
|
||||||
case "eth_getStorage", "eth_storageAt":
|
case "eth_getStorage", "eth_storageAt":
|
||||||
args := new(GetStorageArgs)
|
args := new(GetStorageArgs)
|
||||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||||
@ -251,7 +227,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
*reply = p.xethWithStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage()
|
*reply = p.xeth().AtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage()
|
||||||
case "eth_getStorageAt":
|
case "eth_getStorageAt":
|
||||||
args := new(GetStorageAtArgs)
|
args := new(GetStorageAtArgs)
|
||||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||||
@ -269,7 +245,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
*reply = p.xethWithStateNum(args.BlockNumber).TxCountAt(args.Address)
|
*reply = p.xeth().AtStateNum(args.BlockNumber).TxCountAt(args.Address)
|
||||||
case "eth_getBlockTransactionCountByHash":
|
case "eth_getBlockTransactionCountByHash":
|
||||||
args := new(GetBlockByHashArgs)
|
args := new(GetBlockByHashArgs)
|
||||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||||
@ -314,7 +290,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
|||||||
if err := args.requirements(); err != nil {
|
if err := args.requirements(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*reply = p.xethWithStateNum(args.BlockNumber).CodeAt(args.Address)
|
*reply = p.xeth().AtStateNum(args.BlockNumber).CodeAt(args.Address)
|
||||||
case "eth_sendTransaction", "eth_transact":
|
case "eth_sendTransaction", "eth_transact":
|
||||||
args := new(NewTxArgs)
|
args := new(NewTxArgs)
|
||||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||||
|
18
xeth/xeth.go
18
xeth/xeth.go
@ -154,6 +154,24 @@ func (self *XEth) stop() {
|
|||||||
close(self.quit)
|
close(self.quit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *XEth) AtStateNum(num int64) *XEth {
|
||||||
|
chain := self.Backend().ChainManager()
|
||||||
|
var block *types.Block
|
||||||
|
|
||||||
|
if num < 0 {
|
||||||
|
num = chain.CurrentBlock().Number().Int64() + num + 1
|
||||||
|
}
|
||||||
|
block = chain.GetBlockByNumber(uint64(num))
|
||||||
|
|
||||||
|
var st *state.StateDB
|
||||||
|
if block != nil {
|
||||||
|
st = state.New(block.Root(), self.Backend().StateDb())
|
||||||
|
} else {
|
||||||
|
st = chain.State()
|
||||||
|
}
|
||||||
|
return self.WithState(st)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *XEth) Backend() Backend { return self.eth }
|
func (self *XEth) Backend() Backend { return self.eth }
|
||||||
func (self *XEth) WithState(statedb *state.StateDB) *XEth {
|
func (self *XEth) WithState(statedb *state.StateDB) *XEth {
|
||||||
xeth := &XEth{
|
xeth := &XEth{
|
||||||
|
Loading…
Reference in New Issue
Block a user