Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: total metrics #46

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/api/docs/docs.go

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

8 changes: 4 additions & 4 deletions cmd/api/docs/swagger.json

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

8 changes: 4 additions & 4 deletions cmd/api/docs/swagger.yaml

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

4 changes: 2 additions & 2 deletions cmd/api/handler/responses/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type State struct {
TotalTx int64 `example:"23456" format:"int64" json:"total_tx" swaggertype:"integer"`
TotalAccounts int64 `example:"43" format:"int64" json:"total_accounts" swaggertype:"integer"`
TotalRollups int64 `example:"312" format:"int64" json:"total_rollups" swaggertype:"integer"`
TotalFee string `example:"312" format:"string" json:"total_fee" swaggertype:"string"`
TotalBridges int64 `example:"312" format:"int64" json:"total_bridges" swaggertype:"integer"`
TotalSupply string `example:"312" format:"string" json:"total_supply" swaggertype:"string"`
Synced bool `example:"true" format:"boolean" json:"synced" swaggertype:"boolean"`
}
Expand All @@ -38,7 +38,7 @@ func NewState(state storage.State) State {
TotalTx: state.TotalTx,
TotalAccounts: state.TotalAccounts,
TotalRollups: state.TotalRollups,
TotalFee: state.TotalFee.String(),
TotalBridges: state.TotalBridges,
TotalSupply: state.TotalSupply.String(),
Synced: !state.LastTime.UTC().Add(2 * time.Minute).Before(time.Now().UTC()),
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/api/handler/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/celenium-io/astria-indexer/internal/storage"
"github.com/celenium-io/astria-indexer/internal/storage/mock"
"github.com/labstack/echo/v4"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"
)
Expand All @@ -31,7 +30,7 @@ var (
LastTime: testTime,
TotalTx: 1234,
TotalAccounts: 123,
TotalFee: decimal.RequireFromString("2"),
TotalBridges: 2,
TotalRollups: 30,
}
)
Expand Down Expand Up @@ -88,7 +87,7 @@ func (s *StateTestSuite) TestHead() {
s.Require().EqualValues(100, state.LastHeight)
s.Require().EqualValues(1234, state.TotalTx)
s.Require().EqualValues(123, state.TotalAccounts)
s.Require().Equal("2", state.TotalFee)
s.Require().EqualValues(2, state.TotalBridges)
s.Require().EqualValues(30, state.TotalRollups)
s.Require().Equal(testTime, state.LastTime)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/storage/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Transaction interface {
SaveBalances(ctx context.Context, balances ...Balance) error
SaveBalanceUpdates(ctx context.Context, updates ...BalanceUpdate) error
SaveBlockSignatures(ctx context.Context, signs ...BlockSignature) error
SaveBridges(ctx context.Context, bridges ...*Bridge) error
SaveBridges(ctx context.Context, bridges ...*Bridge) (int64, error)
SaveConstants(ctx context.Context, constants ...Constant) error
SaveRollupActions(ctx context.Context, actions ...*RollupAction) error
SaveRollupAddresses(ctx context.Context, addresses ...*RollupAddress) error
Expand All @@ -68,7 +68,7 @@ type Transaction interface {
RollbackBlockSignatures(ctx context.Context, height types.Level) (err error)
RollbackBlockStats(ctx context.Context, height types.Level) (stats BlockStats, err error)
RollbackBlock(ctx context.Context, height types.Level) error
RollbackBridges(ctx context.Context, height types.Level) error
RollbackBridges(ctx context.Context, height types.Level) (int, error)
RollbackRollupActions(ctx context.Context, height types.Level) (rollupActions []RollupAction, err error)
RollbackRollupAddresses(ctx context.Context, height types.Level) (err error)
RollbackRollups(ctx context.Context, height types.Level) ([]Rollup, error)
Expand Down
30 changes: 16 additions & 14 deletions internal/storage/mock/generic.go

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

36 changes: 27 additions & 9 deletions internal/storage/postgres/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,26 @@ func (tx Transaction) SaveBalanceUpdates(ctx context.Context, updates ...models.
return err
}

func (tx Transaction) SaveBridges(ctx context.Context, bridges ...*models.Bridge) error {
type addedBiridge struct {
bun.BaseModel `bun:"bridge"`
*models.Bridge

Xmax uint64 `bun:"xmax"`
}

func (tx Transaction) SaveBridges(ctx context.Context, bridges ...*models.Bridge) (int64, error) {
if len(bridges) == 0 {
return nil
return 0, nil
}

var count int64

for i := range bridges {
query := tx.Tx().NewInsert().Model(bridges[i]).
add := new(addedBiridge)
add.Bridge = bridges[i]

query := tx.Tx().NewInsert().Model(add).
Column("rollup_id", "address_id", "asset", "fee_asset", "sudo_id", "withdrawer_id", "init_height").
On("CONFLICT (address_id) DO UPDATE")

if bridges[i].SudoId > 0 {
Expand All @@ -234,12 +247,16 @@ func (tx Transaction) SaveBridges(ctx context.Context, bridges ...*models.Bridge
query.Set("fee_asset = ?", bridges[i].FeeAsset)
}

if _, err := query.Exec(ctx); err != nil {
return err
if _, err := query.Returning("xmax, id").Exec(ctx); err != nil {
return count, err
}

if add.Xmax == 0 {
count++
}
}

return nil
return count, nil
}

func (tx Transaction) LastBlock(ctx context.Context) (block models.Block, err error) {
Expand Down Expand Up @@ -296,9 +313,10 @@ func (tx Transaction) RollbackBalanceUpdates(ctx context.Context, height types.L
return
}

func (tx Transaction) RollbackBridges(ctx context.Context, height types.Level) (err error) {
_, err = tx.Tx().NewDelete().Model((*models.Bridge)(nil)).Where("height = ?", height).Exec(ctx)
return
func (tx Transaction) RollbackBridges(ctx context.Context, height types.Level) (int, error) {
var bridge []models.Bridge
_, err := tx.Tx().NewDelete().Model(&bridge).Where("init_height = ?", height).Returning("*").Exec(ctx)
return len(bridge), err
}

func (tx Transaction) RollbackAddressActions(ctx context.Context, height types.Level) (addrActions []models.AddressAction, err error) {
Expand Down
18 changes: 17 additions & 1 deletion internal/storage/postgres/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,9 @@ func (s *TransactionTestSuite) TestSaveBridges() {
bridges[i].RollupId = uint64(i + 50)
}

err = tx.SaveBridges(ctx, bridges...)
count, err := tx.SaveBridges(ctx, bridges...)
s.Require().NoError(err)
s.Require().EqualValues(5, count)

s.Require().NoError(tx.Flush(ctx))
s.Require().NoError(tx.Close(ctx))
Expand Down Expand Up @@ -535,6 +536,21 @@ func (s *TransactionTestSuite) TestRollbackBlock() {
s.Require().EqualValues(7964, block.Height)
}

func (s *TransactionTestSuite) TestRollbackBridge() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()

tx, err := BeginTransaction(ctx, s.storage.Transactable)
s.Require().NoError(err)

count, err := tx.RollbackBridges(ctx, 7316)
s.Require().NoError(err)
s.Require().EqualValues(1, count)

s.Require().NoError(tx.Flush(ctx))
s.Require().NoError(tx.Close(ctx))
}

func (s *TransactionTestSuite) TestRollbackBlockStats() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type State struct {
TotalRollups int64 `bun:"total_rollups" comment:"Rollups count" json:"rollups"`
TotalValidators int `bun:"total_validators" comment:"Validators count" json:"validators"`
TotalSupply decimal.Decimal `bun:"total_supply,type:numeric" comment:"Total supply" json:"supply"`
TotalFee decimal.Decimal `bun:"total_fee,type:numeric" comment:"Total paid fee" json:"fee"`
TotalBridges int64 `bun:"total_bridges" comment:"Count of bridges" json:"bridges"`
}

// TableName -
Expand Down
1 change: 0 additions & 1 deletion pkg/indexer/genesis/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (module *Module) save(ctx context.Context, data parsedData) error {
ChainId: data.block.ChainId,
TotalTx: data.block.Stats.TxCount,
TotalSupply: data.block.Stats.SupplyChange,
TotalFee: data.block.Stats.Fee,
TotalAccounts: totalAccounts,
TotalValidators: len(data.validators),
}); err != nil {
Expand Down
7 changes: 6 additions & 1 deletion pkg/indexer/rollback/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ func rollbackBlock(ctx context.Context, tx storage.Transaction, height types.Lev
return err
}

deletedBridges, err := tx.RollbackBridges(ctx, height)
if err != nil {
return errors.Wrap(err, "bridges")
}

newBlock, err := tx.LastBlock(ctx)
if err != nil {
return err
Expand All @@ -233,8 +238,8 @@ func rollbackBlock(ctx context.Context, tx storage.Transaction, height types.Lev
state.TotalTx -= blockStats.TxCount
state.TotalAccounts -= int64(countDeletedAddresses)
state.TotalRollups -= countDeletedRollups
state.TotalFee = state.TotalFee.Sub(blockStats.Fee)
state.TotalSupply = state.TotalSupply.Sub(blockStats.SupplyChange)
state.TotalBridges -= int64(deletedBridges)

if err := tx.Update(ctx, &state); err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion pkg/indexer/rollback/rollback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ func Test_rollbackBlock(t *testing.T) {
Return(nil).
Times(1)

tx.EXPECT().
RollbackBridges(ctx, height).
Return(0, nil).
Times(1)

lastBlock := storage.Block{
Height: height - 1,
Time: blockTime.Add(-time.Minute),
Expand Down Expand Up @@ -291,7 +296,6 @@ func Test_rollbackBlock(t *testing.T) {
TotalAccounts: 10,
TotalRollups: 10,
TotalSupply: decimal.RequireFromString("1000"),
TotalFee: decimal.Zero,
}, nil).
MaxTimes(1).
MinTimes(1)
Expand Down
12 changes: 6 additions & 6 deletions pkg/indexer/storage/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,39 @@ func saveBridges(
tx storage.Transaction,
addrToId map[string]uint64,
bridges []*storage.Bridge,
) error {
) (int64, error) {
if len(bridges) == 0 {
return nil
return 0, nil
}

for i := range bridges {
if id, ok := addrToId[bridges[i].Address.Hash]; ok {
bridges[i].AddressId = id
} else {
return errors.Errorf("unknown bridge address: %s", bridges[i].Address.Hash)
return 0, errors.Errorf("unknown bridge address: %s", bridges[i].Address.Hash)
}

if bridges[i].Sudo != nil {
if id, ok := addrToId[bridges[i].Sudo.Hash]; ok {
bridges[i].SudoId = id
} else {
return errors.Errorf("unknown sudo bridge address: %s", bridges[i].Sudo.Hash)
return 0, errors.Errorf("unknown sudo bridge address: %s", bridges[i].Sudo.Hash)
}
}

if bridges[i].Withdrawer != nil {
if id, ok := addrToId[bridges[i].Withdrawer.Hash]; ok {
bridges[i].WithdrawerId = id
} else {
return errors.Errorf("unknown withdrawer bridge address: %s", bridges[i].Withdrawer.Hash)
return 0, errors.Errorf("unknown withdrawer bridge address: %s", bridges[i].Withdrawer.Hash)
}
}

if bridges[i].Rollup != nil {
if bridges[i].Rollup.Id == 0 {
rollup, err := tx.GetRollup(ctx, bridges[i].Rollup.AstriaId)
if err != nil {
return err
return 0, err
}
bridges[i].RollupId = rollup.Id
} else {
Expand Down
Loading
Loading