INFURA / go-ethlibs

Ethereum libraries in Go for interacting with Ethereum nodes
MIT License
161 stars 34 forks source link

fix(Block.FromRaw): Use NewQuantityFromRLP in Block.FromRaw #68

Closed ryanschneider closed 1 year ago

ryanschneider commented 2 years ago

When debugging a bad block found on Ropsten today (which I added a test case for in block_from_raw_test.go I ran into another issue: Post merge Block.Difficulty is 0, and since difficulty is a QUANTITY it actually needs to be encoded as 0x80 instead of 0x00.

Luckily in adding EIP-1559 support I already had created a NewQuantityFromRLP function, I just had never gone back to other places where I was decoding RLP and started using it. This PR fixes that and uses the new function instead of special casing a couple block fields in FromRaw.

Also, since I got kind of confused about the fact that 0x00 totally is valid RLP, but is NOT a valid representation of a quantity, I added test cases in rlp_from_test.go with more specific examples.

Anyways this all turned out to be a total side quest, as the bad block was totally unrelated to any of these issues, but rather an issue where Erigon wasn't properly filtering out EIP-1559 txs with the wrong chain id, but since I ran into these issues while debugging this I figured I'd fix them.

I also updated the rlp/fuzz subdir and our go.mod files to work w/ later versions of go-fuzz. I still need to convert this project to using the new builtin fuzzer but that's too far out of scope for this PR.

ryanschneider commented 1 year ago

I'm just wondering if there are any other places where this function should be used.

Good point, I just did a search for NewQuantity(.*\.String) and only hit the implementation of NewQuantityFromRLP so I think this PR got them all:

$ git grep 'NewQuantity(.*\.String)' | cat
eth/quantity.go:    return NewQuantity(v.String)