-
Notifications
You must be signed in to change notification settings - Fork 199
/
transactionType.go
142 lines (117 loc) · 3.62 KB
/
transactionType.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
package coordinator
import (
"bytes"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
)
type txTypeHandler struct {
pubkeyConv state.PubkeyConverter
shardCoordinator sharding.Coordinator
builtInFuncNames map[string]struct{}
argumentParser process.ArgumentsParser
}
// ArgNewTxTypeHandler defines the arguments needed to create a new tx type handler
type ArgNewTxTypeHandler struct {
PubkeyConverter state.PubkeyConverter
ShardCoordinator sharding.Coordinator
BuiltInFuncNames map[string]struct{}
ArgumentParser process.ArgumentsParser
}
// NewTxTypeHandler creates a transaction type handler
func NewTxTypeHandler(
args ArgNewTxTypeHandler,
) (*txTypeHandler, error) {
if check.IfNil(args.PubkeyConverter) {
return nil, process.ErrNilPubkeyConverter
}
if check.IfNil(args.ShardCoordinator) {
return nil, process.ErrNilShardCoordinator
}
if check.IfNil(args.ArgumentParser) {
return nil, process.ErrNilArgumentParser
}
if args.BuiltInFuncNames == nil {
return nil, process.ErrNilBuiltInFunction
}
tc := &txTypeHandler{
pubkeyConv: args.PubkeyConverter,
shardCoordinator: args.ShardCoordinator,
argumentParser: args.ArgumentParser,
builtInFuncNames: args.BuiltInFuncNames,
}
return tc, nil
}
// ComputeTransactionType calculates the transaction type
func (tth *txTypeHandler) ComputeTransactionType(tx data.TransactionHandler) (process.TransactionType, error) {
err := tth.checkTxValidity(tx)
if err != nil {
return process.InvalidTransaction, err
}
isEmptyAddress := tth.isDestAddressEmpty(tx)
if isEmptyAddress {
if len(tx.GetData()) > 0 {
return process.SCDeployment, nil
}
return process.InvalidTransaction, process.ErrWrongTransaction
}
isDestInSelfShard, err := tth.isDestAddressInSelfShard(tx.GetRcvAddr())
if err != nil {
return process.InvalidTransaction, err
}
if !isDestInSelfShard || len(tx.GetData()) == 0 {
return process.MoveBalance, nil
}
if core.IsSmartContractAddress(tx.GetRcvAddr()) || tth.isBuiltInFunctionCall(tx.GetData()) {
return process.SCInvoking, nil
}
return process.MoveBalance, nil
}
func (tth *txTypeHandler) isBuiltInFunctionCall(txData []byte) bool {
if len(tth.builtInFuncNames) == 0 {
return false
}
err := tth.argumentParser.ParseData(string(txData))
if err != nil {
return false
}
function, err := tth.argumentParser.GetFunction()
if err != nil {
return false
}
_, ok := tth.builtInFuncNames[function]
return ok
}
func (tth *txTypeHandler) isDestAddressEmpty(tx data.TransactionHandler) bool {
isEmptyAddress := bytes.Equal(tx.GetRcvAddr(), make([]byte, tth.pubkeyConv.Len()))
return isEmptyAddress
}
func (tth *txTypeHandler) isDestAddressInSelfShard(address []byte) (bool, error) {
adrSrc, err := tth.pubkeyConv.CreateAddressFromBytes(address)
if err != nil {
return false, err
}
shardForCurrentNode := tth.shardCoordinator.SelfId()
shardForSrc := tth.shardCoordinator.ComputeId(adrSrc)
if shardForCurrentNode != shardForSrc {
return false, nil
}
return true, nil
}
func (tth *txTypeHandler) checkTxValidity(tx data.TransactionHandler) error {
if check.IfNil(tx) {
return process.ErrNilTransaction
}
recvAddressIsInvalid := tth.pubkeyConv.Len() != len(tx.GetRcvAddr())
if recvAddressIsInvalid {
return process.ErrWrongTransaction
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (tth *txTypeHandler) IsInterfaceNil() bool {
return tth == nil
}