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
// This file is part of Darwinia.
//
// Copyright (C) 2018-2022 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia 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.
//
// Darwinia 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 Darwinia. If not, see <https://www.gnu.org/licenses/>.

//! Auxillary struct/enums for Darwinia runtime.

// --- core ---
use core::marker::PhantomData;
// --- crates.io ---
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
// --- paritytech ---
use frame_support::traits::{Currency, Get, Imbalance, OnUnbalanced};
use sp_io::offchain;
use sp_npos_elections::ExtendedBalance;
use sp_runtime::{traits::TrailingZeroInput, RuntimeDebug};
// --- darwinia-network ---
use crate::*;
use drml_primitives::AccountId;

darwinia_support::impl_account_data! {
	struct AccountData<Balance>
	for
		RingInstance,
		KtonInstance
	where
		Balance = drml_primitives::Balance
	{
		// other data
	}
}

/// Logic for the author to get a portion of fees.
pub struct ToAuthor<R>(PhantomData<R>);
impl<R> OnUnbalanced<RingNegativeImbalance<R>> for ToAuthor<R>
where
	R: darwinia_balances::Config<RingInstance> + pallet_authorship::Config,
{
	fn on_nonzero_unbalanced(amount: RingNegativeImbalance<R>) {
		if let Some(author) = <pallet_authorship::Pallet<R>>::author() {
			<darwinia_balances::Pallet<R, RingInstance>>::resolve_creating(&author, amount);
		}
	}
}

pub struct DealWithFees<R>(PhantomData<R>);
impl<R> OnUnbalanced<RingNegativeImbalance<R>> for DealWithFees<R>
where
	R: darwinia_balances::Config<RingInstance>
		+ pallet_treasury::Config
		+ pallet_authorship::Config,
	pallet_treasury::Pallet<R>: OnUnbalanced<RingNegativeImbalance<R>>,
	<R as frame_system::Config>::AccountId: From<AccountId> + Into<AccountId>,
	<R as frame_system::Config>::Event: From<darwinia_balances::Event<R, RingInstance>>,
{
	fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = RingNegativeImbalance<R>>) {
		if let Some(fees) = fees_then_tips.next() {
			// for fees, 80% to treasury, 20% to author
			let mut split = fees.ration(80, 20);

			if let Some(tips) = fees_then_tips.next() {
				// for tips, if any, 100% to author
				tips.merge_into(&mut split.1);
			}

			<pallet_treasury::Pallet<R> as OnUnbalanced<_>>::on_unbalanced(split.0);
			<ToAuthor<R> as OnUnbalanced<_>>::on_unbalanced(split.1);
		}
	}
}

/// A source of random balance for the NPoS Solver, which is meant to be run by the OCW election
/// miner.
pub struct OffchainRandomBalancing;
impl Get<Option<(usize, ExtendedBalance)>> for OffchainRandomBalancing {
	fn get() -> Option<(usize, ExtendedBalance)> {
		let iters = match MINER_MAX_ITERATIONS {
			0 => 0,
			max => {
				let seed = offchain::random_seed();
				let random = <u32>::decode(&mut TrailingZeroInput::new(&seed))
					.expect("input is padded with zeroes; qed")
					% max.saturating_add(1);

				random as usize
			},
		};

		Some((iters, 0))
	}
}

#[macro_export]
macro_rules! impl_self_contained_call {
	() => {
		impl fp_self_contained::SelfContainedCall for Call {
			type SignedInfo = H160;

			fn is_self_contained(&self) -> bool {
				match self {
					Call::Ethereum(call) => call.is_self_contained(),
					_ => false,
				}
			}

			fn check_self_contained(
				&self,
			) -> Option<
				Result<
					Self::SignedInfo,
					sp_runtime::transaction_validity::TransactionValidityError,
				>,
			> {
				match self {
					Call::Ethereum(call) => call.check_self_contained(),
					_ => None,
				}
			}

			fn validate_self_contained(
				&self,
				info: &Self::SignedInfo,
			) -> Option<sp_runtime::transaction_validity::TransactionValidity> {
				match self {
					Call::Ethereum(ref call) =>
						Some(validate_self_contained_inner(&self, &call, info)),
					_ => None,
				}
			}

			fn pre_dispatch_self_contained(
				&self,
				info: &Self::SignedInfo,
			) -> Option<Result<(), sp_runtime::transaction_validity::TransactionValidityError>> {
				match self {
					Call::Ethereum(call) => call.pre_dispatch_self_contained(info),
					_ => None,
				}
			}

			fn apply_self_contained(
				self,
				info: Self::SignedInfo,
			) -> Option<sp_runtime::DispatchResultWithInfo<PostDispatchInfoOf<Self>>> {
				match self {
					call @ Call::Ethereum(darwinia_ethereum::Call::transact { .. }) =>
						Some(call.dispatch(Origin::from(
							darwinia_ethereum::RawOrigin::EthereumTransaction(info),
						))),
					_ => None,
				}
			}
		}

		fn validate_self_contained_inner(
			call: &Call,
			eth_call: &darwinia_ethereum::Call<Runtime>,
			signed_info: &<Call as fp_self_contained::SelfContainedCall>::SignedInfo,
		) -> sp_runtime::transaction_validity::TransactionValidity {
			if let darwinia_ethereum::Call::transact { ref transaction } = eth_call {
				// Previously, ethereum transactions were contained in an unsigned
				// extrinsic, we now use a new form of dedicated extrinsic defined by
				// frontier, but to keep the same behavior as before, we must perform
				// the controls that were performed on the unsigned extrinsic.
				use sp_runtime::traits::SignedExtension as _;
				let input_len = match transaction {
					darwinia_ethereum::Transaction::Legacy(t) => t.input.len(),
					darwinia_ethereum::Transaction::EIP2930(t) => t.input.len(),
					darwinia_ethereum::Transaction::EIP1559(t) => t.input.len(),
				};
				let extra_validation =
					SignedExtra::validate_unsigned(call, &call.get_dispatch_info(), input_len)?;
				// Then, do the controls defined by the ethereum pallet.
				use fp_self_contained::SelfContainedCall as _;
				let self_contained_validation =
					eth_call.validate_self_contained(signed_info).ok_or(
						sp_runtime::transaction_validity::TransactionValidityError::Invalid(
							sp_runtime::transaction_validity::InvalidTransaction::BadProof,
						),
					)??;

				Ok(extra_validation.combine_with(self_contained_validation))
			} else {
				Err(sp_runtime::transaction_validity::TransactionValidityError::Unknown(
					sp_runtime::transaction_validity::UnknownTransaction::CannotLookup,
				))
			}
		}
	};
}