getActionType 和 createTypedAction
This commit is contained in:
parent
693333ba68
commit
b44dc772f1
37
Action.js
37
Action.js
@ -1,6 +1,5 @@
|
|||||||
const Ling = require('so.ling')
|
const Ling = require('so.ling')
|
||||||
const ticCrypto = require('tic.crypto')
|
const ticCrypto = require('tic.crypto')
|
||||||
const TypedActionDict = require('./TypedActionDict.js') // 或者直接加载到 Action 类上?
|
|
||||||
|
|
||||||
/** ****************** Public of instance ********************/
|
/** ****************** Public of instance ********************/
|
||||||
|
|
||||||
@ -62,7 +61,7 @@ MOM.verifySig = async function() {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
DAD.verifySig = async function (actionData) {
|
DAD.verifySig = async function (actionData) {
|
||||||
let typedAction = new TypedActionDict[actionData.type](actionData)
|
let typedAction = new (DAD.getActionType(actionData.type))(actionData)
|
||||||
return await typedAction.verifySig()
|
return await typedAction.verifySig()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +69,7 @@ MOM.verifyAddress = function () {
|
|||||||
return this.actorAddress === ticCrypto.pubkey2address(this.actorPubkey)
|
return this.actorAddress === ticCrypto.pubkey2address(this.actorPubkey)
|
||||||
}
|
}
|
||||||
DAD.verifyAddress = function (actionData) {
|
DAD.verifyAddress = function (actionData) {
|
||||||
let typedAction = new TypedActionDict[actionData.type](actionData)
|
let typedAction = new (DAD.getActionType(actionData.type))(actionData)
|
||||||
return typedAction.verifyAddress()
|
return typedAction.verifyAddress()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,27 +77,27 @@ MOM.verifyHash = function () {
|
|||||||
return this.hash === ticCrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
|
return this.hash === ticCrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
|
||||||
}
|
}
|
||||||
DAD.verifyHash = function (actionData) {
|
DAD.verifyHash = function (actionData) {
|
||||||
let typedAction = new TypedActionDict[actionData.type](actionData)
|
let typedAction = new (DAD.getActionType(actionData.type))(actionData)
|
||||||
return typedAction.verifyHash()
|
return typedAction.verifyHash()
|
||||||
}
|
}
|
||||||
|
|
||||||
MOM.validateMe = function() { // Applicable on both client and chain server. 子类应当覆盖本方法,静态的检查事务内容的格式。不能检查 balance 等需要全链数据库的东西,因为本方法也要用在前端检查。
|
MOM.validateMe = function() { // Applicable on both client and chain server. 子类应当覆盖本方法,静态的检查事务内容的格式。不能检查 balance 等需要全链数据库的东西,因为本方法也要用在前端检查。
|
||||||
// to implement in subclasses: 检查子类事务内容的格式
|
// to implement in subclasses: 检查子类事务内容的格式
|
||||||
let typedAction = new TypedActionDict[this.type](this)
|
let typedAction = new (DAD.getActionType(this.type))(this)
|
||||||
return typedAction.validateMe()
|
return typedAction.validateMe()
|
||||||
}
|
}
|
||||||
DAD.validate = function (action) { // Allicable on both client and chain server.
|
DAD.validate = function (action) { // Allicable on both client and chain server.
|
||||||
mylog.info(`Validating action type=${action.type} of hash=${action.hash}`)
|
mylog.info(`Validating action type=${action.type} of hash=${action.hash}`)
|
||||||
let typedAction = new TypedActionDict[action.type](action)
|
let typedAction = new (DAD.getActionType(action.type))(action)
|
||||||
return typedAction.validateMe()
|
return typedAction.validateMe()
|
||||||
}
|
}
|
||||||
|
|
||||||
MOM.executableMe = async function() { // Applicable on chain server. 子类应当覆盖本方法,动态的检查事务内容,在当前链状态下,是否能执行。To check if an action is executableMe given the current chain status.
|
MOM.executableMe = async function() { // Applicable on chain server. 子类应当覆盖本方法,动态的检查事务内容,在当前链状态下,是否能执行。To check if an action is executableMe given the current chain status.
|
||||||
let typedAction = new TypedActionDict[this.type](this)
|
let typedAction = new (DAD.getActionType(this.type))(this)
|
||||||
return await typedAction.executableMe()
|
return await typedAction.executableMe()
|
||||||
}
|
}
|
||||||
DAD.executable = async function(action) { // For chain server.
|
DAD.executable = async function(action) { // For chain server.
|
||||||
let typedAction = new TypedActionDict[action.type](action)
|
let typedAction = new (DAD.getActionType(action.type))(action)
|
||||||
if (typedAction.hasOwnProperty('executableMe')) { // 防止子类忘了定义自己的 executableMe
|
if (typedAction.hasOwnProperty('executableMe')) { // 防止子类忘了定义自己的 executableMe
|
||||||
return await typedAction.executableMe()
|
return await typedAction.executableMe()
|
||||||
}else {
|
}else {
|
||||||
@ -107,31 +106,27 @@ DAD.executable = async function(action) { // For chain server.
|
|||||||
}
|
}
|
||||||
MOM.executeMe = async function() { // For chain server. 子类应当覆盖本方法,执行事务,记录其(除了存入 Action 数据表之外的)副作用到内存数据库或其他地方。
|
MOM.executeMe = async function() { // For chain server. 子类应当覆盖本方法,执行事务,记录其(除了存入 Action 数据表之外的)副作用到内存数据库或其他地方。
|
||||||
// to implement in subclasses: 把action的影响,汇总登记到其他表格(用于辅助的、索引的表格),方便快速索引、处理。每种事务类型都要重定义这个方法。
|
// to implement in subclasses: 把action的影响,汇总登记到其他表格(用于辅助的、索引的表格),方便快速索引、处理。每种事务类型都要重定义这个方法。
|
||||||
let typedAction = new TypedActionDict[this.type](this)
|
let typedAction = new (DAD.getActionType(this.type))(this)
|
||||||
return await typedAction.executeMe()
|
return await typedAction.executeMe()
|
||||||
}
|
}
|
||||||
DAD.execute = async function (action) { // For chain server.
|
DAD.execute = async function (action) { // For chain server.
|
||||||
mylog.info(`Excecuting action type=${action.type} of hash=${action.hash}`)
|
mylog.info(`Excecuting action type=${action.type} of hash=${action.hash}`)
|
||||||
let typedAction = new TypedActionDict[action.type](action)
|
let typedAction = new (DAD.getActionType(action.type))(action)
|
||||||
return await typedAction.executeMe()
|
return await typedAction.executeMe()
|
||||||
}
|
}
|
||||||
// [todo 20190411] 执行事务池中的所有事务
|
// [todo 20190411] 执行事务池中的所有事务
|
||||||
// DAD.executePool = async function() {
|
// DAD.executePool = async function() {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
DAD.getTypedAction = function(type){
|
DAD.getActionType = (type)=>{
|
||||||
return TypedActionDict[type]
|
return require(`./${type}`)
|
||||||
}
|
|
||||||
DAD.addTypedAction = function(TypedAction){
|
|
||||||
TypedActionDict[TypedAction.name] = TypedAction
|
|
||||||
}
|
}
|
||||||
DAD.createTypedAction = function(action){
|
DAD.createTypedAction = function(action){
|
||||||
return new TypedActionDict[action.type](action)
|
return new (DAD.getActionType(action.type))(action)
|
||||||
}
|
}
|
||||||
|
|
||||||
DAD.buildUserAction = async function (action, keypair) { // Applicable on client. 客户端调用 Action.build,即可新建、并打包成一个完整的子事务,不需要亲自调用 constructor, packMe 等方法。
|
DAD.buildUserAction = async function (action, keypair) { // Applicable on client. 客户端调用 Action.build,即可新建、并打包成一个完整的子事务,不需要亲自调用 constructor, packMe 等方法。
|
||||||
if (action && action.type && keypair && keypair.seckey && keypair.pubkey && ticCrypto.seckey2pubkey(keypair.seckey)===keypair.pubkey) {
|
if (action && action.type && keypair && keypair.seckey && keypair.pubkey && ticCrypto.seckey2pubkey(keypair.seckey)===keypair.pubkey) {
|
||||||
let typedAction = new TypedActionDict[action.type](action)
|
let typedAction = new (DAD.getActionType(action.type))(action)
|
||||||
typedAction.actorPubkey = keypair.pubkey
|
typedAction.actorPubkey = keypair.pubkey
|
||||||
if (typedAction.validateMe()) {
|
if (typedAction.validateMe()) {
|
||||||
await typedAction.packMe(keypair) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。
|
await typedAction.packMe(keypair) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。
|
||||||
@ -184,8 +179,8 @@ DAD.api.prepare = async function (option) {
|
|||||||
} catch (error) {}
|
} catch (error) {}
|
||||||
}
|
}
|
||||||
// 前端发来action数据,进行格式检查(不检查是否可执行--这和事务类型、执行顺序有关)后放入缓冲池。
|
// 前端发来action数据,进行格式检查(不检查是否可执行--这和事务类型、执行顺序有关)后放入缓冲池。
|
||||||
if (option && option.Action && option.Action.type && TypedActionDict[option.Action.type] && option.Action.hash && !DAD.actionPool[option.Action.hash]) {
|
if (option && option.Action && option.Action.type && DAD.getActionType(option.Action.type) && option.Action.hash && !DAD.actionPool[option.Action.hash]) {
|
||||||
let typedAction = new TypedActionDict[option.Action.type](option.Action)
|
let typedAction = new (DAD.getActionType(option.Action.type))(option.Action)
|
||||||
if (typedAction.verifyAddress() && // 只检查所有事务通用的格式
|
if (typedAction.verifyAddress() && // 只检查所有事务通用的格式
|
||||||
await typedAction.verifySig() &&
|
await typedAction.verifySig() &&
|
||||||
typedAction.verifyHash() &&
|
typedAction.verifyHash() &&
|
||||||
@ -195,7 +190,7 @@ DAD.api.prepare = async function (option) {
|
|||||||
DAD.actionPool[option.Action.hash] = typedAction
|
DAD.actionPool[option.Action.hash] = typedAction
|
||||||
DAD.actionPoolInfo.totalAmount += option.Action.amount || 0
|
DAD.actionPoolInfo.totalAmount += option.Action.amount || 0
|
||||||
DAD.actionPoolInfo.totalFee += option.Action.fee || 0
|
DAD.actionPoolInfo.totalFee += option.Action.fee || 0
|
||||||
// TypedActionDict.Netnode.broadcast({ Action: option.Action }) // 即使对 master 分支的node.server 也报错:Cannot read property 'broadcast' of undefined
|
// wo.Netnode.broadcast({ Action: option.Action }) // 即使对 master 分支的node.server 也报错:Cannot read property 'broadcast' of undefined
|
||||||
return option.Action
|
return option.Action
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user