refactor, move all hard-coded constants to tests

This commit is contained in:
Sergei Tikhomirov 2024-12-13 09:35:09 +01:00
parent 363d56c980
commit ef4c2d3f69
3 changed files with 39 additions and 40 deletions

View File

@ -1,16 +1,11 @@
{.used.}
import
std/[options, strscans],
testutils/unittests,
chronicles,
chronos,
libp2p/crypto/crypto,
web3
std/[options], testutils/unittests, chronos, web3, stew/byteutils, stint, strutils
import
waku/[node/peer_manager, waku_core],
../testlib/[assertions, wakucore, testasync, futures, testutils],
../testlib/[assertions],
waku/incentivization/[rpc, rpc_codec, common, txid_proof]
# All txids from Ethereum Sepolia testnet
@ -22,7 +17,8 @@ const TxHashContractCall* =
TxHash.fromHex("0x2761f066eeae9a259a0247f529133dd01b7f57bf74254a64d897433397d321cb")
const TxHashSimpleTransfer* =
TxHash.fromHex("0xa3985984b2ec3f1c3d473eb57a4820a56748f25dabbf9414f2b8380312b439cc")
const ExpectedToAddress = Address.fromHex("0x5e809a85aa182a9921edd10a4163745bb3e36284")
const ExpectedValue = 200500000000005063.u256
const EthClient = "https://sepolia.infura.io/v3/470c2e9a16f24057aee6660081729fb9"
suite "Waku Incentivization PoC Eligibility Proofs":
@ -39,32 +35,40 @@ suite "Waku Incentivization PoC Eligibility Proofs":
## Test that an unconfirmed tx is not eligible.
let eligibilityProof =
EligibilityProof(proofOfPayment: some(@(TxHashNonExisting.bytes())))
let txIsEligible = await isEligible(eligibilityProof, EthClient)
let isEligibleTxId = await isEligibleTxId(
eligibilityProof, ExpectedToAddress, ExpectedValue, EthClient
)
check:
not txIsEligible
not isEligibleTxId
asyncTest "incentivization PoC: contract creation tx is not eligible":
## Test that a contract creation tx is not eligible.
let eligibilityProof =
EligibilityProof(proofOfPayment: some(@(TxHashContractCreation.bytes())))
let txIsEligible = await isEligible(eligibilityProof, EthClient)
let isEligibleTxId = await isEligibleTxId(
eligibilityProof, ExpectedToAddress, ExpectedValue, EthClient
)
check:
not txIsEligible
not isEligibleTxId
asyncTest "incentivization PoC: contract call tx is not eligible":
## Test that a contract call tx is not eligible.
## This assumes a payment in native currency (ETH), not a token.
let eligibilityProof =
EligibilityProof(proofOfPayment: some(@(TxHashContractCall.bytes())))
let txIsEligible = await isEligible(eligibilityProof, EthClient)
let isEligibleTxId = await isEligibleTxId(
eligibilityProof, ExpectedToAddress, ExpectedValue, EthClient
)
check:
not txIsEligible
not isEligibleTxId
asyncTest "incentivization PoC: simple transfer tx is eligible":
## Test that a simple transfer tx is eligible (if necessary conditions hold).
let eligibilityProof =
EligibilityProof(proofOfPayment: some(@(TxHashSimpleTransfer.bytes())))
let txIdExists = await isEligible(eligibilityProof, EthClient)
let txIdExists = await isEligibleTxId(
eligibilityProof, ExpectedToAddress, ExpectedValue, EthClient
)
check:
txIdExists

View File

@ -7,12 +7,3 @@ proc new*(T: type EligibilityStatus, isEligible: bool): T =
EligibilityStatus(statusCode: uint32(200), statusDesc: some("OK"))
else:
EligibilityStatus(statusCode: uint32(402), statusDesc: some("Payment Required"))
proc isEligible*(
eligibilityProof: EligibilityProof, ethClient: string
): Future[bool] {.async.} =
## We consider a tx eligible,
## in the context of service incentivization PoC,
## if it is confirmed and pays the expected amount to the server's address.
## See spec: https://github.com/waku-org/specs/blob/master/standards/core/incentivization.md
await txidEligiblityCriteriaMet(eligibilityProof, ethClient)

View File

@ -4,9 +4,22 @@ import waku/incentivization/rpc
const SimpleTransferGasUsed = Quantity(21000)
proc checkTxIdIsEligible(txHash: TxHash, ethClient: string): Future[bool] {.async.} =
proc isEligibleTxId*(
eligibilityProof: EligibilityProof,
expectedToAddress: Address,
expectedValue: UInt256,
ethClient: string,
): Future[bool] {.async.} =
## We consider a tx eligible,
## in the context of service incentivization PoC,
## if it is confirmed and pays the expected amount to the server's address.
## See spec: https://github.com/waku-org/specs/blob/master/standards/core/incentivization.md
if eligibilityProof.proofOfPayment.isNone():
return false
let txHash = TxHash.fromHex(byteutils.toHex(eligibilityProof.proofOfPayment.get()))
let web3 = await newWeb3(ethClient)
try:
# TODO: make requests in parallel (?)
let tx = await web3.provider.eth_getTransactionByHash(txHash)
let txReceipt = await web3.getMinedTransactionReceipt(txHash)
# check that it is not a contract creation tx
@ -20,24 +33,15 @@ proc checkTxIdIsEligible(txHash: TxHash, ethClient: string): Future[bool] {.asyn
let isSimpleTransferTx = (gasUsed == SimpleTransferGasUsed)
if not isSimpleTransferTx:
return false
# check that the amount is "as expected" (hard-coded for now)
let txValue = tx.value
let hasExpectedValue = (txValue == 200500000000005063.u256)
# check that the to address is "as expected" (hard-coded for now)
# check that the to address is "as expected"
let toAddress = toAddressOption.get()
let hasExpectedToAddress =
(toAddress == Address.fromHex("0x5e809a85aa182a9921edd10a4163745bb3e36284"))
let hasExpectedToAddress = (toAddress == expectedToAddress)
# FIXME: move tx query here?
# check that the amount is "as expected"
let txValue = tx.value
let hasExpectedValue = (txValue == expectedValue)
defer:
await web3.close()
return hasExpectedValue and hasExpectedToAddress
except ValueError as e:
return false
proc txidEligiblityCriteriaMet*(
eligibilityProof: EligibilityProof, ethClient: string
): Future[bool] {.async.} =
if eligibilityProof.proofOfPayment.isNone():
return false
let txHash = TxHash.fromHex(byteutils.toHex(eligibilityProof.proofOfPayment.get()))
let txExists = await checkTxIdIsEligible(txHash, ethClient)
return txExists