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
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This file is part of Frontier.
//
// Copyright (c) 2021-2022 Parity Technologies (UK) Ltd.
//
// This program 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.
//
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.

use std::{
	collections::HashMap,
	str::{self, FromStr},
	sync::Arc,
};

use ethereum_types::H256;
use serde::Deserialize;
// Substrate
use sp_runtime::traits::Block as BlockT;

use super::{utils::FrontierDbMessage, FrontierDbCmd, Operation};

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum MetaValue<H> {
	Tips(Vec<H>),
	Schema(HashMap<H256, fp_storage::EthereumStorageSchema>),
}

#[derive(Clone, Copy, Debug)]
pub enum MetaKey {
	Tips,
	Schema,
}

impl FromStr for MetaKey {
	type Err = sc_cli::Error;

	// A convenience function to verify the user input is known.
	fn from_str(input: &str) -> Result<MetaKey, Self::Err> {
		let tips = str::from_utf8(fc_db::kv::static_keys::CURRENT_SYNCING_TIPS).unwrap();
		let schema = str::from_utf8(fp_storage::PALLET_ETHEREUM_SCHEMA_CACHE).unwrap();
		match input {
			x if x == tips => Ok(MetaKey::Tips),
			y if y == schema => Ok(MetaKey::Schema),
			_ => Err(format!("`{:?}` is not a meta column static key", input).into()),
		}
	}
}

pub struct MetaDb<'a, B: BlockT> {
	cmd: &'a FrontierDbCmd,
	backend: Arc<fc_db::kv::Backend<B>>,
}

impl<'a, B: BlockT> MetaDb<'a, B> {
	pub fn new(cmd: &'a FrontierDbCmd, backend: Arc<fc_db::kv::Backend<B>>) -> Self {
		Self { cmd, backend }
	}

	pub fn query(&self, key: &MetaKey, value: &Option<MetaValue<B::Hash>>) -> sc_cli::Result<()> {
		match self.cmd.operation {
			Operation::Create => match (key, value) {
				// Insert data to the meta column, static tips key.
				(MetaKey::Tips, Some(MetaValue::Tips(hashes))) => {
					if self.backend.meta().current_syncing_tips()?.is_empty() {
						self.backend
							.meta()
							.write_current_syncing_tips(hashes.clone())?;
					} else {
						return Err(self.key_not_empty_error(key));
					}
				}
				// Insert data to the meta column, static schema cache key.
				(MetaKey::Schema, Some(MetaValue::Schema(schema_map))) => {
					if self.backend.meta().ethereum_schema()?.is_none() {
						let data = schema_map
							.iter()
							.map(|(key, value)| (*value, *key))
							.collect::<Vec<(fp_storage::EthereumStorageSchema, H256)>>();
						self.backend.meta().write_ethereum_schema(data)?;
					} else {
						return Err(self.key_not_empty_error(key));
					}
				}
				_ => return Err(self.key_value_error(key, value)),
			},
			Operation::Read => match key {
				// Read meta column, static tips key.
				MetaKey::Tips => {
					let value = self.backend.meta().current_syncing_tips()?;
					println!("{:?}", value);
				}
				// Read meta column, static schema cache key.
				MetaKey::Schema => {
					let value = self.backend.meta().ethereum_schema()?;
					println!("{:?}", value);
				}
			},
			Operation::Update => match (key, value) {
				// Update the static tips key's value.
				(MetaKey::Tips, Some(MetaValue::Tips(new_value))) => {
					let value = self.backend.meta().current_syncing_tips()?;
					self.confirmation_prompt(&self.cmd.operation, key, &value, new_value)?;
					self.backend
						.meta()
						.write_current_syncing_tips(new_value.clone())?;
				}
				// Update the static schema cache key's value.
				(MetaKey::Schema, Some(MetaValue::Schema(schema_map))) => {
					let value = self.backend.meta().ethereum_schema()?;
					let new_value = schema_map
						.iter()
						.map(|(key, value)| (*value, *key))
						.collect::<Vec<(fp_storage::EthereumStorageSchema, H256)>>();
					self.confirmation_prompt(
						&self.cmd.operation,
						key,
						&value,
						&Some(new_value.clone()),
					)?;
					self.backend.meta().write_ethereum_schema(new_value)?;
				}
				_ => return Err(self.key_value_error(key, value)),
			},
			Operation::Delete => match key {
				// Deletes the static tips key's value.
				MetaKey::Tips => {
					let value = self.backend.meta().current_syncing_tips()?;
					self.confirmation_prompt(&self.cmd.operation, key, &value, &vec![])?;
					self.backend.meta().write_current_syncing_tips(vec![])?;
				}
				// Deletes the static schema cache key's value.
				MetaKey::Schema => {
					let value = self.backend.meta().ethereum_schema()?;
					self.confirmation_prompt(&self.cmd.operation, key, &value, &Some(vec![]))?;
					self.backend.meta().write_ethereum_schema(vec![])?;
				}
			},
		}
		Ok(())
	}
}

impl<'a, B: BlockT> FrontierDbMessage for MetaDb<'a, B> {}