logic.js 文件如何在 Hyperledger Composer 中工作?
How logic.js file work in Hyperledger Composer?
在 Hyperledger Composer 中,logic.js 与 Transaction 相关吗?
如何从 logic.js 访问列表?
有任何好的教程来了解我可以在 logic.js 文件中做什么?
是的,但不必独占文件名 'logic.js'。在这里查看更多 https://hyperledger.github.io/composer/latest/reference/js_scripts
您首先对数组字段建模https://hyperledger.github.io/composer/latest/reference/cto_language.html then code it. Arrays are handled like any javascript array handling. See examples here -> https://github.com/hyperledger/composer-sample-networks/blob/master/packages/trade-network/lib/logic.js#L45 of results from a query being handled in an array. Also temperatureReadings is an array being handled in the sample networks here https://github.com/hyperledger/composer-sample-networks/blob/v0.16.x/packages/perishable-network/lib/logic.js#L37
我鼓励您尝试教程以验证您的理解。 https://hyperledger.github.io/composer/latest/tutorials/tutorials
不过,这是一个答案,我正在解释 logic.js
与 transaction
的关系,以便 reader 可以理解。
交易是将资产转移到账本上或从账本上转移。在 Hyperledger Composer
中,事务在模型(包含 .cto
扩展名)文件中定义。在 logic.js
(可以是 anything.js
)文件中包含 Transaction processor function
以执行模型文件中定义的事务。
这是示例模型文件:
/**
* My commodity trading network
*/
namespace org.acme.mynetwork
asset Commodity identified by tradingSymbol {
o String tradingSymbol
--> Trader owner
}
participant Trader identified by tradeId {
o String tradeId
o String firstName
o String lastName
}
transaction Trade {
--> Commodity commodity
--> Trader newOwner
}
在模型文件中,定义了一个 Trade
交易,指定与资产和参与者的关系。 Trade
交易旨在简单地接受正在交易的 Commodity
资产的标识符,以及要设置为新所有者的 Trader
参与者的标识符。
这是一个逻辑文件,其中包含 JavaScript 用于执行模型文件中定义的事务的逻辑。
/**
* Track the trade of a commodity from one trader to another
* @param {org.acme.mynetwork.Trade} trade - the trade to be processed
* @transaction
*/
async function tradeCommodity(trade) {
trade.commodity.owner = trade.newOwner;
let assetRegistry = await getAssetRegistry('org.acme.mynetwork.Commodity');
await assetRegistry.update(trade.commodity);
}
教程
在 Hyperledger Composer 中,logic.js 与 Transaction 相关吗?
如何从 logic.js 访问列表?
有任何好的教程来了解我可以在 logic.js 文件中做什么?
是的,但不必独占文件名 'logic.js'。在这里查看更多 https://hyperledger.github.io/composer/latest/reference/js_scripts
您首先对数组字段建模https://hyperledger.github.io/composer/latest/reference/cto_language.html then code it. Arrays are handled like any javascript array handling. See examples here -> https://github.com/hyperledger/composer-sample-networks/blob/master/packages/trade-network/lib/logic.js#L45 of results from a query being handled in an array. Also temperatureReadings is an array being handled in the sample networks here https://github.com/hyperledger/composer-sample-networks/blob/v0.16.x/packages/perishable-network/lib/logic.js#L37
我鼓励您尝试教程以验证您的理解。 https://hyperledger.github.io/composer/latest/tutorials/tutorials
不过,这是一个答案,我正在解释 logic.js
与 transaction
的关系,以便 reader 可以理解。
交易是将资产转移到账本上或从账本上转移。在 Hyperledger Composer
中,事务在模型(包含 .cto
扩展名)文件中定义。在 logic.js
(可以是 anything.js
)文件中包含 Transaction processor function
以执行模型文件中定义的事务。
这是示例模型文件:
/**
* My commodity trading network
*/
namespace org.acme.mynetwork
asset Commodity identified by tradingSymbol {
o String tradingSymbol
--> Trader owner
}
participant Trader identified by tradeId {
o String tradeId
o String firstName
o String lastName
}
transaction Trade {
--> Commodity commodity
--> Trader newOwner
}
在模型文件中,定义了一个 Trade
交易,指定与资产和参与者的关系。 Trade
交易旨在简单地接受正在交易的 Commodity
资产的标识符,以及要设置为新所有者的 Trader
参与者的标识符。
这是一个逻辑文件,其中包含 JavaScript 用于执行模型文件中定义的事务的逻辑。
/**
* Track the trade of a commodity from one trader to another
* @param {org.acme.mynetwork.Trade} trade - the trade to be processed
* @transaction
*/
async function tradeCommodity(trade) {
trade.commodity.owner = trade.newOwner;
let assetRegistry = await getAssetRegistry('org.acme.mynetwork.Commodity');
await assetRegistry.update(trade.commodity);
}
教程