Skip to content

Commit

Permalink
apply pr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec authored and KPrasch committed Jul 12, 2023
1 parent da8123d commit a4de3c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
22 changes: 11 additions & 11 deletions contracts/contracts/coordination/Coordinator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract Coordinator is AccessControlDefaultAdminRules {
event TimeoutChanged(uint32 oldTimeout, uint32 newTimeout);
event MaxDkgSizeChanged(uint16 oldSize, uint16 newSize);

event ParticipantPublicKeySet(address indexed participant, BLS12381.G1Point publicKey);
event ParticipantPublicKeySet(uint32 indexed ritualId, address indexed participant, BLS12381.G2Point publicKey);

enum RitualState {
NON_INITIATED,
Expand Down Expand Up @@ -64,8 +64,8 @@ contract Coordinator is AccessControlDefaultAdminRules {
}

struct ParticipantKey {
uint32 ritualId;
BLS12381.G1Point publicKey;
uint32 lastRitualId;
BLS12381.G2Point publicKey;
}

using SafeERC20 for IERC20;
Expand Down Expand Up @@ -133,21 +133,21 @@ contract Coordinator is AccessControlDefaultAdminRules {
_setRoleAdmin(INITIATOR_ROLE, bytes32(0));
}

function setProviderPublicKey(BLS12381.G1Point calldata _publicKey) public {
function setProviderPublicKey(BLS12381.G2Point calldata _publicKey) public {
uint32 lastRitualId = uint32(rituals.length);
address provider = application.stakingProviderFromOperator(msg.sender);

ParticipantKey memory newRecord = ParticipantKey(lastRitualId, _publicKey);
keysHistory[provider].push(newRecord);

emit ParticipantPublicKeySet(provider, _publicKey);
emit ParticipantPublicKeySet(lastRitualId, provider, _publicKey);
}

function getProviderPublicKey(address _address, uint _ritualId) public view returns (BLS12381.G1Point memory) {
ParticipantKey[] storage participantHistory = keysHistory[_address];
function getProviderPublicKey(address _provider, uint _ritualId) external view returns (BLS12381.G2Point memory) {
ParticipantKey[] storage participantHistory = keysHistory[_provider];

for (uint i = participantHistory.length - 1; i >= 0; i--) {
if (participantHistory[i].ritualId <= _ritualId) {
if (participantHistory[i].lastRitualId <= _ritualId) {
return participantHistory[i].publicKey;
}
}
Expand Down Expand Up @@ -275,7 +275,7 @@ contract Coordinator is AccessControlDefaultAdminRules {
function postAggregation(
uint32 ritualId,
bytes calldata aggregatedTranscript,
BLS12381.G1Point calldata publicKey,
BLS12381.G1Point calldata dkgPublicKey,
bytes calldata decryptionRequestStaticKey
) external {
uint256 initialGasLeft = gasleft();
Expand Down Expand Up @@ -316,9 +316,9 @@ contract Coordinator is AccessControlDefaultAdminRules {

if (ritual.aggregatedTranscript.length == 0) {
ritual.aggregatedTranscript = aggregatedTranscript;
ritual.publicKey = publicKey;
ritual.publicKey = dkgPublicKey;
} else if (
!BLS12381.eqG1Point(ritual.publicKey, publicKey) ||
!BLS12381.eqG1Point(ritual.publicKey, dkgPublicKey) ||
keccak256(ritual.aggregatedTranscript) != aggregatedTranscriptDigest
) {
ritual.aggregationMismatch = true;
Expand Down
16 changes: 10 additions & 6 deletions tests/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def transcript_size(shares, threshold):
return int(424 + 240 * (shares / 2) + 50 * (threshold))


def gen_public_key():
return (os.urandom(32), os.urandom(32), os.urandom(32))


@pytest.fixture(scope="module")
def nodes(accounts):
return sorted(accounts[:MAX_DKG_SIZE], key=lambda x: x.address.lower())
Expand Down Expand Up @@ -110,7 +114,7 @@ def test_invalid_initiate_ritual(coordinator, nodes, accounts, initiator):
coordinator.initiateRitual(nodes, initiator, DURATION, sender=initiator)

for node in nodes:
public_key = (os.urandom(32), os.urandom(16))
public_key = gen_public_key()
coordinator.setProviderPublicKey(public_key, sender=node)
with ape.reverts("Providers must be sorted"):
coordinator.initiateRitual(nodes[1:] + [nodes[0]], initiator, DURATION, sender=initiator)
Expand All @@ -122,7 +126,7 @@ def test_invalid_initiate_ritual(coordinator, nodes, accounts, initiator):

def initiate_ritual(coordinator, erc20, flat_rate_fee_model, initiator, nodes):
for node in nodes:
public_key = (os.urandom(32), os.urandom(16))
public_key = gen_public_key()
coordinator.setProviderPublicKey(public_key, sender=node)
cost = flat_rate_fee_model.getRitualInitiationCost(nodes, DURATION)
erc20.approve(coordinator.address, cost, sender=initiator)
Expand All @@ -143,9 +147,9 @@ def test_initiate_ritual(coordinator, nodes, initiator, erc20, flat_rate_fee_mod
assert coordinator.getRitualState(0) == RitualState.AWAITING_TRANSCRIPTS


def test_test_provider_public_key(coordinator, nodes):
def test_provider_public_key(coordinator, nodes):
selected_provider = nodes[0]
public_key = (os.urandom(32), os.urandom(16))
public_key = gen_public_key()
tx = coordinator.setProviderPublicKey(public_key, sender=selected_provider)
ritual_id = coordinator.numberOfRituals()

Expand Down Expand Up @@ -220,11 +224,11 @@ def test_post_aggregation(coordinator, nodes, initiator, erc20, flat_rate_fee_mo

aggregated = transcript # has the same size as transcript
decryption_request_static_keys = [os.urandom(42) for _ in nodes]
public_key = (os.urandom(32), os.urandom(16))
dkg_public_key = (os.urandom(32), os.urandom(16))
for i, node in enumerate(nodes):
assert coordinator.getRitualState(0) == RitualState.AWAITING_AGGREGATIONS
tx = coordinator.postAggregation(
0, aggregated, public_key, decryption_request_static_keys[i], sender=node
0, aggregated, dkg_public_key, decryption_request_static_keys[i], sender=node
)

events = list(coordinator.AggregationPosted.from_receipt(tx))
Expand Down

0 comments on commit a4de3c9

Please sign in to comment.