Skip to content

Commit ea9a549

Browse files
committed
Removed exported fields from state object and added proper set/getters
1 parent 5c975dd commit ea9a549

File tree

9 files changed

+64
-47
lines changed

9 files changed

+64
-47
lines changed

cmd/ethtest/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func StateObjectFromAccount(db ethutil.Database, addr string, account Account) *
5151
if ethutil.IsHex(account.Code) {
5252
account.Code = account.Code[2:]
5353
}
54-
obj.Code = ethutil.Hex2Bytes(account.Code)
55-
obj.Nonce = ethutil.Big(account.Nonce).Uint64()
54+
obj.SetCode(ethutil.Hex2Bytes(account.Code))
55+
obj.SetNonce(ethutil.Big(account.Nonce).Uint64())
5656

5757
return obj
5858
}

cmd/mist/ui_lib.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ func (ui *UiLib) AssetPath(p string) string {
146146
func (self *UiLib) StartDbWithContractAndData(contractHash, data string) {
147147
dbWindow := NewDebuggerWindow(self)
148148
object := self.eth.ChainManager().State().GetStateObject(ethutil.Hex2Bytes(contractHash))
149-
if len(object.Code) > 0 {
150-
dbWindow.SetCode(ethutil.Bytes2Hex(object.Code))
149+
if len(object.Code()) > 0 {
150+
dbWindow.SetCode(ethutil.Bytes2Hex(object.Code()))
151151
}
152152
dbWindow.SetData(data)
153153

core/state_transition.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ func (self *StateTransition) preCheck() (err error) {
138138
)
139139

140140
// Make sure this transaction's nonce is correct
141-
if sender.Nonce != msg.Nonce() {
142-
return NonceError(msg.Nonce(), sender.Nonce)
141+
if sender.Nonce() != msg.Nonce() {
142+
return NonceError(msg.Nonce(), sender.Nonce())
143143
}
144144

145145
// Pre-pay gas / Buy gas of the coinbase account
@@ -166,7 +166,7 @@ func (self *StateTransition) TransitionState() (ret []byte, err error) {
166166
defer self.RefundGas()
167167

168168
// Increment the nonce for the next transaction
169-
self.state.SetNonce(sender.Address(), sender.Nonce+1)
169+
self.state.SetNonce(sender.Address(), sender.Nonce()+1)
170170
//sender.Nonce += 1
171171

172172
// Transaction gas
@@ -242,7 +242,7 @@ func MakeContract(msg Message, state *state.StateDB) *state.StateObject {
242242
addr := AddressFromMessage(msg)
243243

244244
contract := state.GetOrNewStateObject(addr)
245-
contract.InitCode = msg.Data()
245+
contract.SetInitCode(msg.Data())
246246

247247
return contract
248248
}

core/transaction_pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (pool *TxPool) RemoveInvalid(query StateQuery) {
169169
for _, tx := range pool.txs {
170170
sender := query.GetAccount(tx.From())
171171
err := pool.ValidateTransaction(tx)
172-
if err != nil || sender.Nonce >= tx.Nonce() {
172+
if err != nil || sender.Nonce() >= tx.Nonce() {
173173
removedTxs = append(removedTxs, tx)
174174
}
175175
}

state/dump.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (self *StateDB) Dump() []byte {
3030
for it.Next() {
3131
stateObject := NewStateObjectFromBytes(it.Key, it.Value, self.db)
3232

33-
account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.Nonce, Root: ethutil.Bytes2Hex(stateObject.Root()), CodeHash: ethutil.Bytes2Hex(stateObject.codeHash)}
33+
account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: ethutil.Bytes2Hex(stateObject.Root()), CodeHash: ethutil.Bytes2Hex(stateObject.codeHash)}
3434
account.Storage = make(map[string]string)
3535

3636
storageIt := stateObject.State.trie.Iterator()
@@ -50,7 +50,7 @@ func (self *StateDB) Dump() []byte {
5050

5151
// Debug stuff
5252
func (self *StateObject) CreateOutputForDiff() {
53-
fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.Nonce)
53+
fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.nonce)
5454
it := self.State.trie.Iterator()
5555
for it.Next() {
5656
fmt.Printf("%x %x\n", it.Key, it.Value)

state/state_object.go

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ type StateObject struct {
3636
// Shared attributes
3737
balance *big.Int
3838
codeHash []byte
39-
Nonce uint64
39+
nonce uint64
4040
// Contract related attributes
4141
State *StateDB
42-
Code Code
43-
InitCode Code
42+
code Code
43+
initCode Code
4444

4545
storage Storage
4646

@@ -89,20 +89,21 @@ func NewStateObjectFromBytes(address, data []byte, db ethutil.Database) *StateOb
8989

9090
object := &StateObject{address: address, db: db}
9191
//object.RlpDecode(data)
92-
object.Nonce = extobject.Nonce
92+
object.nonce = extobject.Nonce
9393
object.balance = extobject.Balance
9494
object.codeHash = extobject.CodeHash
9595
object.State = New(extobject.Root, db)
9696
object.storage = make(map[string]*ethutil.Value)
9797
object.gasPool = new(big.Int)
98-
object.Code, _ = db.Get(extobject.CodeHash)
98+
object.code, _ = db.Get(extobject.CodeHash)
9999

100100
return object
101101
}
102102

103103
func (self *StateObject) MarkForDeletion() {
104104
self.remove = true
105-
statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.balance)
105+
self.dirty = true
106+
statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.nonce, self.balance)
106107
}
107108

108109
func (c *StateObject) getAddr(addr []byte) *ethutil.Value {
@@ -159,25 +160,24 @@ func (self *StateObject) Sync() {
159160
}
160161

161162
func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
162-
if int64(len(c.Code)-1) < pc.Int64() {
163+
if int64(len(c.code)-1) < pc.Int64() {
163164
return ethutil.NewValue(0)
164165
}
165166

166-
return ethutil.NewValueFromBytes([]byte{c.Code[pc.Int64()]})
167+
return ethutil.NewValueFromBytes([]byte{c.code[pc.Int64()]})
167168
}
168169

169170
func (c *StateObject) AddBalance(amount *big.Int) {
170171
c.SetBalance(new(big.Int).Add(c.balance, amount))
171-
c.dirty = true
172172

173-
statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.balance, amount)
173+
statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.nonce, c.balance, amount)
174174
}
175175
func (c *StateObject) AddAmount(amount *big.Int) { c.AddBalance(amount) }
176176

177177
func (c *StateObject) SubBalance(amount *big.Int) {
178178
c.SetBalance(new(big.Int).Sub(c.balance, amount))
179179

180-
statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.balance, amount)
180+
statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.nonce, c.balance, amount)
181181
}
182182
func (c *StateObject) SubAmount(amount *big.Int) { c.SubBalance(amount) }
183183

@@ -186,8 +186,6 @@ func (c *StateObject) SetBalance(amount *big.Int) {
186186
c.dirty = true
187187
}
188188

189-
func (self *StateObject) Balance() *big.Int { return self.balance }
190-
191189
//
192190
// Gas setters and getters
193191
//
@@ -243,12 +241,12 @@ func (self *StateObject) Copy() *StateObject {
243241
stateObject := NewStateObject(self.Address(), self.db)
244242
stateObject.balance.Set(self.balance)
245243
stateObject.codeHash = ethutil.CopyBytes(self.codeHash)
246-
stateObject.Nonce = self.Nonce
244+
stateObject.nonce = self.nonce
247245
if self.State != nil {
248246
stateObject.State = self.State.Copy()
249247
}
250-
stateObject.Code = ethutil.CopyBytes(self.Code)
251-
stateObject.InitCode = ethutil.CopyBytes(self.InitCode)
248+
stateObject.code = ethutil.CopyBytes(self.code)
249+
stateObject.initCode = ethutil.CopyBytes(self.initCode)
252250
stateObject.storage = self.storage.Copy()
253251
stateObject.gasPool.Set(self.gasPool)
254252
stateObject.remove = self.remove
@@ -265,8 +263,12 @@ func (self *StateObject) Set(stateObject *StateObject) {
265263
// Attribute accessors
266264
//
267265

266+
func (self *StateObject) Balance() *big.Int {
267+
return self.balance
268+
}
269+
268270
func (c *StateObject) N() *big.Int {
269-
return big.NewInt(int64(c.Nonce))
271+
return big.NewInt(int64(c.nonce))
270272
}
271273

272274
// Returns the address of the contract/account
@@ -276,7 +278,7 @@ func (c *StateObject) Address() []byte {
276278

277279
// Returns the initialization Code
278280
func (c *StateObject) Init() Code {
279-
return c.InitCode
281+
return c.initCode
280282
}
281283

282284
func (self *StateObject) Trie() *trie.Trie {
@@ -287,8 +289,27 @@ func (self *StateObject) Root() []byte {
287289
return self.Trie().Root()
288290
}
289291

292+
func (self *StateObject) Code() []byte {
293+
return self.code
294+
}
295+
290296
func (self *StateObject) SetCode(code []byte) {
291-
self.Code = code
297+
self.code = code
298+
self.dirty = true
299+
}
300+
301+
func (self *StateObject) SetInitCode(code []byte) {
302+
self.initCode = code
303+
self.dirty = true
304+
}
305+
306+
func (self *StateObject) SetNonce(nonce uint64) {
307+
self.nonce = nonce
308+
self.dirty = true
309+
}
310+
311+
func (self *StateObject) Nonce() uint64 {
312+
return self.nonce
292313
}
293314

294315
//
@@ -297,24 +318,24 @@ func (self *StateObject) SetCode(code []byte) {
297318

298319
// State object encoding methods
299320
func (c *StateObject) RlpEncode() []byte {
300-
return ethutil.Encode([]interface{}{c.Nonce, c.balance, c.Root(), c.CodeHash()})
321+
return ethutil.Encode([]interface{}{c.nonce, c.balance, c.Root(), c.CodeHash()})
301322
}
302323

303324
func (c *StateObject) CodeHash() ethutil.Bytes {
304-
return crypto.Sha3(c.Code)
325+
return crypto.Sha3(c.code)
305326
}
306327

307328
func (c *StateObject) RlpDecode(data []byte) {
308329
decoder := ethutil.NewValueFromBytes(data)
309-
c.Nonce = decoder.Get(0).Uint()
330+
c.nonce = decoder.Get(0).Uint()
310331
c.balance = decoder.Get(1).BigInt()
311332
c.State = New(decoder.Get(2).Bytes(), c.db) //New(trie.New(ethutil.Config.Db, decoder.Get(2).Interface()))
312333
c.storage = make(map[string]*ethutil.Value)
313334
c.gasPool = new(big.Int)
314335

315336
c.codeHash = decoder.Get(3).Bytes()
316337

317-
c.Code, _ = c.db.Get(c.codeHash)
338+
c.code, _ = c.db.Get(c.codeHash)
318339
}
319340

320341
// Storage change object. Used by the manifest for notifying changes to

state/statedb.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (self *StateDB) AddBalance(addr []byte, amount *big.Int) {
7272
func (self *StateDB) GetNonce(addr []byte) uint64 {
7373
stateObject := self.GetStateObject(addr)
7474
if stateObject != nil {
75-
return stateObject.Nonce
75+
return stateObject.nonce
7676
}
7777

7878
return 0
@@ -81,7 +81,7 @@ func (self *StateDB) GetNonce(addr []byte) uint64 {
8181
func (self *StateDB) GetCode(addr []byte) []byte {
8282
stateObject := self.GetStateObject(addr)
8383
if stateObject != nil {
84-
return stateObject.Code
84+
return stateObject.code
8585
}
8686

8787
return nil
@@ -99,32 +99,28 @@ func (self *StateDB) GetState(a, b []byte) []byte {
9999
func (self *StateDB) SetNonce(addr []byte, nonce uint64) {
100100
stateObject := self.GetStateObject(addr)
101101
if stateObject != nil {
102-
stateObject.Nonce = nonce
103-
stateObject.dirty = true
102+
stateObject.SetNonce(nonce)
104103
}
105104
}
106105

107106
func (self *StateDB) SetCode(addr, code []byte) {
108107
stateObject := self.GetStateObject(addr)
109108
if stateObject != nil {
110109
stateObject.SetCode(code)
111-
stateObject.dirty = true
112110
}
113111
}
114112

115113
func (self *StateDB) SetState(addr, key []byte, value interface{}) {
116114
stateObject := self.GetStateObject(addr)
117115
if stateObject != nil {
118116
stateObject.SetState(key, ethutil.NewValue(value))
119-
stateObject.dirty = true
120117
}
121118
}
122119

123120
func (self *StateDB) Delete(addr []byte) bool {
124121
stateObject := self.GetStateObject(addr)
125122
if stateObject != nil {
126123
stateObject.MarkForDeletion()
127-
stateObject.dirty = true
128124

129125
return true
130126
}
@@ -141,7 +137,7 @@ func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
141137
addr := stateObject.Address()
142138

143139
if len(stateObject.CodeHash()) > 0 {
144-
self.db.Put(stateObject.CodeHash(), stateObject.Code)
140+
self.db.Put(stateObject.CodeHash(), stateObject.code)
145141
}
146142

147143
self.trie.Update(addr, stateObject.RlpEncode())

tests/vm/gh_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func StateObjectFromAccount(db ethutil.Database, addr string, account Account) *
4646
if ethutil.IsHex(account.Code) {
4747
account.Code = account.Code[2:]
4848
}
49-
obj.Code = ethutil.Hex2Bytes(account.Code)
50-
obj.Nonce = ethutil.Big(account.Nonce).Uint64()
49+
obj.SetCode(ethutil.Hex2Bytes(account.Code))
50+
obj.SetNonce(ethutil.Big(account.Nonce).Uint64())
5151

5252
return obj
5353
}

xeth/xeth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ func (self *XEth) BalanceAt(addr string) string {
128128
}
129129

130130
func (self *XEth) TxCountAt(address string) int {
131-
return int(self.State().SafeGet(address).Nonce)
131+
return int(self.State().SafeGet(address).Nonce())
132132
}
133133

134134
func (self *XEth) CodeAt(address string) string {
135-
return toHex(self.State().SafeGet(address).Code)
135+
return toHex(self.State().SafeGet(address).Code())
136136
}
137137

138138
func (self *XEth) IsContract(address string) bool {
139-
return len(self.State().SafeGet(address).Code) > 0
139+
return len(self.State().SafeGet(address).Code()) > 0
140140
}
141141

142142
func (self *XEth) SecretToAddress(key string) string {

0 commit comments

Comments
 (0)