add methods to xeth:

- SuggestPeer
- Import/Export
- [DumpBlock]
This commit is contained in:
zelig 2015-03-09 14:23:42 +07:00
parent 07955b3041
commit 69bf9f5af6

View file

@ -8,6 +8,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
@ -17,6 +18,7 @@ import (
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/whisper" "github.com/ethereum/go-ethereum/whisper"
) )
@ -30,6 +32,7 @@ type Backend interface {
TxPool() *core.TxPool TxPool() *core.TxPool
PeerCount() int PeerCount() int
IsListening() bool IsListening() bool
SuggestPeer(nodeURL string) error
Peers() []*p2p.Peer Peers() []*p2p.Peer
KeyManager() *crypto.KeyManager KeyManager() *crypto.KeyManager
BlockDb() ethutil.Database BlockDb() ethutil.Database
@ -94,6 +97,23 @@ func (self *XEth) BlockByNumber(num int32) *Block {
return NewBlock(self.chainManager.GetBlockByNumber(uint64(num))) return NewBlock(self.chainManager.GetBlockByNumber(uint64(num)))
} }
// func (self *XEth) DumpBlockByHash(strHash string) (state.World, error) {
// block := self.chainManager.GetBlock(ethutil.Hex2Bytes(hash))
// if block == nil {
// return fmt.Errorf("block %x not found", strHash[:4])
// }
// statedb := state.New(block.Root(), self.eth.StateDb())
// return statedb.RawDump()
// }
// func (self *XEth) DumpBlockByNumber(num int32) *Block {
// if num == -1 {
// return NewBlock(self.chainManager.CurrentBlock())
// }
// return NewBlock(self.chainManager.GetBlockByNumber(uint64(num)))
// }
func (self *XEth) Block(v interface{}) *Block { func (self *XEth) Block(v interface{}) *Block {
if n, ok := v.(int32); ok { if n, ok := v.(int32); ok {
return self.BlockByNumber(n) return self.BlockByNumber(n)
@ -313,3 +333,32 @@ func (self *XEth) Transact(toStr, valueStr, gasStr, gasPriceStr, codeStr string)
return toHex(tx.Hash()), nil return toHex(tx.Hash()), nil
} }
func (self *XEth) SuggestPeer(nodeURL string) error {
return self.eth.SuggestPeer(nodeURL)
}
func (self *XEth) Export(fn string) error {
data := self.chainManager.Export()
return ethutil.WriteFile(fn, data)
}
func (self *XEth) Import(fn string) (err error) {
var fh *os.File
fh, err = os.OpenFile(fn, os.O_RDONLY, os.ModePerm)
if err != nil {
return
}
defer fh.Close()
var blocks types.Blocks
if err = rlp.Decode(fh, &blocks); err != nil {
return
}
self.chainManager.Reset()
if err = self.chainManager.InsertChain(blocks); err != nil {
return
}
return
}