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

Add Governor component #1180

Open
wants to merge 90 commits into
base: main
Choose a base branch
from

Conversation

ericnordelo
Copy link
Member

Fixes #294

PR Checklist

  • Tests
  • Documentation
  • Added entry to CHANGELOG.md
  • Tried the feature on a public network

…eat/remove-mocks-from-released-package-#1141
* feat: remove modules

* fix: mock

* fix: linter

* fix: tests

* fix: mock

* feat: apply review suggestions
…eat/remove-mocks-from-released-package-#1141
…eat/remove-mocks-from-released-package-#1141
…eat/remove-mocks-from-released-package-#1141
Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good start, Eric! I still have to review tests and I know more are forthcoming as well as some TODOs. In the mean time, I left some initial comments and questions

Copy link
Collaborator

@immrsd immrsd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job, Eric! That wasn't an easy walkthrough, the feature is really complex. It's looking good, I left several comments

packages/governance/src/governor/interface.cairo Outdated Show resolved Hide resolved
packages/governance/src/governor/interface.cairo Outdated Show resolved Hide resolved
packages/governance/src/governor/interface.cairo Outdated Show resolved Hide resolved
canceled: canceled > 0,
eta_seconds: eta_seconds.try_into().unwrap()
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using u256_safe_divmod to reduce the number of arithmetic operations? The impl could look like this if we put the boolean value at the latest bit indices

let val: u256 = second_felt.into();
let (val, executed) = u256_safe_divmod(val, 2);
let (val, canceled) = u256_safe_divmod(val, 2);
let (val, eta_seconds) = u256_safe_divmod(val, _2_POW_64);
let (val, vote_duration) = u256_safe_divmod(val, _2_POW_64);
let (val, vote_start) = u256_safe_divmod(val, _2_POW_64);

And this way we'll need only single bits constant instead of 7

Copy link
Member Author

@ericnordelo ericnordelo Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks a lot cleaner indeed. The issue is that u256_safe_divmod is not public, but we can use the DivRem trait which is the exposed mechanism by design.

packages/utils/src/bytearray.cairo Outdated Show resolved Hide resolved
Comment on lines +502 to +505
self
.validate_state(
proposal_id, array![ProposalState::Succeeded, ProposalState::Queued].span()
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be overkill for checking the state: we have to create an array and iterate through it. Maybe a more direct approach would be better

Suggested change
self
.validate_state(
proposal_id, array![ProposalState::Succeeded, ProposalState::Queued].span()
);
match self.state(proposal_id) {
ProposalState::Succeeded => (),
ProposalState::Queued => (),
_ => panic_with_felt252(Errors:: UNEXPECTED_PROPOSAL_STATE),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably more efficient, but the validate_state helper can be used by users as well. We can refactor it into bitmap operations to avoid looping or matching, which should be even more efficient, and also usable by users. Let's keep the thread opened until the pending stuff is finished and I will work on that then.

@ericnordelo ericnordelo marked this pull request as ready for review November 15, 2024 00:05
pub enum VoteType {
Against,
For,
Abstain,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Abstain,
Abstain

Comment on lines +95 to +103
if timelock_dispatcher.is_operation_pending(queue_id) {
ProposalState::Queued
} else if timelock_dispatcher.is_operation_done(queue_id) {
// This can happen if the proposal is executed directly on the timelock.
ProposalState::Executed
} else {
// This can happen if the proposal is canceled directly on the timelock.
ProposalState::Canceled
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but in the case of Executed or Canceled proposal state we'll have to make 2 external calls to timelock contract (is_operation_pending + is_operation_done) instead of a single get_operation_state call

Comment on lines +137 to +147
let (_, key, value) = self
.Governor_quorum_numerator_history
.deref()
.latest_checkpoint();

if key <= timepoint {
return value;
}

// Fallback to the binary search
self.Governor_quorum_numerator_history.deref().upper_lookup(timepoint)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let (_, key, value) = self
.Governor_quorum_numerator_history
.deref()
.latest_checkpoint();
if key <= timepoint {
return value;
}
// Fallback to the binary search
self.Governor_quorum_numerator_history.deref().upper_lookup(timepoint)
let history = self.Governor_quorum_numerator_history.deref();
let (_, key, value) = history.latest_checkpoint();
if key < timepoint {
value
} else {
// Fallback to the binary search
history.upper_lookup(timepoint)

ref self: ComponentState<TContractState>, calls: Span<Call>, description_hash: felt252
) -> felt252 {
let proposal_id = self._hash_proposal(calls, description_hash);
self.validate_state(proposal_id, array![ProposalState::Pending].span());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to cancel a queued proposal?

Comment on lines +627 to +634
let is_valid_signature_felt = ISRC6Dispatcher { contract_address: voter }
.is_valid_signature(hash, signature.into());

// 3. Check either 'VALID' or true for backwards compatibility
let is_valid_signature = is_valid_signature_felt == starknet::VALIDATED
|| is_valid_signature_felt == 1;

assert(is_valid_signature, Errors::INVALID_SIGNATURE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the third place we use the same boilerplate code to validate a signature, shall we consider extracting it into a util function?

Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic job with the first iteration of tests! I left some comments

/// governor as the only proposer, canceller, and executor.
///
/// WARNING: When the executor is not the governor itself (i.e. a timelock), it can call
/// functions that are restricted with the `assert_only_governance` modifier, and also
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// functions that are restricted with the `assert_only_governance` modifier, and also
/// functions that are restricted with the `assert_only_governance` check, and also

or something other than modifier

Comment on lines +824 to +828
/// WARNING: When the executor is not the governor itself (i.e. a timelock), it can call
/// functions that are restricted with this modifier, and also potentially execute
/// transactions on behalf of the governor. Because of this, this module is designed to work
/// with the TimelockController as the unique potential external executor. The timelock
/// MUST have the governor as the only proposer, canceller, and executor.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +649 to +650
/// Emits a `VoteCast` event if no params are provided.
/// Emits a `VoteCastWithParams` event otherwise.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Emits a `VoteCast` event if no params are provided.
/// Emits a `VoteCastWithParams` event otherwise.
/// Emits either:
/// - `VotesCast`...
/// - `VoteCastWithParams` ...

Nitpick-ish: Maybe presenting it like this might be easier to follow? When I see two "Emits" at first glance, I think it emits two events

/// Returns a unique hash given a ByteArray.
///
/// The hash is computed by serializing the data into a span of felts, and
/// then hashing the span using the Poseidon hash algorithm.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// then hashing the span using the Poseidon hash algorithm.
/// then hashing the span using the Pedersen hash algorithm.

to_byte_array(self, base, padding)
}

/// Hashes a byte array using the Poseidon hash algorithm.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Hashes a byte array using the Poseidon hash algorithm.
/// Hashes a byte array using the Pedersen hash algorithm.

Comment on lines +1247 to +1250
// Use invalid nonce (not the account's current nonce)
let (governor, r, s, proposal_id, support, voter, _) =
prepare_governor_and_signature_with_reason_and_params(
@reason, params, 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Use invalid nonce (not the account's current nonce)
let (governor, r, s, proposal_id, support, voter, _) =
prepare_governor_and_signature_with_reason_and_params(
@reason, params, 0
let (governor, r, s, proposal_id, support, voter, _) =
prepare_governor_and_signature_with_reason_and_params(
@reason, params, 0

The nonce looks valid here

Comment on lines +1265 to +1269

// Use invalid nonce (not the account's current nonce)
let (governor, r, s, proposal_id, support, voter, _) =
prepare_governor_and_signature_with_reason_and_params(
@reason, params, 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Use invalid nonce (not the account's current nonce)
let (governor, r, s, proposal_id, support, voter, _) =
prepare_governor_and_signature_with_reason_and_params(
@reason, params, 1
let invalid_nonce = 1;
// Use invalid nonce (not the account's current nonce)
let (governor, r, s, proposal_id, support, voter, _) =
prepare_governor_and_signature_with_reason_and_params(
@reason, params, invalid_nonce

Easier to follow IMO

Comment on lines +1353 to +1361
#[test]
#[should_panic(expected: 'Executor only')]
fn test_relay_invalid_caller() {
let mut state = COMPONENT_STATE();
let call = Call { to: ADMIN(), selector: selector!("foo"), calldata: array![].span() };

start_cheat_caller_address(test_address(), OTHER());
state.relay(call);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include a failing tx test for the syscall (confirming it won't silently fail)

impl SRC5: SRC5Component::HasComponent<TContractState>,
+Drop<TContractState>
> of InternalTrait<TContractState> {
/// Initializes the contract by registering the supported interface Ids.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Initializes the contract by registering the supported interface Ids.
/// Initializes the contract by registering the supported interface id.

Comment on lines +125 to +126
/// Temporary defined as a function since constant ByteArray is not supported yet.
fn DEFAULT_PARAMS() -> Span<felt252>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should adjust the comment. Maybe test this with a non-empty span, WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Governance Contracts
3 participants