Skip to content

Commit 0647b3b

Browse files
authored
Fixes and improvements for PoC-1 Testnet (paritytech#143)
* Fix initialisations and add a test. * Fix test. * Fix overflow bug. * Minor refactoring and fixes. * Fix vote threshold. * Add note. * Fixes for latest rust and the readme. * Better readme. * An extra validator for PoC-1 * Update README. * PoC-1 bootnodes. * don't return async::notready for messages without scheduling wakeup * Fix endowed account * give polkadot control over round proposer based on random seed * address grumbles.
1 parent 6196241 commit 0647b3b

File tree

27 files changed

+307
-59
lines changed

27 files changed

+307
-59
lines changed

README.md

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,104 @@
11
# Polkadot
22

3-
Implementation of a https://polkadot.io node in Rust.
3+
Implementation of a https://polkadot.io node in Rust.
4+
5+
## To play
6+
7+
If you'd like to play with Polkadot, you'll need to install a client like this
8+
one. First, get Rust and the support software if you don't already have it:
9+
10+
```
11+
curl https://sh.rustup.rs -sSf | sh
12+
sudo apt install make clang
13+
```
14+
15+
Then, install Polkadot PoC-1:
16+
17+
```
18+
cargo install --git https://github.com/paritytech/polkadot.git --branch v0.1.0
19+
```
20+
21+
You'll now have a `polkadot` binary installed to your `PATH`. You can drop the
22+
`--branch v0.1.0` to get the very latest version of Polkadot, but these
23+
instructions might not work in that case.
24+
25+
### Development
26+
27+
You can run a simple single-node development "network" on your machine by
28+
running in a terminal:
29+
30+
```
31+
polkadot --chain=dev --validator --key Alice
32+
```
33+
34+
You can muck around by cloning and building the http://github.com/paritytech/polka-ui and http://github.com/paritytech/polkadot-ui or just heading to https://polkadot.js.org/apps.
35+
36+
### PoC-1 Testnet
37+
38+
You can also connect to the global PoC-1 testnet. To do this, just use:
39+
40+
```
41+
polkadot --chain=poc-1
42+
```
43+
44+
If you want to do anything on it (not that there's much to do), then you'll need
45+
to get some PoC-1 testnet DOTs. Ask in the Polkadot watercooler.
46+
47+
## Local Two-node Testnet
48+
49+
If you want to see the multi-node consensus algorithm in action locally, then
50+
you can create a local testnet. You'll need two terminals open. In one, run:
51+
52+
```
53+
polkadot --chain=dev --validator --key Alice -d /tmp/alice
54+
```
55+
56+
and in the other, run:
57+
58+
```
59+
polkadot --chain=dev --validator --key Bob -d /tmp/bob --port 30334 --bootnodes 'enode://ALICE_BOOTNODE_ID_HERE@127.0.0.1:30333'
60+
```
61+
62+
Ensure you replace `ALICE_BOOTNODE_ID_HERE` with the node ID from the output of
63+
the first terminal.
64+
65+
## Hacking on Polkadot
66+
67+
If you'd actually like hack on Polkadot, you can just grab the source code and
68+
build it. Ensure you have Rust and the support software installed:
69+
70+
```
71+
curl https://sh.rustup.rs -sSf | sh
72+
rustup update nightly
73+
rustup target add wasm32-unknown-unknown --toolchain nightly
74+
rustup update stable
75+
cargo install --git https://github.com/alexcrichton/wasm-gc
76+
cargo install --git https://github.com/pepyakin/wasm-export-table.git
77+
sudo apt install make clang
78+
```
79+
80+
Then, grab the Polkadot source code:
81+
82+
```
83+
git clone https://github.com/paritytech/polkadot.git
84+
cd polkadot
85+
```
86+
87+
Then build the code:
88+
89+
```
90+
./build.sh # Builds the WebAssembly binaries
91+
cargo build # Builds all native code
92+
```
93+
94+
You can run the tests if you like:
95+
96+
```
97+
cargo test --all
98+
```
99+
100+
You can start a development chain with:
101+
102+
```
103+
cargo run -- --chain=dev --validator --key Alice
104+
```

demo/executor/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ mod tests {
197197
construct_block(
198198
1,
199199
[69u8; 32].into(),
200-
hex!("57ba67304318efaee95c4f9ab95ed5704eafe030bc8db2df00acb08c2f4979c8").into(),
200+
hex!("a63d59c6a7347cd7a1dc1ec139723b531f0ac450e39b1c532d5ca69ff74ad811").into(),
201201
vec![Extrinsic {
202202
signed: Alice.into(),
203203
index: 0,
@@ -210,7 +210,7 @@ mod tests {
210210
construct_block(
211211
2,
212212
block1().1,
213-
hex!("ead4c60c0cad06b7ee73e64efeec2d4eb82c651469fb2ec748cfe5026bea5c49").into(),
213+
hex!("1c3623b2e3f7e43752debb9015bace4f6931593579b5af34457b931315f5e2ab").into(),
214214
vec![
215215
Extrinsic {
216216
signed: Bob.into(),
Binary file not shown.
Binary file not shown.

polkadot/api/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ extern crate substrate_state_machine as state_machine;
3131
#[macro_use]
3232
extern crate error_chain;
3333

34-
#[macro_use]
35-
extern crate log;
36-
3734
#[cfg(test)]
3835
extern crate substrate_keyring as keyring;
3936

@@ -42,7 +39,7 @@ use client::Client;
4239
use polkadot_executor::Executor as LocalDispatch;
4340
use substrate_executor::{NativeExecutionDispatch, NativeExecutor};
4441
use state_machine::OverlayedChanges;
45-
use primitives::{AccountId, BlockId, Index, SessionKey, Timestamp};
42+
use primitives::{AccountId, BlockId, Hash, Index, SessionKey, Timestamp};
4643
use primitives::parachain::DutyRoster;
4744
use runtime::{Block, Header, UncheckedExtrinsic, Extrinsic, Call, TimestampCall};
4845

@@ -126,6 +123,9 @@ pub trait PolkadotApi {
126123
/// Get validators at a given block.
127124
fn validators(&self, at: &Self::CheckedBlockId) -> Result<Vec<AccountId>>;
128125

126+
/// Get the value of the randomness beacon at a given block.
127+
fn random_seed(&self, at: &Self::CheckedBlockId) -> Result<Hash>;
128+
129129
/// Get the authority duty roster at a block.
130130
fn duty_roster(&self, at: &Self::CheckedBlockId) -> Result<DutyRoster>;
131131

@@ -191,6 +191,10 @@ impl<B: Backend> PolkadotApi for Client<B, NativeExecutor<LocalDispatch>>
191191
with_runtime!(self, at, ::runtime::Session::validators)
192192
}
193193

194+
fn random_seed(&self, at: &CheckedId) -> Result<Hash> {
195+
with_runtime!(self, at, ::runtime::System::random_seed)
196+
}
197+
194198
fn duty_roster(&self, at: &CheckedId) -> Result<DutyRoster> {
195199
// duty roster can only be queried at the start of a block,
196200
// so we create a dummy.

polkadot/cli/src/cli.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ args:
5656
- chain:
5757
long: chain
5858
value_name: CHAIN_SPEC
59-
help: Specify the chain specification (one of dev or poc-1)
59+
help: Specify the chain specification (one of dev, local or poc-1)
6060
takes_value: true
6161
subcommands:

polkadot/cli/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,15 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
126126
}
127127

128128
match matches.value_of("chain") {
129-
Some("poc-1") => config.chain_spec = ChainSpec::PoC1Testnet,
130129
Some("dev") => config.chain_spec = ChainSpec::Development,
130+
Some("local") => config.chain_spec = ChainSpec::LocalTestnet,
131+
Some("poc-1") => config.chain_spec = ChainSpec::PoC1Testnet,
131132
None => (),
132133
Some(unknown) => panic!("Invalid chain name: {}", unknown),
133134
}
134135
info!("Chain specification: {}", match config.chain_spec {
135-
ChainSpec::Development => "Local Development",
136+
ChainSpec::Development => "Development",
137+
ChainSpec::LocalTestnet => "Local Testnet",
136138
ChainSpec::PoC1Testnet => "PoC-1 Testnet",
137139
});
138140

polkadot/consensus/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ impl<C: PolkadotApi, N: Network> bft::ProposerFactory for ProposerFactory<C, N>
495495

496496
let checked_id = self.client.check_id(BlockId::Hash(parent_hash))?;
497497
let duty_roster = self.client.duty_roster(&checked_id)?;
498+
let random_seed = self.client.random_seed(&checked_id)?;
498499

499500
let group_info = make_group_info(duty_roster, authorities)?;
500501
let table = Arc::new(SharedTable::new(group_info, sign_with.clone(), parent_hash));
@@ -510,6 +511,7 @@ impl<C: PolkadotApi, N: Network> bft::ProposerFactory for ProposerFactory<C, N>
510511
parent_hash,
511512
parent_number: parent_header.number,
512513
parent_id: checked_id,
514+
random_seed,
513515
local_key: sign_with,
514516
client: self.client.clone(),
515517
transaction_pool: self.transaction_pool.clone(),
@@ -533,6 +535,7 @@ pub struct Proposer<C: PolkadotApi, R> {
533535
parent_hash: HeaderHash,
534536
parent_number: BlockNumber,
535537
parent_id: C::CheckedBlockId,
538+
random_seed: Hash,
536539
client: Arc<C>,
537540
local_key: Arc<ed25519::Pair>,
538541
transaction_pool: Arc<Mutex<TransactionPool>>,
@@ -561,6 +564,7 @@ impl<C: PolkadotApi, R: TableRouter> bft::Proposer for Proposer<C, R> {
561564
let mut pool = self.transaction_pool.lock();
562565
let mut unqueue_invalid = Vec::new();
563566
let mut pending_size = 0;
567+
pool.cull(None, readiness_evaluator.clone());
564568
for pending in pool.pending(readiness_evaluator.clone()) {
565569
// skip and cull transactions which are too large.
566570
if pending.encoded_size() > MAX_TRANSACTIONS_SIZE {
@@ -624,6 +628,16 @@ impl<C: PolkadotApi, R: TableRouter> bft::Proposer for Proposer<C, R> {
624628
Box::new(self.delay.clone().map_err(Error::from).and_then(move |_| evaluated))
625629
}
626630

631+
fn round_proposer(&self, round_number: usize, authorities: &[AuthorityId]) -> AuthorityId {
632+
use primitives::uint::U256;
633+
634+
let len: U256 = authorities.len().into();
635+
let offset = U256::from_big_endian(&self.random_seed.0) % len;
636+
let offset = offset.low_u64() as usize + round_number;
637+
638+
authorities[offset % authorities.len()].clone()
639+
}
640+
627641
fn import_misbehavior(&self, misbehavior: Vec<(AuthorityId, bft::Misbehavior)>) {
628642
use bft::generic::Misbehavior as GenericMisbehavior;
629643
use primitives::bft::{MisbehaviorKind, MisbehaviorReport};
@@ -634,6 +648,7 @@ impl<C: PolkadotApi, R: TableRouter> bft::Proposer for Proposer<C, R> {
634648
let mut next_index = {
635649
let readiness_evaluator = Ready::create(self.parent_id.clone(), &*self.client);
636650

651+
pool.cull(None, readiness_evaluator.clone());
637652
let cur_index = pool.pending(readiness_evaluator)
638653
.filter(|tx| tx.as_ref().as_ref().signed == local_id)
639654
.last()

polkadot/runtime/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ extern crate substrate_runtime_support as runtime_support;
2727
#[macro_use]
2828
extern crate substrate_runtime_primitives as runtime_primitives;
2929

30+
#[cfg(feature = "std")]
31+
#[macro_use]
32+
extern crate hex_literal;
33+
3034
#[cfg(test)]
3135
extern crate substrate_serializer;
3236

@@ -298,4 +302,24 @@ mod tests {
298302
println!("{}", HexDisplay::from(&v));
299303
assert_eq!(UncheckedExtrinsic::decode(&mut &v[..]).unwrap(), tx);
300304
}
305+
306+
#[test]
307+
fn serialize_checked() {
308+
let xt = Extrinsic {
309+
signed: hex!["0d71d1a9cad6f2ab773435a7dec1bac019994d05d1dd5eb3108211dcf25c9d1e"],
310+
index: 0u64,
311+
function: Call::CouncilVoting(council::voting::Call::propose(Box::new(
312+
PrivCall::Consensus(consensus::PrivCall::set_code(
313+
vec![]
314+
))
315+
))),
316+
};
317+
let v = Slicable::encode(&xt);
318+
319+
let data = hex!["e00000000d71d1a9cad6f2ab773435a7dec1bac019994d05d1dd5eb3108211dcf25c9d1e000000000000000007000000000000006369D39D892B7B87A6769F90E14C618C2B84EBB293E2CC46640136E112C078C75619AC2E0815F2511568736623C055156C8FC427CE2AEE4AE2838F86EFE80208"];
320+
let uxt: UncheckedExtrinsic = Slicable::decode(&mut &data[..]).unwrap();
321+
assert_eq!(uxt.extrinsic, xt);
322+
323+
assert_eq!(Extrinsic::decode(&mut &v[..]).unwrap(), xt);
324+
}
301325
}

polkadot/runtime/wasm/genesis.wasm

35.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)