Skip to content

Commit

Permalink
add API GetBlockSummaryByBlockNumber (#102)
Browse files Browse the repository at this point in the history
Co-authored-by: dayong <dayong@conflux-chain.org>
  • Loading branch information
wangdayong228 and dayong authored Aug 5, 2021
1 parent 792356d commit 7b50414
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
4 changes: 3 additions & 1 deletion changeLog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Go-conflux-sdk Change Log
## v1.0.13
- Add API GetBlockSummaryByBlockNumber
## v1.0.12
- Fix test for Marshal/UnMarshal Block
## v1.0.11
- Add `blockNumber` to block related methods `cfx_getBlockByHash`, `cfx_getBlockByEpochNumber`, `cfx_getBlockByHashWithPivotAssumption` which need `Conflux-rust v1.1.5` or above.
- Add new RPC method `cfx_getBlockByBlockNubmer`
- Add new RPC method `cfx_getBlockByBlockNumber`
- Refactor SubscribeLogs for avoiding lossing timing sequence of Chain-Reorg and Log
- Add variadic arguments support for rpc service
## v1.0.10
Expand Down
15 changes: 10 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,23 @@ func (client *Client) GetBlockSummaryByEpoch(epoch *types.Epoch) (blockSummary *
return
}

func (client *Client) GetBlockByBlockNumber(blockNumer hexutil.Uint64) (block *types.Block, err error) {
err = client.wrappedCallRPC(&block, "cfx_getBlockByBlockNumber", blockNumer, true)
return
}

func (client *Client) GetBlockSummaryByBlockNumber(blockNumer hexutil.Uint64) (block *types.BlockSummary, err error) {
err = client.wrappedCallRPC(&block, "cfx_getBlockByBlockNumber", blockNumer, false)
return
}

// GetBlockByEpoch returns the block of specified epoch.
// If the epoch is invalid, return the concrete error.
func (client *Client) GetBlockByEpoch(epoch *types.Epoch) (block *types.Block, err error) {
err = client.wrappedCallRPC(&block, "cfx_getBlockByEpochNumber", epoch, true)
return
}

func (client *Client) GetBlockByBlockNumber(blockNumer hexutil.Uint64, includeTxs ...bool) (block *types.Block, err error) {
err = client.wrappedCallRPC(&block, "cfx_getBlockByBlockNumber", blockNumer, get1stBoolIfy(includeTxs))
return
}

// GetBestBlockHash returns the current best block hash.
func (client *Client) GetBestBlockHash() (hash types.Hash, err error) {
err = client.wrappedCallRPC(&hash, "cfx_getBestBlockHash")
Expand Down
8 changes: 4 additions & 4 deletions example/context/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NodeURL = "ws://localhost:12535"
BlockHash = "0x734f2f46a701cab967f812ed6a97a82419a85a68e0a3450c4c5d79e21780ee09"
TransactionHash = "0xef37ff824229255cf43b1f65b556c3fded6a9edc3257e5b0502b1f27794bd3aa"
BlockHashOfNewContract = "0xbf191ae2fa6706edc3f330ec505e1bf68905dea891bd2f2243e9b9cfb2fb95fe"
ERC20Address = "net3384528341:acc2pmewzkpr34n3deu10s2dm47ssbmxs2zhuuk1j7"
BlockHash = "0x7fc9c1a2b7ad263464ef44a71fd2b3548ff50b35220ee26326031458ad046950"
TransactionHash = "0x2fec58fa823148f67740a0dba4f40dc1ad1817f066818c484e076d3dce438baf"
BlockHashOfNewContract = "0x8b9c78a05a63c9449e347e3a12cd7e173d9190e135f823000abe7dc5ff5fdea7"
ERC20Address = "net3887114884:acc2pmewzkpr34n3deu10s2dm47ssbmxs24hvrjw4d"
11 changes: 6 additions & 5 deletions example/example_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,13 @@ func getBlockByEpoch() {

func GetBlockByBlockNumber() {
fmt.Println("\n- start get block by block number")
if epochNumber, err := client.GetEpochNumber(); err == nil {
_blockNumber := hexutil.Uint64(epochNumber.ToInt().Uint64())
client.GetBlockByBlockNumber(_blockNumber)
client.GetBlockByBlockNumber(_blockNumber, true)
}

b, err := client.GetBlockByHash(config.BlockHash)
utils.PanicIfErr(err)

_blockNumber := hexutil.Uint64(b.BlockNumber.ToInt().Uint64())
client.GetBlockByBlockNumber(_blockNumber)
client.GetBlockSummaryByBlockNumber(_blockNumber)
}

func getBlocksByEpoch() {
Expand Down
3 changes: 2 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ type ClientOperator interface {
GetBlockByHash(blockHash types.Hash) (*types.Block, error)
GetBlockSummaryByEpoch(epoch *types.Epoch) (*types.BlockSummary, error)
GetBlockByEpoch(epoch *types.Epoch) (*types.Block, error)
GetBlockByBlockNumber(blockNumer hexutil.Uint64, includeTxs ...bool) (block *types.Block, err error)
GetBlockByBlockNumber(blockNumer hexutil.Uint64) (block *types.Block, err error)
GetBlockSummaryByBlockNumber(blockNumer hexutil.Uint64) (block *types.BlockSummary, err error)
GetBestBlockHash() (types.Hash, error)
GetRawBlockConfirmationRisk(blockhash types.Hash) (*hexutil.Big, error)
GetBlockConfirmationRisk(blockHash types.Hash) (*big.Float, error)
Expand Down

0 comments on commit 7b50414

Please sign in to comment.