forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_access.go
583 lines (523 loc) · 16 KB
/
core_access.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package state
import (
"context"
"errors"
"fmt"
"math"
"sync"
"time"
sdkErrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/api/tendermint/abci"
nodeservice "github.com/cosmos/cosmos-sdk/client/grpc/node"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
logging "github.com/ipfs/go-log/v2"
"github.com/tendermint/tendermint/crypto/merkle"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/tendermint/tendermint/rpc/client/http"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/celestiaorg/celestia-app/app"
apperrors "github.com/celestiaorg/celestia-app/app/errors"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
appblob "github.com/celestiaorg/celestia-app/x/blob"
apptypes "github.com/celestiaorg/celestia-app/x/blob/types"
libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/celestia-node/blob"
"github.com/celestiaorg/celestia-node/header"
)
var (
log = logging.Logger("state")
ErrInvalidAmount = errors.New("state: amount must be greater than zero")
)
const maxRetries = 5
// CoreAccessor implements service over a gRPC connection
// with a celestia-core node.
type CoreAccessor struct {
ctx context.Context
cancel context.CancelFunc
signer *apptypes.KeyringSigner
getter libhead.Head[*header.ExtendedHeader]
queryCli banktypes.QueryClient
stakingCli stakingtypes.QueryClient
rpcCli rpcclient.ABCIClient
prt *merkle.ProofRuntime
coreConn *grpc.ClientConn
coreIP string
rpcPort string
grpcPort string
// these fields are mutatable and thus need to be protected by a mutex
lock sync.Mutex
lastPayForBlob int64
payForBlobCount int64
// minGasPrice is the minimum gas price that the node will accept.
// NOTE: just because the first node accepts the transaction, does not mean it
// will find a proposer that does accept the transaction. Better would be
// to set a global min gas price that correct processes conform to.
minGasPrice float64
}
// NewCoreAccessor dials the given celestia-core endpoint and
// constructs and returns a new CoreAccessor (state service) with the active
// connection.
func NewCoreAccessor(
signer *apptypes.KeyringSigner,
getter libhead.Head[*header.ExtendedHeader],
coreIP,
rpcPort string,
grpcPort string,
) *CoreAccessor {
// create verifier
prt := merkle.DefaultProofRuntime()
prt.RegisterOpDecoder(storetypes.ProofOpIAVLCommitment, storetypes.CommitmentOpDecoder)
prt.RegisterOpDecoder(storetypes.ProofOpSimpleMerkleCommitment, storetypes.CommitmentOpDecoder)
return &CoreAccessor{
signer: signer,
getter: getter,
coreIP: coreIP,
rpcPort: rpcPort,
grpcPort: grpcPort,
prt: prt,
}
}
func (ca *CoreAccessor) Start(ctx context.Context) error {
if ca.coreConn != nil {
return fmt.Errorf("core-access: already connected to core endpoint")
}
ca.ctx, ca.cancel = context.WithCancel(context.Background())
// dial given celestia-core endpoint
endpoint := fmt.Sprintf("%s:%s", ca.coreIP, ca.grpcPort)
client, err := grpc.DialContext(
ctx,
endpoint,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return err
}
ca.coreConn = client
// create the query client
queryCli := banktypes.NewQueryClient(ca.coreConn)
ca.queryCli = queryCli
// create the staking query client
stakingCli := stakingtypes.NewQueryClient(ca.coreConn)
ca.stakingCli = stakingCli
// create ABCI query client
cli, err := http.New(fmt.Sprintf("http://%s:%s", ca.coreIP, ca.rpcPort), "/websocket")
if err != nil {
return err
}
ca.rpcCli = cli
ca.minGasPrice, err = ca.queryMinimumGasPrice(ctx)
if err != nil {
return fmt.Errorf("querying minimum gas price: %w", err)
}
return nil
}
func (ca *CoreAccessor) Stop(context.Context) error {
if ca.cancel == nil {
log.Warn("core accessor already stopped")
return nil
}
if ca.coreConn == nil {
log.Warn("no connection found to close")
return nil
}
defer ca.cancelCtx()
// close out core connection
err := ca.coreConn.Close()
if err != nil {
return err
}
ca.coreConn = nil
ca.queryCli = nil
return nil
}
func (ca *CoreAccessor) cancelCtx() {
ca.cancel()
ca.cancel = nil
}
func (ca *CoreAccessor) constructSignedTx(
ctx context.Context,
msg sdktypes.Msg,
opts ...apptypes.TxBuilderOption,
) ([]byte, error) {
// should be called first in order to make a valid tx
err := ca.signer.QueryAccountNumber(ctx, ca.coreConn)
if err != nil {
return nil, err
}
tx, err := ca.signer.BuildSignedTx(ca.signer.NewTxBuilder(opts...), msg)
if err != nil {
return nil, err
}
return ca.signer.EncodeTx(tx)
}
// SubmitPayForBlob builds, signs, and synchronously submits a MsgPayForBlob. It blocks until the
// transaction is committed and returns the TxReponse. If gasLim is set to 0, the method will
// automatically estimate the gas limit. If the fee is negative, the method will use the nodes min
// gas price multiplied by the gas limit.
func (ca *CoreAccessor) SubmitPayForBlob(
ctx context.Context,
fee Int,
gasLim uint64,
blobs []*blob.Blob,
) (*TxResponse, error) {
if len(blobs) == 0 {
return nil, errors.New("state: no blobs provided")
}
appblobs := make([]*apptypes.Blob, len(blobs))
for i := range blobs {
if err := blobs[i].Namespace().ValidateForBlob(); err != nil {
return nil, err
}
appblobs[i] = &blobs[i].Blob
}
// we only estimate gas if the user wants us to (by setting the gasLim to 0). In the future we may
// want to make these arguments optional.
if gasLim == 0 {
blobSizes := make([]uint32, len(blobs))
for i, blob := range blobs {
blobSizes[i] = uint32(len(blob.Data))
}
// TODO (@cmwaters): the default gas per byte and the default tx size cost per byte could be changed
// through governance. This section could be more robust by tracking these values and adjusting the
// gas limit accordingly (as is done for the gas price)
gasLim = apptypes.EstimateGas(blobSizes, appconsts.DefaultGasPerBlobByte, auth.DefaultTxSizeCostPerByte)
}
minGasPrice := ca.getMinGasPrice()
// set the fee for the user as the minimum gas price multiplied by the gas limit
estimatedFee := false
if fee.IsNegative() {
estimatedFee = true
fee = sdktypes.NewInt(int64(math.Ceil(minGasPrice * float64(gasLim))))
}
var lastErr error
for attempt := 0; attempt < maxRetries; attempt++ {
response, err := appblob.SubmitPayForBlob(
ctx,
ca.signer,
ca.coreConn,
sdktx.BroadcastMode_BROADCAST_MODE_BLOCK,
appblobs,
apptypes.SetGasLimit(gasLim),
withFee(fee),
)
// the node is capable of changing the min gas price at any time so we must be able to detect it and
// update our version accordingly
if apperrors.IsInsufficientMinGasPrice(err) && estimatedFee {
// The error message contains enough information to parse the new min gas price
minGasPrice, err = apperrors.ParseInsufficientMinGasPrice(err, minGasPrice, gasLim)
if err != nil {
return nil, fmt.Errorf("parsing insufficient min gas price error: %w", err)
}
ca.setMinGasPrice(minGasPrice)
lastErr = err
// update the fee to retry again
fee = sdktypes.NewInt(int64(math.Ceil(minGasPrice * float64(gasLim))))
continue
}
// metrics should only be counted on a successful PFD tx
if err == nil && response.Code == 0 {
ca.markSuccessfulPFB()
}
if response != nil && response.Code != 0 {
err = errors.Join(err, sdkErrors.ABCIError(response.Codespace, response.Code, response.Logs.String()))
}
return response, err
}
return nil, fmt.Errorf("failed to submit blobs after %d attempts: %w", maxRetries, lastErr)
}
func (ca *CoreAccessor) AccountAddress(context.Context) (Address, error) {
addr, err := ca.signer.GetSignerInfo().GetAddress()
if err != nil {
return Address{nil}, err
}
return Address{addr}, nil
}
func (ca *CoreAccessor) Balance(ctx context.Context) (*Balance, error) {
addr, err := ca.signer.GetSignerInfo().GetAddress()
if err != nil {
return nil, err
}
return ca.BalanceForAddress(ctx, Address{addr})
}
func (ca *CoreAccessor) BalanceForAddress(ctx context.Context, addr Address) (*Balance, error) {
head, err := ca.getter.Head(ctx)
if err != nil {
return nil, err
}
// construct an ABCI query for the height at head-1 because
// the AppHash contained in the head is actually the state root
// after applying the transactions contained in the previous block.
// TODO @renaynay: once https://github.com/cosmos/cosmos-sdk/pull/12674 is merged, use this method
// instead
prefixedAccountKey := append(banktypes.CreateAccountBalancesPrefix(addr.Bytes()), []byte(app.BondDenom)...)
abciReq := abci.RequestQuery{
// TODO @renayay: once https://github.com/cosmos/cosmos-sdk/pull/12674 is merged, use const instead
Path: fmt.Sprintf("store/%s/key", banktypes.StoreKey),
Height: int64(head.Height() - 1),
Data: prefixedAccountKey,
Prove: true,
}
opts := rpcclient.ABCIQueryOptions{
Height: abciReq.Height,
Prove: abciReq.Prove,
}
result, err := ca.rpcCli.ABCIQueryWithOptions(ctx, abciReq.Path, abciReq.Data, opts)
if err != nil {
return nil, err
}
if !result.Response.IsOK() {
return nil, sdkErrorToGRPCError(result.Response)
}
// unmarshal balance information
value := result.Response.Value
// if the value returned is empty, the account balance does not yet exist
if len(value) == 0 {
log.Errorf("balance for account %s does not exist at block height %d", addr.String(), head.Height()-1)
return &Balance{
Denom: app.BondDenom,
Amount: sdktypes.NewInt(0),
}, nil
}
coin, ok := sdktypes.NewIntFromString(string(value))
if !ok {
return nil, fmt.Errorf("cannot convert %s into sdktypes.Int", string(value))
}
// verify balance
err = ca.prt.VerifyValueFromKeys(
result.Response.GetProofOps(),
head.AppHash,
[][]byte{[]byte(banktypes.StoreKey),
prefixedAccountKey,
}, value)
if err != nil {
return nil, err
}
return &Balance{
Denom: app.BondDenom,
Amount: coin,
}, nil
}
func (ca *CoreAccessor) SubmitTx(ctx context.Context, tx Tx) (*TxResponse, error) {
txResp, err := apptypes.BroadcastTx(ctx, ca.coreConn, sdktx.BroadcastMode_BROADCAST_MODE_BLOCK, tx)
if err != nil {
return nil, err
}
return txResp.TxResponse, nil
}
func (ca *CoreAccessor) SubmitTxWithBroadcastMode(
ctx context.Context,
tx Tx,
mode sdktx.BroadcastMode,
) (*TxResponse, error) {
txResp, err := apptypes.BroadcastTx(ctx, ca.coreConn, mode, tx)
if err != nil {
return nil, err
}
return txResp.TxResponse, nil
}
func (ca *CoreAccessor) Transfer(
ctx context.Context,
addr AccAddress,
amount,
fee Int,
gasLim uint64,
) (*TxResponse, error) {
if amount.IsNil() || amount.Int64() <= 0 {
return nil, ErrInvalidAmount
}
from, err := ca.signer.GetSignerInfo().GetAddress()
if err != nil {
return nil, err
}
coins := sdktypes.NewCoins(sdktypes.NewCoin(app.BondDenom, amount))
msg := banktypes.NewMsgSend(from, addr, coins)
signedTx, err := ca.constructSignedTx(ctx, msg, apptypes.SetGasLimit(gasLim), withFee(fee))
if err != nil {
return nil, err
}
return ca.SubmitTx(ctx, signedTx)
}
func (ca *CoreAccessor) CancelUnbondingDelegation(
ctx context.Context,
valAddr ValAddress,
amount,
height,
fee Int,
gasLim uint64,
) (*TxResponse, error) {
if amount.IsNil() || amount.Int64() <= 0 {
return nil, ErrInvalidAmount
}
from, err := ca.signer.GetSignerInfo().GetAddress()
if err != nil {
return nil, err
}
coins := sdktypes.NewCoin(app.BondDenom, amount)
msg := stakingtypes.NewMsgCancelUnbondingDelegation(from, valAddr, height.Int64(), coins)
signedTx, err := ca.constructSignedTx(ctx, msg, apptypes.SetGasLimit(gasLim), withFee(fee))
if err != nil {
return nil, err
}
return ca.SubmitTx(ctx, signedTx)
}
func (ca *CoreAccessor) BeginRedelegate(
ctx context.Context,
srcValAddr,
dstValAddr ValAddress,
amount,
fee Int,
gasLim uint64,
) (*TxResponse, error) {
if amount.IsNil() || amount.Int64() <= 0 {
return nil, ErrInvalidAmount
}
from, err := ca.signer.GetSignerInfo().GetAddress()
if err != nil {
return nil, err
}
coins := sdktypes.NewCoin(app.BondDenom, amount)
msg := stakingtypes.NewMsgBeginRedelegate(from, srcValAddr, dstValAddr, coins)
signedTx, err := ca.constructSignedTx(ctx, msg, apptypes.SetGasLimit(gasLim), withFee(fee))
if err != nil {
return nil, err
}
return ca.SubmitTx(ctx, signedTx)
}
func (ca *CoreAccessor) Undelegate(
ctx context.Context,
delAddr ValAddress,
amount,
fee Int,
gasLim uint64,
) (*TxResponse, error) {
if amount.IsNil() || amount.Int64() <= 0 {
return nil, ErrInvalidAmount
}
from, err := ca.signer.GetSignerInfo().GetAddress()
if err != nil {
return nil, err
}
coins := sdktypes.NewCoin(app.BondDenom, amount)
msg := stakingtypes.NewMsgUndelegate(from, delAddr, coins)
signedTx, err := ca.constructSignedTx(ctx, msg, apptypes.SetGasLimit(gasLim), withFee(fee))
if err != nil {
return nil, err
}
return ca.SubmitTx(ctx, signedTx)
}
func (ca *CoreAccessor) Delegate(
ctx context.Context,
delAddr ValAddress,
amount Int,
fee Int,
gasLim uint64,
) (*TxResponse, error) {
if amount.IsNil() || amount.Int64() <= 0 {
return nil, ErrInvalidAmount
}
from, err := ca.signer.GetSignerInfo().GetAddress()
if err != nil {
return nil, err
}
coins := sdktypes.NewCoin(app.BondDenom, amount)
msg := stakingtypes.NewMsgDelegate(from, delAddr, coins)
signedTx, err := ca.constructSignedTx(ctx, msg, apptypes.SetGasLimit(gasLim), withFee(fee))
if err != nil {
return nil, err
}
return ca.SubmitTx(ctx, signedTx)
}
func (ca *CoreAccessor) QueryDelegation(
ctx context.Context,
valAddr ValAddress,
) (*stakingtypes.QueryDelegationResponse, error) {
delAddr, err := ca.signer.GetSignerInfo().GetAddress()
if err != nil {
return nil, err
}
return ca.stakingCli.Delegation(ctx, &stakingtypes.QueryDelegationRequest{
DelegatorAddr: delAddr.String(),
ValidatorAddr: valAddr.String(),
})
}
func (ca *CoreAccessor) QueryUnbonding(
ctx context.Context,
valAddr ValAddress,
) (*stakingtypes.QueryUnbondingDelegationResponse, error) {
delAddr, err := ca.signer.GetSignerInfo().GetAddress()
if err != nil {
return nil, err
}
return ca.stakingCli.UnbondingDelegation(ctx, &stakingtypes.QueryUnbondingDelegationRequest{
DelegatorAddr: delAddr.String(),
ValidatorAddr: valAddr.String(),
})
}
func (ca *CoreAccessor) QueryRedelegations(
ctx context.Context,
srcValAddr,
dstValAddr ValAddress,
) (*stakingtypes.QueryRedelegationsResponse, error) {
delAddr, err := ca.signer.GetSignerInfo().GetAddress()
if err != nil {
return nil, err
}
return ca.stakingCli.Redelegations(ctx, &stakingtypes.QueryRedelegationsRequest{
DelegatorAddr: delAddr.String(),
SrcValidatorAddr: srcValAddr.String(),
DstValidatorAddr: dstValAddr.String(),
})
}
func (ca *CoreAccessor) LastPayForBlob() int64 {
ca.lock.Lock()
defer ca.lock.Unlock()
return ca.lastPayForBlob
}
func (ca *CoreAccessor) PayForBlobCount() int64 {
ca.lock.Lock()
defer ca.lock.Unlock()
return ca.payForBlobCount
}
func (ca *CoreAccessor) markSuccessfulPFB() {
ca.lock.Lock()
defer ca.lock.Unlock()
ca.lastPayForBlob = time.Now().UnixMilli()
ca.payForBlobCount++
}
func (ca *CoreAccessor) setMinGasPrice(minGasPrice float64) {
ca.lock.Lock()
defer ca.lock.Unlock()
ca.minGasPrice = minGasPrice
}
func (ca *CoreAccessor) getMinGasPrice() float64 {
ca.lock.Lock()
defer ca.lock.Unlock()
return ca.minGasPrice
}
// QueryMinimumGasPrice returns the minimum gas price required by the node.
func (ca *CoreAccessor) queryMinimumGasPrice(
ctx context.Context,
) (float64, error) {
rsp, err := nodeservice.NewServiceClient(ca.coreConn).Config(ctx, &nodeservice.ConfigRequest{})
if err != nil {
return 0, err
}
coins, err := sdktypes.ParseDecCoins(rsp.MinimumGasPrice)
if err != nil {
return 0, err
}
return coins.AmountOf(app.BondDenom).MustFloat64(), nil
}
func (ca *CoreAccessor) IsStopped(context.Context) bool {
return ca.ctx.Err() != nil
}
func withFee(fee Int) apptypes.TxBuilderOption {
gasFee := sdktypes.NewCoins(sdktypes.NewCoin(app.BondDenom, fee))
return apptypes.SetFeeAmount(gasFee)
}