Compare commits

...

6 Commits
main ... kiss

4 changed files with 47 additions and 49 deletions

View File

@ -1,6 +1,6 @@
const Ling = require('so.ling')
const ticCrypto = require('tic.crypto')
const wo = {}
const my = {} // 私有数据
/** ****************** Public of instance ********************/
@ -35,12 +35,12 @@ MOM._model = {
json: { default: undefined, sqlite: 'TEXT' } // 给不同类型的 ActionXxx 子类来自定义其所需的数据结构
}
MOM.packMe = async function (keypair) { // 由前端调用,后台不创建
this.actorPubkey = keypair.pubkey
this.actorAddress = ticCrypto.pubkey2address(keypair.pubkey)
MOM.packMe = async function ({seckey, pubkey}={}) { // 由前端调用,后台不创建
this.actorPubkey = pubkey
this.actorAddress = ticCrypto.pubkey2address(pubkey)
this.timestamp = new Date()
await this.signMe(keypair.seckey)
await this.signMe(seckey)
this.hashMe()
return this
}
@ -62,7 +62,7 @@ MOM.verifySig = async function() {
return result
}
DAD.verifySig = async function (actionData) {
let typedAction = new wo[actionData.type](actionData)
let typedAction = new (DAD.getActionType(actionData.type))(actionData)
return await typedAction.verifySig()
}
@ -70,7 +70,7 @@ MOM.verifyAddress = function () {
return this.actorAddress === ticCrypto.pubkey2address(this.actorPubkey)
}
DAD.verifyAddress = function (actionData) {
let typedAction = new wo[actionData.type](actionData)
let typedAction = new (DAD.getActionType(actionData.type))(actionData)
return typedAction.verifyAddress()
}
@ -78,27 +78,27 @@ MOM.verifyHash = function () {
return this.hash === ticCrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
}
DAD.verifyHash = function (actionData) {
let typedAction = new wo[actionData.type](actionData)
let typedAction = new (DAD.getActionType(actionData.type))(actionData)
return typedAction.verifyHash()
}
MOM.validateMe = function() { // Applicable on both client and chain server. 子类应当覆盖本方法,静态的检查事务内容的格式。不能检查 balance 等需要全链数据库的东西,因为本方法也要用在前端检查。
// to implement in subclasses: 检查子类事务内容的格式
let typedAction = new wo[this.type](this)
let typedAction = new (DAD.getActionType(this.type))(this)
return typedAction.validateMe()
}
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)
let typedAction = new (DAD.getActionType(action.type))(action)
return typedAction.validateMe()
}
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)
let typedAction = new (DAD.getActionType(this.type))(this)
return await typedAction.executableMe()
}
DAD.executable = async function(action) { // For chain server.
let typedAction = new wo[action.type](action)
let typedAction = new (DAD.getActionType(action.type))(action)
if (typedAction.hasOwnProperty('executableMe')) { // 防止子类忘了定义自己的 executableMe
return await typedAction.executableMe()
}else {
@ -107,34 +107,33 @@ DAD.executable = async function(action) { // For chain server.
}
MOM.executeMe = async function() { // For chain server. 子类应当覆盖本方法,执行事务,记录其(除了存入 Action 数据表之外的)副作用到内存数据库或其他地方。
// to implement in subclasses: 把action的影响汇总登记到其他表格用于辅助的、索引的表格方便快速索引、处理。每种事务类型都要重定义这个方法。
let typedAction = new wo[this.type](this)
let typedAction = new (DAD.getActionType(this.type))(this)
return await typedAction.executeMe()
}
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)
let typedAction = new (DAD.getActionType(action.type))(action)
return await typedAction.executeMe()
}
// [todo 20190411] 执行事务池中的所有事务
// DAD.executePool = async function() {
// }
DAD._initTypeDict = function(typedActionDict) {
Object.assign(wo, typedActionDict)
}
DAD.getTypedAction = function(type){
return wo[type]
DAD.getActionType = (type)=>{
if (!my.ActionTypeDict) {
my.ActionTypeDict = require('./indexActionTypes.js') // 避免动态require
}
return my.ActionTypeDict[type]
}
DAD.createTypedAction = function(action){
return new wo[action.type](action)
return new (DAD.getActionType(action.type))(action)
}
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) {
let typedAction = new wo[action.type](action)
typedAction.actorPubkey = keypair.pubkey
DAD.buildUserAction = async function (action, {seckey, pubkey}={}) { // Applicable on client. 客户端调用 Action.build即可新建、并打包成一个完整的子事务不需要亲自调用 constructor, packMe 等方法。
if (action && action.type && seckey && pubkey && ticCrypto.seckey2pubkey(seckey)===pubkey) {
let typedAction = new (DAD.getActionType(action.type))(action)
typedAction.actorPubkey = pubkey
if (typedAction.validateMe()) {
await typedAction.packMe(keypair) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。
await typedAction.packMe({seckey, pubkey}) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。
return typedAction
}
}
@ -165,10 +164,16 @@ DAD.api = {}
DAD.api.getAction = async function (option) {
return await DAD.getOne(option)
}
DAD.api.getActionList = async function (option) {
return await DAD.getAll(option)
}
DAD.api.getMyActionList = async function ({Action={}, config}={}) {
let list = {}
let {myAddress, ...action} = Action
list.fromMe = await DAD.getAll({Action:{actorAddress:myAddress, ...action}, config})
list.toMe = await DAD.getAll({Action:{toAddress:myAddress, ...action}, config})
return list
}
DAD.api.prepare = async function (option) {
if (typeof option === 'string') {
@ -177,8 +182,8 @@ DAD.api.prepare = async function (option) {
} catch (error) {}
}
// 前端发来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 (option && option.Action && option.Action.type && DAD.getActionType(option.Action.type) && option.Action.hash && !DAD.actionPool[option.Action.hash]) {
let typedAction = new (DAD.getActionType(option.Action.type))(option.Action)
if (typedAction.verifyAddress() && // 只检查所有事务通用的格式
await typedAction.verifySig() &&
typedAction.verifyHash() &&

View File

@ -18,17 +18,20 @@ MOM.validateMe = function () {
}
MOM.executableMe = async function() {
let balance = await wo.Store.getBalance(this.actorAddress)
let balance = await wo.Account.getBalance(this.actorAddress)
return balance >= this.amount + this.fee
}
MOM.executeMe = async function () {
let balance = await wo.Store.getBalance(this.actorAddress)
if (balance >= this.amount + this.fee) {
await wo.Store.decrease(this.actorAddress, this.amount + this.fee)
await wo.Store.increase(this.toAddress, this.amount)
let sender= await wo.Account.getOne({Account: { address: this.actorAddress }})
if (sender && sender.type !== 'multisig' && this.toAddress != this.actorAddress && sender.balance >= this.amount + this.fee){
await sender.setMe({Account:{ balance: Number(sender.balance)-Number(this.amount)-Number(this.fee), countAction: sender.countAction+1 }, cond:{ address:sender.address}})
let getter= await wo.Account.getOne({Account: { address: this.toAddress }}) || await wo.Account.addOne({Account: { address: this.toAddress }})
await getter.setMe({Account:{ balance: Number(getter.balance)+Number(this.amount), countAction: getter.countAction+1 }, cond:{ address:getter.address}})
mylog.info('Excecuted action='+JSON.stringify(this))
return this
}
// mylog.info('balance('+sender.address+')='+sender.balance+' is less than '+this.amount+', 无法转账')
return null
}

View File

@ -1,15 +1 @@
// const Action = require('./Action.js') // 不要在 index 里引入 Action避免循环无限引入。
const ActionTransfer = require('./ActionTransfer.js')
const ActionStore = require('./ActionStore.js')
const ActionMultisig = require('./ActionMultisig.js')
const ActionLockProof = require('./ActionLockProof.js')
// const ActionTac = require('./ActionTac.js')
module.exports = {
// Action,
ActionTransfer,
ActionStore,
ActionMultisig,
ActionLockProof,
// ActionTac
}
module.exports = require('./Action.js')

4
indexActionTypes.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
ActionTransfer: require('./ActionTransfer.js'),
ActionStore: require('./ActionStore.js')
}