2017-09-14 162 views
0

型号:处理相关对象Hyperledger作曲家

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

您的模型看起来不错,现在您需要编写一个交易处理器函数,该函数订阅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); 

     }); 

} 
+0

嘿丹,感谢您的响应。我经历了盆地网络。但我有几个问题。 1.我们是否可以通过交易更新参与者详细信息?在上面的例子中,对'Tobias'来说,我想通过检查他的余额是否超过某个条件来将“客户段”更改为其他值。 帮我这个: '交易updateCustomerSegemnt { - >帐户A}'' – Atreyee

+0

功能updateCustomerSegemnt(TX){ VAR余额= tx.A.balance; 如果(平衡> 3000){ 账户 A的(即托比亚斯)客户细分的\t帐户持有人应更新为 'E' } 回报getParticipantRegistry( 'org.acme.sample.Customer') 。然后(函数(participantRegistry){ 返回participantRegistry.update(TX。); }。) 。然后(函数(){ VAR事件= getFactory()newEvent( 'org.acme.sample', 'SampleEvent') ; ... emit(event); }); } – Atreyee

+0

2.如何更新从客户(资产)获取客户(参与者)[输入到交易] 关系是Account_Holder of account是客户。 3.我们如何更新参与者注册表 - 假设客户是交易的输入为 ' - >客户C' 尝试使用以下代码更新参与者之前,似乎没有为我工作。 'return getParticipantRegistry('org.acme.sample。客户') 。然后(函数(participantRegistry){ return participantRegistry.update(tx.C); })' – Atreyee