更新时间:2021-12-30 GMT+08:00
回滚跨链资产(rollback)
在跨链资产交换的过程中如遇到异常情况,需要读取跨链资产对应数据锁中的PreValue,并根据该值回滚跨链资产交换中涉及资产已发生的变化。可将上述逻辑封装至一个方法中,便于后续在其他智能合约方法(主要是rollbackSend与rollbackRecv)中调用:
/* * rollback function will recover the account balance to the previous status * @Param account: The name of the account whose balance will be rollback * @Param txID: The id of this transaction that transfer units of one account to another account */ func rollback(stub shim.ChaincodeStubInterface, txID string, account string) error { // get server side's local account lock(with preValue inside) accountLockKey := account + lockSuffix accountLockBytes, err := stub.GetState(accountLockKey) if err != nil { return fmt.Errorf("failed to get accountLock state: %v", err) } if accountLockBytes == nil { return nil } accountLock := AccountLock{} err = json.Unmarshal(accountLockBytes, &accountLock) if err != nil { return fmt.Errorf("failed to unmarshal accountLockBytes: %v", err) } // recover the balance of server side's local account to the previous balance err = stub.PutState(account, []byte(accountLock.PreValue)) if err != nil { return fmt.Errorf("failed to put state of preValue account: %v", err) } // unlock server side's local account err = unlockAccount(stub, txID, account) if err != nil { return fmt.Errorf("failed to unlock in rollbackRev: %v", err) } return nil }
父主题: 跨链智能合约方法示例