Skip to content

Commit

Permalink
ARCO-155: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Jul 9, 2024
1 parent cd47dc5 commit cc99c65
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
18 changes: 12 additions & 6 deletions pkg/api/handler/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func (m ArcDefaultHandler) POSTTransaction(ctx echo.Context, params api.POSTTran
}

if len(failOutputs) > 0 {
// if an error is returned, the processing failed
e = failOutputs[0]
// if an error is returned, the processing failed, and we should return a 500 error
return ctx.JSON(e.Status, e)
}

Expand Down Expand Up @@ -221,7 +221,7 @@ func (m ArcDefaultHandler) POSTTransactions(ctx echo.Context, params api.POSTTra
return ctx.JSON(e.Status, e)
}

sizingInfo := make([][]uint64, 0)
sizingInfo := make([][]uint64, 0, len(txs))
for _, btTx := range txs {
normalBytes, dataBytes, feeAmount := getSizings(btTx)
sizingInfo = append(sizingInfo, []uint64{normalBytes, dataBytes, feeAmount})
Expand Down Expand Up @@ -336,6 +336,7 @@ func (m ArcDefaultHandler) processTransactions(ctx context.Context, txsHex []byt
submitedTxs = append(submitedTxs, tx.Transaction)
}
}

txIds = append(txIds, beefTx.GetLatestTx().TxID())
} else {
transaction, bytesUsed, err := bt.NewTxFromStream(txsHex)
Expand All @@ -355,6 +356,10 @@ func (m ArcDefaultHandler) processTransactions(ctx context.Context, txsHex []byt
}
}

if len(submitedTxs) == 0 {
return nil, nil, failOutputs, nil
}

timeoutCtx, cancel := context.WithTimeout(ctx, time.Duration(transactionOptions.MaxTimeout+2)*time.Second)
defer cancel()

Expand All @@ -367,6 +372,7 @@ func (m ArcDefaultHandler) processTransactions(ctx context.Context, txsHex []byt
// process returned transaction statuses and return to user
txStatuses = filterStatusesByTxIDs(txIds, txStatuses)

now := m.now()
outputs = make([]*api.TransactionResponse, 0, len(submitedTxs))

for idx, tx := range txStatuses {
Expand All @@ -377,11 +383,11 @@ func (m ArcDefaultHandler) processTransactions(ctx context.Context, txsHex []byt
outputs = append(outputs, &api.TransactionResponse{
Status: int(api.StatusOK),
Title: "OK",
BlockHash: &txStatuses[idx].BlockHash,
BlockHeight: &txStatuses[idx].BlockHeight,
BlockHash: &tx.BlockHash,
BlockHeight: &tx.BlockHeight,
TxStatus: tx.Status,
ExtraInfo: &txStatuses[idx].ExtraInfo,
Timestamp: m.now(),
ExtraInfo: &tx.ExtraInfo,
Timestamp: now,
Txid: txID,
MerklePath: &tx.MerklePath,
})
Expand Down
11 changes: 4 additions & 7 deletions pkg/api/handler/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,13 @@ func convertMerkleRootsRequest(beefMerkleRoots []beef.MerkleRootVerificationRequ
return merkleRoots
}

func findStatusByTxID(txID string, statuses []*metamorph.TransactionStatus) *metamorph.TransactionStatus {
for _, status := range statuses {
if status.TxID == txID {
return status
func filterStatusesByTxIDs(txIDs []string, allStatuses []*metamorph.TransactionStatus) []*metamorph.TransactionStatus {
if len(txIDs) == 1 && len(allStatuses) == 1 {
if allStatuses[0].TxID == txIDs[0] {
return allStatuses
}
}
return nil
}

func filterStatusesByTxIDs(txIDs []string, allStatuses []*metamorph.TransactionStatus) []*metamorph.TransactionStatus {
idsMap := make(map[string]struct{})
for _, id := range txIDs {
idsMap[id] = struct{}{}
Expand Down

0 comments on commit cc99c65

Please sign in to comment.