Skip to content

Commit 0a1eeca

Browse files
committed
conversions. -compilable-
1 parent 9429806 commit 0a1eeca

12 files changed

Lines changed: 139 additions & 130 deletions

File tree

‎cmd/ethereum/admin.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"time"
99

1010
"github.com/ethereum/go-ethereum/cmd/utils"
11-
"github.com/ethereum/go-ethereum/core/types"
1211
"github.com/ethereum/go-ethereum/common"
12+
"github.com/ethereum/go-ethereum/core/types"
1313
"github.com/ethereum/go-ethereum/rlp"
1414
"github.com/ethereum/go-ethereum/rpc"
1515
"github.com/ethereum/go-ethereum/state"
@@ -239,7 +239,7 @@ func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value {
239239
block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num))
240240
} else if call.Argument(0).IsString() {
241241
hash, _ := call.Argument(0).ToString()
242-
block = js.ethereum.ChainManager().GetBlock(common.Hex2Bytes(hash))
242+
block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash))
243243
} else {
244244
fmt.Println("invalid argument for dump. Either hex string or number")
245245
}

‎cmd/ethereum/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func dump(ctx *cli.Context) {
302302
for _, arg := range ctx.Args() {
303303
var block *types.Block
304304
if hashish(arg) {
305-
block = chainmgr.GetBlock(common.Hex2Bytes(arg))
305+
block = chainmgr.GetBlock(common.HexToHash(arg))
306306
} else {
307307
num, _ := strconv.Atoi(arg)
308308
block = chainmgr.GetBlockByNumber(uint64(num))

‎core/state_transition.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ type Message interface {
5757
Data() []byte
5858
}
5959

60+
func AddressFromMessage(msg Message) common.Address {
61+
from, _ := msg.From()
62+
63+
return crypto.CreateAddress(from, msg.Nonce())
64+
}
65+
6066
func MessageCreatesContract(msg Message) bool {
6167
return msg.To() == nil
6268
}

‎eth/backend.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"github.com/ethereum/ethash"
1111
"github.com/ethereum/go-ethereum/accounts"
1212
"github.com/ethereum/go-ethereum/blockpool"
13+
"github.com/ethereum/go-ethereum/common"
1314
"github.com/ethereum/go-ethereum/core"
1415
"github.com/ethereum/go-ethereum/core/types"
1516
"github.com/ethereum/go-ethereum/crypto"
1617
"github.com/ethereum/go-ethereum/ethdb"
17-
"github.com/ethereum/go-ethereum/common"
1818
"github.com/ethereum/go-ethereum/event"
1919
"github.com/ethereum/go-ethereum/logger"
2020
"github.com/ethereum/go-ethereum/miner"
@@ -288,7 +288,7 @@ func (s *Ethereum) StartMining() error {
288288
servlogger.Errorf("Cannot start mining without coinbase: %v\n", err)
289289
return fmt.Errorf("no coinbase: %v", err)
290290
}
291-
s.miner.Start(cb)
291+
s.miner.Start(common.BytesToAddress(cb))
292292
return nil
293293
}
294294

@@ -304,9 +304,9 @@ func (s *Ethereum) TxPool() *core.TxPool { return s.txPool }
304304
func (s *Ethereum) BlockPool() *blockpool.BlockPool { return s.blockPool }
305305
func (s *Ethereum) Whisper() *whisper.Whisper { return s.whisper }
306306
func (s *Ethereum) EventMux() *event.TypeMux { return s.eventMux }
307-
func (s *Ethereum) BlockDb() common.Database { return s.blockDb }
308-
func (s *Ethereum) StateDb() common.Database { return s.stateDb }
309-
func (s *Ethereum) ExtraDb() common.Database { return s.extraDb }
307+
func (s *Ethereum) BlockDb() common.Database { return s.blockDb }
308+
func (s *Ethereum) StateDb() common.Database { return s.stateDb }
309+
func (s *Ethereum) ExtraDb() common.Database { return s.extraDb }
310310
func (s *Ethereum) IsListening() bool { return true } // Always listening
311311
func (s *Ethereum) PeerCount() int { return s.net.PeerCount() }
312312
func (s *Ethereum) PeerInfo() int { return s.net.PeerCount() }

‎eth/protocol.go‎

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package eth
22

33
import (
4-
"bytes"
54
"fmt"
65
"math/big"
76

7+
"github.com/ethereum/go-ethereum/common"
88
"github.com/ethereum/go-ethereum/core/types"
99
"github.com/ethereum/go-ethereum/errs"
10-
"github.com/ethereum/go-ethereum/common"
1110
"github.com/ethereum/go-ethereum/logger"
1211
"github.com/ethereum/go-ethereum/p2p"
1312
"github.com/ethereum/go-ethereum/rlp"
@@ -76,15 +75,15 @@ type txPool interface {
7675
}
7776

7877
type chainManager interface {
79-
GetBlockHashesFromHash(hash []byte, amount uint64) (hashes [][]byte)
80-
GetBlock(hash []byte) (block *types.Block)
81-
Status() (td *big.Int, currentBlock []byte, genesisBlock []byte)
78+
GetBlockHashesFromHash(hash common.Hash, amount uint64) (hashes []common.Hash)
79+
GetBlock(hash common.Hash) (block *types.Block)
80+
Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash)
8281
}
8382

8483
type blockPool interface {
85-
AddBlockHashes(next func() ([]byte, bool), peerId string)
84+
AddBlockHashes(next func() (common.Hash, bool), peerId string)
8685
AddBlock(block *types.Block, peerId string)
87-
AddPeer(td *big.Int, currentBlock []byte, peerId string, requestHashes func([]byte) error, requestBlocks func([][]byte) error, peerError func(*errs.Error)) (best bool)
86+
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)
8887
RemovePeer(peerId string)
8988
}
9089

@@ -95,7 +94,7 @@ type newBlockMsgData struct {
9594
}
9695

9796
type getBlockHashesMsgData struct {
98-
Hash []byte
97+
Hash common.Hash
9998
Amount uint64
10099
}
101100

@@ -167,7 +166,7 @@ func (self *ethProtocol) handle() error {
167166
}
168167
for _, tx := range txs {
169168
jsonlogger.LogJson(&logger.EthTxReceived{
170-
TxHash: common.Bytes2Hex(tx.Hash()),
169+
TxHash: tx.Hash().Hex(),
171170
RemoteId: self.peer.ID().String(),
172171
})
173172
}
@@ -183,7 +182,7 @@ func (self *ethProtocol) handle() error {
183182
request.Amount = maxHashes
184183
}
185184
hashes := self.chainManager.GetBlockHashesFromHash(request.Hash, request.Amount)
186-
return p2p.EncodeMsg(self.rw, BlockHashesMsg, common.ByteSliceToInterface(hashes)...)
185+
return p2p.EncodeMsg(self.rw, BlockHashesMsg, rlp.Flat(hashes))
187186

188187
case BlockHashesMsg:
189188
msgStream := rlp.NewStream(msg.Payload)
@@ -192,14 +191,16 @@ func (self *ethProtocol) handle() error {
192191
}
193192

194193
var i int
195-
iter := func() (hash []byte, ok bool) {
196-
hash, err := msgStream.Bytes()
194+
iter := func() (hash common.Hash, ok bool) {
195+
var h common.Hash
196+
err := msgStream.Decode(&h)
197197
if err == rlp.EOL {
198-
return nil, false
198+
return common.Hash{}, false
199199
} else if err != nil {
200200
self.protoError(ErrDecode, "msg %v: after %v hashes : %v", msg, i, err)
201-
return nil, false
201+
return common.Hash{}, false
202202
}
203+
203204
i++
204205
return hash, true
205206
}
@@ -215,14 +216,14 @@ func (self *ethProtocol) handle() error {
215216
var i int
216217
for {
217218
i++
218-
var hash []byte
219-
if err := msgStream.Decode(&hash); err != nil {
220-
if err == rlp.EOL {
221-
break
222-
} else {
223-
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
224-
}
219+
var hash common.Hash
220+
err := msgStream.Decode(&hash)
221+
if err == rlp.EOL {
222+
break
223+
} else {
224+
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
225225
}
226+
226227
block := self.chainManager.GetBlock(hash)
227228
if block != nil {
228229
blocks = append(blocks, block)
@@ -259,10 +260,10 @@ func (self *ethProtocol) handle() error {
259260
_, chainHead, _ := self.chainManager.Status()
260261

261262
jsonlogger.LogJson(&logger.EthChainReceivedNewBlock{
262-
BlockHash: common.Bytes2Hex(hash),
263+
BlockHash: hash.Hex(),
263264
BlockNumber: request.Block.Number(), // this surely must be zero
264-
ChainHeadHash: common.Bytes2Hex(chainHead),
265-
BlockPrevHash: common.Bytes2Hex(request.Block.ParentHash()),
265+
ChainHeadHash: chainHead.Hex(),
266+
BlockPrevHash: request.Block.ParentHash().Hex(),
266267
RemoteId: self.peer.ID().String(),
267268
})
268269
// to simplify backend interface adding a new block
@@ -282,8 +283,8 @@ type statusMsgData struct {
282283
ProtocolVersion uint32
283284
NetworkId uint32
284285
TD *big.Int
285-
CurrentBlock []byte
286-
GenesisBlock []byte
286+
CurrentBlock common.Hash
287+
GenesisBlock common.Hash
287288
}
288289

289290
func (self *ethProtocol) statusMsg() p2p.Msg {
@@ -325,7 +326,7 @@ func (self *ethProtocol) handleStatus() error {
325326

326327
_, _, genesisBlock := self.chainManager.Status()
327328

328-
if !bytes.Equal(status.GenesisBlock, genesisBlock) {
329+
if status.GenesisBlock != genesisBlock {
329330
return self.protoError(ErrGenesisBlockMismatch, "%x (!= %x)", status.GenesisBlock, genesisBlock)
330331
}
331332

@@ -344,14 +345,14 @@ func (self *ethProtocol) handleStatus() error {
344345
return nil
345346
}
346347

347-
func (self *ethProtocol) requestBlockHashes(from []byte) error {
348+
func (self *ethProtocol) requestBlockHashes(from common.Hash) error {
348349
self.peer.Debugf("fetching hashes (%d) %x...\n", maxHashes, from[0:4])
349350
return p2p.EncodeMsg(self.rw, GetBlockHashesMsg, interface{}(from), uint64(maxHashes))
350351
}
351352

352-
func (self *ethProtocol) requestBlocks(hashes [][]byte) error {
353+
func (self *ethProtocol) requestBlocks(hashes []common.Hash) error {
353354
self.peer.Debugf("fetching %v blocks", len(hashes))
354-
return p2p.EncodeMsg(self.rw, GetBlocksMsg, common.ByteSliceToInterface(hashes)...)
355+
return p2p.EncodeMsg(self.rw, GetBlocksMsg, rlp.Flat(hashes))
355356
}
356357

357358
func (self *ethProtocol) protoError(code int, format string, params ...interface{}) (err *errs.Error) {

‎miner/miner.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"math/big"
55

66
"github.com/ethereum/ethash"
7+
"github.com/ethereum/go-ethereum/common"
78
"github.com/ethereum/go-ethereum/core"
89
"github.com/ethereum/go-ethereum/logger"
910
"github.com/ethereum/go-ethereum/pow"
@@ -32,7 +33,7 @@ func (self *Miner) Mining() bool {
3233
return self.mining
3334
}
3435

35-
func (self *Miner) Start(coinbase []byte) {
36+
func (self *Miner) Start(coinbase common.Address) {
3637
self.mining = true
3738
self.worker = newWorker(coinbase, self.eth)
3839
self.worker.register(NewCpuMiner(0, self.pow))

‎miner/worker.go‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"sync"
88
"time"
99

10+
"github.com/ethereum/go-ethereum/common"
1011
"github.com/ethereum/go-ethereum/core"
1112
"github.com/ethereum/go-ethereum/core/types"
12-
"github.com/ethereum/go-ethereum/common"
1313
"github.com/ethereum/go-ethereum/event"
1414
"github.com/ethereum/go-ethereum/logger"
1515
"github.com/ethereum/go-ethereum/pow"
@@ -39,7 +39,7 @@ func env(block *types.Block, eth core.Backend) *environment {
3939
coinbase: state.GetOrNewStateObject(block.Coinbase()),
4040
}
4141
for _, ancestor := range eth.ChainManager().GetAncestors(block, 7) {
42-
env.ancestors.Add(string(ancestor.Hash()))
42+
env.ancestors.Add(ancestor.Hash())
4343
}
4444

4545
return env
@@ -71,14 +71,14 @@ type worker struct {
7171
eth core.Backend
7272
chain *core.ChainManager
7373
proc *core.BlockProcessor
74-
coinbase []byte
74+
coinbase common.Address
7575

7676
current *environment
7777

7878
mining bool
7979
}
8080

81-
func newWorker(coinbase []byte, eth core.Backend) *worker {
81+
func newWorker(coinbase common.Address, eth core.Backend) *worker {
8282
return &worker{
8383
eth: eth,
8484
mux: eth.EventMux(),
@@ -152,13 +152,13 @@ func (self *worker) wait() {
152152
block := self.current.block
153153
if block.Number().Uint64() == work.Number && block.Nonce() == 0 {
154154
self.current.block.SetNonce(work.Nonce)
155-
self.current.block.Header().MixDigest = work.MixDigest
155+
self.current.block.Header().MixDigest = common.BytesToHash(work.MixDigest)
156156

157157
jsonlogger.LogJson(&logger.EthMinerNewBlock{
158-
BlockHash: common.Bytes2Hex(block.Hash()),
158+
BlockHash: block.Hash().Hex(),
159159
BlockNumber: block.Number(),
160-
ChainHeadHash: common.Bytes2Hex(block.ParentHeaderHash),
161-
BlockPrevHash: common.Bytes2Hex(block.ParentHeaderHash),
160+
ChainHeadHash: block.ParentHeaderHash.Hex(),
161+
BlockPrevHash: block.ParentHeaderHash.Hex(),
162162
})
163163

164164
if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil {
@@ -208,9 +208,10 @@ gasLimit:
208208
fallthrough
209209
case core.IsInvalidTxErr(err):
210210
// Remove invalid transactions
211-
self.chain.TxState().RemoveNonce(tx.From(), tx.Nonce())
211+
from, _ := tx.From()
212+
self.chain.TxState().RemoveNonce(from, tx.Nonce())
212213
remove = append(remove, tx)
213-
minerlogger.Infof("TX (%x) failed. Transaction will be removed\n", tx.Hash()[:4])
214+
minerlogger.Infof("TX (%x) failed. Transaction will be removed\n", tx.Hash().Bytes()[:4])
214215
case state.IsGasLimitErr(err):
215216
minerlogger.Infof("Gas limit reached for block. %d TXs included in this block\n", i)
216217
// Break on gas limit
@@ -232,13 +233,13 @@ var (
232233
)
233234

234235
func (self *worker) commitUncle(uncle *types.Header) error {
235-
if self.current.uncles.Has(string(uncle.Hash())) {
236+
if self.current.uncles.Has(uncle.Hash()) {
236237
// Error not unique
237238
return core.UncleError("Uncle not unique")
238239
}
239-
self.current.uncles.Add(string(uncle.Hash()))
240+
self.current.uncles.Add(uncle.Hash())
240241

241-
if !self.current.ancestors.Has(string(uncle.ParentHash)) {
242+
if !self.current.ancestors.Has(uncle.ParentHash) {
242243
return core.UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4]))
243244
}
244245

‎rpc/api.go‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
668668
return NewValidationError("Index", "does not exist")
669669
}
670670

671-
uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false)
671+
uncle, err := p.GetBlockByHash(v.Uncles[args.Index].Hex(), false)
672672
if err != nil {
673673
return err
674674
}
@@ -687,7 +687,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
687687
return NewValidationError("Index", "does not exist")
688688
}
689689

690-
uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false)
690+
uncle, err := p.GetBlockByHash(v.Uncles[args.Index].Hex(), false)
691691
if err != nil {
692692
return err
693693
}
@@ -832,12 +832,12 @@ func toFilterOptions(options *FilterOptions) core.FilterOptions {
832832

833833
// Convert optional address slice/string to byte slice
834834
if str, ok := options.Address.(string); ok {
835-
opts.Address = [][]byte{common.FromHex(str)}
835+
opts.Address = []common.Address{common.HexToAddress(str)}
836836
} else if slice, ok := options.Address.([]interface{}); ok {
837-
bslice := make([][]byte, len(slice))
837+
bslice := make([]common.Address, len(slice))
838838
for i, addr := range slice {
839839
if saddr, ok := addr.(string); ok {
840-
bslice[i] = common.FromHex(saddr)
840+
bslice[i] = common.HexToAddress(saddr)
841841
}
842842
}
843843
opts.Address = bslice
@@ -846,16 +846,15 @@ func toFilterOptions(options *FilterOptions) core.FilterOptions {
846846
opts.Earliest = options.Earliest
847847
opts.Latest = options.Latest
848848

849-
topics := make([][][]byte, len(options.Topics))
849+
topics := make([][]common.Hash, len(options.Topics))
850850
for i, topicDat := range options.Topics {
851851
if slice, ok := topicDat.([]interface{}); ok {
852-
topics[i] = make([][]byte, len(slice))
852+
topics[i] = make([]common.Hash, len(slice))
853853
for j, topic := range slice {
854-
topics[i][j] = common.FromHex(topic.(string))
854+
topics[i][j] = common.HexToHash(topic.(string))
855855
}
856856
} else if str, ok := topicDat.(string); ok {
857-
topics[i] = make([][]byte, 1)
858-
topics[i][0] = common.FromHex(str)
857+
topics[i] = []common.Hash{common.HexToHash(str)}
859858
}
860859
}
861860
opts.Topics = topics

0 commit comments

Comments
 (0)