Skip to content

Commit dd21f07

Browse files
authored
core/state: fix staticcheck warnings (ethereum#20357)
Also remove dependency on gopkg.in/check.v1 in tests.
1 parent 36a684c commit dd21f07

File tree

7 files changed

+211
-228
lines changed

7 files changed

+211
-228
lines changed

core/state/dump.go

+25-22
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,25 @@ type Dump struct {
4747
}
4848

4949
// iterativeDump is a 'collector'-implementation which dump output line-by-line iteratively
50-
type iterativeDump json.Encoder
50+
type iterativeDump struct {
51+
*json.Encoder
52+
}
5153

5254
// Collector interface which the state trie calls during iteration
5355
type collector interface {
5456
onRoot(common.Hash)
5557
onAccount(common.Address, DumpAccount)
5658
}
5759

58-
func (self *Dump) onRoot(root common.Hash) {
59-
self.Root = fmt.Sprintf("%x", root)
60+
func (d *Dump) onRoot(root common.Hash) {
61+
d.Root = fmt.Sprintf("%x", root)
6062
}
6163

62-
func (self *Dump) onAccount(addr common.Address, account DumpAccount) {
63-
self.Accounts[addr] = account
64+
func (d *Dump) onAccount(addr common.Address, account DumpAccount) {
65+
d.Accounts[addr] = account
6466
}
6567

66-
func (self iterativeDump) onAccount(addr common.Address, account DumpAccount) {
68+
func (d iterativeDump) onAccount(addr common.Address, account DumpAccount) {
6769
dumpAccount := &DumpAccount{
6870
Balance: account.Balance,
6971
Nonce: account.Nonce,
@@ -77,25 +79,26 @@ func (self iterativeDump) onAccount(addr common.Address, account DumpAccount) {
7779
if addr != (common.Address{}) {
7880
dumpAccount.Address = &addr
7981
}
80-
(*json.Encoder)(&self).Encode(dumpAccount)
82+
d.Encode(dumpAccount)
8183
}
82-
func (self iterativeDump) onRoot(root common.Hash) {
83-
(*json.Encoder)(&self).Encode(struct {
84+
85+
func (d iterativeDump) onRoot(root common.Hash) {
86+
d.Encode(struct {
8487
Root common.Hash `json:"root"`
8588
}{root})
8689
}
8790

88-
func (self *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissingPreimages bool) {
91+
func (s *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissingPreimages bool) {
8992
emptyAddress := (common.Address{})
9093
missingPreimages := 0
91-
c.onRoot(self.trie.Hash())
92-
it := trie.NewIterator(self.trie.NodeIterator(nil))
94+
c.onRoot(s.trie.Hash())
95+
it := trie.NewIterator(s.trie.NodeIterator(nil))
9396
for it.Next() {
9497
var data Account
9598
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
9699
panic(err)
97100
}
98-
addr := common.BytesToAddress(self.trie.GetKey(it.Key))
101+
addr := common.BytesToAddress(s.trie.GetKey(it.Key))
99102
obj := newObject(nil, addr, data)
100103
account := DumpAccount{
101104
Balance: data.Balance.String(),
@@ -112,18 +115,18 @@ func (self *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissi
112115
account.SecureKey = it.Key
113116
}
114117
if !excludeCode {
115-
account.Code = common.Bytes2Hex(obj.Code(self.db))
118+
account.Code = common.Bytes2Hex(obj.Code(s.db))
116119
}
117120
if !excludeStorage {
118121
account.Storage = make(map[common.Hash]string)
119-
storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil))
122+
storageIt := trie.NewIterator(obj.getTrie(s.db).NodeIterator(nil))
120123
for storageIt.Next() {
121124
_, content, _, err := rlp.Split(storageIt.Value)
122125
if err != nil {
123126
log.Error("Failed to decode the value returned by iterator", "error", err)
124127
continue
125128
}
126-
account.Storage[common.BytesToHash(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
129+
account.Storage[common.BytesToHash(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
127130
}
128131
}
129132
c.onAccount(addr, account)
@@ -134,17 +137,17 @@ func (self *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissi
134137
}
135138

136139
// RawDump returns the entire state an a single large object
137-
func (self *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump {
140+
func (s *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump {
138141
dump := &Dump{
139142
Accounts: make(map[common.Address]DumpAccount),
140143
}
141-
self.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages)
144+
s.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages)
142145
return *dump
143146
}
144147

145148
// Dump returns a JSON string representing the entire state as a single json-object
146-
func (self *StateDB) Dump(excludeCode, excludeStorage, excludeMissingPreimages bool) []byte {
147-
dump := self.RawDump(excludeCode, excludeStorage, excludeMissingPreimages)
149+
func (s *StateDB) Dump(excludeCode, excludeStorage, excludeMissingPreimages bool) []byte {
150+
dump := s.RawDump(excludeCode, excludeStorage, excludeMissingPreimages)
148151
json, err := json.MarshalIndent(dump, "", " ")
149152
if err != nil {
150153
fmt.Println("dump err", err)
@@ -153,6 +156,6 @@ func (self *StateDB) Dump(excludeCode, excludeStorage, excludeMissingPreimages b
153156
}
154157

155158
// IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
156-
func (self *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) {
157-
self.dump(iterativeDump(*output), excludeCode, excludeStorage, excludeMissingPreimages)
159+
func (s *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) {
160+
s.dump(iterativeDump{output}, excludeCode, excludeStorage, excludeMissingPreimages)
158161
}

core/state/journal.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ type (
127127
hash common.Hash
128128
}
129129
touchChange struct {
130-
account *common.Address
131-
prev bool
132-
prevDirty bool
130+
account *common.Address
133131
}
134132
)
135133

core/state/main_test.go

-25
This file was deleted.

core/state/state_test.go

+31-22
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@ import (
2525
"github.com/ethereum/go-ethereum/core/rawdb"
2626
"github.com/ethereum/go-ethereum/crypto"
2727
"github.com/ethereum/go-ethereum/ethdb"
28-
checker "gopkg.in/check.v1"
2928
)
3029

31-
type StateSuite struct {
30+
var toAddr = common.BytesToAddress
31+
32+
type stateTest struct {
3233
db ethdb.Database
3334
state *StateDB
3435
}
3536

36-
var _ = checker.Suite(&StateSuite{})
37+
func newStateTest() *stateTest {
38+
db := rawdb.NewMemoryDatabase()
39+
sdb, _ := New(common.Hash{}, NewDatabase(db))
40+
return &stateTest{db: db, state: sdb}
41+
}
3742

38-
var toAddr = common.BytesToAddress
43+
func TestDump(t *testing.T) {
44+
s := newStateTest()
3945

40-
func (s *StateSuite) TestDump(c *checker.C) {
4146
// generate a few entries
4247
obj1 := s.state.GetOrNewStateObject(toAddr([]byte{0x01}))
4348
obj1.AddBalance(big.NewInt(22))
@@ -78,16 +83,12 @@ func (s *StateSuite) TestDump(c *checker.C) {
7883
}
7984
}`
8085
if got != want {
81-
c.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want)
86+
t.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want)
8287
}
8388
}
8489

85-
func (s *StateSuite) SetUpTest(c *checker.C) {
86-
s.db = rawdb.NewMemoryDatabase()
87-
s.state, _ = New(common.Hash{}, NewDatabase(s.db))
88-
}
89-
90-
func (s *StateSuite) TestNull(c *checker.C) {
90+
func TestNull(t *testing.T) {
91+
s := newStateTest()
9192
address := common.HexToAddress("0x823140710bf13990e4500136726d8b55")
9293
s.state.CreateAccount(address)
9394
//value := common.FromHex("0x823140710bf13990e4500136726d8b55")
@@ -97,18 +98,19 @@ func (s *StateSuite) TestNull(c *checker.C) {
9798
s.state.Commit(false)
9899

99100
if value := s.state.GetState(address, common.Hash{}); value != (common.Hash{}) {
100-
c.Errorf("expected empty current value, got %x", value)
101+
t.Errorf("expected empty current value, got %x", value)
101102
}
102103
if value := s.state.GetCommittedState(address, common.Hash{}); value != (common.Hash{}) {
103-
c.Errorf("expected empty committed value, got %x", value)
104+
t.Errorf("expected empty committed value, got %x", value)
104105
}
105106
}
106107

107-
func (s *StateSuite) TestSnapshot(c *checker.C) {
108+
func TestSnapshot(t *testing.T) {
108109
stateobjaddr := toAddr([]byte("aa"))
109110
var storageaddr common.Hash
110111
data1 := common.BytesToHash([]byte{42})
111112
data2 := common.BytesToHash([]byte{43})
113+
s := newStateTest()
112114

113115
// snapshot the genesis state
114116
genesis := s.state.Snapshot()
@@ -121,21 +123,28 @@ func (s *StateSuite) TestSnapshot(c *checker.C) {
121123
s.state.SetState(stateobjaddr, storageaddr, data2)
122124
s.state.RevertToSnapshot(snapshot)
123125

124-
c.Assert(s.state.GetState(stateobjaddr, storageaddr), checker.DeepEquals, data1)
125-
c.Assert(s.state.GetCommittedState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{})
126+
if v := s.state.GetState(stateobjaddr, storageaddr); v != data1 {
127+
t.Errorf("wrong storage value %v, want %v", v, data1)
128+
}
129+
if v := s.state.GetCommittedState(stateobjaddr, storageaddr); v != (common.Hash{}) {
130+
t.Errorf("wrong committed storage value %v, want %v", v, common.Hash{})
131+
}
126132

127133
// revert up to the genesis state and ensure correct content
128134
s.state.RevertToSnapshot(genesis)
129-
c.Assert(s.state.GetState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{})
130-
c.Assert(s.state.GetCommittedState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{})
135+
if v := s.state.GetState(stateobjaddr, storageaddr); v != (common.Hash{}) {
136+
t.Errorf("wrong storage value %v, want %v", v, common.Hash{})
137+
}
138+
if v := s.state.GetCommittedState(stateobjaddr, storageaddr); v != (common.Hash{}) {
139+
t.Errorf("wrong committed storage value %v, want %v", v, common.Hash{})
140+
}
131141
}
132142

133-
func (s *StateSuite) TestSnapshotEmpty(c *checker.C) {
143+
func TestSnapshotEmpty(t *testing.T) {
144+
s := newStateTest()
134145
s.state.RevertToSnapshot(s.state.Snapshot())
135146
}
136147

137-
// use testing instead of checker because checker does not support
138-
// printing/logging in tests (-check.vv does not work)
139148
func TestSnapshot2(t *testing.T) {
140149
state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()))
141150

0 commit comments

Comments
 (0)