-
Notifications
You must be signed in to change notification settings - Fork 19
/
admin_send_raw_transaction.go
167 lines (140 loc) · 5.08 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
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"`
SendingMode string `json:"sendingMode"`
EncodedTransaction string `json:"encodedTransaction"`
}
type ParsedAdminSendRawTransactionParams struct {
Network string
NodeAddress string
Retries uint64
SendingMode apipb.SubmitTransactionRequest_Type
RawTransaction string
}
type AdminSendRawTransactionResult struct {
ReceivedAt time.Time `json:"receivedAt"`
SentAt time.Time `json:"sentAt"`
TxHash string `json:"transactionHash"`
Tx *commandspb.Transaction `json:"transaction"`
}
type AdminSendRawTransaction struct {
networkStore NetworkStore
nodeSelectorBuilder NodeSelectorBuilder
}
func (h *AdminSendRawTransaction) Handle(ctx context.Context, rawParams jsonrpc.Params, _ jsonrpc.RequestMetadata) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
params, err := validateAdminSendRawTransactionParams(rawParams)
if err != nil {
return nil, invalidParams(err)
}
receivedAt := time.Now()
tx := &commandspb.Transaction{}
if err := proto.Unmarshal([]byte(params.RawTransaction), tx); err != nil {
return nil, invalidParams(ErrTransactionIsMalformed)
}
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, internalError(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))
}
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,
TxHash: txHash,
Tx: tx,
}, 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,
}, nil
}