This repository was archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 587
Add eip1898 support #462
Merged
fedekunze
merged 18 commits into
evmos:main
from
thomas-nguy:thomas/460-eip1898-support
Aug 25, 2021
Merged
Add eip1898 support #462
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
90dfca3
support for eip1898
thomas-nguy ab860be
update changelog
thomas-nguy 18cb29e
fix linter
thomas-nguy 9392a39
fix linter
thomas-nguy e466500
refactor code
thomas-nguy 013bbd2
add test
thomas-nguy 6b7d069
cleanup code
thomas-nguy 4c47c0d
add comment
thomas-nguy 35fb189
support for eip1898
thomas-nguy 4d4c3f8
fix linter
thomas-nguy 426d392
fix linter
thomas-nguy 9717e4e
refactor code
thomas-nguy fcf6ef3
add test
thomas-nguy de8e8fc
create const for block param
thomas-nguy f5e8882
change default to be earliest to be consistant with geth
thomas-nguy 4b6dc97
correct comment
thomas-nguy 99164d9
update doc
thomas-nguy 56d3828
update doc
thomas-nguy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,14 @@ package types | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"math" | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
|
||
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" | ||
"github.com/spf13/cast" | ||
"google.golang.org/grpc/metadata" | ||
|
@@ -23,6 +26,12 @@ const ( | |
EthEarliestBlockNumber = BlockNumber(0) | ||
) | ||
|
||
const ( | ||
BlockParamEarliest = "earliest" | ||
BlockParamLatest = "latest" | ||
BlockParamPending = "pending" | ||
) | ||
|
||
// NewBlockNumber creates a new BlockNumber instance. | ||
func NewBlockNumber(n *big.Int) BlockNumber { | ||
return BlockNumber(n.Int64()) | ||
|
@@ -52,13 +61,13 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error { | |
} | ||
|
||
switch input { | ||
case "earliest": | ||
case BlockParamEarliest: | ||
*bn = EthEarliestBlockNumber | ||
return nil | ||
case "latest": | ||
case BlockParamLatest: | ||
*bn = EthLatestBlockNumber | ||
return nil | ||
case "pending": | ||
case BlockParamPending: | ||
*bn = EthPendingBlockNumber | ||
return nil | ||
} | ||
|
@@ -103,3 +112,74 @@ func (bn BlockNumber) TmHeight() *int64 { | |
height := bn.Int64() | ||
return &height | ||
} | ||
|
||
// BlockNumberOrHash represents a block number or a block hash. | ||
type BlockNumberOrHash struct { | ||
BlockNumber *BlockNumber `json:"blockNumber,omitempty"` | ||
BlockHash *common.Hash `json:"blockHash,omitempty"` | ||
} | ||
|
||
func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error { | ||
type erased BlockNumberOrHash | ||
e := erased{} | ||
err := json.Unmarshal(data, &e) | ||
if err == nil { | ||
return bnh.checkUnmarshal(BlockNumberOrHash(e)) | ||
} | ||
var input string | ||
err = json.Unmarshal(data, &input) | ||
if err != nil { | ||
return err | ||
} | ||
err = bnh.decodeFromString(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (bnh *BlockNumberOrHash) checkUnmarshal(e BlockNumberOrHash) error { | ||
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 | ||
} | ||
|
||
func (bnh *BlockNumberOrHash) decodeFromString(input string) error { | ||
switch input { | ||
case BlockParamEarliest: | ||
bn := EthEarliestBlockNumber | ||
bnh.BlockNumber = &bn | ||
case BlockParamLatest: | ||
bn := EthLatestBlockNumber | ||
bnh.BlockNumber = &bn | ||
case BlockParamPending: | ||
bn := EthPendingBlockNumber | ||
bnh.BlockNumber = &bn | ||
default: | ||
fedekunze marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// check if the input is a block hash | ||
if len(input) == 66 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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) |
||
hash := common.Hash{} | ||
err := hash.UnmarshalText([]byte(input)) | ||
if err != nil { | ||
return err | ||
} | ||
bnh.BlockHash = &hash | ||
break | ||
} | ||
// otherwise take the hex string has int64 value | ||
blockNumber, err := hexutil.DecodeUint64(input) | ||
if err != nil { | ||
return err | ||
} | ||
if blockNumber > math.MaxInt64 { | ||
return fmt.Errorf("blocknumber %d is too high", blockNumber) | ||
} | ||
fedekunze marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bn := BlockNumber(blockNumber) | ||
bnh.BlockNumber = &bn | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.