1.聯(lián)盟鏈
聯(lián)盟鏈僅限于聯(lián)盟成員项鬼,因其只針對成員開放全部或部分功能,所以聯(lián)盟規(guī)則就是聯(lián)盟鏈的權(quán)限機制耻陕。聯(lián)盟鏈上的共識過程由預(yù)先選好的節(jié)點控制拙徽,目前普遍適用于機構(gòu)間的信息共享,如交易诗宣、結(jié)算膘怕、或清算等B2B場景。聯(lián)盟鏈幾乎不采用工作量證明共識機制而是采用權(quán)益證明或PBTF等共識算法梧田。聯(lián)盟鏈由參與成員機構(gòu)共同維護淳蔼,并提供了對參與成員的管理、認證裁眯、授權(quán)鹉梨、監(jiān)控、審計等全套安全管理功能穿稳。
2015年成立的R3聯(lián)盟存皂,就是銀行業(yè)的一個聯(lián)盟鏈,目前已加入的成員多達40多個,包括世界著名的銀行摩根大通旦袋、匯豐骤菠、高盛等。聯(lián)盟鏈建立在各個節(jié)點本身之間已經(jīng)存在信任疤孕,一般用于行業(yè)協(xié)會商乎、高級別機構(gòu)組織、大型連鎖企業(yè)對下屬單位和分管機構(gòu)的交易和監(jiān)管祭阀。
最近商業(yè)落地比較多的應(yīng)該就是超級積分鹉戚。
-
超級積分demo
image.png
抽象出來,合約主要的功能:有創(chuàng)建組織對象(oneitfarm)专控、創(chuàng)建app抹凳、創(chuàng)建用戶、增加積分伦腐、減少積分赢底;
我貼一段自己寫「超級積分」智能合約邏輯部分代碼(js):
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Sample transaction processor function.
* @param {org.acme.model.createInitialData} trade The sample transaction instance.
* @transaction
*/
function createInitialData(tx){
//創(chuàng)建一個對象叫oneitfarm,給他初始值 100w
var factory = getFactory();
var oneitfarm = factory.newResource( 'org.acme.model', 'OneItFarm','1' );
oneitfarm.value = "1000000";
oneitfarm.isSet = true;
return getParticipantRegistry('org.acme.model.OneItFarm')
.then(function (registry) {
// update the grower's balance
return registry.add(oneitfarm);
})
}
function createApp(tx){
//獲取oneitfarm 對象
// 創(chuàng)建 App 對象柏蘑,賦值1w
// oneitfarm 內(nèi)value 減少1w
var NS = 'org.acme.model'
var oneitfarm = null;
var oneitfarmRegist = null;
var coin = 10000
return getParticipantRegistry( NS + '.OneItFarm' )
.then(function (oneitfarmRegistry) {
oneitfarmRegist = oneitfarmRegistry
return oneitfarmRegist.get('1');
})
.then(function(oneitfarmDriver){
oneitfarm = oneitfarmDriver
if (oneitfarm.value < coin) {
throw new Error ("Insufficient funds");
}
var factory = getFactory();
var app = factory.newResource( NS, 'App', tx.appkey);
app.value = coin;
return getParticipantRegistry('org.acme.model.App')
.then(function(appRegistry){
return appRegistry.add(app);
})
.then(function(){
oneitfarm.value = oneitfarm.value - coin;
return oneitfarmRegist.update(oneitfarm);
})
.catch(function (error) {
// Add optional error handling here.
});
})
.catch(function (error) {
// Add optional error handling here.
});
}
function createMember(tx){
//獲取App 對象
// 創(chuàng)建 Member 對象幸冻,賦值10
// accountId所屬的App 內(nèi)value 減少10
var NS = 'org.acme.model'
var appkey = tx.app.appkey
var coin = 10
var app = null;
var appRegist = null;
return getParticipantRegistry( NS + '.App')
.then(function (appRegistry) {
appRegist = appRegistry;
return appRegist.get(appkey);
})
.then(function(appDriver){
console.log("111111")
app = appDriver;
if (app.value < coin) {
throw new Error ("Insufficient funds");
}
var factory = getFactory();
var member = factory.newResource( NS, 'Member', tx.accountId);
member.value = coin;
member.app = tx.app
return getParticipantRegistry('org.acme.model.Member')
.then(function(memberRegistry){
return memberRegistry.add(member);
})
.then(function(){
console.log("333333")
app.value = app.value - coin;
return appRegist.update(app);
})
.catch(function (error) {
// Add optional error handling here.
});
})
.catch(function (error) {
// Add optional error handling here.
});
}
/**
* Sample transaction processor function.
* @param {org.acme.model.addMemberValue} trade The sample transaction instance.
* @transaction
*/
function addMemberValue(tx) {
//用戶買東西- 商家給用戶積分
//用戶增加積分
//商家加減少積分
var NS = 'org.acme.model'
var coin = tx.coin
var appkey = tx.app.appkey
var app = null
var appRegist = null
var accountId = tx.member.accountId
var member = null
var memberRegist = null
return getParticipantRegistry( NS + '.App')
.then(function (appRegistry) {
appRegist = appRegistry;
return appRegist.get(appkey);
})
.then(function(appDriver){
app = appDriver;
if (app.value < coin) {
throw new Error ("Insufficient funds");
}
return getParticipantRegistry( NS + '.Member')
.then(function(memberRegistry){
memberRegist = memberRegistry
return memberRegist.get(accountId);
})
.then(function(memberDriver){
member = memberDriver;
member.value += coin;
return memberRegist.update(member);
})
.then(function(){
app.value -=coin;
return appRegist.update(app);
})
.catch(function (error) {
// Add optional error handling here.
});
})
.catch(function (error) {
// Add optional error handling here.
});
}
/**
* Sample transaction processor function.
* @param {org.acme.model.reduceMemberValue} trade The sample transaction instance.
* @transaction
*/
function reduceMemberValue(tx) {
//兌換
//用戶減少積分
//商家增加積分
var NS = 'org.acme.model'
var coin = tx.coin
var appkey = tx.app.appkey
var app = null
var appRegist = null
var accountId = tx.member.accountId
var member = null
var memberRegist = null
return getParticipantRegistry( NS + '.Member')
.then(function(memberRegistry){
memberRegist = memberRegistry
return memberRegist.get(accountId);
})
.then(function(memberDriver){
member = memberDriver;
if (member.value < coin) {
throw new Error ("Insufficient funds");
}
return getParticipantRegistry( NS + '.App')
.then(function (appRegistry) {
appRegist = appRegistry;
return appRegist.get(appkey);
})
.then(function(appDriver){
app = appDriver;
app.value +=coin;
return appRegist.update(app);
})
.then(function(){
member.value -=coin;
return memberRegist.update(member);
})
.catch(function (error) {
// Add optional error handling here.
});
})
.catch(function (error) {
// Add optional error handling here.
});
}