Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ rustc-serialize = "0.3"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
staking = { path = "basic_module/staking" }
tokio-core = "0.1.17"
toml = "0.4"
cidr = "0.0.4"
Expand Down Expand Up @@ -81,5 +82,6 @@ members = [
"module-macros",
"coordinator",
"basesandbox",
"basic_module/account"
"basic_module/account",
"basic_module/staking"
]
16 changes: 16 additions & 0 deletions basic_module/staking/Cargo.toml
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" }
35 changes: 35 additions & 0 deletions basic_module/staking/src/check.rs
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(())
}
}
46 changes: 46 additions & 0 deletions basic_module/staking/src/core.rs
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>;
}
66 changes: 66 additions & 0 deletions basic_module/staking/src/error.rs
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)
}
}
Loading