Skip to content

Commit

Permalink
eth: core: implement final block #24282
Browse files Browse the repository at this point in the history
备注:只合并了影响API调用的部分逻辑
  • Loading branch information
niuxiaojie81 committed Nov 10, 2023
1 parent 1d658c6 commit 5c3e18c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error
return stateDb.RawDump(opts), nil
}
var block *types.Block
if blockNr == rpc.LatestBlockNumber {
if blockNr == rpc.LatestBlockNumber || blockNr == rpc.FinalizedBlockNumber {
block = api.eth.blockchain.CurrentBlock()
} else {
block = api.eth.blockchain.GetBlockByNumber(uint64(blockNr))
Expand Down Expand Up @@ -306,7 +306,7 @@ func (api *PrivateDebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, st
_, stateDb = api.eth.miner.Pending()
} else {
var block *types.Block
if number == rpc.LatestBlockNumber {
if number == rpc.LatestBlockNumber || number == rpc.FinalizedBlockNumber {
block = api.eth.blockchain.CurrentBlock()
} else {
block = api.eth.blockchain.GetBlockByNumber(uint64(number))
Expand Down
4 changes: 2 additions & 2 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumb
return block.Header(), nil
}
// Otherwise resolve and return the block
if number == rpc.LatestBlockNumber {
if number == rpc.LatestBlockNumber || number == rpc.FinalizedBlockNumber {
return b.eth.blockchain.CurrentBlock().Header(), nil
}
return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
Expand Down Expand Up @@ -111,7 +111,7 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
return block, nil
}
// Otherwise resolve and return the block
if number == rpc.LatestBlockNumber {
if number == rpc.LatestBlockNumber || number == rpc.FinalizedBlockNumber {
return b.eth.blockchain.CurrentBlock(), nil
}
return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
Expand Down
2 changes: 1 addition & 1 deletion internal/jsre/deps/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4012,7 +4012,7 @@ require=(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c=
};

var isPredefinedBlockNumber = function (blockNumber) {
return blockNumber === 'latest' || blockNumber === 'pending' || blockNumber === 'earliest';
return blockNumber === 'latest' || blockNumber === 'pending' || blockNumber === 'earliest' || blockNumber === 'finalized';
};

var inputDefaultBlockNumberFormatter = function (blockNumber) {
Expand Down
16 changes: 13 additions & 3 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ type jsonWriter interface {
type BlockNumber int64

const (
PendingBlockNumber = BlockNumber(-2)
LatestBlockNumber = BlockNumber(-1)
EarliestBlockNumber = BlockNumber(0)
FinalizedBlockNumber = BlockNumber(-3)
PendingBlockNumber = BlockNumber(-2)
LatestBlockNumber = BlockNumber(-1)
EarliestBlockNumber = BlockNumber(0)
)

// UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
Expand All @@ -89,6 +90,9 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
case "pending":
*bn = PendingBlockNumber
return nil
case "finalized":
*bn = FinalizedBlockNumber
return nil
}

blckNum, err := hexutil.DecodeUint64(input)
Expand All @@ -113,6 +117,8 @@ func (bn BlockNumber) MarshalText() ([]byte, error) {
return []byte("latest"), nil
case PendingBlockNumber:
return []byte("pending"), nil
case FinalizedBlockNumber:
return []byte("finalized"), nil
default:
return hexutil.Uint64(bn).MarshalText()
}
Expand Down Expand Up @@ -159,6 +165,10 @@ func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error {
bn := PendingBlockNumber
bnh.BlockNumber = &bn
return nil
case "finalized":
bn := FinalizedBlockNumber
bnh.BlockNumber = &bn
return nil
default:
if len(input) == 66 {
hash := common.Hash{}
Expand Down

0 comments on commit 5c3e18c

Please sign in to comment.