Skip to content

Commit

Permalink
Merge #3724
Browse files Browse the repository at this point in the history
3724: feat: adding RPC: `get_transaction_and_witness_proof` & `verify_transaction_and_witness_proof` r=quake a=code-monad

<!--
Thank you for contributing to nervosnetwork/ckb!

If you haven't already, please read [CONTRIBUTING](https://github.com/nervosnetwork/ckb/blob/develop/CONTRIBUTING.md) document.

If you're unsure about anything, just ask; somebody should be along to answer within a day or two.

PR Title Format:
1. module [, module2, module3]: what's changed
2. *: what's changed
-->

### What problem does this PR solve?

Issue Number: close #3628 <!-- REMOVE this line if no issue to close -->

Problem Summary:

### What is changed and how it works?


What's Changed:

- Introduced two new RPC method called `get_transaction_and_witness_proof ` & `verify_transaction_and_witness_proof`
#### `get_transaction_and_witne_proof `

This methods accepts the same params as `get_transaction_proof`, and returns a similar structure `TransactionAndWitnessProof`

```rust
struct TransactionAndWitnessProof {
    /// Block hash
    pub block_hash: H256,
    /// Merkle proof of all transactions' hash
    pub transactions_proof: MerkleProof,
    /// Merkle proof of transactions' witnesses
    pub witnesses_proof: MerkleProof,
}
```

#### `verify_transaction_and_witness_proof`

When we called with same valid param of `get_transaction_and_witness_proof ` & `get_transaction_proof`, use their result as param of `verify_transaction_and_witness_proof` & `verify_transaction_proof`, the final result should be the same(the hash of the transaction this proof pointed to)

### Check List <!--REMOVE the items that are not applicable-->

Tests <!-- At least one of them must be included. -->

- Unit test
- Integration test
- Manual test (add detailed scripts or steps below)
- No code ci-runs-only: [ quick_checks,linters ]

### Release note <!-- Choose from None, Title Only and Note. Bugfixes or new features need a release note. -->

```release-note
Note: Add a note under the PR title in the release note.
```



Co-authored-by: Code Monad <codemonad@cryptape.com>
  • Loading branch information
bors[bot] and Code Monad authored Nov 28, 2022
2 parents c51f891 + 5b551fb commit dc48d92
Show file tree
Hide file tree
Showing 4 changed files with 431 additions and 73 deletions.
127 changes: 127 additions & 0 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ The crate `ckb-rpc`'s minimum supported rustc version is 1.61.0.
* [Method `get_block_economic_state`](#method-get_block_economic_state)
* [Method `get_transaction_proof`](#method-get_transaction_proof)
* [Method `verify_transaction_proof`](#method-verify_transaction_proof)
* [Method `get_transaction_and_witness_proof`](#method-get_transaction_and_witness_proof)
* [Method `verify_transaction_and_witness_proof`](#method-verify_transaction_and_witness_proof)
* [Method `get_fork_block`](#method-get_fork_block)
* [Method `get_consensus`](#method-get_consensus)
* [Method `get_block_median_time`](#method-get_block_median_time)
Expand Down Expand Up @@ -172,6 +174,7 @@ The crate `ckb-rpc`'s minimum supported rustc version is 1.61.0.
* [Type `SyncState`](#type-syncstate)
* [Type `Timestamp`](#type-timestamp)
* [Type `Transaction`](#type-transaction)
* [Type `TransactionAndWitnessProof`](#type-transactionandwitnessproof)
* [Type `TransactionProof`](#type-transactionproof)
* [Type `TransactionTemplate`](#type-transactiontemplate)
* [Type `TransactionView`](#type-transactionview)
Expand Down Expand Up @@ -1376,6 +1379,115 @@ Response
```


#### Method `get_transaction_and_witness_proof`
* `get_transaction_and_witness_proof(tx_hashes, block_hash)`
* `tx_hashes`: `Array<` [`H256`](#type-h256) `>`
* `block_hash`: [`H256`](#type-h256) `|` `null`
* result: [`TransactionAndWitnessProof`](#type-transactionandwitnessproof)

Returns a Merkle proof of transactions’ witness included in a block.

###### Params

* `tx_hashes` - Transaction hashes, all transactions must be in the same block

* `block_hash` - An optional parameter, if specified, looks for transactions in the block with this hash

###### Examples

Request


```
{
"id": 42,
"jsonrpc": "2.0",
"method": "get_transaction_and_witness_proof",
"params": [
[ "0xa4037a893eb48e18ed4ef61034ce26eba9c585f15c9cee102ae58505565eccc3" ]
]
}
```


Response


```
{
"jsonrpc": "2.0",
"result": {
"block_hash": "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed",
"transactions_proof": {
"indices": [ "0x0" ],
"lemmas": []
},
"witnesses_proof": {
"indices": [
"0x0"
],
"lemmas": []
}
},
"id": 42
}
```


#### Method `verify_transaction_and_witness_proof`
* `verify_transaction_and_witness_proof(tx_proof)`
* `tx_proof`: [`TransactionAndWitnessProof`](#type-transactionandwitnessproof)
* result: `Array<` [`H256`](#type-h256) `>`

Verifies that a proof points to transactions in a block, returning the transaction hashes it commits to.

###### Parameters

* `tx_proof` - proof generated by [`get_transaction_and_witness_proof`](#method-get_transaction_and_witness_proof).

###### Examples

Request


```
{
"id": 42,
"jsonrpc": "2.0",
"method": "verify_transaction_and_witness_proof",
"params": [
{
"block_hash": "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed",
"transactions_proof": {
"indices": [ "0x0" ],
"lemmas": []
},
"witnesses_proof": {
"indices": [
"0x0"
],
"lemmas": []
}
}
]
}
```


Response


```
{
"id": 42,
"jsonrpc": "2.0",
"result": [
"0xa4037a893eb48e18ed4ef61034ce26eba9c585f15c9cee102ae58505565eccc3"
]
}
```


#### Method `get_fork_block`
* `get_fork_block(block_hash, verbosity)`
* `block_hash`: [`H256`](#type-h256)
Expand Down Expand Up @@ -6574,6 +6686,21 @@ Refer to RFC [CKB Transaction Structure](https://github.com/nervosnetwork/rfcs/b
For example, the bundled secp256k1 lock script requires storing the signature in `witnesses`.


### Type `TransactionAndWitnessProof`

Merkle proof for transactions’ witnesses in a block.

#### Fields

`TransactionAndWitnessProof` is a JSON object with the following fields.

* `block_hash`: [`H256`](#type-h256) - Block hash

* `transactions_proof`: [`MerkleProof`](#type-merkleproof) - Merkle proof of all transactions’ hash

* `witnesses_proof`: [`MerkleProof`](#type-merkleproof) - Merkle proof of transactions’ witnesses


### Type `TransactionProof`

Merkle proof for transactions in a block.
Expand Down
Loading

0 comments on commit dc48d92

Please sign in to comment.