Skip to content

Commit

Permalink
First commit Alpha Testnet v.0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
inodelisia committed Jan 19, 2021
1 parent 731d6d1 commit d71439d
Show file tree
Hide file tree
Showing 196 changed files with 30,547 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**/target
**/*.rs.bk
/keys
/data*
/shell.nix
85 changes: 85 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[package]
name = "betelgeuse"
version = "0.0.4"
authors = ["MVS Core DEV Team"]
license = "GPL-3.0-or-later"
build = "build.rs"
edition = "2018"

[[bin]]
name = "betelgeuse"
path = "src/main.rs"

[dependencies]
futures = "0.3"
log = "0.4"
structopt = "0.3"
codec = { package = "parity-scale-codec", version = "1.0.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
jsonrpc-core = "15.0.0"
parking_lot = "0.10.0"

sc-cli = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-rpc = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-keystore = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-keystore = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-core = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-api = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-blockchain = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-block-builder = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-rpc-api = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-timestamp = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-executor = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-service = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-inherents = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-transaction-pool = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-transaction-pool = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-network = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-consensus-pow = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-consensus = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-consensus = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-finality-grandpa = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-finality-grandpa = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-client-api = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sp-runtime = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
sc-basic-authorship = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
substrate-frame-rpc-system = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
pallet-transaction-payment-rpc = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }

pallet-grandpa = { version = "2.0.0", git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
pallet-session = { version = "2.0.0", git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
pallet-authorship = { version = "2.0.0", git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }

pallet-rewards = { path = "frame/rewards" }
pallet-eras = { path = "frame/eras" }
betelgeuse-pow = { path = "pow" }
betelgeuse-runtime = { path = "runtime" }
betelgeuse-primitives = { path = "primitives" }

# benchmarking
frame-benchmarking = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }
frame-benchmarking-cli = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }

[build-dependencies]
substrate-build-script-utils = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b" }

[features]
default = []
wasmtime = [
"sc-executor/wasmtime",
"sc-cli/wasmtime",
"sc-service/wasmtime",
]
runtime-benchmarks = [
"wasmtime",
"betelgeuse-runtime/runtime-benchmarks",
]

[workspace]
members = [
"runtime",
"pow",
"frame/rewards",
"frame/eras",
]
79 changes: 79 additions & 0 deletions betelgeuse.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use substrate_build_script_utils::{generate_cargo_keys, rerun_if_git_head_changed};

fn main() {
generate_cargo_keys();

rerun_if_git_head_changed();
}
35 changes: 35 additions & 0 deletions frame/difficulty/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "pallet-difficulty"
version = "2.4.0"
authors = ["Wei Tang <wei@that.world>"]
license = "GPL-3.0-or-later"
edition = "2018"
description = "Difficulty adjustment module"

[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = ["derive"] }
sp-std = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
sp-core = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
sp-runtime = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
sp-timestamp = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
frame-system = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
frame-support = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
pallet-timestamp = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }

betelgeuse-primitives = { path = "../../primitives", default-features = false }

[features]
default = ["std"]
std = [
"serde",
"codec/std",
"sp-std/std",
"sp-core/std",
"sp-runtime/std",
"sp-timestamp/std",
"frame-system/std",
"frame-support/std",
"pallet-timestamp/std",
"betelgeuse-primitives/std",
]
137 changes: 137 additions & 0 deletions frame/difficulty/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// This file is part of Betelgeuse.
//
// Copyright (c) 2019-2020 Wei Tang.
//
// Betelgeuse is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Betelgeuse 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Betelgeuse. If not, see <http://www.gnu.org/licenses/>.

//! Difficulty adjustment module.
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Encode, Decode};
use sp_std::cmp::{min, max};
use sp_core::U256;
use sp_runtime::traits::UniqueSaturatedInto;
use sp_timestamp::OnTimestampSet;
use frame_support::{decl_storage, decl_module, traits::Get};
use betelgeuse_primitives::{
DIFFICULTY_ADJUST_WINDOW, DIFFICULTY_DAMP_FACTOR, CLAMP_FACTOR,
MIN_DIFFICULTY, MAX_DIFFICULTY, Difficulty,
};

#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, Debug)]
pub struct DifficultyAndTimestamp<M> {
pub difficulty: Difficulty,
pub timestamp: M,
}

/// Move value linearly toward a goal
pub fn damp(actual: u128, goal: u128, damp_factor: u128) -> u128 {
(actual + (damp_factor - 1) * goal) / damp_factor
}

/// limit value to be within some factor from a goal
pub fn clamp(actual: u128, goal: u128, clamp_factor: u128) -> u128 {
max(goal / clamp_factor, min(actual, goal * clamp_factor))
}

pub trait Config: pallet_timestamp::Config {
/// Target block time in millseconds.
type TargetBlockTime: Get<Self::Moment>;
}

decl_storage! {
trait Store for Module<T: Config> as Difficulty {
/// Past difficulties and timestamps, from earliest to latest.
PastDifficultiesAndTimestamps:
[Option<DifficultyAndTimestamp<T::Moment>>; 60]
= [None; DIFFICULTY_ADJUST_WINDOW as usize];
/// Current difficulty.
pub CurrentDifficulty get(fn difficulty) build(|config: &GenesisConfig| {
config.initial_difficulty
}): Difficulty;
/// Initial difficulty.
pub InitialDifficulty config(initial_difficulty): Difficulty;
}
}

decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
/// Target block time in milliseconds.
const TargetBlockTime: T::Moment = T::TargetBlockTime::get();
}
}

impl<T: Config> OnTimestampSet<T::Moment> for Module<T> {
fn on_timestamp_set(now: T::Moment) {
let block_time = UniqueSaturatedInto::<u128>::unique_saturated_into(T::TargetBlockTime::get());
let block_time_window = DIFFICULTY_ADJUST_WINDOW as u128 * block_time;

let mut data = PastDifficultiesAndTimestamps::<T>::get();

for i in 1..data.len() {
data[i - 1] = data[i];
}

data[data.len() - 1] = Some(DifficultyAndTimestamp {
timestamp: now,
difficulty: Self::difficulty(),
});

let mut ts_delta = 0;
for i in 1..(DIFFICULTY_ADJUST_WINDOW as usize) {
let prev: Option<u128> = data[i - 1].map(|d| d.timestamp.unique_saturated_into());
let cur: Option<u128> = data[i].map(|d| d.timestamp.unique_saturated_into());

let delta = match (prev, cur) {
(Some(prev), Some(cur)) => cur.saturating_sub(prev),
_ => block_time.into(),
};
ts_delta += delta;
}

if ts_delta == 0 {
ts_delta = 1;
}

let mut diff_sum = U256::zero();
for i in 0..(DIFFICULTY_ADJUST_WINDOW as usize) {
let diff = match data[i].map(|d| d.difficulty) {
Some(diff) => diff,
None => InitialDifficulty::get(),
};
diff_sum += diff;
}

if diff_sum < U256::from(MIN_DIFFICULTY) {
diff_sum = U256::from(MIN_DIFFICULTY);
}

// adjust time delta toward goal subject to dampening and clamping
let adj_ts = clamp(
damp(ts_delta, block_time_window, DIFFICULTY_DAMP_FACTOR),
block_time_window,
CLAMP_FACTOR,
);

// minimum difficulty avoids getting stuck due to dampening
let difficulty = min(U256::from(MAX_DIFFICULTY),
max(U256::from(MIN_DIFFICULTY),
diff_sum * U256::from(block_time) / U256::from(adj_ts)));

<PastDifficultiesAndTimestamps<T>>::put(data);
<CurrentDifficulty>::put(difficulty);
}
}
24 changes: 24 additions & 0 deletions frame/eras/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "pallet-eras"
version = "2.4.0"
authors = ["Wei Tang <wei@that.world>"]
license = "GPL-3.0-or-later"
edition = "2018"
description = "Era information recording for later use."

[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = ["derive"] }
sp-std = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
frame-system = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
frame-support = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }

[features]
default = ["std"]
std = [
"serde",
"codec/std",
"sp-std/std",
"frame-system/std",
"frame-support/std",
]
51 changes: 51 additions & 0 deletions frame/eras/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// This file is part of Betelgeuse.
//
// Copyright (c) 2020 Wei Tang.
//
// Betelgeuse is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Betelgeuse 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Betelgeuse. If not, see <http://www.gnu.org/licenses/>.

//! Era information recording.
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Encode, Decode};
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
use sp_std::prelude::*;
use frame_support::{decl_storage, decl_module};

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode)]
pub struct Era<H> {
/// Genesis block hash of the era.
pub genesis_block_hash: H,
/// Final block hash.
pub final_block_hash: H,
/// Final state root.
pub final_state_root: H,
}

pub trait Config: frame_system::Config { }

decl_storage! {
trait Store for Module<T: Config> as Eras {
/// Past eras.
pub PastEras get(fn past_eras) config(past_eras): Vec<Era<T::Hash>>;
}
}

decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin { }
}
41 changes: 41 additions & 0 deletions frame/rewards/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
name = "pallet-rewards"
version = "2.4.0"
authors = ["Wei Tang <wei@that.world>"]
license = "GPL-3.0-or-later"
edition = "2018"

[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
sp-runtime = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
sp-std = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
sp-inherents = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
sp-consensus-pow = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
frame-support = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
frame-system = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }

# Benchmarking
frame-benchmarking = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false, optional = true }

[dev-dependencies]
sp-core = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
sp-io = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }
pallet-balances = { git = "https://github.com/Sarmale-Team/substrate", rev = "62e252b", default-features = false }

[features]
default = ["std"]
std = [
"serde",
"codec/std",
"sp-runtime/std",
"sp-std/std",
"sp-inherents/std",
"sp-consensus-pow/std",
"frame-support/std",
"frame-system/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
]
Loading

0 comments on commit d71439d

Please sign in to comment.