-
Notifications
You must be signed in to change notification settings - Fork 4
/
service.go
354 lines (305 loc) · 10.3 KB
/
service.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package proof
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
rpcclient "github.com/cometbft/cometbft/rpc/client"
"github.com/cosmos/cosmos-sdk/client"
gogogrpc "github.com/cosmos/gogoproto/grpc"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/ODIN-PROTOCOL/odin-core/x/oracle/types"
)
// RegisterProofService registers the node gRPC service on the provided gRPC router.
func RegisterProofService(clientCtx client.Context, server gogogrpc.Server) {
RegisterServiceServer(server, NewProofServer(clientCtx))
}
// RegisterGRPCGatewayRoutes mounts the node gRPC service's GRPC-gateway routes
// on the given mux object.
func RegisterGRPCGatewayRoutes(clientConn gogogrpc.ClientConn, mux *runtime.ServeMux) {
_ = RegisterServiceHandlerClient(context.Background(), mux, NewServiceClient(clientConn))
}
// to check proofServer implements ServiceServer
var _ ServiceServer = proofServer{}
// proofServer implements ServiceServer
type proofServer struct {
clientCtx client.Context
}
// NewProofServer returns new proofServer from provided client.Context
func NewProofServer(clientCtx client.Context) ServiceServer {
return proofServer{
clientCtx: clientCtx,
}
}
// Proof returns a proof from provided request ID and block height
func (s proofServer) Proof(ctx context.Context, req *QueryProofRequest) (*ServiceProofResponse, error) {
cliCtx := s.clientCtx
// Set the height in the client context to the requested height
cliCtx.Height = req.Height
height := &cliCtx.Height
// If the height is 0, set the pointer to nil
if cliCtx.Height == 0 {
height = nil
}
// Convert the request ID to the appropriate type
requestID := types.RequestID(req.RequestId)
// Get the commit at the specified height from the client
commit, err := cliCtx.Client.Commit(context.Background(), height)
if err != nil {
return nil, err
}
// Get the proofs for the requested id and height
value, iavlEp, multiStoreEp, err := getProofsByKey(
cliCtx,
types.ResultStoreKey(requestID),
rpcclient.ABCIQueryOptions{Height: commit.Height - 1, Prove: true},
true,
)
if err != nil {
return nil, err
}
// Get the signatures and common vote prefix from the commit header
signatures, commonVote, err := GetSignaturesAndPrefix(&commit.SignedHeader)
if err != nil {
return nil, err
}
// Create a BlockRelayProof object with the relevant information
blockRelay := BlockRelayProof{
MultiStoreProof: GetMultiStoreProof(multiStoreEp),
BlockHeaderMerkleParts: GetBlockHeaderMerkleParts(commit.Header),
CommonEncodedVotePart: commonVote,
Signatures: signatures,
}
// Unmarshal the value into a Result object
var rs types.Result
types.ModuleCdc.MustUnmarshal(value, &rs)
// Create an OracleDataProof object with the relevant information
oracleData := OracleDataProof{
Result: rs,
Version: decodeIAVLLeafPrefix(iavlEp.Leaf.Prefix),
MerklePaths: GetMerklePaths(iavlEp),
}
// Calculate byte for proofbytes
var relayAndVerifyArguments abi.Arguments
format := `[{"type":"bytes"},{"type":"bytes"}]`
err = json.Unmarshal([]byte(format), &relayAndVerifyArguments)
if err != nil {
panic(err)
}
blockRelayBytes, err := blockRelay.encodeToEthData()
if err != nil {
return nil, err
}
oracleDataBytes, err := oracleData.encodeToEthData(uint64(commit.Height))
if err != nil {
return nil, err
}
// Pack the encoded block relay bytes and oracle data bytes into a evm proof bytes
evmProofBytes, err := relayAndVerifyArguments.Pack(blockRelayBytes, oracleDataBytes)
if err != nil {
return nil, err
}
// If the height is negative, return an error
if cliCtx.Height < 0 {
return nil, fmt.Errorf("negative height in response")
}
// Return a QueryProofResponse object with the relevant information
return &ServiceProofResponse{
Height: cliCtx.Height,
Result: SingleProofResponse{
Proof: SingleProof{
BlockHeight: uint64(commit.Height),
OracleDataProof: oracleData,
BlockRelayProof: blockRelay,
},
EvmProofBytes: evmProofBytes,
},
}, nil
}
// MultiProof returns a proof for multiple request IDs
func (s proofServer) MultiProof(ctx context.Context, req *QueryMultiProofRequest) (*ServiceMultiProofResponse, error) {
// Get the client context from the server context
cliCtx := s.clientCtx
height := &cliCtx.Height
// If the height is 0, set the pointer to nil
if cliCtx.Height == 0 {
height = nil
}
// Get the request IDs from the request object
requestIDs := req.RequestIds
// If there are no request IDs, return an error
if len(requestIDs) == 0 {
return nil, fmt.Errorf("please provide request ids")
}
// Get the commit at the specified height from the ABCI client
commit, err := cliCtx.Client.Commit(context.Background(), height)
if err != nil {
return nil, err
}
// Get the signatures and common vote from the commit header
signatures, commonVote, err := GetSignaturesAndPrefix(&commit.SignedHeader)
if err != nil {
return nil, err
}
// Create a BlockRelayProof object with the relevant information
blockRelay := BlockRelayProof{
BlockHeaderMerkleParts: GetBlockHeaderMerkleParts(commit.Header),
CommonEncodedVotePart: commonVote,
Signatures: signatures,
}
// Create lists to store the oracle data proof objects and encoded bytes for each request ID
oracleDataBytesList := make([][]byte, len(requestIDs))
oracleDataList := make([]OracleDataProof, len(requestIDs))
// Loop through each request ID and get the relevant proofs
for idx, intRequestID := range requestIDs {
requestID := types.RequestID(intRequestID)
value, iavlEp, multiStoreEp, err := getProofsByKey(
cliCtx,
types.ResultStoreKey(requestID),
rpcclient.ABCIQueryOptions{Height: commit.Height - 1, Prove: true},
idx == 0,
)
if err != nil {
return nil, err
}
// Unmarshal the value into a Result object
var rs types.Result
types.ModuleCdc.MustUnmarshal(value, &rs)
// Create an OracleDataProof object with the relevant information
oracleData := OracleDataProof{
Result: rs,
Version: decodeIAVLLeafPrefix(iavlEp.Leaf.Prefix),
MerklePaths: GetMerklePaths(iavlEp),
}
// Encode the oracle data proof into Ethereum-compatible format
oracleDataBytes, err := oracleData.encodeToEthData(uint64(commit.Height))
if err != nil {
return nil, err
}
// Append the encoded oracle data proof to the list
oracleDataBytesList[idx] = oracleDataBytes
oracleDataList[idx] = oracleData
// If this is the first iteration, set the multiStoreProof in the blockRelay object
if idx == 0 {
blockRelay.MultiStoreProof = GetMultiStoreProof(multiStoreEp)
}
}
// Encode the block relay proof
blockRelayBytes, err := blockRelay.encodeToEthData()
if err != nil {
return nil, err
}
// Calculate byte for MultiProofbytes
var relayAndVerifyArguments abi.Arguments
format := `[{"type":"bytes"},{"type":"bytes[]"}]`
err = json.Unmarshal([]byte(format), &relayAndVerifyArguments)
if err != nil {
panic(err)
}
// Pack the encoded block relay bytes and oracle data bytes into a evm proof bytes
evmProofBytes, err := relayAndVerifyArguments.Pack(blockRelayBytes, oracleDataBytesList)
if err != nil {
return nil, err
}
// If the height is negative, return an error
if cliCtx.Height < 0 {
return nil, fmt.Errorf("negative height in response")
}
// Return a QueryMultiProofResponse object with the relevant information
return &ServiceMultiProofResponse{
Height: cliCtx.Height,
Result: MultiProofResponse{
Proof: MultiProof{
BlockHeight: uint64(commit.Height),
OracleDataMultiProof: oracleDataList,
BlockRelayProof: blockRelay,
},
EvmProofBytes: evmProofBytes,
},
}, nil
}
// RequestCountProof returns a count proof
func (s proofServer) RequestCountProof(
ctx context.Context,
req *QueryRequestCountProofRequest,
) (*ServiceRequestCountProofResponse, error) {
// Get the client context from the server context
cliCtx := s.clientCtx
height := &cliCtx.Height
// If the height is 0, set the pointer to nil
if cliCtx.Height == 0 {
height = nil
}
// Get the commit at the specified height from the client
commit, err := cliCtx.Client.Commit(context.Background(), height)
if err != nil {
return nil, err
}
// Get the proofs for the count from the IAVL tree
value, iavlEp, multiStoreEp, err := getProofsByKey(
cliCtx,
types.RequestCountStoreKey,
rpcclient.ABCIQueryOptions{Height: commit.Height - 1, Prove: true},
true,
)
if err != nil {
return nil, err
}
// Create a BlockRelayProof object with the relevant information
signatures, commonVote, err := GetSignaturesAndPrefix(&commit.SignedHeader)
if err != nil {
return nil, err
}
blockRelay := BlockRelayProof{
MultiStoreProof: GetMultiStoreProof(multiStoreEp),
BlockHeaderMerkleParts: GetBlockHeaderMerkleParts(commit.Header),
CommonEncodedVotePart: commonVote,
Signatures: signatures,
}
// Parse the request count from the binary value
rs := binary.BigEndian.Uint64(value)
// Create a RequestsCountProof object with the relevant information
requestsCountProof := RequestsCountProof{
Count: rs,
Version: decodeIAVLLeafPrefix(iavlEp.Leaf.Prefix),
MerklePaths: GetMerklePaths(iavlEp),
}
// Calculate byte for proofbytes
var relayAndVerifyCountArguments abi.Arguments
format := `[{"type":"bytes"},{"type":"bytes"}]`
err = json.Unmarshal([]byte(format), &relayAndVerifyCountArguments)
if err != nil {
panic(err)
}
// Encode the block relay proof and the requests count proof into Ethereum-compatible format
blockRelayBytes, err := blockRelay.encodeToEthData()
if err != nil {
return nil, err
}
requestsCountBytes, err := requestsCountProof.encodeToEthData(uint64(commit.Height))
if err != nil {
return nil, err
}
// Pack the encoded proofs into a single byte array
evmProofBytes, err := relayAndVerifyCountArguments.Pack(blockRelayBytes, requestsCountBytes)
if err != nil {
return nil, err
}
// If the client context height is negative, return an error
if cliCtx.Height < 0 {
return nil, fmt.Errorf("negative height in response")
}
// Return the QueryRequestCountProofResponse object with the relevant information
return &ServiceRequestCountProofResponse{
Height: cliCtx.Height,
Result: CountProofResponse{
Proof: CountProof{
BlockHeight: uint64(commit.Height),
CountProof: requestsCountProof,
BlockRelayProof: blockRelay,
},
EvmProofBytes: evmProofBytes,
},
}, nil
}