-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(Issue Nos. 5 & 6) Squashed commit of the following:
commit 4517504 Author: Malte Kliemann <[email protected]> Date: Tue Dec 10 18:51:24 2024 +0100 Benchmark `DecisionMarketOracle` commit 5832f0b Author: Malte Kliemann <[email protected]> Date: Tue Dec 10 16:55:48 2024 +0100 Implement `DecisionMarketOracle` using scoreboard commit e684d47 Author: Malte Kliemann <[email protected]> Date: Mon Dec 9 21:24:47 2024 +0100 Add `BlockNumber` parameter to `update` commit 5f51378 Author: Malte Kliemann <[email protected]> Date: Mon Dec 9 21:17:20 2024 +0100 Update oracles on each block commit 2b831fb Author: Malte Kliemann <[email protected]> Date: Mon Dec 9 20:40:26 2024 +0100 Implement `try_mutate_all` commit 59cb185 Author: Malte Kliemann <[email protected]> Date: Mon Dec 9 18:24:56 2024 +0100 Use `ProposalStorage` and `MaxProposals` commit 58afe87 Author: Malte Kliemann <[email protected]> Date: Mon Dec 9 17:56:53 2024 +0100 Add `MaxProposals` value commit 22068d8 Author: Malte Kliemann <[email protected]> Date: Mon Dec 9 14:15:33 2024 +0100 Add `update` function to `FutarchyOracle` trait
- Loading branch information
1 parent
41d8752
commit 2ce829b
Showing
20 changed files
with
412 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::{ | ||
traits::ProposalStorage, types::Proposal, Config, Error, Pallet, ProposalCount, Proposals, | ||
ProposalsOf, | ||
}; | ||
use alloc::{collections::BTreeMap, vec, vec::Vec}; | ||
use frame_support::{ensure, require_transactional, traits::Get}; | ||
use frame_system::pallet_prelude::BlockNumberFor; | ||
use sp_runtime::{DispatchError, SaturatedConversion}; | ||
use zeitgeist_primitives::math::checked_ops_res::{CheckedIncRes, CheckedSubRes}; | ||
|
||
impl<T> ProposalStorage<T> for Pallet<T> | ||
where | ||
T: Config, | ||
{ | ||
fn count() -> u32 { | ||
ProposalCount::<T>::get() | ||
} | ||
|
||
#[require_transactional] | ||
fn add(block_number: BlockNumberFor<T>, proposal: Proposal<T>) -> Result<(), DispatchError> { | ||
let proposal_count = ProposalCount::<T>::get(); | ||
ensure!(proposal_count < T::MaxProposals::get(), Error::<T>::CacheFull); | ||
|
||
let new_proposal_count = proposal_count.checked_inc_res()?; | ||
ProposalCount::<T>::put(new_proposal_count); | ||
|
||
// Can't error unless state is invalid. | ||
let mutate_result = Proposals::<T>::try_mutate(block_number, |proposals| { | ||
proposals.try_push(proposal).map_err(|_| Error::<T>::CacheFull) | ||
}); | ||
|
||
Ok(mutate_result?) | ||
} | ||
|
||
/// Take all proposals scheduled at `block_number`. | ||
fn take(block_number: BlockNumberFor<T>) -> Result<ProposalsOf<T>, DispatchError> { | ||
let proposals = Proposals::<T>::take(block_number); | ||
|
||
// Can't error unless state is invalid. | ||
let proposal_count = ProposalCount::<T>::get(); | ||
let proposals_len: u32 = proposals.len().try_into().map_err(|_| Error::<T>::CacheFull)?; | ||
let new_proposal_count = proposal_count.checked_sub_res(&proposals_len)?; | ||
ProposalCount::<T>::put(new_proposal_count); | ||
|
||
Ok(proposals) | ||
} | ||
|
||
/// Returns all proposals scheduled at `block_number`. | ||
fn get(block_number: BlockNumberFor<T>) -> ProposalsOf<T> { | ||
Proposals::<T>::get(block_number) | ||
} | ||
|
||
fn mutate_all<R, F>( | ||
mut mutator: F, | ||
) -> Result<BTreeMap<BlockNumberFor<T>, Vec<R>>, DispatchError> | ||
where | ||
F: FnMut(&mut Proposal<T>) -> R, | ||
{ | ||
// Collect keys to avoid iterating over the keys whilst modifying the map. Won't saturate | ||
// unless `usize` has fewer bits than `u32` for some reason. | ||
let keys: Vec<_> = | ||
Proposals::<T>::iter_keys().take(T::MaxProposals::get().saturated_into()).collect(); | ||
|
||
let mut result_map = BTreeMap::new(); | ||
|
||
for k in keys.into_iter() { | ||
let proposals = Self::get(k); | ||
|
||
let mut results = vec![]; | ||
|
||
// If mutation goes out of bounds, we've clearly failed. | ||
let proposals = proposals | ||
.try_mutate(|v| { | ||
for p in v.iter_mut() { | ||
let r = mutator(p); | ||
results.push(r); | ||
} | ||
}) | ||
.ok_or(Error::<T>::UnexpectedStorageFailure)?; | ||
|
||
result_map.insert(k, results); | ||
|
||
Proposals::<T>::insert(k, proposals); | ||
} | ||
|
||
Ok(result_map) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.