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

invalid block nonce #149

Closed
rafaelkallis opened this issue Jun 3, 2022 · 11 comments · Fixed by #156 or #260
Closed

invalid block nonce #149

rafaelkallis opened this issue Jun 3, 2022 · 11 comments · Fixed by #156 or #260
Assignees
Labels
bug Something isn't working P1
Milestone

Comments

@rafaelkallis
Copy link
Contributor

rafaelkallis commented Jun 3, 2022

Description

Hi! I trying to consume the relay node json-rpc api using go-ethereum, specifically I am trying to get a block by number.

The operation fails with the error hex string of odd length.

I wrote a test case for you to reproduce:

package main

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

	"github.com/ethereum/go-ethereum"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/ethereum/go-ethereum/rpc"
	"github.com/stretchr/testify/assert"

	log "github.com/sirupsen/logrus"
)

func createRelayEthClient() *ethclient.Client {
	rpcClient, err := rpc.Dial("http://localhost:7546")
	if err != nil {
		log.Panicf("error connecting to hedera relay node: %s", err.Error())
	}
	return ethclient.NewClient(rpcClient)
}

func TestBlockByNumber(t *testing.T) {
	ethClient := createRelayEthClient()
	defer ethClient.Close()

	blockNumber := big.NewInt(21370223)
	block, err := ethClient.BlockByNumber(context.Background(), blockNumber)
	if err != nil {
		t.Error(err)
		return
	}
	assert.Equal(t, blockNumber, block.Number)
}

Steps to reproduce

  1. Start relay node locally using docker and connect to public (testnet) mirror node
  2. Run the go script above

Additional context

Relay logs

[2022-06-03 18:21:31.469 +0000] DEBUG (rpc-server/50 on 0a0a8226dc9a): eth_getBlockByNumber
[2022-06-03 18:21:31.469 +0000] TRACE (relay-eth/50 on 0a0a8226dc9a): getBlockByNumber(blockNum=21370223, showDetails=%o)
[2022-06-03 18:21:31.469 +0000] DEBUG (mirror-node/50 on 0a0a8226dc9a): Mirror Request: blocks/21370223
[2022-06-03 18:21:32.167 +0000] DEBUG (mirror-node/50 on 0a0a8226dc9a): Mirror Request: contracts/results?timestamp=gte:1653669740.939629000&timestamp=lte:1653669740.939629000
[2022-06-03 18:21:32.692 +0000] INFO (rpc-server/50 on 0a0a8226dc9a): [POST] eth_getBlockByNumber: 1223 ms Success

Hedera network

testnet

Version

0.1.0-rc1

Operating system

Linux

@rafaelkallis rafaelkallis added the bug Something isn't working label Jun 3, 2022
@Nana-EC
Copy link
Collaborator

Nana-EC commented Jun 3, 2022

Thanks @rafaelkallis .
Can you kindly share the relay logs output as that should give further insight as to what the relay is seeing

@rafaelkallis
Copy link
Contributor Author

@Nana-EC I have added relay logs to the ticket above.

@Nana-EC
Copy link
Collaborator

Nana-EC commented Jun 3, 2022

Thanks @rafaelkallis
Hmmm, strange, per the logs the call is successful.
The flow is as expected and server reports success on return

Let's get more info.
You can add the following line to the logAndHandleResponse method so you can see what the response is and what the issue may be

    logger.info(`${methodName} response: ${JSON.stringify(response)}`);

My current guess is this is an issue on the go client side so this should help you see the response item

@rafaelkallis
Copy link
Contributor Author

rafaelkallis commented Jun 3, 2022

Here you go, do you see anything odd in the response?

curl -X POST http://localhost:7546 \
                                       -H 'Content-Type: application/json' \
                                       -d '{"id":"2","jsonrpc":"2.0","method":"eth_getBlockByNumber","params":[21370223,true]}' --silent | jq
{
  "result": {
    "timestamp": "0x0",
    "difficulty": "0x0",
    "extraData": "0x",
    "gasLimit": "0x0",
    "baseFeePerGas": "0x0",
    "gasUsed": "0x0",
    "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "miner": "0x0000000000000000000000000000000000000000",
    "mixHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
    "nonce": "0x0",
    "receiptsRoot": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
    "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
    "size": "0x0",
    "stateRoot": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
    "totalDifficulty": "0x0",
    "transactions": [],
    "transactionsRoot": "0x7983627d806b9888f818591ebb4dee99ceec9f9e3808f6e4ee0e85ccd9309fad",
    "uncles": [],
    "number": "0x146156f",
    "hash": "0x7983627d806b9888f818591ebb4dee99ceec9f9e3808f6e4ee0e85ccd9309fad",
    "parentHash": "0x60fa091d11691db6f2cf05bd30359384c71e51c32f69fa2ea9ca27f350d5d59c"
  },
  "jsonrpc": "2.0",
  "id": "2"
}

I also tried the request using the metamask injected window.ethereum client, that seems to be fine as well
Screenshot from 2022-06-03 22-04-51

@rafaelkallis
Copy link
Contributor Author

rafaelkallis commented Jun 3, 2022

My hypothesis is that the nonce prop is the issue.

The go-ethereum client expects exactly 8 bytes for the nonce (see https:/ethereum/go-ethereum/blob/master/core/types/block.go#L42)

The relay node sends 0x0. I tested with a BSC rpc node, it uses 0x0000000000000000 as a nonce value and it works fine.

To confirm I will setup a wiremock server and mock jsonrpc responses to confirm the behavior of go-ethereum. Expect a PR soon once I confirm the behavior.

@rafaelkallis
Copy link
Contributor Author

You actually have the right constant here

public readonly nonce:string = '0x0000000000000000';

but use the "wrong" one here

nonce: EthImpl.zeroHex,

@Nana-EC
Copy link
Collaborator

Nana-EC commented Jun 3, 2022

Thanks. Good catch. Nonce return should be 8 Bytes.
Would you like to spin up your 2nd PR of the day to address this :)
If not i'll get one out

@Nana-EC Nana-EC self-assigned this Jun 3, 2022
@Nana-EC Nana-EC added the P1 label Jun 3, 2022
@Nana-EC Nana-EC modified the milestones: 0.1.0, 0.2.0 Jun 3, 2022
@Nana-EC
Copy link
Collaborator

Nana-EC commented Jun 3, 2022

@rafaelkallis picking this up now

@shemnon
Copy link
Contributor

shemnon commented Jun 6, 2022

For the record go-ethereum is incorrect based on the published specs. Nonce for blocks is of type bytes, and not a fixed length bytes, just bytes - https:/ethereum/execution-apis/blob/main/src/schemas/block.json#L82 and bytes can have any number of hex digits (including zero!), it is not constrained to 2 bytes pairs - https:/ethereum/execution-apis/blob/main/src/schemas/base-types.json#L17

Fix can stay, it improves compatibility within the bounds of the spec, so it's reasonable.

@shemnon
Copy link
Contributor

shemnon commented Jun 6, 2022

Original spec does specify 8 bytes for a block hash however - https://eth.wiki/json-rpc/API#eth_getblockbyhash. I'll raise the issue in the execution APIs.

@rafaelkallis rafaelkallis changed the title go-ethereum BlockByNumber error "hex string of odd length" invalid block nonce Jun 23, 2022
@rafaelkallis
Copy link
Contributor Author

@Nana-EC the nonce value is again wrong, please reopen

@Nana-EC Nana-EC reopened this Jun 23, 2022
@Nana-EC Nana-EC modified the milestones: 0.2.0, 0.3.0 Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P1
Projects
None yet
3 participants