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

TransactionByHash returns error #3932

Closed
envestcc opened this issue Sep 21, 2023 · 2 comments · Fixed by #3933
Closed

TransactionByHash returns error #3932

envestcc opened this issue Sep 21, 2023 · 2 comments · Fixed by #3933
Assignees
Labels
bug Something isn't working

Comments

@envestcc
Copy link
Member

What version of iotex-core image (or code branch) are you using?

v1.11.1

What operating system and processor architecture are you using?

macOS, M1 Pro

What did you do? If possible, provide a recipe for reproducing the error.

querying transaction via ethclient on testnet for tx hash 0xcbc2560d986d79a46bfd96a08d18c6045b29f97352c1360289e371d9cffd6b6a returns error:

package main

import (
	"context"
	"math/big"
	"testing"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/common/hexutil"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/stretchr/testify/require"
)

func TestIoTexRead(t *testing.T) {
	r := require.New(t)

	cli, err := ethclient.Dial("https://babel-api.testnet.iotex.io")
	r.NoError(err)
	tx, isPending, err := cli.TransactionByHash(context.Background(), common.HexToHash("0xcbc2560d986d79a46bfd96a08d18c6045b29f97352c1360289e371d9cffd6b6a"))
	r.NoError(err)
	r.False(isPending)
	r.NotNil(tx)
	t.Logf("%+v", tx)
}

What did you expect to see?

should return transaction information

What did you see instead?

❯ go test -v -timeout 45s -run ^TestIoTexRead$ testgo/eth
=== RUN   TestIoTexRead
    base_test.go:36: 
                Error Trace:    /Users/chenchen/dev/testgo/eth/base_test.go:36
                Error:          Received unexpected error:
                                json: cannot unmarshal hex number with leading zero digits into Go struct field txJSON.r of type *hexutil.Big
                Test:           TestIoTexRead
--- FAIL: TestIoTexRead (0.90s)
FAIL
FAIL    testgo/eth      1.045s
FAIL
@envestcc envestcc added the bug Something isn't working label Sep 21, 2023
@envestcc
Copy link
Member Author

The output of eth_getTransactionByHash for this tx is as follows:

{
    "hash": "0xcbc2560d986d79a46bfd96a08d18c6045b29f97352c1360289e371d9cffd6b6a",
    "nonce": "0x131",
    "blockHash": "0xbdcf722cbc0dc9b9eb5b46ad4af9e15944a886310b1cb2227129070c0e4b7de5",
    "blockNumber": "0x1551bdb",
    "transactionIndex": "0x0",
    "from": "0xec4daee51c4bf81bd00af165f8ca66823ee3b12a",
    "to": "0xc3527348De07d591c9d567ce1998eFA2031B8675",
    "value": "0x0",
    "gasPrice": "0xe8d4a51000",
    "gas": "0x488ab",
    "input": "0x1fad948c0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ec4daee51c4bf81bd00af165f8ca66823ee3b12a000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000003e24491a4f2a946e30baf624fda6b4484d106c12000000000000000000000000000000000000000000000000000000000000005b000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000009fd90000000000000000000000000000000000000000000000000000000000011da4000000000000000000000000000000000000000000000000000000000000db26000000000000000000000000000000000000000000000000000000e8d4a51000000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084b61d27f6000000000000000000000000065e1164818487818e6ba714e8d80b91718ad75800000000000000000000000000000000000000000000000000038d7ea4c6800000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095ccc7012efb2e65aa31752f3ac01e23817c08a47500000000000000000000000000000000000000000000000000000000650af9f8000000000000000000000000000000000000000000000000000000006509a878b5acba7277159ae6fa661ed1988cc10ac2c96c58dc332bde2a6dc0d8531ea3924d9d04cda681c271411250ae7d9e9aea47661dba67a66f08d19804a255e45c561b0000000000000000000000000000000000000000000000000000000000000000000000000000000000004160daa88165299ca7e585d5d286cee98b54397b57ac704b74331a48d67651195322ef3884c7d60023333f2542a07936f34edc9efa3cbd19e8cd0f8972c54171a21b00000000000000000000000000000000000000000000000000000000000000",
    "r": "0x00d50c3169003e7c9c9392069a1c9e5251c8f558120cceb9aa312f0fa4624770",
    "s": "0x1f4cad47a5540cdda5459f7a46c62df752ca588e0b73722ec767ff08994134a4",
    "v": "0x1b"
}

According to the error message, the reason should be that the format of the r field is invalid, which can not begin with 0.

@envestcc
Copy link
Member Author

Compare the implementation:
iotex

// api/web3server_marshal.go:263
R:                byteToHex(obj.signature[:32]),

// api/web3server_utils.go:247
func byteToHex(b []byte) string {
	return "0x" + hex.EncodeToString(b)
}

eth

// internal/ethapi/api.go:1446
v, r, s := tx.RawSignatureValues()
...
R:        (*hexutil.Big)(r),

// common/hexutil/hexutil.go:179
func EncodeBig(bigint *big.Int) string {
	if sign := bigint.Sign(); sign == 0 {
		return "0x0"
	} else if sign > 0 {
		return "0x" + bigint.Text(16)
	} else {
		return "-0x" + bigint.Text(16)[1:]
	}
}

// core/types/transaction_signing.go:536
func decodeSignature(sig []byte) (r, s, v *big.Int) {
	if len(sig) != crypto.SignatureLength {
		panic(fmt.Sprintf("wrong size for signature: got %d, want %d", len(sig), crypto.SignatureLength))
	}
	r = new(big.Int).SetBytes(sig[:32])
	s = new(big.Int).SetBytes(sig[32:64])
	v = new(big.Int).SetBytes([]byte{sig[64] + 27})
	return r, s, v
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant