From 46281945f08385e43d9633ad5b6057a9ca2d2369 Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Wed, 27 Oct 2021 19:19:37 +0530 Subject: [PATCH] Address comments --- lib/common/optional/types.go | 17 +++++++++-------- lib/runtime/constants.go | 2 +- lib/runtime/interface.go | 2 +- lib/runtime/storage/trie.go | 13 +++---------- lib/runtime/wasmer/imports.go | 15 ++++++--------- 5 files changed, 20 insertions(+), 29 deletions(-) diff --git a/lib/common/optional/types.go b/lib/common/optional/types.go index 1b58fab701..9247453a99 100644 --- a/lib/common/optional/types.go +++ b/lib/common/optional/types.go @@ -25,7 +25,7 @@ import ( "math/big" "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/lib/scale" + "github.com/ChainSafe/gossamer/pkg/scale" ) const none = "None" @@ -134,7 +134,7 @@ func (x *Bytes) Encode() ([]byte, error) { return []byte{0}, nil } - value, err := scale.Encode(x.value) + value, err := scale.Marshal(x.value) if err != nil { return nil, err } @@ -153,15 +153,15 @@ func (x *Bytes) Decode(r io.Reader) (*Bytes, error) { return nil, ErrInvalidOptional } - x.exists = (exists != 0) + x.exists = exists != 0 if x.exists { - sd := scale.Decoder{Reader: r} - value, err := sd.DecodeByteArray() + sd := scale.NewDecoder(r) + err := sd.Decode(&x.value) + if err != nil { return nil, err } - x.value = value } return x, nil @@ -176,11 +176,12 @@ func (x *Bytes) DecodeBytes(data []byte) (*Bytes, error) { x.exists = data[0] != 0 if x.exists { - decData, err := scale.Decode(data[1:], []byte{}) + var decData []byte + err := scale.Unmarshal(data[1:], &decData) if err != nil { return nil, err } - x.value = decData.([]byte) + x.value = decData } else { x.value = nil } diff --git a/lib/runtime/constants.go b/lib/runtime/constants.go index 0dc5f8fa78..97da61485d 100644 --- a/lib/runtime/constants.go +++ b/lib/runtime/constants.go @@ -44,7 +44,7 @@ const ( POLKADOT_RUNTIME_FP = "polkadot_runtime.compact.wasm" POLKADOT_RUNTIME_URL = "https://github.com/noot/polkadot/blob/noot/v0.8.25/polkadot_runtime.wasm?raw=true" - // v0.8 test API wasm + // v0.9 test API wasm HOST_API_TEST_RUNTIME = "hostapi_runtime" HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm" HOST_API_TEST_RUNTIME_URL = "https://github.com/ChainSafe/polkadot-spec/blob/update-hostapi-wasm/test/runtimes/hostapi/hostapi_runtime.compact.wasm?raw=true" diff --git a/lib/runtime/interface.go b/lib/runtime/interface.go index 1a6df6951d..e540151f01 100644 --- a/lib/runtime/interface.go +++ b/lib/runtime/interface.go @@ -76,7 +76,7 @@ type Storage interface { GetChildNextKey(keyToChild, key []byte) ([]byte, error) GetChild(keyToChild []byte) (*trie.Trie, error) ClearPrefix(prefix []byte) error - ClearPrefixLimit(prefix []byte, limit *optional.Bytes) (uint32, bool, error) + ClearPrefixLimit(prefix []byte, limit uint32) (uint32, bool) BeginStorageTransaction() CommitStorageTransaction() RollbackStorageTransaction() diff --git a/lib/runtime/storage/trie.go b/lib/runtime/storage/trie.go index 8789fbdcda..101f65ae54 100644 --- a/lib/runtime/storage/trie.go +++ b/lib/runtime/storage/trie.go @@ -18,7 +18,6 @@ package storage import ( "encoding/binary" - "errors" "sort" "sync" @@ -140,18 +139,12 @@ func (s *TrieState) ClearPrefix(prefix []byte) error { } // ClearPrefixLimit deletes key-value pairs from the trie where the key starts with the given prefix till limit reached -func (s *TrieState) ClearPrefixLimit(prefix []byte, limit *optional.Bytes) (uint32, bool, error) { +func (s *TrieState) ClearPrefixLimit(prefix []byte, limit uint32) (uint32, bool) { s.lock.Lock() defer s.lock.Unlock() - if limit == nil || !limit.Exists() { - return 0, false, errors.New("limit not exists") - } - - limitUint := binary.LittleEndian.Uint32(limit.Value()) - - num, del := s.t.ClearPrefixLimit(prefix, limitUint) - return num, del, nil + num, del := s.t.ClearPrefixLimit(prefix, limit) + return num, del } // TrieEntries returns every key-value pair in the trie diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 93d59e19f7..48703b1353 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -1803,20 +1803,17 @@ func ext_storage_clear_prefix_version_2(context unsafe.Pointer, prefixSpan, lim logger.Debug("[ext_storage_clear_prefix_version_2]", "prefix", fmt.Sprintf("0x%x", prefix)) limitBytes := asMemorySlice(instanceContext, lim) - buf := &bytes.Buffer{} - buf.Write(limitBytes) - limit, err := optional.NewBytes(true, nil).Decode(buf) + var limit []byte + err := scale.Unmarshal(limitBytes, &limit) if err != nil { logger.Warn("[ext_storage_clear_prefix_version_2] cannot generate limit", "error", err) - return 0 - } - - numRemoved, all, err := storage.ClearPrefixLimit(prefix, limit) - if err != nil { - logger.Error("[ext_storage_clear_prefix_version_2]", "error", err) + ret, _ := toWasmMemory(instanceContext, nil) + return C.int64_t(ret) } + limitUint := binary.LittleEndian.Uint32(limit) + numRemoved, all := storage.ClearPrefixLimit(prefix, limitUint) encBytes, err := toKillStorageResultEnum(all, numRemoved) if err != nil {