/
mapprotocol.go
293 lines (259 loc) · 8.87 KB
/
mapprotocol.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
// Copyright 2021 Compass Systems
// SPDX-License-Identifier: LGPL-3.0-only
package mapprotocol
import (
"context"
"encoding/json"
"fmt"
"github.com/mapprotocol/compass/internal/constant"
"github.com/mapprotocol/compass/pkg/contract"
nearclient "github.com/mapprotocol/near-api-go/pkg/client"
"math/big"
"github.com/mapprotocol/near-api-go/pkg/client/block"
"github.com/ethereum/go-ethereum"
goeth "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/mapprotocol/compass/msg"
"github.com/mapprotocol/compass/pkg/ethclient"
"github.com/pkg/errors"
)
type GetHeight func() (*big.Int, error)
type GetVerifyRange func() (*big.Int, *big.Int, error)
var (
MapId string
GlobalMapConn *ethclient.Client
SyncOtherMap = make(map[msg.ChainId]*big.Int) // map to other chain init height
Map2OtherHeight = make(map[msg.ChainId]GetHeight) // get map to other height function collect
ContractMapping = make(map[msg.ChainId]*contract.Call)
Get2MapHeight = func(chainId msg.ChainId) (*big.Int, error) { return nil, nil } // get other chain to map height
GetEth22MapNumber = func(chainId msg.ChainId) (*big.Int, *big.Int, error) { return nil, nil, nil } // can reform, return data is []byte
GetDataByManager = func(string, ...interface{}) ([]byte, error) { return nil, nil }
)
func InitLightManager(lightNode common.Address) {
GetDataByManager = func(method string, params ...interface{}) ([]byte, error) {
input, err := PackInput(LightManger, method, params...)
if err != nil {
return nil, errors.Wrap(err, "get other2map packInput failed")
}
output, err := GlobalMapConn.CallContract(
context.Background(),
goeth.CallMsg{From: constant.ZeroAddress, To: &lightNode, Data: input},
nil,
)
if err != nil {
return nil, err
}
outputs := LightManger.Methods[method].Outputs
unpack, err := outputs.Unpack(output)
if err != nil {
return nil, err
}
ret := make([]byte, 0)
if err = outputs.Copy(&ret, unpack); err != nil {
return nil, err
}
return ret, nil
}
}
func Init2GetEth22MapNumber(lightNode common.Address) {
GetEth22MapNumber = func(chainId msg.ChainId) (*big.Int, *big.Int, error) {
input, err := PackInput(LightManger, MethodClientState, big.NewInt(int64(chainId)))
if err != nil {
return nil, nil, errors.Wrap(err, "get eth22map packInput failed")
}
output, err := GlobalMapConn.CallContract(context.Background(),
goeth.CallMsg{From: constant.ZeroAddress, To: &lightNode, Data: input}, nil)
if err != nil {
return nil, nil, err
}
outputs := LightManger.Methods[MethodClientState].Outputs
unpack, err := outputs.Unpack(output)
if err != nil {
return nil, nil, err
}
back := make([]byte, 0)
if err = outputs.Copy(&back, unpack); err != nil {
return nil, nil, err
}
ret := struct {
StartNumber *big.Int
EndNumber *big.Int
}{}
analysis, err := Eth2.Methods[MethodClientStateAnalysis].Outputs.Unpack(back)
if err != nil {
return nil, nil, errors.Wrap(err, "analysis")
}
if err = Eth2.Methods[MethodClientStateAnalysis].Outputs.Copy(&ret, analysis); err != nil {
return nil, nil, errors.Wrap(err, "analysis copy")
}
return ret.StartNumber, ret.EndNumber, nil
}
}
func InitOtherChain2MapHeight(lightManager common.Address) {
Get2MapHeight = func(chainId msg.ChainId) (*big.Int, error) {
input, err := PackInput(LightManger, MethodOfHeaderHeight, big.NewInt(int64(chainId)))
if err != nil {
return nil, errors.Wrap(err, "get other2map by manager packInput failed")
}
height, err := HeaderHeight(lightManager, input)
if err != nil {
return nil, errors.Wrap(err, "get other2map headerHeight by lightManager failed")
}
return height, nil
}
}
func Map2EthHeight(fromUser string, lightNode common.Address, client *ethclient.Client) GetHeight {
return func() (*big.Int, error) {
from := common.HexToAddress(fromUser)
input, err := PackInput(Height, MethodOfHeaderHeight)
if err != nil {
return nil, fmt.Errorf("pack lightNode headerHeight Input failed, err is %v", err.Error())
}
output, err := client.CallContract(context.Background(),
ethereum.CallMsg{
From: from,
To: &lightNode,
Data: input,
},
nil,
)
if err != nil {
return nil, fmt.Errorf("headerHeight callContract failed, err is %v", err.Error())
}
return UnpackHeaderHeightOutput(output)
}
}
func Map2NearHeight(lightNode string, client *nearclient.Client) GetHeight {
return func() (*big.Int, error) {
res, err := client.ContractViewCallFunction(context.Background(), lightNode, NearHeaderHeight,
"e30=", block.FinalityFinal())
if err != nil {
return nil, errors.Wrap(err, "call near lightNode to headerHeight failed")
}
if res.Error != nil {
return nil, fmt.Errorf("call near lightNode to get headerHeight resp exist error(%v)", *res.Error)
}
result := "" // use string return
err = json.Unmarshal(res.Result, &result)
if err != nil {
return nil, errors.Wrap(err, "near lightNode headerHeight resp json marshal failed")
}
ret := new(big.Int)
ret.SetString(result, 10)
return ret, nil
}
}
func PackInput(commonAbi abi.ABI, abiMethod string, params ...interface{}) ([]byte, error) {
input, err := commonAbi.Pack(abiMethod, params...)
if err != nil {
return nil, err
}
return input, nil
}
func UnpackHeaderHeightOutput(output []byte) (*big.Int, error) {
outputs := Height.Methods[MethodOfHeaderHeight].Outputs
unpack, err := outputs.Unpack(output)
if err != nil {
return big.NewInt(0), err
}
height := new(big.Int)
if err = outputs.Copy(&height, unpack); err != nil {
return big.NewInt(0), err
}
return height, nil
}
func HeaderHeight(to common.Address, input []byte) (*big.Int, error) {
output, err := GlobalMapConn.CallContract(context.Background(), goeth.CallMsg{From: constant.ZeroAddress, To: &to, Data: input}, nil)
if err != nil {
return nil, err
}
height, err := UnpackHeaderHeightOutput(output)
if err != nil {
return nil, err
}
return height, nil
}
//
//func InitOtherChain2MapVerifyRange(lightManager common.Address) {
// Get2MapVerifyRange = func(chainId msg.ChainId) (*big.Int, *big.Int, error) {
// input, err := PackInput(LightManger, MethodVerifiableHeaderRange, big.NewInt(int64(chainId)))
// if err != nil {
// return nil, nil, errors.Wrap(err, "get other2map verifyRange packInput failed")
// }
//
// left, right, err := VerifyRange(lightManager, input)
// if err != nil {
// return nil, nil, errors.Wrap(err, "get other2map verifyRange by lightManager failed")
// }
// return left, right, nil
// }
//}
func Map2EthVerifyRange(fromUser string, lightNode common.Address, client *ethclient.Client) GetVerifyRange {
return func() (*big.Int, *big.Int, error) {
from := common.HexToAddress(fromUser)
input, err := PackInput(Verify, MethodVerifiableHeaderRange)
if err != nil {
return nil, nil, errors.Wrap(err, "pack lightNode verifiableHeaderRange Input failed")
}
output, err := client.CallContract(context.Background(),
ethereum.CallMsg{
From: from,
To: &lightNode,
Data: input,
},
nil,
)
if err != nil {
return nil, nil, fmt.Errorf("verifiableHeaderRange callContract failed, err is %v", err.Error())
}
return UnpackVerifyRangeOutput(output)
}
}
func Map2NearVerifyRange(lightNode string, client *nearclient.Client) GetVerifyRange {
return func() (*big.Int, *big.Int, error) {
res, err := client.ContractViewCallFunction(context.Background(), lightNode, NearVerifyRange,
"e30=", block.FinalityFinal())
if err != nil {
return nil, nil, errors.Wrap(err, "call near lightNode to get_verifiable_header_range failed")
}
if res.Error != nil {
return nil, nil, fmt.Errorf("call near lightNode to get get_verifiable_header_range resp exist error(%v)", *res.Error)
}
var verifyRange [2]string
err = json.Unmarshal(res.Result, &verifyRange)
if err != nil {
return nil, nil, errors.Wrap(err, "near lightNode get_verifiable_header_range resp json marshal failed")
}
var left, right big.Int
left.SetString(verifyRange[0], 10)
right.SetString(verifyRange[1], 10)
return &left, &right, nil
}
}
func VerifyRange(to common.Address, input []byte) (*big.Int, *big.Int, error) {
output, err := GlobalMapConn.CallContract(context.Background(), goeth.CallMsg{From: constant.ZeroAddress, To: &to, Data: input}, nil)
if err != nil {
return nil, nil, err
}
left, right, err := UnpackVerifyRangeOutput(output)
if err != nil {
return nil, nil, err
}
return left, right, nil
}
func UnpackVerifyRangeOutput(output []byte) (*big.Int, *big.Int, error) {
outputs := Verify.Methods[MethodVerifiableHeaderRange].Outputs
unpack, err := outputs.Unpack(output)
if err != nil {
return new(big.Int), new(big.Int), err
}
ret := struct {
Left *big.Int
Right *big.Int
}{}
if err = outputs.Copy(&ret, unpack); err != nil {
return new(big.Int), new(big.Int), err
}
return ret.Left, ret.Right, nil
}