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
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of Tetsy Vapory.

// Tetsy Vapory 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.

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

//! Blockchain DB extras.

use std::io::Write;
use std::convert::AsRef;

use common_types::BlockNumber;
use common_types::engines::epoch::Transition as EpochTransition;
use common_types::receipt::Receipt;
use vapory_types::{H256, H264, U256};
use tetsy_util_mem::MallocSizeOf;
use tetsy_kvdb::PREFIX_LEN as DB_PREFIX_LEN;
use tetsy_rlp;
use tetsy_rlp_derive::{RlpEncodableWrapper, RlpDecodableWrapper, RlpEncodable, RlpDecodable};

use crate::db::Key;

/// Represents index of extra data in database
#[derive(Copy, Debug, Hash, Eq, PartialEq, Clone)]
pub enum ExtrasIndex {
	/// Block details index
	BlockDetails = 0,
	/// Block hash index
	BlockHash = 1,
	/// Transaction address index
	TransactionAddress = 2,
	/// Block receipts index
	BlockReceipts = 4,
	/// Epoch transition data index.
	EpochTransitions = 5,
	/// Pending epoch transition data index.
	PendingEpochTransition = 6,
}

fn with_index(hash: &H256, i: ExtrasIndex) -> H264 {
	let mut result = H264::default();
	result.as_bytes_mut()[0] = i as u8;
	result.as_bytes_mut()[1..].clone_from_slice(hash.as_bytes());
	result
}

/// Wrapper for block number used as a DB key.
pub struct BlockNumberKey([u8; 5]);

impl AsRef<[u8]> for BlockNumberKey {
	fn as_ref(&self) -> &[u8] {
		&self.0[..]
	}
}

impl Key<H256> for BlockNumber {
	type Target = BlockNumberKey;

	fn key(&self) -> Self::Target {
		let mut result = [0u8; 5];
		result[0] = ExtrasIndex::BlockHash as u8;
		result[1] = (self >> 24) as u8;
		result[2] = (self >> 16) as u8;
		result[3] = (self >> 8) as u8;
		result[4] = *self as u8;
		BlockNumberKey(result)
	}
}

impl Key<BlockDetails> for H256 {
	type Target = H264;

	fn key(&self) -> H264 {
		with_index(self, ExtrasIndex::BlockDetails)
	}
}

impl Key<TransactionAddress> for H256 {
	type Target = H264;

	fn key(&self) -> H264 {
		with_index(self, ExtrasIndex::TransactionAddress)
	}
}

impl Key<BlockReceipts> for H256 {
	type Target = H264;

	fn key(&self) -> H264 {
		with_index(self, ExtrasIndex::BlockReceipts)
	}
}

impl Key<common_types::engines::epoch::PendingTransition> for H256 {
	type Target = H264;

	fn key(&self) -> H264 {
		with_index(self, ExtrasIndex::PendingEpochTransition)
	}
}

/// length of epoch keys.
pub const EPOCH_KEY_LEN: usize = DB_PREFIX_LEN + 16;

/// epoch key prefix.
/// used to iterate over all epoch transitions in order from genesis.
pub const EPOCH_KEY_PREFIX: &'static [u8; DB_PREFIX_LEN] = &[
	ExtrasIndex::EpochTransitions as u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

/// Epoch transitions key
pub struct EpochTransitionsKey([u8; EPOCH_KEY_LEN]);

impl AsRef<[u8]> for EpochTransitionsKey {
	fn as_ref(&self) -> &[u8] { &self.0[..] }
}

impl Key<EpochTransitions> for u64 {
	type Target = EpochTransitionsKey;

	fn key(&self) -> Self::Target {
		let mut arr = [0u8; EPOCH_KEY_LEN];
		arr[..DB_PREFIX_LEN].copy_from_slice(&EPOCH_KEY_PREFIX[..]);

		write!(&mut arr[DB_PREFIX_LEN..], "{:016x}", self)
			.expect("format arg is valid; no more than 16 chars will be written; qed");

		EpochTransitionsKey(arr)
	}
}

/// Familial details concerning a block
#[derive(Debug, Clone, MallocSizeOf)]
pub struct BlockDetails {
	/// Block number
	pub number: BlockNumber,
	/// Total difficulty of the block and all its parents
	pub total_difficulty: U256,
	/// Parent block hash
	pub parent: H256,
	/// List of children block hashes
	pub children: Vec<H256>,
	/// Whether the block is considered finalized
	pub is_finalized: bool,
}

impl tetsy_rlp::Encodable for BlockDetails {
	fn rlp_append(&self, stream: &mut tetsy_rlp::RlpStream) {
		let use_short_version = !self.is_finalized;

		match use_short_version {
			true => { stream.begin_list(4); },
			false => { stream.begin_list(5); },
		}

		stream.append(&self.number);
		stream.append(&self.total_difficulty);
		stream.append(&self.parent);
		stream.append_list(&self.children);
		if !use_short_version {
			stream.append(&self.is_finalized);
		}
	}
}

impl tetsy_rlp::Decodable for BlockDetails {
	fn decode(rlp: &tetsy_rlp::Rlp) -> Result<Self, tetsy_rlp::DecoderError> {
		let use_short_version = match rlp.item_count()? {
			4 => true,
			5 => false,
			_ => return Err(tetsy_rlp::DecoderError::RlpIncorrectListLen),
		};

		Ok(BlockDetails {
			number: rlp.val_at(0)?,
			total_difficulty: rlp.val_at(1)?,
			parent: rlp.val_at(2)?,
			children: rlp.list_at(3)?,
			is_finalized: if use_short_version {
				false
			} else {
				rlp.val_at(4)?
			},
		})
	}
}

/// Represents address of certain transaction within block
#[derive(Debug, PartialEq, Clone, RlpEncodable, RlpDecodable, MallocSizeOf)]
pub struct TransactionAddress {
	/// Block hash
	pub block_hash: H256,
	/// Transaction index within the block
	pub index: usize
}

/// Contains all block receipts.
#[derive(Debug, Clone, RlpEncodableWrapper, RlpDecodableWrapper, MallocSizeOf)]
pub struct BlockReceipts {
	/// Block receipts
	pub receipts: Vec<Receipt>,
}

impl BlockReceipts {
	/// Create new block receipts wrapper.
	pub fn new(receipts: Vec<Receipt>) -> Self {
		BlockReceipts { receipts }
	}
}

/// Candidate transitions to an epoch with specific number.
#[derive(Clone, RlpEncodable, RlpDecodable)]
pub struct EpochTransitions {
	/// Epoch number
	pub number: u64,
	/// List of candidate transitions
	pub candidates: Vec<EpochTransition>,
}

#[cfg(test)]
mod tests {
	use tetsy_rlp::*;

	use super::BlockReceipts;

	#[test]
	fn encode_block_receipts() {
		let br = BlockReceipts::new(Vec::new());

		let mut s = RlpStream::new_list(2);
		s.append(&br);
		assert!(!s.is_finished(), "List shouldn't finished yet");
		s.append(&br);
		assert!(s.is_finished(), "List should be finished now");
		s.out();
	}
}