Skip to content

Commit

Permalink
add logic to prevent submitting transactions where fee rate is too hi…
Browse files Browse the repository at this point in the history
…gh (#704)
  • Loading branch information
lazynina authored Nov 22, 2024
1 parent 1416a91 commit a92eb0f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions routes/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ func (fes *APIServer) SubmitAtomicTransaction(ww http.ResponseWriter, req *http.
innerTxnPreSignatureHashToSignature[*preSignatureInnerTxnHash]
}

atomicTxnLen, err := atomicTxn.ToBytes(false)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"SubmitAtomicTransaction: Problem serializing completed atomic transaction: %v", err))
return
}
if TransactionFeeRateTooHigh(atomicTxn, uint64(len(atomicTxnLen))) {
_AddBadRequestError(ww, fmt.Sprintf("SubmitAtomicTransaction: Transaction fee rate too high"))
return
}

// Verify and broadcast the completed atomic transaction.
if err := fes.backendServer.VerifyAndBroadcastTransaction(atomicTxn); err != nil {
_AddBadRequestError(ww,
Expand Down Expand Up @@ -275,6 +286,19 @@ type SubmitTransactionResponse struct {
PostEntryResponse *PostEntryResponse
}

// FeeRateNanosPerKBThreshold is the threshold above which transactions will be rejected if the fee rate exceeds it.
const FeeRateNanosPerKBThreshold = 1e8

func TransactionFeeRateTooHigh(txn *lib.MsgDeSoTxn, txnLen uint64) bool {
// Handle base cases.
if txn.TxnFeeNanos == 0 || txnLen == 0 {
return false
}
// Compute the fee rate in nanos per KB.
feeRateNanosPerKB := (txn.TxnFeeNanos * 1000) / txnLen
return feeRateNanosPerKB > FeeRateNanosPerKBThreshold
}

func (fes *APIServer) SubmitTransaction(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := SubmitTransactionRequest{}
Expand All @@ -296,6 +320,11 @@ func (fes *APIServer) SubmitTransaction(ww http.ResponseWriter, req *http.Reques
return
}

if TransactionFeeRateTooHigh(txn, uint64(len(txnBytes))) {
_AddBadRequestError(ww, fmt.Sprintf("SubmitTransactionRequest: Transaction fee rate too high"))
return
}

if err = fes.backendServer.VerifyAndBroadcastTransaction(txn); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitTransaction: Problem processing transaction: %v", err))
return
Expand Down

0 comments on commit a92eb0f

Please sign in to comment.