When to call getAssetRegistry to update assets (and the participants equivalent)

ぃ、小莉子 提交于 2019-12-31 04:38:06

问题


I'm able to write some simplistic smart contracts using my composer development environment but am confused about when to persist assets and participants into the registry.

I've read the docs on composer-runtime.AssetRegistry and the getAssetRegistry function to return an asset registry object and to perform updates but am still not clear which assets/partipants to update.

Here's an example (may not be fully working):

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
}

If I write a transaction processor function to perform an account transaction (e.g. a withdrawal or deposit) such as this:

/**
 * 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);
    });
}

Am I right in assuming that only the BankAccount asset needs to be updated? (because the balance variable in the BankAccount asset has been updated)

Would I also need to update the Bank and Trader participants as well, since the Trader participant was part of the transaction AccountTrx and the Bank participant is linked to from the BankAccount asset? I don't see that anything has changed in either the Trader participant or BankAccount asset.


回答1:


you shouldn't need to. The Asset account has a relationship yes, and you're calling the right AssetRegistry. One assumes you're passing in an amount in the first place when calling the txn using POST or otherwise. What do you see for an updated asset (BankAccount balance)? Why not use console.log() to check..



来源:https://stackoverflow.com/questions/45428189/when-to-call-getassetregistry-to-update-assets-and-the-participants-equivalent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!