1. 把 validateMe 分解成 静态数据检查(给客户端调用)validateMe 和 动态可执行性检查(给链节点调用)executableMe.
2. 在 Action.api.prepare() 里,生成可运行的对象 typedAction 存入 ActionPool,而不是仅仅存数据 option.Action进去。 3. 删除 Action.getJson(),把 DAD.verifyXxx(action) 都改为 MOM.verifyXxx(). 4. 添加了 ActionRegisterChain.js 作为 应用链注册事务。
This commit is contained in:
105
Action.js
105
Action.js
@@ -1,5 +1,5 @@
|
||||
var Ling = require('fon.ling')
|
||||
var Ticrypto = require('tic.crypto')
|
||||
var ticCrypto = require('tic.crypto')
|
||||
|
||||
/** ****************** Public of instance ********************/
|
||||
|
||||
@@ -35,7 +35,7 @@ MOM._model = {
|
||||
|
||||
MOM.packMe = function (keypair) { // 由前端调用,后台不创建
|
||||
this.actorPubkey = keypair.pubkey
|
||||
this.actorAddress = Ticrypto.pubkey2address(keypair.pubkey)
|
||||
this.actorAddress = ticCrypto.pubkey2address(keypair.pubkey)
|
||||
this.timestamp = new Date()
|
||||
|
||||
this.signMe(keypair.seckey)
|
||||
@@ -45,61 +45,88 @@ MOM.packMe = function (keypair) { // 由前端调用,后台不创建
|
||||
|
||||
MOM.signMe = function (seckey) { // 由前端调用,后台不该进行签名
|
||||
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] }) // 是前端用户发起事务时签字,这时候还不知道进入哪个区块,所以不能计入blockHash
|
||||
this.actorSignature = Ticrypto.sign(json, seckey)
|
||||
this.actorSignature = ticCrypto.sign(json, seckey)
|
||||
return this
|
||||
}
|
||||
|
||||
MOM.hashMe = function () {
|
||||
this.hash = Ticrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] })) // block.hash 受到所包含的actionList影响,所以action不能受blockHash影响,否则循环了
|
||||
this.hash = ticCrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] })) // block.hash 受到所包含的actionList影响,所以action不能受blockHash影响,否则循环了
|
||||
return this
|
||||
}
|
||||
|
||||
DAD.getJson = function (action, option = {}) {
|
||||
let data = {}
|
||||
let sortedKey = Object.keys(DAD.prototype._model).sort()
|
||||
for (let exkey of option.exclude) { sortedKey.splice(sortedKey.indexOf(exkey), 1) }
|
||||
for (let key of sortedKey) { // 忽略一些不需要签名的属性
|
||||
data[key] = action[key]
|
||||
MOM.verifySig = function() {
|
||||
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] })
|
||||
let result = ticCrypto.verify(json, this.actorSignature, this.actorPubkey)
|
||||
return result
|
||||
}
|
||||
DAD.verifySig = function (actionData) {
|
||||
let typedAction = new wo[actionData.type](actionData)
|
||||
return typedAction.verifySig()
|
||||
}
|
||||
|
||||
MOM.verifyAddress = function () {
|
||||
return this.actorAddress === ticCrypto.pubkey2address(this.actorPubkey)
|
||||
}
|
||||
DAD.verifyAddress = function (actionData) {
|
||||
let typedAction = new wo[actionData.type](actionData)
|
||||
return typedAction.verifyAddress()
|
||||
}
|
||||
|
||||
MOM.verifyHash = function () {
|
||||
return this.hash === ticCrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
|
||||
}
|
||||
DAD.verifyHash = function (actionData) {
|
||||
let typedAction = new wo[actionData.type](actionData)
|
||||
return typedAction.verifyHash()
|
||||
}
|
||||
|
||||
DAD.build = function (action, keypair) { // Applicable on client. 客户端调用 Action.build,即可新建、并打包成一个完整的子事务,不需要亲自调用 constructor, packMe 等方法。
|
||||
if (action && action.type && keypair && keypair.seckey && keypair.pubkey) {
|
||||
let typedAction = new wo[action.type](action)
|
||||
if (typedAction.validateMe()) {
|
||||
typedAction.packMe(keypair)
|
||||
return typedAction
|
||||
}
|
||||
}
|
||||
let json = JSON.stringify(data)
|
||||
return json
|
||||
return null
|
||||
}
|
||||
|
||||
DAD.verifySig = function (action) {
|
||||
let json = DAD.getJson(action, { exclude: ['hash', 'blockHash', 'actorSignature'] })
|
||||
let res = Ticrypto.verify(json, action.actorSignature, action.actorPubkey)
|
||||
return res
|
||||
}
|
||||
|
||||
DAD.verifyAddress = function (action) {
|
||||
return action.actorAddress === Ticrypto.pubkey2address(action.actorPubkey)
|
||||
}
|
||||
|
||||
DAD.verifyHash = function (action) {
|
||||
return action.hash === Ticrypto.hash(DAD.getJson(action, { exclude: ['hash', 'blockHash'] }))
|
||||
}
|
||||
|
||||
MOM.validateMe = async function() { // 子类应当覆盖本方法。
|
||||
MOM.validateMe = function() { // Applicable on chain server. 子类应当覆盖本方法,静态的检查事务内容的格式和语义,是否符合该子类事务的特性要求。To validate an action's content format.
|
||||
// to implement in subclasses: 检查子类事务内容的格式
|
||||
let typedAction = new wo[this.type](this)
|
||||
return await typedAction.validateMe()
|
||||
return typedAction.validateMe()
|
||||
}
|
||||
DAD.validate = async function (action) {
|
||||
DAD.validate = function (action) { // Allicable on both client and chain server.
|
||||
mylog.info(`Validating action type=${action.type} of hash=${action.hash}`)
|
||||
let typedAction = new wo[action.type](action)
|
||||
return await typedAction.validateMe()
|
||||
return typedAction.validateMe()
|
||||
}
|
||||
|
||||
MOM.executeMe = async function() { // 子类应当覆盖本方法。
|
||||
MOM.executableMe = async function() { // Applicable on chain server. 子类应当覆盖本方法,动态的检查事务内容,在当前链状态下,是否能执行。To check if an action is executableMe given the current chain status.
|
||||
let typedAction = new wo[this.type](this)
|
||||
return await typedAction.executableMe()
|
||||
}
|
||||
DAD.executable = async function(action) { // For chain server.
|
||||
let typedAction = new wo[action.type](action)
|
||||
if (typedAction.hasOwnProperty('executableMe')) { // 防止子类忘了定义自己的 executableMe
|
||||
return await typedAction.executableMe()
|
||||
}else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
MOM.executeMe = async function() { // For chain server. 子类应当覆盖本方法,执行事务,记录其(除了存入 Action 数据表之外的)副作用到内存数据库或其他地方。
|
||||
// to implement in subclasses: 把action的影响,汇总登记到其他表格(用于辅助的、索引的表格),方便快速索引、处理。每种事务类型都要重定义这个方法。
|
||||
let typedAction = new wo[this.type](this)
|
||||
return await typedAction.executeMe()
|
||||
}
|
||||
DAD.execute = async function (action) {
|
||||
DAD.execute = async function (action) { // For chain server.
|
||||
mylog.info(`Excecuting action type=${action.type} of hash=${action.hash}`)
|
||||
let typedAction = new wo[action.type](action)
|
||||
return await typedAction.executeMe()
|
||||
}
|
||||
// [todo 20190411] 执行事务池中的所有事务
|
||||
// DAD.executePool = async function() {
|
||||
// }
|
||||
|
||||
/**
|
||||
* 获取一批交易,在出块时调用。调用actionPool的内容被深拷贝到currentActionPool后自动清空。
|
||||
@@ -137,14 +164,16 @@ DAD.api.prepare = async function (option) {
|
||||
} catch (error) {}
|
||||
}
|
||||
// 前端发来action数据,进行格式检查(不检查是否可执行--这和事务类型、执行顺序有关)后放入缓冲池。
|
||||
if (option && option.Action && option.Action.type && option.Action.hash && !DAD.actionPool[option.Action.hash]) {
|
||||
if (DAD.verifyAddress(option.Action) && // 只检查所有事务通用的格式
|
||||
DAD.verifySig(option.Action) &&
|
||||
DAD.verifyHash(option.Action) &&
|
||||
if (option && option.Action && option.Action.type && wo[option.Action.type] && option.Action.hash && !DAD.actionPool[option.Action.hash]) {
|
||||
let typedAction = new wo[option.Action.type](option.Action)
|
||||
if (typedAction.verifyAddress() && // 只检查所有事务通用的格式
|
||||
typedAction.verifySig() &&
|
||||
typedAction.verifyHash() &&
|
||||
!DAD.actionPool[option.Action.hash] &&
|
||||
(await DAD.validate(option.Action)) // 调用子类的 validate 方法,检查子类的事务内容格式
|
||||
typedAction.validateMe() && // 检查事务的内容是否符合该子类事务的格式
|
||||
(await typedAction.executableMe()) // 检查事务是否可执行,在当前链的状态下。
|
||||
) {
|
||||
DAD.actionPool[option.Action.hash] = option.Action
|
||||
DAD.actionPool[option.Action.hash] = typedAction
|
||||
DAD.actionPoolInfo.totalAmount += option.Action.amount || 0
|
||||
DAD.actionPoolInfo.totalFee += option.Action.fee || 0
|
||||
wo.NodeNet.broadcast({ Action: option.Action })
|
||||
|
||||
Reference in New Issue
Block a user