Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Update send function of Interoperability method #7562

Closed
Tracked by #7211
ishantiw opened this issue Sep 22, 2022 · 0 comments · Fixed by #7682
Closed
Tracked by #7211

Update send function of Interoperability method #7562

ishantiw opened this issue Sep 22, 2022 · 0 comments · Fixed by #7682
Assignees
Labels
framework/module/interoperability Interoperability module
Milestone

Comments

@ishantiw
Copy link
Contributor

ishantiw commented Sep 22, 2022

Description

The send function is used to create and add a message to the outbox of a partner chain.

def sendInternal(sendingAddress: Address,
    module: Module,
    crossChainCommand: CrossChainCommand,
    receivingChainID: ChainID,
    fee: uint64,
    status: uint32,
    params: object
) -> None:

    # Not possible to send messages to the own chain.
    if receivingChainID == ownChainAccount.chainID:
        emitPersistentEvent(
            module = MODULE_NAME_INTEROPERABILITY,
            name = EVENT_NAME_CCM_SENT_FAILED,
            data = {"ccm": ccm, "code": CCM_SENT_FAILED_CODE_INVALID_RECEIVING_CHAIN},
            topics = []
        )
        raise Exception("Sending chain cannot be the receiving chain.")

    ccm = {
        "nonce": ownChainAccount.nonce,
        "module": module,
        "crossChainCommand": crossChainCommand,
        "sendingChainID": ownChainAccount.chainID,
        "receivingChainID": receivingChainID,
        "fee": fee,
        "status": status,
        "params": EMPTY_BYTES
    }

    try:
        paramsSchema = JSON schema associated with (module, crossChainCommand)
        ccm.params = encode(paramsSchema, params)
    except Exception as e:
        emitPersistentEvent(
            module = MODULE_NAME_INTEROPERABILITY,
            name = EVENT_NAME_CCM_SENT_FAILED,
            data = {"ccm": ccm, "code": CCM_SENT_FAILED_CODE_INVALID_PARAMS},
            topics = []
        )
        raise e

    # Validate ccm size.
    try:
        validateFormat(ccm)
    except Exception as e:
        ccm.params = EMPTY_BYTES
        emitPersistentEvent(
            module = MODULE_NAME_INTEROPERABILITY,
            name = EVENT_NAME_CCM_SENT_FAILED,
            data = {"ccm": ccm, "code": CCM_SENT_FAILED_CODE_INVALID_FORMAT},
            topics = []
        )
        raise e

    # From now on, we can assume that the ccm is valid.

    # receivingChainID must correspond to a live chain.
    if not isLive(receivingChainID):
        ccm.params = EMPTY_BYTES
        emitPersistentEvent(
            module = MODULE_NAME_INTEROPERABILITY,
            name = EVENT_NAME_CCM_SENT_FAILED,
            data = {"ccm": ccm, "code": CCM_SENT_FAILED_CODE_CHANNEL_UNAVAILABLE},
            topics = []
        )
        raise Exception('Receiving chain is not live.')

    # Processing on the mainchain.
    if ownChainAccount.chainID == CHAIN_ID_MAINCHAIN:
        partnerChainID = receivingChainID
    # Processing on a sidechain.
    else:
        # Check for direct channel.
        if chainAccount(receivingChainID) does not exist:
            partnerChainID = CHAIN_ID_MAINCHAIN
        else:
            partnerChainID = receivingChainID

    # partnerChainID must correspond to an active chain (in this case, not registered).
    if not chainAccount(partnerChainID).status == CHAIN_STATUS_ACTIVE:
        ccm.params = EMPTY_BYTES
        emitPersistentEvent(
            module = MODULE_NAME_INTEROPERABILITY,
            name = EVENT_NAME_CCM_SENT_FAILED,
            data = {"ccm": ccm, "code": CCM_SENT_FAILED_CODE_CHANNEL_UNAVAILABLE},
            topics = []
        )
        raise Exception('Channel is not active.')

    # Pay message fee.
    if fee > 0:
        try:
            Token.payMessageFee(sendingAddress, fee, ccm.receivingChainID)
        except Exception as e:
            ccm.params = EMPTY_BYTES
            emitPersistentEvent(
                module = MODULE_NAME_INTEROPERABILITY,
                name = EVENT_NAME_CCM_SENT_FAILED,
                data = {"ccm": ccm, "code": CCM_SENT_FAILED_CODE_MESSAGE_FEE_EXCEPTION},
                topics = []
            )
            raise e

    ccmID = sha256(encode(crossChainMessageSchema, ccm))
    addToOutbox(partnerChainID, ccm)
    ownChainAccount.nonce += 1

    # Emit CCM Sent Event.
    emitEvent(
        module = MODULE_NAME_INTEROPERABILITY,
        name = EVENT_NAME_CCM_SENT_SUCCESS,
        data = {"ccmID": ccmID},
        topics = [ccm.sendingChainID, ccm.receivingChainID, ccmID]
    )

Acceptance Criteria

  • Should pass its unit test

Additional Information

@ishantiw ishantiw added the framework/module/interoperability Interoperability module label Sep 22, 2022
@shuse2 shuse2 added this to the Sprint 79 milestone Sep 26, 2022
@shuse2 shuse2 modified the milestones: Sprint 79, Sprint 80 Oct 10, 2022
@ishantiw ishantiw self-assigned this Oct 23, 2022
@ishantiw ishantiw linked a pull request Oct 23, 2022 that will close this issue
@shuse2 shuse2 modified the milestones: Sprint 80, Sprint 81 Oct 24, 2022
ishantiw added a commit that referenced this issue Oct 28, 2022
### What was the problem?

This PR resolves #7562

### How was it solved?

- Update send function based on updated LIP https://github.com/LiskHQ/lips/blob/main/proposals/lip-0045.md#sendinternal

### How was it tested?

`npm run test`
ishantiw added a commit that referenced this issue Nov 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
framework/module/interoperability Interoperability module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants