Skip to content

Commit

Permalink
feat: add GetBlockEconomicState
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Mar 10, 2021
1 parent fc920d4 commit 0edae6a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
33 changes: 33 additions & 0 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ type Client interface {

// GetCellbaseOutputCapacityDetails returns each component of the created CKB in this block's cellbase,
// which is issued to a block N - 1 - ProposalWindow.farthest, where this block's height is N.
// Deprecated: This method will be disabled by default from v0.40.0 and will be removed from v0.41.0,Please use the method GetBlockEconomicState instead.
GetCellbaseOutputCapacityDetails(ctx context.Context, hash types.Hash) (*types.BlockReward, error)

// GetBlockEconomicState return block economic state, It includes the rewards details and when it is finalized.
GetBlockEconomicState(ctx context.Context, hash types.Hash) (*types.BlockEconomicState, error)

// GetBlockByNumber get block by number
GetBlockByNumber(ctx context.Context, number uint64) (*types.Block, error)

Expand Down Expand Up @@ -292,6 +296,7 @@ func (cli *client) GetTransaction(ctx context.Context, hash types.Hash) (*types.
}, err
}

// Deprecated: This method will be disabled by default from v0.40.0 and will be removed from v0.41.0,Please use the method GetBlockEconomicState instead.
func (cli *client) GetCellbaseOutputCapacityDetails(ctx context.Context, hash types.Hash) (*types.BlockReward, error) {
var result blockReward
err := cli.c.CallContext(ctx, &result, "get_cellbase_output_capacity_details", hash)
Expand Down Expand Up @@ -692,3 +697,31 @@ func (cli *client) GetTransactions(ctx context.Context, searchKey *indexer.Searc
}
return cli.indexer.GetTransactions(ctx, searchKey, order, limit, afterCursor)
}

func (cli *client) GetBlockEconomicState(ctx context.Context, blockHash types.Hash) (*types.BlockEconomicState, error) {
var result blockEconomicState
err := cli.c.CallContext(ctx, &result, "get_block_economic_state", blockHash)
if err != nil {
return nil, err
}

// if FinalizedAt is equal to "0x0000000000000000000000000000000000000000000000000000000000000000" means block economic state is empty
if result.FinalizedAt == types.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000") {
return nil, nil
}

return &types.BlockEconomicState{
Issuance: types.BlockIssuance{
Primary: (*big.Int)(&result.Issuance.Primary),
Secondary: (*big.Int)(&result.Issuance.Secondary),
},
MinerReward: types.MinerReward{
Primary: (*big.Int)(&result.MinerReward.Primary),
Secondary: (*big.Int)(&result.MinerReward.Secondary),
Committed: (*big.Int)(&result.MinerReward.Committed),
Proposal: (*big.Int)(&result.MinerReward.Proposal),
},
TxsFee: (*big.Int)(&result.TxsFee),
FinalizedAt: result.FinalizedAt,
}, err
}
19 changes: 19 additions & 0 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ type blockchainInfo struct {
MedianTime hexutil.Uint64 `json:"median_time"`
}

type blockEconomicState struct {
Issuance blockIssuance `json:"issuance"`
MinerReward minerReward `json:"miner_reward"`
TxsFee hexutil.Big `json:"txs_fee"`
FinalizedAt types.Hash `json:"finalized_at"`
}

type blockIssuance struct {
Primary hexutil.Big `json:"primary"`
Secondary hexutil.Big `json:"secondary"`
}

type minerReward struct {
Primary hexutil.Big `json:"primary"`
Secondary hexutil.Big `json:"secondary"`
Committed hexutil.Big `json:"committed"`
Proposal hexutil.Big `json:"proposal"`
}

func toHeader(head header) *types.Header {
return &types.Header{
CompactTarget: uint(head.CompactTarget),
Expand Down
19 changes: 19 additions & 0 deletions types/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,22 @@ type BlockReward struct {
Total *big.Int `json:"total"`
TxFee *big.Int `json:"tx_fee"`
}

type BlockEconomicState struct {
Issuance BlockIssuance `json:"issuance"`
MinerReward MinerReward `json:"miner_reward"`
TxsFee *big.Int `json:"txs_fee"`
FinalizedAt Hash `json:"finalized_at"`
}

type BlockIssuance struct {
Primary *big.Int `json:"primary"`
Secondary *big.Int `json:"secondary"`
}

type MinerReward struct {
Primary *big.Int `json:"primary"`
Secondary *big.Int `json:"secondary"`
Committed *big.Int `json:"committed"`
Proposal *big.Int `json:"proposal"`
}

0 comments on commit 0edae6a

Please sign in to comment.