-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
227 lines (204 loc) · 5.84 KB
/
types.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
218
219
220
221
222
223
224
225
226
227
// 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 types
import (
"encoding/json"
"reflect"
"github.com/bnchain/bnchain/common/address"
log "github.com/bnchain/bnchain/common/log/log15"
"github.com/bnchain/bnchain/types"
)
var tlog = log.New("module", GameX)
func init() {
// init executor type
types.AllowUserExec = append(types.AllowUserExec, []byte(GameX))
types.RegistorExecutor(GameX, NewType())
types.RegisterDappFork(GameX, "Enable", 0)
}
//getRealExecName
//如果paraName == "", 那么自动用 types.ExecName("game")
//如果设置了paraName , 那么强制用paraName
//也就是说,我们可以构造其他平行链的交易
func getRealExecName(paraName string) string {
return types.ExecName(paraName + GameX)
}
// NewType new type
func NewType() *GameType {
c := &GameType{}
c.SetChild(c)
return c
}
// GameType execType
type GameType struct {
types.ExecTypeBase
}
// GetName 获取执行器名称
func (gt *GameType) GetName() string {
return GameX
}
// GetLogMap get log
func (gt *GameType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{
TyLogCreateGame: {Ty: reflect.TypeOf(ReceiptGame{}), Name: "LogCreateGame"},
TyLogCancleGame: {Ty: reflect.TypeOf(ReceiptGame{}), Name: "LogCancleGame"},
TyLogMatchGame: {Ty: reflect.TypeOf(ReceiptGame{}), Name: "LogMatchGame"},
TyLogCloseGame: {Ty: reflect.TypeOf(ReceiptGame{}), Name: "LogCloseGame"},
}
}
// GetPayload get payload
func (gt *GameType) GetPayload() types.Message {
return &GameAction{}
}
// GetTypeMap get typeMap
func (gt *GameType) GetTypeMap() map[string]int32 {
return map[string]int32{
"Create": GameActionCreate,
"Cancel": GameActionCancel,
"Close": GameActionClose,
"Match": GameActionMatch,
}
}
// CreateTx unused,just empty implementation
func (gt GameType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
tlog.Debug("Game.CreateTx", "action", action)
if action == ActionCreateGame {
var param GamePreCreateTx
err := json.Unmarshal(message, ¶m)
if err != nil {
tlog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawGamePreCreateTx(¶m)
} else if action == ActionMatchGame {
var param GamePreMatchTx
err := json.Unmarshal(message, ¶m)
if err != nil {
tlog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawGamePreMatchTx(¶m)
} else if action == ActionCancelGame {
var param GamePreCancelTx
err := json.Unmarshal(message, ¶m)
if err != nil {
tlog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawGamePreCancelTx(¶m)
} else if action == ActionCloseGame {
var param GamePreCloseTx
err := json.Unmarshal(message, ¶m)
if err != nil {
tlog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawGamePreCloseTx(¶m)
}
return nil, types.ErrNotSupport
}
// CreateRawGamePreCreateTx unused,just empty implementation
func CreateRawGamePreCreateTx(parm *GamePreCreateTx) (*types.Transaction, error) {
if parm == nil {
tlog.Error("CreateRawGamePreCreateTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &GameCreate{
Value: parm.Amount,
HashType: parm.HashType,
HashValue: parm.HashValue,
}
precreate := &GameAction{
Ty: GameActionCreate,
Value: &GameAction_Create{v},
}
tx := &types.Transaction{
Execer: []byte(getRealExecName(types.GetParaName())),
Payload: types.Encode(precreate),
Fee: parm.Fee,
To: address.ExecAddress(getRealExecName(types.GetParaName())),
}
name := getRealExecName(types.GetParaName())
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawGamePreMatchTx unused,just empty implementation
func CreateRawGamePreMatchTx(parm *GamePreMatchTx) (*types.Transaction, error) {
if parm == nil {
return nil, types.ErrInvalidParam
}
v := &GameMatch{
GameId: parm.GameID,
Guess: parm.Guess,
}
game := &GameAction{
Ty: GameActionMatch,
Value: &GameAction_Match{v},
}
tx := &types.Transaction{
Execer: []byte(getRealExecName(types.GetParaName())),
Payload: types.Encode(game),
Fee: parm.Fee,
To: address.ExecAddress(getRealExecName(types.GetParaName())),
}
name := getRealExecName(types.GetParaName())
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawGamePreCancelTx unused,just empty implementation
func CreateRawGamePreCancelTx(parm *GamePreCancelTx) (*types.Transaction, error) {
if parm == nil {
return nil, types.ErrInvalidParam
}
v := &GameCancel{
GameId: parm.GameID,
}
cancel := &GameAction{
Ty: GameActionCancel,
Value: &GameAction_Cancel{v},
}
tx := &types.Transaction{
Execer: []byte(getRealExecName(types.GetParaName())),
Payload: types.Encode(cancel),
Fee: parm.Fee,
To: address.ExecAddress(getRealExecName(types.GetParaName())),
}
name := getRealExecName(types.GetParaName())
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawGamePreCloseTx unused,just empty implementation
func CreateRawGamePreCloseTx(parm *GamePreCloseTx) (*types.Transaction, error) {
if parm == nil {
return nil, types.ErrInvalidParam
}
v := &GameClose{
GameId: parm.GameID,
Secret: parm.Secret,
}
close := &GameAction{
Ty: GameActionClose,
Value: &GameAction_Close{v},
}
tx := &types.Transaction{
Execer: []byte(getRealExecName(types.GetParaName())),
Payload: types.Encode(close),
Fee: parm.Fee,
To: address.ExecAddress(getRealExecName(types.GetParaName())),
}
name := getRealExecName(types.GetParaName())
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}