2014-10-31 13:43:14 +00:00
|
|
|
package state
|
2014-07-22 09:54:48 +00:00
|
|
|
|
|
|
|
import (
|
2014-11-14 21:01:52 +00:00
|
|
|
checker "gopkg.in/check.v1"
|
2014-08-04 08:42:40 +00:00
|
|
|
|
2014-10-23 13:01:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2015-01-02 12:00:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ptrie"
|
2014-07-22 09:54:48 +00:00
|
|
|
)
|
|
|
|
|
2014-11-14 21:01:52 +00:00
|
|
|
type StateSuite struct {
|
2014-12-10 09:57:19 +00:00
|
|
|
state *StateDB
|
2014-11-14 21:01:52 +00:00
|
|
|
}
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-14 21:01:52 +00:00
|
|
|
var _ = checker.Suite(&StateSuite{})
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-14 21:01:52 +00:00
|
|
|
// var ZeroHash256 = make([]byte, 32)
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-14 21:01:52 +00:00
|
|
|
func (s *StateSuite) TestDump(c *checker.C) {
|
|
|
|
key := []byte{0x01}
|
2015-01-02 12:00:25 +00:00
|
|
|
value := []byte("foo")
|
|
|
|
s.state.trie.Update(key, value)
|
2014-11-14 21:01:52 +00:00
|
|
|
dump := s.state.Dump()
|
|
|
|
c.Assert(dump, checker.NotNil)
|
|
|
|
}
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-14 21:01:52 +00:00
|
|
|
func (s *StateSuite) SetUpTest(c *checker.C) {
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
|
|
|
ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
|
|
|
|
ethutil.Config.Db = db
|
2015-01-02 12:00:25 +00:00
|
|
|
s.state = New(ptrie.New(nil, db))
|
2014-11-14 21:01:52 +00:00
|
|
|
}
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-14 21:01:52 +00:00
|
|
|
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)
|
2014-07-22 09:54:48 +00:00
|
|
|
}
|