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

Improved Filtering options #131

Merged
merged 8 commits into from
Sep 18, 2023
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
2 changes: 1 addition & 1 deletion components/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
Name = "inx-indexer"

// Version of the app.
Version = "1.0.0-rc.3"
Version = "1.0.0-rc.4"
)

func App() *app.App {
Expand Down
62 changes: 44 additions & 18 deletions pkg/indexer/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import (
"fmt"
"time"

"gorm.io/gorm"

iotago "github.com/iotaledger/iota.go/v3"
)

type alias struct {
AliasID aliasIDBytes `gorm:"primaryKey;notnull"`
OutputID outputIDBytes `gorm:"unique;notnull"`
NativeTokenCount uint32 `gorm:"notnull;type:integer"`
StateController addressBytes `gorm:"notnull;index:alias_state_controller"`
Governor addressBytes `gorm:"notnull;index:alias_governor"`
Issuer addressBytes `gorm:"index:alias_issuer"`
Sender addressBytes `gorm:"index:alias_sender"`
CreatedAt time.Time `gorm:"notnull;index:alias_created_at"`
AliasID []byte `gorm:"primaryKey;notnull"`
OutputID []byte `gorm:"unique;notnull"`
NativeTokenCount uint32 `gorm:"notnull;type:integer"`
StateController []byte `gorm:"notnull;index:alias_state_controller"`
Governor []byte `gorm:"notnull;index:alias_governor"`
Issuer []byte `gorm:"index:alias_issuer"`
Sender []byte `gorm:"index:alias_sender"`
CreatedAt time.Time `gorm:"notnull;index:alias_created_at"`
}

func (o *alias) String() string {
Expand All @@ -27,6 +29,7 @@ type AliasFilterOptions struct {
hasNativeTokens *bool
minNativeTokenCount *uint32
maxNativeTokenCount *uint32
unlockableByAddress *iotago.Address
stateController *iotago.Address
governor *iotago.Address
issuer *iotago.Address
Expand Down Expand Up @@ -57,6 +60,12 @@ func AliasMaxNativeTokenCount(value uint32) AliasFilterOption {
}
}

func AliasUnlockableByAddress(address iotago.Address) AliasFilterOption {
return func(args *AliasFilterOptions) {
args.unlockableByAddress = &address
}
}

func AliasStateController(address iotago.Address) AliasFilterOption {
return func(args *AliasFilterOptions) {
args.stateController = &address
Expand Down Expand Up @@ -123,8 +132,7 @@ func (i *Indexer) AliasOutput(aliasID *iotago.AliasID) *IndexerResult {
return i.combineOutputIDFilteredQuery(query, 0, nil)
}

func (i *Indexer) AliasOutputsWithFilters(filter ...AliasFilterOption) *IndexerResult {
opts := aliasFilterOptions(filter)
func (i *Indexer) aliasQueryWithFilter(opts *AliasFilterOptions) (*gorm.DB, error) {
query := i.db.Model(&alias{})

if opts.hasNativeTokens != nil {
Expand All @@ -143,36 +151,44 @@ func (i *Indexer) AliasOutputsWithFilters(filter ...AliasFilterOption) *IndexerR
query = query.Where("native_token_count <= ?", *opts.maxNativeTokenCount)
}

if opts.unlockableByAddress != nil {
addr, err := addressBytesForAddress(*opts.unlockableByAddress)
if err != nil {
return nil, err
}
query = query.Where("(state_controller = ? OR governor = ?)", addr, addr)
}

if opts.stateController != nil {
addr, err := addressBytesForAddress(*opts.stateController)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("state_controller = ?", addr[:])
query = query.Where("state_controller = ?", addr)
}

if opts.governor != nil {
addr, err := addressBytesForAddress(*opts.governor)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("governor = ?", addr[:])
query = query.Where("governor = ?", addr)
}

if opts.sender != nil {
addr, err := addressBytesForAddress(*opts.sender)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("sender = ?", addr[:])
query = query.Where("sender = ?", addr)
}

if opts.issuer != nil {
addr, err := addressBytesForAddress(*opts.issuer)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("issuer = ?", addr[:])
query = query.Where("issuer = ?", addr)
}

if opts.createdBefore != nil {
Expand All @@ -183,5 +199,15 @@ func (i *Indexer) AliasOutputsWithFilters(filter ...AliasFilterOption) *IndexerR
query = query.Where("created_at > ?", *opts.createdAfter)
}

return query, nil
}

func (i *Indexer) AliasOutputsWithFilters(filter ...AliasFilterOption) *IndexerResult {
opts := aliasFilterOptions(filter)
query, err := i.aliasQueryWithFilter(opts)
if err != nil {
return errorResult(err)
}

return i.combineOutputIDFilteredQuery(query, opts.pageSize, opts.cursor)
}
62 changes: 44 additions & 18 deletions pkg/indexer/basic_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import (
"fmt"
"time"

"gorm.io/gorm"

iotago "github.com/iotaledger/iota.go/v3"
)

type basicOutput struct {
OutputID outputIDBytes `gorm:"primaryKey;notnull"`
NativeTokenCount uint32 `gorm:"notnull;type:integer"`
Sender addressBytes `gorm:"index:basic_outputs_sender_tag"`
Tag []byte `gorm:"index:basic_outputs_sender_tag"`
Address addressBytes `gorm:"notnull;index:basic_outputs_address"`
OutputID []byte `gorm:"primaryKey;notnull"`
NativeTokenCount uint32 `gorm:"notnull;type:integer"`
Sender []byte `gorm:"index:basic_outputs_sender_tag"`
Tag []byte `gorm:"index:basic_outputs_sender_tag"`
Address []byte `gorm:"notnull;index:basic_outputs_address"`
StorageDepositReturn *uint64
StorageDepositReturnAddress addressBytes `gorm:"index:basic_outputs_storage_deposit_return_address"`
StorageDepositReturnAddress []byte `gorm:"index:basic_outputs_storage_deposit_return_address"`
TimelockTime *time.Time
ExpirationTime *time.Time
ExpirationReturnAddress addressBytes `gorm:"index:basic_outputs_expiration_return_address"`
CreatedAt time.Time `gorm:"notnull;index:basic_outputs_created_at"`
ExpirationReturnAddress []byte `gorm:"index:basic_outputs_expiration_return_address"`
CreatedAt time.Time `gorm:"notnull;index:basic_outputs_created_at"`
}

func (o *basicOutput) String() string {
Expand All @@ -31,6 +33,7 @@ type BasicOutputFilterOptions struct {
minNativeTokenCount *uint32
maxNativeTokenCount *uint32
unlockableByAddress *iotago.Address
address *iotago.Address
hasStorageDepositReturnCondition *bool
storageDepositReturnAddress *iotago.Address
hasExpirationCondition *bool
Expand Down Expand Up @@ -74,6 +77,12 @@ func BasicOutputUnlockableByAddress(address iotago.Address) BasicOutputFilterOpt
}
}

func BasicOutputUnlockAddress(address iotago.Address) BasicOutputFilterOption {
return func(args *BasicOutputFilterOptions) {
args.address = &address
}
}

func BasicOutputHasStorageDepositReturnCondition(value bool) BasicOutputFilterOption {
return func(args *BasicOutputFilterOptions) {
args.hasStorageDepositReturnCondition = &value
Expand Down Expand Up @@ -174,8 +183,7 @@ func basicOutputFilterOptions(optionalOptions []BasicOutputFilterOption) *BasicO
return result
}

func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *IndexerResult {
opts := basicOutputFilterOptions(filters)
func (i *Indexer) basicOutputsQueryWithFilter(opts *BasicOutputFilterOptions) (*gorm.DB, error) {
query := i.db.Model(&basicOutput{})

if opts.hasNativeTokens != nil {
Expand All @@ -197,9 +205,17 @@ func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *I
if opts.unlockableByAddress != nil {
addr, err := addressBytesForAddress(*opts.unlockableByAddress)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("address = ?", addr[:])
query = query.Where("(address = ? OR expiration_return_address = ? OR storage_deposit_return_address = ?)", addr, addr, addr)
}

if opts.address != nil {
addr, err := addressBytesForAddress(*opts.address)
if err != nil {
return nil, err
}
query = query.Where("address = ?", addr)
}

if opts.hasStorageDepositReturnCondition != nil {
Expand All @@ -213,9 +229,9 @@ func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *I
if opts.storageDepositReturnAddress != nil {
addr, err := addressBytesForAddress(*opts.storageDepositReturnAddress)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("storage_deposit_return_address = ?", addr[:])
query = query.Where("storage_deposit_return_address = ?", addr)
}

if opts.hasExpirationCondition != nil {
Expand All @@ -229,9 +245,9 @@ func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *I
if opts.expirationReturnAddress != nil {
addr, err := addressBytesForAddress(*opts.expirationReturnAddress)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("expiration_return_address = ?", addr[:])
query = query.Where("expiration_return_address = ?", addr)
}

if opts.expiresBefore != nil {
Expand Down Expand Up @@ -261,9 +277,9 @@ func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *I
if opts.sender != nil {
addr, err := addressBytesForAddress(*opts.sender)
if err != nil {
return errorResult(err)
return nil, err
}
query = query.Where("sender = ?", addr[:])
query = query.Where("sender = ?", addr)
}

if opts.tag != nil && len(opts.tag) > 0 {
Expand All @@ -278,5 +294,15 @@ func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *I
query = query.Where("created_at > ?", *opts.createdAfter)
}

return query, nil
}

func (i *Indexer) BasicOutputsWithFilters(filters ...BasicOutputFilterOption) *IndexerResult {
opts := basicOutputFilterOptions(filters)
query, err := i.basicOutputsQueryWithFilter(opts)
if err != nil {
return errorResult(err)
}

return i.combineOutputIDFilteredQuery(query, opts.pageSize, opts.cursor)
}
Loading
Loading