-
Notifications
You must be signed in to change notification settings - Fork 254
/
types.go
159 lines (140 loc) · 3.95 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
// 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 (
"reflect"
"github.com/33cn/chain33/types"
)
const (
// CoinsActionTransfer defines const number
CoinsActionTransfer = 1
// CoinsActionGenesis defines const coinsactiongenesis number
CoinsActionGenesis = 2
// CoinsActionWithdraw defines const number coinsactionwithdraw
CoinsActionWithdraw = 3
// CoinsActionTransferToExec defines const number coinsactiontransfertoExec
CoinsActionTransferToExec = 10
)
var (
// CoinsX defines a global string
CoinsX = "coins"
// ExecerCoins execer coins
ExecerCoins = []byte(CoinsX)
actionName = map[string]int32{
"Transfer": CoinsActionTransfer,
"TransferToExec": CoinsActionTransferToExec,
"Withdraw": CoinsActionWithdraw,
"Genesis": CoinsActionGenesis,
}
logmap = make(map[int64]*types.LogInfo)
)
func init() {
types.AllowUserExec = append(types.AllowUserExec, ExecerCoins)
types.RegFork(CoinsX, InitFork)
types.RegExec(CoinsX, InitExecutor)
}
func InitFork(cfg *types.Chain33Config) {
cfg.RegisterDappFork(CoinsX, "Enable", 0)
}
func InitExecutor(cfg *types.Chain33Config) {
types.RegistorExecutor(CoinsX, NewType(cfg))
}
// CoinsType defines exec type
type CoinsType struct {
types.ExecTypeBase
}
// NewType new coinstype
func NewType(cfg *types.Chain33Config) *CoinsType {
c := &CoinsType{}
c.SetChild(c)
c.SetConfig(cfg)
return c
}
// GetPayload return payload
func (c *CoinsType) GetPayload() types.Message {
return &CoinsAction{}
}
// GetName return coins string
func (c *CoinsType) GetName() string {
return CoinsX
}
// GetLogMap return log for map
func (c *CoinsType) GetLogMap() map[int64]*types.LogInfo {
return logmap
}
// GetTypeMap return actionname for map
func (c *CoinsType) GetTypeMap() map[string]int32 {
return actionName
}
//DecodePayloadValue 为了性能考虑,coins 是最常用的合约,我们这里不用反射吗,做了特殊化的优化
func (c *CoinsType) DecodePayloadValue(tx *types.Transaction) (string, reflect.Value, error) {
name, value, err := c.decodePayloadValue(tx)
return name, value, err
}
func (c *CoinsType) decodePayloadValue(tx *types.Transaction) (string, reflect.Value, error) {
var action CoinsAction
if tx.GetPayload() == nil {
return "", reflect.ValueOf(nil), types.ErrActionNotSupport
}
err := types.Decode(tx.Payload, &action)
if err != nil {
return "", reflect.ValueOf(nil), err
}
var name string
var value types.Message
switch action.Ty {
case CoinsActionTransfer:
name = "Transfer"
value = action.GetTransfer()
case CoinsActionTransferToExec:
name = "TransferToExec"
value = action.GetTransferToExec()
case CoinsActionWithdraw:
name = "Withdraw"
value = action.GetWithdraw()
case CoinsActionGenesis:
name = "Genesis"
value = action.GetGenesis()
}
if value == nil {
return "", reflect.ValueOf(nil), types.ErrActionNotSupport
}
return name, reflect.ValueOf(value), nil
}
// RPC_Default_Process default process fo rpc
func (c *CoinsType) RPC_Default_Process(action string, msg interface{}) (*types.Transaction, error) {
var create *types.CreateTx
if _, ok := msg.(*types.CreateTx); !ok {
return nil, types.ErrInvalidParam
}
create = msg.(*types.CreateTx)
if create.IsToken {
return nil, types.ErrNotSupport
}
tx, err := c.AssertCreate(create)
if err != nil {
return nil, err
}
//to地址的问题,如果是主链交易,to地址就是直接是设置to
types := c.GetConfig()
if !types.IsPara() {
tx.To = create.To
}
return tx, err
}
// GetAssets return asset list
func (c *CoinsType) GetAssets(tx *types.Transaction) ([]*types.Asset, error) {
assets, err := c.ExecTypeBase.GetAssets(tx)
if err != nil || len(assets) == 0 {
return nil, err
}
if assets[0].Symbol == "" {
types := c.GetConfig()
assets[0].Symbol = types.GetCoinSymbol()
}
if assets[0].Symbol == "bty" {
assets[0].Symbol = "BTY"
}
return assets, nil
}