何时调用getAssetRegistry来更新资产(和参与者等效)

时间:2017-08-01 02:51:54

标签: hyperledger hyperledger-fabric hyperledger-composer

我可以使用我的作曲家开发环境编写一些简单的智能合约,但对何时将资产和参与者持久存入注册表感到困惑。

我已阅读 composer-runtime.AssetRegistry getAssetRegistry 函数的文档,以返回资产注册表对象并执行更新,但仍不清楚哪些资产/ partipants要更新。

这是一个例子(可能不完全正常):

participant Trader identified by userID {
  o String userID
  o String firstName
  o String lastName
}    

participant Bank identified by bankID {
  o String bankID
  o String description
  --> BankAccount[] accounts optional
}

asset BankAccount identified by accountID {
  o String accountID
  o Double balance
  o AccountTrx[] transactions optional
  --> Trader owner
}

transaction AccountTrx {
  o Double amount
  o String operation
  --> BankAccount account
  --> Trader party
}

如果我编写交易处理器功能来执行帐户交易(例如提款或存款),例如:

/**
 * Perform a deposit or withdrawal from a bank account
 * @param {org.acme.auctionnetwork.AccountTrx} transaction
 * @transaction
 */

function execTrx(transaction) {   
    // initialize array of transactions if none exist

    if(transaction.account.transactions == null) {
        transaction.account.transactions = [];
    }

    // determine whether this is a deposit or withdrawal and execute accordingly

    if(transaction.operation == 'W') {
        transaction.account.balance -= transaction.amount;
    } else if(transaction.operation == 'D') {
        transaction.account.balance += transaction.amount;
    }

    // add the current transaction to the bank account's transaction history

    transaction.account.transactions.push(transaction);

    // update the registry (but which ones???)

    return getAssetRegistry('org.acme.auctionnetwork.BankAccount')
    .then(function(regBankAccount) {
        return regBankAccount.update(transaction.account);
    });
}

我是否正确地假设只需要更新BankAccount资产? (因为BankAccount资产中的余额变量已更新)

我是否还需要更新银行和交易员参与者,因为交易者参与者是交易AccountTrx的一部分,银行参与者是从BankAccount资产链接的?我没有看到Trader参与者或BankAccount资产发生任何变化。

1 个答案:

答案 0 :(得分:1)

你应该不需要。资产account有一个关系是,您正在调用正确的AssetRegistry。一个假设你在使用POST或其他方式调用txn时首先传入了一个数量。您对更新的资产(BankAccount余额)有何看法?为什么不使用console.log()来检查..