Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
core/state: fix staticcheck warnings (ethereum#20357)
Browse files Browse the repository at this point in the history
Also remove dependency on gopkg.in/check.v1 in tests.
  • Loading branch information
fjl authored and elizabethengelman committed Dec 30, 2019
1 parent 9fa5359 commit 4c2ce94
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 228 deletions.
47 changes: 25 additions & 22 deletions core/state/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,25 @@ type Dump struct {
}

// iterativeDump is a 'collector'-implementation which dump output line-by-line iteratively
type iterativeDump json.Encoder
type iterativeDump struct {
*json.Encoder
}

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

func (self *Dump) onRoot(root common.Hash) {
self.Root = fmt.Sprintf("%x", root)
func (d *Dump) onRoot(root common.Hash) {
d.Root = fmt.Sprintf("%x", root)
}

func (self *Dump) onAccount(addr common.Address, account DumpAccount) {
self.Accounts[addr] = account
func (d *Dump) onAccount(addr common.Address, account DumpAccount) {
d.Accounts[addr] = account
}

func (self iterativeDump) onAccount(addr common.Address, account DumpAccount) {
func (d iterativeDump) onAccount(addr common.Address, account DumpAccount) {
dumpAccount := &DumpAccount{
Balance: account.Balance,
Nonce: account.Nonce,
Expand All @@ -77,25 +79,26 @@ func (self iterativeDump) onAccount(addr common.Address, account DumpAccount) {
if addr != (common.Address{}) {
dumpAccount.Address = &addr
}
(*json.Encoder)(&self).Encode(dumpAccount)
d.Encode(dumpAccount)
}
func (self iterativeDump) onRoot(root common.Hash) {
(*json.Encoder)(&self).Encode(struct {

func (d iterativeDump) onRoot(root common.Hash) {
d.Encode(struct {
Root common.Hash `json:"root"`
}{root})
}

func (self *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissingPreimages bool) {
func (s *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissingPreimages bool) {
emptyAddress := (common.Address{})
missingPreimages := 0
c.onRoot(self.trie.Hash())
it := trie.NewIterator(self.trie.NodeIterator(nil))
c.onRoot(s.trie.Hash())
it := trie.NewIterator(s.trie.NodeIterator(nil))
for it.Next() {
var data Account
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
panic(err)
}
addr := common.BytesToAddress(self.trie.GetKey(it.Key))
addr := common.BytesToAddress(s.trie.GetKey(it.Key))
obj := newObject(nil, addr, data)
account := DumpAccount{
Balance: data.Balance.String(),
Expand All @@ -112,18 +115,18 @@ func (self *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissi
account.SecureKey = it.Key
}
if !excludeCode {
account.Code = common.Bytes2Hex(obj.Code(self.db))
account.Code = common.Bytes2Hex(obj.Code(s.db))
}
if !excludeStorage {
account.Storage = make(map[common.Hash]string)
storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil))
storageIt := trie.NewIterator(obj.getTrie(s.db).NodeIterator(nil))
for storageIt.Next() {
_, content, _, err := rlp.Split(storageIt.Value)
if err != nil {
log.Error("Failed to decode the value returned by iterator", "error", err)
continue
}
account.Storage[common.BytesToHash(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
account.Storage[common.BytesToHash(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
}
}
c.onAccount(addr, account)
Expand All @@ -134,17 +137,17 @@ func (self *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissi
}

// RawDump returns the entire state an a single large object
func (self *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump {
func (s *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump {
dump := &Dump{
Accounts: make(map[common.Address]DumpAccount),
}
self.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages)
s.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages)
return *dump
}

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

// IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
func (self *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) {
self.dump(iterativeDump(*output), excludeCode, excludeStorage, excludeMissingPreimages)
func (s *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) {
s.dump(iterativeDump{output}, excludeCode, excludeStorage, excludeMissingPreimages)
}
4 changes: 1 addition & 3 deletions core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ type (
hash common.Hash
}
touchChange struct {
account *common.Address
prev bool
prevDirty bool
account *common.Address
}
)

Expand Down
25 changes: 0 additions & 25 deletions core/state/main_test.go

This file was deleted.

53 changes: 31 additions & 22 deletions core/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@ import (
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
checker "gopkg.in/check.v1"
)

type StateSuite struct {
var toAddr = common.BytesToAddress

type stateTest struct {
db ethdb.Database
state *StateDB
}

var _ = checker.Suite(&StateSuite{})
func newStateTest() *stateTest {
db := rawdb.NewMemoryDatabase()
sdb, _ := New(common.Hash{}, NewDatabase(db))
return &stateTest{db: db, state: sdb}
}

var toAddr = common.BytesToAddress
func TestDump(t *testing.T) {
s := newStateTest()

func (s *StateSuite) TestDump(c *checker.C) {
// generate a few entries
obj1 := s.state.GetOrNewStateObject(toAddr([]byte{0x01}))
obj1.AddBalance(big.NewInt(22))
Expand Down Expand Up @@ -78,16 +83,12 @@ func (s *StateSuite) TestDump(c *checker.C) {
}
}`
if got != want {
c.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want)
t.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want)
}
}

func (s *StateSuite) SetUpTest(c *checker.C) {
s.db = rawdb.NewMemoryDatabase()
s.state, _ = New(common.Hash{}, NewDatabase(s.db))
}

func (s *StateSuite) TestNull(c *checker.C) {
func TestNull(t *testing.T) {
s := newStateTest()
address := common.HexToAddress("0x823140710bf13990e4500136726d8b55")
s.state.CreateAccount(address)
//value := common.FromHex("0x823140710bf13990e4500136726d8b55")
Expand All @@ -97,18 +98,19 @@ func (s *StateSuite) TestNull(c *checker.C) {
s.state.Commit(false)

if value := s.state.GetState(address, common.Hash{}); value != (common.Hash{}) {
c.Errorf("expected empty current value, got %x", value)
t.Errorf("expected empty current value, got %x", value)
}
if value := s.state.GetCommittedState(address, common.Hash{}); value != (common.Hash{}) {
c.Errorf("expected empty committed value, got %x", value)
t.Errorf("expected empty committed value, got %x", value)
}
}

func (s *StateSuite) TestSnapshot(c *checker.C) {
func TestSnapshot(t *testing.T) {
stateobjaddr := toAddr([]byte("aa"))
var storageaddr common.Hash
data1 := common.BytesToHash([]byte{42})
data2 := common.BytesToHash([]byte{43})
s := newStateTest()

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

c.Assert(s.state.GetState(stateobjaddr, storageaddr), checker.DeepEquals, data1)
c.Assert(s.state.GetCommittedState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{})
if v := s.state.GetState(stateobjaddr, storageaddr); v != data1 {
t.Errorf("wrong storage value %v, want %v", v, data1)
}
if v := s.state.GetCommittedState(stateobjaddr, storageaddr); v != (common.Hash{}) {
t.Errorf("wrong committed storage value %v, want %v", v, common.Hash{})
}

// revert up to the genesis state and ensure correct content
s.state.RevertToSnapshot(genesis)
c.Assert(s.state.GetState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{})
c.Assert(s.state.GetCommittedState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{})
if v := s.state.GetState(stateobjaddr, storageaddr); v != (common.Hash{}) {
t.Errorf("wrong storage value %v, want %v", v, common.Hash{})
}
if v := s.state.GetCommittedState(stateobjaddr, storageaddr); v != (common.Hash{}) {
t.Errorf("wrong committed storage value %v, want %v", v, common.Hash{})
}
}

func (s *StateSuite) TestSnapshotEmpty(c *checker.C) {
func TestSnapshotEmpty(t *testing.T) {
s := newStateTest()
s.state.RevertToSnapshot(s.state.Snapshot())
}

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

Expand Down
Loading

0 comments on commit 4c2ce94

Please sign in to comment.