From 39e096da643ba92561d61aba993885eb5880c5bd Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 11 Jun 2021 13:39:48 +0300 Subject: [PATCH 1/2] examples: use base64 to encode HASHY token ID Base58 does not preserve one-to-one byte correspondence with the original data, so different combinations of the same number of bytes might have different encoded string length. We use GAS transfer to mint HASHY token, where the token hash is Base58Encode(Ripemd160(data + txHash)). The problem is that `invokescript` RPC call is used to define transfer tx sysfee, thus, txHash during testinvoke differs from the actual one, that's why resulting token ID may have different length during testinvoke and real invoke. As far as we use token ID as a key to store contract values, the storage price may also differ. The result is failing TestNEP11_OwnerOf_BalanceOf_Transfer test due to `gas limit exceeded` error: ``` logger.go:130: 2021-06-10T21:09:08.984+0300 WARN contract invocation failed {"tx": "45a0220b19725eaa0a4d01fa7a6cdaac8498592e8f3b43bdde27aae7d9ecf635", "block": 5, "error": "error encountered at instruction 36 (SYSCALL): error during call from native: error encountered at instruction 22 (SYSCALL): failed to invoke syscall 1736177434: gas limit exceeded"} executor_test.go:219: Error Trace: executor_test.go:219 nep11_test.go:132 nep11_test.go:235 Error: Not equal: expected: 0x2 actual : 0x4 Test: TestNEP11_OwnerOf_BalanceOf_Transfer ``` Fixed by using base64 instead of base58 (base64 preserves the resulting encoded string length for the same input length). --- cli/nep11_test.go | 10 +++++----- examples/nft-nd/nft.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cli/nep11_test.go b/cli/nep11_test.go index 0a0809129..48f34e665 100644 --- a/cli/nep11_test.go +++ b/cli/nep11_test.go @@ -214,7 +214,7 @@ func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) { // tokensOf: good e.Run(t, cmdTokensOf...) - e.checkNextLine(t, string(tokenID)) + require.Equal(t, string(tokenID), e.getNextLine(t)) // properties: no contract cmdProperties := []string{ @@ -240,8 +240,8 @@ func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) { fst, snd = snd, fst } - e.checkNextLine(t, string(fst)) - e.checkNextLine(t, string(snd)) + require.Equal(t, string(fst), e.getNextLine(t)) + require.Equal(t, string(snd), e.getNextLine(t)) // tokens: missing contract hash cmdTokens := []string{"neo-go", "wallet", "nep11", "tokens", @@ -252,8 +252,8 @@ func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) { // tokens: good, several tokens e.Run(t, cmdTokens...) - e.checkNextLine(t, string(fst)) - e.checkNextLine(t, string(snd)) + require.Equal(t, string(fst), e.getNextLine(t)) + require.Equal(t, string(snd), e.getNextLine(t)) // balance check: several tokens, ok e.Run(t, append(cmdCheckBalance, "--token", h.StringLE())...) diff --git a/examples/nft-nd/nft.go b/examples/nft-nd/nft.go index 6dea87361..f153abe23 100644 --- a/examples/nft-nd/nft.go +++ b/examples/nft-nd/nft.go @@ -2,7 +2,7 @@ Package nft contains non-divisible non-fungible NEP11-compatible token implementation. This token can be minted with GAS transfer to contract address, it will hash some data (including data provided in transfer) and produce -base58-encoded string that is your NFT. Since it's based on hashing and basically +base64-encoded string that is your NFT. Since it's based on hashing and basically you own a hash it's HASHY. */ package nft @@ -230,7 +230,7 @@ func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) { } tokenHash := crypto.Ripemd160(tokIn) - token := std.Base58Encode(tokenHash) + token := std.Base64Encode(tokenHash) addToken(ctx, from, []byte(token)) setOwnerOf(ctx, []byte(token), from) From 6ca9c87b9c2e8977a2cff6403962a9004dc72f11 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 11 Jun 2021 13:51:58 +0300 Subject: [PATCH 2/2] Revert "cli: band-aid failing TestNEP11_OwnerOf_BalanceOf_Transfer" This reverts commit f01d94ae53dd5f277d3337c3a7368e463881d619. Extra GAS is not needed here after HASHY fix. --- cli/nep11_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/nep11_test.go b/cli/nep11_test.go index 48f34e665..b61350c03 100644 --- a/cli/nep11_test.go +++ b/cli/nep11_test.go @@ -125,7 +125,6 @@ func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) { e.Run(t, "neo-go", "wallet", "nep17", "transfer", "--rpc-endpoint", "http://"+e.RPC.Addr, "--wallet", wall, - "--gas", "0.001", // test fails sometimes running out of GAS "--to", h.StringLE(), "--token", "GAS", "--amount", "10",