@@ -53,6 +53,9 @@ const (
5353 // this is the estimated memory overhead for each
5454 // block fetched by the indexer.
5555 sizeMultiplier = 15
56+
57+ // zeroValue is 0 as a string
58+ zeroValue = "0"
5659)
5760
5861var (
@@ -74,7 +77,6 @@ type Client interface {
7477var _ syncer.Handler = (* Indexer )(nil )
7578var _ syncer.Helper = (* Indexer )(nil )
7679var _ services.Indexer = (* Indexer )(nil )
77- var _ storage.CoinStorageHelper = (* Indexer )(nil )
7880
7981// Indexer caches blocks and provides balance query functionality.
8082type Indexer struct {
@@ -85,11 +87,12 @@ type Indexer struct {
8587
8688 client Client
8789
88- asserter * asserter.Asserter
89- database storage.Database
90- blockStorage * storage.BlockStorage
91- coinStorage * storage.CoinStorage
92- workers []storage.BlockWorker
90+ asserter * asserter.Asserter
91+ database storage.Database
92+ blockStorage * storage.BlockStorage
93+ balanceStorage * storage.BalanceStorage
94+ coinStorage * storage.CoinStorage
95+ workers []storage.BlockWorker
9396
9497 waiter * waitTable
9598}
@@ -197,9 +200,21 @@ func Initialize(
197200 asserter : asserter ,
198201 }
199202
200- coinStorage := storage .NewCoinStorage (localStore , i , asserter )
203+ coinStorage := storage .NewCoinStorage (
204+ localStore ,
205+ & CoinStorageHelper {blockStorage },
206+ asserter ,
207+ )
201208 i .coinStorage = coinStorage
202- i .workers = []storage.BlockWorker {coinStorage }
209+
210+ balanceStorage := storage .NewBalanceStorage (localStore )
211+ balanceStorage .Initialize (
212+ & BalanceStorageHelper {asserter },
213+ & BalanceStorageHandler {},
214+ )
215+ i .balanceStorage = balanceStorage
216+
217+ i .workers = []storage.BlockWorker {coinStorage , balanceStorage }
203218
204219 return i , nil
205220}
@@ -748,7 +763,11 @@ func (i *Indexer) GetBlockTransaction(
748763 blockIdentifier * types.BlockIdentifier ,
749764 transactionIdentifier * types.TransactionIdentifier ,
750765) (* types.Transaction , error ) {
751- return i .blockStorage .GetBlockTransaction (ctx , blockIdentifier , transactionIdentifier )
766+ return i .blockStorage .GetBlockTransaction (
767+ ctx ,
768+ blockIdentifier ,
769+ transactionIdentifier ,
770+ )
752771}
753772
754773// GetCoins returns all unspent coins for a particular *types.AccountIdentifier.
@@ -759,11 +778,42 @@ func (i *Indexer) GetCoins(
759778 return i .coinStorage .GetCoins (ctx , accountIdentifier )
760779}
761780
762- // CurrentBlockIdentifier returns the current head block identifier
763- // and is used to comply with the CoinStorageHelper interface .
764- func (i * Indexer ) CurrentBlockIdentifier (
781+ // GetBalance returns the balance of an account
782+ // at a particular *types.PartialBlockIdentifier .
783+ func (i * Indexer ) GetBalance (
765784 ctx context.Context ,
766- transaction storage.DatabaseTransaction ,
767- ) (* types.BlockIdentifier , error ) {
768- return i .blockStorage .GetHeadBlockIdentifierTransactional (ctx , transaction )
785+ accountIdentifier * types.AccountIdentifier ,
786+ currency * types.Currency ,
787+ blockIdentifier * types.PartialBlockIdentifier ,
788+ ) (* types.Amount , * types.BlockIdentifier , error ) {
789+ dbTx := i .database .NewDatabaseTransaction (ctx , false )
790+ defer dbTx .Discard (ctx )
791+
792+ blockResponse , err := i .blockStorage .GetBlockLazyTransactional (
793+ ctx ,
794+ blockIdentifier ,
795+ dbTx ,
796+ )
797+ if err != nil {
798+ return nil , nil , err
799+ }
800+
801+ amount , err := i .balanceStorage .GetBalanceTransactional (
802+ ctx ,
803+ dbTx ,
804+ accountIdentifier ,
805+ currency ,
806+ blockResponse .Block .BlockIdentifier .Index ,
807+ )
808+ if errors .Is (err , storage .ErrAccountMissing ) {
809+ return & types.Amount {
810+ Value : zeroValue ,
811+ Currency : currency ,
812+ }, blockResponse .Block .BlockIdentifier , nil
813+ }
814+ if err != nil {
815+ return nil , nil , err
816+ }
817+
818+ return amount , blockResponse .Block .BlockIdentifier , nil
769819}
0 commit comments