处理Hyperledger composer中的相关对象

时间:2017-09-14 09:49:20

标签: hyperledger-composer

型号:

namespace org.acme.model

enum CustomerSegment {
o P
o E
}

asset Account identified by Account_No {
  o String Account_No
  --> Customer Account_Holder
  o String Account_Type
  o Integer Balance
}

participant Customer identified by Customer_ID {
  o String Customer_ID
  o String First_Name
  o CustomerSegment Customer_Segment
}

transaction UpdateCustomerSegment{
  --> Account A
  --> Customer C
}

transaction changeBalance{
  --> Account A
  o Integer newBalance
}

  event UCS_Event{
    -->Account A
    o String oldsegment
    o String newsegment
  }

event BalanceChangEvent{
  -->Account A
  o Integer oldbalance
  o Integer newbalance
}

脚本:

  /**
     * Place an order for a vehicle
     * @param {org.acme.model.UpdateCustomerSegment} ucs - the transaction that calculated Customer Segment
     * @transaction
     */

function UpdateCustomerSegment(ucs)
{
  var CustomerID=ucs.C.Customer_ID;
  var oldCS=ucs.C.Customer_Segment;

  if(ucs.A.Balance>3000)
    ucs.C.Customer_Segment="E"
  else
    ucs.C.Customer_Segment="P"

  var cust = getParticipantRegistry('org.acme.model.Customer')
  .then(function(participantRegistry){
  return participantRegistry.update(ucs.C);
  })

  .then(function(){
    //Emit Event
    var event_g= getFactory().newEvent('org.acme.model','UCS_Event');
    event_g.A=ucs.A;
    event_g.oldsegment=oldCS;
    event_g.newsegment=ucs.C.Customer_Segment
    emit(event_g);
  })

  return cust;
}

我需要做的是 - 仅将帐号传递给交易。 交易应该取得适当的客户 - 客户ID与账户的账户持有人相同,并更新客户的客户部分

我无法做到。它是否可行。我是新人,所以不确定。

在上述交易中,我通过了账号和客户ID

第二个问题 我们可以更新参与者和资产都在同一笔交易中?如果是的话。

第3个问题: 如何从另一个交易中调用一个交易。

1 个答案:

答案 0 :(得分:1)

您的模型看起来不错,现在您需要编写一个事务处理器函数,该函数订阅UpdateCustomerSegment事务并实现您的逻辑以更改客户的客户群,然后保留客户。

basic-sample-network包含一个类似的简单事务处理器:

/**
 * Sample transaction processor function.
 * @param {org.acme.sample.SampleTransaction} tx The sample transaction instance.
 * @transaction
 */
function sampleTransaction(tx) {

    // Save the old value of the asset.
    var oldValue = tx.asset.value;

    // Update the asset with the new value.
    tx.asset.value = tx.newValue;

    // Get the asset registry for the asset.
    return getAssetRegistry('org.acme.sample.SampleAsset')
        .then(function (assetRegistry) {

            // Update the asset in the asset registry.
            return assetRegistry.update(tx.asset);

        })
        .then(function () {

            // Emit an event for the modified asset.
            var event = getFactory().newEvent('org.acme.sample', 'SampleEvent');
            event.asset = tx.asset;
            event.oldValue = oldValue;
            event.newValue = tx.newValue;
            emit(event);

        });

}