Skip to content

Commit

Permalink
feat (rollapp): register balances l2 (#1247)
Browse files Browse the repository at this point in the history
Co-authored-by: Faulty Tolly <@faulttolerance.net>
Co-authored-by: Sergi Rene <[email protected]>
Co-authored-by: keruch <[email protected]>
Co-authored-by: Daniel T <[email protected]>
Co-authored-by: keruch <[email protected]>
  • Loading branch information
5 people authored Nov 22, 2024
1 parent 98a3348 commit cf2976a
Show file tree
Hide file tree
Showing 22 changed files with 467 additions and 10 deletions.
93 changes: 93 additions & 0 deletions block/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package block

import (
"context"
"fmt"
"strconv"
"sync"
"time"

"github.com/cockroachdb/errors"
"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/types"
)

const CheckBalancesInterval = 3 * time.Minute

// MonitorBalances checks the balances of the node and updates the gauges for prometheus
func (m *Manager) MonitorBalances(ctx context.Context) error {
ticker := time.NewTicker(CheckBalancesInterval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
m.logger.Info("Checking balances.")
balances, err := m.checkBalances()

if balances.DA != nil {
if amountFloat, errDA := strconv.ParseFloat(balances.DA.Amount.String(), 64); errDA == nil {
types.DaLayerBalanceGauge.Set(amountFloat)
} else {
m.logger.Error("Parsing DA balance amount", "error", errDA)
}
}

if balances.SL != nil {
if amountFloat, errSL := strconv.ParseFloat(balances.SL.Amount.String(), 64); errSL == nil {
types.HubLayerBalanceGauge.Set(amountFloat)
} else {
m.logger.Error("Parsing SL balance amount", "error", errSL)
}
}

if err != nil {
m.logger.Error("Checking balances", "error", err)
}
}
}
}

type Balances struct {
DA *da.Balance
SL *types.Balance
}

func (m *Manager) checkBalances() (*Balances, error) {
balances := &Balances{}
var wg sync.WaitGroup
wg.Add(2)

var errDA, errSL error

go func() {
defer wg.Done()
balance, err := m.DAClient.GetSignerBalance()
if err != nil {
errDA = fmt.Errorf("get DA signer balance: %w", err)
return
}
balances.DA = &balance
}()

go func() {
defer wg.Done()
balance, err := m.SLClient.GetSignerBalance()
if err != nil {
errSL = fmt.Errorf("get SL signer balance: %w", err)
return
}
balances.SL = &balance
}()

wg.Wait()

errs := errors.Join(errDA, errSL)
if errs != nil {
return balances, fmt.Errorf("errors checking balances: %w", errs)
}

return balances, nil
}
4 changes: 4 additions & 0 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ func (m *Manager) Start(ctx context.Context) error {
return m.MonitorForkUpdateLoop(ctx)
})

uerrors.ErrGroupGoLog(eg, m.logger, func() error {
return m.MonitorBalances(ctx)
})

// run based on the node role
if !amIProposer {
return m.runAsFullNode(ctx, eg)
Expand Down
7 changes: 6 additions & 1 deletion da/avail/avail.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func WithBatchRetryAttempts(attempts uint) da.Option {
}

// Init initializes DataAvailabilityLayerClient instance.
func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KV, logger types.Logger, options ...da.Option) error {
func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, _ store.KV, logger types.Logger, options ...da.Option) error {
c.logger = logger
c.synced = make(chan struct{}, 1)

Expand Down Expand Up @@ -442,3 +442,8 @@ func (c *DataAvailabilityLayerClient) getHeightFromHash(hash availtypes.Hash) (u
func (d *DataAvailabilityLayerClient) GetMaxBlobSizeBytes() uint32 {
return maxBlobSize
}

// GetBalance returns the balance for a specific address
func (c *DataAvailabilityLayerClient) GetSignerBalance() (da.Balance, error) {
return da.Balance{}, nil
}
20 changes: 19 additions & 1 deletion da/celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func WithSubmitBackoff(c uretry.BackoffConfig) da.Option {
}

// Init initializes DataAvailabilityLayerClient instance.
func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KV, logger types.Logger, options ...da.Option) error {
func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, _ store.KV, logger types.Logger, options ...da.Option) error {
c.logger = logger
c.synced = make(chan struct{}, 1)
var err error
Expand Down Expand Up @@ -605,3 +605,21 @@ func (c *DataAvailabilityLayerClient) sync(rpc *openrpc.Client) {
func (d *DataAvailabilityLayerClient) GetMaxBlobSizeBytes() uint32 {
return maxBlobSizeBytes
}

// GetSignerBalance returns the balance for a specific address
func (d *DataAvailabilityLayerClient) GetSignerBalance() (da.Balance, error) {
ctx, cancel := context.WithTimeout(d.ctx, d.config.Timeout)
defer cancel()

balance, err := d.rpc.GetSignerBalance(ctx)
if err != nil {
return da.Balance{}, fmt.Errorf("get balance: %w", err)
}

daBalance := da.Balance{
Amount: balance.Amount,
Denom: balance.Denom,
}

return daBalance, nil
}
12 changes: 9 additions & 3 deletions da/celestia/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/celestiaorg/celestia-openrpc/types/blob"
"github.com/celestiaorg/celestia-openrpc/types/header"
"github.com/celestiaorg/celestia-openrpc/types/share"
"github.com/celestiaorg/celestia-openrpc/types/state"

"github.com/dymensionxyz/dymint/da/celestia/types"
)
Expand Down Expand Up @@ -35,7 +36,7 @@ func (c *OpenRPC) Submit(ctx context.Context, blobs []*blob.Blob, options *blob.
return c.rpc.Blob.Submit(ctx, blobs, options)
}

// Getting proof for submitted blob
// GetProof gets the proof for a specific share commitment.
func (c *OpenRPC) GetProof(ctx context.Context, height uint64, namespace share.Namespace, commitment blob.Commitment) (*blob.Proof, error) {
return c.rpc.Blob.GetProof(ctx, height, namespace, commitment)
}
Expand All @@ -45,12 +46,17 @@ func (c *OpenRPC) Get(ctx context.Context, height uint64, namespace share.Namesp
return c.rpc.Blob.Get(ctx, height, namespace, commitment)
}

// Get extended Celestia headers for a specific height
// GetByHeight gets the header by height
func (c *OpenRPC) GetByHeight(ctx context.Context, height uint64) (*header.ExtendedHeader, error) {
return c.rpc.Header.GetByHeight(ctx, height)
}

// Get extended Celestia headers for a specific height
// Included checks if a blob is included in the chain
func (c *OpenRPC) Included(ctx context.Context, height uint64, namespace share.Namespace, proof *blob.Proof, commitment blob.Commitment) (bool, error) {
return c.rpc.Blob.Included(ctx, height, namespace, proof, commitment)
}

// GetSignerBalance balance for a specific address
func (c *OpenRPC) GetSignerBalance(ctx context.Context) (*state.Balance, error) {
return c.rpc.State.Balance(ctx)
}
4 changes: 4 additions & 0 deletions da/celestia/types/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/celestiaorg/celestia-openrpc/types/blob"
"github.com/celestiaorg/celestia-openrpc/types/header"
"github.com/celestiaorg/celestia-openrpc/types/share"
"github.com/celestiaorg/celestia-openrpc/types/state"
)

type CelestiaRPCClient interface {
Expand All @@ -18,4 +19,7 @@ type CelestiaRPCClient interface {

/* --------------------------------- header --------------------------------- */
GetByHeight(ctx context.Context, height uint64) (*header.ExtendedHeader, error)

/* ---------------------------------- state --------------------------------- */
GetSignerBalance(ctx context.Context) (*state.Balance, error)
}
9 changes: 9 additions & 0 deletions da/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"cosmossdk.io/math"
"github.com/celestiaorg/celestia-openrpc/types/blob"
"github.com/cometbft/cometbft/crypto/merkle"
"github.com/dymensionxyz/dymint/store"
Expand Down Expand Up @@ -74,6 +75,11 @@ type DASubmitMetaData struct {
Root []byte
}

type Balance struct {
Amount math.Int
Denom string
}

const PathSeparator = "|"

// ToPath converts a DAMetaData to a path.
Expand Down Expand Up @@ -221,6 +227,9 @@ type DataAvailabilityLayerClient interface {

// Returns the maximum allowed blob size in the DA, used to check the max batch size configured
GetMaxBlobSizeBytes() uint32

// GetSignerBalance returns the balance for a specific address
GetSignerBalance() (Balance, error)
}

// BatchRetriever is additional interface that can be implemented by Data Availability Layer Client that is able to retrieve
Expand Down
8 changes: 8 additions & 0 deletions da/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"strconv"

"cosmossdk.io/math"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

Expand Down Expand Up @@ -153,3 +154,10 @@ func (d *DataAvailabilityLayerClient) RetrieveBatches(daMetaData *da.DASubmitMet
Batches: batches,
}
}

func (d *DataAvailabilityLayerClient) GetSignerBalance() (da.Balance, error) {
return da.Balance{
Amount: math.ZeroInt(),
Denom: "adym",
}, nil
}
8 changes: 8 additions & 0 deletions da/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync/atomic"
"time"

"cosmossdk.io/math"
"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/store"
"github.com/dymensionxyz/dymint/types"
Expand Down Expand Up @@ -181,3 +182,10 @@ func (m *DataAvailabilityLayerClient) updateDAHeight() {
func (d *DataAvailabilityLayerClient) GetMaxBlobSizeBytes() uint32 {
return maxBlobSize
}

func (m *DataAvailabilityLayerClient) GetSignerBalance() (da.Balance, error) {
return da.Balance{
Amount: math.ZeroInt(),
Denom: "adym",
}, nil
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12
github.com/cosmos/cosmos-sdk v0.46.16
github.com/dgraph-io/badger/v4 v4.3.0
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20240821081230-b4018b2bac13
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20241121093220-e0d7ad456fbd
github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241119103059-def6322e4345
github.com/dymensionxyz/gerr-cosmos v1.0.0
github.com/go-kit/kit v0.12.0
Expand Down Expand Up @@ -257,7 +257,7 @@ require (
)

require (
cosmossdk.io/math v1.3.0 // indirect
cosmossdk.io/math v1.3.0
github.com/DataDog/zstd v1.5.5 // indirect
github.com/Jorropo/jsync v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQx
github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/dymensionxyz/cometbft v0.34.29-0.20241104165035-feade34f8f89 h1:rGkCcx4dWX9mxAUrq7zrdOc44XddMY/nM6kqYTWjerY=
github.com/dymensionxyz/cometbft v0.34.29-0.20241104165035-feade34f8f89/go.mod h1:L9shMfbkZ8B+7JlwANEr+NZbBcn+hBpwdbeYvA5rLCw=
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20240821081230-b4018b2bac13 h1:u5yeve5jZR6TdRjjR+vYT/8PWKbhwCZxUmAu+/Tnxyg=
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20240821081230-b4018b2bac13/go.mod h1:jabDQYXrccscSE0fXkh7eQFYPWJCRiuWKonFGObVq6s=
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20241121093220-e0d7ad456fbd h1:V89QyOFM84o9w0iFdctMU6So8SS/Xt32JWAXGqJduT0=
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20241121093220-e0d7ad456fbd/go.mod h1:3weqpVj/TqTFpC0LjEB3H+HZSpm7BrQ1QkEg1Ahy6KY=
github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241119103059-def6322e4345 h1:FcHidPgGEHh9ELwodNJkGcHqsG+mdPiGdughzG4W+X8=
github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241119103059-def6322e4345/go.mod h1:y89w1OG4C4aF7yyW8bv9PwV3o1KkCx1hyt34ap04Rnk=
github.com/dymensionxyz/evmos/v12 v12.1.6-dymension-v0.3 h1:vmAdUGUc4rTIiO3Phezr7vGq+0uPDVKSA4WAe8+yl6w=
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cf2976a

Please sign in to comment.