From a06db0556fc5dd8ccc69b37af28dc616acbe006d Mon Sep 17 00:00:00 2001 From: Artemka374 Date: Tue, 15 Aug 2023 13:40:31 +0300 Subject: [PATCH] fix checkpoints, add check for hex in _is_valid_description --- contracts/src/governance/governor/internal.rs | 7 +++++++ contracts/src/utils/checkpoint/mod.rs | 21 ++++--------------- tests/e2e/governance/governor.tests.ts | 4 ++-- tests/e2e/governance/helper.ts | 2 ++ 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/contracts/src/governance/governor/internal.rs b/contracts/src/governance/governor/internal.rs index 4ef22869d..efabf5fc9 100644 --- a/contracts/src/governance/governor/internal.rs +++ b/contracts/src/governance/governor/internal.rs @@ -252,6 +252,13 @@ pub trait GovernorInternal: return Ok(true) } + let pos = description.find("proposer=0x").unwrap(); + let address = &description[pos..]; + + if hex::decode(address).is_err() { + return Ok(true) + } + let proposer_str = hex::encode(proposer); let result = String::from("#proposer=0x".to_owned() + &proposer_str); diff --git a/contracts/src/utils/checkpoint/mod.rs b/contracts/src/utils/checkpoint/mod.rs index 1c67e42e3..0ac4c2e44 100644 --- a/contracts/src/utils/checkpoint/mod.rs +++ b/contracts/src/utils/checkpoint/mod.rs @@ -94,14 +94,7 @@ impl Checkpoints { match pos == 0 { true => None, - false => { - Some( - self.checkpoints - .get(pos - 1) - .unwrap_or_else(|| panic!("wrong checkpoint position"))// todo: remove panic - .value, - ) - } + false => Some(self.checkpoints[pos - 1].value), } } @@ -165,14 +158,8 @@ impl Checkpoints { fn _upper_binary_lookup(&self, key: u64, mut low: usize, mut high: usize) -> usize { while low < high { - let mid = low / 2 + high / 2; - if key - < self - .checkpoints - .get(mid) - .unwrap_or_else(|| panic!("wrong checkpoint position"))// todo: remove panic - .key - { + let mid = (low + high) / 2; + if key < self.checkpoints[mid].key { high = mid; } else { low = mid + 1; @@ -183,7 +170,7 @@ impl Checkpoints { fn _lower_binary_lookup(&self, key: u64, mut low: usize, mut high: usize) -> usize { while low < high { - let mid = low / 2 + high / 2; + let mid = (low + high) / 2; if key > self.checkpoints[mid].key { low = mid + 1; } else { diff --git a/tests/e2e/governance/governor.tests.ts b/tests/e2e/governance/governor.tests.ts index f7a02b71a..107d8cb45 100644 --- a/tests/e2e/governance/governor.tests.ts +++ b/tests/e2e/governance/governor.tests.ts @@ -666,7 +666,7 @@ describe('Governor', function () { contractReceiver.address, getSelectorByName(contractReceiver.abi.messages, 'mock_function'), [], - '#proposer=' + '0x1234' + '#proposer=' + '0x3C44CdDdB6a900fa2b585dd299e03d12FA429XYZ' ) await expect(helper.propose(deployer)).to.eventually.be.fulfilled @@ -681,7 +681,7 @@ describe('Governor', function () { contractReceiver.address, getSelectorByName(contractReceiver.abi.messages, 'mock_function'), [], - '#proposer=' + '0x1234' + '#proposer=' + '0x3C44CdDdB6a900fa2b585dd299e03d12FA429XYZ' ) await expect(helper.propose(alice)).to.eventually.be.fulfilled diff --git a/tests/e2e/governance/helper.ts b/tests/e2e/governance/helper.ts index 8c6cb10bc..5f3d6a33a 100644 --- a/tests/e2e/governance/helper.ts +++ b/tests/e2e/governance/helper.ts @@ -50,6 +50,8 @@ export class GovernorHelper { console.log('Proposal ID: ', await this.calculateProposalId()) + console.log((await this.governor?.query.propose([this.proposal!], this.description!))?.value.ok?.err) + if(proposer) { await this.governor?.withSigner(proposer).tx.propose([this.proposal!], this.description!) }