diff --git a/pkg/api/handler/default.go b/pkg/api/handler/default.go index afb2d9869..ddc8dc65f 100644 --- a/pkg/api/handler/default.go +++ b/pkg/api/handler/default.go @@ -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) } @@ -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}) @@ -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) @@ -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() @@ -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 { @@ -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, }) diff --git a/pkg/api/handler/helpers.go b/pkg/api/handler/helpers.go index 741db06b1..43942e540 100644 --- a/pkg/api/handler/helpers.go +++ b/pkg/api/handler/helpers.go @@ -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{}{}