Skip to content

use map to reduce loop complexity #838

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

Merged
merged 1 commit into from
May 14, 2020
Merged
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
16 changes: 7 additions & 9 deletions core/service/transactionCoreService.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ func (tg *TransactionCoreService) GetTransactionsByIds(transactionIds []int64) (
// GetTransactionsByBlockID get transactions of the block
func (tg *TransactionCoreService) GetTransactionsByBlockID(blockID int64) ([]*model.Transaction, error) {
var (
transactions []*model.Transaction
escrows []*model.Escrow
txIdsStr []string
err error
transactionsMap = make(map[int64]*model.Transaction)
transactions []*model.Transaction
escrows []*model.Escrow
txIdsStr []string
err error
)

// get transaction of the block
Expand All @@ -100,6 +101,7 @@ func (tg *TransactionCoreService) GetTransactionsByBlockID(blockID int64) ([]*mo
// fetch escrow if exist
for _, tx := range transactions {
txIdsStr = append(txIdsStr, "'"+strconv.FormatInt(tx.ID, 10)+"'")
transactionsMap[tx.ID] = tx
}
if len(txIdsStr) > 0 {
escrows, err = func() ([]*model.Escrow, error) {
Expand All @@ -117,11 +119,7 @@ func (tg *TransactionCoreService) GetTransactionsByBlockID(blockID int64) ([]*mo
return nil, blocker.NewBlocker(blocker.DBErr, err.Error())
}
for _, escrow := range escrows {
for _, tx := range transactions {
if tx.ID == escrow.ID {
tx.Escrow = escrow
}
}
transactionsMap[escrow.ID].Escrow = escrow
}
}
return transactions, nil
Expand Down