Skip to content

Commit

Permalink
chore(lib/runtime): fix deepsource issues in wazero implementation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 authored Aug 2, 2023
1 parent dfaa31e commit 2eb6313
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
27 changes: 14 additions & 13 deletions lib/runtime/wazero/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ func ext_hashing_twox_128_version_1(ctx context.Context, m api.Module, dataSpan
"data 0x%x hash hash 0x%x",
data, hash)

out, err := write(m, rtCtx.Allocator, hash[:])
out, err := write(m, rtCtx.Allocator, hash)
if err != nil {
logger.Errorf("failed to allocate: %s", err)
return 0
Expand All @@ -1567,7 +1567,7 @@ func ext_hashing_twox_64_version_1(ctx context.Context, m api.Module, dataSpan u
"data 0x%x has hash 0x%x",
data, hash)

out, err := write(m, rtCtx.Allocator, hash[:])
out, err := write(m, rtCtx.Allocator, hash)
if err != nil {
logger.Errorf("failed to allocate: %s", err)
return 0
Expand Down Expand Up @@ -1638,7 +1638,7 @@ func ext_offchain_local_storage_clear_version_1(ctx context.Context, m api.Modul
}
}

func ext_offchain_is_validator_version_1(ctx context.Context, m api.Module) uint32 {
func ext_offchain_is_validator_version_1(ctx context.Context, _ api.Module) uint32 {
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
Expand Down Expand Up @@ -1821,20 +1821,20 @@ func ext_offchain_submit_transaction_version_1(ctx context.Context, m api.Module
return ret
}

func ext_offchain_timestamp_version_1(ctx context.Context, m api.Module) uint64 {
func ext_offchain_timestamp_version_1(_ context.Context, _ api.Module) uint64 {
now := time.Now().Unix()
return uint64(now)
}

func ext_offchain_sleep_until_version_1(ctx context.Context, m api.Module, deadline uint64) {
func ext_offchain_sleep_until_version_1(_ context.Context, _ api.Module, deadline uint64) {
dur := time.Until(time.UnixMilli(int64(deadline)))
if dur > 0 {
time.Sleep(dur)
}
}

func ext_offchain_http_request_start_version_1(
ctx context.Context, m api.Module, methodSpan, uriSpan, metaSpan uint64) (pointerSize uint64) {
ctx context.Context, m api.Module, methodSpan, uriSpan, metaSpan uint64) (pointerSize uint64) { //skipcq: RVV-B0012
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
Expand Down Expand Up @@ -1999,7 +1999,8 @@ func ext_storage_append_version_1(ctx context.Context, m api.Module, keySpan, va
}

// Always returns `None`. This function exists for compatibility reasons.
func ext_storage_changes_root_version_1(ctx context.Context, m api.Module, parentHashSpan uint64) uint64 {
func ext_storage_changes_root_version_1(
ctx context.Context, m api.Module, parentHashSpan uint64) uint64 { //skipcq: RVV-B0012
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
Expand Down Expand Up @@ -2265,7 +2266,7 @@ func ext_storage_root_version_1(ctx context.Context, m api.Module) uint64 {
return rootSpan
}

func ext_storage_root_version_2(ctx context.Context, m api.Module, version uint32) uint64 {
func ext_storage_root_version_2(ctx context.Context, m api.Module, version uint32) uint64 { //skipcq: RVV-B0012
// TODO: update to use state trie version 1 (#2418)
return ext_storage_root_version_1(ctx, m)
}
Expand All @@ -2292,31 +2293,31 @@ func ext_storage_set_version_1(ctx context.Context, m api.Module, keySpan, value
}
}

func ext_storage_start_transaction_version_1(ctx context.Context, m api.Module) {
func ext_storage_start_transaction_version_1(ctx context.Context, _ api.Module) {
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
}
rtCtx.Storage.BeginStorageTransaction()
}

func ext_storage_rollback_transaction_version_1(ctx context.Context, m api.Module) {
func ext_storage_rollback_transaction_version_1(ctx context.Context, _ api.Module) {
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
}
rtCtx.Storage.RollbackStorageTransaction()
}

func ext_storage_commit_transaction_version_1(ctx context.Context, m api.Module) {
func ext_storage_commit_transaction_version_1(ctx context.Context, _ api.Module) {
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
}
rtCtx.Storage.CommitStorageTransaction()
}

func ext_allocator_free_version_1(ctx context.Context, m api.Module, addr uint32) {
func ext_allocator_free_version_1(ctx context.Context, _ api.Module, addr uint32) {
allocator := ctx.Value(runtimeContextKey).(*runtime.Context).Allocator

// Deallocate memory
Expand All @@ -2326,7 +2327,7 @@ func ext_allocator_free_version_1(ctx context.Context, m api.Module, addr uint32
}
}

func ext_allocator_malloc_version_1(ctx context.Context, m api.Module, size uint32) uint32 {
func ext_allocator_malloc_version_1(ctx context.Context, _ api.Module, size uint32) uint32 {
allocator := ctx.Value(runtimeContextKey).(*runtime.Context).Allocator

// Allocate memory
Expand Down
16 changes: 8 additions & 8 deletions lib/runtime/wazero/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Test_ext_crypto_ed25519_generate_version_1(t *testing.T) {
seedData, err := scale.Marshal(data)
require.NoError(t, err)

params := append(idData, seedData...)
params := append(idData, seedData...) //skipcq: CRT-D0001

pubKeyBytes, err := inst.Exec("rtm_ext_crypto_ed25519_generate_version_1", params)
require.NoError(t, err)
Expand Down Expand Up @@ -154,7 +154,7 @@ func Test_ext_crypto_ed25519_sign_version_1(t *testing.T) {
require.NotNil(t, val)

value := make([]byte, 64)
copy(value[:], val[:])
copy(value, val[:])

ok, err := kp.Public().Verify(msgData, value)
require.NoError(t, err)
Expand Down Expand Up @@ -384,7 +384,7 @@ func Test_ext_crypto_sr25519_generate_version_1(t *testing.T) {
seedData, err := scale.Marshal(data)
require.NoError(t, err)

params := append(idData, seedData...)
params := append(idData, seedData...) //skipcq: CRT-D0001

ret, err := inst.Exec("rtm_ext_crypto_sr25519_generate_version_1", params)
require.NoError(t, err)
Expand Down Expand Up @@ -473,7 +473,7 @@ func Test_ext_crypto_sr25519_sign_version_1(t *testing.T) {
require.NotNil(t, val)

value := make([]byte, 64)
copy(value[:], val[:])
copy(value, val[:])

ok, err := kp.Public().Verify(msgData, value)
require.NoError(t, err)
Expand Down Expand Up @@ -1184,7 +1184,7 @@ func Test_ext_hashing_blake2_128_version_1(t *testing.T) {

expected, err := common.Blake2b128(data)
require.NoError(t, err)
require.Equal(t, expected[:], hash)
require.Equal(t, expected, hash)
}

func Test_ext_hashing_blake2_256_version_1(t *testing.T) {
Expand Down Expand Up @@ -1241,7 +1241,7 @@ func Test_ext_hashing_twox_128_version_1(t *testing.T) {

expected, err := common.Twox128Hash(data)
require.NoError(t, err)
require.Equal(t, expected[:], hash)
require.Equal(t, expected, hash)
}

func Test_ext_hashing_twox_64_version_1(t *testing.T) {
Expand All @@ -1260,7 +1260,7 @@ func Test_ext_hashing_twox_64_version_1(t *testing.T) {

expected, err := common.Twox64(data)
require.NoError(t, err)
require.Equal(t, expected[:], hash)
require.Equal(t, expected, hash)
}

func Test_ext_hashing_sha2_256_version_1(t *testing.T) {
Expand Down Expand Up @@ -1302,7 +1302,7 @@ func Test_ext_offchain_sleep_until_version_1(t *testing.T) {
enc, err := scale.Marshal(input)
require.NoError(t, err)

_, err = inst.Exec("rtm_ext_offchain_sleep_until_version_1", enc) //auto conversion to i64
_, err = inst.Exec("rtm_ext_offchain_sleep_until_version_1", enc) // auto conversion to i64
require.NoError(t, err)
}

Expand Down
10 changes: 5 additions & 5 deletions lib/runtime/wazero/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func (in *Instance) Version() (runtime.Version, error) {

// version calls runtime function Core_Version and returns the
// decoded version structure.
func (in *Instance) version() error {
func (in *Instance) version() error { //skipcq: RVV-B0001
res, err := in.Exec(runtime.CoreVersion, []byte{})
if err != nil {
return err
Expand Down Expand Up @@ -753,7 +753,7 @@ func (in *Instance) QueryCallFeeDetails(ext []byte) (*types.FeeDetails, error) {

// CheckInherents checks inherents in the block verification process.
// TODO: use this in block verification process (#1873)
func (in *Instance) CheckInherents() {}
func (*Instance) CheckInherents() {}

// GrandpaGenerateKeyOwnershipProof returns grandpa key ownership proof from the runtime.
func (in *Instance) GrandpaGenerateKeyOwnershipProof(authSetID uint64, authorityID ed25519.PublicKeyBytes) (
Expand Down Expand Up @@ -808,13 +808,13 @@ func (in *Instance) GrandpaSubmitReportEquivocationUnsignedExtrinsic(
return nil
}

func (in *Instance) RandomSeed() {
func (*Instance) RandomSeed() {
panic("unimplemented")
}
func (in *Instance) OffchainWorker() {
func (*Instance) OffchainWorker() {
panic("unimplemented")
}
func (in *Instance) GenerateSessionKeys() {
func (*Instance) GenerateSessionKeys() {
panic("unimplemented")
}

Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/wazero/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ func TestInstance_TransactionPaymentCallApi_QueryCallInfo(t *testing.T) {
// call removing encoding (first byte), polkadot.js/api v9.5.1: api.tx.system.remark("Ed")
// polkadot.js/api returns error: RPC-CORE: call(method: Text, data: Bytes, at?: BlockHash):
// Bytes:: -32000: Client error: Execution failed: Execution aborted due to trap: wasm trap: wasm
//`unreachable` instruction executed
// `unreachable` instruction executed
callHex: "0x040001084564",
errMessage: "running runtime function: wasm error",
},
Expand Down Expand Up @@ -1061,7 +1061,7 @@ func TestInstance_TransactionPaymentCallApi_QueryCallFeeDetails(t *testing.T) {
{
// call without removing any bytes, polkadot.js/api v9.5.1: api.tx.system.remark("Ed test")
// when calling polkadot (v0.9.29) with polkadot.js/api the node returns error: Error: createType(
//Call):: findMetaCall: Unable to find Call with index [44, 4]/[44,4]
// Call):: findMetaCall: Unable to find Call with index [44, 4]/[44,4]
callHex: "0x18040001084564",
errMessage: "running runtime function: wasm error",
},
Expand Down
2 changes: 1 addition & 1 deletion lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (t *Trie) Hash() (rootHash common.Hash, err error) {
return rootHash, nil
}

// Entries returns all the key-value pairs in the trie as a slice of key value
// EntriesList returns all the key-value pairs in the trie as a slice of key value
// where the keys are encoded in Little Endian. The slice starts with root node.
func (t *Trie) EntriesList() [][2][]byte {
list := make([][2][]byte, 0)
Expand Down

0 comments on commit 2eb6313

Please sign in to comment.