2014-10-31 13:43:14 +00:00
|
|
|
package state
|
2014-07-22 09:54:48 +00:00
|
|
|
|
|
|
|
import (
|
2014-11-11 11:52:43 +00:00
|
|
|
. "gopkg.in/check.v1"
|
2014-08-04 08:42:40 +00:00
|
|
|
"testing"
|
|
|
|
|
2014-10-23 13:01:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2014-10-31 17:40:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2014-07-22 09:54:48 +00:00
|
|
|
)
|
|
|
|
|
2014-11-11 11:52:43 +00:00
|
|
|
func Test(t *testing.T) { TestingT(t) }
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-11 11:52:43 +00:00
|
|
|
type StateSuite struct {
|
|
|
|
state *State
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Suite(&StateSuite{})
|
|
|
|
|
|
|
|
const expectedasbytes = "Expected % x Got % x"
|
|
|
|
|
|
|
|
// var ZeroHash256 = make([]byte, 32)
|
|
|
|
|
|
|
|
func (s *StateSuite) SetUpTest(c *C) {
|
2014-07-22 09:54:48 +00:00
|
|
|
db, _ := ethdb.NewMemDatabase()
|
|
|
|
ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
|
|
|
|
ethutil.Config.Db = db
|
2014-11-11 11:52:43 +00:00
|
|
|
s.state = New(trie.New(db, ""))
|
|
|
|
}
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-11 11:52:43 +00:00
|
|
|
func (s *StateSuite) TestSnapshot(c *C) {
|
2014-11-11 12:07:24 +00:00
|
|
|
stateobjaddr := []byte("aa")
|
|
|
|
storageaddr := ethutil.Big("0")
|
2014-11-11 11:52:43 +00:00
|
|
|
data1 := ethutil.NewValue(42)
|
|
|
|
data2 := ethutil.NewValue(43)
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-11 12:07:24 +00:00
|
|
|
// get state object
|
2014-11-11 11:52:43 +00:00
|
|
|
stateObject := s.state.GetOrNewStateObject(stateobjaddr)
|
2014-11-11 12:07:24 +00:00
|
|
|
// set inital state object value
|
2014-11-11 11:52:43 +00:00
|
|
|
stateObject.SetStorage(storageaddr, data1)
|
2014-11-11 12:07:24 +00:00
|
|
|
// get snapshot of current state
|
2014-11-11 11:52:43 +00:00
|
|
|
snapshot := s.state.Copy()
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-11 12:07:24 +00:00
|
|
|
// get state object. is this strictly necessary?
|
2014-11-11 11:52:43 +00:00
|
|
|
stateObject = s.state.GetStateObject(stateobjaddr)
|
2014-11-11 12:07:24 +00:00
|
|
|
// set new state object value
|
2014-11-11 11:52:43 +00:00
|
|
|
stateObject.SetStorage(storageaddr, data2)
|
2014-11-11 12:07:24 +00:00
|
|
|
// restore snapshot
|
2014-11-11 11:52:43 +00:00
|
|
|
s.state.Set(snapshot)
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-11 12:07:24 +00:00
|
|
|
// get state object
|
2014-11-11 11:52:43 +00:00
|
|
|
stateObject = s.state.GetStateObject(stateobjaddr)
|
2014-11-11 12:07:24 +00:00
|
|
|
// get state storage value
|
2014-11-11 11:52:43 +00:00
|
|
|
res := stateObject.GetStorage(storageaddr)
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-11-11 11:52:43 +00:00
|
|
|
c.Assert(data1, DeepEquals, res, Commentf(expectedasbytes, data1, res))
|
2014-07-22 09:54:48 +00:00
|
|
|
}
|