code.vegaprotocol.io/vega@v0.79.0/datanode/service/ledger.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package service 17 18 import ( 19 "context" 20 "io" 21 22 "code.vegaprotocol.io/vega/datanode/entities" 23 "code.vegaprotocol.io/vega/datanode/utils" 24 "code.vegaprotocol.io/vega/logging" 25 "code.vegaprotocol.io/vega/protos/vega" 26 ) 27 28 type ledgerStore interface { 29 Flush(ctx context.Context) ([]entities.LedgerEntry, error) 30 Add(le entities.LedgerEntry) error 31 Query(ctx context.Context, filter *entities.LedgerEntryFilter, dateRange entities.DateRange, pagination entities.CursorPagination) (*[]entities.AggregatedLedgerEntry, entities.PageInfo, error) 32 Export(ctx context.Context, partyID string, assetID *string, dateRange entities.DateRange, writer io.Writer) error 33 GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.LedgerEntry, error) 34 } 35 36 type LedgerEntriesStore interface { 37 Query(filter *entities.LedgerEntryFilter, dateRange entities.DateRange, pagination entities.CursorPagination) (*[]entities.AggregatedLedgerEntry, entities.PageInfo, error) 38 Export(ctx context.Context, partyID, assetID string, dateRange entities.DateRange, pagination entities.CursorPagination) ([]byte, entities.PageInfo, error) 39 } 40 41 type Ledger struct { 42 store ledgerStore 43 transferResponses []*vega.LedgerMovement 44 observer utils.Observer[*vega.LedgerMovement] 45 } 46 47 func NewLedger(store ledgerStore, log *logging.Logger) *Ledger { 48 return &Ledger{ 49 store: store, 50 observer: utils.NewObserver[*vega.LedgerMovement]("ledger", log, 0, 0), 51 } 52 } 53 54 func (l *Ledger) Flush(ctx context.Context) error { 55 _, err := l.store.Flush(ctx) 56 if err != nil { 57 return err 58 } 59 l.observer.Notify(l.transferResponses) 60 l.transferResponses = []*vega.LedgerMovement{} 61 return nil 62 } 63 64 func (l *Ledger) AddLedgerEntry(le entities.LedgerEntry) error { 65 return l.store.Add(le) 66 } 67 68 func (l *Ledger) AddTransferResponse(le *vega.LedgerMovement) { 69 l.transferResponses = append(l.transferResponses, le) 70 } 71 72 func (l *Ledger) Observe(ctx context.Context, retries int) (<-chan []*vega.LedgerMovement, uint64) { 73 ch, ref := l.observer.Observe(ctx, 74 retries, 75 func(tr *vega.LedgerMovement) bool { 76 return true 77 }) 78 return ch, ref 79 } 80 81 func (l *Ledger) GetSubscribersCount() int32 { 82 return l.observer.GetSubscribersCount() 83 } 84 85 func (l *Ledger) Query( 86 ctx context.Context, 87 filter *entities.LedgerEntryFilter, 88 dateRange entities.DateRange, 89 pagination entities.CursorPagination, 90 ) (*[]entities.AggregatedLedgerEntry, entities.PageInfo, error) { 91 return l.store.Query( 92 ctx, 93 filter, 94 dateRange, 95 pagination) 96 } 97 98 func (l *Ledger) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.LedgerEntry, error) { 99 return l.store.GetByTxHash(ctx, txHash) 100 } 101 102 func (l *Ledger) Export( 103 ctx context.Context, 104 partyID string, 105 assetID *string, 106 dateRange entities.DateRange, 107 writer io.Writer, 108 ) error { 109 return l.store.Export( 110 ctx, 111 partyID, 112 assetID, 113 dateRange, 114 writer) 115 }