Skip to content

Commit f486c0a

Browse files
committed
new type + additional methods
1 parent b523441 commit f486c0a

7 files changed

Lines changed: 107 additions & 62 deletions

File tree

‎common/bytes.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func RightPadString(str string, l int) string {
211211

212212
}
213213

214-
func Address(slice []byte) (addr []byte) {
214+
func ToAddress(slice []byte) (addr []byte) {
215215
if len(slice) < 20 {
216216
addr = LeftPadBytes(slice, 20)
217217
} else if len(slice) > 20 {

‎common/types.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,39 @@ type (
44
Hash [32]byte
55
Address [20]byte
66
)
7+
8+
// Don't use the default 'String' method in case we want to overwrite
9+
10+
// Get the string representation of the underlying hash
11+
func (h Hash) Str() string {
12+
return string(h[:])
13+
}
14+
15+
// Sets the hash to the value of b. If b is larger than len(h) it will panic
16+
func (h Hash) SetBytes(b []byte) {
17+
if len(b) > len(h) {
18+
panic("unable to set bytes. too big")
19+
}
20+
21+
// reverse loop
22+
for i := len(b); i >= 0; i-- {
23+
h[i] = b[i]
24+
}
25+
}
26+
27+
// Get the string representation of the underlying address
28+
func (a Address) Str() string {
29+
return string(a[:])
30+
}
31+
32+
// Sets the address to the value of b. If b is larger than len(a) it will panic
33+
func (h Address) SetBytes(b []byte) {
34+
if len(b) > len(h) {
35+
panic("unable to set bytes. too big")
36+
}
37+
38+
// reverse loop
39+
for i := len(b); i >= 0; i-- {
40+
h[i] = b[i]
41+
}
42+
}

‎core/types/block.go‎

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

11-
"github.com/ethereum/go-ethereum/crypto"
1211
"github.com/ethereum/go-ethereum/common"
12+
"github.com/ethereum/go-ethereum/crypto"
1313
"github.com/ethereum/go-ethereum/rlp"
1414
)
1515

1616
type Header struct {
1717
// Hash to the previous block
18-
ParentHash []byte
18+
ParentHash common.Hash
1919
// Uncles of this block
20-
UncleHash []byte
20+
UncleHash common.Hash
2121
// The coin base address
22-
Coinbase []byte
22+
Coinbase common.Address
2323
// Block Trie state
24-
Root []byte
24+
Root common.Hash
2525
// Tx sha
26-
TxHash []byte
26+
TxHash common.Hash
2727
// Receipt sha
28-
ReceiptHash []byte
28+
ReceiptHash common.Hash
2929
// Bloom
30-
Bloom []byte
30+
Bloom [256]byte
3131
// Difficulty for the current block
3232
Difficulty *big.Int
3333
// The block number
@@ -41,9 +41,9 @@ type Header struct {
4141
// Extra data
4242
Extra string
4343
// Mix digest for quick checking to prevent DOS
44-
MixDigest []byte
44+
MixDigest common.Hash
4545
// Nonce
46-
Nonce []byte
46+
Nonce [8]byte
4747
}
4848

4949
func (self *Header) rlpData(withNonce bool) []interface{} {
@@ -206,13 +206,13 @@ func (self *Block) SetNonce(nonce uint64) {
206206
self.header.setNonce(nonce)
207207
}
208208

209-
func (self *Block) Bloom() []byte { return self.header.Bloom }
210-
func (self *Block) Coinbase() []byte { return self.header.Coinbase }
211-
func (self *Block) Time() int64 { return int64(self.header.Time) }
212-
func (self *Block) GasLimit() *big.Int { return self.header.GasLimit }
213-
func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
214-
func (self *Block) Root() []byte { return self.header.Root }
215-
func (self *Block) SetRoot(root []byte) { self.header.Root = root }
209+
func (self *Block) Bloom() []byte { return self.header.Bloom }
210+
func (self *Block) Coinbase() []byte { return self.header.Coinbase }
211+
func (self *Block) Time() int64 { return int64(self.header.Time) }
212+
func (self *Block) GasLimit() *big.Int { return self.header.GasLimit }
213+
func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
214+
func (self *Block) Root() []byte { return self.header.Root }
215+
func (self *Block) SetRoot(root []byte) { self.header.Root = root }
216216
func (self *Block) Size() common.StorageSize { return common.StorageSize(len(common.Encode(self))) }
217217
func (self *Block) GetTransaction(i int) *Transaction {
218218
if len(self.transactions) > i {

‎state/managed_state.go‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package state
22

3-
import "sync"
3+
import (
4+
"sync"
5+
6+
"github.com/ethereum/go-ethereum/common"
7+
)
48

59
type account struct {
610
stateObject *StateObject
@@ -29,7 +33,7 @@ func (ms *ManagedState) SetState(statedb *StateDB) {
2933
ms.StateDB = statedb
3034
}
3135

32-
func (ms *ManagedState) RemoveNonce(addr []byte, n uint64) {
36+
func (ms *ManagedState) RemoveNonce(addr common.Address, n uint64) {
3337
if ms.hasAccount(addr) {
3438
ms.mu.Lock()
3539
defer ms.mu.Unlock()
@@ -43,7 +47,7 @@ func (ms *ManagedState) RemoveNonce(addr []byte, n uint64) {
4347
}
4448
}
4549

46-
func (ms *ManagedState) NewNonce(addr []byte) uint64 {
50+
func (ms *ManagedState) NewNonce(addr common.Address) uint64 {
4751
ms.mu.RLock()
4852
defer ms.mu.RUnlock()
4953

@@ -57,26 +61,27 @@ func (ms *ManagedState) NewNonce(addr []byte) uint64 {
5761
return uint64(len(account.nonces)) + account.nstart
5862
}
5963

60-
func (ms *ManagedState) hasAccount(addr []byte) bool {
61-
_, ok := ms.accounts[string(addr)]
64+
func (ms *ManagedState) hasAccount(addr common.Address) bool {
65+
_, ok := ms.accounts[addr.Str()]
6266
return ok
6367
}
6468

65-
func (ms *ManagedState) getAccount(addr []byte) *account {
66-
if account, ok := ms.accounts[string(addr)]; !ok {
69+
func (ms *ManagedState) getAccount(addr common.Address) *account {
70+
straddr := addr.Str()
71+
if account, ok := ms.accounts[straddr]; !ok {
6772
so := ms.GetOrNewStateObject(addr)
68-
ms.accounts[string(addr)] = newAccount(so)
73+
ms.accounts[straddr] = newAccount(so)
6974
} else {
7075
// Always make sure the state account nonce isn't actually higher
7176
// than the tracked one.
7277
so := ms.StateDB.GetStateObject(addr)
7378
if so != nil && uint64(len(account.nonces))+account.nstart < so.nonce {
74-
ms.accounts[string(addr)] = newAccount(so)
79+
ms.accounts[straddr] = newAccount(so)
7580
}
7681

7782
}
7883

79-
return ms.accounts[string(addr)]
84+
return ms.accounts[straddr]
8085
}
8186

8287
func newAccount(so *StateObject) *account {

‎state/state_object.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"math/big"
77

8-
"github.com/ethereum/go-ethereum/crypto"
98
"github.com/ethereum/go-ethereum/common"
9+
"github.com/ethereum/go-ethereum/crypto"
1010
"github.com/ethereum/go-ethereum/rlp"
1111
"github.com/ethereum/go-ethereum/trie"
1212
)
@@ -44,7 +44,7 @@ type StateObject struct {
4444
State *StateDB
4545

4646
// Address belonging to this account
47-
address []byte
47+
address common.Address
4848
// The balance of the account
4949
balance *big.Int
5050
// The nonce of the account
@@ -77,9 +77,9 @@ func (self *StateObject) Reset() {
7777
self.State.Reset()
7878
}
7979

80-
func NewStateObject(addr []byte, db common.Database) *StateObject {
80+
func NewStateObject(address common.Address, db common.Database) *StateObject {
8181
// This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter.
82-
address := common.Address(addr)
82+
//address := common.ToAddress(addr)
8383

8484
object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true}
8585
object.State = New(nil, db) //New(trie.New(common.Config.Db, ""))
@@ -90,12 +90,12 @@ func NewStateObject(addr []byte, db common.Database) *StateObject {
9090
return object
9191
}
9292

93-
func NewStateObjectFromBytes(address, data []byte, db common.Database) *StateObject {
93+
func NewStateObjectFromBytes(address common.Address, data []byte, db common.Database) *StateObject {
9494
// TODO clean me up
9595
var extobject struct {
9696
Nonce uint64
9797
Balance *big.Int
98-
Root []byte
98+
Root common.Hash
9999
CodeHash []byte
100100
}
101101
err := rlp.Decode(bytes.NewReader(data), &extobject)
@@ -284,7 +284,7 @@ func (c *StateObject) N() *big.Int {
284284
}
285285

286286
// Returns the address of the contract/account
287-
func (c *StateObject) Address() []byte {
287+
func (c *StateObject) Address() common.Address {
288288
return c.address
289289
}
290290

‎state/statedb.go‎

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ func (self *StateDB) Logs() Logs {
4545
return self.logs
4646
}
4747

48-
func (self *StateDB) Refund(addr []byte, gas *big.Int) {
49-
if self.refund[string(addr)] == nil {
50-
self.refund[string(addr)] = new(big.Int)
48+
func (self *StateDB) Refund(address common.Address, gas *big.Int) {
49+
addr := address.Str()
50+
if self.refund[addr] == nil {
51+
self.refund[addr] = new(big.Int)
5152
}
52-
self.refund[string(addr)].Add(self.refund[string(addr)], gas)
53+
self.refund[addr].Add(self.refund[addr], gas)
5354
}
5455

5556
// Retrieve the balance from the given address or 0 if object not found
56-
func (self *StateDB) GetBalance(addr []byte) *big.Int {
57+
func (self *StateDB) GetBalance(addr common.Address) *big.Int {
5758
stateObject := self.GetStateObject(addr)
5859
if stateObject != nil {
5960
return stateObject.balance
@@ -62,14 +63,14 @@ func (self *StateDB) GetBalance(addr []byte) *big.Int {
6263
return common.Big0
6364
}
6465

65-
func (self *StateDB) AddBalance(addr []byte, amount *big.Int) {
66+
func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
6667
stateObject := self.GetStateObject(addr)
6768
if stateObject != nil {
6869
stateObject.AddBalance(amount)
6970
}
7071
}
7172

72-
func (self *StateDB) GetNonce(addr []byte) uint64 {
73+
func (self *StateDB) GetNonce(addr common.Address) uint64 {
7374
stateObject := self.GetStateObject(addr)
7475
if stateObject != nil {
7576
return stateObject.nonce
@@ -78,7 +79,7 @@ func (self *StateDB) GetNonce(addr []byte) uint64 {
7879
return 0
7980
}
8081

81-
func (self *StateDB) GetCode(addr []byte) []byte {
82+
func (self *StateDB) GetCode(addr common.Address) []byte {
8283
stateObject := self.GetStateObject(addr)
8384
if stateObject != nil {
8485
return stateObject.code
@@ -87,7 +88,7 @@ func (self *StateDB) GetCode(addr []byte) []byte {
8788
return nil
8889
}
8990

90-
func (self *StateDB) GetState(a, b []byte) []byte {
91+
func (self *StateDB) GetState(a common.Adress, b common.Hash) []byte {
9192
stateObject := self.GetStateObject(a)
9293
if stateObject != nil {
9394
return stateObject.GetState(b).Bytes()
@@ -96,28 +97,28 @@ func (self *StateDB) GetState(a, b []byte) []byte {
9697
return nil
9798
}
9899

99-
func (self *StateDB) SetNonce(addr []byte, nonce uint64) {
100+
func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
100101
stateObject := self.GetStateObject(addr)
101102
if stateObject != nil {
102103
stateObject.SetNonce(nonce)
103104
}
104105
}
105106

106-
func (self *StateDB) SetCode(addr, code []byte) {
107+
func (self *StateDB) SetCode(addr common.Address, code []byte) {
107108
stateObject := self.GetStateObject(addr)
108109
if stateObject != nil {
109110
stateObject.SetCode(code)
110111
}
111112
}
112113

113-
func (self *StateDB) SetState(addr, key []byte, value interface{}) {
114+
func (self *StateDB) SetState(addr common.Address, key common.Hash, value interface{}) {
114115
stateObject := self.GetStateObject(addr)
115116
if stateObject != nil {
116117
stateObject.SetState(key, common.NewValue(value))
117118
}
118119
}
119120

120-
func (self *StateDB) Delete(addr []byte) bool {
121+
func (self *StateDB) Delete(addr common.Address) bool {
121122
stateObject := self.GetStateObject(addr)
122123
if stateObject != nil {
123124
stateObject.MarkForDeletion()
@@ -129,7 +130,7 @@ func (self *StateDB) Delete(addr []byte) bool {
129130
return false
130131
}
131132

132-
func (self *StateDB) IsDeleted(addr []byte) bool {
133+
func (self *StateDB) IsDeleted(addr common.Address) bool {
133134
stateObject := self.GetStateObject(addr)
134135
if stateObject != nil {
135136
return stateObject.remove
@@ -143,27 +144,27 @@ func (self *StateDB) IsDeleted(addr []byte) bool {
143144

144145
// Update the given state object and apply it to state trie
145146
func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
146-
addr := stateObject.Address()
147+
//addr := stateObject.Address()
147148

148149
if len(stateObject.CodeHash()) > 0 {
149150
self.db.Put(stateObject.CodeHash(), stateObject.code)
150151
}
151152

152-
self.trie.Update(addr, stateObject.RlpEncode())
153+
self.trie.Update(stateObject.Address(), stateObject.RlpEncode())
153154
}
154155

155156
// Delete the given state object and delete it from the state trie
156157
func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
157158
self.trie.Delete(stateObject.Address())
158159

159-
delete(self.stateObjects, string(stateObject.Address()))
160+
delete(self.stateObjects, stateObject.Address().Str())
160161
}
161162

162163
// Retrieve a state object given my the address. Nil if not found
163-
func (self *StateDB) GetStateObject(addr []byte) *StateObject {
164-
addr = common.Address(addr)
164+
func (self *StateDB) GetStateObject(addr common.Address) *StateObject {
165+
//addr = common.Address(addr)
165166

166-
stateObject := self.stateObjects[string(addr)]
167+
stateObject := self.stateObjects[addr.Str()]
167168
if stateObject != nil {
168169
return stateObject
169170
}
@@ -180,11 +181,11 @@ func (self *StateDB) GetStateObject(addr []byte) *StateObject {
180181
}
181182

182183
func (self *StateDB) SetStateObject(object *StateObject) {
183-
self.stateObjects[string(object.address)] = object
184+
self.stateObjects[object.Address().Str()] = object
184185
}
185186

186187
// Retrieve a state object or create a new state object if nil
187-
func (self *StateDB) GetOrNewStateObject(addr []byte) *StateObject {
188+
func (self *StateDB) GetOrNewStateObject(addr common.Address) *StateObject {
188189
stateObject := self.GetStateObject(addr)
189190
if stateObject == nil {
190191
stateObject = self.NewStateObject(addr)
@@ -194,8 +195,8 @@ func (self *StateDB) GetOrNewStateObject(addr []byte) *StateObject {
194195
}
195196

196197
// Create a state object whether it exist in the trie or not
197-
func (self *StateDB) NewStateObject(addr []byte) *StateObject {
198-
addr = common.Address(addr)
198+
func (self *StateDB) NewStateObject(addr common.Address) *StateObject {
199+
//addr = common.Address(addr)
199200

200201
statelogger.Debugf("(+) %x\n", addr)
201202

0 commit comments

Comments
 (0)