darwinia-network / darwinia-messages-sol

Darwinia cross-chain messages gateway and protocol for EVM developers 💌
MIT License
29 stars 8 forks source link

Transactional solution sample for contracts using message #176

Closed hackfisher closed 1 year ago

hackfisher commented 2 years ago
Use case: lock_and_remote_issue (CBA Model based)

Source Chain

contract Backing // transferId = messageId
{
    // Incremental Merkle Tree  存在性证明
    // https://github.com/ConsenSys/deposit-sc-dafny/blob/master/wiki/background.md
    transfer_mapping (hash(messageId, messageDetail)) => bool;

    // TBD 不存在性证明
    unlock_for_failed_remote_issue_mapping (messageId) => bool;

    lock_and_remote_issue(..) {
        // ...

        // deposit a new item to transfer_mapping SMT.

    }

    unlock_for_failed_remote_issue(..) {
        1. Checking (messageId, messageDetail) is in transfer_mapping, and get messageDetail from it.  存在性证明

        2. Ensure messageId not in unlock_for_failed_remote_issue_mapping 不存在性证明

        3. unlock tokens according to infos in messageDetail

        4. Add messageId to unlock_for_failed_remote_issue_mapping
    }

}

------------------------

Target Chain

InboundLane

```rust
message_proof() {
   // verify
   // validate and predispatch such spec version weight and other pre-dispatches
   // dispatch (e.g. to some contract call)
}

contract Issuing { // Incremental Merkle Tree, 不存在性证明, messageId grows monotonically recieved_mesasges_mapping (messageId) => bool;

fn issue_from_remote(...) { // mint // ...

   recieved_mesasges_mapping[messageId] = true;

}

fn handle_failed_issue_from_remote(messageId) { // 1. Make sure messageId is not in recieved_mesasges_mapping. 不存在性证明 // 2. Make sure messageId is recieved by checking it in InboundLane (using precompile state storage).

   // 3. Sending a `unlock_for_failed_remote_issue` message to Backing on Source chain.

} }

hackfisher commented 2 years ago

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/4a9cc8b4918ef3736229a5cc5a310bdc17bf759f/contracts/utils/structs/BitMaps.sol

Only for auto incremental nonces.

hackfisher commented 2 years ago

// 2. Make sure messageId is recieved by checking it in InboundLane (using precompile state storage).

Use this:

https://github.com/darwinia-network/darwinia-messages-substrate/blob/main/primitives/messages/src/lib.rs#L171-L173

hujw77 commented 1 year ago

@wuminzhe any updates about issue?

wuminzhe commented 1 year ago

@wuminzhe any updates about issue?

We can close this issue now. It is used as a sample. @xiaoch05 has already implemented it.