This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
cmd.go
230 lines (205 loc) · 6.57 KB
/
cmd.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
package deploy
import (
"context"
"strconv"
"time"
"github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/base"
evm2 "github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/keys/evm"
"github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/keys"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
"github.com/celestiaorg/celestia-app/x/qgb/types"
"github.com/celestiaorg/orchestrator-relayer/evm"
"github.com/celestiaorg/orchestrator-relayer/rpc"
"github.com/celestiaorg/orchestrator-relayer/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
tmlog "github.com/tendermint/tendermint/libs/log"
)
func Command() *cobra.Command {
command := &cobra.Command{
Use: "deploy <flags>",
Short: "Deploys the Blobstream contract and initializes it using the provided Celestia chain",
RunE: func(cmd *cobra.Command, args []string) error {
config, err := parseDeployFlags(cmd)
if err != nil {
return err
}
logger, err := base.GetLogger(config.logLevel, config.logFormat)
if err != nil {
return err
}
// checking if the provided home is already initiated
isInit := store.IsInit(logger, config.Home, store.InitOptions{NeedEVMKeyStore: true})
if !isInit {
logger.Info("please initialize the EVM keystore using the `blobstream deploy keys add/import` command")
return store.ErrNotInited
}
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...)
appQuerier := rpc.NewAppQuerier(logger, config.coreGRPC, encCfg)
err = appQuerier.Start(config.grpcInsecure)
if err != nil {
return err
}
defer func() {
err := appQuerier.Stop()
if err != nil {
logger.Error(err.Error())
}
}()
tmQuerier := rpc.NewTmQuerier(config.coreRPC, logger)
err = tmQuerier.Start()
if err != nil {
return err
}
defer func(tmQuerier *rpc.TmQuerier) {
err := tmQuerier.Stop()
if err != nil {
logger.Error(err.Error())
}
}(tmQuerier)
vs, err := getStartingValset(cmd.Context(), *tmQuerier, appQuerier, config.startingNonce)
if err != nil {
logger.Error("couldn't get valset from state (probably pruned). connect to an archive node to be able to deploy the contract", "err", err.Error())
return errors.Wrap(
err,
"cannot initialize the Blobstream contract without having a valset request: %s",
)
}
startingNonce, err := parseStartingNonce(cmd.Context(), appQuerier, config.startingNonce)
if err != nil {
return err
}
// creating the data store
openOptions := store.OpenOptions{HasEVMKeyStore: true}
s, err := store.OpenStore(logger, config.Home, openOptions)
if err != nil {
return err
}
defer func(s *store.Store, log tmlog.Logger) {
err := s.Close(log, openOptions)
if err != nil {
logger.Error(err.Error())
}
}(s, logger)
logger.Info("loading EVM account", "address", config.evmAccAddress)
acc, err := evm2.GetAccountFromStoreAndUnlockIt(s.EVMKeyStore, config.evmAccAddress, config.EVMPassphrase)
if err != nil {
return err
}
defer func(EVMKeyStore *keystore.KeyStore, addr common.Address) {
err := EVMKeyStore.Lock(addr)
if err != nil {
panic(err)
}
}(s.EVMKeyStore, acc.Address)
evmClient := evm.NewClient(
logger,
nil,
s.EVMKeyStore,
&acc,
config.evmRPC,
config.evmGasLimit,
)
txOpts, err := evmClient.NewTransactionOpts(cmd.Context())
if err != nil {
return err
}
backend, err := evmClient.NewEthClient()
if err != nil {
return err
}
defer backend.Close()
address, tx, _, err := evmClient.DeployBlobstreamContract(txOpts, backend, *vs, startingNonce, false)
if err != nil {
logger.Error("failed to deploy Blobstream contract")
return err
}
receipt, err := evmClient.WaitForTransaction(cmd.Context(), backend, tx, 5*time.Minute)
if err == nil && receipt != nil && receipt.Status == 1 {
logger.Info("deployed Blobstream contract", "proxy_address", address.Hex(), "tx_hash", tx.Hash().String())
}
return nil
},
}
command.AddCommand(keys.Command(ServiceNameDeployer))
return addDeployFlags(command)
}
// getStartingValset get the valset that will be used to init the bridge contract.
func getStartingValset(ctx context.Context, tmQuerier rpc.TmQuerier, appQuerier *rpc.AppQuerier, startingNonce string) (*types.Valset, error) {
switch startingNonce {
case "latest":
vs, err := appQuerier.QueryLatestValset(ctx)
if err != nil {
appQuerier.Logger.Debug("couldn't get the attestation from node state. trying with historical data if the target node is archival", "nonce", 1, "err", err.Error())
currentHeight, err := tmQuerier.QueryHeight(ctx)
if err != nil {
return nil, err
}
return appQuerier.QueryRecursiveLatestValset(ctx, uint64(currentHeight))
}
return vs, nil
case "earliest":
// TODO make the first nonce 1 a const
att, err := appQuerier.QueryAttestationByNonce(ctx, 1)
if err != nil {
appQuerier.Logger.Debug("couldn't get the attestation from node state. trying with historical data if the target node is archival", "nonce", 1, "err", err.Error())
historicalAtt, err := appQuerier.QueryHistoricalAttestationByNonce(ctx, 1, 1)
if err != nil {
return nil, err
}
att = historicalAtt
}
vs, ok := att.(*types.Valset)
if !ok {
return nil, ErrUnmarshallValset
}
return vs, nil
default:
nonce, err := strconv.ParseUint(startingNonce, 10, 0)
if err != nil {
return nil, err
}
currentHeight, err := tmQuerier.QueryHeight(ctx)
if err != nil {
return nil, err
}
attestation, err := appQuerier.QueryRecursiveHistoricalAttestationByNonce(ctx, nonce, uint64(currentHeight))
if err != nil {
return nil, err
}
if attestation == nil {
return nil, types.ErrNilAttestation
}
switch value := attestation.(type) {
case *types.Valset:
return value, nil
case *types.DataCommitment:
return appQuerier.QueryRecursiveHistoricalLastValsetBeforeNonce(ctx, nonce, value.EndBlock)
}
}
return nil, ErrNotFound
}
// parseStartingNonce parse the provided starting nonce.
func parseStartingNonce(ctx context.Context, appQuerier *rpc.AppQuerier, startingNonce string) (uint64, error) {
switch startingNonce {
case "latest":
nonce, err := appQuerier.QueryLatestAttestationNonce(ctx)
if err != nil {
if err != nil {
return 0, err
}
}
return nonce, nil
case "earliest":
return 1, nil
default:
nonce, err := strconv.ParseUint(startingNonce, 10, 0)
if err != nil {
return 0, err
}
return nonce, nil
}
}