u
This commit is contained in:
parent
cfad03864a
commit
e3920911b0
102
Action.js
102
Action.js
@ -1,14 +1,14 @@
|
||||
const Ling = require('so.ling')
|
||||
const ticCrypto = require('tic.crypto')
|
||||
const ticrypto = require('tic-crypto')
|
||||
const wo = {}
|
||||
|
||||
/** ****************** Public of instance ********************/
|
||||
|
||||
const DAD = module.exports = function Action (prop) {
|
||||
const DAD = (module.exports = function Action (prop) {
|
||||
this._class = this.constructor.name
|
||||
this.setProp(prop)
|
||||
this.type = this.constructor.name
|
||||
}
|
||||
})
|
||||
DAD.__proto__ = Ling
|
||||
|
||||
const MOM = DAD.prototype
|
||||
@ -18,7 +18,11 @@ MOM.__proto__ = Ling.prototype
|
||||
MOM._table = DAD.name
|
||||
MOM._tablekey = 'hash'
|
||||
MOM._model = {
|
||||
hash: { default: undefined, sqlite: 'TEXT UNIQUE', mysql: 'VARCHAR(64) PRIMARY KEY' }, // 不纳入签名和哈希
|
||||
hash: {
|
||||
default: undefined,
|
||||
sqlite: 'TEXT UNIQUE',
|
||||
mysql: 'VARCHAR(64) PRIMARY KEY'
|
||||
}, // 不纳入签名和哈希
|
||||
version: { default: 0, sqlite: 'INTEGER' },
|
||||
type: { default: 'Action', sqlite: 'TEXT', mysql: 'VARCHAR(100)' }, // 是否放在 assets里更好?这里该放action自己的version
|
||||
blockHash: { default: undefined, sqlite: 'TEXT', mysql: 'VARCHAR(64)' }, // 不纳入签名和哈希。只为了方便查找
|
||||
@ -35,9 +39,10 @@ MOM._model = {
|
||||
json: { default: undefined, sqlite: 'TEXT' } // 给不同类型的 ActionXxx 子类来自定义其所需的数据结构
|
||||
}
|
||||
|
||||
MOM.packMe = async function (keypair) { // 由前端调用,后台不创建
|
||||
MOM.packMe = async function (keypair) {
|
||||
// 由前端调用,后台不创建
|
||||
this.actorPubkey = keypair.pubkey
|
||||
this.actorAddress = ticCrypto.pubkey2address(keypair.pubkey)
|
||||
this.actorAddress = ticrypto.pubkey2address(keypair.pubkey)
|
||||
this.timestamp = new Date()
|
||||
|
||||
await this.signMe(keypair.seckey)
|
||||
@ -45,20 +50,25 @@ MOM.packMe = async function (keypair) { // 由前端调用,后台不创建
|
||||
return this
|
||||
}
|
||||
|
||||
MOM.signMe = async function (seckey) { // 由前端调用,后台不该进行签名
|
||||
MOM.signMe = async function (seckey) {
|
||||
// 由前端调用,后台不该进行签名
|
||||
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] }) // 是前端用户发起事务时签字,这时候还不知道进入哪个区块,所以不能计入blockHash
|
||||
this.actorSignature = await ticCrypto.sign(json, seckey)
|
||||
this.actorSignature = await ticrypto.sign(json, seckey)
|
||||
return this
|
||||
}
|
||||
|
||||
MOM.hashMe = function () {
|
||||
this.hash = ticCrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] })) // block.hash 受到所包含的actionList影响,所以action不能受blockHash影响,否则循环了
|
||||
this.hash = ticrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] })) // block.hash 受到所包含的actionList影响,所以action不能受blockHash影响,否则循环了
|
||||
return this
|
||||
}
|
||||
|
||||
MOM.verifySig = async function() {
|
||||
MOM.verifySig = async function () {
|
||||
let json = this.getJson({ exclude: ['hash', 'blockHash', 'actorSignature'] })
|
||||
let result = await ticCrypto.verify(json, this.actorSignature, this.actorPubkey)
|
||||
let result = await ticrypto.verify(
|
||||
json,
|
||||
this.actorSignature,
|
||||
this.actorPubkey
|
||||
)
|
||||
return result
|
||||
}
|
||||
DAD.verifySig = async function (actionData) {
|
||||
@ -67,7 +77,7 @@ DAD.verifySig = async function (actionData) {
|
||||
}
|
||||
|
||||
MOM.verifyAddress = function () {
|
||||
return this.actorAddress === ticCrypto.pubkey2address(this.actorPubkey)
|
||||
return this.actorAddress === ticrypto.pubkey2address(this.actorPubkey)
|
||||
}
|
||||
DAD.verifyAddress = function (actionData) {
|
||||
let typedAction = new wo[actionData.type](actionData)
|
||||
@ -75,42 +85,52 @@ DAD.verifyAddress = function (actionData) {
|
||||
}
|
||||
|
||||
MOM.verifyHash = function () {
|
||||
return this.hash === ticCrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
|
||||
return (
|
||||
this.hash ===
|
||||
ticrypto.hash(this.getJson({ exclude: ['hash', 'blockHash'] }))
|
||||
)
|
||||
}
|
||||
DAD.verifyHash = function (actionData) {
|
||||
let typedAction = new wo[actionData.type](actionData)
|
||||
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: 检查子类事务内容的格式
|
||||
let typedAction = new wo[this.type](this)
|
||||
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}`)
|
||||
let typedAction = new wo[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.
|
||||
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.
|
||||
DAD.executable = async function (action) {
|
||||
// For chain server.
|
||||
let typedAction = new wo[action.type](action)
|
||||
if (typedAction.hasOwnProperty('executableMe')) { // 防止子类忘了定义自己的 executableMe
|
||||
if (typedAction.hasOwnProperty('executableMe')) {
|
||||
// 防止子类忘了定义自己的 executableMe
|
||||
return await typedAction.executableMe()
|
||||
}else {
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
MOM.executeMe = async function() { // For chain server. 子类应当覆盖本方法,执行事务,记录其(除了存入 Action 数据表之外的)副作用到内存数据库或其他地方。
|
||||
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) { // For chain server.
|
||||
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()
|
||||
@ -119,18 +139,26 @@ DAD.execute = async function (action) { // For chain server.
|
||||
// DAD.executePool = async function() {
|
||||
// }
|
||||
|
||||
DAD._initTypeDict = function(typedActionDict) {
|
||||
DAD._initTypeDict = function (typedActionDict) {
|
||||
Object.assign(wo, typedActionDict)
|
||||
}
|
||||
DAD.getTypedAction = function(type){
|
||||
DAD.getTypedAction = function (type) {
|
||||
return wo[type]
|
||||
}
|
||||
DAD.createTypedAction = function(action){
|
||||
DAD.createTypedAction = function (action) {
|
||||
return new wo[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) {
|
||||
DAD.buildUserAction = async function (action, keypair) {
|
||||
// Applicable on client. 客户端调用 Action.build,即可新建、并打包成一个完整的子事务,不需要亲自调用 constructor, packMe 等方法。
|
||||
if (
|
||||
action &&
|
||||
action.type &&
|
||||
keypair &&
|
||||
keypair.seckey &&
|
||||
keypair.pubkey &&
|
||||
ticrypto.seckey2pubkey(keypair.seckey) === keypair.pubkey
|
||||
) {
|
||||
let typedAction = new wo[action.type](action)
|
||||
typedAction.actorPubkey = keypair.pubkey
|
||||
if (typedAction.validateMe()) {
|
||||
@ -177,18 +205,26 @@ 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]) {
|
||||
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() && // 只检查所有事务通用的格式
|
||||
await typedAction.verifySig() &&
|
||||
typedAction.verifyHash() &&
|
||||
typedAction.validateMe() && // 检查事务的内容是否符合该子类事务的格式
|
||||
(await typedAction.executableMe()) // 检查事务是否可执行,在当前链的状态下。
|
||||
if (
|
||||
typedAction.verifyAddress() && // 只检查所有事务通用的格式
|
||||
(await typedAction.verifySig()) &&
|
||||
typedAction.verifyHash() &&
|
||||
typedAction.validateMe() && // 检查事务的内容是否符合该子类事务的格式
|
||||
(await typedAction.executableMe()) // 检查事务是否可执行,在当前链的状态下。
|
||||
) {
|
||||
DAD.actionPool[option.Action.hash] = typedAction
|
||||
DAD.actionPoolInfo.totalAmount += option.Action.amount || 0
|
||||
DAD.actionPoolInfo.totalFee += option.Action.fee || 0
|
||||
// wo.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
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
const Action = require('./Action.js')
|
||||
const ticCrypto = require('tic.crypto')
|
||||
const ticrypto = require('tic-crypto')
|
||||
|
||||
const DAD = module.exports = function ActionTransfer (prop) {
|
||||
const DAD = (module.exports = function ActionTransfer (prop) {
|
||||
this._class = this.constructor.name
|
||||
this.setProp(prop) // 没有定义 ActionTransfer.prototype._model,因此继承了上级Action.prototype._model,因此通过this.setProp,继承了上级Action定义的实例自有数据。另一个方案是,调用 Action.call(this, prop)
|
||||
this.type = this.constructor.name
|
||||
}
|
||||
})
|
||||
DAD.__proto__ = Action
|
||||
|
||||
const MOM = DAD.prototype
|
||||
@ -13,13 +13,19 @@ MOM.__proto__ = Action.prototype
|
||||
|
||||
MOM.validateMe = function () {
|
||||
// if (sender && sender.type !== 'multisig' && action.toAddress != action.actorAddress && sender.balance >= action.amount + action.fee){
|
||||
return this.actorPubkey && this.toAddress && ticCrypto.pubkey2address(this.actorPubkey)!== this.toAddress // 不能转帐给自己。
|
||||
&& this.amount && this.amount > 0 && (this.fee >= 0)
|
||||
return (
|
||||
this.actorPubkey &&
|
||||
this.toAddress &&
|
||||
ticrypto.pubkey2address(this.actorPubkey) !== this.toAddress && // 不能转帐给自己。
|
||||
this.amount &&
|
||||
this.amount > 0 &&
|
||||
this.fee >= 0
|
||||
)
|
||||
}
|
||||
|
||||
MOM.executableMe = async function() {
|
||||
MOM.executableMe = async function () {
|
||||
let balance = await wo.Store.getBalance(this.actorAddress)
|
||||
return balance >= this.amount + this.fee
|
||||
return balance >= this.amount + this.fee
|
||||
}
|
||||
|
||||
MOM.executeMe = async function () {
|
||||
|
@ -1,9 +1,9 @@
|
||||
{
|
||||
"name": "tic.action",
|
||||
"name": "tic-traction",
|
||||
"version": "0.1.0",
|
||||
"dependencies": {
|
||||
"so.ling": "git+https://git.faronear.org/so/so.ling",
|
||||
"tic.crypto": "git+https://git.faronear.org/tic/tic.crypto"
|
||||
"tic-crypto": "git+https://git.faronear.org/tic/tic-crypto"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"scripts": {},
|
||||
|
Loading…
Reference in New Issue
Block a user