From a7f686656ae95aae616c8e1e6f969d2ea15360bd Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Tue, 3 Dec 2024 18:35:40 +0800 Subject: [PATCH] Complete ckb-sdk-go missing rpc Signed-off-by: Eval EXEC --- dao/helper.go | 8 +-- indexer/types.go | 14 +++-- rpc/client.go | 144 ++++++++++++++++++++++++++++++++++++++------- types/chain.go | 10 ++-- types/common.go | 5 +- types/json_test.go | 3 +- types/pool.go | 59 +++++++++++++++++++ types/stats.go | 34 +++++++++++ 8 files changed, 239 insertions(+), 38 deletions(-) diff --git a/dao/helper.go b/dao/helper.go index 0bdf1141..ba22adf7 100644 --- a/dao/helper.go +++ b/dao/helper.go @@ -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 } @@ -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 } @@ -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 } diff --git a/indexer/types.go b/indexer/types.go index dff0da6d..df93391b 100644 --- a/indexer/types.go +++ b/indexer/types.go @@ -1,6 +1,8 @@ package indexer import ( + "encoding/json" + "github.com/nervosnetwork/ckb-sdk-go/v2/types" ) @@ -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 { diff --git a/rpc/client.go b/rpc/client.go index 04b1e18d..c8443fa0 100644 --- a/rpc/client.go +++ b/rpc/client.go @@ -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) @@ -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) @@ -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 @@ -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 } @@ -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 { @@ -471,13 +499,19 @@ 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 @@ -485,9 +519,13 @@ func (cli *client) GetTransaction(ctx context.Context, hash types.Hash, onlyComm 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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 +} diff --git a/types/chain.go b/types/chain.go index c5e0612e..59908074 100644 --- a/types/chain.go +++ b/types/chain.go @@ -1,6 +1,7 @@ package types import ( + "encoding/json" "math/big" "github.com/nervosnetwork/ckb-sdk-go/v2/crypto/blake2b" @@ -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 { diff --git a/types/common.go b/types/common.go index 496eb34f..462d24d6 100644 --- a/types/common.go +++ b/types/common.go @@ -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 ( diff --git a/types/json_test.go b/types/json_test.go index 824fa4e2..6a061732 100644 --- a/types/json_test.go +++ b/types/json_test.go @@ -473,7 +473,8 @@ func TestJsonBlock(t *testing.T) { ] } ], - "uncles": [] + "uncles": [], + "extension": [] }`) var v Block json.Unmarshal(jsonText1, &v) diff --git a/types/pool.go b/types/pool.go index 494920e4..8aed1ae0 100644 --- a/types/pool.go +++ b/types/pool.go @@ -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, +// /// 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"` diff --git a/types/stats.go b/types/stats.go index 05b4eee0..975b4bcd 100644 --- a/types/stats.go +++ b/types/stats.go @@ -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, +// /// 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, +// /// 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, +// } +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"`