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
// Copyright 2018 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Compact Blocks.

use rand::{thread_rng, Rng};

use crate::core::block::{Block, BlockHeader, Error};
use crate::core::hash::{DefaultHashable, Hashed};
use crate::core::id::ShortIdentifiable;
use crate::core::{Output, ShortId, TxKernel};
use crate::ser::{self, read_multi, Readable, Reader, VerifySortedAndUnique, Writeable, Writer};

/// Container for full (full) outputs and kernels and kern_ids for a compact block.
#[derive(Debug, Clone)]
pub struct CompactBlockBody {
	/// List of full outputs - specifically the coinbase output(s)
	pub out_full: Vec<Output>,
	/// List of full kernels - specifically the coinbase kernel(s)
	pub kern_full: Vec<TxKernel>,
	/// List of transaction kernels, excluding those in the full list
	/// (short_ids)
	pub kern_ids: Vec<ShortId>,
}

impl CompactBlockBody {
	fn init(
		out_full: Vec<Output>,
		kern_full: Vec<TxKernel>,
		kern_ids: Vec<ShortId>,
		verify_sorted: bool,
	) -> Result<Self, Error> {
		let body = CompactBlockBody {
			out_full,
			kern_full,
			kern_ids,
		};

		if verify_sorted {
			// If we are verifying sort order then verify and
			// return an error if not sorted lexicographically.
			body.verify_sorted()?;
			Ok(body)
		} else {
			// If we are not verifying sort order then sort in place and return.
			let mut body = body;
			body.sort();
			Ok(body)
		}
	}

	/// Sort everything.
	fn sort(&mut self) {
		self.out_full.sort_unstable();
		self.kern_full.sort_unstable();
		self.kern_ids.sort_unstable();
	}

	/// "Lightweight" validation.
	fn validate_read(&self) -> Result<(), Error> {
		self.verify_sorted()?;
		Ok(())
	}

	// Verify everything is sorted in lexicographical order and no duplicates present.
	fn verify_sorted(&self) -> Result<(), Error> {
		self.out_full.verify_sorted_and_unique()?;
		self.kern_full.verify_sorted_and_unique()?;
		self.kern_ids.verify_sorted_and_unique()?;
		Ok(())
	}
}

impl Readable for CompactBlockBody {
	fn read(reader: &mut dyn Reader) -> Result<CompactBlockBody, ser::Error> {
		let (out_full_len, kern_full_len, kern_id_len) =
			ser_multiread!(reader, read_u64, read_u64, read_u64);

		let out_full = read_multi(reader, out_full_len)?;
		let kern_full = read_multi(reader, kern_full_len)?;
		let kern_ids = read_multi(reader, kern_id_len)?;

		// Initialize compact block body, verifying sort order.
		let body = CompactBlockBody::init(out_full, kern_full, kern_ids, true)
			.map_err(|_| ser::Error::CorruptedData)?;

		Ok(body)
	}
}

impl Writeable for CompactBlockBody {
	fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
		ser_multiwrite!(
			writer,
			[write_u64, self.out_full.len() as u64],
			[write_u64, self.kern_full.len() as u64],
			[write_u64, self.kern_ids.len() as u64]
		);

		self.out_full.write(writer)?;
		self.kern_full.write(writer)?;
		self.kern_ids.write(writer)?;

		Ok(())
	}
}

impl Into<CompactBlockBody> for CompactBlock {
	fn into(self) -> CompactBlockBody {
		self.body
	}
}

/// Compact representation of a full block.
/// Each input/output/kernel is represented as a short_id.
/// A node is reasonably likely to have already seen all tx data (tx broadcast
/// before block) and can go request missing tx data from peers if necessary to
/// hydrate a compact block into a full block.
#[derive(Debug, Clone)]
pub struct CompactBlock {
	/// The header with metadata and commitments to the rest of the data
	pub header: BlockHeader,
	/// Nonce for connection specific short_ids
	pub nonce: u64,
	/// Container for out_full, kern_full and kern_ids in the compact block.
	body: CompactBlockBody,
}

impl DefaultHashable for CompactBlock {}

impl CompactBlock {
	/// "Lightweight" validation.
	fn validate_read(&self) -> Result<(), Error> {
		self.body.validate_read()?;
		Ok(())
	}

	/// Get kern_ids
	pub fn kern_ids(&self) -> &Vec<ShortId> {
		&self.body.kern_ids
	}

	/// Get full (coinbase) kernels
	pub fn kern_full(&self) -> &Vec<TxKernel> {
		&self.body.kern_full
	}

	/// Get full (coinbase) outputs
	pub fn out_full(&self) -> &Vec<Output> {
		&self.body.out_full
	}
}

impl From<Block> for CompactBlock {
	fn from(block: Block) -> Self {
		let header = block.header.clone();
		let nonce = thread_rng().gen();

		let out_full = block
			.outputs()
			.iter()
			.filter(|x| x.is_coinbase())
			.cloned()
			.collect::<Vec<_>>();

		let mut kern_full = vec![];
		let mut kern_ids = vec![];

		for k in block.kernels() {
			if k.is_coinbase() {
				kern_full.push(k.clone());
			} else {
				kern_ids.push(k.short_id(&header.hash(), nonce));
			}
		}

		// Initialize a compact block body and sort everything.
		let body = CompactBlockBody::init(out_full, kern_full, kern_ids, false)
			.expect("sorting, not verifying");

		CompactBlock {
			header,
			nonce,
			body,
		}
	}
}

/// Implementation of Writeable for a compact block, defines how to write the
/// block to a binary writer. Differentiates between writing the block for the
/// purpose of full serialization and the one of just extracting a hash.
impl Writeable for CompactBlock {
	fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
		self.header.write(writer)?;

		if writer.serialization_mode() != ser::SerializationMode::Hash {
			writer.write_u64(self.nonce)?;
			self.body.write(writer)?;
		}

		Ok(())
	}
}

/// Implementation of Readable for a compact block, defines how to read a
/// compact block from a binary stream.
impl Readable for CompactBlock {
	fn read(reader: &mut dyn Reader) -> Result<CompactBlock, ser::Error> {
		let header = BlockHeader::read(reader)?;
		let nonce = reader.read_u64()?;
		let body = CompactBlockBody::read(reader)?;

		let cb = CompactBlock {
			header,
			nonce,
			body,
		};

		// Now validate the compact block and treat any validation error as corrupted data.
		cb.validate_read().map_err(|_| ser::Error::CorruptedData)?;

		Ok(cb)
	}
}