Skip to content

Commit

Permalink
fix: add transaction hash (#3535)
Browse files Browse the repository at this point in the history
  • Loading branch information
umerm-work committed Nov 22, 2022
1 parent 28bf1d7 commit 6050e6a
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 146 deletions.
2 changes: 1 addition & 1 deletion openapi/Swarm.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.3

info:
version: 3.3.0
version: 3.4.0
title: Bee API
description: "A list of the currently provided Interfaces to interact with the swarm, implementing file operations and sending messages"

Expand Down
2 changes: 2 additions & 0 deletions openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ components:
properties:
batchID:
$ref: "#/components/schemas/BatchID"
txHash:
$ref: "#/components/schemas/TransactionHash"

Response:
type: object
Expand Down
30 changes: 17 additions & 13 deletions pkg/api/postage.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (b hexByte) MarshalJSON() ([]byte, error) {

type postageCreateResponse struct {
BatchID hexByte `json:"batchID"`
TxHash string `json:"txHash"`
}

func (s *Service) postageCreateHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -90,7 +91,7 @@ func (s *Service) postageCreateHandler(w http.ResponseWriter, r *http.Request) {
return
}

batchID, err := s.postageContract.CreateBatch(
txHash, batchID, err := s.postageContract.CreateBatch(
r.Context(),
paths.Amount,
paths.Depth,
Expand All @@ -99,31 +100,32 @@ func (s *Service) postageCreateHandler(w http.ResponseWriter, r *http.Request) {
)
if err != nil {
if errors.Is(err, postagecontract.ErrChainDisabled) {
logger.Debug("create batch: no chain backend", "error", err)
logger.Debug("create batch: no chain backend", "error", err, "txHash", txHash)
logger.Error(nil, "create batch: no chain backend")
jsonhttp.MethodNotAllowed(w, "no chain backend")
return
}
if errors.Is(err, postagecontract.ErrInsufficientFunds) {
logger.Debug("create batch: out of funds", "error", err)
logger.Debug("create batch: out of funds", "error", err, "txHash", txHash)
logger.Error(nil, "create batch: out of funds")
jsonhttp.BadRequest(w, "out of funds")
return
}
if errors.Is(err, postagecontract.ErrInvalidDepth) {
logger.Debug("create batch: invalid depth", "error", err)
logger.Debug("create batch: invalid depth", "error", err, "txHash", txHash)
logger.Error(nil, "create batch: invalid depth")
jsonhttp.BadRequest(w, "invalid depth")
return
}
logger.Debug("create batch: create failed", "error", err)
logger.Debug("create batch: create failed", "error", err, "txHash", txHash)
logger.Error(nil, "create batch: create failed")
jsonhttp.InternalServerError(w, "cannot create batch")
return
}

jsonhttp.Created(w, &postageCreateResponse{
BatchID: batchID,
TxHash: txHash.String(),
})
}

Expand Down Expand Up @@ -461,28 +463,29 @@ func (s *Service) postageTopUpHandler(w http.ResponseWriter, r *http.Request) {
}
hexBatchID := hex.EncodeToString(paths.BatchID)

err := s.postageContract.TopUpBatch(r.Context(), paths.BatchID, paths.Amount)
txHash, err := s.postageContract.TopUpBatch(r.Context(), paths.BatchID, paths.Amount)
if err != nil {
if errors.Is(err, postagecontract.ErrInsufficientFunds) {
logger.Debug("topup batch: out of funds", "batch_id", hexBatchID, "amount", paths.Amount, "error", err)
logger.Debug("topup batch: out of funds", "batch_id", hexBatchID, "amount", paths.Amount, "error", err, "tx_hash", txHash)
logger.Error(nil, "topup batch: out of funds")
jsonhttp.PaymentRequired(w, "out of funds")
return
}
if errors.Is(err, postagecontract.ErrNotImplemented) {
logger.Debug("topup batch: not implemented", "error", err)
logger.Debug("topup batch: not implemented", "error", err, "tx_hash", txHash)
logger.Error(nil, "topup batch: not implemented")
jsonhttp.NotImplemented(w, nil)
return
}
logger.Debug("topup batch: topup failed", "batch_id", hexBatchID, "amount", paths.Amount, "error", err)
logger.Debug("topup batch: topup failed", "batch_id", hexBatchID, "amount", paths.Amount, "error", err, "tx_hash", txHash)
logger.Error(nil, "topup batch: topup failed")
jsonhttp.InternalServerError(w, "cannot topup batch")
return
}

jsonhttp.Accepted(w, &postageCreateResponse{
BatchID: paths.BatchID,
TxHash: txHash.String(),
})
}

Expand All @@ -499,27 +502,28 @@ func (s *Service) postageDiluteHandler(w http.ResponseWriter, r *http.Request) {
}
hexBatchID := hex.EncodeToString(paths.BatchID)

err := s.postageContract.DiluteBatch(r.Context(), paths.BatchID, paths.Depth)
txHash, err := s.postageContract.DiluteBatch(r.Context(), paths.BatchID, paths.Depth)
if err != nil {
if errors.Is(err, postagecontract.ErrInvalidDepth) {
logger.Debug("dilute batch: invalid depth", "error", err)
logger.Debug("dilute batch: invalid depth", "error", err, "tx_hash", txHash)
logger.Error(nil, "dilute batch: invalid depth")
jsonhttp.BadRequest(w, "invalid depth")
return
}
if errors.Is(err, postagecontract.ErrNotImplemented) {
logger.Debug("dilute batch: not implemented", "error", err)
logger.Debug("dilute batch: not implemented", "error", "tx_hash", txHash)
logger.Error(nil, "dilute batch: not implemented")
jsonhttp.NotImplemented(w, nil)
return
}
logger.Debug("dilute batch: dilute failed", "batch_id", hexBatchID, "depth", paths.Depth, "error", err)
logger.Debug("dilute batch: dilute failed", "batch_id", hexBatchID, "depth", paths.Depth, "error", err, "tx_hash", txHash)
logger.Error(nil, "dilute batch: dilute failed")
jsonhttp.InternalServerError(w, "cannot dilute batch")
return
}

jsonhttp.Accepted(w, &postageCreateResponse{
BatchID: paths.BatchID,
TxHash: txHash.String(),
})
}

0 comments on commit 6050e6a

Please sign in to comment.