1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use std::collections::HashMap;
use super::hash::H256;
use chain;
use super::transaction::RawTransaction;
use miner;

/// Block template as described in:
/// https://github.com/bitcoin/bips/blob/master/bip-0022.mediawiki
/// https://github.com/bitcoin/bips/blob/master/bip-0023.mediawiki
/// https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki#getblocktemplate_changes
/// https://github.com/bitcoin/bips/blob/master/bip-0145.mediawiki
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct BlockTemplate {
	/// The preferred block version
	pub version: u32,
	/// Specific block rules that are to be enforced
	pub rules: Option<Vec<String>>,
	/// Set of pending, supported versionbit (BIP 9) softfork deployments
	/// Keys: named softfork rules
	/// Values: identifies the bit number as indicating acceptance and readiness for given key
	pub vbavailable: Option<HashMap<String, u32>>,
	/// Bit mask of versionbits the server requires set in submissions
	pub vbrequired: Option<u32>,
	/// The hash of previous (best known) block
	pub previousblockhash: H256,
	/// Contents of non-coinbase transactions that should be included in the next block
	pub transactions: Vec<BlockTemplateTransaction>,
	/// Data that should be included in the coinbase's scriptSig content
	/// Keys: ignored
	/// Values: value to be included in scriptSig
	pub coinbaseaux: Option<HashMap<String, String>>,
	/// Maximum allowable input to coinbase transaction, including the generation award and transaction fees (in Satoshis)
	pub coinbasevalue: Option<u64>,
	/// information for coinbase transaction
	pub coinbasetxn: Option<BlockTemplateTransaction>,
	/// The hash target
	pub target: H256,
	/// The minimum timestamp appropriate for next block time in seconds since epoch (Jan 1 1970 GMT)
	pub mintime: Option<i64>,
	/// List of ways the block template may be changed, e.g. 'time', 'transactions', 'prevblock'
	pub mutable: Option<Vec<String>>,
	/// A range of valid nonces (constant 00000000ffffffff)
	pub noncerange: Option<String>,
	/// Limit of sigops in blocks
	pub sigoplimit: Option<u32>,
	/// Limit of block size
	pub sizelimit: Option<u32>,
	/// Limit of block weight
	pub weightlimit: Option<u32>,
	/// Current timestamp in seconds since epoch (Jan 1 1970 GMT)
	pub curtime: u32,
	/// Compressed target of next block
	pub bits: u32,
	/// The height of the next block
	pub height: u32,
}

/// Transaction data as included in `BlockTemplate`
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct BlockTemplateTransaction {
	/// Transaction data encoded in hexadecimal
	pub data: RawTransaction,
	/// Transaction id encoded in little-endian hexadecimal
	pub txid: Option<H256>,
	/// Hash encoded in little-endian hexadecimal (including witness data)
	pub hash: Option<H256>,
	/// Transactions before this one (by 1-based index in 'transactions' list) that must be present in the final block if this one is
	pub depends: Option<Vec<u64>>,
	/// Difference in value between transaction inputs and outputs (in Satoshis).
	/// For coinbase transactions, this is a negative Number of the total collected block fees (ie, not including the block subsidy).
	/// If key is not present, fee is unknown and clients MUST NOT assume there isn't one
	pub fee: Option<i64>,
	/// Total SigOps cost, as counted for purposes of block limits.
	/// If key is not present, sigop cost is unknown and clients MUST NOT assume it is zero.
	pub sigops: Option<i64>,
	/// Total transaction weight, as counted for purposes of block limits.
	pub weight: Option<i64>,
	/// If provided and true, this transaction must be in the final block
	pub required: bool,
}

impl From<miner::BlockTemplate> for BlockTemplate {
	fn from(block: miner::BlockTemplate) -> Self {
		BlockTemplate {
			version: block.version,
			previousblockhash: block.previous_header_hash.reversed().into(),
			curtime: block.time,
			bits: block.bits.into(),
			height: block.height,
			transactions: block.transactions.into_iter().map(Into::into).collect(),
			coinbasevalue: Some(block.coinbase_value),
			sizelimit: Some(block.size_limit),
			sigoplimit: Some(block.sigop_limit),
			..Default::default()
		}
	}
}

impl From<chain::IndexedTransaction> for BlockTemplateTransaction {
	fn from(transaction: chain::IndexedTransaction) -> Self {
		use ser::serialize;
		let serialize = serialize(&transaction.raw);
		BlockTemplateTransaction {
			data: RawTransaction::new(Vec::from((*serialize).clone())),
			..Default::default()
		}
	}
}


#[cfg(test)]
mod tests {
	use serde_json;
	use super::super::hash::H256;
	use super::super::bytes::Bytes;
	use hex::FromHex;
	use super::*;

	#[test]
	fn block_template_transaction_serialize() {
		assert_eq!(serde_json::to_string(&BlockTemplateTransaction {
			data: Bytes("00010203".from_hex().unwrap()),
			txid: None,
			hash: None,
			depends: None,
			fee: None,
			sigops: None,
			weight: None,
			required: false,
		}).unwrap(), r#"{"data":"00010203","txid":null,"hash":null,"depends":null,"fee":null,"sigops":null,"weight":null,"required":false}"#);
		assert_eq!(serde_json::to_string(&BlockTemplateTransaction {
			data: Bytes("00010203".from_hex().unwrap()),
			txid: Some(H256::from(1)),
			hash: Some(H256::from(2)),
			depends: Some(vec![1, 2]),
			fee: Some(100),
			sigops: Some(200),
			weight: Some(300),
			required: true,
		}).unwrap(), r#"{"data":"00010203","txid":"0100000000000000000000000000000000000000000000000000000000000000","hash":"0200000000000000000000000000000000000000000000000000000000000000","depends":[1,2],"fee":100,"sigops":200,"weight":300,"required":true}"#);
	}

	#[test]
	fn block_template_transaction_deserialize() {
		assert_eq!(
			serde_json::from_str::<BlockTemplateTransaction>(r#"{"data":"00010203","txid":null,"hash":null,"depends":null,"fee":null,"sigops":null,"weight":null,"required":false}"#).unwrap(),
			BlockTemplateTransaction {
				data: Bytes("00010203".from_hex().unwrap()),
				txid: None,
				hash: None,
				depends: None,
				fee: None,
				sigops: None,
				weight: None,
				required: false,
			});
		assert_eq!(
			serde_json::from_str::<BlockTemplateTransaction>(r#"{"data":"00010203","txid":"0100000000000000000000000000000000000000000000000000000000000000","hash":"0200000000000000000000000000000000000000000000000000000000000000","depends":[1,2],"fee":100,"sigops":200,"weight":300,"required":true}"#).unwrap(),
			BlockTemplateTransaction {
				data: Bytes("00010203".from_hex().unwrap()),
				txid: Some(H256::from(1)),
				hash: Some(H256::from(2)),
				depends: Some(vec![1, 2]),
				fee: Some(100),
				sigops: Some(200),
				weight: Some(300),
				required: true,
			});
	}

	#[test]
	fn block_template_serialize() {
		assert_eq!(serde_json::to_string(&BlockTemplate {
			version: 0,
			rules: None,
			vbavailable: None,
			vbrequired: None,
			previousblockhash: H256::default(),
			transactions: vec![],
			coinbaseaux: None,
			coinbasevalue: None,
			coinbasetxn: None,
			target: H256::default(),
			mintime: None,
			mutable: None,
			noncerange: None,
			sigoplimit: None,
			sizelimit: None,
			weightlimit: None,
			curtime: 100,
			bits: 200,
			height: 300,
		}).unwrap(), r#"{"version":0,"rules":null,"vbavailable":null,"vbrequired":null,"previousblockhash":"0000000000000000000000000000000000000000000000000000000000000000","transactions":[],"coinbaseaux":null,"coinbasevalue":null,"coinbasetxn":null,"target":"0000000000000000000000000000000000000000000000000000000000000000","mintime":null,"mutable":null,"noncerange":null,"sigoplimit":null,"sizelimit":null,"weightlimit":null,"curtime":100,"bits":200,"height":300}"#);
		assert_eq!(serde_json::to_string(&BlockTemplate {
			version: 0,
			rules: Some(vec!["a".to_owned()]),
			vbavailable: Some(vec![("b".to_owned(), 5)].into_iter().collect()),
			vbrequired: Some(10),
			previousblockhash: H256::from(10),
			transactions: vec![BlockTemplateTransaction {
				data: Bytes("00010203".from_hex().unwrap()),
				txid: None,
				hash: None,
				depends: None,
				fee: None,
				sigops: None,
				weight: None,
				required: false,
			}],
			coinbaseaux: Some(vec![("c".to_owned(), "d".to_owned())].into_iter().collect()),
			coinbasevalue: Some(30),
			coinbasetxn: Some(BlockTemplateTransaction {
				data: Bytes("555555".from_hex().unwrap()),
				txid: Some(H256::from(44)),
				hash: Some(H256::from(55)),
				depends: Some(vec![1]),
				fee: Some(300),
				sigops: Some(400),
				weight: Some(500),
				required: true,
			}),
			target: H256::from(100),
			mintime: Some(7),
			mutable: Some(vec!["afg".to_owned()]),
			noncerange: Some("00000000ffffffff".to_owned()),
			sigoplimit: Some(45),
			sizelimit: Some(449),
			weightlimit: Some(523),
			curtime: 100,
			bits: 200,
			height: 300,
		}).unwrap(), r#"{"version":0,"rules":["a"],"vbavailable":{"b":5},"vbrequired":10,"previousblockhash":"0a00000000000000000000000000000000000000000000000000000000000000","transactions":[{"data":"00010203","txid":null,"hash":null,"depends":null,"fee":null,"sigops":null,"weight":null,"required":false}],"coinbaseaux":{"c":"d"},"coinbasevalue":30,"coinbasetxn":{"data":"555555","txid":"2c00000000000000000000000000000000000000000000000000000000000000","hash":"3700000000000000000000000000000000000000000000000000000000000000","depends":[1],"fee":300,"sigops":400,"weight":500,"required":true},"target":"6400000000000000000000000000000000000000000000000000000000000000","mintime":7,"mutable":["afg"],"noncerange":"00000000ffffffff","sigoplimit":45,"sizelimit":449,"weightlimit":523,"curtime":100,"bits":200,"height":300}"#);
	}

	#[test]
	fn block_template_deserialize() {
		assert_eq!(
			serde_json::from_str::<BlockTemplate>(r#"{"version":0,"rules":null,"vbavailable":null,"vbrequired":null,"previousblockhash":"0000000000000000000000000000000000000000000000000000000000000000","transactions":[],"coinbaseaux":null,"coinbasevalue":null,"coinbasetxn":null,"target":"0000000000000000000000000000000000000000000000000000000000000000","mintime":null,"mutable":null,"noncerange":null,"sigoplimit":null,"sizelimit":null,"weightlimit":null,"curtime":100,"bits":200,"height":300}"#).unwrap(),
			BlockTemplate {
				version: 0,
				rules: None,
				vbavailable: None,
				vbrequired: None,
				previousblockhash: H256::default(),
				transactions: vec![],
				coinbaseaux: None,
				coinbasevalue: None,
				coinbasetxn: None,
				target: H256::default(),
				mintime: None,
				mutable: None,
				noncerange: None,
				sigoplimit: None,
				sizelimit: None,
				weightlimit: None,
				curtime: 100,
				bits: 200,
				height: 300,
			});
		assert_eq!(
			serde_json::from_str::<BlockTemplate>(r#"{"version":0,"rules":["a"],"vbavailable":{"b":5},"vbrequired":10,"previousblockhash":"0a00000000000000000000000000000000000000000000000000000000000000","transactions":[{"data":"00010203","txid":null,"hash":null,"depends":null,"fee":null,"sigops":null,"weight":null,"required":false}],"coinbaseaux":{"c":"d"},"coinbasevalue":30,"coinbasetxn":{"data":"555555","txid":"2c00000000000000000000000000000000000000000000000000000000000000","hash":"3700000000000000000000000000000000000000000000000000000000000000","depends":[1],"fee":300,"sigops":400,"weight":500,"required":true},"target":"6400000000000000000000000000000000000000000000000000000000000000","mintime":7,"mutable":["afg"],"noncerange":"00000000ffffffff","sigoplimit":45,"sizelimit":449,"weightlimit":523,"curtime":100,"bits":200,"height":300}"#).unwrap(),
			BlockTemplate {
				version: 0,
				rules: Some(vec!["a".to_owned()]),
				vbavailable: Some(vec![("b".to_owned(), 5)].into_iter().collect()),
				vbrequired: Some(10),
				previousblockhash: H256::from(10),
				transactions: vec![BlockTemplateTransaction {
					data: Bytes("00010203".from_hex().unwrap()),
					txid: None,
					hash: None,
					depends: None,
					fee: None,
					sigops: None,
					weight: None,
					required: false,
				}],
				coinbaseaux: Some(vec![("c".to_owned(), "d".to_owned())].into_iter().collect()),
				coinbasevalue: Some(30),
				coinbasetxn: Some(BlockTemplateTransaction {
					data: Bytes("555555".from_hex().unwrap()),
					txid: Some(H256::from(44)),
					hash: Some(H256::from(55)),
					depends: Some(vec![1]),
					fee: Some(300),
					sigops: Some(400),
					weight: Some(500),
					required: true,
				}),
				target: H256::from(100),
				mintime: Some(7),
				mutable: Some(vec!["afg".to_owned()]),
				noncerange: Some("00000000ffffffff".to_owned()),
				sigoplimit: Some(45),
				sizelimit: Some(449),
				weightlimit: Some(523),
				curtime: 100,
				bits: 200,
				height: 300,
			});
	}
}