-
Notifications
You must be signed in to change notification settings - Fork 22
/
admin_send_raw_transaction.go
189 lines (159 loc) · 6.29 KB
/
admin_send_raw_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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package api
import (
"context"
"encoding/base64"
"fmt"
"time"
"code.vegaprotocol.io/vega/libs/jsonrpc"
"code.vegaprotocol.io/vega/libs/proto"
apipb "code.vegaprotocol.io/vega/protos/vega/api/v1"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
"github.com/mitchellh/mapstructure"
)
type AdminSendRawTransactionParams struct {
Network string `json:"network"`
NodeAddress string `json:"nodeAddress"`
Retries uint64 `json:"retries"`
MaximumRequestDuration time.Duration `json:"maximumRequestDuration"`
SendingMode string `json:"sendingMode"`
EncodedTransaction string `json:"encodedTransaction"`
}
type ParsedAdminSendRawTransactionParams struct {
Network string
NodeAddress string
Retries uint64
MaximumRequestDuration time.Duration
SendingMode apipb.SubmitTransactionRequest_Type
RawTransaction string
}
type AdminSendRawTransactionResult struct {
ReceivedAt time.Time `json:"receivedAt"`
SentAt time.Time `json:"sentAt"`
TransactionHash string `json:"transactionHash"`
Transaction *commandspb.Transaction `json:"transaction"`
Node AdminSendRawTransactionNodeResult `json:"node"`
}
type AdminSendRawTransactionNodeResult struct {
Host string `json:"host"`
}
type AdminSendRawTransaction struct {
networkStore NetworkStore
nodeSelectorBuilder NodeSelectorBuilder
}
func (h *AdminSendRawTransaction) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
receivedAt := time.Now()
params, err := validateAdminSendRawTransactionParams(rawParams)
if err != nil {
return nil, InvalidParams(err)
}
tx := &commandspb.Transaction{}
if err := proto.Unmarshal([]byte(params.RawTransaction), tx); err != nil {
return nil, InvalidParams(ErrRawTransactionIsNotValidVegaTransaction)
}
hosts := []string{params.NodeAddress}
if len(params.Network) != 0 {
exists, err := h.networkStore.NetworkExists(params.Network)
if err != nil {
return nil, InternalError(fmt.Errorf("could not determine if the network exists: %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, InternalError(ErrNetworkConfigurationDoesNotHaveGRPCNodes)
}
hosts = n.API.GRPC.Hosts
}
nodeSelector, err := h.nodeSelectorBuilder(hosts, params.Retries, params.MaximumRequestDuration)
if err != nil {
return nil, InternalError(fmt.Errorf("could not initialize the node selector: %w", err))
}
currentNode, err := nodeSelector.Node(ctx, noNodeSelectionReporting)
if err != nil {
return nil, NodeCommunicationError(ErrNoHealthyNodeAvailable)
}
sentAt := time.Now()
txHash, err := currentNode.SendTransaction(ctx, tx, params.SendingMode)
if err != nil {
return nil, NetworkErrorFromTransactionError(err)
}
return AdminSendRawTransactionResult{
ReceivedAt: receivedAt,
SentAt: sentAt,
TransactionHash: txHash,
Transaction: tx,
Node: AdminSendRawTransactionNodeResult{
Host: currentNode.Host(),
},
}, nil
}
func NewAdminSendRawTransaction(networkStore NetworkStore, nodeSelectorBuilder NodeSelectorBuilder) *AdminSendRawTransaction {
return &AdminSendRawTransaction{
networkStore: networkStore,
nodeSelectorBuilder: nodeSelectorBuilder,
}
}
func validateAdminSendRawTransactionParams(rawParams jsonrpc.Params) (ParsedAdminSendRawTransactionParams, error) {
if rawParams == nil {
return ParsedAdminSendRawTransactionParams{}, ErrParamsRequired
}
params := AdminSendRawTransactionParams{}
if err := mapstructure.Decode(rawParams, ¶ms); err != nil {
return ParsedAdminSendRawTransactionParams{}, ErrParamsDoNotMatch
}
if params.Network == "" && params.NodeAddress == "" {
return ParsedAdminSendRawTransactionParams{}, ErrNetworkOrNodeAddressIsRequired
}
if params.Network != "" && params.NodeAddress != "" {
return ParsedAdminSendRawTransactionParams{}, ErrSpecifyingNetworkAndNodeAddressIsNotSupported
}
if params.SendingMode == "" {
return ParsedAdminSendRawTransactionParams{}, ErrSendingModeIsRequired
}
isValidSendingMode := false
var sendingMode apipb.SubmitTransactionRequest_Type
for tp, sm := range apipb.SubmitTransactionRequest_Type_value {
if tp == params.SendingMode {
isValidSendingMode = true
sendingMode = apipb.SubmitTransactionRequest_Type(sm)
}
}
if !isValidSendingMode {
return ParsedAdminSendRawTransactionParams{}, fmt.Errorf("the sending mode %q is not a valid one", params.SendingMode)
}
if sendingMode == apipb.SubmitTransactionRequest_TYPE_UNSPECIFIED {
return ParsedAdminSendRawTransactionParams{}, ErrSendingModeCannotBeTypeUnspecified
}
if params.EncodedTransaction == "" {
return ParsedAdminSendRawTransactionParams{}, ErrEncodedTransactionIsRequired
}
tx, err := base64.StdEncoding.DecodeString(params.EncodedTransaction)
if err != nil {
return ParsedAdminSendRawTransactionParams{}, ErrEncodedTransactionIsNotValidBase64String
}
return ParsedAdminSendRawTransactionParams{
Network: params.Network,
NodeAddress: params.NodeAddress,
RawTransaction: string(tx),
SendingMode: sendingMode,
Retries: params.Retries,
MaximumRequestDuration: params.MaximumRequestDuration,
}, nil
}