Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add incentivization PoC for RLNaaS in Lightpush #3166

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tests/incentivization/test_all.nim
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import ./test_rpc_codec
import
./test_rpc_codec,
./test_poc
50 changes: 50 additions & 0 deletions tests/incentivization/test_poc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{.used.}

import
std/[options, strscans],
testutils/unittests,
chronicles,
chronos,
libp2p/crypto/crypto,
web3

import
waku/[
node/peer_manager,
waku_core,
],
../testlib/[assertions, wakucore, testasync, futures, testutils],
waku/incentivization/[
rpc,
rpc_codec,
common,
txid_proof,
eligibility
]


# a random confirmed txid (Sepolia)
const TxHashExisting* = TxHash.fromHex(
"0xc1be5f442d3688a8d3e4b5980a73f15e4351358e0f16e2fdd99c2517c9cf6270"
)
const TxHashNonExisting* = TxHash.fromHex(
"0x0000000000000000000000000000000000000000000000000000000000000000"
)

const EthClient = "https://sepolia.infura.io/v3/470c2e9a16f24057aee6660081729fb9"


suite "Waku Incentivization PoC Eligibility Proofs":

asyncTest "incentivization PoC: dummy protocol with a valid txid eligibility proof":
let eligibilityProof = EligibilityProof(proofOfPayment: some(@(TxHashExisting.bytes())))
let txIdExists = await isEligible(eligibilityProof, EthClient)
check:
txIdExists

asyncTest "incentivization PoC: dummy protocol client with an invalid txid eligibility proof":
let eligibilityProof = EligibilityProof(proofOfPayment: some(@(TxHashNonExisting.bytes())))
let txIdExists = await isEligible(eligibilityProof, EthClient)
check:
not txIdExists

43 changes: 35 additions & 8 deletions tests/incentivization/test_rpc_codec.nim
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
import
std/options,
std/strscans,
testutils/unittests,
chronicles,
chronos,
libp2p/crypto/crypto
libp2p/crypto/crypto,
web3

import waku/incentivization/rpc, waku/incentivization/rpc_codec
import
waku/incentivization/[
rpc,
rpc_codec,
common,
txid_proof,
eligibility
]

#[
let txHash = TxHash.fromHex(
"0x0000000000000000000000000000000000000000000000000000000000000000"
)
let txHashAsBytes = @(txHash.bytes())
]#

suite "Waku Incentivization Eligibility Codec":
asyncTest "encode eligibility proof":

asyncTest "encode eligibility proof from bytes":
# FIXME: remove this test?
var byteSequence: seq[byte] = @[1, 2, 3, 4, 5, 6, 7, 8]
let epRpc = EligibilityProof(proofOfPayment: some(byteSequence))
let encoded = encode(epRpc)
let decoded = EligibilityProof.decode(encoded.buffer).get()
check:
epRpc == decoded

asyncTest "encode eligibility proof from txid":
let txHash = TxHash.fromHex(
"0x0000000000000000000000000000000000000000000000000000000000000000")
let txHashAsBytes = @(txHash.bytes())
let eligibilityProof = EligibilityProof(proofOfPayment: some(txHashAsBytes))
let encoded = encode(eligibilityProof)
let decoded = EligibilityProof.decode(encoded.buffer).get()
check:
eligibilityProof == decoded

asyncTest "encode eligibility status":
let esRpc = EligibilityStatus(statusCode: uint32(200), statusDesc: some("OK"))
let encoded = encode(esRpc)
let eligibilityStatus = genEligibilityStatus(true)
let encoded = encode(eligibilityStatus)
let decoded = EligibilityStatus.decode(encoded.buffer).get()
check:
esRpc == decoded
eligibilityStatus == decoded


23 changes: 23 additions & 0 deletions waku/incentivization/common.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import
std/options,
std/strscans,
std/sequtils,
testutils/unittests,
chronicles,
chronos,
libp2p/crypto/crypto

import stew/results, libp2p/peerid

import
waku/incentivization/rpc

proc genEligibilityStatus*(isEligible: bool): EligibilityStatus =
if isEligible:
EligibilityStatus(
statusCode: uint32(200),
statusDesc: some("OK"))
else:
EligibilityStatus(
statusCode: uint32(402),
statusDesc: some("Payment Required"))
20 changes: 20 additions & 0 deletions waku/incentivization/eligibility.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import
std/options,
std/strscans,
std/sequtils,
testutils/unittests,
chronicles,
chronos,
libp2p/crypto/crypto

import stew/results, libp2p/peerid

import
waku/incentivization/rpc,
waku/incentivization/rpc_codec,
waku/incentivization/common,
waku/incentivization/txid_proof


proc isEligible*(eligibilityProof: EligibilityProof, ethClient: string): Future[bool] {.async.} =
result = await txidEligiblityCriteriaMet(eligibilityProof, ethClient)
2 changes: 1 addition & 1 deletion waku/incentivization/rpc.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json_serialization, std/options
import ../waku_core
import waku/waku_core

# Implementing the RFC:
# https://github.com/vacp2p/rfc/tree/master/content/docs/rfcs/73
Expand Down
2 changes: 2 additions & 0 deletions waku/incentivization/rpc_codec.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import std/options
import ../common/protobuf, ../waku_core, ./rpc

const DefaultMaxRpcSize* = -1

# Codec for EligibilityProof

proc encode*(epRpc: EligibilityProof): ProtoBuffer =
Expand Down
30 changes: 30 additions & 0 deletions waku/incentivization/txid_proof.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import
std/options,
chronos,
web3,
stew/byteutils

import waku/incentivization/rpc

proc checkTxIdExists(txHash: TxHash, ethClient: string): Future[bool] {.async.} =
let web3 = await newWeb3(ethClient)
try:
discard await web3.provider.eth_getTransactionByHash(txHash)
result = true
except ValueError as e:
result = false
if result:
try:
discard await web3.getMinedTransactionReceipt(txHash)
result = true
except ValueError as e:
result = false
await web3.close()
result

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 checkTxIdExists(txHash, ethClient)
return txExists
Loading