Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Add eip1898 support #462

Merged
merged 18 commits into from
Aug 25, 2021
Merged

Conversation

thomas-nguy
Copy link
Contributor

Closes: #460

Description

Add support for eip1898
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1898.md


For contributor use:

  • Targeted PR against correct branch (see CONTRIBUTING.md)
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards.
  • Wrote unit and integration tests
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added relevant godoc comments.
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer

For admin use:

  • Added appropriate labels to PR (ex. WIP, R4R, docs, etc)
  • Reviewers assigned
  • Squashed all commits, uses message "Merge pull request #XYZ: [title]" (coding standards)

Copy link
Contributor

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we can clean up the code to avoid code duplication. Also please add tests for UnmarshalJSON and include relevant code comments

Comment on lines 233 to 228
var blockNum rpctypes.BlockNumber
if blockNrOrHash.BlockHash != nil {
blockHeader, err := e.backend.HeaderByHash(*blockNrOrHash.BlockHash)
if err != nil {
return nil, err
}
blockNum = rpctypes.NewBlockNumber(blockHeader.Number)
} else if blockNrOrHash.BlockNumber != nil {
blockNum = *blockNrOrHash.BlockNumber
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we define this as a function to avoid code duplication?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this?

var foundblock:=getBlock(blockHeader)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

} else if blockNrOrHash.BlockNumber != nil {
blockNum = *blockNrOrHash.BlockNumber
}

e.logger.Debug("eth_getStorageAt", "address", address.Hex(), "key", key, "block number", blockNum)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd define this before the blockNum statement you defined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

ethereum/rpc/types/block.go Show resolved Hide resolved
Comment on lines 117 to 128
type erased BlockNumberOrHash
e := erased{}
err := json.Unmarshal(data, &e)
if err == nil {
if e.BlockNumber != nil && e.BlockHash != nil {
return fmt.Errorf("cannot specify both BlockHash and BlockNumber, choose one or the other")
}
bnh.BlockNumber = e.BlockNumber
bnh.BlockHash = e.BlockHash
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be grouped in one private function that checks for the unmarshaling of the full struct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines 128 to 166
var input string
err = json.Unmarshal(data, &input)
if err != nil {
return err
}
switch input {
case "earliest":
bn := EthEarliestBlockNumber
bnh.BlockNumber = &bn
return nil
case "latest":
bn := EthLatestBlockNumber
bnh.BlockNumber = &bn
return nil
case "pending":
bn := EthPendingBlockNumber
bnh.BlockNumber = &bn
return nil
default:
if len(input) == 66 {
hash := common.Hash{}
err := hash.UnmarshalText([]byte(input))
if err != nil {
return err
}
bnh.BlockHash = &hash
return nil
}

blckNum, err := hexutil.DecodeUint64(input)
if err != nil {
return err
}
if blckNum > math.MaxInt64 {
return fmt.Errorf("blocknumber too high")
}
bn := BlockNumber(blckNum)
bnh.BlockNumber = &bn
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can also be grouped

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

bnh.BlockNumber = &bn
return nil
default:
if len(input) == 66 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 66 length?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the size of the hash string in ethereum ( "0x" + 2 * 32)

if err != nil {
return err
}
if blckNum > math.MaxInt64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int64 is enough ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, blockNumber is coded into an int64

}
blockNum = rpctypes.NewBlockNumber(blockHeader.Number)
} else if blockNrOrHash.BlockNumber != nil {
blockNum = *blockNrOrHash.BlockNumber
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as full word like blockNumber ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@codecov
Copy link

codecov bot commented Aug 23, 2021

Codecov Report

Merging #462 (56d3828) into main (c0ca43f) will decrease coverage by 2.47%.
The diff coverage is 70.68%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #462      +/-   ##
==========================================
- Coverage   50.38%   47.91%   -2.48%     
==========================================
  Files          51       56       +5     
  Lines        5005     5374     +369     
==========================================
+ Hits         2522     2575      +53     
- Misses       2373     2683     +310     
- Partials      110      116       +6     
Impacted Files Coverage Δ
ethereum/rpc/types/block.go 50.47% <70.68%> (ø)
ethereum/rpc/types/addrlock.go 0.00% <0.00%> (ø)
ethereum/rpc/types/utils.go 0.00% <0.00%> (ø)
ethereum/rpc/types/query_client.go 0.00% <0.00%> (ø)
ethereum/rpc/types/types.go 0.00% <0.00%> (ø)

Comment on lines 895 to 901
if blockNrOrHash.BlockHash != nil {
blockHeader, err := e.backend.HeaderByHash(*blockNrOrHash.BlockHash)
if err != nil {
return blockNum, err
}
blockNum = rpctypes.NewBlockNumber(blockHeader.Number)
} else if blockNrOrHash.BlockNumber != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use switch with the following cases:

BlockHash == nil && BlockNumber == nil --> error
BlockHash != nil
BlockNumber != nil
default : EthLatestBlockNumber

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


func (bnh *BlockNumberOrHash) decodeFromString(input string) error {
switch input {
case "earliest":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we use constants for these strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not at the moment, but I can do the change

ethereum/rpc/types/block.go Show resolved Hide resolved
ethereum/rpc/types/block_test.go Show resolved Hide resolved
Copy link
Contributor

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK, can you also update the docs on endpoints.md? 🙏

@fedekunze fedekunze enabled auto-merge (squash) August 24, 2021 12:45
@fedekunze fedekunze merged commit 4ea3cc1 into evmos:main Aug 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EIP-1898 Support
3 participants