mukan-ignite/ignite/pkg/cosmostxcollector/collector.go
Mukan Erkin Törük c32551b6f7
Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
refactor: replace all github.com upstream refs with git.cw.tr/mukan-network
2026-05-11 03:36:24 +03:00

58 lines
1.8 KiB
Go

package cosmostxcollector
import (
"context"
"golang.org/x/sync/errgroup"
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/cosmosclient"
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/cosmostxcollector/adapter"
)
// TXsCollector defines the interface for Cosmos clients that support collection of transactions.
//
//go:generate mockery --name TXsCollector --filename txs_collector.go --with-expecter
type TXsCollector interface {
CollectTXs(ctx context.Context, fromHeight int64, tc chan<- []cosmosclient.TX) error
}
// New creates a new Cosmos transaction collector.
func New(db adapter.Saver, client TXsCollector) Collector {
return Collector{db, client}
}
// Collector defines a type to collect and save Cosmos transactions in a data backend.
type Collector struct {
db adapter.Saver
client TXsCollector
}
// Collect gathers transactions for all blocks starting from a specific height.
// Each group of block transactions is saved sequentially after being collected.
func (c Collector) Collect(ctx context.Context, fromHeight int64) error {
tc := make(chan []cosmosclient.TX)
wg, ctx := errgroup.WithContext(ctx)
// Start collecting block transactions.
// The transactions channel is closed by the client when all transactions
// are collected or when an error occurs during the collection.
wg.Go(func() error {
return c.client.CollectTXs(ctx, fromHeight, tc)
})
// The transactions for each block are saved in "bulks" so they are not
// kept in memory. Also, they are saved sequentially to avoid block height
// gaps that can occur if a group of transactions from a previous block
// fail to be saved.
wg.Go(func() error {
for txs := range tc {
if err := c.db.Save(ctx, txs); err != nil {
return err
}
}
return nil
})
return wg.Wait()
}