main.rs 3.91 KB
Newer Older
Shawn Tabrizi's avatar
Shawn Tabrizi committed
1
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// This file is part of Polkadot.

// Polkadot 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.

// Polkadot 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 Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! Collator for polkadot

use std::collections::HashMap;
use std::sync::Arc;

use adder::{HeadData as AdderHead, BlockData as AdderBody};
23
use sp_core::Pair;
24
use codec::{Encode, Decode};
asynchronous rob's avatar
asynchronous rob committed
25
use primitives::v0::{
26
	Hash, DownwardMessage,
27
	HeadData, BlockData, Id as ParaId, LocalValidationData, GlobalValidationData,
28
};
29
use collator::{ParachainContext, Network, BuildParachainContext, Cli, SubstrateCli};
30
use parking_lot::Mutex;
31
use futures::future::{Ready, ready, FutureExt};
32
33
34
35

const GENESIS: AdderHead = AdderHead {
	number: 0,
	parent_hash: [0; 32],
36
37
38
39
	post_state: [
		1, 27, 77, 3, 221, 140, 1, 241, 4, 145, 67, 207, 156, 76, 129, 126, 75,
		22, 127, 29, 27, 131, 229, 198, 240, 241, 13, 137, 186, 30, 123, 206
	],
40
41
42
43
44
45
46
47
48
49
};

const GENESIS_BODY: AdderBody = AdderBody {
	state: 0,
	add: 0,
};

#[derive(Clone)]
struct AdderContext {
	db: Arc<Mutex<HashMap<AdderHead, AdderBody>>>,
50
51
	/// We store it here to make sure that our interfaces require the correct bounds.
	_network: Option<Arc<dyn Network>>,
52
53
54
55
}

/// The parachain context.
impl ParachainContext for AdderContext {
56
	type ProduceCandidate = Ready<Option<(BlockData, HeadData)>>;
57

Ashley's avatar
Ashley committed
58
	fn produce_candidate(
59
		&mut self,
60
		_relay_parent: Hash,
61
		_global_validation: GlobalValidationData,
62
		local_validation: LocalValidationData,
63
		_: Vec<DownwardMessage>,
64
	) -> Self::ProduceCandidate
65
	{
66
67
68
		let adder_head = match AdderHead::decode(&mut &local_validation.parent_head.0[..]).ok() {
			Some(res) => res,
			None => return ready(None),
69
		};
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

		let mut db = self.db.lock();

		let last_body = if adder_head == GENESIS {
			GENESIS_BODY
		} else {
			db.get(&adder_head)
				.expect("All past bodies stored since this is the only collator")
				.clone()
		};

		let next_body = AdderBody {
			state: last_body.state.overflowing_add(last_body.add).0,
			add: adder_head.number % 100,
		};

86
		let next_head = adder::execute(adder_head.hash(), adder_head, &next_body)
87
88
89
90
91
92
93
94
95
			.expect("good execution params; qed");

		let encoded_head = HeadData(next_head.encode());
		let encoded_body = BlockData(next_body.encode());

		println!("Created collation for #{}, post-state={}",
			next_head.number, next_body.state.overflowing_add(next_body.add).0);

		db.insert(next_head.clone(), next_body);
96
		ready(Some((encoded_body, encoded_head)))
97
98
99
	}
}

100
101
102
impl BuildParachainContext for AdderContext {
	type ParachainContext = Self;

Seun Lanlege's avatar
Seun Lanlege committed
103
	fn build<Client, SP, Extrinsic>(
104
		self,
Seun Lanlege's avatar
Seun Lanlege committed
105
		_: Arc<Client>,
106
		_: SP,
107
		network: impl Network + Clone + 'static,
108
	) -> Result<Self::ParachainContext, ()> {
109
		Ok(Self { _network: Some(Arc::new(network)), ..self })
110
111
112
	}
}

113
fn main() -> Result<(), Box<dyn std::error::Error>> {
114
	let key = Arc::new(Pair::from_seed(&[1; 32]));
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
	let id: ParaId = 100.into();

	println!("Starting adder collator with genesis: ");

	{
		let encoded = GENESIS.encode();
		println!("Dec: {:?}", encoded);
		print!("Hex: 0x");
		for byte in encoded {
			print!("{:02x}", byte);
		}

		println!();
	}

	let context = AdderContext {
		db: Arc::new(Mutex::new(HashMap::new())),
132
		_network: None,
133
134
	};

135
136
137
	let cli = Cli::from_iter(&["-dev"]);
	let runner = cli.create_runner(&cli.run.base)?;
	runner.async_run(|config| {
138
		let (future, task_manager) = collator::start_collator(
139
140
141
142
			context,
			id,
			key,
			config,
143
144
145
		)?;

		Ok((future.map(Ok), task_manager))
146
147
148
	})?;

	Ok(())
149
}