aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryenlin.lai <yenlin.lai@cobinhood.com>2019-04-12 11:18:24 +0800
committerJhih-Ming Huang <jm.huang@cobinhood.com>2019-05-06 10:44:04 +0800
commit5b71bbd50a64d3b1576626d4cf0ca22569b57cf8 (patch)
tree50f3b391c6f5995225396a1a9379791c2b292a93
parentc044cbc979abae7563344d6b047a6b0c616d7b7f (diff)
downloaddexon-5b71bbd50a64d3b1576626d4cf0ca22569b57cf8.tar.gz
dexon-5b71bbd50a64d3b1576626d4cf0ca22569b57cf8.tar.zst
dexon-5b71bbd50a64d3b1576626d4cf0ca22569b57cf8.zip
sqlvm: common: refine StateDB/Storage
In the context of vm module, we usually access vm.StateDB interface object instead of state.StateDB directly. Also, state.StateDB is usually passed with pointer and used with pointer receiver. Implement the similar behavior on Storage object.
-rw-r--r--core/vm/sqlvm/common/context.go2
-rw-r--r--core/vm/sqlvm/common/storage.go62
-rw-r--r--core/vm/sqlvm/common/storage_rw.go8
-rw-r--r--core/vm/sqlvm/common/storage_rw_test.go1
-rw-r--r--core/vm/sqlvm/common/storage_test.go13
-rw-r--r--core/vm/sqlvm/runtime/instructions_test.go12
6 files changed, 44 insertions, 54 deletions
diff --git a/core/vm/sqlvm/common/context.go b/core/vm/sqlvm/common/context.go
index 56ad89ded..b6c34a9fc 100644
--- a/core/vm/sqlvm/common/context.go
+++ b/core/vm/sqlvm/common/context.go
@@ -11,7 +11,7 @@ type Option struct {
type Context struct {
vm.Context
- Storage Storage
+ Storage *Storage
Contract *vm.Contract
Opt Option
diff --git a/core/vm/sqlvm/common/storage.go b/core/vm/sqlvm/common/storage.go
index e977ee3ed..6ed7c2525 100644
--- a/core/vm/sqlvm/common/storage.go
+++ b/core/vm/sqlvm/common/storage.go
@@ -7,7 +7,7 @@ import (
"golang.org/x/crypto/sha3"
"github.com/dexon-foundation/dexon/common"
- "github.com/dexon-foundation/dexon/core/state"
+ "github.com/dexon-foundation/dexon/core/vm"
"github.com/dexon-foundation/dexon/core/vm/sqlvm/ast"
"github.com/dexon-foundation/dexon/core/vm/sqlvm/schema"
"github.com/dexon-foundation/dexon/crypto"
@@ -27,13 +27,13 @@ var (
// Storage holds SQLVM required data and method.
type Storage struct {
- state.StateDB
+ vm.StateDB
Schema schema.Schema
}
// NewStorage return Storage instance.
-func NewStorage(state *state.StateDB) Storage {
- s := Storage{*state, schema.Schema{}}
+func NewStorage(state vm.StateDB) *Storage {
+ s := &Storage{state, schema.Schema{}}
return s
}
@@ -62,7 +62,7 @@ func addressToHash(addr common.Address) common.Hash {
return common.BytesToHash(addr.Bytes())
}
-func (s Storage) hashPathKey(key [][]byte) (h common.Hash) {
+func (s *Storage) hashPathKey(key [][]byte) (h common.Hash) {
hw := sha3.NewLegacyKeccak256()
rlp.Encode(hw, key)
// length of common.Hash is 256bit,
@@ -72,7 +72,7 @@ func (s Storage) hashPathKey(key [][]byte) (h common.Hash) {
}
// GetRowPathHash return primary key hash which points to row data.
-func (s Storage) GetRowPathHash(tableName []byte, rowID uint64) common.Hash {
+func (s *Storage) GetRowPathHash(tableName []byte, rowID uint64) common.Hash {
// PathKey(["tables", "{table_name}", "primary", uint64({row_id})])
key := [][]byte{
pathCompTables,
@@ -85,7 +85,7 @@ func (s Storage) GetRowPathHash(tableName []byte, rowID uint64) common.Hash {
// GetIndexValuesPathHash return the hash address to IndexValues structure
// which contains all possible values.
-func (s Storage) GetIndexValuesPathHash(
+func (s *Storage) GetIndexValuesPathHash(
tableName, indexName []byte,
) common.Hash {
// PathKey(["tables", "{table_name}", "indices", "{index_name}"])
@@ -100,7 +100,7 @@ func (s Storage) GetIndexValuesPathHash(
// GetIndexEntryPathHash return the hash address to IndexEntry structure for a
// given value.
-func (s Storage) GetIndexEntryPathHash(
+func (s *Storage) GetIndexEntryPathHash(
tableName, indexName []byte,
values ...[]byte,
) common.Hash {
@@ -113,7 +113,7 @@ func (s Storage) GetIndexEntryPathHash(
// GetReverseIndexPathHash return the hash address to IndexRev structure for a
// row in a table.
-func (s Storage) GetReverseIndexPathHash(
+func (s *Storage) GetReverseIndexPathHash(
tableName []byte,
rowID uint64,
) common.Hash {
@@ -128,7 +128,7 @@ func (s Storage) GetReverseIndexPathHash(
}
// getSequencePathHash return the hash address of a sequence.
-func (s Storage) getSequencePathHash(
+func (s *Storage) getSequencePathHash(
tableName []byte, seqIdx uint8,
) common.Hash {
// PathKey(["tables", "{table_name}", "sequence", uint8(sequence_idx)])
@@ -141,13 +141,13 @@ func (s Storage) getSequencePathHash(
return s.hashPathKey(key)
}
-func (s Storage) getOwnerPathHash() common.Hash {
+func (s *Storage) getOwnerPathHash() common.Hash {
// PathKey(["owner"])
key := [][]byte{pathCompOwner}
return s.hashPathKey(key)
}
-func (s Storage) getTableWritersPathHash(tableName []byte) common.Hash {
+func (s *Storage) getTableWritersPathHash(tableName []byte) common.Hash {
// PathKey(["tables", "{table_name}", "writers"])
key := [][]byte{
pathCompTables,
@@ -157,7 +157,7 @@ func (s Storage) getTableWritersPathHash(tableName []byte) common.Hash {
return s.hashPathKey(key)
}
-func (s Storage) getTableWriterRevIdxPathHash(
+func (s *Storage) getTableWriterRevIdxPathHash(
tableName []byte,
account common.Address,
) common.Hash {
@@ -172,14 +172,14 @@ func (s Storage) getTableWriterRevIdxPathHash(
}
// ShiftHashUint64 shift hash in uint64.
-func (s Storage) ShiftHashUint64(hash common.Hash, shift uint64) common.Hash {
+func (s *Storage) ShiftHashUint64(hash common.Hash, shift uint64) common.Hash {
bigIntOffset := new(big.Int)
bigIntOffset.SetUint64(shift)
return s.ShiftHashBigInt(hash, bigIntOffset)
}
// ShiftHashBigInt shift hash in big.Int
-func (s Storage) ShiftHashBigInt(hash common.Hash, shift *big.Int) common.Hash {
+func (s *Storage) ShiftHashBigInt(hash common.Hash, shift *big.Int) common.Hash {
head := hash.Big()
head.Add(head, shift)
return common.BytesToHash(head.Bytes())
@@ -187,7 +187,7 @@ func (s Storage) ShiftHashBigInt(hash common.Hash, shift *big.Int) common.Hash {
// ShiftHashListEntry shift hash from the head of a list to the hash of
// idx-th entry.
-func (s Storage) ShiftHashListEntry(
+func (s *Storage) ShiftHashListEntry(
base common.Hash,
headerSize uint64,
entrySize uint64,
@@ -215,7 +215,7 @@ func getDByteSize(data common.Hash) uint64 {
}
// DecodeDByteBySlot given contract address and slot return the dynamic bytes data.
-func (s Storage) DecodeDByteBySlot(address common.Address, slot common.Hash) []byte {
+func (s *Storage) DecodeDByteBySlot(address common.Address, slot common.Hash) []byte {
data := s.GetState(address, slot)
length := getDByteSize(data)
if length < common.HashLength {
@@ -255,7 +255,7 @@ type IndexEntry struct {
}
// LoadIndexValues load IndexValues struct of a given index.
-func (s Storage) LoadIndexValues(
+func (s *Storage) LoadIndexValues(
contract common.Address,
tableName, indexName []byte,
onlyHeader bool,
@@ -277,7 +277,7 @@ func (s Storage) LoadIndexValues(
}
// LoadIndexEntry load IndexEntry struct of a given value key on an index.
-func (s Storage) LoadIndexEntry(
+func (s *Storage) LoadIndexEntry(
contract common.Address,
tableName, indexName []byte,
onlyHeader bool,
@@ -337,7 +337,7 @@ func (c *tableWriterRevIdx) Valid() bool {
return c.IndexToValuesOffset != 0
}
-func (s Storage) loadTableWriterRevIdx(
+func (s *Storage) loadTableWriterRevIdx(
contract common.Address,
path common.Hash,
) *tableWriterRevIdx {
@@ -347,7 +347,7 @@ func (s Storage) loadTableWriterRevIdx(
return ret
}
-func (s Storage) storeTableWriterRevIdx(
+func (s *Storage) storeTableWriterRevIdx(
contract common.Address,
path common.Hash,
rev *tableWriterRevIdx,
@@ -357,7 +357,7 @@ func (s Storage) storeTableWriterRevIdx(
s.SetState(contract, path, data)
}
-func (s Storage) loadTableWriters(
+func (s *Storage) loadTableWriters(
contract common.Address,
pathHash common.Hash,
onlyHeader bool,
@@ -375,7 +375,7 @@ func (s Storage) loadTableWriters(
return ret
}
-func (s Storage) storeTableWritersHeader(
+func (s *Storage) storeTableWritersHeader(
contract common.Address,
pathHash common.Hash,
w *tableWriters,
@@ -385,14 +385,14 @@ func (s Storage) storeTableWritersHeader(
s.SetState(contract, pathHash, header)
}
-func (s Storage) shiftTableWriterList(
+func (s *Storage) shiftTableWriterList(
base common.Hash,
idx uint64,
) common.Hash {
return s.ShiftHashListEntry(base, 1, 1, idx)
}
-func (s Storage) loadSingleTableWriter(
+func (s *Storage) loadSingleTableWriter(
contract common.Address,
writersPathHash common.Hash,
idx uint64,
@@ -402,7 +402,7 @@ func (s Storage) loadSingleTableWriter(
return hashToAddress(acc)
}
-func (s Storage) storeSingleTableWriter(
+func (s *Storage) storeSingleTableWriter(
contract common.Address,
writersPathHash common.Hash,
idx uint64,
@@ -413,7 +413,7 @@ func (s Storage) storeSingleTableWriter(
}
// IsTableWriter check if an account is writer to the table.
-func (s Storage) IsTableWriter(
+func (s *Storage) IsTableWriter(
contract common.Address,
tableName []byte,
account common.Address,
@@ -424,7 +424,7 @@ func (s Storage) IsTableWriter(
}
// LoadTableWriters load writers of a table.
-func (s Storage) LoadTableWriters(
+func (s *Storage) LoadTableWriters(
contract common.Address,
tableName []byte,
) (ret []common.Address) {
@@ -434,7 +434,7 @@ func (s Storage) LoadTableWriters(
}
// InsertTableWriter insert an account into writer list of the table.
-func (s Storage) InsertTableWriter(
+func (s *Storage) InsertTableWriter(
contract common.Address,
tableName []byte,
account common.Address,
@@ -457,7 +457,7 @@ func (s Storage) InsertTableWriter(
}
// DeleteTableWriter delete an account from writer list of the table.
-func (s Storage) DeleteTableWriter(
+func (s *Storage) DeleteTableWriter(
contract common.Address,
tableName []byte,
account common.Address,
@@ -487,7 +487,7 @@ func (s Storage) DeleteTableWriter(
}
// IncSequence increment value of sequence by inc and return the old value.
-func (s Storage) IncSequence(
+func (s *Storage) IncSequence(
contract common.Address,
tableName []byte,
seqIdx uint8,
diff --git a/core/vm/sqlvm/common/storage_rw.go b/core/vm/sqlvm/common/storage_rw.go
index 7df1d73bd..1ac16c746 100644
--- a/core/vm/sqlvm/common/storage_rw.go
+++ b/core/vm/sqlvm/common/storage_rw.go
@@ -10,7 +10,7 @@ import (
// Notice that we have cache in Reader, so it become invalid after any writes
// to Storage or StateDB.
type StorageReader struct {
- storage Storage
+ storage *Storage
contract common.Address
cursor common.Hash
buffer []byte
@@ -21,7 +21,7 @@ var _ io.Reader = (*StorageReader)(nil)
// NewStorageReader create a Reader on Storage.
func NewStorageReader(
- storage Storage,
+ storage *Storage,
contract common.Address,
startPos common.Hash,
) *StorageReader {
@@ -57,7 +57,7 @@ func (r *StorageReader) Read(p []byte) (n int, err error) {
// StorageWriter implements io.Writer on Storage.
type StorageWriter struct {
- storage Storage
+ storage *Storage
contract common.Address
cursor common.Hash
byteShift int // bytes already written in last slot. value: 0 ~ 31
@@ -68,7 +68,7 @@ var _ io.Writer = (*StorageWriter)(nil)
// NewStorageWriter create a Writer on Storage.
func NewStorageWriter(
- storage Storage,
+ storage *Storage,
contract common.Address,
startPos common.Hash,
) *StorageWriter {
diff --git a/core/vm/sqlvm/common/storage_rw_test.go b/core/vm/sqlvm/common/storage_rw_test.go
index 99b7a0ae7..74c53bc1b 100644
--- a/core/vm/sqlvm/common/storage_rw_test.go
+++ b/core/vm/sqlvm/common/storage_rw_test.go
@@ -34,7 +34,6 @@ func (s *StorageRWTestSuite) TestRW() {
s.Require().Equal(v, n)
cursor += v
}
- storage.Commit(false)
// Read and check.
reader := NewStorageReader(storage, contract, start)
diff --git a/core/vm/sqlvm/common/storage_test.go b/core/vm/sqlvm/common/storage_test.go
index 625d89158..aed07b4a8 100644
--- a/core/vm/sqlvm/common/storage_test.go
+++ b/core/vm/sqlvm/common/storage_test.go
@@ -37,7 +37,7 @@ func (s *StorageTestSuite) TestGetRowAddress() {
hw := sha3.NewLegacyKeccak256()
rlp.Encode(hw, key)
bytes := hw.Sum(nil)
- storage := Storage{}
+ storage := &Storage{}
result := storage.GetRowPathHash(table, id)
s.Require().Equal(bytes, result[:])
}
@@ -76,7 +76,7 @@ func (s *StorageTestSuite) TestDecodeDByte() {
result: []byte(""),
},
}
- SetDataToStateDB(head, storage, address, testcase)
+ SetDataToStorage(head, storage, address, testcase)
for i, t := range testcase {
slot := storage.ShiftHashUint64(head, uint64(i))
result := storage.DecodeDByteBySlot(address, slot)
@@ -84,7 +84,7 @@ func (s *StorageTestSuite) TestDecodeDByte() {
}
}
-func SetDataToStateDB(head common.Hash, storage Storage, addr common.Address,
+func SetDataToStorage(head common.Hash, storage *Storage, addr common.Address,
testcase []decodeTestCase) {
for i, t := range testcase {
slot := storage.ShiftHashUint64(head, uint64(i))
@@ -107,7 +107,6 @@ func SetDataToStateDB(head common.Hash, storage Storage, addr common.Address,
}
}
}
- storage.Commit(false)
}
func (s *StorageTestSuite) TestOwner() {
@@ -122,12 +121,10 @@ func (s *StorageTestSuite) TestOwner() {
storage.StoreOwner(contractA, ownerA)
storage.StoreOwner(contractB, ownerB)
- storage.Commit(false)
s.Require().Equal(ownerA, storage.LoadOwner(contractA))
s.Require().Equal(ownerB, storage.LoadOwner(contractB))
storage.StoreOwner(contractA, ownerB)
- storage.Commit(false)
s.Require().Equal(ownerB, storage.LoadOwner(contractA))
}
@@ -155,7 +152,6 @@ func (s *StorageTestSuite) TestTableWriter() {
storage.InsertTableWriter(contractA, table1, addrs[1])
storage.InsertTableWriter(contractA, table1, addrs[2])
storage.InsertTableWriter(contractB, table2, addrs[0])
- storage.Commit(false)
s.Require().Equal(addrs, storage.LoadTableWriters(contractA, table1))
s.Require().Len(storage.LoadTableWriters(contractA, table2), 0)
s.Require().Len(storage.LoadTableWriters(contractB, table1), 0)
@@ -166,14 +162,12 @@ func (s *StorageTestSuite) TestTableWriter() {
storage.InsertTableWriter(contractA, table1, addrs[0])
storage.InsertTableWriter(contractA, table1, addrs[1])
storage.InsertTableWriter(contractA, table1, addrs[2])
- storage.Commit(false)
s.Require().Equal(addrs, storage.LoadTableWriters(contractA, table1))
// Delete some writer.
storage.DeleteTableWriter(contractA, table1, addrs[0])
storage.DeleteTableWriter(contractA, table2, addrs[0])
storage.DeleteTableWriter(contractB, table2, addrs[0])
- storage.Commit(false)
s.Require().Equal([]common.Address{addrs[2], addrs[1]},
storage.LoadTableWriters(contractA, table1))
s.Require().Len(storage.LoadTableWriters(contractA, table2), 0)
@@ -182,7 +176,6 @@ func (s *StorageTestSuite) TestTableWriter() {
// Delete again.
storage.DeleteTableWriter(contractA, table1, addrs[2])
- storage.Commit(false)
s.Require().Equal([]common.Address{addrs[1]},
storage.LoadTableWriters(contractA, table1))
diff --git a/core/vm/sqlvm/runtime/instructions_test.go b/core/vm/sqlvm/runtime/instructions_test.go
index fe9f15994..7cf05c9f5 100644
--- a/core/vm/sqlvm/runtime/instructions_test.go
+++ b/core/vm/sqlvm/runtime/instructions_test.go
@@ -59,13 +59,12 @@ func createSchema(storage *common.Storage, raws []*raw) {
)
}
storage.Schema.SetupColumnOffset()
- storage.Commit(false)
}
// setSlotDataInStateDB store data in StateDB, and
// return corresponding slot hash and raw slice.
func setSlotDataInStateDB(head dexCommon.Hash, addr dexCommon.Address,
- storage common.Storage) ([]dexCommon.Hash, []*raw) {
+ storage *common.Storage) ([]dexCommon.Hash, []*raw) {
hash := dexCommon.Hash{}
var b []byte
@@ -176,7 +175,6 @@ func setSlotDataInStateDB(head dexCommon.Hash, addr dexCommon.Address,
ptr = crypto.Keccak256Hash(ptr.Bytes())
storage.SetState(addr, ptr, hash)
- storage.Commit(false)
return hData, raws
}
@@ -219,7 +217,7 @@ func (s *opLoadSuite) SetupTest() {
s.ctx.Contract = vm.NewContract(vm.AccountRef(s.address),
vm.AccountRef(s.address), new(big.Int), 0)
s.slotHash, s.raws = setSlotDataInStateDB(s.headHash, s.address, s.ctx.Storage)
- createSchema(&s.ctx.Storage, s.raws)
+ createSchema(s.ctx.Storage, s.raws)
s.setColData("Table_B", 654321)
}
@@ -285,10 +283,10 @@ func (s *opLoadSuite) getOKCaseFields(raws []*raw) []uint8 {
}
func (s *opLoadSuite) getDecodeTestCases(headHash dexCommon.Hash,
- address dexCommon.Address, storage common.Storage) []decodeTestCase {
+ address dexCommon.Address, storage *common.Storage) []decodeTestCase {
slotHash, raws := setSlotDataInStateDB(headHash, address, storage)
- createSchema(&storage, raws)
+ createSchema(storage, raws)
testCases := make([]decodeTestCase, len(raws))
for i := range testCases {
@@ -375,7 +373,7 @@ func newFieldsOperand(fields []uint8) *Operand {
return o
}
-func (s *opLoadSuite) newStorage() common.Storage {
+func (s *opLoadSuite) newStorage() *common.Storage {
db := ethdb.NewMemDatabase()
state, _ := state.New(dexCommon.Hash{}, state.NewDatabase(db))
storage := common.NewStorage(state)