Skip to content

Commit 38fec42

Browse files
authored
Merge pull request #422 from cygnusv/handover-gas
Simplify management of participants public key history
2 parents 666aace + ba11522 commit 38fec42

File tree

4 files changed

+28
-45
lines changed

4 files changed

+28
-45
lines changed

contracts/contracts/coordination/Coordinator.sol

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ contract Coordinator is Initializable, AccessControlDefaultAdminRulesUpgradeable
129129
BLS12381.G2Point publicKey;
130130
}
131131

132-
bytes32 public constant TREASURY_ROLE = keccak256("TREASURY_ROLE");
133132
bytes32 public constant FEE_MODEL_MANAGER_ROLE = keccak256("FEE_MODEL_MANAGER_ROLE");
134133
bytes32 public constant HANDOVER_SUPERVISOR_ROLE = keccak256("HANDOVER_SUPERVISOR_ROLE");
135134

@@ -140,7 +139,7 @@ contract Coordinator is Initializable, AccessControlDefaultAdminRulesUpgradeable
140139
uint32 public immutable handoverTimeout;
141140

142141
Ritual[] private ritualsStub; // former rituals, "internal" for testing only
143-
uint32 public dkgTimeoutStub;
142+
uint32 private timeoutStub; // former (dkg) timeout
144143
uint16 public maxDkgSize;
145144
bool private stub1; // former isInitiationPublic
146145

@@ -287,6 +286,7 @@ contract Coordinator is Initializable, AccessControlDefaultAdminRulesUpgradeable
287286
function setProviderPublicKey(BLS12381.G2Point calldata publicKey) external {
288287
uint32 lastRitualId = uint32(numberOfRituals);
289288
address stakingProvider = application.operatorToStakingProvider(msg.sender);
289+
require(!isProviderKeySet(msg.sender), "Provider has already set public key");
290290
require(stakingProvider != address(0), "Operator has no bond with staking provider");
291291

292292
ParticipantKey memory newRecord = ParticipantKey(lastRitualId, publicKey);
@@ -300,25 +300,18 @@ contract Coordinator is Initializable, AccessControlDefaultAdminRulesUpgradeable
300300

301301
function getProviderPublicKey(
302302
address provider,
303-
uint256 ritualId
303+
uint256
304304
) external view returns (BLS12381.G2Point memory) {
305305
ParticipantKey[] storage participantHistory = participantKeysHistory[provider];
306-
307-
for (uint256 i = participantHistory.length; i > 0; i--) {
308-
if (participantHistory[i - 1].lastRitualId <= ritualId) {
309-
return participantHistory[i - 1].publicKey;
310-
}
311-
}
312-
313-
revert("No keys found prior to the provided ritual");
306+
return participantHistory[participantHistory.length - 1].publicKey;
314307
}
315308

316-
/**
317-
* @dev This method is deprecated. Use `isProviderKeySet` instead.
318-
*/
319-
function isProviderPublicKeySet(address) external view returns (bool) {
320-
revert("Deprecated method. Upgrade your node to latest version");
321-
}
309+
// /**
310+
// * @dev This method is deprecated. Use `isProviderKeySet` instead.
311+
// */
312+
// function isProviderPublicKeySet(address) external view returns (bool) {
313+
// revert("Deprecated method. Upgrade your node to latest version");
314+
// }
322315

323316
function isProviderKeySet(address provider) public view returns (bool) {
324317
ParticipantKey[] storage participantHistory = participantKeysHistory[provider];
@@ -409,10 +402,6 @@ contract Coordinator is Initializable, AccessControlDefaultAdminRulesUpgradeable
409402
return id;
410403
}
411404

412-
function cohortFingerprint(address[] calldata nodes) public pure returns (bytes32) {
413-
return keccak256(abi.encode(nodes));
414-
}
415-
416405
function expectedTranscriptSize(
417406
uint16 dkgSize,
418407
uint16 threshold
@@ -591,6 +580,7 @@ contract Coordinator is Initializable, AccessControlDefaultAdminRulesUpgradeable
591580
bytes calldata transcript,
592581
bytes calldata decryptionRequestStaticKey
593582
) external {
583+
uint256 initialGasLeft = gasleft();
594584
require(isRitualActive(ritualId), "Ritual is not active");
595585
require(transcript.length > 0, "Parameters can't be empty");
596586
require(
@@ -609,9 +599,11 @@ contract Coordinator is Initializable, AccessControlDefaultAdminRulesUpgradeable
609599
handover.transcript = transcript;
610600
handover.decryptionRequestStaticKey = decryptionRequestStaticKey;
611601
emit HandoverTranscriptPosted(ritualId, departingParticipant, provider);
602+
processReimbursement(initialGasLeft);
612603
}
613604

614605
function postBlindedShare(uint32 ritualId, bytes calldata blindedShare) external {
606+
uint256 initialGasLeft = gasleft();
615607
require(isRitualActive(ritualId), "Ritual is not active");
616608

617609
address provider = application.operatorToStakingProvider(msg.sender);
@@ -624,6 +616,7 @@ contract Coordinator is Initializable, AccessControlDefaultAdminRulesUpgradeable
624616

625617
handover.blindedShare = blindedShare;
626618
emit BlindedSharePosted(ritualId, provider);
619+
processReimbursement(initialGasLeft);
627620
}
628621

629622
function cancelHandover(

scripts/simulate_coordinator_upgrade.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Usage:
22
# > ape run --interactive simulate_coordinator_upgrade --network polygon:mainnet-fork:foundry
33

4-
from ape import accounts, chain, project
4+
from ape import accounts, chain, networks, project
55

66
from deployment.constants import CONSTRUCTOR_PARAMS_DIR
77
from deployment.params import Deployer

tests/test_coordinator.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ def deployer(accounts):
4444

4545

4646
@pytest.fixture(scope="module")
47-
def treasury(accounts):
48-
treasury_index = MAX_DKG_SIZE + 3
49-
assert len(accounts) >= treasury_index
50-
return accounts[treasury_index]
47+
def fee_manager(accounts):
48+
fee_manager_index = MAX_DKG_SIZE + 3
49+
assert len(accounts) >= fee_manager_index
50+
return accounts[fee_manager_index]
5151

5252

5353
@pytest.fixture()
@@ -87,13 +87,12 @@ def coordinator(project, deployer, application, oz_dependency):
8787

8888

8989
@pytest.fixture()
90-
def fee_model(project, deployer, coordinator, erc20, treasury):
90+
def fee_model(project, deployer, coordinator, erc20, fee_manager):
9191
contract = project.FlatRateFeeModel.deploy(
9292
coordinator.address, erc20.address, FEE_RATE, sender=deployer
9393
)
94-
coordinator.grantRole(coordinator.FEE_MODEL_MANAGER_ROLE(), treasury, sender=deployer)
95-
coordinator.approveFeeModel(contract.address, sender=treasury)
96-
coordinator.grantRole(coordinator.TREASURY_ROLE(), treasury, sender=deployer)
94+
coordinator.grantRole(coordinator.FEE_MODEL_MANAGER_ROLE(), fee_manager, sender=deployer)
95+
coordinator.approveFeeModel(contract.address, sender=fee_manager)
9796
return contract
9897

9998

@@ -178,7 +177,7 @@ def initiate_ritual(coordinator, fee_model, erc20, authority, nodes, allow_logic
178177

179178

180179
def test_initiate_ritual(
181-
coordinator, nodes, initiator, erc20, fee_model, deployer, treasury, global_allow_list
180+
coordinator, nodes, initiator, erc20, fee_model, deployer, fee_manager, global_allow_list
182181
):
183182
authority, tx = initiate_ritual(
184183
coordinator=coordinator,
@@ -222,7 +221,7 @@ def test_initiate_ritual(
222221
assert fee_model.pendingFees(ritualID) == fee
223222

224223
with ape.reverts():
225-
fee_model.withdrawTokens(1, sender=treasury)
224+
fee_model.withdrawTokens(1, sender=fee_manager)
226225

227226
with ape.reverts("Can't withdraw pending fees"):
228227
fee_model.withdrawTokens(1, sender=deployer)
@@ -479,7 +478,7 @@ def test_get_participant(nodes, coordinator, initiator, erc20, fee_model, global
479478

480479

481480
def test_post_aggregation(
482-
coordinator, nodes, initiator, erc20, fee_model, treasury, deployer, global_allow_list
481+
coordinator, nodes, initiator, erc20, fee_model, fee_manager, deployer, global_allow_list
483482
):
484483
initiate_ritual(
485484
coordinator=coordinator,
@@ -527,7 +526,7 @@ def test_post_aggregation(
527526
assert retrieved_public_key == dkg_public_key
528527
assert coordinator.getRitualIdFromPublicKey(dkg_public_key) == ritualID
529528

530-
fee_model.processPendingFee(ritualID, sender=treasury)
529+
fee_model.processPendingFee(ritualID, sender=fee_manager)
531530
fee = fee_model.getRitualCost(len(nodes), DURATION)
532531
assert erc20.balanceOf(fee_model) == fee
533532
assert fee_model.totalPendingFees() == 0
@@ -539,7 +538,7 @@ def test_post_aggregation(
539538

540539

541540
def test_post_aggregation_fails(
542-
coordinator, nodes, initiator, erc20, fee_model, treasury, deployer, global_allow_list
541+
coordinator, nodes, initiator, erc20, fee_model, fee_manager, deployer, global_allow_list
543542
):
544543
initiator_balance_before_payment = erc20.balanceOf(initiator)
545544

@@ -593,7 +592,7 @@ def test_post_aggregation_fails(
593592
initiator_balance_before_refund = erc20.balanceOf(initiator)
594593
assert initiator_balance_before_refund == initiator_balance_before_payment - fee
595594

596-
fee_model.processPendingFee(ritualID, sender=treasury)
595+
fee_model.processPendingFee(ritualID, sender=fee_manager)
597596

598597
initiator_balance_after_refund = erc20.balanceOf(initiator)
599598
fee_model_balance_after_refund = erc20.balanceOf(fee_model)

tests/test_example.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)