When to call getAssetRegistry to update assets (an

2019-03-01 12:42发布

问题:

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..