Skip to content

Commit 024d41d

Browse files
obscurenfjl
authored andcommitted
core, core/state, core/vm: remove exported account getters (bnb-chain#3618)
Removed exported statedb object accessors, reducing the chance for nasty bugs to creep in. It's also ugly and unnecessary to have these methods.
1 parent 46ec435 commit 024d41d

23 files changed

Lines changed: 245 additions & 238 deletions

‎cmd/evm/main.go‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/ethereum/go-ethereum/core/state"
3030
"github.com/ethereum/go-ethereum/core/vm"
3131
"github.com/ethereum/go-ethereum/core/vm/runtime"
32-
"github.com/ethereum/go-ethereum/crypto"
3332
"github.com/ethereum/go-ethereum/ethdb"
3433
"github.com/ethereum/go-ethereum/logger/glog"
3534
"gopkg.in/urfave/cli.v1"
@@ -115,9 +114,13 @@ func run(ctx *cli.Context) error {
115114
glog.SetToStderr(true)
116115
glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
117116

118-
db, _ := ethdb.NewMemDatabase()
119-
statedb, _ := state.New(common.Hash{}, db)
120-
sender := statedb.CreateAccount(common.StringToAddress("sender"))
117+
var (
118+
db, _ = ethdb.NewMemDatabase()
119+
statedb, _ = state.New(common.Hash{}, db)
120+
address = common.StringToAddress("sender")
121+
sender = vm.AccountRef(address)
122+
)
123+
statedb.CreateAccount(common.StringToAddress("sender"))
121124

122125
logger := vm.NewStructLogger(nil)
123126

@@ -166,10 +169,11 @@ func run(ctx *cli.Context) error {
166169
},
167170
})
168171
} else {
169-
receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
170-
receiver.SetCode(crypto.Keccak256Hash(code), code)
172+
receiverAddress := common.StringToAddress("receiver")
173+
statedb.CreateAccount(receiverAddress)
174+
statedb.SetCode(receiverAddress, code)
171175

172-
ret, err = runtime.Call(receiver.Address(), common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtime.Config{
176+
ret, err = runtime.Call(receiverAddress, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtime.Config{
173177
Origin: sender.Address(),
174178
State: statedb,
175179
GasLimit: common.Big(ctx.GlobalString(GasFlag.Name)).Uint64(),

‎core/blockchain.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ func (self *BlockChain) loadLastState() error {
214214
return err
215215
}
216216
self.stateCache = statedb
217-
self.stateCache.GetAccount(common.Address{})
218217

219218
// Issue a status log for the user
220219
headerTd := self.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64())

‎core/dao.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ func ValidateDAOHeaderExtraData(config *params.ChainConfig, header *types.Header
6262
// contract.
6363
func ApplyDAOHardFork(statedb *state.StateDB) {
6464
// Retrieve the contract to refund balances into
65-
refund := statedb.GetOrNewStateObject(params.DAORefundContract)
65+
if !statedb.Exist(params.DAORefundContract) {
66+
statedb.CreateAccount(params.DAORefundContract)
67+
}
6668

6769
// Move every DAO account and extra-balance account funds into the refund contract
6870
for _, addr := range params.DAODrainList {
69-
if account := statedb.GetStateObject(addr); account != nil {
70-
refund.AddBalance(account.Balance())
71-
account.SetBalance(new(big.Int))
72-
}
71+
statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr))
72+
statedb.SetBalance(addr, new(big.Int))
7373
}
7474
}

‎core/state/journal.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type (
3434
account *common.Address
3535
}
3636
resetObjectChange struct {
37-
prev *StateObject
37+
prev *stateObject
3838
}
3939
suicideChange struct {
4040
account *common.Address
@@ -86,7 +86,7 @@ func (ch resetObjectChange) undo(s *StateDB) {
8686
}
8787

8888
func (ch suicideChange) undo(s *StateDB) {
89-
obj := s.GetStateObject(*ch.account)
89+
obj := s.getStateObject(*ch.account)
9090
if obj != nil {
9191
obj.suicided = ch.prev
9292
obj.setBalance(ch.prevbalance)
@@ -103,19 +103,19 @@ func (ch touchChange) undo(s *StateDB) {
103103
}
104104

105105
func (ch balanceChange) undo(s *StateDB) {
106-
s.GetStateObject(*ch.account).setBalance(ch.prev)
106+
s.getStateObject(*ch.account).setBalance(ch.prev)
107107
}
108108

109109
func (ch nonceChange) undo(s *StateDB) {
110-
s.GetStateObject(*ch.account).setNonce(ch.prev)
110+
s.getStateObject(*ch.account).setNonce(ch.prev)
111111
}
112112

113113
func (ch codeChange) undo(s *StateDB) {
114-
s.GetStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
114+
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
115115
}
116116

117117
func (ch storageChange) undo(s *StateDB) {
118-
s.GetStateObject(*ch.account).setState(ch.key, ch.prevalue)
118+
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
119119
}
120120

121121
func (ch refundChange) undo(s *StateDB) {

‎core/state/managed_state.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
type account struct {
26-
stateObject *StateObject
26+
stateObject *stateObject
2727
nstart uint64
2828
nonces []bool
2929
}
@@ -128,7 +128,7 @@ func (ms *ManagedState) getAccount(addr common.Address) *account {
128128
} else {
129129
// Always make sure the state account nonce isn't actually higher
130130
// than the tracked one.
131-
so := ms.StateDB.GetStateObject(addr)
131+
so := ms.StateDB.getStateObject(addr)
132132
if so != nil && uint64(len(account.nonces))+account.nstart < so.Nonce() {
133133
ms.accounts[addr] = newAccount(so)
134134
}
@@ -138,6 +138,6 @@ func (ms *ManagedState) getAccount(addr common.Address) *account {
138138
return ms.accounts[addr]
139139
}
140140

141-
func newAccount(so *StateObject) *account {
141+
func newAccount(so *stateObject) *account {
142142
return &account{so, so.Nonce(), nil}
143143
}

‎core/state/managed_state_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func create() (*ManagedState, *account) {
3030
statedb, _ := New(common.Hash{}, db)
3131
ms := ManageState(statedb)
3232
ms.StateDB.SetNonce(addr, 100)
33-
ms.accounts[addr] = newAccount(ms.StateDB.GetStateObject(addr))
33+
ms.accounts[addr] = newAccount(ms.StateDB.getStateObject(addr))
3434
return ms, ms.accounts[addr]
3535
}
3636

0 commit comments

Comments
 (0)