-
Notifications
You must be signed in to change notification settings - Fork 5
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
Init genesis in gov xmod #110
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8b8d704
init xmod
NYBACHOK d4e0d4f
tmp
NYBACHOK 60bda2d
add some init structure for genesis
NYBACHOK 3864ec8
implement params keeper and add trait for it in gears
NYBACHOK cd181bc
Merge remote-tracking branch 'upstream/main' into gov-xmod
NYBACHOK 67ae30a
merge
NYBACHOK 1dfc760
set deposit
NYBACHOK fa27aaa
vote genesis
NYBACHOK 4b68dc4
proposal genesis
NYBACHOK 45ea035
Merge remote-tracking branch 'upstream/main' into gov-xmod
NYBACHOK a7c9cb0
add without acc
NYBACHOK e08a87c
clarify code removal and difference from golang
NYBACHOK c4467d6
fmt
NYBACHOK 425150c
remove cast
NYBACHOK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
pub mod params; |
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,34 @@ | ||
use database::Database; | ||
use kv_store::StoreKey; | ||
|
||
use crate::{ | ||
context::{InfallibleContext, InfallibleContextMut, QueryableContext, TransactionalContext}, | ||
params::{ParamsDeserialize, ParamsSerialize, ParamsSubspaceKey}, | ||
types::store::gas::errors::GasStoreErrors, | ||
}; | ||
|
||
pub trait ParamsKeeper<PSK: ParamsSubspaceKey> { | ||
type Param: ParamsSerialize + ParamsDeserialize; | ||
|
||
fn get<DB: Database, SK: StoreKey, CTX: InfallibleContext<DB, SK>>( | ||
&self, | ||
ctx: &CTX, | ||
) -> Self::Param; | ||
|
||
fn try_get<DB: Database, SK: StoreKey, CTX: QueryableContext<DB, SK>>( | ||
&self, | ||
ctx: &CTX, | ||
) -> Result<Self::Param, GasStoreErrors>; | ||
|
||
fn set<DB: Database, SK: StoreKey, KV: InfallibleContextMut<DB, SK>>( | ||
&self, | ||
ctx: &mut KV, | ||
params: Self::Param, | ||
); | ||
|
||
fn try_set<DB: Database, SK: StoreKey, KV: TransactionalContext<DB, SK>>( | ||
&self, | ||
ctx: &mut KV, | ||
params: Self::Param, | ||
) -> Result<(), GasStoreErrors>; | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
pub mod ext; | ||
mod constants; | ||
pub mod errors; | ||
pub mod ext; | ||
pub mod guard; | ||
pub mod kv; | ||
pub mod prefix; | ||
|
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,13 @@ | ||
[package] | ||
name = "gov" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
gears = { path = "../../gears", features = ["cli", "export", "xmods"] } | ||
serde = { workspace = true } | ||
serde_json = { workspace = true } | ||
chrono = { workspace = true } | ||
|
||
[lints] | ||
workspace = true |
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,2 @@ | ||
pub const SERDE_JSON_CONVERSION: &str = "conversion to json shouldn't fail"; | ||
pub const EXISTS: &str = "value guaranteed to exists"; |
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,28 @@ | ||
use crate::{ | ||
params::GovParams, | ||
types::{deposit::Deposit, proposal::Proposal, vote::Vote}, | ||
}; | ||
|
||
pub struct GovGenesisState { | ||
pub starting_proposal_id: u64, | ||
pub deposits: Vec<Deposit>, | ||
pub votes: Vec<Vote>, | ||
pub proposals: Vec<Proposal>, | ||
pub params: GovParams, | ||
} | ||
|
||
impl Default for GovGenesisState { | ||
fn default() -> Self { | ||
Self { | ||
starting_proposal_id: 1, | ||
deposits: Vec::new(), | ||
votes: Vec::new(), | ||
proposals: Vec::new(), | ||
params: GovParams { | ||
tally: Default::default(), | ||
voting: Default::default(), | ||
deposit: Default::default(), | ||
}, | ||
} | ||
} | ||
} |
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,136 @@ | ||
use std::marker::PhantomData; | ||
|
||
use gears::{ | ||
application::keepers::params::ParamsKeeper, | ||
context::init::InitContext, | ||
params::ParamsSubspaceKey, | ||
store::{database::Database, StoreKey}, | ||
types::store::gas::ext::GasResultExt, | ||
x::{keepers::bank::BankKeeper, module::Module}, | ||
}; | ||
|
||
use crate::{ | ||
errors::SERDE_JSON_CONVERSION, genesis::GovGenesisState, params::GovParamsKeeper, | ||
types::proposal::ProposalStatus, | ||
}; | ||
|
||
const PROPOSAL_ID_KEY: [u8; 1] = [0x03]; | ||
pub(crate) const KEY_PROPOSAL_PREFIX: [u8; 1] = [0x00]; | ||
pub(crate) const KEY_DEPOSIT_PREFIX: [u8; 1] = [0x10]; | ||
pub(crate) const KEY_VOTES_PREFIX: [u8; 1] = [0x20]; | ||
|
||
#[allow(dead_code)] | ||
pub struct GovKeeper< | ||
SK: StoreKey, | ||
PSK: ParamsSubspaceKey, | ||
M: Module, | ||
BM: Module, | ||
BK: BankKeeper<SK, BM>, | ||
> { | ||
store_key: SK, | ||
gov_params_keeper: GovParamsKeeper<PSK>, | ||
gov_mod: M, | ||
bank_keeper: BK, | ||
_bank_marker: PhantomData<BM>, | ||
} | ||
|
||
impl<SK: StoreKey, PSK: ParamsSubspaceKey, M: Module, BM: Module, BK: BankKeeper<SK, BM>> | ||
GovKeeper<SK, PSK, M, BM, BK> | ||
{ | ||
pub fn new(store_key: SK, params_subspace_key: PSK, gov_mod: M, bank_keeper: BK) -> Self { | ||
Self { | ||
store_key, | ||
gov_params_keeper: GovParamsKeeper { | ||
params_subspace_key, | ||
}, | ||
gov_mod, | ||
bank_keeper, | ||
_bank_marker: PhantomData, | ||
} | ||
} | ||
|
||
pub fn init_genesis<DB: Database>( | ||
&self, | ||
ctx: &mut InitContext<'_, DB, SK>, | ||
GovGenesisState { | ||
starting_proposal_id, | ||
deposits, | ||
votes, | ||
proposals, | ||
params, | ||
}: GovGenesisState, | ||
) { | ||
{ | ||
let mut store = ctx.kv_store_mut(&self.store_key); | ||
store.set(PROPOSAL_ID_KEY, starting_proposal_id.to_be_bytes()) | ||
} | ||
self.gov_params_keeper.set(ctx, params); | ||
|
||
let total_deposits = { | ||
let mut store_mut = ctx.kv_store_mut(&self.store_key); | ||
|
||
let total_deposits = { | ||
let mut total_deposits = Vec::with_capacity(deposits.len()); | ||
for deposit in deposits { | ||
store_mut.set( | ||
deposit.key(), | ||
serde_json::to_vec(&deposit).expect(SERDE_JSON_CONVERSION), | ||
); // TODO:NOW IS THIS CORRECT SERIALIZATION? | ||
total_deposits.push(deposit.amount); | ||
} | ||
|
||
total_deposits.into_iter().flatten().collect::<Vec<_>>() | ||
}; | ||
|
||
for vote in votes { | ||
store_mut.set( | ||
vote.key(), | ||
serde_json::to_vec(&vote).expect(SERDE_JSON_CONVERSION), | ||
) | ||
} | ||
|
||
for proposal in proposals { | ||
match proposal.status { | ||
ProposalStatus::DepositPeriod => { | ||
store_mut.set( | ||
proposal.inactive_queue_key(), | ||
proposal.proposal_id.to_be_bytes(), | ||
); | ||
} | ||
ProposalStatus::VotingPeriod => store_mut.set( | ||
proposal.active_queue_key(), | ||
proposal.proposal_id.to_be_bytes(), | ||
), | ||
_ => (), | ||
} | ||
|
||
store_mut.set( | ||
proposal.key(), | ||
serde_json::to_vec(&proposal).expect(SERDE_JSON_CONVERSION), | ||
); | ||
} | ||
|
||
total_deposits | ||
}; | ||
|
||
let balance = self | ||
.bank_keeper | ||
.balance_all(ctx, &self.gov_mod.get_address()) | ||
.unwrap_gas(); | ||
/* | ||
Okay. I think that in our implementation there is no need to create account if it. | ||
|
||
So I should omit this lines... | ||
if balance.is_empty() || balance.iter().any(|this| this.amount.is_zero()) { | ||
https://github.com/cosmos/cosmos-sdk/blob/d3f09c222243bb3da3464969f0366330dcb977a8/x/gov/genesis.go#L47 | ||
} | ||
*/ | ||
|
||
if !(balance == total_deposits) { | ||
panic!( | ||
"expected module account was {:?} but we got {:?}", | ||
balance, total_deposits | ||
) | ||
} | ||
} | ||
} |
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,5 @@ | ||
pub mod errors; | ||
pub mod genesis; | ||
pub mod keeper; | ||
pub mod params; | ||
pub mod types; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we're expecting that in the bank module init genesis the gov_mod account will be initialized to the right amount?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. But I need to make sure that it created before usage(I suspected that It doesn't) and additional thing to note is denom.