Skip to content

Commit

Permalink
Feature: filters for address messages (#86)
Browse files Browse the repository at this point in the history
* Feature: filters for address messages

* Fix: licensec
  • Loading branch information
aopoltorzhicky committed Dec 31, 2023
1 parent 841c607 commit 3c9c796
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 80 deletions.
28 changes: 18 additions & 10 deletions cmd/api/handler/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type AddressHandler struct {
address storage.IAddress
txs storage.ITx
blobLogs storage.IBlobLog
messages storage.IMessage
state storage.IState
indexerName string
}
Expand All @@ -26,13 +27,15 @@ func NewAddressHandler(
address storage.IAddress,
txs storage.ITx,
blobLogs storage.IBlobLog,
messages storage.IMessage,
state storage.IState,
indexerName string,
) *AddressHandler {
return &AddressHandler{
address: address,
txs: txs,
blobLogs: blobLogs,
messages: messages,
state: state,
indexerName: indexerName,
}
Expand Down Expand Up @@ -177,10 +180,11 @@ func (handler *AddressHandler) Transactions(c echo.Context) error {
}

type getAddressMessages struct {
Hash string `param:"hash" validate:"required,address"`
Limit uint64 `query:"limit" validate:"omitempty,min=1,max=100"`
Offset uint64 `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
Hash string `param:"hash" validate:"required,address"`
Limit uint64 `query:"limit" validate:"omitempty,min=1,max=100"`
Offset uint64 `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
MsgType StringArray `query:"msg_type" validate:"omitempty,dive,msg_type"`
}

func (p *getAddressMessages) SetDefault() {
Expand All @@ -190,13 +194,17 @@ func (p *getAddressMessages) SetDefault() {
if p.Sort == "" {
p.Sort = asc
}
if p.MsgType == nil {
p.MsgType = make(StringArray, 0)
}
}

func (p *getAddressMessages) ToFilters() storage.AddressMsgsFilter {
return storage.AddressMsgsFilter{
Limit: int(p.Limit),
Offset: int(p.Offset),
Sort: pgSort(p.Sort),
Limit: int(p.Limit),
Offset: int(p.Offset),
Sort: pgSort(p.Sort),
MessageTypes: p.MsgType,
}
}

Expand All @@ -211,7 +219,7 @@ func (p *getAddressMessages) ToFilters() storage.AddressMsgsFilter {
// @Param offset query integer false "Offset" minimum(1)
// @Param sort query string false "Sort order" Enums(asc, desc)
// @Produce json
// @Success 200 {array} responses.Message
// @Success 200 {array} responses.MessageForAddress
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/address/{hash}/messages [get]
Expand All @@ -234,12 +242,12 @@ func (handler *AddressHandler) Messages(c echo.Context) error {
}

filters := req.ToFilters()
msgs, err := handler.address.Messages(c.Request().Context(), address.Id, filters)
msgs, err := handler.messages.ByAddress(c.Request().Context(), address.Id, filters)
if err != nil {
return handleError(c, err, handler.address)
}

response := make([]responses.Message, len(msgs))
response := make([]responses.MessageForAddress, len(msgs))
for i := range msgs {
response[i] = responses.NewMessageForAddress(msgs[i])
}
Expand Down
25 changes: 18 additions & 7 deletions cmd/api/handler/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type AddressTestSuite struct {
address *mock.MockIAddress
txs *mock.MockITx
blobLogs *mock.MockIBlobLog
messages *mock.MockIMessage
state *mock.MockIState
echo *echo.Echo
handler *AddressHandler
Expand All @@ -57,8 +58,9 @@ func (s *AddressTestSuite) SetupSuite() {
s.address = mock.NewMockIAddress(s.ctrl)
s.txs = mock.NewMockITx(s.ctrl)
s.blobLogs = mock.NewMockIBlobLog(s.ctrl)
s.messages = mock.NewMockIMessage(s.ctrl)
s.state = mock.NewMockIState(s.ctrl)
s.handler = NewAddressHandler(s.address, s.txs, s.blobLogs, s.state, testIndexerName)
s.handler = NewAddressHandler(s.address, s.txs, s.blobLogs, s.messages, s.state, testIndexerName)
}

// TearDownSuite -
Expand Down Expand Up @@ -254,13 +256,15 @@ func (s *AddressTestSuite) TestMessages() {
Address: testAddress,
}, nil)

s.address.EXPECT().
Messages(gomock.Any(), uint64(1), gomock.Any()).
Return([]storage.MsgAddress{
s.messages.EXPECT().
ByAddress(gomock.Any(), uint64(1), gomock.Any()).
Return([]storage.AddressMessageWithTx{
{
AddressId: 1,
MsgId: 1,
Type: types.MsgAddressTypeDelegator,
MsgAddress: storage.MsgAddress{
AddressId: 1,
MsgId: 1,
Type: types.MsgAddressTypeDelegator,
},
Msg: &storage.Message{
Id: 1,
Height: 1000,
Expand All @@ -269,6 +273,12 @@ func (s *AddressTestSuite) TestMessages() {
TxId: 1,
Data: nil,
},
Tx: &storage.Tx{
Id: 1,
MessageTypes: types.NewMsgTypeBitMask(types.MsgWithdrawDelegatorReward),
MessagesCount: 1,
Status: types.StatusSuccess,
},
},
}, nil)

Expand All @@ -285,6 +295,7 @@ func (s *AddressTestSuite) TestMessages() {
s.Require().EqualValues(1000, msg.Height)
s.Require().Equal(int64(0), msg.Position)
s.Require().EqualValues(types.MsgWithdrawDelegatorReward, msg.Type)
s.Require().NotNil(msg.Tx)
}

func (s *AddressTestSuite) TestBlobs() {
Expand Down
39 changes: 27 additions & 12 deletions cmd/api/handler/responses/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ func NewMessage(msg storage.Message) Message {
}
}

func NewMessageForAddress(msg storage.MsgAddress) Message {
return Message{
Id: msg.MsgId,
Height: msg.Msg.Height,
Time: msg.Msg.Time,
Position: msg.Msg.Position,
TxId: msg.Msg.TxId,
Type: msg.Msg.Type,
Data: msg.Msg.Data,
}
}

func NewMessageWithTx(msg storage.MessageWithTx) Message {
message := Message{
Id: msg.Id,
Expand All @@ -67,3 +55,30 @@ func NewMessageWithTx(msg storage.MessageWithTx) Message {

return message
}

type MessageForAddress struct {
Id uint64 `example:"321" format:"int64" json:"id" swaggertype:"integer"`
Height pkgTypes.Level `example:"100" format:"int64" json:"height" swaggertype:"integer"`
Time time.Time `example:"2023-07-04T03:10:57+00:00" format:"date-time" json:"time" swaggertype:"string"`
Position int64 `example:"2" format:"int64" json:"position" swaggertype:"integer"`
TxId uint64 `example:"11" format:"int64" json:"tx_id,omitempty" swaggertype:"integer"`

Type types.MsgType `example:"MsgCreatePeriodicVestingAccount" json:"type"`

Data map[string]any `json:"data"`
Tx TxForAddress `json:"tx"`
}

func NewMessageForAddress(msg storage.AddressMessageWithTx) MessageForAddress {
message := MessageForAddress{
Id: msg.MsgId,
Height: msg.Msg.Height,
Time: msg.Msg.Time,
Position: msg.Msg.Position,
TxId: msg.Msg.TxId,
Type: msg.Msg.Type,
Data: msg.Msg.Data,
Tx: NewTxForAddress(msg.Tx),
}
return message
}
24 changes: 23 additions & 1 deletion cmd/api/handler/responses/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package responses

import (
"encoding/hex"
pkgTypes "github.com/celenium-io/celestia-indexer/pkg/types"
"time"

pkgTypes "github.com/celenium-io/celestia-indexer/pkg/types"

"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/internal/storage/types"
)
Expand Down Expand Up @@ -68,3 +69,24 @@ func NewTx(tx storage.Tx) Tx {
func (Tx) SearchType() string {
return "tx"
}

type TxForAddress struct {
MessagesCount int64 `example:"1" format:"int64" json:"messages_count" swaggertype:"integer"`
Hash string `example:"652452A670018D629CC116E510BA88C1CABE061336661B1F3D206D248BD558AF" format:"binary" json:"hash" swaggertype:"string"`
Fee string `example:"9348" format:"int64" json:"fee" swaggertype:"string"`

MessageTypes []types.MsgType `example:"MsgSend,MsgUnjail" json:"message_types"`
Status types.Status `example:"success" json:"status"`
}

func NewTxForAddress(tx *storage.Tx) TxForAddress {
result := TxForAddress{
MessagesCount: tx.MessagesCount,
Fee: tx.Fee.String(),
Status: tx.Status,
Hash: hex.EncodeToString(tx.Hash),
MessageTypes: tx.MessageTypes.Names(),
}

return result
}
2 changes: 1 addition & 1 deletion cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
searchHandler := handler.NewSearchHandler(db.Address, db.Blocks, db.Namespace, db.Tx)
v1.GET("/search", searchHandler.Search)

addressHandlers := handler.NewAddressHandler(db.Address, db.Tx, db.BlobLogs, db.State, cfg.Indexer.Name)
addressHandlers := handler.NewAddressHandler(db.Address, db.Tx, db.BlobLogs, db.Message, db.State, cfg.Indexer.Name)
addressesGroup := v1.Group("/address")
{
addressesGroup.GET("", addressHandlers.List)
Expand Down
7 changes: 0 additions & 7 deletions internal/storage/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@ type AddressListFilter struct {
Sort storage.SortOrder
}

type AddressMsgsFilter struct {
Limit int
Offset int
Sort storage.SortOrder
}

//go:generate mockgen -source=$GOFILE -destination=mock/$GOFILE -package=mock -typed
type IAddress interface {
storage.Table[*Address]

ByHash(ctx context.Context, hash []byte) (Address, error)
ListWithBalance(ctx context.Context, filters AddressListFilter) ([]Address, error)
Messages(ctx context.Context, id uint64, filters AddressMsgsFilter) ([]MsgAddress, error)
}

// Address -
Expand Down
18 changes: 17 additions & 1 deletion internal/storage/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,35 @@ type MessageListWithTxFilters struct {
MessageTypes []string
}

type AddressMsgsFilter struct {
Limit int
Offset int
Sort storage.SortOrder
MessageTypes []string
}

type MessageWithTx struct {
bun.BaseModel `bun:"message,alias:message" comment:"Table with celestia messages."`
bun.BaseModel `bun:"message,alias:message"`

Message
Tx *Tx `bun:"rel:belongs-to"`
}

type AddressMessageWithTx struct {
bun.BaseModel `bun:"message,alias:message"`

MsgAddress
Msg *Message `bun:"rel:belongs-to,join:msg_id=id"`
Tx *Tx `bun:"rel:belongs-to,join:msg__tx_id=id"`
}

//go:generate mockgen -source=$GOFILE -destination=mock/$GOFILE -package=mock -typed
type IMessage interface {
storage.Table[*Message]

ByTxId(ctx context.Context, txId uint64) ([]Message, error)
ListWithTx(ctx context.Context, filters MessageListWithTxFilters) ([]MessageWithTx, error)
ByAddress(ctx context.Context, id uint64, filters AddressMsgsFilter) ([]AddressMessageWithTx, error)
}

// Message -
Expand Down
39 changes: 0 additions & 39 deletions internal/storage/mock/address.go

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

39 changes: 39 additions & 0 deletions internal/storage/mock/message.go

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

Loading

0 comments on commit 3c9c796

Please sign in to comment.