From 0a1eeca41e6ba5920ba65d9b41654768299bc7e3 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 18 Mar 2015 13:00:01 +0100 Subject: [PATCH] conversions. -compilable- --- cmd/ethereum/admin.go | 4 +-- cmd/ethereum/main.go | 2 +- core/state_transition.go | 6 ++++ eth/backend.go | 10 +++--- eth/protocol.go | 61 +++++++++++++++---------------- miner/miner.go | 3 +- miner/worker.go | 27 +++++++------- rpc/api.go | 19 +++++----- rpc/responses.go | 77 +++++++++++++++++++++------------------- rpc/util.go | 4 +-- xeth/types.go | 6 ++-- xeth/xeth.go | 50 ++++++++++++-------------- 12 files changed, 139 insertions(+), 130 deletions(-) diff --git a/cmd/ethereum/admin.go b/cmd/ethereum/admin.go index 880a22c22..967af2553 100644 --- a/cmd/ethereum/admin.go +++ b/cmd/ethereum/admin.go @@ -8,8 +8,8 @@ import ( "time" "github.com/ethereum/go-ethereum/cmd/utils" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/state" @@ -239,7 +239,7 @@ func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value { block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num)) } else if call.Argument(0).IsString() { hash, _ := call.Argument(0).ToString() - block = js.ethereum.ChainManager().GetBlock(common.Hex2Bytes(hash)) + block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash)) } else { fmt.Println("invalid argument for dump. Either hex string or number") } diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go index 3b952dd79..459059e6c 100644 --- a/cmd/ethereum/main.go +++ b/cmd/ethereum/main.go @@ -302,7 +302,7 @@ func dump(ctx *cli.Context) { for _, arg := range ctx.Args() { var block *types.Block if hashish(arg) { - block = chainmgr.GetBlock(common.Hex2Bytes(arg)) + block = chainmgr.GetBlock(common.HexToHash(arg)) } else { num, _ := strconv.Atoi(arg) block = chainmgr.GetBlockByNumber(uint64(num)) diff --git a/core/state_transition.go b/core/state_transition.go index 1509a5258..d0b2c5d7c 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -57,6 +57,12 @@ type Message interface { Data() []byte } +func AddressFromMessage(msg Message) common.Address { + from, _ := msg.From() + + return crypto.CreateAddress(from, msg.Nonce()) +} + func MessageCreatesContract(msg Message) bool { return msg.To() == nil } diff --git a/eth/backend.go b/eth/backend.go index 18093008b..c26bdceb5 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -10,11 +10,11 @@ import ( "github.com/ethereum/ethash" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/blockpool" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/miner" @@ -288,7 +288,7 @@ func (s *Ethereum) StartMining() error { servlogger.Errorf("Cannot start mining without coinbase: %v\n", err) return fmt.Errorf("no coinbase: %v", err) } - s.miner.Start(cb) + s.miner.Start(common.BytesToAddress(cb)) return nil } @@ -304,9 +304,9 @@ func (s *Ethereum) TxPool() *core.TxPool { return s.txPool } func (s *Ethereum) BlockPool() *blockpool.BlockPool { return s.blockPool } func (s *Ethereum) Whisper() *whisper.Whisper { return s.whisper } func (s *Ethereum) EventMux() *event.TypeMux { return s.eventMux } -func (s *Ethereum) BlockDb() common.Database { return s.blockDb } -func (s *Ethereum) StateDb() common.Database { return s.stateDb } -func (s *Ethereum) ExtraDb() common.Database { return s.extraDb } +func (s *Ethereum) BlockDb() common.Database { return s.blockDb } +func (s *Ethereum) StateDb() common.Database { return s.stateDb } +func (s *Ethereum) ExtraDb() common.Database { return s.extraDb } func (s *Ethereum) IsListening() bool { return true } // Always listening func (s *Ethereum) PeerCount() int { return s.net.PeerCount() } func (s *Ethereum) PeerInfo() int { return s.net.PeerCount() } diff --git a/eth/protocol.go b/eth/protocol.go index e368bbec5..15a8e1831 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -1,13 +1,12 @@ package eth import ( - "bytes" "fmt" "math/big" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/errs" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rlp" @@ -76,15 +75,15 @@ type txPool interface { } type chainManager interface { - GetBlockHashesFromHash(hash []byte, amount uint64) (hashes [][]byte) - GetBlock(hash []byte) (block *types.Block) - Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) + GetBlockHashesFromHash(hash common.Hash, amount uint64) (hashes []common.Hash) + GetBlock(hash common.Hash) (block *types.Block) + Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) } type blockPool interface { - AddBlockHashes(next func() ([]byte, bool), peerId string) + AddBlockHashes(next func() (common.Hash, bool), peerId string) AddBlock(block *types.Block, peerId string) - AddPeer(td *big.Int, currentBlock []byte, peerId string, requestHashes func([]byte) error, requestBlocks func([][]byte) error, peerError func(*errs.Error)) (best bool) + AddPeer(td *big.Int, currentBlock common.Hash, peerId string, requestHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool) RemovePeer(peerId string) } @@ -95,7 +94,7 @@ type newBlockMsgData struct { } type getBlockHashesMsgData struct { - Hash []byte + Hash common.Hash Amount uint64 } @@ -167,7 +166,7 @@ func (self *ethProtocol) handle() error { } for _, tx := range txs { jsonlogger.LogJson(&logger.EthTxReceived{ - TxHash: common.Bytes2Hex(tx.Hash()), + TxHash: tx.Hash().Hex(), RemoteId: self.peer.ID().String(), }) } @@ -183,7 +182,7 @@ func (self *ethProtocol) handle() error { request.Amount = maxHashes } hashes := self.chainManager.GetBlockHashesFromHash(request.Hash, request.Amount) - return p2p.EncodeMsg(self.rw, BlockHashesMsg, common.ByteSliceToInterface(hashes)...) + return p2p.EncodeMsg(self.rw, BlockHashesMsg, rlp.Flat(hashes)) case BlockHashesMsg: msgStream := rlp.NewStream(msg.Payload) @@ -192,14 +191,16 @@ func (self *ethProtocol) handle() error { } var i int - iter := func() (hash []byte, ok bool) { - hash, err := msgStream.Bytes() + iter := func() (hash common.Hash, ok bool) { + var h common.Hash + err := msgStream.Decode(&h) if err == rlp.EOL { - return nil, false + return common.Hash{}, false } else if err != nil { self.protoError(ErrDecode, "msg %v: after %v hashes : %v", msg, i, err) - return nil, false + return common.Hash{}, false } + i++ return hash, true } @@ -215,14 +216,14 @@ func (self *ethProtocol) handle() error { var i int for { i++ - var hash []byte - if err := msgStream.Decode(&hash); err != nil { - if err == rlp.EOL { - break - } else { - return self.protoError(ErrDecode, "msg %v: %v", msg, err) - } + var hash common.Hash + err := msgStream.Decode(&hash) + if err == rlp.EOL { + break + } else { + return self.protoError(ErrDecode, "msg %v: %v", msg, err) } + block := self.chainManager.GetBlock(hash) if block != nil { blocks = append(blocks, block) @@ -259,10 +260,10 @@ func (self *ethProtocol) handle() error { _, chainHead, _ := self.chainManager.Status() jsonlogger.LogJson(&logger.EthChainReceivedNewBlock{ - BlockHash: common.Bytes2Hex(hash), + BlockHash: hash.Hex(), BlockNumber: request.Block.Number(), // this surely must be zero - ChainHeadHash: common.Bytes2Hex(chainHead), - BlockPrevHash: common.Bytes2Hex(request.Block.ParentHash()), + ChainHeadHash: chainHead.Hex(), + BlockPrevHash: request.Block.ParentHash().Hex(), RemoteId: self.peer.ID().String(), }) // to simplify backend interface adding a new block @@ -282,8 +283,8 @@ type statusMsgData struct { ProtocolVersion uint32 NetworkId uint32 TD *big.Int - CurrentBlock []byte - GenesisBlock []byte + CurrentBlock common.Hash + GenesisBlock common.Hash } func (self *ethProtocol) statusMsg() p2p.Msg { @@ -325,7 +326,7 @@ func (self *ethProtocol) handleStatus() error { _, _, genesisBlock := self.chainManager.Status() - if !bytes.Equal(status.GenesisBlock, genesisBlock) { + if status.GenesisBlock != genesisBlock { return self.protoError(ErrGenesisBlockMismatch, "%x (!= %x)", status.GenesisBlock, genesisBlock) } @@ -344,14 +345,14 @@ func (self *ethProtocol) handleStatus() error { return nil } -func (self *ethProtocol) requestBlockHashes(from []byte) error { +func (self *ethProtocol) requestBlockHashes(from common.Hash) error { self.peer.Debugf("fetching hashes (%d) %x...\n", maxHashes, from[0:4]) return p2p.EncodeMsg(self.rw, GetBlockHashesMsg, interface{}(from), uint64(maxHashes)) } -func (self *ethProtocol) requestBlocks(hashes [][]byte) error { +func (self *ethProtocol) requestBlocks(hashes []common.Hash) error { self.peer.Debugf("fetching %v blocks", len(hashes)) - return p2p.EncodeMsg(self.rw, GetBlocksMsg, common.ByteSliceToInterface(hashes)...) + return p2p.EncodeMsg(self.rw, GetBlocksMsg, rlp.Flat(hashes)) } func (self *ethProtocol) protoError(code int, format string, params ...interface{}) (err *errs.Error) { diff --git a/miner/miner.go b/miner/miner.go index 7bf67a6ec..ccc19c754 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -4,6 +4,7 @@ import ( "math/big" "github.com/ethereum/ethash" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/pow" @@ -32,7 +33,7 @@ func (self *Miner) Mining() bool { return self.mining } -func (self *Miner) Start(coinbase []byte) { +func (self *Miner) Start(coinbase common.Address) { self.mining = true self.worker = newWorker(coinbase, self.eth) self.worker.register(NewCpuMiner(0, self.pow)) diff --git a/miner/worker.go b/miner/worker.go index 10fc6f508..63d1bfa0b 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -7,9 +7,9 @@ import ( "sync" "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/pow" @@ -39,7 +39,7 @@ func env(block *types.Block, eth core.Backend) *environment { coinbase: state.GetOrNewStateObject(block.Coinbase()), } for _, ancestor := range eth.ChainManager().GetAncestors(block, 7) { - env.ancestors.Add(string(ancestor.Hash())) + env.ancestors.Add(ancestor.Hash()) } return env @@ -71,14 +71,14 @@ type worker struct { eth core.Backend chain *core.ChainManager proc *core.BlockProcessor - coinbase []byte + coinbase common.Address current *environment mining bool } -func newWorker(coinbase []byte, eth core.Backend) *worker { +func newWorker(coinbase common.Address, eth core.Backend) *worker { return &worker{ eth: eth, mux: eth.EventMux(), @@ -152,13 +152,13 @@ func (self *worker) wait() { block := self.current.block if block.Number().Uint64() == work.Number && block.Nonce() == 0 { self.current.block.SetNonce(work.Nonce) - self.current.block.Header().MixDigest = work.MixDigest + self.current.block.Header().MixDigest = common.BytesToHash(work.MixDigest) jsonlogger.LogJson(&logger.EthMinerNewBlock{ - BlockHash: common.Bytes2Hex(block.Hash()), + BlockHash: block.Hash().Hex(), BlockNumber: block.Number(), - ChainHeadHash: common.Bytes2Hex(block.ParentHeaderHash), - BlockPrevHash: common.Bytes2Hex(block.ParentHeaderHash), + ChainHeadHash: block.ParentHeaderHash.Hex(), + BlockPrevHash: block.ParentHeaderHash.Hex(), }) if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil { @@ -208,9 +208,10 @@ gasLimit: fallthrough case core.IsInvalidTxErr(err): // Remove invalid transactions - self.chain.TxState().RemoveNonce(tx.From(), tx.Nonce()) + from, _ := tx.From() + self.chain.TxState().RemoveNonce(from, tx.Nonce()) remove = append(remove, tx) - minerlogger.Infof("TX (%x) failed. Transaction will be removed\n", tx.Hash()[:4]) + minerlogger.Infof("TX (%x) failed. Transaction will be removed\n", tx.Hash().Bytes()[:4]) case state.IsGasLimitErr(err): minerlogger.Infof("Gas limit reached for block. %d TXs included in this block\n", i) // Break on gas limit @@ -232,13 +233,13 @@ var ( ) func (self *worker) commitUncle(uncle *types.Header) error { - if self.current.uncles.Has(string(uncle.Hash())) { + if self.current.uncles.Has(uncle.Hash()) { // Error not unique return core.UncleError("Uncle not unique") } - self.current.uncles.Add(string(uncle.Hash())) + self.current.uncles.Add(uncle.Hash()) - if !self.current.ancestors.Has(string(uncle.ParentHash)) { + if !self.current.ancestors.Has(uncle.ParentHash) { return core.UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4])) } diff --git a/rpc/api.go b/rpc/api.go index 079347192..3c46684d1 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -668,7 +668,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return NewValidationError("Index", "does not exist") } - uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false) + uncle, err := p.GetBlockByHash(v.Uncles[args.Index].Hex(), false) if err != nil { return err } @@ -687,7 +687,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return NewValidationError("Index", "does not exist") } - uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false) + uncle, err := p.GetBlockByHash(v.Uncles[args.Index].Hex(), false) if err != nil { return err } @@ -832,12 +832,12 @@ func toFilterOptions(options *FilterOptions) core.FilterOptions { // Convert optional address slice/string to byte slice if str, ok := options.Address.(string); ok { - opts.Address = [][]byte{common.FromHex(str)} + opts.Address = []common.Address{common.HexToAddress(str)} } else if slice, ok := options.Address.([]interface{}); ok { - bslice := make([][]byte, len(slice)) + bslice := make([]common.Address, len(slice)) for i, addr := range slice { if saddr, ok := addr.(string); ok { - bslice[i] = common.FromHex(saddr) + bslice[i] = common.HexToAddress(saddr) } } opts.Address = bslice @@ -846,16 +846,15 @@ func toFilterOptions(options *FilterOptions) core.FilterOptions { opts.Earliest = options.Earliest opts.Latest = options.Latest - topics := make([][][]byte, len(options.Topics)) + topics := make([][]common.Hash, len(options.Topics)) for i, topicDat := range options.Topics { if slice, ok := topicDat.([]interface{}); ok { - topics[i] = make([][]byte, len(slice)) + topics[i] = make([]common.Hash, len(slice)) for j, topic := range slice { - topics[i][j] = common.FromHex(topic.(string)) + topics[i][j] = common.HexToHash(topic.(string)) } } else if str, ok := topicDat.(string); ok { - topics[i] = make([][]byte, 1) - topics[i][0] = common.FromHex(str) + topics[i] = []common.Hash{common.HexToHash(str)} } } opts.Topics = topics diff --git a/rpc/responses.go b/rpc/responses.go index f41ce7b96..5b2b9d488 100644 --- a/rpc/responses.go +++ b/rpc/responses.go @@ -5,6 +5,7 @@ import ( // "fmt" "math/big" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" ) @@ -12,14 +13,14 @@ type BlockRes struct { fullTx bool BlockNumber int64 `json:"number"` - BlockHash []byte `json:"hash"` - ParentHash []byte `json:"parentHash"` - Nonce []byte `json:"nonce"` - Sha3Uncles []byte `json:"sha3Uncles"` - LogsBloom []byte `json:"logsBloom"` - TransactionRoot []byte `json:"transactionRoot"` - StateRoot []byte `json:"stateRoot"` - Miner []byte `json:"miner"` + BlockHash common.Hash `json:"hash"` + ParentHash common.Hash `json:"parentHash"` + Nonce [8]byte `json:"nonce"` + Sha3Uncles common.Hash `json:"sha3Uncles"` + LogsBloom types.Bloom `json:"logsBloom"` + TransactionRoot common.Hash `json:"transactionRoot"` + StateRoot common.Hash `json:"stateRoot"` + Miner common.Address `json:"miner"` Difficulty int64 `json:"difficulty"` TotalDifficulty int64 `json:"totalDifficulty"` Size int64 `json:"size"` @@ -29,7 +30,7 @@ type BlockRes struct { GasUsed int64 `json:"gasUsed"` UnixTimestamp int64 `json:"timestamp"` Transactions []*TransactionRes `json:"transactions"` - Uncles [][]byte `json:"uncles"` + Uncles []common.Hash `json:"uncles"` } func (b *BlockRes) MarshalJSON() ([]byte, error) { @@ -57,14 +58,14 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) { // convert strict types to hexified strings ext.BlockNumber = toHex(big.NewInt(b.BlockNumber).Bytes()) - ext.BlockHash = toHex(b.BlockHash) - ext.ParentHash = toHex(b.ParentHash) - ext.Nonce = toHex(b.Nonce) - ext.Sha3Uncles = toHex(b.Sha3Uncles) - ext.LogsBloom = toHex(b.LogsBloom) - ext.TransactionRoot = toHex(b.TransactionRoot) - ext.StateRoot = toHex(b.StateRoot) - ext.Miner = toHex(b.Miner) + ext.BlockHash = b.BlockHash.Hex() + ext.ParentHash = b.ParentHash.Hex() + ext.Nonce = toHex(b.Nonce[:]) + ext.Sha3Uncles = b.Sha3Uncles.Hex() + ext.LogsBloom = toHex(b.LogsBloom[:]) + ext.TransactionRoot = b.TransactionRoot.Hex() + ext.StateRoot = b.StateRoot.Hex() + ext.Miner = b.Miner.Hex() ext.Difficulty = toHex(big.NewInt(b.Difficulty).Bytes()) ext.TotalDifficulty = toHex(big.NewInt(b.TotalDifficulty).Bytes()) ext.Size = toHex(big.NewInt(b.Size).Bytes()) @@ -80,12 +81,12 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) { } } else { for i, tx := range b.Transactions { - ext.Transactions[i] = toHex(tx.Hash) + ext.Transactions[i] = tx.Hash.Hex() } } ext.Uncles = make([]string, len(b.Uncles)) for i, v := range b.Uncles { - ext.Uncles[i] = toHex(v) + ext.Uncles[i] = v.Hex() } return json.Marshal(ext) @@ -124,7 +125,7 @@ func NewBlockRes(block *types.Block) *BlockRes { v.TxIndex = int64(i) res.Transactions[i] = v } - res.Uncles = make([][]byte, len(block.Uncles())) + res.Uncles = make([]common.Hash, len(block.Uncles())) for i, uncle := range block.Uncles() { res.Uncles[i] = uncle.Hash() } @@ -132,17 +133,17 @@ func NewBlockRes(block *types.Block) *BlockRes { } type TransactionRes struct { - Hash []byte `json:"hash"` - Nonce int64 `json:"nonce"` - BlockHash []byte `json:"blockHash,omitempty"` - BlockNumber int64 `json:"blockNumber,omitempty"` - TxIndex int64 `json:"transactionIndex,omitempty"` - From []byte `json:"from"` - To []byte `json:"to"` - Value int64 `json:"value"` - Gas int64 `json:"gas"` - GasPrice int64 `json:"gasPrice"` - Input []byte `json:"input"` + Hash common.Hash `json:"hash"` + Nonce int64 `json:"nonce"` + BlockHash common.Hash `json:"blockHash,omitempty"` + BlockNumber int64 `json:"blockNumber,omitempty"` + TxIndex int64 `json:"transactionIndex,omitempty"` + From common.Address `json:"from"` + To *common.Address `json:"to"` + Value int64 `json:"value"` + Gas int64 `json:"gas"` + GasPrice int64 `json:"gasPrice"` + Input []byte `json:"input"` } func (t *TransactionRes) MarshalJSON() ([]byte, error) { @@ -160,13 +161,17 @@ func (t *TransactionRes) MarshalJSON() ([]byte, error) { Input string `json:"input"` } - ext.Hash = toHex(t.Hash) + ext.Hash = t.Hash.Hex() ext.Nonce = toHex(big.NewInt(t.Nonce).Bytes()) - ext.BlockHash = toHex(t.BlockHash) + ext.BlockHash = t.BlockHash.Hex() ext.BlockNumber = toHex(big.NewInt(t.BlockNumber).Bytes()) ext.TxIndex = toHex(big.NewInt(t.TxIndex).Bytes()) - ext.From = toHex(t.From) - ext.To = toHex(t.To) + ext.From = t.From.Hex() + if t.To == nil { + ext.To = "0x00" + } else { + ext.To = t.To.Hex() + } ext.Value = toHex(big.NewInt(t.Value).Bytes()) ext.Gas = toHex(big.NewInt(t.Gas).Bytes()) ext.GasPrice = toHex(big.NewInt(t.GasPrice).Bytes()) @@ -179,7 +184,7 @@ func NewTransactionRes(tx *types.Transaction) *TransactionRes { var v = new(TransactionRes) v.Hash = tx.Hash() v.Nonce = int64(tx.Nonce()) - v.From = tx.From() + v.From, _ = tx.From() v.To = tx.To() v.Value = tx.Value().Int64() v.Gas = tx.Gas().Int64() diff --git a/rpc/util.go b/rpc/util.go index e5610dc2c..b9b0fa442 100644 --- a/rpc/util.go +++ b/rpc/util.go @@ -155,11 +155,11 @@ func toLogs(logs state.Logs) (ls []Log) { for i, log := range logs { var l Log l.Topic = make([]string, len(log.Topics())) - l.Address = toHex(log.Address()) + l.Address = log.Address().Hex() l.Data = toHex(log.Data()) l.Number = log.Number() for j, topic := range log.Topics() { - l.Topic[j] = toHex(topic) + l.Topic[j] = topic.Hex() } ls[i] = l } diff --git a/xeth/types.go b/xeth/types.go index d6d2a11f7..d8d6ff19f 100644 --- a/xeth/types.go +++ b/xeth/types.go @@ -142,9 +142,9 @@ func NewTx(tx *types.Transaction) *Transaction { hash := tx.Hash().Hex() receiver := tx.To().Hex() if len(receiver) == 0 { - receiver = toHex(core.AddressFromMessage(tx)) + receiver = core.AddressFromMessage(tx).Hex() } - sender := toHex(tx.From()) + sender, _ := tx.From() createsContract := core.MessageCreatesContract(tx) var data string @@ -154,7 +154,7 @@ func NewTx(tx *types.Transaction) *Transaction { data = toHex(tx.Data()) } - return &Transaction{ref: tx, Hash: hash, Value: common.CurrencyToString(tx.Value()), Address: receiver, Contract: createsContract, Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: toHex(tx.Data())} + return &Transaction{ref: tx, Hash: hash, Value: common.CurrencyToString(tx.Value()), Address: receiver, Contract: createsContract, Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender.Hex(), CreatesContract: createsContract, RawData: toHex(tx.Data())} } func (self *Transaction) ToString() string { diff --git a/xeth/xeth.go b/xeth/xeth.go index 6c7a26c04..672b83332 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -8,10 +8,10 @@ import ( "math/big" "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p" @@ -116,14 +116,14 @@ func (self *XEth) State() *State { return self.state } func (self *XEth) Whisper() *Whisper { return self.whisper } func (self *XEth) BlockByHash(strHash string) *Block { - hash := common.FromHex(strHash) + hash := common.HexToHash(strHash) block := self.chainManager.GetBlock(hash) return NewBlock(block) } func (self *XEth) EthBlockByHash(strHash string) *types.Block { - hash := common.FromHex(strHash) + hash := common.HexToHash(strHash) block := self.chainManager.GetBlock(hash) return block @@ -293,9 +293,9 @@ func (self *XEth) PushTx(encodedTx string) (string, error) { if tx.To() == nil { addr := core.AddressFromMessage(tx) - return toHex(addr), nil + return addr.Hex(), nil } - return toHex(tx.Hash()), nil + return tx.Hash().Hex(), nil } var ( @@ -306,8 +306,8 @@ var ( func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr string) (string, error) { statedb := self.State().State() //self.chainManager.TransState() msg := callmsg{ - from: statedb.GetOrNewStateObject(common.FromHex(fromStr)), - to: common.FromHex(toStr), + from: statedb.GetOrNewStateObject(common.HexToAddress(fromStr)), + to: common.HexToAddress(toStr), gas: common.Big(gasStr), gasPrice: common.Big(gasPriceStr), value: common.Big(valueStr), @@ -330,8 +330,8 @@ func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr st func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { var ( - from []byte - to []byte + from = common.HexToAddress(fromStr) + to = common.HexToAddress(toStr) value = common.NewValue(valueStr) gas = common.NewValue(gasStr) price = common.NewValue(gasPriceStr) @@ -339,10 +339,8 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt contractCreation bool ) - from = common.FromHex(fromStr) data = common.FromHex(codeStr) - to = common.FromHex(toStr) - if len(to) == 0 { + if len(toStr) == 0 { contractCreation = true } @@ -368,21 +366,19 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt if contractCreation { addr := core.AddressFromMessage(tx) pipelogger.Infof("Contract addr %x\n", addr) - } - if types.IsContractAddr(to) { - return toHex(core.AddressFromMessage(tx)), nil + return core.AddressFromMessage(tx).Hex(), nil } - return toHex(tx.Hash()), nil + return tx.Hash().Hex(), nil } -func (self *XEth) sign(tx *types.Transaction, from []byte, didUnlock bool) error { - sig, err := self.accountManager.Sign(accounts.Account{Address: from}, tx.Hash()) +func (self *XEth) sign(tx *types.Transaction, from common.Address, didUnlock bool) error { + sig, err := self.accountManager.Sign(accounts.Account{Address: from.Bytes()}, tx.Hash().Bytes()) if err == accounts.ErrLocked { if didUnlock { return fmt.Errorf("sender account still locked after successful unlock") } - if !self.frontend.UnlockAccount(from) { + if !self.frontend.UnlockAccount(from.Bytes()) { return fmt.Errorf("could not unlock sender account") } // retry signing, the account should now be unlocked. @@ -397,17 +393,17 @@ func (self *XEth) sign(tx *types.Transaction, from []byte, didUnlock bool) error // callmsg is the message type used for call transations. type callmsg struct { from *state.StateObject - to []byte + to common.Address gas, gasPrice *big.Int value *big.Int data []byte } // accessor boilerplate to implement core.Message -func (m callmsg) From() []byte { return m.from.Address() } -func (m callmsg) Nonce() uint64 { return m.from.Nonce() } -func (m callmsg) To() []byte { return m.to } -func (m callmsg) GasPrice() *big.Int { return m.gasPrice } -func (m callmsg) Gas() *big.Int { return m.gas } -func (m callmsg) Value() *big.Int { return m.value } -func (m callmsg) Data() []byte { return m.data } +func (m callmsg) From() (common.Address, error) { return m.from.Address(), nil } +func (m callmsg) Nonce() uint64 { return m.from.Nonce() } +func (m callmsg) To() *common.Address { return &m.to } +func (m callmsg) GasPrice() *big.Int { return m.gasPrice } +func (m callmsg) Gas() *big.Int { return m.gas } +func (m callmsg) Value() *big.Int { return m.value } +func (m callmsg) Data() []byte { return m.data }