aboutsummaryrefslogtreecommitdiffstats
path: root/state
diff options
context:
space:
mode:
Diffstat (limited to 'state')
-rw-r--r--state/dump.go8
-rw-r--r--state/main_test.go9
-rw-r--r--state/state_object.go12
-rw-r--r--state/state_test.go67
4 files changed, 64 insertions, 32 deletions
diff --git a/state/dump.go b/state/dump.go
index be3b362c4..186c6dd73 100644
--- a/state/dump.go
+++ b/state/dump.go
@@ -46,3 +46,11 @@ func (self *State) Dump() []byte {
return json
}
+
+// Debug stuff
+func (self *StateObject) CreateOutputForDiff() {
+ fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.Nonce)
+ self.EachStorage(func(addr string, value *ethutil.Value) {
+ fmt.Printf("%x %x\n", addr, value.Bytes())
+ })
+}
diff --git a/state/main_test.go b/state/main_test.go
new file mode 100644
index 000000000..f3d3f7e23
--- /dev/null
+++ b/state/main_test.go
@@ -0,0 +1,9 @@
+package state
+
+import (
+ "testing"
+
+ checker "gopkg.in/check.v1"
+)
+
+func Test(t *testing.T) { checker.TestingT(t) }
diff --git a/state/state_object.go b/state/state_object.go
index 5ce74c434..f02d1b5ab 100644
--- a/state/state_object.go
+++ b/state/state_object.go
@@ -148,9 +148,7 @@ func (self *StateObject) EachStorage(cb trie.EachCallback) {
func (self *StateObject) Sync() {
for key, value := range self.storage {
- if value.Len() == 0 { // value.BigInt().Cmp(ethutil.Big0) == 0 {
- //data := self.getStorage([]byte(key))
- //fmt.Printf("deleting %x %x 0x%x\n", self.Address(), []byte(key), data)
+ if value.Len() == 0 {
self.State.Trie.Delete(string(key))
continue
}
@@ -287,14 +285,6 @@ func (self *StateObject) Root() []byte {
return self.State.Trie.GetRoot()
}
-// Debug stuff
-func (self *StateObject) CreateOutputForDiff() {
- fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.Nonce)
- self.EachStorage(func(addr string, value *ethutil.Value) {
- fmt.Printf("%x %x\n", addr, value.Bytes())
- })
-}
-
//
// Encoding
//
diff --git a/state/state_test.go b/state/state_test.go
index 737815e90..825d21fcc 100644
--- a/state/state_test.go
+++ b/state/state_test.go
@@ -1,36 +1,61 @@
package state
import (
- "testing"
+ checker "gopkg.in/check.v1"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/trie"
)
-var ZeroHash256 = make([]byte, 32)
-
-func TestSnapshot(t *testing.T) {
- db, _ := ethdb.NewMemDatabase()
- ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
- ethutil.Config.Db = db
-
- state := New(trie.New(db, ""))
-
- stateObject := state.GetOrNewStateObject([]byte("aa"))
+type StateSuite struct {
+ state *State
+}
- stateObject.SetStorage(ethutil.Big("0"), ethutil.NewValue(42))
+var _ = checker.Suite(&StateSuite{})
- snapshot := state.Copy()
+// var ZeroHash256 = make([]byte, 32)
- stateObject = state.GetStateObject([]byte("aa"))
- stateObject.SetStorage(ethutil.Big("0"), ethutil.NewValue(43))
+func (s *StateSuite) TestDump(c *checker.C) {
+ key := []byte{0x01}
+ value := "foo"
+ node := []interface{}{key, value}
+ s.state.Trie.Put(node)
+ dump := s.state.Dump()
+ c.Assert(dump, checker.NotNil)
+}
- state.Set(snapshot)
+func (s *StateSuite) SetUpTest(c *checker.C) {
+ db, _ := ethdb.NewMemDatabase()
+ ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
+ ethutil.Config.Db = db
+ s.state = New(trie.New(db, ""))
+}
- stateObject = state.GetStateObject([]byte("aa"))
- res := stateObject.GetStorage(ethutil.Big("0"))
- if !res.Cmp(ethutil.NewValue(42)) {
- t.Error("Expected storage 0 to be 42", res)
- }
+func (s *StateSuite) TestSnapshot(c *checker.C) {
+ stateobjaddr := []byte("aa")
+ storageaddr := ethutil.Big("0")
+ data1 := ethutil.NewValue(42)
+ data2 := ethutil.NewValue(43)
+
+ // get state object
+ stateObject := s.state.GetOrNewStateObject(stateobjaddr)
+ // set inital state object value
+ stateObject.SetStorage(storageaddr, data1)
+ // get snapshot of current state
+ snapshot := s.state.Copy()
+
+ // get state object. is this strictly necessary?
+ stateObject = s.state.GetStateObject(stateobjaddr)
+ // set new state object value
+ stateObject.SetStorage(storageaddr, data2)
+ // restore snapshot
+ s.state.Set(snapshot)
+
+ // get state object
+ stateObject = s.state.GetStateObject(stateobjaddr)
+ // get state storage value
+ res := stateObject.GetStorage(storageaddr)
+
+ c.Assert(data1, checker.DeepEquals, res)
}