/
transaction.go
129 lines (109 loc) · 3.05 KB
/
transaction.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
package equality
import (
"errors"
"reflect"
"strings"
"github.com/SecretBlockChain/go-secret/common"
"github.com/SecretBlockChain/go-secret/core/types"
)
// Transaction custom transaction interface.
type Transaction interface {
Type() TransactionType
Action() string
Decode(*types.Transaction, []byte) error
}
// TransactionType custom transaction type enums.
type TransactionType string
const (
EventTransactionType TransactionType = "event"
)
var (
prototypes = []Transaction{
new(EventBecomeCandidate),
new(EventCancelCandidate),
}
prototypeMapper = map[TransactionType][]Transaction{}
)
func init() {
for _, prototype := range prototypes {
slice, _ := prototypeMapper[prototype.Type()]
prototypeMapper[prototype.Type()] = append(slice, prototype)
}
}
// NewTransaction new custom transaction from transaction data.
// data format: equality:version:type:action:data
func NewTransaction(tx *types.Transaction) (Transaction, error) {
slice := strings.Split(string(tx.Data()), ":")
if len(slice) < 4 {
return nil, errors.New("invalid custom transaction data")
}
prefix, version, txType, action := slice[0], slice[1], TransactionType(slice[2]), slice[3]
if prefix != "equality" {
return nil, errors.New("invalid custom transaction prefix")
}
if version != "1" {
return nil, errors.New("invalid custom transaction version")
}
types, ok := prototypeMapper[txType]
if !ok {
return nil, errors.New("undefined custom transaction type")
}
var data []byte
if len(slice) > 4 {
data = []byte(strings.Join(slice[4:], ":"))
}
for _, typ := range types {
if typ.Action() == action {
t := reflect.TypeOf(typ)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
ctx := reflect.New(t).Interface().(Transaction)
if err := ctx.Decode(tx, data); err != nil {
return nil, err
}
return ctx, nil
}
}
return nil, errors.New("undefined custom transaction action")
}
// EventBecomeCandidate apply to become Candidate.
// data like "equality:1:event:candidate"
// Sender will become a Candidate
type EventBecomeCandidate struct {
Candidate common.Address
}
func (event *EventBecomeCandidate) Type() TransactionType {
return EventTransactionType
}
func (event *EventBecomeCandidate) Action() string {
return "candidate"
}
func (event *EventBecomeCandidate) Decode(tx *types.Transaction, data []byte) error {
txSender, err := types.Sender(types.NewEIP155Signer(tx.ChainId()), tx)
if err != nil {
return err
}
event.Candidate = txSender
return nil
}
// EventCancelCandidate apply to cancel Candidate.
// data like "equality:1:event:delegator"
// Sender will cancel Candidate status
type EventCancelCandidate struct {
Delegator common.Address
}
func (event *EventCancelCandidate) Type() TransactionType {
return EventTransactionType
}
func (event *EventCancelCandidate) Action() string {
return "delegator"
}
func (event *EventCancelCandidate) Decode(tx *types.Transaction, data []byte) error {
txSender, err := types.Sender(types.NewEIP155Signer(tx.ChainId()), tx)
if err != nil {
return err
}
event.Delegator = txSender
return nil
}