-
Notifications
You must be signed in to change notification settings - Fork 9
/
transaction.go
332 lines (277 loc) · 9.71 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package rpc
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/filefilego/filefilego/common/hexutil"
"github.com/filefilego/filefilego/keystore"
"github.com/filefilego/filefilego/node/protocols/messages"
"github.com/filefilego/filefilego/transaction"
"google.golang.org/protobuf/proto"
)
// Blockchain defines the blockchain functionality needed for this rpc service,
type Blockchain interface {
PutMemPool(tx transaction.Transaction) error
GetTransactionsFromPool() []transaction.Transaction
GetAddressTransactions(address []byte) ([]transaction.Transaction, []uint64, error)
GetTransactionByHash(hash []byte) ([]transaction.Transaction, []uint64, error)
}
// NetworkMessagePublisher is a pub sub message broadcaster.
type NetworkMessagePublisher interface {
PublishMessageToNetwork(ctx context.Context, data []byte) error
}
// TransactionsResponse represents a response with block and transaction
type TransactionsResponse struct {
Transactions []JSONBlockTransaction `json:"transactions"`
}
// TransactionResponse represents a response with a transaction.
type TransactionResponse struct {
Transaction JSONTransaction `json:"transaction"`
}
// JSONBlockTransaction represnts the block number and a transaction.
type JSONBlockTransaction struct {
BlockNumber uint64 `json:"block_number"`
Transaction JSONTransaction `json:"transaction"`
}
// JSONTransaction represents a json transaction.
type JSONTransaction struct {
Hash string `json:"hash"`
Signature string `json:"signature"`
PublicKey string `json:"public_key"`
Nounce string `json:"nounce"`
Data string `json:"data"`
From string `json:"from"`
To string `json:"to"`
Value string `json:"value"`
TransactionFees string `json:"transaction_fees"`
Chain string `json:"chain"`
}
// TransactionAPI represents the transaction rpc service.
type TransactionAPI struct {
keystore keystore.KeyAuthorizer
publisher NetworkMessagePublisher
blockchain Blockchain
superLightNode bool
}
// NewTransactionAPI creates a new transaction API to be served using JSONRPC.
func NewTransactionAPI(keystore keystore.KeyAuthorizer, publisher NetworkMessagePublisher, blockchain Blockchain, superLightNode bool) (*TransactionAPI, error) {
if keystore == nil {
return nil, errors.New("keystore is nil")
}
if publisher == nil {
return nil, errors.New("publisher is nil")
}
if blockchain == nil {
return nil, errors.New("blockchain is nil")
}
return &TransactionAPI{
keystore: keystore,
publisher: publisher,
blockchain: blockchain,
superLightNode: superLightNode,
}, nil
}
// SendRawTransactionArgs is a raw transaction sent by clients.
type SendRawTransactionArgs struct {
RawTransaction string `json:"raw_transaction"`
}
// SendRawTransaction sends a raw transaction.
func (api *TransactionAPI) SendRawTransaction(r *http.Request, args *SendRawTransactionArgs, response *TransactionResponse) error {
jsonTX := JSONTransaction{}
if err := json.Unmarshal([]byte(args.RawTransaction), &jsonTX); err != nil {
return fmt.Errorf("failed to unmarshal transaction: %w", err)
}
txHash, err := hexutil.Decode(jsonTX.Hash)
if err != nil {
return fmt.Errorf("failed to decode transaction hash: %w", err)
}
txSig, err := hexutil.Decode(jsonTX.Signature)
if err != nil {
return fmt.Errorf("failed to decode transaction signature: %w", err)
}
txPublicKey, err := hexutil.Decode(jsonTX.PublicKey)
if err != nil {
return fmt.Errorf("failed to decode transaction public key: %w", err)
}
txNounce, err := hexutil.DecodeUint64(jsonTX.Nounce)
if err != nil {
return fmt.Errorf("failed to decode transaction nounce: %w", err)
}
txNounceBytes := hexutil.EncodeUint64ToBytes(txNounce)
txData, err := hexutil.Decode(jsonTX.Data)
if err != nil {
return fmt.Errorf("failed to decode transaction nounce: %w", err)
}
txChain, err := hexutil.Decode(jsonTX.Chain)
if err != nil {
return fmt.Errorf("failed to decode transaction chain: %w", err)
}
tx := transaction.Transaction{
Hash: txHash,
Signature: txSig,
PublicKey: txPublicKey,
Nounce: txNounceBytes,
Data: txData,
From: jsonTX.From,
To: jsonTX.To,
Value: jsonTX.Value,
TransactionFees: jsonTX.TransactionFees,
Chain: txChain,
}
return api.validateBroadcastTxSetResponse(r.Context(), &tx, response)
}
func (api *TransactionAPI) validateBroadcastTxSetResponse(ctx context.Context, tx *transaction.Transaction, response *TransactionResponse) error {
ok, err := tx.Validate()
if err != nil || !ok {
return fmt.Errorf("failed to validate transaction: %w", err)
}
if !api.superLightNode {
if err := api.blockchain.PutMemPool(*tx); err != nil {
return fmt.Errorf("failed to insert transaction from rpc method to mempool: %w", err)
}
}
payload := messages.GossipPayload{
Message: &messages.GossipPayload_Transaction{
Transaction: transaction.ToProtoTransaction(*tx),
},
}
txBytes, err := proto.Marshal(&payload)
if err != nil {
return fmt.Errorf("failed to marshal gossip payload: %w", err)
}
response.Transaction = toJSONTransaction(*tx)
if err := api.publisher.PublishMessageToNetwork(ctx, txBytes); err != nil {
return fmt.Errorf("failed to publish transaction to network: %w", err)
}
return nil
}
// SendTransactionArgs represents the arguments for sending a transaction using the client keystore mechanism.
type SendTransactionArgs struct {
AccessToken string `json:"access_token"`
Nounce string `json:"nounce"`
Data string `json:"data"`
From string `json:"from"`
To string `json:"to"`
Value string `json:"value"`
TransactionFees string `json:"transaction_fees"`
}
// SendTransaction sends a transaction.
func (api *TransactionAPI) SendTransaction(r *http.Request, args *SendTransactionArgs, response *TransactionResponse) error {
if args.AccessToken == "" {
return errors.New("access token is empty")
}
ok, unlockedKey, err := api.keystore.Authorized(args.AccessToken)
if err != nil || !ok {
return errors.New("unauthorized access")
}
txNounce, err := hexutil.DecodeUint64(args.Nounce)
if err != nil {
return fmt.Errorf("failed to decode transaction nounce: %w", err)
}
txNounceBytes := hexutil.EncodeUint64ToBytes(txNounce)
txData, err := hexutil.Decode(args.Data)
if err != nil {
return fmt.Errorf("failed to decode transaction data: %w", err)
}
mainChain, err := hexutil.Decode(transaction.ChainID)
if err != nil {
return fmt.Errorf("failed to decode chainID: %w", err)
}
publicKeyBytes, err := unlockedKey.Key.PublicKey.Raw()
if err != nil {
return fmt.Errorf("failed to get public key of unlocked account: %w", err)
}
tx := transaction.Transaction{
PublicKey: publicKeyBytes,
Nounce: txNounceBytes,
Data: txData,
From: args.From,
To: args.To,
Value: args.Value,
TransactionFees: args.TransactionFees,
Chain: mainChain,
}
if err := tx.Sign(unlockedKey.Key.PrivateKey); err != nil {
return fmt.Errorf("failed to sign transaction: %w", err)
}
return api.validateBroadcastTxSetResponse(r.Context(), &tx, response)
}
// MemPoolResponse represents the mempool hashes.
type MemPoolResponse struct {
TransactionHashes []string `json:"transaction_hashes"`
}
// Pool gets the list of transactions in mempool.
func (api *TransactionAPI) Pool(r *http.Request, args *EmptyArgs, response *MemPoolResponse) error {
memPool := api.blockchain.GetTransactionsFromPool()
response.TransactionHashes = make([]string, len(memPool))
for i, v := range memPool {
response.TransactionHashes[i] = hexutil.Encode(v.Hash)
}
return nil
}
// ReceiptArgs receipt arguments.
type ReceiptArgs struct {
Hash string `json:"hash"`
}
// Receipt gets the transaction receipt.
func (api *TransactionAPI) Receipt(r *http.Request, args *ReceiptArgs, response *TransactionsResponse) error {
transactionBytes, err := hexutil.Decode(args.Hash)
if err != nil {
return err
}
transactions, blockNumbers, err := api.blockchain.GetTransactionByHash(transactionBytes)
if err != nil {
return err
}
response.Transactions = make([]JSONBlockTransaction, 0)
for i, tx := range transactions {
jtx := toJSONTransaction(tx)
receipt := JSONBlockTransaction{
BlockNumber: blockNumbers[i],
Transaction: jtx,
}
response.Transactions = append(response.Transactions, receipt)
}
return nil
}
// ByAddressArgs get transactions by address arguments.
type ByAddressArgs struct {
Address string `json:"address"`
}
// ByAddress gets the list of transactions by address.
func (api *TransactionAPI) ByAddress(r *http.Request, args *ByAddressArgs, response *TransactionsResponse) error {
addressBytes, err := hexutil.Decode(args.Address)
if err != nil {
return err
}
transactions, blockNumbers, err := api.blockchain.GetAddressTransactions(addressBytes)
if err != nil {
return err
}
response.Transactions = make([]JSONBlockTransaction, 0)
for i, tx := range transactions {
jtx := toJSONTransaction(tx)
receipt := JSONBlockTransaction{
BlockNumber: blockNumbers[i],
Transaction: jtx,
}
response.Transactions = append(response.Transactions, receipt)
}
return nil
}
func toJSONTransaction(t transaction.Transaction) JSONTransaction {
return JSONTransaction{
Hash: hexutil.Encode(t.Hash),
Signature: hexutil.Encode(t.Signature),
PublicKey: hexutil.Encode(t.PublicKey),
Nounce: hexutil.EncodeUint64BytesToHexString(t.Nounce),
Data: hexutil.Encode(t.Data),
From: t.From,
To: t.To,
Value: t.Value,
TransactionFees: t.TransactionFees,
Chain: hexutil.Encode(t.Chain),
}
}