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
// This file is part of Frontier.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// 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::{marker::PhantomData, sync::Arc};

use ethereum_types::{Address, H256, U256};
// Substrate
use sp_api::{ApiExt, ApiRef, ProvideRuntimeApi};
use sp_runtime::{traits::Block as BlockT, Permill};
// Frontier
use fp_rpc::{EthereumRuntimeRPCApi, TransactionStatus};

use crate::overrides::StorageOverride;

/// A storage override for runtimes that use runtime API.
#[derive(Clone)]
pub struct RuntimeApiStorageOverride<B, C> {
	client: Arc<C>,
	_marker: PhantomData<B>,
}

impl<B, C> RuntimeApiStorageOverride<B, C> {
	pub fn new(client: Arc<C>) -> Self {
		Self {
			client,
			_marker: PhantomData,
		}
	}
}

impl<B, C> RuntimeApiStorageOverride<B, C>
where
	B: BlockT,
	C: ProvideRuntimeApi<B>,
	C::Api: EthereumRuntimeRPCApi<B>,
{
	fn api_version(api: &ApiRef<'_, C::Api>, block_hash: B::Hash) -> Option<u32> {
		match api.api_version::<dyn EthereumRuntimeRPCApi<B>>(block_hash) {
			Ok(Some(api_version)) => Some(api_version),
			_ => None,
		}
	}
}

impl<B, C> StorageOverride<B> for RuntimeApiStorageOverride<B, C>
where
	B: BlockT,
	C: ProvideRuntimeApi<B> + Send + Sync,
	C::Api: EthereumRuntimeRPCApi<B>,
{
	fn account_code_at(&self, block_hash: B::Hash, address: Address) -> Option<Vec<u8>> {
		self.client
			.runtime_api()
			.account_code_at(block_hash, address)
			.ok()
	}

	fn account_storage_at(
		&self,
		block_hash: B::Hash,
		address: Address,
		index: U256,
	) -> Option<H256> {
		self.client
			.runtime_api()
			.storage_at(block_hash, address, index)
			.ok()
	}

	fn current_block(&self, block_hash: B::Hash) -> Option<ethereum::BlockV3> {
		let api = self.client.runtime_api();

		let api_version = Self::api_version(&api, block_hash)?;
		if api_version == 1 {
			#[allow(deprecated)]
			let old_block = api.current_block_before_version_2(block_hash).ok()?;
			old_block.map(|block| block.into())
		} else {
			api.current_block(block_hash).ok()?
		}
	}

	fn current_receipts(&self, block_hash: B::Hash) -> Option<Vec<ethereum::ReceiptV4>> {
		let api = self.client.runtime_api();

		let api_version = Self::api_version(&api, block_hash)?;
		if api_version < 4 {
			#[allow(deprecated)]
			let old_receipts = api.current_receipts_before_version_4(block_hash).ok()?;
			old_receipts.map(|receipts| {
				receipts
					.into_iter()
					.map(|r| {
						ethereum::ReceiptV4::Legacy(ethereum::EIP658ReceiptData {
							status_code: r.state_root.to_low_u64_be() as u8,
							used_gas: r.used_gas,
							logs_bloom: r.logs_bloom,
							logs: r.logs,
						})
					})
					.collect()
			})
		} else {
			self.client
				.runtime_api()
				.current_receipts(block_hash)
				.ok()?
		}
	}

	fn current_transaction_statuses(&self, block_hash: B::Hash) -> Option<Vec<TransactionStatus>> {
		self.client
			.runtime_api()
			.current_transaction_statuses(block_hash)
			.ok()?
	}

	fn elasticity(&self, block_hash: B::Hash) -> Option<Permill> {
		if self.is_eip1559(block_hash) {
			self.client.runtime_api().elasticity(block_hash).ok()?
		} else {
			None
		}
	}

	fn is_eip1559(&self, block_hash: B::Hash) -> bool {
		let api = self.client.runtime_api();
		if let Some(api_version) = Self::api_version(&api, block_hash) {
			api_version >= 2
		} else {
			false
		}
	}
}