This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
payload.go
135 lines (114 loc) · 2.59 KB
/
payload.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
package payload
import (
"fmt"
"strings"
)
/*
Payload (Transaction) is an atomic operation on the ledger state.
Account Txs:
- SendTx Send coins to address
- CallTx Send a msg to a contract that runs in the vm
- NameTx Store some value under a name in the global namereg
Validation Txs:
- BondTx New validator posts a bond
- UnbondTx Validator leaves
Admin Txs:
- PermsTx
*/
type Type uint32
// Types of Payload implementations
const (
TypeUnknown = Type(0x00)
// Account transactions
TypeSend = Type(0x01)
TypeCall = Type(0x02)
TypeName = Type(0x03)
TypeBatch = Type(0x04)
// Validation transactions
TypeBond = Type(0x11)
TypeUnbond = Type(0x12)
// Admin transactions
TypePermissions = Type(0x21)
TypeGovernance = Type(0x22)
TypeProposal = Type(0x23)
)
type Payload interface {
String() string
GetInputs() []*TxInput
Type() Type
Any() *Any
// The serialised size in bytes
Size() int
}
var nameFromType = map[Type]string{
TypeUnknown: "UnknownTx",
TypeSend: "SendTx",
TypeCall: "CallTx",
TypeName: "NameTx",
TypeBatch: "BatchTx",
TypePermissions: "PermsTx",
TypeGovernance: "GovTx",
TypeProposal: "ProposalTx",
TypeBond: "BondTx",
TypeUnbond: "UnbondTx",
}
var typeFromName = make(map[string]Type)
func init() {
for t, n := range nameFromType {
typeFromName[n] = t
}
}
func TxTypeFromString(name string) Type {
return typeFromName[name]
}
func (typ Type) String() string {
name, ok := nameFromType[typ]
if ok {
return name
}
return "UnknownTx"
}
func (typ Type) MarshalText() ([]byte, error) {
return []byte(typ.String()), nil
}
func (typ *Type) UnmarshalText(data []byte) error {
*typ = TxTypeFromString(string(data))
return nil
}
// Protobuf support
func (typ Type) Marshal() ([]byte, error) {
return typ.MarshalText()
}
func (typ *Type) Unmarshal(data []byte) error {
return typ.UnmarshalText(data)
}
func InputsString(inputs []*TxInput) string {
strs := make([]string, len(inputs))
for i, in := range inputs {
strs[i] = in.Address.String()
}
return strings.Join(strs, ",")
}
func New(txType Type) (Payload, error) {
switch txType {
case TypeSend:
return &SendTx{}, nil
case TypeCall:
return &CallTx{}, nil
case TypeName:
return &NameTx{}, nil
case TypeBatch:
return &BatchTx{}, nil
case TypePermissions:
return &PermsTx{}, nil
case TypeGovernance:
return &GovTx{}, nil
case TypeBond:
return &BondTx{}, nil
case TypeUnbond:
return &UnbondTx{}, nil
case TypeProposal:
return &ProposalTx{}, nil
}
return nil, fmt.Errorf("unknown payload type: %d", txType)
}