mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-22 15:53:10 +00:00
refactor: store expected amount and address in EligibilityManager
This commit is contained in:
parent
3cd3e58fec
commit
2a6e3c9119
@ -130,7 +130,8 @@ suite "Waku Incentivization PoC Eligibility Proofs":
|
||||
var manager {.threadvar.}: EligibilityManager
|
||||
|
||||
asyncSetup:
|
||||
manager = await EligibilityManager.init(EthClient)
|
||||
# Setup manager with expected receiver and amount
|
||||
manager = await EligibilityManager.init(EthClient, Address.fromHex(receiverExpected.toHex()), TxValueExpectedWei)
|
||||
|
||||
(
|
||||
txHashWrongReceiverRightAmount, txHashRightReceiverWrongAmount,
|
||||
@ -147,7 +148,7 @@ suite "Waku Incentivization PoC Eligibility Proofs":
|
||||
let eligibilityProof =
|
||||
EligibilityProof(proofOfPayment: some(@(TxHashNonExisting.bytes())))
|
||||
let isEligible = await manager.isEligibleTxId(
|
||||
eligibilityProof, receiverExpected, TxValueExpectedWei
|
||||
eligibilityProof
|
||||
)
|
||||
check:
|
||||
isEligible.isErr()
|
||||
@ -158,7 +159,7 @@ suite "Waku Incentivization PoC Eligibility Proofs":
|
||||
let eligibilityProof =
|
||||
EligibilityProof(proofOfPayment: some(@(txHashContractCreation.bytes())))
|
||||
let isEligible = await manager.isEligibleTxId(
|
||||
eligibilityProof, receiverExpected, TxValueExpectedWei
|
||||
eligibilityProof
|
||||
)
|
||||
check:
|
||||
isEligible.isErr()
|
||||
@ -170,7 +171,7 @@ suite "Waku Incentivization PoC Eligibility Proofs":
|
||||
let eligibilityProof =
|
||||
EligibilityProof(proofOfPayment: some(@(txHashContractCall.bytes())))
|
||||
let isEligible = await manager.isEligibleTxId(
|
||||
eligibilityProof, receiverExpected, TxValueExpectedWei
|
||||
eligibilityProof
|
||||
)
|
||||
check:
|
||||
isEligible.isErr()
|
||||
@ -181,7 +182,7 @@ suite "Waku Incentivization PoC Eligibility Proofs":
|
||||
let eligibilityProof =
|
||||
EligibilityProof(proofOfPayment: some(@(txHashRightReceiverRightAmount.bytes())))
|
||||
let isEligible = await manager.isEligibleTxId(
|
||||
eligibilityProof, receiverExpected, TxValueExpectedWei
|
||||
eligibilityProof
|
||||
)
|
||||
|
||||
assert isEligible.isOk(), isEligible.error
|
||||
@ -193,11 +194,11 @@ suite "Waku Incentivization PoC Eligibility Proofs":
|
||||
EligibilityProof(proofOfPayment: some(@(txHashRightReceiverRightAmount.bytes())))
|
||||
|
||||
let isEligibleOnce = await manager.isEligibleTxId(
|
||||
eligibilityProof, receiverExpected, TxValueExpectedWei
|
||||
eligibilityProof
|
||||
)
|
||||
|
||||
let isEligibleTwice = await manager.isEligibleTxId(
|
||||
eligibilityProof, receiverExpected, TxValueExpectedWei
|
||||
eligibilityProof
|
||||
)
|
||||
|
||||
assert isEligibleOnce.isOk()
|
||||
|
||||
@ -445,22 +445,23 @@ proc setupProtocols(
|
||||
# Check that RLN Relay and Lightpush are mounted
|
||||
if node.wakuRlnRelay.isNil or node.wakuLightPush.isNil:
|
||||
return err("Eligibility manager requires both RLN Relay and Lightpush protocols to be mounted")
|
||||
let ethUrl = conf.eligibilityConf.get().ethClientUrls[0]
|
||||
let eligibilityConf = conf.eligibilityConf.get()
|
||||
let ethUrl = eligibilityConf.ethClientUrls[0]
|
||||
let expectedToAddress = Address.fromHex(eligibilityConf.receiverAddress)
|
||||
let expectedValueWei = eligibilityConf.paymentAmountWei.u256
|
||||
try:
|
||||
let manager = await EligibilityManager.init(ethUrl)
|
||||
debug "initializing eligibilityManager..."
|
||||
let manager = await EligibilityManager.init(
|
||||
ethUrl,
|
||||
expectedToAddress,
|
||||
expectedValueWei
|
||||
)
|
||||
node.peerManager.eligibilityManager = some(manager)
|
||||
debug "i13n: eligibilityManager initialized with values:", ethUrl=ethUrl, expectedToAddress=expectedToAddress, expectedValueWei=$expectedValueWei
|
||||
# FIXME: why debug displays expectedValueWei 3948404736 and not 30000000000000?
|
||||
except CatchableError:
|
||||
return err("failed to initialize eligibility manager: " & getCurrentExceptionMsg())
|
||||
|
||||
|
||||
# TODO: figure out where to store conf: expected address and expected amount
|
||||
# NOtE: consider both client's and server's perspective!
|
||||
|
||||
#if conf.eligibilityEnabled:
|
||||
# debug "i13n: eligibility enabled!"
|
||||
# debug "eligibilityReceiverAddress:", conf.eligibilityReceiverAddress
|
||||
# debug "eligibilityPaymentAmountWei:", conf.eligibilityPaymentAmountWei
|
||||
|
||||
return ok()
|
||||
|
||||
## Start node
|
||||
|
||||
@ -8,13 +8,19 @@ const TxReceiptQueryTimeout = 3.seconds
|
||||
type EligibilityManager* = ref object # FIXME: make web3 private?
|
||||
web3*: Web3
|
||||
seenTxIds*: HashSet[TxHash]
|
||||
expectedToAddress*: Address
|
||||
expectedValueWei*: UInt256
|
||||
|
||||
# Initialize the eligibilityManager with a web3 instance
|
||||
# Initialize the eligibilityManager with a web3 instance and expected params
|
||||
proc init*(
|
||||
T: type EligibilityManager, ethClient: string
|
||||
T: type EligibilityManager, ethClient: string, expectedToAddress: Address, expectedValueWei: UInt256
|
||||
): Future[EligibilityManager] {.async.} =
|
||||
return
|
||||
EligibilityManager(web3: await newWeb3(ethClient), seenTxIds: initHashSet[TxHash]())
|
||||
return EligibilityManager(
|
||||
web3: await newWeb3(ethClient),
|
||||
seenTxIds: initHashSet[TxHash](),
|
||||
expectedToAddress: expectedToAddress,
|
||||
expectedValueWei: expectedValueWei
|
||||
)
|
||||
# TODO: handle error if web3 instance is not established
|
||||
|
||||
# Clean up the web3 instance
|
||||
@ -49,9 +55,7 @@ proc getTxAndTxReceipt(
|
||||
|
||||
proc isEligibleTxId*(
|
||||
eligibilityManager: EligibilityManager,
|
||||
eligibilityProof: EligibilityProof,
|
||||
expectedToAddress: Address,
|
||||
expectedValueWei: UInt256,
|
||||
eligibilityProof: EligibilityProof
|
||||
): Future[Result[void, string]] {.async.} =
|
||||
## We consider a tx eligible,
|
||||
## in the context of service incentivization PoC,
|
||||
@ -89,10 +93,10 @@ proc isEligibleTxId*(
|
||||
return err("A contract call tx is not eligible")
|
||||
# check that the to address is "as expected"
|
||||
let toAddress = toAddressOption.get()
|
||||
if toAddress != expectedToAddress:
|
||||
if toAddress != eligibilityManager.expectedToAddress:
|
||||
return err("Wrong destination address: " & $toAddress)
|
||||
# check that the amount is "as expected"
|
||||
let txValueWei = tx.value
|
||||
if txValueWei != expectedValueWei:
|
||||
return err("Wrong tx value: got " & $txValueWei & ", expected " & $expectedValueWei)
|
||||
if txValueWei != eligibilityManager.expectedValueWei:
|
||||
return err("Wrong tx value: got " & $txValueWei & ", expected " & $eligibilityManager.expectedValueWei)
|
||||
return ok()
|
||||
|
||||
@ -1222,27 +1222,21 @@ proc lightpushPublish*(
|
||||
else:
|
||||
debug "eligibilityManager is enabled"
|
||||
var em = node.peerManager.eligibilityManager.get()
|
||||
# FIXME: where should I init eligibilityManager?
|
||||
# FIXME: extract values from... config parameters (which config?)
|
||||
# FIXME: how to reuse EthClient from CLI arguments?
|
||||
debug "initializing eligibilityManager..."
|
||||
|
||||
try:
|
||||
let ethClient = "https://sepolia.infura.io/v3/470c2e9a16f24057aee6660081729fb9"
|
||||
em = await EligibilityManager.init(ethClient)
|
||||
#let ethClient = "https://sepolia.infura.io/v3/470c2e9a16f24057aee6660081729fb9"
|
||||
#let expectedToAddress = Address.fromHex("0xe8284Af9A5F3b0CD1334DBFaf512F09BeDA805a3")
|
||||
#let expectedValueWei = 30000000000000.u256
|
||||
#em = await EligibilityManager.init(em.ethClient, em.expectedToAddress, em.expectedValueWei)
|
||||
|
||||
debug "checking eligibilityProof..."
|
||||
|
||||
# Check if eligibility proof is provided before accessing it
|
||||
if eligibilityProof.isNone():
|
||||
let msg = "Eligibility proof is required"
|
||||
return lighpushErrorResult(PAYMENT_REQUIRED, msg)
|
||||
|
||||
#let txNonExistent = TxHash.fromHex("0x0000000000000000000000000000000000000000000000000000000000000000")
|
||||
let expectedToAddress = Address.fromHex("0xe8284Af9A5F3b0CD1334DBFaf512F09BeDA805a3")
|
||||
let expectedValueWei = 30000000000000.u256
|
||||
|
||||
let isEligible = await em.isEligibleTxId(
|
||||
eligibilityProof.get(), expectedToAddress, expectedValueWei
|
||||
)
|
||||
let isEligible = await em.isEligibleTxId(eligibilityProof.get())
|
||||
|
||||
except CatchableError:
|
||||
let msg = "Eligibility check threw exception: " & getCurrentExceptionMsg()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user