Skip to content

Commit

Permalink
Merge pull request #8 from celenium-io/feature/bridged-account
Browse files Browse the repository at this point in the history
Feature: bridge accounts
  • Loading branch information
aopoltorzhicky authored Mar 23, 2024
2 parents 363f614 + b8738ad commit 835165b
Show file tree
Hide file tree
Showing 22 changed files with 188 additions and 49 deletions.
10 changes: 10 additions & 0 deletions cmd/api/docs/docs.go

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

7 changes: 7 additions & 0 deletions cmd/api/docs/swagger.json

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

5 changes: 5 additions & 0 deletions cmd/api/docs/swagger.yaml

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

9 changes: 7 additions & 2 deletions cmd/api/handler/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ func (handler *AddressHandler) Get(c echo.Context) error {
return handleError(c, err, handler.address)
}

return c.JSON(http.StatusOK, responses.NewAddress(address))
rollup, err := handler.rollups.ByBridgeAddress(c.Request().Context(), address.Id)
if err != nil {
return handleError(c, err, handler.address)
}

return c.JSON(http.StatusOK, responses.NewAddress(address, &rollup))
}

// List godoc
Expand Down Expand Up @@ -111,7 +116,7 @@ func (handler *AddressHandler) List(c echo.Context) error {

response := make([]responses.Address, len(address))
for i := range address {
response[i] = responses.NewAddress(address[i])
response[i] = responses.NewAddress(address[i], nil)
}

return returnArray(c, response)
Expand Down
6 changes: 6 additions & 0 deletions cmd/api/handler/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (s *AddressTestSuite) TestGet() {
Return(testAddress, nil).
Times(1)

s.rollups.EXPECT().
ByBridgeAddress(gomock.Any(), testAddress.Id).
Return(testRollup, nil).
Times(1)

s.Require().NoError(s.handler.Get(c))
s.Require().Equal(http.StatusOK, rec.Code)

Expand All @@ -82,6 +87,7 @@ func (s *AddressTestSuite) TestGet() {
s.Require().EqualValues(0, address.Height)
s.Require().EqualValues(10, address.Nonce)
s.Require().Equal(testAddressHash, address.Hash)
s.Require().Equal(testRollup.String(), address.BridgedRollup)
}

func (s *AddressTestSuite) TestGetInvalidAddress() {
Expand Down
10 changes: 6 additions & 4 deletions cmd/api/handler/responses/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package responses

import (
"encoding/hex"

"github.com/celenium-io/astria-indexer/internal/storage"
pkgTypes "github.com/celenium-io/astria-indexer/pkg/types"
)
Expand All @@ -21,16 +19,17 @@ type Address struct {
Nonce uint32 `example:"10" json:"nonce" swaggertype:"integer"`
Hash string `example:"115F94D8C98FFD73FE65182611140F0EDC7C3C94" json:"hash" swaggertype:"string"`
Balance *Balance `json:"balance,omitempty"`
BridgedRollup string `json:"bridged_rollup,omitempty"`
}

func NewAddress(addr storage.Address) Address {
func NewAddress(addr storage.Address, bridgedRollup *storage.Rollup) Address {
result := Address{
Id: addr.Id,
Height: addr.Height,
ActionsCount: addr.ActionsCount,
SignedTxCount: addr.SignedTxCount,
Nonce: addr.Nonce,
Hash: hex.EncodeToString(addr.Hash),
Hash: addr.String(),
}

if addr.Balance != nil {
Expand All @@ -39,6 +38,9 @@ func NewAddress(addr storage.Address) Address {
Value: addr.Balance.Total.String(),
}
}
if bridgedRollup != nil {
result.BridgedRollup = bridgedRollup.String()
}

return result
}
Expand Down
19 changes: 13 additions & 6 deletions cmd/api/handler/responses/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ import (
)

type Rollup struct {
Id uint64 `example:"321" json:"id" swaggertype:"integer"`
FirstHeight types.Level `example:"100" json:"first_height" swaggertype:"integer"`
AstriaId string `example:"19ba8abb3e4b56a309df6756c47b97e298e3a72d88449d36a0fadb1ca7366539" json:"hash" swaggertype:"string"`
ActionsCount int64 `example:"100" json:"actions_count" swaggertype:"integer"`
Size int64 `example:"100" json:"size" swaggertype:"integer"`
Id uint64 `example:"321" json:"id" swaggertype:"integer"`
FirstHeight types.Level `example:"100" json:"first_height" swaggertype:"integer"`
AstriaId string `example:"19ba8abb3e4b56a309df6756c47b97e298e3a72d88449d36a0fadb1ca7366539" json:"hash" swaggertype:"string"`
ActionsCount int64 `example:"100" json:"actions_count" swaggertype:"integer"`
Size int64 `example:"100" json:"size" swaggertype:"integer"`
BridgeAddress string `example:"115F94D8C98FFD73FE65182611140F0EDC7C3C94" json:"bridge_address,omitempty" swaggertype:"string"`
}

func NewRollup(rollup *storage.Rollup) Rollup {
return Rollup{
r := Rollup{
Id: rollup.Id,
AstriaId: hex.EncodeToString(rollup.AstriaId),
FirstHeight: rollup.FirstHeight,
ActionsCount: rollup.ActionsCount,
Size: rollup.Size,
}

if rollup.BridgeAddress != nil {
r.BridgeAddress = rollup.BridgeAddress.String()
}

return r
}
2 changes: 1 addition & 1 deletion cmd/api/handler/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (handler *RollupHandler) Addresses(c echo.Context) error {
response := make([]responses.Address, len(addresses))
for i := range addresses {
if addresses[i].Address != nil {
response[i] = responses.NewAddress(*addresses[i].Address)
response[i] = responses.NewAddress(*addresses[i].Address, nil)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handler/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *SearchHandler) Search(c echo.Context) error {
return internalServerError(c, err)
}
results := []responses.SearchResult{
responses.NewSearchResult(address.String(), "address", responses.NewAddress(address)),
responses.NewSearchResult(address.String(), "address", responses.NewAddress(address, nil)),
}
return returnArray(c, results)
}
Expand Down
39 changes: 39 additions & 0 deletions internal/storage/mock/rollup.go

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

9 changes: 9 additions & 0 deletions internal/storage/postgres/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ func createIndices(ctx context.Context, conn *database.Bun) error {
Exec(ctx); err != nil {
return err
}
if _, err := tx.NewCreateIndex().
IfNotExists().
Model((*storage.Rollup)(nil)).
Index("rollup_bridge_address_id_idx").
Column("bridge_address_id").
Where("bridge_address_id is not null").
Exec(ctx); err != nil {
return err
}

// Rollup actions
if _, err := tx.NewCreateIndex().
Expand Down
20 changes: 18 additions & 2 deletions internal/storage/postgres/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,16 @@ func (r *Rollup) CountActionsByTxId(ctx context.Context, txId uint64) (int64, er
}

func (r *Rollup) ByHash(ctx context.Context, hash []byte) (rollup storage.Rollup, err error) {
err = r.DB().NewSelect().Model(&rollup).
query := r.DB().NewSelect().Model((*storage.Rollup)(nil)).
Where("astria_id = ?", hash).
Scan(ctx)
Limit(1)

err = r.DB().NewSelect().
TableExpr("(?) as rollup", query).
ColumnExpr("rollup.*").
ColumnExpr("address.hash as bridge_address__hash").
Join("left join address on address.id = rollup.bridge_address_id").
Scan(ctx, &rollup)
return
}

Expand Down Expand Up @@ -113,3 +120,12 @@ func (r *Rollup) ListExt(ctx context.Context, fltrs storage.RollupListFilter) (r
err = query.Scan(ctx)
return
}

func (r *Rollup) ByBridgeAddress(ctx context.Context, id uint64) (rollup storage.Rollup, err error) {
err = r.DB().NewSelect().
Model(&rollup).
Where("bridge_address_id = ?", id).
Limit(1).
Scan(ctx)
return
}
26 changes: 13 additions & 13 deletions internal/storage/postgres/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func (s *StorageTestSuite) TestRollupByHash() {
s.Require().EqualValues(hash, rollup.AstriaId)
s.Require().EqualValues(112, rollup.Size)
s.Require().EqualValues(1, rollup.ActionsCount)

s.Require().Nil(rollup.BridgeAddress)
}

func (s *StorageTestSuite) TestRollupAddresses() {
Expand Down Expand Up @@ -122,25 +124,23 @@ func (s *StorageTestSuite) TestListExt() {
"",
} {
rollups, err := s.storage.Rollup.ListExt(ctx, models.RollupListFilter{
Limit: 1,
Limit: 2,
Offset: 0,
SortField: field,
SortOrder: order,
})
s.Require().NoError(err)
s.Require().Len(rollups, 1)

rollup := rollups[0]

hash, err := hex.DecodeString("19ba8abb3e4b56a309df6756c47b97e298e3a72d88449d36a0fadb1ca7366539")
s.Require().NoError(err)

s.Require().EqualValues(1, rollup.Id)
s.Require().EqualValues(hash, rollup.AstriaId)
s.Require().EqualValues(112, rollup.Size)
s.Require().EqualValues(1, rollup.ActionsCount)
s.Require().EqualValues(7316, rollup.FirstHeight)
s.Require().Len(rollups, 2)
}
}

}

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

rollup, err := s.storage.Rollup.ByBridgeAddress(ctx, 2)
s.Require().NoError(err)
s.Require().EqualValues(2, rollup.Id)
}
12 changes: 6 additions & 6 deletions internal/storage/postgres/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ func (tx Transaction) SaveRollups(ctx context.Context, rollups ...*models.Rollup
rs[i].Rollup = rollups[i]
}

_, err := tx.Tx().NewInsert().Model(&rs).
Column("first_height", "astria_id", "actions_count", "size").
query := tx.Tx().NewInsert().Model(&rs).
Column("first_height", "astria_id", "actions_count", "size", "bridge_address_id").
On("CONFLICT ON CONSTRAINT rollup_id DO UPDATE").
Set("actions_count = added_rollup.actions_count + EXCLUDED.actions_count").
Set("size = added_rollup.size + EXCLUDED.size").
Returning("xmax, id").
Exec(ctx)
if err != nil {
Set("bridge_address_id = case when EXCLUDED.bridge_address_id is not null then EXCLUDED.bridge_address_id else added_rollup.bridge_address_id end")

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

Expand All @@ -149,7 +149,7 @@ func (tx Transaction) SaveRollups(ctx context.Context, rollups ...*models.Rollup
}
}

return count, err
return count, nil
}

func (tx Transaction) SaveRollupActions(ctx context.Context, actions ...*models.RollupAction) error {
Expand Down
7 changes: 7 additions & 0 deletions internal/storage/postgres/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ func (s *TransactionTestSuite) TestSaveRollups() {
ActionsCount: 1,
Size: 10,
}
if i%2 == 1 {
rollups[i].BridgeAddressId = uint64(i)
}
}

count, err := tx.SaveRollups(ctx, rollups...)
Expand All @@ -256,6 +259,10 @@ func (s *TransactionTestSuite) TestSaveRollups() {

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

ret, err := s.storage.Rollup.List(ctx, 10, 0, sdk.SortOrderAsc)
s.Require().NoError(err)
s.Require().Len(ret, 7)
}

func (s *TransactionTestSuite) TestSaveRollupActions() {
Expand Down
Loading

0 comments on commit 835165b

Please sign in to comment.