更新时间:2021-12-30 GMT+08:00
分享

解锁跨链资产(unlockAccount)

在跨链资产交换即将完成时,需要解锁跨链资产交换中涉及的资产。可将上述逻辑封装至一个方法中,便于后续在其他智能合约方法(主要是commitSend、commitRecv、rollbackSend与rollbackRecv)中调用:

/*
 * unlockAccount will delete the account's lock from the blockchain
 * @Param account: The name of the account whose lock will be unlocked
 * @Param txID: The id of this transaction that transfer units of one account to another account
 */
func unlockAccount(stub shim.ChaincodeStubInterface, txID string, account string) error {
	// get account lock state with account lock's key
	accountLockKey := account + lockSuffix
	accountLockBytes, err := stub.GetState(accountLockKey)
	if err != nil {
	        return fmt.Errorf("failed to get accountLock state: %v", err)
	}
	if accountLockBytes == nil {
		// if the account lock state does not exist, no need to execute unlock
		return nil
	} else {
		accountLock := &AccountLock{}
		err = json.Unmarshal(accountLockBytes, &accountLock)
		if err != nil {
			return fmt.Errorf("failed to unmarshal accountLockBytes: %v", err)
		}
		if accountLock.CrossTXID != txID {
			return fmt.Errorf("not the owner of the lock")
		}
		err = stub.DelState(accountLockKey)
		if err != nil {
			return fmt.Errorf("failed to delete state: %v", err)
		}
	}
	return nil
}

相关文档