-
Notifications
You must be signed in to change notification settings - Fork 12
Implement Staking module #335
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b3c89dd
Initialize the staking module
HoOngEe a7bfce2
Implement the state and types of Staking module
HoOngEe 6d2ce4c
Implement transaction and related types for the staking module
HoOngEe 80e7fb0
Improve functionality of TransactionExecutionOutcome
HoOngEe 611e35c
Implement abci requirements
HoOngEe a5ac0e1
Implement transactions related to term changes
HoOngEe 79d56e9
Implement AdditionalTxCreator
HoOngEe 012a96b
Sort validators by public key
HoOngEe 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 hidden or 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 hidden or 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,16 @@ | ||
| [package] | ||
| name = "staking" | ||
| version = "0.1.0" | ||
| authors = ["CodeChain Team <hi@codechain.io>"] | ||
| edition = "2018" | ||
|
|
||
| [dependencies] | ||
| primitives = { git = "https://github.com/CodeChain-io/rust-codechain-primitives.git", version = "0.4" } | ||
| coordinator = { path = "../../coordinator" } | ||
| lazy_static = "1.4" | ||
| serde_cbor = "0.11.1" | ||
| serde = { version = "1.0", features = ["derive"] } | ||
| serde_derive = "1.0.105" | ||
| codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.2" } | ||
| ftypes = { path = "../../types", package = "codechain-types" } | ||
| fkey = { path = "../../key", package = "codechain-key" } |
This file contains hidden or 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,35 @@ | ||
| // Copyright 2020 Kodebox, Inc. | ||
| // This file is part of CodeChain. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| use crate::check_network_id; | ||
| use crate::syntax_error::Error; | ||
| use crate::transactions::{SignedTransaction, UserTransaction}; | ||
|
|
||
| pub fn check(signed_tx: &SignedTransaction) -> Result<(), Error> { | ||
| if !signed_tx.verify() { | ||
| Err(Error::InvalidSignature(signed_tx.signature)) | ||
| } else { | ||
| check_inner(&signed_tx.tx) | ||
| } | ||
| } | ||
|
|
||
| fn check_inner(tx: &UserTransaction) -> Result<(), Error> { | ||
| if !check_network_id(tx.network_id) { | ||
| Err(Error::InvalidNetworkId(tx.network_id)) | ||
| } else { | ||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or 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,46 @@ | ||
| // Copyright 2020 Kodebox, Inc. | ||
| // This file is part of CodeChain. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| use crate::state::{Banned, Params}; | ||
| use crate::transactions::Transaction; | ||
| use crate::types::{Header, Public, Validator}; | ||
| pub use coordinator::context::SubStorageAccess; | ||
| pub use coordinator::types::{ExecuteTransactionError, HeaderError, TransactionExecutionOutcome, VerifiedCrime}; | ||
| use std::collections::HashMap; | ||
|
|
||
| pub trait Abci { | ||
| fn open_block(&self, header: &Header, verified_crime: &[VerifiedCrime]) -> Result<(), HeaderError>; | ||
| fn execute_transactions( | ||
| &self, | ||
| transactions: Vec<Transaction>, | ||
| ) -> Result<Vec<TransactionExecutionOutcome>, ExecuteTransactionError>; | ||
| fn check_transaction(&self, transaction: &Transaction) -> Result<(), i64>; | ||
| } | ||
|
|
||
| pub trait StakingView { | ||
| fn get_stakes(&self) -> HashMap<Public, u64>; | ||
| fn get_validators(&self) -> Vec<Validator>; | ||
| fn current_term_id(&self) -> u64; | ||
| fn get_term_common_params(&self) -> Params; | ||
| fn is_term_changed(&self) -> bool; | ||
| fn last_term_finished_block_num(&self) -> u64; | ||
| fn era(&self) -> u64; | ||
| fn get_banned_validators(&self) -> Banned; | ||
| } | ||
|
|
||
| pub trait AdditionalTxCreator { | ||
| fn create(&self) -> Vec<Transaction>; | ||
| } |
This file contains hidden or 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,66 @@ | ||
| // Copyright 2020 Kodebox, Inc. | ||
| // This file is part of CodeChain. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| pub use crate::runtime_error::Error as RuntimeError; | ||
| pub use crate::syntax_error::Error as SyntaxError; | ||
| use std::fmt::{Display, Formatter, Result as FormatResult}; | ||
|
|
||
| #[derive(Debug)] | ||
| /// Error indicating an expected value was not found. | ||
| pub struct Mismatch<T> { | ||
| /// Value expected. | ||
| pub expected: T, | ||
| /// Value found. | ||
| pub found: T, | ||
| } | ||
|
|
||
| impl<T: Display> Display for Mismatch<T> { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult { | ||
| write!(f, "Expected {}, found {}", self.expected, self.found) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct Insufficient<T> { | ||
| /// Value to have at least | ||
| pub required: T, | ||
| /// Value found | ||
| pub actual: T, | ||
| } | ||
|
|
||
| impl<T: Display> Display for Insufficient<T> { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult { | ||
| write!(f, "Required at least {}, found {}", self.required, self.actual) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum Error { | ||
| Runtime(RuntimeError), | ||
| Syntax(SyntaxError), | ||
| } | ||
|
|
||
| impl From<RuntimeError> for Error { | ||
| fn from(error: RuntimeError) -> Error { | ||
| Error::Runtime(error) | ||
| } | ||
| } | ||
|
|
||
| impl From<SyntaxError> for Error { | ||
| fn from(error: SyntaxError) -> Error { | ||
| Error::Syntax(error) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.