/
ticket.go
217 lines (187 loc) · 5.82 KB
/
ticket.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// 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 是一个货币的exec。内置货币的执行器。
主要提供两种操作:
EventTransfer -> 转移资产
*/
//package none execer for unknow execer
//all none transaction exec ok, execept nofee
//nofee transaction will not pack into block
import (
"bytes"
"fmt"
log "github.com/33cn/chain33/common/log/log15"
drivers "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
ty "github.com/33cn/plugin/plugin/dapp/ticket/types"
)
var clog = log.New("module", "execs.ticket")
var driverName = "ticket"
// Init initial
func Init(name string, cfg *types.Chain33Config, sub []byte) {
drivers.Register(cfg, GetName(), newTicket, cfg.GetDappFork(driverName, "Enable"))
drivers.RegisterKVExpiredChecker(ty.TicketX, expiredKVChecker)
InitExecType()
}
//InitExecType ...
func InitExecType() {
ety := types.LoadExecutorType(driverName)
ety.InitFuncList(types.ListMethod(&Ticket{}))
}
// GetName get name
func GetName() string {
return newTicket().GetName()
}
// Ticket driver type
type Ticket struct {
drivers.DriverBase
}
func newTicket() drivers.Driver {
t := &Ticket{}
t.SetChild(t)
t.SetExecutorType(types.LoadExecutorType(driverName))
return t
}
// GetDriverName ...
func (t *Ticket) GetDriverName() string {
return driverName
}
func (t *Ticket) saveTicketBind(b *ty.ReceiptTicketBind) (kvs []*types.KeyValue) {
//解除原来的绑定
if len(b.OldMinerAddress) > 0 {
kv := &types.KeyValue{
Key: calcBindMinerKey(b.OldMinerAddress, b.ReturnAddress),
Value: nil,
}
//tlog.Warn("tb:del", "key", string(kv.Key))
kvs = append(kvs, kv)
}
kv := &types.KeyValue{Key: calcBindReturnKey(b.ReturnAddress), Value: []byte(b.NewMinerAddress)}
//tlog.Warn("tb:add", "key", string(kv.Key), "value", string(kv.Value))
kvs = append(kvs, kv)
kv = &types.KeyValue{
Key: calcBindMinerKey(b.GetNewMinerAddress(), b.ReturnAddress),
Value: []byte(b.ReturnAddress),
}
//tlog.Warn("tb:add", "key", string(kv.Key), "value", string(kv.Value))
kvs = append(kvs, kv)
return kvs
}
func (t *Ticket) delTicketBind(b *ty.ReceiptTicketBind) (kvs []*types.KeyValue) {
//被取消了,刚好操作反
kv := &types.KeyValue{
Key: calcBindMinerKey(b.NewMinerAddress, b.ReturnAddress),
Value: nil,
}
kvs = append(kvs, kv)
if len(b.OldMinerAddress) > 0 {
//恢复旧的绑定
kv := &types.KeyValue{Key: calcBindReturnKey(b.ReturnAddress), Value: []byte(b.OldMinerAddress)}
kvs = append(kvs, kv)
kv = &types.KeyValue{
Key: calcBindMinerKey(b.OldMinerAddress, b.ReturnAddress),
Value: []byte(b.ReturnAddress),
}
kvs = append(kvs, kv)
} else {
//删除旧的数据
kv := &types.KeyValue{Key: calcBindReturnKey(b.ReturnAddress), Value: nil}
kvs = append(kvs, kv)
}
return kvs
}
func (t *Ticket) saveTicket(ticketlog *ty.ReceiptTicket) (kvs []*types.KeyValue) {
if ticketlog.PrevStatus > 0 {
kv := delticket(ticketlog.Addr, ticketlog.TicketId, ticketlog.PrevStatus)
kvs = append(kvs, kv)
}
kvs = append(kvs, addticket(ticketlog.Addr, ticketlog.TicketId, ticketlog.Status))
return kvs
}
func (t *Ticket) delTicket(ticketlog *ty.ReceiptTicket) (kvs []*types.KeyValue) {
if ticketlog.PrevStatus > 0 {
kv := addticket(ticketlog.Addr, ticketlog.TicketId, ticketlog.PrevStatus)
kvs = append(kvs, kv)
}
kvs = append(kvs, delticket(ticketlog.Addr, ticketlog.TicketId, ticketlog.Status))
return kvs
}
func calcTicketKey(addr string, ticketID string, status int32) []byte {
key := fmt.Sprintf("LODB-ticket-tl:%s:%d:%s", addr, status, ticketID)
return []byte(key)
}
func calcBindReturnKey(returnAddress string) []byte {
key := fmt.Sprintf("LODB-ticket-bind:%s", returnAddress)
return []byte(key)
}
func calcBindMinerKey(minerAddress string, returnAddress string) []byte {
key := fmt.Sprintf("LODB-ticket-miner:%s:%s", minerAddress, returnAddress)
return []byte(key)
}
func calcBindMinerKeyPrefix(minerAddress string) []byte {
key := fmt.Sprintf("LODB-ticket-miner:%s", minerAddress)
return []byte(key)
}
func calcTicketPrefix(addr string, status int32) []byte {
key := fmt.Sprintf("LODB-ticket-tl:%s:%d", addr, status)
return []byte(key)
}
func addticket(addr string, ticketID string, status int32) *types.KeyValue {
kv := &types.KeyValue{}
kv.Key = calcTicketKey(addr, ticketID, status)
kv.Value = []byte(ticketID)
return kv
}
func delticket(addr string, ticketID string, status int32) *types.KeyValue {
kv := &types.KeyValue{}
kv.Key = calcTicketKey(addr, ticketID, status)
kv.Value = nil
return kv
}
// IsFriend check is fri
func (t *Ticket) IsFriend(myexec, writekey []byte, tx *types.Transaction) bool {
clog.Error("ticket IsFriend", "myex", string(myexec), "writekey", string(writekey))
//不允许平行链
return false
}
// CheckTx check tx
func (t *Ticket) CheckTx(tx *types.Transaction, index int) error {
//index == -1 only when check in mempool
if index == -1 {
var action ty.TicketAction
err := types.Decode(tx.Payload, &action)
if err != nil {
return err
}
if action.Ty == ty.TicketActionMiner && action.GetMiner() != nil {
return ty.ErrMinerTx
}
}
return nil
}
// CheckReceiptExecOk return true to check if receipt ty is ok
func (t *Ticket) CheckReceiptExecOk() bool {
return true
}
// 自定义接口,用于删除不再需要保存的kv
// 比如 ticket 已经 close 之后就废弃了,可以删除
func expiredKVChecker(key, value []byte) bool {
// 由于 ticketBindKeyPrefix 包含了 ticketKeyPrefix,所以需要多做一次检查
if bytes.HasPrefix(key, ticketBindKeyPrefix) {
return false
}
if !bytes.HasPrefix(key, ticketKeyPrefix) {
return false
}
var tk ty.Ticket
if err := types.Decode(value, &tk); err != nil {
return false
}
if tk.Status == ty.TicketClosed {
return true
}
return false
}