Skip to content

Commit

Permalink
Merge branch 'development' into eclesio/err-msg-checkOrGetDescendantHash
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior authored Nov 29, 2022
2 parents 3e5ca1e + 19c9a7e commit 4211e61
Show file tree
Hide file tree
Showing 20 changed files with 173 additions and 111 deletions.
4 changes: 2 additions & 2 deletions dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ func createTestService(t *testing.T, genesisFilePath string,
cfgRuntime, err := wasmer.NewRuntimeFromGenesis(rtCfg)
require.NoError(t, err)

cfgRuntime.(*wasmer.Instance).GetContext().Storage.Set(aliceBalanceKey, encodedAccountInfo)
cfgRuntime.(*wasmer.Instance).GetContext().Storage.Put(aliceBalanceKey, encodedAccountInfo)
// this key is System.UpgradedToDualRefCount -> set to true since all accounts have been upgraded to v0.9 format
cfgRuntime.(*wasmer.Instance).GetContext().Storage.Set(common.UpgradedToDualRefKey, []byte{1})
cfgRuntime.(*wasmer.Instance).GetContext().Storage.Put(common.UpgradedToDualRefKey, []byte{1})

cfgBlockState.StoreRuntime(cfgBlockState.BestBlockHash(), cfgRuntime)

Expand Down
4 changes: 2 additions & 2 deletions dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
testRuntime, err := os.ReadFile(updateNodeRuntimeWasmPath)
require.NoError(t, err)

ts.Set(common.CodeKey, testRuntime)
ts.Put(common.CodeKey, testRuntime)
rtUpdateBhash := newBlockRTUpdate.Header.Hash()

// update runtime for new block
Expand Down Expand Up @@ -625,7 +625,7 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
ts, err = s.storageState.TrieState(nil)
require.NoError(t, err)

ts.Set(common.CodeKey, testRuntime)
ts.Put(common.CodeKey, testRuntime)
rtUpdateBhash := newBlock.Header.Hash()

// update runtime for new block
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func useInstanceFromRuntimeV0910(t *testing.T, rtStorage *storage.TrieState) (in
bytes, err := os.ReadFile(testRuntimeFilePath)
require.NoError(t, err)

rtStorage.Set(common.CodeKey, bytes)
rtStorage.Put(common.CodeKey, bytes)

cfg := wasmer.Config{
Role: 0,
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/modules/childstate_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ func setupChildStateStorage(t *testing.T) (*ChildStateModule, common.Hash) {
tr, err := st.Storage.TrieState(nil)
require.NoError(t, err)

tr.Set([]byte(":first_key"), []byte(":value1"))
tr.Set([]byte(":second_key"), []byte(":second_value"))
tr.Put([]byte(":first_key"), []byte(":value1"))
tr.Put([]byte(":second_key"), []byte(":second_value"))

childTr := trie.NewEmptyTrie()
childTr.Put([]byte(":child_first"), []byte(":child_first_value"))
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/modules/childstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func createTestTrieState(t *testing.T) (*trie.Trie, common.Hash) {
_, genesisTrie, _ := newTestGenesisWithTrieAndHeader(t)
tr := rtstorage.NewTrieState(&genesisTrie)

tr.Set([]byte(":first_key"), []byte(":value1"))
tr.Set([]byte(":second_key"), []byte(":second_value"))
tr.Put([]byte(":first_key"), []byte(":value1"))
tr.Put([]byte(":second_key"), []byte(":second_value"))

childTr := trie.NewEmptyTrie()
childTr.Put([]byte(":child_first"), []byte(":child_first_value"))
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/modules/state_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,8 @@ func setupStateModule(t *testing.T) (*StateModule, *common.Hash, *common.Hash) {
ts, err := chain.Storage.TrieState(nil)
require.NoError(t, err)

ts.Set([]byte(`:key2`), []byte(`value2`))
ts.Set([]byte(`:key1`), []byte(`value1`))
ts.Put([]byte(`:key2`), []byte(`value2`))
ts.Put([]byte(`:key1`), []byte(`value1`))
ts.SetChildStorage([]byte(`:child1`), []byte(`:key1`), []byte(`:childValue1`))

sr1, err := ts.Root()
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/system_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func setupSystemModule(t *testing.T) *SystemModule {

aliceAcctEncoded, err := scale.Marshal(aliceAcctInfo)
require.NoError(t, err)
ts.Set(aliceAcctStoKey, aliceAcctEncoded)
ts.Put(aliceAcctStoKey, aliceAcctEncoded)

err = chain.Storage.StoreTrie(ts, nil)
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions dot/state/storage_notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestStorageState_RegisterStorageObserver(t *testing.T) {
ss.RegisterStorageObserver(mockobs)
defer ss.UnregisterStorageObserver(mockobs)

ts.Set([]byte("mackcom"), []byte("wuz here"))
ts.Put([]byte("mackcom"), []byte("wuz here"))
err = ss.StoreTrie(ts, nil)
require.NoError(t, err)

Expand Down Expand Up @@ -81,7 +81,7 @@ func TestStorageState_RegisterStorageObserver_Multi(t *testing.T) {
key1 := []byte("key1")
value1 := []byte("value1")

ts.Set(key1, value1)
ts.Put(key1, value1)

err = ss.StoreTrie(ts, nil)
require.NoError(t, err)
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestStorageState_RegisterStorageObserver_Multi_Filter(t *testing.T) {
ss.RegisterStorageObserver(mockobs)
}

ts.Set(key1, value1)
ts.Put(key1, value1)
err = ss.StoreTrie(ts, nil)
require.NoError(t, err)

Expand Down
10 changes: 5 additions & 5 deletions dot/state/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestStorage_GetStorageByBlockHash(t *testing.T) {

key := []byte("testkey")
value := []byte("testvalue")
ts.Set(key, value)
ts.Put(key, value)

root, err := ts.Root()
require.NoError(t, err)
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestStorage_TrieState(t *testing.T) {
storage := newTestStorageState(t)
ts, err := storage.TrieState(&trie.EmptyHash)
require.NoError(t, err)
ts.Set([]byte("noot"), []byte("washere"))
ts.Put([]byte("noot"), []byte("washere"))

root, err := ts.Root()
require.NoError(t, err)
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestStorage_LoadFromDB(t *testing.T) {
}

for _, kv := range trieKV {
ts.Set(kv.key, kv.value)
ts.Put(kv.key, kv.value)
}

root, err := ts.Root()
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestStorage_StoreTrie_NotSyncing(t *testing.T) {

key := []byte("testkey")
value := []byte("testvalue")
ts.Set(key, value)
ts.Put(key, value)

err = storage.StoreTrie(ts, nil)
require.NoError(t, err)
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestGetStorageChildAndGetStorageFromChild(t *testing.T) {

testChildTrie.Put([]byte("keyInsidechild"), []byte("voila"))

err = genTrie.PutChild([]byte("keyToChild"), testChildTrie)
err = genTrie.SetChild([]byte("keyToChild"), testChildTrie)
require.NoError(t, err)

tries := newTriesEmpty()
Expand Down
2 changes: 1 addition & 1 deletion dot/state/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func generateBlockWithRandomTrie(t *testing.T, serv *Service,
rand := time.Now().UnixNano()
key := []byte("testKey" + fmt.Sprint(rand))
value := []byte("testValue" + fmt.Sprint(rand))
trieState.Set(key, value)
trieState.Put(key, value)

trieStateRoot, err := trieState.Root()
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Instance interface {

// Storage interface
type Storage interface {
Set(key []byte, value []byte)
Put(key []byte, value []byte)
Get(key []byte) []byte
Root() (common.Hash, error)
SetChild(keyToChild []byte, child *trie.Trie) error
Expand Down
6 changes: 3 additions & 3 deletions lib/runtime/storage/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (s *TrieState) RollbackStorageTransaction() {
s.oldTrie = nil
}

// Set sets a key-value pair in the trie
func (s *TrieState) Set(key, value []byte) {
// Put puts thread safely a value at the specified key in the trie.
func (s *TrieState) Put(key, value []byte) {
s.lock.Lock()
defer s.lock.Unlock()
s.t.Put(key, value)
Expand Down Expand Up @@ -142,7 +142,7 @@ func (s *TrieState) TrieEntries() map[string][]byte {
func (s *TrieState) SetChild(keyToChild []byte, child *trie.Trie) error {
s.lock.Lock()
defer s.lock.Unlock()
return s.t.PutChild(keyToChild, child)
return s.t.SetChild(keyToChild, child)
}

// SetChildStorage sets a key-value pair in a child trie
Expand Down
18 changes: 9 additions & 9 deletions lib/runtime/storage/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var testCases = []string{
func TestTrieState_SetGet(t *testing.T) {
testFunc := func(ts *TrieState) {
for _, tc := range testCases {
ts.Set([]byte(tc), []byte(tc))
ts.Put([]byte(tc), []byte(tc))
}

for _, tc := range testCases {
Expand All @@ -43,7 +43,7 @@ func TestTrieState_SetGet(t *testing.T) {
func TestTrieState_Delete(t *testing.T) {
testFunc := func(ts *TrieState) {
for _, tc := range testCases {
ts.Set([]byte(tc), []byte(tc))
ts.Put([]byte(tc), []byte(tc))
}

ts.Delete([]byte(testCases[0]))
Expand All @@ -58,7 +58,7 @@ func TestTrieState_Delete(t *testing.T) {
func TestTrieState_Root(t *testing.T) {
testFunc := func(ts *TrieState) {
for _, tc := range testCases {
ts.Set([]byte(tc), []byte(tc))
ts.Put([]byte(tc), []byte(tc))
}

expected := ts.MustRoot()
Expand All @@ -79,7 +79,7 @@ func TestTrieState_ClearPrefix(t *testing.T) {
}

for i, key := range keys {
ts.Set([]byte(key), []byte{byte(i)})
ts.Put([]byte(key), []byte{byte(i)})
}

ts.ClearPrefix([]byte("noo"))
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestTrieState_NextKey(t *testing.T) {
ts := &TrieState{t: trie.NewEmptyTrie()}

for _, tc := range testCases {
ts.Set([]byte(tc), []byte(tc))
ts.Put([]byte(tc), []byte(tc))
}

sort.Slice(testCases, func(i, j int) bool {
Expand All @@ -152,12 +152,12 @@ func TestTrieState_CommitStorageTransaction(t *testing.T) {
ts := &TrieState{t: trie.NewEmptyTrie()}

for _, tc := range testCases {
ts.Set([]byte(tc), []byte(tc))
ts.Put([]byte(tc), []byte(tc))
}

ts.BeginStorageTransaction()
testValue := []byte("noot")
ts.Set([]byte(testCases[0]), testValue)
ts.Put([]byte(testCases[0]), testValue)
ts.CommitStorageTransaction()

val := ts.Get([]byte(testCases[0]))
Expand All @@ -168,12 +168,12 @@ func TestTrieState_RollbackStorageTransaction(t *testing.T) {
ts := &TrieState{t: trie.NewEmptyTrie()}

for _, tc := range testCases {
ts.Set([]byte(tc), []byte(tc))
ts.Put([]byte(tc), []byte(tc))
}

ts.BeginStorageTransaction()
testValue := []byte("noot")
ts.Set([]byte(testCases[0]), testValue)
ts.Put([]byte(testCases[0]), testValue)
ts.RollbackStorageTransaction()

val := ts.Get([]byte(testCases[0]))
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/wasmer/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ func TestNodeRuntime_ValidateTransaction(t *testing.T) {
encBal, err := scale.Marshal(accInfo)
require.NoError(t, err)

rt.(*Instance).ctx.Storage.Set(aliceBalanceKey, encBal)
rt.(*Instance).ctx.Storage.Put(aliceBalanceKey, encBal)
// this key is System.UpgradedToDualRefCount -> set to true since all accounts have been upgraded to v0.9 format
rt.(*Instance).ctx.Storage.Set(common.UpgradedToDualRefKey, []byte{1})
rt.(*Instance).ctx.Storage.Put(common.UpgradedToDualRefKey, []byte{1})

genesisHeader := &types.Header{
Number: 0,
Expand Down
51 changes: 50 additions & 1 deletion lib/runtime/wasmer/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,55 @@ func toWasmMemoryOptionalUint32(context wasmer.InstanceContext, data *uint32) (
return toWasmMemory(context, enc)
}

func mustToWasmMemoryNil(context wasmer.InstanceContext) (
cPointerSize C.int64_t) {
allocator := context.Data().(*runtime.Context).Allocator
ptr, err := allocator.Allocate(0)
if err != nil {
// we allocate 0 byte, this should never fail
panic(err)
}
pointerSize := toPointerSize(ptr, 0)
return C.int64_t(pointerSize)
}

func toWasmMemoryOptionalNil(context wasmer.InstanceContext) (
cPointerSize C.int64_t, err error) {
pointerSize, err := toWasmMemoryOptional(context, nil)
if err != nil {
return 0, err
}

return C.int64_t(pointerSize), nil
}

func mustToWasmMemoryOptionalNil(context wasmer.InstanceContext) (
cPointerSize C.int64_t) {
cPointerSize, err := toWasmMemoryOptionalNil(context)
if err != nil {
panic(err)
}
return cPointerSize
}

func toWasmMemoryResultEmpty(context wasmer.InstanceContext) (
cPointerSize C.int64_t, err error) {
pointerSize, err := toWasmMemoryResult(context, nil)
if err != nil {
return 0, err
}
return C.int64_t(pointerSize), nil
}

func mustToWasmMemoryResultEmpty(context wasmer.InstanceContext) (
cPointerSize C.int64_t) {
cPointerSize, err := toWasmMemoryResultEmpty(context)
if err != nil {
panic(err)
}
return cPointerSize
}

// toKillStorageResultEnum encodes the `allRemoved` flag and
// the `numRemoved` uint32 to a byte slice and returns it.
// The format used is:
Expand Down Expand Up @@ -211,6 +260,6 @@ func storageAppend(storage runtime.Storage, key, valueToAppend []byte) error {
}

logger.Debugf("resulting value: 0x%x", value)
storage.Set(key, value)
storage.Put(key, value)
return nil
}
Loading

0 comments on commit 4211e61

Please sign in to comment.