Skip to content

Commit

Permalink
Backend(UtxoCoin): make sure fee is less than out
Browse files Browse the repository at this point in the history
This prevents any future bug to cause unreasonable fees.
  • Loading branch information
aarani committed Oct 2, 2023
1 parent d7cff49 commit 86e96e5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/GWallet.Backend/Exceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ exception InvalidJson
exception TransactionAlreadySigned
exception TransactionNotSignedYet

exception MinerFeeHigherThanOutputs
37 changes: 35 additions & 2 deletions src/GWallet.Backend/UtxoCoin/UtxoCoinAccount.fs
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,42 @@ module Account =
let internal CheckValidPassword (account: NormalAccount) (password: string) =
GetPrivateKey account password |> ignore

let private ValidateMinerFee currency (rawTransaction: string) =
async {
let network = GetNetwork currency

let txToValidate = Transaction.Parse (rawTransaction, network)

let totalOutputsAmount = txToValidate.TotalOut

let getInputAmount (input: TxIn) =
async {
let job = ElectrumClient.GetBlockchainTransaction (input.PrevOut.Hash.ToString())
let! inputOriginTxString = Server.Query currency (QuerySettings.Default ServerSelectionMode.Fast) job None
let inputOriginTx = Transaction.Parse (inputOriginTxString, network)
return inputOriginTx.Outputs.[input.PrevOut.N].Value
}

let! amounts =
txToValidate.Inputs
|> Seq.map getInputAmount
|> Async.Parallel

let totalInputsAmount = Seq.sum amounts

let minerFee = totalInputsAmount - totalOutputsAmount
if minerFee > totalOutputsAmount then
return raise MinerFeeHigherThanOutputs

return ()
}

let private BroadcastRawTransaction currency (rawTx: string): Async<string> =
let job = ElectrumClient.BroadcastTransaction rawTx
Server.Query currency QuerySettings.Broadcast job None
async {
do! ValidateMinerFee currency rawTx
let job = ElectrumClient.BroadcastTransaction rawTx
return! Server.Query currency QuerySettings.Broadcast job None
}

let internal BroadcastTransaction currency (transaction: SignedTransaction<_>) =
// FIXME: stop embedding TransactionInfo element in SignedTransaction<BTC>
Expand Down

0 comments on commit 86e96e5

Please sign in to comment.