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 <execvy@gmail.com>
  • Loading branch information
eval-exec committed Dec 3, 2024
1 parent 5f52557 commit e09d93d
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 32 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
91 changes: 70 additions & 21 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ 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)
Expand All @@ -64,7 +64,7 @@ type Client interface {
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,24 @@ type Client interface {

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

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

GenerateBlockWithTemplate(ctx context.Context) (*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)

DryRunTransaction(ctx context.Context, tx types.Transaction) (types.DryRunTransactionResult, error)

Check failure on line 179 in rpc/client.go

View workflow job for this annotation

GitHub Actions / test

duplicate method DryRunTransaction

Check failure on line 179 in rpc/client.go

View workflow job for this annotation

GitHub Actions / test

duplicate method DryRunTransaction

Check failure on line 179 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

duplicate method DryRunTransaction

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 +376,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 +409,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 +501,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 +537,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 +759,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 +775,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
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
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
34 changes: 34 additions & 0 deletions types/stats.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
package types

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

// pub struct Alert {
// /// The identifier of the alert. Clients use id to filter duplicated alerts.
// pub id: AlertId,
// /// Cancel a previous sent alert.
// pub cancel: AlertId,
// /// Optionally set the minimal version of the target clients.
// ///
// /// See [Semantic Version](https://semver.org/) about how to specify a version.
// pub min_version: Option<String>,
// /// Optionally set the maximal version of the target clients.
// ///
// /// See [Semantic Version](https://semver.org/) about how to specify a version.
// pub max_version: Option<String>,
// /// Alerts are sorted by priority, highest first.
// pub priority: AlertPriority,
// /// The alert is expired after this timestamp.
// pub notice_until: Timestamp,
// /// Alert message.
// pub message: String,
// /// The list of required signatures.
// pub signatures: Vec<JsonBytes>,
// }
type Alert struct {
Id uint32 `json:"id"`
Cancel uint32 `json:"cancel"`
MinVersion *string `json:"min_version"`
MaxVersion *string `json:"max_version"`
Priority uint32 `json:"priority"`
NoticeUntil uint64 `json:"notice_until"`
Message string `json:"message"`
Signatures []json.RawMessage `json:"signatures"`
}

type AlertMessage struct {
Id uint32 `json:"id"`
Expand Down

0 comments on commit e09d93d

Please sign in to comment.