Skip to content

Commit

Permalink
stellar#4433: pr feedback, make zero-based operations index the stand…
Browse files Browse the repository at this point in the history
…ard on function interfaces
  • Loading branch information
sreuland committed Jul 20, 2022
1 parent 7886ea6 commit 9f0bee9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
30 changes: 30 additions & 0 deletions exp/lighthorizon/archive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,39 @@ type LedgerTransactionReader interface {

// Archive here only has the methods LightHorizon cares about, to make caching/wrapping easier
type Archive interface {

//GetLedger - retreive a ledger's meta data
//
//ctx - the caller's request context
//ledgerCloseMeta - the sequence number of ledger to fetch
//
//returns error or meta data for requested ledger
GetLedger(ctx context.Context, sequence uint32) (xdr.LedgerCloseMeta, error)

// Close - releases any resources used for this archive instance.
Close() error

// NewLedgerTransactionReaderFromLedgerCloseMeta - get a reader for ledger meta data
//
// networkPassphrase - the network passphrase
// ledgerCloseMeta - the meta data for a ledger
//
// returns error or LedgerTransactionReader
NewLedgerTransactionReaderFromLedgerCloseMeta(networkPassphrase string, ledgerCloseMeta xdr.LedgerCloseMeta) (LedgerTransactionReader, error)

// GetTransactionParticipants - get set of all participants(accounts) in a transaction
//
// transaction - the ledger transaction
//
// returns error or map with keys of participant account id's and value of empty struct
GetTransactionParticipants(transaction LedgerTransaction) (map[string]struct{}, error)

// GetOperationParticipants - get set of all participants(accounts) in a operation
//
// transaction - the ledger transaction
// operation - the operation within this transaction
// opIndex - the 0 based index of the operation within the transaction
//
// returns error or map with keys of participant account id's and value of empty struct
GetOperationParticipants(transaction LedgerTransaction, operation xdr.Operation, opIndex int) (map[string]struct{}, error)
}
6 changes: 6 additions & 0 deletions exp/lighthorizon/index/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func GetTransactionParticipants(transaction ingest.LedgerTransaction) ([]string,
return participantsForOperations(transaction, false)
}

// transaction - the ledger transaction
// operation - the operation within this transaction
// opIndex - the 0 based index of the operation within the transaction
func GetOperationParticipants(transaction ingest.LedgerTransaction, operation xdr.Operation, opIndex int) ([]string, error) {
return participantsForOperation(transaction, operation, opIndex, false)
}
Expand All @@ -130,6 +133,9 @@ func participantsForOperations(transaction ingest.LedgerTransaction, onlyPayment
return participants, nil
}

// transaction - the ledger transaction
// operation - the operation within this transaction
// opIndex - the 0 based index of the operation within the transaction
func participantsForOperation(transaction ingest.LedgerTransaction, operation xdr.Operation, opIndex int, onlyPayments bool) ([]string, error) {
participants := []string{}
opSource := operation.SourceAccount
Expand Down
12 changes: 5 additions & 7 deletions exp/lighthorizon/services/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (os *OperationsService) GetOperationsByAccount(ctx context.Context, cursor
ops := []common.Operation{}
opsCallback := func(tx archive.LedgerTransaction, ledgerHeader *xdr.LedgerHeader) (bool, error) {
for operationOrder, op := range tx.Envelope.Operations() {
opParticipants, opParticipantErr := os.Config.Archive.GetOperationParticipants(tx, op, operationOrder+1)
opParticipants, opParticipantErr := os.Config.Archive.GetOperationParticipants(tx, op, operationOrder)
if opParticipantErr != nil {
return false, opParticipantErr
}
Expand All @@ -59,7 +59,7 @@ func (os *OperationsService) GetOperationsByAccount(ctx context.Context, cursor
TransactionResult: &tx.Result.Result,
LedgerHeader: ledgerHeader,
TxIndex: int32(tx.Index),
OpIndex: int32(operationOrder + 1),
OpIndex: int32(operationOrder),
})
if uint64(len(ops)) == limit {
return true, nil
Expand Down Expand Up @@ -146,12 +146,10 @@ func searchTxByAccount(ctx context.Context, cursor int64, accountId string, conf
}
}
nextCursor := toid.New(int32(nextLedger), 1, 1).ToInt64()

nextLedger, err = getAccountNextLedgerCursor(accountId, nextCursor, config.IndexStore)
if err != nil {
if err == io.EOF {
return nil
}
if err == io.EOF {
return nil
} else if err != nil {
return err
}
}
Expand Down
12 changes: 6 additions & 6 deletions exp/lighthorizon/services/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ func mockArchiveAndIndex(ctx context.Context, passphrase string) (archive.Archiv
mockArchive.On("GetTransactionParticipants", expectedLedger3Transaction1).Return(allParticipants, nil)
mockArchive.On("GetTransactionParticipants", expectedLedger3Transaction2).Return(partialParticipants, nil)

mockArchive.On("GetOperationParticipants", expectedLedger1Transaction1, mock.Anything, int(0)).Return(partialParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger1Transaction1, mock.Anything, int(1)).Return(partialParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger1Transaction1, mock.Anything, int(2)).Return(partialParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger1Transaction2, mock.Anything, int(1)).Return(partialParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger2Transaction1, mock.Anything, int(1)).Return(partialParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger2Transaction2, mock.Anything, int(1)).Return(allParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger3Transaction1, mock.Anything, int(1)).Return(allParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger3Transaction2, mock.Anything, int(1)).Return(partialParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger1Transaction2, mock.Anything, int(0)).Return(partialParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger2Transaction1, mock.Anything, int(0)).Return(partialParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger2Transaction2, mock.Anything, int(0)).Return(allParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger3Transaction1, mock.Anything, int(0)).Return(allParticipants, nil)
mockArchive.On("GetOperationParticipants", expectedLedger3Transaction2, mock.Anything, int(0)).Return(partialParticipants, nil)

mockReaderLedger1.On("Read").Return(expectedLedger1Transaction1, nil).Once()
mockReaderLedger1.On("Read").Return(expectedLedger1Transaction2, nil).Once()
Expand Down

0 comments on commit 9f0bee9

Please sign in to comment.