forked from 33cn/chain33
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coins.go
92 lines (77 loc) · 2.23 KB
/
coins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package executor coins执行器
package executor
/*
coins 是一个货币的exec。内置货币的执行器。
主要提供两种操作:
EventTransfer -> 转移资产
*/
// package none execer for unknow execer
// all none transaction exec ok, execept nofee
// nofee transaction will not pack into block
import (
drivers "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
)
// var clog = log.New("module", "execs.coins")
var driverName = "coins"
// Init defines a register function
func Init(name string, sub []byte) {
if name != driverName {
panic("system dapp can't be rename")
}
drivers.Register(driverName, newCoins, types.GetDappFork(driverName, "Enable"))
}
// the initialization process is relatively heavyweight, lots of reflact, so it's global
func init() {
ety := types.LoadExecutorType(driverName)
ety.InitFuncList(types.ListMethod(&Coins{}))
}
// GetName return name string
func GetName() string {
return newCoins().GetName()
}
// Coins defines coins
type Coins struct {
drivers.DriverBase
}
func newCoins() drivers.Driver {
c := &Coins{}
c.SetChild(c)
c.SetExecutorType(types.LoadExecutorType(driverName))
return c
}
// GetDriverName get drive name
func (c *Coins) GetDriverName() string {
return driverName
}
// CheckTx check transaction amount 必须不能为负数
func (c *Coins) CheckTx(tx *types.Transaction, index int) error {
ety := c.GetExecutorType()
amount, err := ety.Amount(tx)
if err != nil {
return err
}
if amount < 0 {
return types.ErrAmount
}
return nil
}
// IsFriend coins contract the mining transaction that runs the ticket contract
func (c *Coins) IsFriend(myexec, writekey []byte, othertx *types.Transaction) bool {
//step1 先判定自己合约的权限
if !c.AllowIsSame(myexec) {
return false
}
//step2 判定 othertx 的 执行器名称(只允许主链,并且是挖矿的行为)
if string(othertx.Execer) == "ticket" && othertx.ActionName() == "miner" {
return true
}
return false
}
// CheckReceiptExecOk return true to check if receipt ty is ok
func (c *Coins) CheckReceiptExecOk() bool {
return true
}