axonweb3 / axon

Axon is a Layer 2 framework of CKB with native cross-chain and interoperability.
https://axonweb3.io
MIT License
65 stars 39 forks source link

feat: add ckb mbt proof verify precompile contract #1578

Closed KaoImin closed 10 months ago

KaoImin commented 11 months ago

What this PR does / why we need it?

This PR add ckb-mbt(aka merkle binary tree) proof verify precompile contract. The input struct is

#[derive(EthAbiCodec, EthAbiType, Clone, Debug, PartialEq, Eq)]
pub struct VerifyProofPayload {
    /// If the verify_type is 0, the leaves should be in the
    /// raw_transactions_root, otherwise in the witnesses_root.
    pub verify_type:           u8,
    pub transactions_root:     [u8; 32],
    pub witnesses_root:        [u8; 32],
    pub raw_transactions_root: [u8; 32],
    pub proof:                 Proof,
}

#[derive(EthAbiCodec, EthAbiType, Clone, Debug, PartialEq, Eq)]
pub struct Proof {
    pub indices: Vec<u32>,
    pub lemmas:  Vec<[u8; 32]>,
    pub leaves:  Vec<[u8; 32]>,
}

Because of the transactions_root in a CKB block header is consist of two layers:


                                  Transactions Root                        
                                        /   \                              
                                       /     \                             
                                      /       \                            
                                     /         \                           
                                    v           v                          
                  Raw Transactions Root          Witnesses Root            
                    /       |     \               |         \              
                   /        |      \              |          \             
                  /         |       \             |           \            
                 v          v        v            v            v           
            TxHash00    TxHash01   TxHash02   WitnessHash00   WitnessHash01

So apart from the transactions, witnesses root also needs. And it should be use ABI encode to bytes such as:

abi.encode(VerifyProofPayload(transactions_root, witnesses_root, raw_transactions_root, indices, lemmas, leaves))

What is the impact of this PR?

No Breaking Change

PR relation:

CI Settings
### **CI Usage** **Tip**: Check the CI you want to run below, and then comment `/run-ci`. **CI Switch** - [ ] Web3 Compatible Tests - [ ] OCT 1-5 And 12-15 - [ ] OCT 6-10 - [ ] OCT 11 - [ ] OCT 16-19 - [ ] v3 Core Tests ### **CI Description** | CI Name | Description | | ----------------------------------------- | ------------------------------------------------------------------------- | | *Web3 Compatible Test* | Test the Web3 compatibility of Axon | | *v3 Core Test* | Run the compatibility tests provided by Uniswap V3 | | *OCT 1-5 \| 6-10 \| 11 \| 12-15 \| 16-19* | Run the compatibility tests provided by OpenZeppelin |
jjyr commented 11 months ago

This contract only supports verify raw_transaction, but users may want to also verify the witness transaction(to ensure the witness part is valid on CKB). This contract must support merkle verify the witness_root.

I suggest to add another input field tx_verify_type: u8 to indicates wether we want to verify a raw_transaction or a witness_transaction.

If the tx_verify_type is 0, the merkle root must equals raw_transaction_root, if the tx_verify_type is 1 the merkle root must equals to witness_transaction_root.

KaoImin commented 11 months ago

This contract only supports verify raw_transaction, but users may want to also verify the witness transaction(to ensure the witness part is valid on CKB). This contract must support merkle verify the witness_root.

I suggest to add another input field tx_verify_type: u8 to indicates wether we want to verify a raw_transaction or a witness_transaction.

If the tx_verify_type is 0, the merkle root must equals raw_transaction_root, if the tx_verify_type is 1 the merkle root must equals to witness_transaction_root.

Sounds good.