This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsend_transaction.go
222 lines (202 loc) · 6.76 KB
/
send_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
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
package api
import (
"context"
"fmt"
"github.com/Worldcoin/hubble-commander/bls"
"github.com/Worldcoin/hubble-commander/encoder"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/dto"
"github.com/Worldcoin/hubble-commander/storage"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
)
var (
ErrFeeTooLow = fmt.Errorf("fee must be greater than 0")
// TODO: is there a way to merge these and tell you the expected nonce?
// see storage/error.go:24 NewNotFoundError
ErrNonceTooLow = fmt.Errorf("nonce too low")
ErrNonceTooHigh = fmt.Errorf("nonce too high")
ErrNotEnoughBalance = fmt.Errorf("not enough balance")
ErrTransferToSelf = fmt.Errorf("transfer to the same state id")
ErrInvalidAmount = fmt.Errorf("amount must be positive")
ErrUnsupportedTxType = fmt.Errorf("unsupported transaction type")
ErrNonexistentSender = fmt.Errorf("sender state ID does not exist")
ErrNonexistentReceiver = fmt.Errorf("receiver state ID does not exist")
ErrSpokeDoesNotExist = fmt.Errorf("spoke with given ID does not exist")
ErrAlreadyMinedTransaction = fmt.Errorf("transaction already mined")
ErrPendingTransaction = fmt.Errorf("transaction already exists")
ErrSendTxMethodDisabled = fmt.Errorf("commander instance is not accepting transactions")
APIErrAnyMissingField = NewAPIError(
10002,
"some field is missing, verify the transfer/create2transfer object",
)
APIErrTransferToSelf = NewAPIError(
10003,
"invalid recipient, cannot send funds to yourself",
)
APIErrNonceTooLow = NewAPIError(
10004,
"nonce too low",
)
APIErrNonceTooHigh = NewAPIError(
10005,
"nonce too high",
)
APIErrNotEnoughBalance = NewAPIError(
10006,
"not enough balance",
)
APIErrInvalidAmount = NewAPIError(
10007,
"amount must be greater than 0",
)
APIErrFeeTooLow = NewAPIError(
10008,
"fee too low",
)
APIErrInvalidSignature = NewAPIError(
10009,
"invalid signature",
)
APINotDecimalEncodableAmountError = NewAPIError(
10010,
"amount is not encodable as multi-precission decimal",
)
APINotDecimalEncodableFeeError = NewAPIError(
10011,
"fee is not encodable as multi-precission decimal",
)
APISenderDoesNotExistError = NewAPIError(
10012,
"sender with given ID does not exist",
)
APIReceiverDoesNotExistError = NewAPIError(
10013,
"receiver with given ID does not exist",
)
APIErrMinedTransaction = NewAPIError(
10014,
"cannot update mined transaction",
)
APIErrPendingTransaction = NewAPIError(
10015,
"transaction already exists",
)
APIErrSpokeDoesNotExist = NewAPIError(
10016,
"spoke with given ID does not exist",
)
APIErrSendTxMethodDisabled = NewAPIError(
10017,
"commander instance is not accepting transactions",
)
)
var sendTransactionAPIErrors = map[error]*APIError{
// TODO: something about this wrapping throws away information about _which_ field
// is missing
AnyMissingFieldError: APIErrAnyMissingField,
AnyInvalidSignatureError: APIErrInvalidSignature,
ErrNonexistentSender: APISenderDoesNotExistError,
ErrNonexistentReceiver: APIReceiverDoesNotExistError,
ErrTransferToSelf: APIErrTransferToSelf,
ErrNonceTooLow: APIErrNonceTooLow,
ErrNonceTooHigh: APIErrNonceTooHigh,
ErrNotEnoughBalance: APIErrNotEnoughBalance,
ErrInvalidAmount: APIErrInvalidAmount,
ErrFeeTooLow: APIErrFeeTooLow,
NewNotDecimalEncodableError("amount"): APINotDecimalEncodableAmountError,
NewNotDecimalEncodableError("fee"): APINotDecimalEncodableFeeError,
ErrSpokeDoesNotExist: APIErrSpokeDoesNotExist,
ErrAlreadyMinedTransaction: APIErrMinedTransaction,
ErrPendingTransaction: APIErrPendingTransaction,
ErrSendTxMethodDisabled: APIErrSendTxMethodDisabled,
}
func (a *API) SendTransaction(ctx context.Context, tx dto.Transaction) (*common.Hash, error) {
if !a.isAcceptingTransactions {
return nil, sanitizeError(ErrSendTxMethodDisabled, sendTransactionAPIErrors)
}
transactionHash, err := a.unsafeSendTransaction(ctx, tx)
if err != nil {
return nil, sanitizeError(err, sendTransactionAPIErrors)
}
return transactionHash, nil
}
func (a *API) unsafeSendTransaction(ctx context.Context, tx dto.Transaction) (*common.Hash, error) {
switch t := tx.Parsed.(type) {
case dto.Transfer:
return a.handleTransfer(ctx, t)
case dto.Create2Transfer:
return a.handleCreate2Transfer(ctx, t)
case dto.MassMigration:
return a.handleMassMigration(t)
default:
return nil, errors.WithStack(ErrUnsupportedTxType)
}
}
func validateAmount(amount *models.Uint256) error {
_, err := encoder.EncodeDecimal(*amount)
if err != nil {
return errors.WithStack(NewNotDecimalEncodableError("amount"))
}
if amount.CmpN(0) <= 0 {
return errors.WithStack(ErrInvalidAmount)
}
return nil
}
func validateFee(fee *models.Uint256) error {
if fee.CmpN(0) != 1 {
return errors.WithStack(ErrFeeTooLow)
}
_, err := encoder.EncodeDecimal(*fee)
if err != nil {
return errors.WithStack(NewNotDecimalEncodableError("fee"))
}
return nil
}
func validateNonce(txStorage *storage.Storage, transaction *models.TransactionBase, senderStateID uint32) error {
senderNonce, err := txStorage.GetPendingNonce(senderStateID)
if err != nil {
return err
}
if transaction.Nonce.Cmp(senderNonce) < 0 {
return errors.WithStack(ErrNonceTooLow)
}
if transaction.Nonce.Cmp(senderNonce) > 0 {
return errors.WithStack(ErrNonceTooHigh)
}
return nil
}
func validateBalance(txStorage *storage.Storage, transactionAmount, transactionFee *models.Uint256, senderStateID uint32) error {
senderBalance, err := txStorage.GetPendingBalance(senderStateID)
if err != nil {
return err
}
if transactionAmount.Add(transactionFee).Cmp(senderBalance) > 0 {
return errors.WithStack(ErrNotEnoughBalance)
}
return nil
}
func validateSignature(
txStorage *storage.Storage,
encodedTransaction []byte,
transactionSignature *models.Signature,
senderState *models.UserState,
domain *bls.Domain,
) error {
senderAccount, err := txStorage.AccountTree.Leaf(senderState.PubKeyID)
if err != nil {
return errors.WithStack(NewInvalidSignatureError(err.Error()))
}
signature, err := bls.NewSignatureFromBytes(transactionSignature.Bytes(), *domain)
if err != nil {
return errors.WithStack(NewInvalidSignatureError(err.Error()))
}
isValid, err := signature.Verify(encodedTransaction, &senderAccount.PublicKey)
if err != nil {
return errors.WithStack(NewInvalidSignatureError(err.Error()))
}
if !isValid {
return errors.WithStack(NewInvalidSignatureError("the signature hasn't passed the verification process"))
}
return nil
}