Skip to content

Commit

Permalink
use mapping to store participant pk
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 30, 2023
1 parent d6f07bd commit 48d5794
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
19 changes: 16 additions & 3 deletions contracts/contracts/coordination/Coordinator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ contract Coordinator is AccessControlDefaultAdminRules {

bytes32 public constant INITIATOR_ROLE = keccak256("INITIATOR_ROLE");

mapping(address => bytes) public providerPublicKey;

IAccessControlApplication public immutable application;

Ritual[] public rituals;
Expand Down Expand Up @@ -124,6 +126,12 @@ contract Coordinator is AccessControlDefaultAdminRules {
_setRoleAdmin(INITIATOR_ROLE, bytes32(0));
}

function setProviderPublicKey(bytes calldata publicKey) external {
// TODO: Verify public key length
require(publicKey.length == 48, "Invalid public key length");
providerPublicKey[msg.sender] = publicKey;
}

function setTimeout(uint32 newTimeout) external onlyRole(DEFAULT_ADMIN_ROLE) {
emit TimeoutChanged(timeout, newTimeout);
timeout = newTimeout;
Expand All @@ -136,7 +144,7 @@ contract Coordinator is AccessControlDefaultAdminRules {

function setReimbursementPool(IReimbursementPool pool) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(
address(pool) == address(0) ||
address(pool) == address(0) ||
pool.isAuthorized(address(this)),
"Invalid ReimbursementPool"
);
Expand Down Expand Up @@ -179,6 +187,11 @@ contract Coordinator is AccessControlDefaultAdminRules {
for(uint256 i=0; i < length; i++){
Participant storage newParticipant = ritual.participant.push();
address current = providers[i];
// Make sure that current provider has already set their public key
require(
providerPublicKey[current].length > 0,
"Provider has not set their public key"
);
require(previous < current, "Providers must be sorted");
// TODO: Improve check for eligible nodes (staking, etc) - nucypher#3109
// TODO: Change check to isAuthorized(), without amount
Expand All @@ -191,7 +204,7 @@ contract Coordinator is AccessControlDefaultAdminRules {
}

processRitualPayment(id, providers, duration);

// TODO: Include cohort fingerprint in StartRitual event?
emit StartRitual(id, ritual.authority, providers);
return id;
Expand Down Expand Up @@ -369,7 +382,7 @@ contract Coordinator is AccessControlDefaultAdminRules {
currency.transferFrom(address(this), ritual.initiator, refundableFee);
}
}

function processReimbursement(uint256 initialGasLeft) internal {
if(address(reimbursementPool) != address(0)){ // TODO: Consider defining a method
uint256 gasUsed = initialGasLeft - gasleft();
Expand Down
2 changes: 1 addition & 1 deletion tests/application/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
REWARD_DURATION = 60 * 60 * 24 * 7 # one week in seconds
DEAUTHORIZATION_DURATION = 60 * 60 * 24 * 60 # 60 days in seconds

DEPENDENCY = project.dependencies["openzeppelin"]["4.8.1"]
DEPENDENCY = project.dependencies["openzeppelin"]["4.9.1"]


@pytest.fixture()
Expand Down
56 changes: 30 additions & 26 deletions tests/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,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 +74,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 All @@ -100,6 +97,11 @@ def test_invalid_initiate_ritual(coordinator, nodes, accounts, initiator):
with ape.reverts("Invalid ritual duration"):
coordinator.initiateRitual(nodes, initiator, 0, sender=initiator)

with ape.reverts("Provider has not set their public key"):
coordinator.initiateRitual(nodes, initiator, DURATION, sender=initiator)

for node in nodes:
coordinator.setProviderPublicKey(os.urandom(48), sender=node)
with ape.reverts("Providers must be sorted"):
coordinator.initiateRitual(nodes[1:] + [nodes[0]], initiator, DURATION, sender=initiator)

Expand All @@ -108,11 +110,17 @@ def test_invalid_initiate_ritual(coordinator, nodes, accounts, initiator):
coordinator.initiateRitual(nodes, initiator, DURATION, sender=initiator)


def test_initiate_ritual(coordinator, nodes, initiator, erc20, flat_rate_fee_model):
def initiate_ritual(coordinator, erc20, flat_rate_fee_model, initiator, nodes):
for node in nodes:
coordinator.setProviderPublicKey(os.urandom(48), sender=node)
cost = flat_rate_fee_model.getRitualInitiationCost(nodes, DURATION)
erc20.approve(coordinator.address, cost, sender=initiator)
authority = initiator
tx = coordinator.initiateRitual(nodes, authority, DURATION, sender=initiator)
tx = coordinator.initiateRitual(nodes, initiator, DURATION, sender=initiator)
return initiator, tx


def test_initiate_ritual(coordinator, nodes, initiator, erc20, flat_rate_fee_model):
authority, tx = initiate_ritual(coordinator, erc20, flat_rate_fee_model, initiator, nodes)

events = list(coordinator.StartRitual.from_receipt(tx))
assert len(events) == 1
Expand All @@ -125,9 +133,7 @@ def test_initiate_ritual(coordinator, nodes, initiator, erc20, flat_rate_fee_mod


def test_post_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)
initiate_ritual(coordinator, erc20, flat_rate_fee_model, initiator, nodes)

for node in nodes:
assert coordinator.getRitualState(0) == RitualState.AWAITING_TRANSCRIPTS
Expand All @@ -150,27 +156,27 @@ 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):
cost = flat_rate_fee_model.getRitualInitiationCost(nodes, DURATION)
erc20.approve(coordinator.address, cost, sender=initiator)
coordinator.initiateRitual(nodes, initiator, DURATION, sender=initiator)
def test_post_transcript_but_not_part_of_ritual(
coordinator, nodes, initiator, erc20, flat_rate_fee_model
):
initiate_ritual(coordinator, erc20, flat_rate_fee_model, initiator, nodes)
with ape.reverts("Participant not part of ritual"):
coordinator.postTranscript(0, os.urandom(TRANSCRIPT_SIZE), sender=initiator)


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)
def test_post_transcript_but_already_posted_transcript(
coordinator, nodes, initiator, erc20, flat_rate_fee_model
):
initiate_ritual(coordinator, erc20, flat_rate_fee_model, initiator, nodes)
coordinator.postTranscript(0, os.urandom(TRANSCRIPT_SIZE), sender=nodes[0])
with ape.reverts("Node already posted transcript"):
coordinator.postTranscript(0, os.urandom(TRANSCRIPT_SIZE), sender=nodes[0])


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)
def test_post_transcript_but_not_waiting_for_transcripts(
coordinator, nodes, initiator, erc20, flat_rate_fee_model
):
initiate_ritual(coordinator, erc20, flat_rate_fee_model, initiator, nodes)
for node in nodes:
transcript = os.urandom(TRANSCRIPT_SIZE)
coordinator.postTranscript(0, transcript, sender=node)
Expand All @@ -180,9 +186,7 @@ def test_post_transcript_but_not_waiting_for_transcripts(coordinator, nodes, ini


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)
initiate_ritual(coordinator, erc20, flat_rate_fee_model, initiator, nodes)
transcript = os.urandom(TRANSCRIPT_SIZE)
for node in nodes:
coordinator.postTranscript(0, transcript, sender=node)
Expand Down

0 comments on commit 48d5794

Please sign in to comment.