Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/stable'
Browse files Browse the repository at this point in the history
To bring error handling.
  • Loading branch information
knocte committed Oct 23, 2024
2 parents 8f97232 + b6468e8 commit 5d13d4b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/GWallet.Backend/Ether/EtherExceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type RpcErrorCode =
| CannotFulfillRequest = -32046
| ResourceNotFound = -32001
| InternalError = -32603
| UnparsableResponseType = -39000

type ServerCannotBeResolvedException =
inherit CommunicationUnsuccessfulException
Expand Down Expand Up @@ -95,3 +96,10 @@ type UnhandledWebException =
}
new (info: SerializationInfo, context: StreamingContext) =
{ inherit Exception (info, context) }

/// Exception indicating that response JSON contains null value where it should not.
/// E.g. {"jsonrpc":"2.0","id":1,"result":null}
type AbnormalNullValueInJsonResponseException(message: string) =
inherit CommunicationUnsuccessfulException(message)

static member BalanceJobErrorMessage = "Abnormal null response from balance job"
39 changes: 32 additions & 7 deletions src/GWallet.Backend/Ether/EtherServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ module Server =
raise <| ServerTimedOutException(exMsg, httpReqEx)
if HttpRequestExceptionMatchesErrorCode httpReqEx (int CloudFlareError.OriginUnreachable) then
raise <| ServerTimedOutException(exMsg, httpReqEx)
if HttpRequestExceptionMatchesErrorCode httpReqEx (int HttpStatusCode.RequestTimeout) then
raise <| ServerTimedOutException(exMsg, httpReqEx)

if HttpRequestExceptionMatchesErrorCode httpReqEx (int CloudFlareError.OriginSslHandshakeError) then
raise <| ServerChannelNegotiationException(exMsg, CloudFlareError.OriginSslHandshakeError, httpReqEx)
Expand Down Expand Up @@ -163,6 +165,8 @@ module Server =
raise <| ServerUnavailableException(exMsg, httpReqEx)
if HttpRequestExceptionMatchesErrorCode httpReqEx (int HttpStatusCode.NotFound) then
raise <| ServerUnavailableException(exMsg, httpReqEx)
if HttpRequestExceptionMatchesErrorCode httpReqEx (int HttpStatusCode.Gone) then
raise <| ServerUnavailableException(exMsg, httpReqEx)

// this happened once with ETC (www.ethercluster.com/etc) when trying to broadcast a transaction
// (not sure if it's correct to ignore it but I can't fathom how the tx could be a bad request:
Expand Down Expand Up @@ -234,6 +238,8 @@ module Server =
raise <| ServerMisconfiguredException(exMsg, rpcResponseEx)
| i when i = int RpcErrorCode.InternalError ->
raise <| ServerFaultException(exMsg, rpcResponseEx)
| j when j = int RpcErrorCode.UnparsableResponseType ->
raise <| ServerFaultException(exMsg, rpcResponseEx)
| _ ->
raise
<| Exception (SPrintF3 "RpcResponseException with RpcError Code <%i> and Message '%s' (%s)"
Expand Down Expand Up @@ -482,7 +488,10 @@ module Server =
let! cancelToken = Async.CancellationToken
let task =
web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(address, null, cancelToken)
return! Async.AwaitTask task
let! txCount = Async.AwaitTask task
if isNull txCount then
raise <| AbnormalNullValueInJsonResponseException "Abnormal null response from tx count job"
return txCount
}
GetRandomizedFuncs currency web3Func
return! faultTolerantEtherClient.Query
Expand All @@ -498,7 +507,7 @@ module Server =
web3.Eth.Blocks.GetBlockNumber.SendRequestAsync (null, cancelToken)
|> Async.AwaitTask
if isNull latestBlock then
failwith "latestBlock somehow is null"
raise <| AbnormalNullValueInJsonResponseException "latestBlock somehow is null"

let blockToCheck = BigInteger.Subtract(latestBlock.Value,
NUMBER_OF_CONFIRMATIONS_TO_CONSIDER_BALANCE_CONFIRMED)
Expand Down Expand Up @@ -560,8 +569,10 @@ module Server =
let task = web3.Eth.GetBalance.SendRequestAsync (address, null, cancelToken)
return! Async.AwaitTask task
}
if Object.ReferenceEquals(balance, null) then
failwith "Weird null response from balance job"
if isNull balance then
raise <|
AbnormalNullValueInJsonResponseException
AbnormalNullValueInJsonResponseException.BalanceJobErrorMessage
return UnitConversion.Convert.FromWei(balance.Value, UnitConversion.EthUnit.Ether)
}
GetRandomizedFuncs currency web3Func
Expand Down Expand Up @@ -591,7 +602,7 @@ module Server =

let contractHandler = web3.Eth.GetContractHandler contractAddress
if isNull contractHandler then
failwith "contractHandler somehow is null"
raise <| AbnormalNullValueInJsonResponseException "contractHandler somehow is null"

let! cancelToken = Async.CancellationToken
cancelToken.ThrowIfCancellationRequested()
Expand Down Expand Up @@ -653,7 +664,10 @@ module Server =
let! cancelToken = Async.CancellationToken
let task =
contractHandler.EstimateGasAsync<TransferFunction>(transferFunctionMsg, cancelToken)
return! Async.AwaitTask task
let! fee = Async.AwaitTask task
if isNull fee then
raise <| AbnormalNullValueInJsonResponseException "Abnormal null response from transfer fee job"
return fee
}
GetRandomizedFuncs account.Currency web3Func
return! faultTolerantEtherClient.Query
Expand All @@ -676,6 +690,8 @@ module Server =
let! cancelToken = Async.CancellationToken
let task = web3.Eth.GasPrice.SendRequestAsync(null, cancelToken)
let! hexBigInteger = Async.AwaitTask task
if isNull hexBigInteger then
raise <| AbnormalNullValueInJsonResponseException "Abnormal null response from gas price job"
if hexBigInteger.Value = BigInteger 0 then
return failwith "Some server returned zero for gas price, which is invalid"
return hexBigInteger
Expand Down Expand Up @@ -706,7 +722,12 @@ module Server =
let! cancelToken = Async.CancellationToken
let task =
web3.Eth.Transactions.SendRawTransaction.SendRequestAsync(transaction, null, cancelToken)
return! Async.AwaitTask task
let! response = Async.AwaitTask task
if isNull response then
raise <|
AbnormalNullValueInJsonResponseException
"Abnormal null response from broadcast transaction job"
return response
}
GetRandomizedFuncs currency web3Func
try
Expand Down Expand Up @@ -737,6 +758,10 @@ module Server =
let task =
web3.TransactionManager.TransactionReceiptService.PollForReceiptAsync(txHash, cancelToken)
let! transactionReceipt = Async.AwaitTask task
if isNull transactionReceipt || isNull transactionReceipt.GasUsed || isNull transactionReceipt.Status then
raise <|
AbnormalNullValueInJsonResponseException
(SPrintF1 "Abnormal null response when getting details from tx receipt (%A)" transactionReceipt)
return {
GasUsed = transactionReceipt.GasUsed.Value
Status = transactionReceipt.Status.Value
Expand Down
4 changes: 4 additions & 0 deletions src/GWallet.Backend/ServerManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ module ServerManager =
let web3Func (web3: Ether.SomeWeb3): Async<decimal> =
async {
let! balance = Async.AwaitTask (web3.Eth.GetBalance.SendRequestAsync ETH_GENESISBLOCK_ADDRESS)
if isNull balance then
raise <|
Ether.AbnormalNullValueInJsonResponseException
Ether.AbnormalNullValueInJsonResponseException.BalanceJobErrorMessage
return balance.Value |> decimal
}

Expand Down

0 comments on commit 5d13d4b

Please sign in to comment.