Skip to content

Commit

Permalink
Merge pull request #90 from piotr-roslaniec/providers-not-sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec authored Jul 5, 2023
2 parents 10cb497 + e176e39 commit 951187c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ape-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ deployments:

test:
mnemonic: test test test test test test test test test test test junk
number_of_accounts: 15
number_of_accounts: 10
57 changes: 36 additions & 21 deletions tests/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import pytest
from web3 import Web3

TRANSCRIPT_SIZE = 500
TIMEOUT = 1000
MAX_DKG_SIZE = 64
MAX_DKG_SIZE = 4
FEE_RATE = 42
ERC20_SUPPLY = 10**24
DURATION = 1234
Expand All @@ -26,19 +25,29 @@
)


# This formula returns an approximated size
# To have a representative size, create transcripts with `nucypher-core`
def transcript_size(shares, threshold):
return int(424 + 240 * (shares / 2) + 50 * (threshold))


@pytest.fixture(scope="module")
def nodes(accounts):
return sorted(accounts[:8], key=lambda x: x.address)
return sorted(accounts[:MAX_DKG_SIZE], key=lambda x: x.address.lower())


@pytest.fixture(scope="module")
def initiator(accounts):
return accounts[9]
initiator_index = MAX_DKG_SIZE + 1
assert len(accounts) >= initiator_index
return accounts[initiator_index]


@pytest.fixture(scope="module")
def deployer(accounts):
return accounts[8]
deployer_index = MAX_DKG_SIZE + 2
assert len(accounts) >= deployer_index
return accounts[deployer_index]


@pytest.fixture()
Expand All @@ -60,10 +69,7 @@ def erc20(project, initiator):
@pytest.fixture()
def flat_rate_fee_model(project, deployer, stake_info, erc20):
contract = project.FlatRateFeeModel.deploy(
erc20.address,
FEE_RATE,
stake_info.address,
sender=deployer
erc20.address, FEE_RATE, stake_info.address, sender=deployer
)
return contract

Expand All @@ -77,7 +83,7 @@ def coordinator(project, deployer, stake_info, flat_rate_fee_model, initiator):
MAX_DKG_SIZE,
admin,
flat_rate_fee_model.address,
sender=deployer
sender=deployer,
)
contract.grantRole(contract.INITIATOR_ROLE(), initiator, sender=admin)
return contract
Expand Down Expand Up @@ -128,11 +134,11 @@ def test_post_transcript(coordinator, nodes, initiator, erc20, flat_rate_fee_mod
cost = flat_rate_fee_model.getRitualInitiationCost(nodes, DURATION)
erc20.approve(coordinator.address, cost, sender=initiator)
coordinator.initiateRitual(nodes, initiator, DURATION, sender=initiator)
transcript = os.urandom(transcript_size(len(nodes), len(nodes)))

for node in nodes:
assert coordinator.getRitualState(0) == RitualState.AWAITING_TRANSCRIPTS

transcript = os.urandom(TRANSCRIPT_SIZE)
tx = coordinator.postTranscript(0, transcript, sender=node)

events = list(coordinator.TranscriptPosted.from_receipt(tx))
Expand All @@ -150,44 +156,53 @@ def test_post_transcript(coordinator, nodes, initiator, erc20, flat_rate_fee_mod
assert coordinator.getRitualState(0) == RitualState.AWAITING_AGGREGATIONS


def test_post_transcript_but_not_part_of_ritual(coordinator, nodes, initiator, erc20, flat_rate_fee_model):
def test_post_transcript_but_not_part_of_ritual(
coordinator, nodes, initiator, erc20, flat_rate_fee_model
):
cost = flat_rate_fee_model.getRitualInitiationCost(nodes, DURATION)
erc20.approve(coordinator.address, cost, sender=initiator)
coordinator.initiateRitual(nodes, initiator, DURATION, sender=initiator)
transcript = os.urandom(transcript_size(len(nodes), len(nodes)))
with ape.reverts("Participant not part of ritual"):
coordinator.postTranscript(0, os.urandom(TRANSCRIPT_SIZE), sender=initiator)
coordinator.postTranscript(0, transcript, sender=initiator)


def test_post_transcript_but_already_posted_transcript(coordinator, nodes, initiator, erc20, flat_rate_fee_model):
def test_post_transcript_but_already_posted_transcript(
coordinator, nodes, initiator, erc20, flat_rate_fee_model
):
cost = flat_rate_fee_model.getRitualInitiationCost(nodes, DURATION)
erc20.approve(coordinator.address, cost, sender=initiator)
coordinator.initiateRitual(nodes, initiator, DURATION, sender=initiator)
coordinator.postTranscript(0, os.urandom(TRANSCRIPT_SIZE), sender=nodes[0])
transcript = os.urandom(transcript_size(len(nodes), len(nodes)))
coordinator.postTranscript(0, transcript, sender=nodes[0])
with ape.reverts("Node already posted transcript"):
coordinator.postTranscript(0, os.urandom(TRANSCRIPT_SIZE), sender=nodes[0])
coordinator.postTranscript(0, transcript, sender=nodes[0])


def test_post_transcript_but_not_waiting_for_transcripts(coordinator, nodes, initiator, erc20, flat_rate_fee_model):
def test_post_transcript_but_not_waiting_for_transcripts(
coordinator, nodes, initiator, erc20, flat_rate_fee_model
):
cost = flat_rate_fee_model.getRitualInitiationCost(nodes, DURATION)
erc20.approve(coordinator.address, cost, sender=initiator)
coordinator.initiateRitual(nodes, initiator, DURATION, sender=initiator)
transcript = os.urandom(transcript_size(len(nodes), len(nodes)))
for node in nodes:
transcript = os.urandom(TRANSCRIPT_SIZE)
coordinator.postTranscript(0, transcript, sender=node)

with ape.reverts("Not waiting for transcripts"):
coordinator.postTranscript(0, os.urandom(TRANSCRIPT_SIZE), sender=nodes[1])
coordinator.postTranscript(0, transcript, sender=nodes[1])


def test_post_aggregation(coordinator, nodes, initiator, erc20, flat_rate_fee_model):
cost = flat_rate_fee_model.getRitualInitiationCost(nodes, DURATION)
erc20.approve(coordinator.address, cost, sender=initiator)
coordinator.initiateRitual(nodes, initiator, DURATION, sender=initiator)
transcript = os.urandom(TRANSCRIPT_SIZE)
transcript = os.urandom(transcript_size(len(nodes), len(nodes)))

for node in nodes:
coordinator.postTranscript(0, transcript, sender=node)

aggregated = os.urandom(TRANSCRIPT_SIZE)
aggregated = transcript # has the same size as transcript
decryptionRequestStaticKeys = [os.urandom(42) for node in nodes]
publicKey = (os.urandom(32), os.urandom(16))
for i, node in enumerate(nodes):
Expand Down

0 comments on commit 951187c

Please sign in to comment.