Skip to content

Commit

Permalink
Complete ckb-sdk-go missing rpc
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <[email protected]>
  • Loading branch information
eval-exec committed Dec 16, 2024
1 parent 5f52557 commit f049bd8
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 34 deletions.
14 changes: 9 additions & 5 deletions indexer/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package indexer

import (
"encoding/json"

"github.com/nervosnetwork/ckb-sdk-go/v2/types"
)

Expand All @@ -24,11 +26,13 @@ type SearchKey struct {
}

type Filter struct {
Script *types.Script `json:"script"`
ScriptLenRange *[2]uint64 `json:"script_len_range,omitempty"`
OutputDataLenRange *[2]uint64 `json:"output_data_len_range,omitempty"`
OutputCapacityRange *[2]uint64 `json:"output_capacity_range,omitempty"`
BlockRange *[2]uint64 `json:"block_range,omitempty"`
Script *types.Script `json:"script"`
ScriptLenRange *[2]uint64 `json:"script_len_range,omitempty"`
OutputData *json.RawMessage `json:"output_data,omitempty"`
OutputDataFilterMode *types.ScriptSearchMode `json:"output_data_filter_mode,omitempty"`
OutputDataLenRange *[2]uint64 `json:"output_data_len_range,omitempty"`
OutputCapacityRange *[2]uint64 `json:"output_capacity_range,omitempty"`
BlockRange *[2]uint64 `json:"block_range,omitempty"`
}

type LiveCell struct {
Expand Down
153 changes: 131 additions & 22 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ type Client interface {
GetPackedBlockWithCycles(ctx context.Context, hash types.Hash) (*types.BlockWithCycles, error)

// GetHeader returns the information about a block header by hash.
GetHeader(ctx context.Context, hash types.Hash) (*types.Header, error)
GetHeader(ctx context.Context, hash types.Hash, verbosity *uint32) (*types.Header, error)

// GetHeaderVerbosity0 returns the information about a block header by hash, but with verbosity specified to 0.
GetPackedHeader(ctx context.Context, hash types.Hash) (*types.Header, error)

// GetHeaderByNumber returns the information about a block header by block number.
GetHeaderByNumber(ctx context.Context, number uint64) (*types.Header, error)
GetHeaderByNumber(ctx context.Context, number uint64, verbosity *uint32) (*types.Header, error)

// GetHeaderByNumberVerbosity0 returns the information about a block header by block number.
GetPackedHeaderByNumber(ctx context.Context, number uint64) (*types.Header, error)
GetPackedHeaderByNumber(ctx context.Context, number uint64, verbosity *uint32) (*types.Header, error)
// GetLiveCell returns the information about a cell by out_point if it is live.
// If second with_data argument set to true, will return cell data and data_hash if it is live.
GetLiveCell(ctx context.Context, outPoint *types.OutPoint, withData bool, includeTxPool *bool) (*types.CellWithStatus, error)

// GetTransaction returns the information about a transaction requested by transaction hash.
GetTransaction(ctx context.Context, hash types.Hash, onlyCommitted *bool) (*types.TransactionWithStatus, error)
GetTransaction(ctx context.Context, hash types.Hash, verbosity *uint32, onlyCommitted *bool) (*types.TransactionWithStatus, 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)
Expand All @@ -82,10 +82,10 @@ type Client interface {
VerifyTransactionAndWitnessProof(ctx context.Context, proof *types.TransactionAndWitnessProof) ([]*types.Hash, error)

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

// GetBlockByNumber get block by number
GetBlockByNumberWithCycles(ctx context.Context, number uint64) (*types.BlockWithCycles, error)
GetBlockByNumberWithCycles(ctx context.Context, number uint64, verbosity *uint32) (*types.BlockWithCycles, error)

// GetForkBlock The RPC returns a fork block or null. When the RPC returns a block, the block hash must equal to the parameter block_hash.
GetForkBlock(ctx context.Context, blockHash types.Hash) (*types.Block, error)
Expand Down Expand Up @@ -164,8 +164,22 @@ type Client interface {

GetPoolTxDetailInfo(ctx context.Context, hash types.Hash) (*types.PoolTxDetailInfo, error)

GenerateBlock(ctx context.Context) (*types.Hash, error)

GenerateBlockWithTemplate(ctx context.Context, block_template types.BlockTemplate) (*types.Hash, error)

Truncate(ctx context.Context, target types.Hash) error

RemoveTransaction(ctx context.Context, tx_hash types.Hash) (bool, error)

SendAlert(ctx context.Context, alert types.AlertMessage) error

GetBlockTemplate(ctx context.Context) (types.BlockTemplate, error)

TxPoolReady(ctx context.Context) (bool, error)

// GetRawTxPool Returns all transaction ids in tx pool as a json array of string transaction ids.
GetRawTxPool(ctx context.Context) (*types.RawTxPool, error)
GetRawTxPool(ctx context.Context, verbose *bool) (*types.RawTxPool, error)

// ClearTxPool Removes all transactions from the transaction pool.
ClearTxPool(ctx context.Context) error
Expand Down Expand Up @@ -360,9 +374,16 @@ func (cli *client) GetPackedBlockWithCycles(ctx context.Context, hash types.Hash
return result, nil
}

func (cli *client) GetHeader(ctx context.Context, hash types.Hash) (*types.Header, error) {
func (cli *client) GetHeader(ctx context.Context, hash types.Hash, verbosity *uint32) (*types.Header, error) {
var result types.Header
err := cli.c.CallContext(ctx, &result, "get_header", hash)

// if verbosityi is nil, let it be 1
if verbosity == nil {
defaultVerbosity := uint32(1)
verbosity = &defaultVerbosity
}

err := cli.c.CallContext(ctx, &result, "get_header", hash, verbosity)
if err != nil {
return nil, err
}
Expand All @@ -386,16 +407,23 @@ func (cli *client) GetPackedHeader(ctx context.Context, hash types.Hash) (*types
return types.UnpackHeader(rawHeader), nil
}

func (cli *client) GetHeaderByNumber(ctx context.Context, number uint64) (*types.Header, error) {
func (cli *client) GetHeaderByNumber(ctx context.Context, number uint64, verbosity *uint32) (*types.Header, error) {
var result types.Header
err := cli.c.CallContext(ctx, &result, "get_header_by_number", hexutil.Uint64(number))

// if verbosityi is nil, let it be 1
if verbosity == nil {
defaultVerbosity := uint32(1)
verbosity = &defaultVerbosity
}

err := cli.c.CallContext(ctx, &result, "get_header_by_number", hexutil.Uint64(number), verbosity)
if err != nil {
return nil, err
}
return &result, nil
}

func (cli *client) GetPackedHeaderByNumber(ctx context.Context, number uint64) (*types.Header, error) {
func (cli *client) GetPackedHeaderByNumber(ctx context.Context, number uint64, verbosity *uint32) (*types.Header, error) {
var headerHash string
err := cli.c.CallContext(ctx, &headerHash, "get_header_by_number", hexutil.Uint64(number), hexutil.Uint64(0))
if err != nil {
Expand Down Expand Up @@ -471,23 +499,33 @@ func (cli *client) GetLiveCell(ctx context.Context, point *types.OutPoint, withD
return &result, err
}

func (cli *client) GetTransaction(ctx context.Context, hash types.Hash, onlyCommitted *bool) (*types.TransactionWithStatus, error) {
func (cli *client) GetTransaction(ctx context.Context, hash types.Hash, verbosity *uint32, onlyCommitted *bool) (*types.TransactionWithStatus, error) {
var result types.TransactionWithStatus
var err error
// if verbosityi is nil, let it be 2
if verbosity == nil {
defaultVerbosity := uint32(2)
verbosity = &defaultVerbosity
}

if onlyCommitted == nil {
err = cli.c.CallContext(ctx, &result, "get_transaction", hash)
err = cli.c.CallContext(ctx, &result, "get_transaction", hash, *verbosity)
} else {
err = cli.c.CallContext(ctx, &result, "get_transaction", hash, *onlyCommitted)
err = cli.c.CallContext(ctx, &result, "get_transaction", hash, *verbosity, *onlyCommitted)
}
if err != nil {
return nil, err
}
return &result, nil
}

func (cli *client) GetBlockByNumber(ctx context.Context, number uint64) (*types.Block, error) {
func (cli *client) GetBlockByNumber(ctx context.Context, number uint64, verbosity *uint32) (*types.Block, error) {
var result types.Block
err := cli.c.CallContext(ctx, &result, "get_block_by_number", hexutil.Uint64(number))
if verbosity == nil {
defaultVerbosity := uint32(2)
verbosity = &defaultVerbosity
}
err := cli.c.CallContext(ctx, &result, "get_block_by_number", hexutil.Uint64(number), verbosity)
if err != nil {
return nil, err
}
Expand All @@ -497,9 +535,13 @@ func (cli *client) GetBlockByNumber(ctx context.Context, number uint64) (*types.
return &result, nil
}

func (cli *client) GetBlockByNumberWithCycles(ctx context.Context, number uint64) (*types.BlockWithCycles, error) {
func (cli *client) GetBlockByNumberWithCycles(ctx context.Context, number uint64, verbosity *uint32) (*types.BlockWithCycles, error) {
var result types.BlockWithCycles
err := cli.c.CallContext(ctx, &result, "get_block_by_number", hexutil.Uint64(number), nil, true)
if verbosity == nil {
defaultVerbosity := uint32(2)
verbosity = &defaultVerbosity
}
err := cli.c.CallContext(ctx, &result, "get_block_by_number", hexutil.Uint64(number), verbosity, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -715,7 +757,7 @@ func (cli *client) TestTxPoolAccept(ctx context.Context, tx *types.Transaction)

func (cli *client) GetPoolTxDetailInfo(ctx context.Context, hash types.Hash) (*types.PoolTxDetailInfo, error) {
var result types.PoolTxDetailInfo
err := cli.c.CallContext(ctx, &result, "get_pool_tx_detail", hash)
err := cli.c.CallContext(ctx, &result, "get_pool_tx_detail_info", hash)
if err != nil {
return nil, err
}
Expand All @@ -731,9 +773,14 @@ func (cli *client) TxPoolInfo(ctx context.Context) (*types.TxPoolInfo, error) {
return &result, nil
}

func (cli *client) GetRawTxPool(ctx context.Context) (*types.RawTxPool, error) {
func (cli *client) GetRawTxPool(ctx context.Context, verbose *bool) (*types.RawTxPool, error) {

Check failure on line 776 in rpc/client.go

View workflow job for this annotation

GitHub Actions / test

previous declaration

Check failure on line 776 in rpc/client.go

View workflow job for this annotation

GitHub Actions / test

previous declaration
var txPool types.RawTxPool
err := cli.c.CallContext(ctx, &txPool, "get_raw_tx_pool")

if verbose == nil {
defaultVerbose := false
verbose = &defaultVerbose
}
err := cli.c.CallContext(ctx, &txPool, "get_raw_tx_pool", verbose)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -916,3 +963,65 @@ func (cli *client) GenerateEpochs(ctx context.Context, numEpochs uint64) (uint64
}
return uint64(result), nil
}

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

func (cli *client) GenerateBlockWithTemplate(ctx context.Context, block_template types.BlockTemplate) (*types.Hash, error) {
var result types.Hash
err := cli.c.CallContext(ctx, &result, "generate_block_with_template", block_template)
if err != nil {
return nil, err
}
return &result, nil
}

func (cli *client) Truncate(ctx context.Context, target types.Hash) error {
return cli.c.CallContext(ctx, nil, "truncate", target)
}

func (cli *client) RemoveTransaction(ctx context.Context, tx_hash types.Hash) (bool, error) {
var result bool
err := cli.c.CallContext(ctx, &result, "remove_transaction", tx_hash)
if err != nil {
return false, err
}
return result, nil
}

func (cli *client) SendAlert(ctx context.Context, alert types.AlertMessage) error {
return cli.c.CallContext(ctx, nil, "send_alert", alert)
}

func (cli *client) GetBlockTemplate(ctx context.Context) (types.BlockTemplate, error) {
var result types.BlockTemplate
err := cli.c.CallContext(ctx, &result, "get_block_template")
if err != nil {
return types.BlockTemplate{}, err
}
return result, nil
}

func (cli *client) TxPoolReady(ctx context.Context) (bool, error) {
var result bool
err := cli.c.CallContext(ctx, &result, "tx_pool_ready")
if err != nil {
return false, err
}
return result, nil
}

func (cli *client) GetRawTxPool(ctx context.Context, verbose *bool) (*types.RawTxPool, error) {

Check failure on line 1020 in rpc/client.go

View workflow job for this annotation

GitHub Actions / test

(*client).GetRawTxPool redeclared in this block

Check failure on line 1020 in rpc/client.go

View workflow job for this annotation

GitHub Actions / test

(*client).GetRawTxPool redeclared in this block

Check failure on line 1020 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

method client.GetRawTxPool already declared at rpc/client.go:776:20
var result types.RawTxPool
err := cli.c.CallContext(ctx, &result, "get_raw_tx_pool", verbose)
if err != nil {
return nil, err
}
return &result, nil
}
10 changes: 6 additions & 4 deletions types/chain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/json"
"math/big"

"github.com/nervosnetwork/ckb-sdk-go/v2/crypto/blake2b"
Expand Down Expand Up @@ -179,10 +180,11 @@ type UncleBlock struct {
}

type Block struct {
Header *Header `json:"header"`
Proposals []string `json:"proposals"`
Transactions []*Transaction `json:"transactions"`
Uncles []*UncleBlock `json:"uncles"`
Header *Header `json:"header"`
Proposals []string `json:"proposals"`
Transactions []*Transaction `json:"transactions"`
Uncles []*UncleBlock `json:"uncles"`
Extension *json.RawMessage `json:"extension"`
}

type PackedBlock struct {
Expand Down
5 changes: 3 additions & 2 deletions types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ const (
ScriptTypeLock ScriptType = "lock"
ScriptTypeType ScriptType = "type"

ScriptSearchModePrefix ScriptSearchMode = "prefix"
ScriptSearchModeExact ScriptSearchMode = "exact"
ScriptSearchModePrefix ScriptSearchMode = "prefix"
ScriptSearchModeExact ScriptSearchMode = "exact"
ScriptSearchModePartial ScriptSearchMode = "partial"
)

var (
Expand Down
3 changes: 2 additions & 1 deletion types/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ func TestJsonBlock(t *testing.T) {
]
}
],
"uncles": []
"uncles": [],
"extension": []
}`)
var v Block
json.Unmarshal(jsonText1, &v)
Expand Down
59 changes: 59 additions & 0 deletions types/pool.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
package types

import "encoding/json"

// /// The uncle block template of the new block for miners.
// #[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, Debug, JsonSchema)]
// pub struct UncleTemplate {
// /// The uncle block hash.
// pub hash: H256,
// /// Whether miners must include this uncle in the submit block.
// pub required: bool,
// /// The proposals of the uncle block.
// ///
// /// Miners must keep this unchanged when including this uncle in the new block.
// pub proposals: Vec<ProposalShortId>,
// /// The header of the uncle block.
// ///
// /// Miners must keep this unchanged when including this uncle in the new block.
// pub header: Header,
// }

type UncleTemplate struct {
Hash Hash `json:"hash"`
Required bool `json:"required"`
Proposals []string `json:"proposals"`
Header *Header `json:"header"`
}

type BlockTemplate struct {
Version uint32 `json:"version"`
CompactTarget uint32 `json:"compact_target"`
CurrentTime uint64 `json:"current_time"`
Number uint64 `json:"number"`
Epoch uint64 `json:"epoch"`
ParentHash Hash `json:"parent_hash"`
CyclesLimit uint64 `json:"cycles_limit"`
BytesLimit uint64 `json:"bytes_limit"`
UnclesCountLimit uint64 `json:"uncles_count_limit"`
Uncles []UncleTemplate `json:"uncles"`
Transactions []TransactionTemplate `json:"transactions"`
Proposals []string `json:"proposals"`
Cellbase CellbaseTemplate `json:"cellbase"`
WorkId uint64 `json:"work_id"`
Dao Hash `json:"dao"`
Extension *json.RawMessage `json:"extension"`
}

type CellbaseTemplate struct {
Hash Hash `json:"hash"`
Cycles *uint64 `json:"cycles"`
Data Transaction `json:"data"`
}

type TransactionTemplate struct {
Hash Hash `json:"hash"`
Required bool `json:"required"`
Cycles *uint64 `json:"cycles"`
Depends *[]uint64 `json:"depends"`
Data Transaction `json:"data"`
}

type TxPoolInfo struct {
LastTxsUpdatedAt uint64 `json:"last_txs_updated_at"`
MaxTxPoolSize uint64 `json:"max_tx_pool_size"`
Expand Down
Loading

0 comments on commit f049bd8

Please sign in to comment.