Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib/runtime): return correct encoded value for None #3451

Merged
merged 16 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
b37c34f
chore: instead of 0 return and empty optional `0x01, 0x00`
EclesioMeloJunior Aug 22, 2023
55f89ea
feat: standartize the none encoded returns in the host functions
EclesioMeloJunior Aug 23, 2023
f26ef82
Merge branch 'development' into eclesio/host-optional-returns
EclesioMeloJunior Aug 23, 2023
98e3815
chore: deploy right none encoded value
EclesioMeloJunior Aug 23, 2023
09e74a3
Merge branch 'development' into eclesio/host-optional-returns
EclesioMeloJunior Aug 24, 2023
714aabc
Merge branch 'eclesio/host-optional-returns' of github.com:ChainSafe/…
EclesioMeloJunior Aug 24, 2023
a2d5a40
chore: check for `nil` since `Some(())` is len 0
EclesioMeloJunior Aug 25, 2023
ec2bacc
chore: check if child is not nil
EclesioMeloJunior Aug 28, 2023
df577df
chore: test flaky test
EclesioMeloJunior Aug 28, 2023
817f812
chore: link todo issue
EclesioMeloJunior Aug 28, 2023
4fb69f2
Merge branch 'development' into eclesio/host-optional-returns
EclesioMeloJunior Aug 28, 2023
3da167a
Merge branch 'development' into eclesio/host-optional-returns
EclesioMeloJunior Aug 28, 2023
4b28d84
chore: improve coverage on `Test_ext_default_child_storage_get_versio…
EclesioMeloJunior Aug 28, 2023
82bfae9
Merge branch 'eclesio/host-optional-returns' of github.com:ChainSafe/…
EclesioMeloJunior Aug 28, 2023
7274614
chore: increase code cov on `Test_ext_default_child_storage_read_vers…
EclesioMeloJunior Aug 28, 2023
43ff124
chore: use `mustWrite` to reduce code repetition
EclesioMeloJunior Aug 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion dot/network/transaction_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func TestHandleTransactionMessage(t *testing.T) {
HandleTransactionMessage(peer.ID(""), expectedMsgArg).
Return(true, nil)

transactionHandler.EXPECT().TransactionsCount().Return(0)
// TODO: https://github.com/ChainSafe/gossamer/issues/1975
transactionHandler.EXPECT().
TransactionsCount().
Return(0).AnyTimes()

config := &Config{
BasePath: t.TempDir(),
Expand Down
117 changes: 61 additions & 56 deletions lib/runtime/wazero/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
log.AddContext("pkg", "runtime"),
log.AddContext("module", "wazero"),
)

noneEncoded []byte = []byte{0x00}
)

const (
Expand Down Expand Up @@ -229,13 +231,17 @@ func ext_crypto_ed25519_sign_version_1(ctx context.Context, m api.Module, keyTyp
pubKey, err := ed25519.NewPublicKey(pubKeyData)
if err != nil {
logger.Errorf("failed to get public keys: %s", err)
return 0
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
return ret
}

ks, err := rtCtx.Keystore.GetKeystore(id)
if err != nil {
logger.Warnf("error for id 0x%x: %s", id, err)
ret, err := write(m, rtCtx.Allocator, []byte{0})
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
Expand All @@ -245,7 +251,7 @@ func ext_crypto_ed25519_sign_version_1(ctx context.Context, m api.Module, keyTyp
signingKey := ks.GetKeypair(pubKey)
if signingKey == nil {
logger.Error("could not find public key " + pubKey.Hex() + " in keystore")
ret, err := write(m, rtCtx.Allocator, []byte{0})
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
Expand All @@ -255,25 +261,18 @@ func ext_crypto_ed25519_sign_version_1(ctx context.Context, m api.Module, keyTyp
sig, err := signingKey.Sign(read(m, msg))
if err != nil {
logger.Error("could not sign message")
}

var fixedSize [64]byte
copy(fixedSize[:], sig)

encoded, err := scale.Marshal(&fixedSize)
if err != nil {
logger.Error(fmt.Sprintf("scale encoding: %s", err))
ret, err := write(m, rtCtx.Allocator, []byte{0})
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
return ret
}

ret, err := write(m, rtCtx.Allocator, encoded)
var fixedSize [64]byte
copy(fixedSize[:], sig)
ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(&fixedSize))
if err != nil {
logger.Errorf("failed to allocate memory: %s", err)
return 0
panic(err)
}

return ret
Expand Down Expand Up @@ -629,12 +628,10 @@ func ext_crypto_sr25519_sign_version_1(ctx context.Context, m api.Module, keyTyp
panic("read overflow")
}

var optionSig *[64]byte

ks, err := rtCtx.Keystore.GetKeystore(id)
if err != nil {
logger.Warnf("error for id 0x%x: %s", id, err)
ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(optionSig))
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
Expand All @@ -649,7 +646,7 @@ func ext_crypto_sr25519_sign_version_1(ctx context.Context, m api.Module, keyTyp
pubKey, err := sr25519.NewPublicKey(kb)
if err != nil {
logger.Errorf("failed to get public key: %s", err)
ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(optionSig))
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
Expand All @@ -659,7 +656,7 @@ func ext_crypto_sr25519_sign_version_1(ctx context.Context, m api.Module, keyTyp
signingKey := ks.GetKeypair(pubKey)
if signingKey == nil {
logger.Error("could not find public key " + pubKey.Hex() + " in keystore")
ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(optionSig))
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
Expand All @@ -670,7 +667,7 @@ func ext_crypto_sr25519_sign_version_1(ctx context.Context, m api.Module, keyTyp
sig, err := signingKey.Sign(msgData)
if err != nil {
logger.Errorf("could not sign message: %s", err)
ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(optionSig))
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
Expand All @@ -679,16 +676,10 @@ func ext_crypto_sr25519_sign_version_1(ctx context.Context, m api.Module, keyTyp

var fixedSig [64]byte
copy(fixedSig[:], sig)
optionSig = &fixedSig

keysPtr, err := write(m, rtCtx.Allocator, scale.MustMarshal(optionSig))
keysPtr, err := write(m, rtCtx.Allocator, scale.MustMarshal(&fixedSig))
if err != nil {
logger.Errorf("failed to allocate memory: %s", err)
ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(optionSig))
if err != nil {
panic(err)
}
return ret
panic(err)
}
return keysPtr
}
Expand Down Expand Up @@ -990,11 +981,10 @@ func ext_misc_runtime_version_version_1(ctx context.Context, m api.Module, dataS
}
code := read(m, dataSpan)

var option *[]byte
version, err := GetRuntimeVersion(code)
if err != nil {
logger.Errorf("failed to get runtime version: %s", err)
ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(option))
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
Expand All @@ -1011,12 +1001,14 @@ func ext_misc_runtime_version_version_1(ctx context.Context, m api.Module, dataS
encodedData, err := scale.Marshal(version)
if err != nil {
logger.Errorf("failed to encode result: %s", err)
return 0
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
return ret
}

option = &encodedData

ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(option))
ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(&encodedData))
if err != nil {
panic(err)
}
Expand All @@ -1035,7 +1027,11 @@ func ext_default_child_storage_read_version_1(
value, err := rtCtx.Storage.GetChildStorage(keyToChild, keyBytes)
if err != nil {
logger.Errorf("failed to get child storage: %s", err)
return 0
ret, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
panic(err)
}
return ret
}

valueBuf, _ := splitPointerSize(valueOut)
Expand Down Expand Up @@ -1192,6 +1188,7 @@ func ext_default_child_storage_exists_version_1(ctx context.Context, m api.Modul
logger.Errorf("failed to get child from child storage: %s", err)
return 0
}

if child != nil {
return 1
}
Expand All @@ -1208,15 +1205,20 @@ func ext_default_child_storage_get_version_1(ctx context.Context, m api.Module,
keyToChild := read(m, childStorageKey)
keyBytes := read(m, key)
child, err := storage.GetChildStorage(keyToChild, keyBytes)
if err != nil {
logger.Errorf("failed to get child from child storage: %s", err)
return 0
var encodedChildOptional []byte

if err != nil || child == nil {
logger.Warnf("child storage not found: %s", err)
encodedChildOptional = noneEncoded
} else {
encodedChildOptional = scale.MustMarshal(&child)
}

ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(&child))
ret, err := write(m, rtCtx.Allocator, encodedChildOptional)
if err != nil {
panic(err)
}

return ret
}

Expand Down Expand Up @@ -1707,11 +1709,15 @@ func ext_offchain_local_storage_get_version_1(ctx context.Context, m api.Module,
res, err = rtCtx.NodeStorage.LocalStorage.Get(storageKey)
}

if err != nil {
var encodedOption []byte
if err != nil || len(res) == 0 {
logger.Errorf("failed to get value from storage: %s", err)
encodedOption = noneEncoded
} else {
encodedOption = res
}

ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(&res))
ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(&encodedOption))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -2146,12 +2152,14 @@ func ext_storage_get_version_1(ctx context.Context, m api.Module, keySpan uint64
value := storage.Get(key)
logger.Debugf("value: 0x%x", value)

var option *[]byte
var encodedOption []byte
if value != nil {
option = &value
encodedOption = scale.MustMarshal(&value)
} else {
encodedOption = noneEncoded
}

valueSpan, err := write(m, rtCtx.Allocator, scale.MustMarshal(option))
valueSpan, err := write(m, rtCtx.Allocator, encodedOption)
if err != nil {
logger.Errorf("failed to allocate: %s", err)
panic(err)
Expand All @@ -2173,12 +2181,14 @@ func ext_storage_next_key_version_1(ctx context.Context, m api.Module, keySpan u
"key: 0x%x; next key 0x%x",
key, next)

var option *[]byte
if len(next) > 0 {
option = &next
var encodedOption []byte
if len(next) == 0 {
encodedOption = noneEncoded
} else {
encodedOption = scale.MustMarshal(&next)
}

nextSpan, err := write(m, rtCtx.Allocator, scale.MustMarshal(option))
nextSpan, err := write(m, rtCtx.Allocator, encodedOption)
if err != nil {
logger.Errorf("failed to allocate: %s", err)
panic(err)
Expand All @@ -2193,17 +2203,14 @@ func ext_storage_read_version_1(ctx context.Context, m api.Module, keySpan, valu
}
storage := rtCtx.Storage

// memory := instanceContext.Memory().Data()
var option *uint32 = nil

key := read(m, keySpan)
value := storage.Get(key)
logger.Debugf(
"key 0x%x has value 0x%x",
key, value)

if value == nil {
res, err := write(m, rtCtx.Allocator, scale.MustMarshal(option))
res, err := write(m, rtCtx.Allocator, noneEncoded)
if err != nil {
logger.Errorf("failed to allocate: %s", err)
panic(err)
Expand Down Expand Up @@ -2233,9 +2240,7 @@ func ext_storage_read_version_1(ctx context.Context, m api.Module, keySpan, valu
}

size := uint32(len(data))
option = &size

sizeSpan, err := write(m, rtCtx.Allocator, scale.MustMarshal(option))
sizeSpan, err := write(m, rtCtx.Allocator, scale.MustMarshal(&size))
if err != nil {
logger.Errorf("failed to allocate: %s", err)
panic(err)
Expand Down