Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arijitAD committed Oct 27, 2021
1 parent cf0bc37 commit 4628194
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 29 deletions.
17 changes: 9 additions & 8 deletions lib/common/optional/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 3 additions & 10 deletions lib/runtime/storage/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package storage

import (
"encoding/binary"
"errors"
"sort"
"sync"

Expand Down Expand Up @@ -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
Expand Down
15 changes: 6 additions & 9 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4628194

Please sign in to comment.