-
Notifications
You must be signed in to change notification settings - Fork 569
Add eip1898 support #462
Add eip1898 support #462
Conversation
There was a problem hiding this 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
ethereum/rpc/namespaces/eth/api.go
Outdated
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 | ||
} | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
ethereum/rpc/namespaces/eth/api.go
Outdated
} else if blockNrOrHash.BlockNumber != nil { | ||
blockNum = *blockNrOrHash.BlockNumber | ||
} | ||
|
||
e.logger.Debug("eth_getStorageAt", "address", address.Hex(), "key", key, "block number", blockNum) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Outdated
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 | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Outdated
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why 66 length?
There was a problem hiding this comment.
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)
ethereum/rpc/types/block.go
Outdated
if err != nil { | ||
return err | ||
} | ||
if blckNum > math.MaxInt64 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int64 is enough ?
There was a problem hiding this comment.
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
ethereum/rpc/namespaces/eth/api.go
Outdated
} | ||
blockNum = rpctypes.NewBlockNumber(blockHeader.Number) | ||
} else if blockNrOrHash.BlockNumber != nil { | ||
blockNum = *blockNrOrHash.BlockNumber |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
Codecov Report
@@ 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
|
ethereum/rpc/namespaces/eth/api.go
Outdated
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
ethereum/rpc/types/block.go
Outdated
|
||
func (bnh *BlockNumberOrHash) decodeFromString(input string) error { | ||
switch input { | ||
case "earliest": |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this 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
? 🙏
0c02e32
to
56d3828
Compare
Closes: #460
Description
Add support for eip1898
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1898.md
For contributor use:
docs/
) or specification (x/<module>/spec/
)godoc
comments.Unreleased
section inCHANGELOG.md
Files changed
in the Github PR explorerFor admin use:
WIP
,R4R
,docs
, etc)