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 a7f6866
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 38 deletions.
8 changes: 4 additions & 4 deletions dao/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type DaoDepositCellInfo struct {

// GetDaoDepositCellInfo Get information for DAO cell deposited as outpoint and withdrawn in block of withdrawBlockHash
func (c *DaoHelper) GetDaoDepositCellInfo(outpoint *types.OutPoint, withdrawBlockHash types.Hash) (DaoDepositCellInfo, error) {
blockHeader, err := c.Client.GetHeader(context.Background(), withdrawBlockHash)
blockHeader, err := c.Client.GetHeader(context.Background(), withdrawBlockHash, nil)
if err != nil {
return DaoDepositCellInfo{}, err
}
Expand All @@ -33,7 +33,7 @@ func (c *DaoHelper) GetDaoDepositCellInfo(outpoint *types.OutPoint, withdrawBloc

// GetDaoDepositCellInfoWithWithdrawOutpoint Get information for DAO cell deposited as outpoint and withdrawn in block where the withdrawCellOutPoint is
func (c *DaoHelper) GetDaoDepositCellInfoWithWithdrawOutpoint(outpoint *types.OutPoint, withdrawCellOutPoint *types.OutPoint) (DaoDepositCellInfo, error) {
withdrawTransaction, err := c.Client.GetTransaction(context.Background(), withdrawCellOutPoint.TxHash, nil)
withdrawTransaction, err := c.Client.GetTransaction(context.Background(), withdrawCellOutPoint.TxHash, nil, nil)
if err != nil {
return DaoDepositCellInfo{}, err
}
Expand All @@ -51,11 +51,11 @@ func (c *DaoHelper) GetDaoDepositCellInfoByNow(outpoint *types.OutPoint) (DaoDep

// getDaoDepositCellInfo Get information for DAO cell deposited as outpoint and withdrawn in withdrawBlock
func (c *DaoHelper) getDaoDepositCellInfo(outpoint *types.OutPoint, withdrawBlockHeader *types.Header) (DaoDepositCellInfo, error) {
depositTransactionWithStatus, err := c.Client.GetTransaction(context.Background(), outpoint.TxHash, nil)
depositTransactionWithStatus, err := c.Client.GetTransaction(context.Background(), outpoint.TxHash, nil, nil)
if err != nil {
return DaoDepositCellInfo{}, err
}
depositBlockHeader, err := c.Client.GetHeader(context.Background(), *depositTransactionWithStatus.TxStatus.BlockHash)
depositBlockHeader, err := c.Client.GetHeader(context.Background(), *depositTransactionWithStatus.TxStatus.BlockHash, nil)
if err != nil {
return DaoDepositCellInfo{}, err
}
Expand Down
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
144 changes: 122 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) {
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,56 @@ 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
}
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
Loading

0 comments on commit a7f6866

Please sign in to comment.