-
Notifications
You must be signed in to change notification settings - Fork 22
/
admin_send_transaction.go
254 lines (213 loc) · 7.88 KB
/
admin_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
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
package api
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"code.vegaprotocol.io/vega/commands"
vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/libs/jsonrpc"
apipb "code.vegaprotocol.io/vega/protos/vega/api/v1"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
walletpb "code.vegaprotocol.io/vega/protos/vega/wallet/v1"
"code.vegaprotocol.io/vega/wallet/api/node"
wcommands "code.vegaprotocol.io/vega/wallet/commands"
"github.com/golang/protobuf/jsonpb"
"github.com/mitchellh/mapstructure"
)
type AdminSendTransactionParams struct {
Wallet string `json:"wallet"`
Passphrase string `json:"passphrase"`
PublicKey string `json:"publicKey"`
Network string `json:"network"`
NodeAddress string `json:"nodeAddress"`
Retries uint64 `json:"retries"`
SendingMode string `json:"sendingMode"`
Transaction interface{} `json:"transaction"`
}
type ParsedAdminSendTransactionParams struct {
Wallet string
Passphrase string
PublicKey string
Network string
NodeAddress string
Retries uint64
SendingMode apipb.SubmitTransactionRequest_Type
RawTransaction string
}
type AdminSendTransactionResult struct {
ReceivedAt time.Time `json:"receivedAt"`
SentAt time.Time `json:"sentAt"`
TxHash string `json:"transactionHash"`
Tx *commandspb.Transaction `json:"transaction"`
}
type AdminSendTransaction struct {
walletStore WalletStore
networkStore NetworkStore
nodeSelectorBuilder NodeSelectorBuilder
}
func (h *AdminSendTransaction) Handle(ctx context.Context, rawParams jsonrpc.Params, _ jsonrpc.RequestMetadata) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
params, err := validateAdminSendTransactionParams(rawParams)
if err != nil {
return nil, invalidParams(err)
}
receivedAt := time.Now()
if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil {
return nil, internalError(fmt.Errorf("could not verify the wallet existence: %w", err))
} else if !exist {
return nil, invalidParams(ErrWalletDoesNotExist)
}
w, err := h.walletStore.GetWallet(ctx, params.Wallet, params.Passphrase)
if err != nil {
return nil, internalError(fmt.Errorf("could not retrieve the wallet: %w", err))
}
request := &walletpb.SubmitTransactionRequest{}
if err := jsonpb.Unmarshal(strings.NewReader(params.RawTransaction), request); err != nil {
return nil, invalidParams(ErrTransactionIsNotValidVegaCommand)
}
request.PubKey = params.PublicKey
request.Propagate = true
if errs := wcommands.CheckSubmitTransactionRequest(request); !errs.Empty() {
return nil, invalidParams(errs)
}
node, errDetails := h.getNode(ctx, params)
if errDetails != nil {
return nil, errDetails
}
lastBlockData, errDetails := h.getLastBlockDataFromNetwork(ctx, node)
if err != nil {
return nil, errDetails
}
marshaledInputData, err := wcommands.ToMarshaledInputData(request, lastBlockData.BlockHeight)
if err != nil {
return nil, internalError(fmt.Errorf("could not marshal the input data: %w", err))
}
signature, err := w.SignTx(params.PublicKey, commands.BundleInputDataForSigning(marshaledInputData, lastBlockData.ChainID))
if err != nil {
return nil, internalError(fmt.Errorf("could not sign the transaction: %w", err))
}
// Build the transaction.
tx := commands.NewTransaction(params.PublicKey, marshaledInputData, &commandspb.Signature{
Value: signature.Value,
Algo: signature.Algo,
Version: signature.Version,
})
// Generate the proof of work for the transaction.
txID := vgcrypto.RandomHash()
powNonce, _, err := vgcrypto.PoW(lastBlockData.BlockHash, txID, uint(lastBlockData.ProofOfWorkDifficulty), lastBlockData.ProofOfWorkHashFunction)
if err != nil {
return nil, internalError(fmt.Errorf("could not compute the proof-of-work: %w", err))
}
tx.Pow = &commandspb.ProofOfWork{
Nonce: powNonce,
Tid: txID,
}
sentAt := time.Now()
txHash, err := node.SendTransaction(ctx, tx, params.SendingMode)
if err != nil {
return nil, networkErrorFromTransactionError(err)
}
return AdminSendTransactionResult{
ReceivedAt: receivedAt,
SentAt: sentAt,
TxHash: txHash,
Tx: tx,
}, nil
}
func (h *AdminSendTransaction) getNode(ctx context.Context, params ParsedAdminSendTransactionParams) (node.Node, *jsonrpc.ErrorDetails) {
var hosts []string
var retries uint64
if len(params.Network) != 0 {
exists, err := h.networkStore.NetworkExists(params.Network)
if err != nil {
return nil, internalError(fmt.Errorf("could not check the network existence: %w", err))
} else if !exists {
return nil, invalidParams(ErrNetworkDoesNotExist)
}
n, err := h.networkStore.GetNetwork(params.Network)
if err != nil {
return nil, internalError(fmt.Errorf("could not retrieve the network configuration: %w", err))
}
if err := n.EnsureCanConnectGRPCNode(); err != nil {
return nil, invalidParams(ErrNetworkConfigurationDoesNotHaveGRPCNodes)
}
hosts = n.API.GRPC.Hosts
retries = n.API.GRPC.Retries
} else {
hosts = []string{params.NodeAddress}
retries = params.Retries
}
nodeSelector, err := h.nodeSelectorBuilder(hosts, retries)
if err != nil {
return nil, internalError(fmt.Errorf("could not initializing the node selector: %w", err))
}
node, err := nodeSelector.Node(ctx, noNodeSelectionReporting)
if err != nil {
return nil, nodeCommunicationError(ErrNoHealthyNodeAvailable)
}
return node, nil
}
func (h *AdminSendTransaction) getLastBlockDataFromNetwork(ctx context.Context, node node.Node) (*AdminLastBlockData, *jsonrpc.ErrorDetails) {
lastBlock, err := node.LastBlock(ctx)
if err != nil {
return nil, nodeCommunicationError(ErrCouldNotGetLastBlockInformation)
}
if lastBlock.ChainID == "" {
return nil, nodeCommunicationError(ErrCouldNotGetChainIDFromNode)
}
return &AdminLastBlockData{
BlockHash: lastBlock.BlockHash,
ChainID: lastBlock.ChainID,
BlockHeight: lastBlock.BlockHeight,
ProofOfWorkHashFunction: lastBlock.ProofOfWorkHashFunction,
ProofOfWorkDifficulty: lastBlock.ProofOfWorkDifficulty,
}, nil
}
func NewAdminSendTransaction(walletStore WalletStore, networkStore NetworkStore, nodeSelectorBuilder NodeSelectorBuilder) *AdminSendTransaction {
return &AdminSendTransaction{
walletStore: walletStore,
networkStore: networkStore,
nodeSelectorBuilder: nodeSelectorBuilder,
}
}
func validateAdminSendTransactionParams(rawParams jsonrpc.Params) (ParsedAdminSendTransactionParams, error) {
if rawParams == nil {
return ParsedAdminSendTransactionParams{}, ErrParamsRequired
}
params := AdminSendTransactionParams{}
if err := mapstructure.Decode(rawParams, ¶ms); err != nil {
return ParsedAdminSendTransactionParams{}, ErrParamsDoNotMatch
}
if params.Wallet == "" {
return ParsedAdminSendTransactionParams{}, ErrWalletIsRequired
}
if params.Passphrase == "" {
return ParsedAdminSendTransactionParams{}, ErrPassphraseIsRequired
}
if params.PublicKey == "" {
return ParsedAdminSendTransactionParams{}, ErrPublicKeyIsRequired
}
if params.Network == "" && params.NodeAddress == "" {
return ParsedAdminSendTransactionParams{}, ErrNetworkOrNodeAddressIsRequired
}
if params.Network != "" && params.NodeAddress != "" {
return ParsedAdminSendTransactionParams{}, ErrSpecifyingNetworkAndNodeAddressIsNotSupported
}
if params.Transaction == nil || params.Transaction == "" {
return ParsedAdminSendTransactionParams{}, ErrTransactionIsRequired
}
tx, err := json.Marshal(params.Transaction)
if err != nil {
return ParsedAdminSendTransactionParams{}, ErrTransactionIsNotValidJSON
}
return ParsedAdminSendTransactionParams{
Wallet: params.Wallet,
Passphrase: params.Passphrase,
PublicKey: params.PublicKey,
RawTransaction: string(tx),
Network: params.Network,
NodeAddress: params.NodeAddress,
Retries: params.Retries,
}, nil
}