1.联盟链
联盟链仅限于联盟成员,因其只针对成员开放全部或部分功能,所以联盟规则就是联盟链的权限机制。联盟链上的共识过程由预先选好的节点控制,目前普遍适用于机构间的信息共享,如交易、结算、或清算等B2B场景。联盟链几乎不采用工作量证明共识机制而是采用权益证明或PBTF等共识算法。联盟链由参与成员机构共同维护,并提供了对参与成员的管理、认证、授权、监控、审计等全套安全管理功能。
2015年成立的R3联盟,就是银行业的一个联盟链,目前已加入的成员多达40多个,包括世界著名的银行摩根大通、汇丰、高盛等。联盟链建立在各个节点本身之间已经存在信任,一般用于行业协会、高级别机构组织、大型连锁企业对下属单位和分管机构的交易和监管。
最近商业落地比较多的应该就是超级积分。
-
超级积分demo
抽象出来,合约主要的功能:有创建组织对象(oneitfarm)、创建app、创建用户、增加积分、减少积分;
我贴一段自己写「超级积分」智能合约逻辑部分代码(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){
//创建一个对象叫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 对象
// 创建 App 对象,赋值1w
// oneitfarm 内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 对象
// 创建 Member 对象,赋值10
// accountId所属的App 内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.
});
}