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

Update apply function of CCM execution process #7600

Closed
Tracked by #7211
ishantiw opened this issue Sep 30, 2022 · 0 comments
Closed
Tracked by #7211

Update apply function of CCM execution process #7600

ishantiw opened this issue Sep 30, 2022 · 0 comments
Assignees
Labels
framework/module/interoperability Interoperability module
Milestone

Comments

@ishantiw
Copy link
Contributor

ishantiw commented Sep 30, 2022

Description

Update apply function of CCM execution process based on LIP0049 update

def apply(ccu: Transaction, ccm: CCM) -> None:
    # Calculate CCM ID, used later in events.
    ccmID = sha256(encode(crossChainMessageSchema, ccm))
    try:
        if not isLive(ccm.sendingChainID):  
            raise Exception(f"Sending chain {ccm.sendingChainID} is not live.")
        # Modules can verify the CCM.
        # The Token module verifies the escrowed balance in the CCM sending chain for the message fee.
        for each module mdl for which verifyCrossChainMessage exists:
            mdl.verifyCrossChainMessage(ccu, ccm)
    except:    
        emitEvent(
            module = MODULE_NAME_INTEROPERABILITY,
            name = EVENT_NAME_CCM_PROCESSED,
            data = {"ccmID": ccmID, "result": CCM_PROCESSED_RESULT_DISCARDED, "code": CCM_PROCESSED_CODE_INVALID_CCM_VERIFY_CCM_EXCEPTION},
            topics = [ccm.sendingChainID, ccm.receivingChainID]
        )
        terminateChain(ccm.sendingChainID)
        # Notice that, since the sending chain has been terminated,
        # the verification of all future CCMs will fail.
        return

    if ccm.module is not supported:
        bounce(ccm, CCM_STATUS_CODE_MODULE_NOT_SUPPORTED, CCM_PROCESSED_CODE_MODULE_NOT_SUPPORTED)
        return
    elif crossChainCommand is not supported:
        bounce(ccm, CCM_STATUS_CODE_CROSS_CHAIN_COMMAND_NOT_SUPPORTED, CCM_PROCESSED_CODE_CROSS_CHAIN_COMMAND_NOT_SUPPORTED)
        return

    crossChainCommand = cross-chain command associated with (ccm.module, ccm.crossChainCommand)
    try:  
        crossChainCommand.verify(ccu, ccm)
    except:
        emitEvent(
            module = MODULE_NAME_INTEROPERABILITY,
            name = EVENT_NAME_CCM_PROCESSED,
            data = {"ccmID": ccmID, "result": CCM_PROCESSED_RESULT_DISCARDED, "code": CCM_PROCESSED_CODE_INVALID_CCM_VERIFY_EXCEPTION},
            topics = [ccm.sendingChainID, ccm.receivingChainID]
        )
        terminateChain(ccm.sendingChainID)
        return

    # Create a state snapshot.
    baseSnapshot = snapshot of the current state
    try:  
        # Call the beforeCrossChainCommandExecution functions from other modules.
        # For example, the Token module assigns the message fee to the CCU sender.
        for each module mdl for which beforeCrossChainCommandExecution exists:
            mdl.beforeCrossChainCommandExecution(ccu, ccm)
    except:
        revert state to baseSnapshot
        emitEvent(
            module = MODULE_NAME_INTEROPERABILITY,
            name = EVENT_NAME_CCM_PROCESSED,
            data = {"ccmID": ccmID, "result": CCM_PROCESSED_RESULT_DISCARDED, "code": CCM_PROCESSED_CODE_INVALID_CCM_BEFORE_CCC_EXECUTION_EXCEPTION},
            topics = [ccm.sendingChainID, ccm.receivingChainID]
        )
        terminateChain(ccm.sendingChainID)
        return
    # Create a state snapshot.
    executionSnapshot = snapshot of the current state
    try:
        # Execute the cross-chain command.
        crossChainCommand.execute(ccu, ccm)
        emitEvent(
            module = MODULE_NAME_INTEROPERABILITY,
            name = EVENT_NAME_CCM_PROCESSED,
            data = {"ccmID": ccmID, "result": CCM_PROCESSED_RESULT_APPLIED, "code": CCM_PROCESSED_CODE_SUCCESS},
            topics = [ccm.sendingChainID, ccm.receivingChainID]
        )
    except:
        revert state to executionSnapshot
        bounce(ccm, CCM_STATUS_CODE_FAILED_CCM, CCM_PROCESSED_CODE_FAILED_CCM)

    try:
        # Call the afterCrossChainCommandExecution functions from other modules.
        for each module mdl for which afterCrossChainCommandExecution exists:
            mdl.afterCrossChainCommandExecution(ccu, ccm)     
    except:
        revert state to baseSnapshot
        emitEvent(
            module = MODULE_NAME_INTEROPERABILITY,
            name = EVENT_NAME_CCM_PROCESSED,
            data = {"ccmID": ccmID, "result": CCM_PROCESSED_RESULT_DISCARDED, "code": CCM_PROCESSED_CODE_INVALID_CCM_AFTER_CCC_EXECUTION_EXCEPTION},
            topics = [ccm.sendingChainID, ccm.receivingChainID]
        )
        terminateChain(ccm.sendingChainID)  

Acceptance Criteria

  • Update unit tests related to apply function covering all the code paths including emitted events

Additional Information

@ishantiw ishantiw added the framework/module/interoperability Interoperability module label Sep 30, 2022
@shuse2 shuse2 added this to the Sprint 81 milestone Oct 24, 2022
@shuse2 shuse2 self-assigned this Nov 7, 2022
@shuse2 shuse2 modified the milestones: Sprint 81, Sprint 82 Nov 7, 2022
@shuse2 shuse2 closed this as completed Nov 18, 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

No branches or pull requests

2 participants