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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]

use sp_io::hashing::blake2_256;

use serai_primitives::*;

pub use in_instructions_primitives as primitives;
use primitives::*;

// TODO: Investigate why Substrate generates these
#[allow(
  unreachable_patterns,
  clippy::cast_possible_truncation,
  clippy::no_effect_underscore_binding,
  clippy::empty_docs
)]
#[frame_support::pallet]
pub mod pallet {
  use sp_std::vec;
  use sp_application_crypto::RuntimePublic;
  use sp_runtime::traits::Zero;
  use sp_core::sr25519::Public;

  use frame_support::pallet_prelude::*;
  use frame_system::{pallet_prelude::*, RawOrigin};

  use coins_pallet::{
    Config as CoinsConfig, Pallet as Coins,
    primitives::{OutInstruction, OutInstructionWithBalance},
  };
  use dex_pallet::{Config as DexConfig, Pallet as Dex};
  use validator_sets_pallet::{
    primitives::{Session, ValidatorSet, ExternalValidatorSet},
    Config as ValidatorSetsConfig, Pallet as ValidatorSets,
  };

  use genesis_liquidity_pallet::{
    Pallet as GenesisLiq, Config as GenesisLiqConfig, primitives::GENESIS_LIQUIDITY_ACCOUNT,
  };
  use emissions_pallet::{Pallet as Emissions, Config as EmissionsConfig, primitives::POL_ACCOUNT};

  use super::*;

  #[pallet::config]
  pub trait Config:
    frame_system::Config
    + CoinsConfig
    + DexConfig
    + ValidatorSetsConfig
    + GenesisLiqConfig
    + EmissionsConfig
  {
    type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
  }

  #[pallet::event]
  #[pallet::generate_deposit(fn deposit_event)]
  pub enum Event<T: Config> {
    Batch { network: ExternalNetworkId, id: u32, block: BlockHash, instructions_hash: [u8; 32] },
    InstructionFailure { network: ExternalNetworkId, id: u32, index: u32 },
    Halt { network: ExternalNetworkId },
  }

  #[pallet::error]
  pub enum Error<T> {
    /// Coin and OutAddress types don't match.
    InvalidAddressForCoin,
  }

  #[pallet::pallet]
  pub struct Pallet<T>(PhantomData<T>);

  // The ID of the last executed Batch for a network.
  #[pallet::storage]
  #[pallet::getter(fn batches)]
  pub(crate) type LastBatch<T: Config> =
    StorageMap<_, Identity, ExternalNetworkId, u32, OptionQuery>;

  // The last Serai block in which this validator set included a batch
  #[pallet::storage]
  #[pallet::getter(fn last_batch_block)]
  pub(crate) type LastBatchBlock<T: Config> =
    StorageMap<_, Identity, ExternalNetworkId, BlockNumberFor<T>, OptionQuery>;

  // Halted networks.
  #[pallet::storage]
  pub(crate) type Halted<T: Config> = StorageMap<_, Identity, ExternalNetworkId, (), OptionQuery>;

  // The latest block a network has acknowledged as finalized
  #[pallet::storage]
  #[pallet::getter(fn latest_network_block)]
  pub(crate) type LatestNetworkBlock<T: Config> =
    StorageMap<_, Identity, ExternalNetworkId, BlockHash, OptionQuery>;

  impl<T: Config> Pallet<T> {
    // Use a dedicated transaction layer when executing this InInstruction
    // This lets it individually error without causing any storage modifications
    #[frame_support::transactional]
    fn execute(instruction: InInstructionWithBalance) -> Result<(), DispatchError> {
      match instruction.instruction {
        InInstruction::Transfer(address) => {
          Coins::<T>::mint(address.into(), instruction.balance.into())?;
        }
        InInstruction::Dex(call) => {
          // This will only be initiated by external chain transactions. That is why we only need
          // add liquidity and swaps. Other functionalities (such as remove_liq, etc) will be
          // called directly from Serai with a native transaction.
          match call {
            DexCall::SwapAndAddLiquidity(address) => {
              let origin = RawOrigin::Signed(IN_INSTRUCTION_EXECUTOR.into());
              let coin = instruction.balance.coin;

              // mint the given coin on the account
              Coins::<T>::mint(IN_INSTRUCTION_EXECUTOR.into(), instruction.balance.into())?;

              // swap half of it for SRI
              let half = instruction.balance.amount.0 / 2;
              let path = BoundedVec::try_from(vec![coin.into(), Coin::Serai]).unwrap();
              Dex::<T>::swap_exact_tokens_for_tokens(
                origin.clone().into(),
                path,
                half,
                1, // minimum out, so we accept whatever we get.
                IN_INSTRUCTION_EXECUTOR.into(),
              )?;

              // get how much we got for our swap
              let sri_amount = Coins::<T>::balance(IN_INSTRUCTION_EXECUTOR.into(), Coin::Serai).0;

              // add liquidity
              Dex::<T>::add_liquidity(
                origin.clone().into(),
                coin,
                half,
                sri_amount,
                1,
                1,
                address.into(),
              )?;

              // TODO: minimums are set to 1 above to guarantee successful adding liq call.
              // Ideally we either get this info from user or send the leftovers back to user.
              // Let's send the leftovers back to user for now.
              let coin_balance = Coins::<T>::balance(IN_INSTRUCTION_EXECUTOR.into(), coin.into());
              let sri_balance = Coins::<T>::balance(IN_INSTRUCTION_EXECUTOR.into(), Coin::Serai);
              if coin_balance != Amount(0) {
                Coins::<T>::transfer_internal(
                  IN_INSTRUCTION_EXECUTOR.into(),
                  address.into(),
                  Balance { coin: coin.into(), amount: coin_balance },
                )?;
              }
              if sri_balance != Amount(0) {
                Coins::<T>::transfer_internal(
                  IN_INSTRUCTION_EXECUTOR.into(),
                  address.into(),
                  Balance { coin: Coin::Serai, amount: sri_balance },
                )?;
              }
            }
            DexCall::Swap(out_balance, out_address) => {
              let send_to_external = !out_address.is_native();
              let native_coin = out_balance.coin.is_native();

              // we can't send native coin to external chain
              if native_coin && send_to_external {
                Err(Error::<T>::InvalidAddressForCoin)?;
              }

              // mint the given coin on our account
              Coins::<T>::mint(IN_INSTRUCTION_EXECUTOR.into(), instruction.balance.into())?;

              // get the path
              let mut path = vec![instruction.balance.coin.into(), Coin::Serai];
              if !native_coin {
                path.push(out_balance.coin);
              }

              // get the swap address
              // if the address is internal, we can directly swap to it. if not, we swap to
              // ourselves and burn the coins to send them back on the external chain.
              let send_to = if send_to_external {
                IN_INSTRUCTION_EXECUTOR
              } else {
                out_address.clone().as_native().unwrap()
              };

              // do the swap
              let origin = RawOrigin::Signed(IN_INSTRUCTION_EXECUTOR.into());
              Dex::<T>::swap_exact_tokens_for_tokens(
                origin.clone().into(),
                BoundedVec::try_from(path).unwrap(),
                instruction.balance.amount.0,
                out_balance.amount.0,
                send_to.into(),
              )?;

              // burn the received coins so that they sent back to the user
              // if it is requested to an external address.
              if send_to_external {
                // see how much we got
                let coin_balance =
                  Coins::<T>::balance(IN_INSTRUCTION_EXECUTOR.into(), out_balance.coin);
                let instruction = OutInstructionWithBalance {
                  instruction: OutInstruction {
                    address: out_address.as_external().unwrap(),
                    // TODO: Properly pass data. Replace address with an OutInstruction entirely?
                    data: None,
                  },
                  balance: ExternalBalance {
                    coin: out_balance.coin.try_into().unwrap(),
                    amount: coin_balance,
                  },
                };
                Coins::<T>::burn_with_instruction(origin.into(), instruction)?;
              }
            }
          }
        }
        InInstruction::GenesisLiquidity(address) => {
          Coins::<T>::mint(GENESIS_LIQUIDITY_ACCOUNT.into(), instruction.balance.into())?;
          GenesisLiq::<T>::add_coin_liquidity(address.into(), instruction.balance)?;
        }
        InInstruction::SwapToStakedSRI(address, network) => {
          Coins::<T>::mint(POL_ACCOUNT.into(), instruction.balance.into())?;
          Emissions::<T>::swap_to_staked_sri(address.into(), network, instruction.balance)?;
        }
      }
      Ok(())
    }

    pub fn halt(network: ExternalNetworkId) -> Result<(), DispatchError> {
      Halted::<T>::set(network, Some(()));
      Self::deposit_event(Event::Halt { network });
      Ok(())
    }
  }

  fn keys_for_network<T: Config>(
    network: ExternalNetworkId,
  ) -> Result<(Session, Option<Public>, Option<Public>), InvalidTransaction> {
    // If there's no session set, and therefore no keys set, then this must be an invalid signature
    let Some(session) = ValidatorSets::<T>::session(NetworkId::from(network)) else {
      Err(InvalidTransaction::BadProof)?
    };
    let mut set = ExternalValidatorSet { network, session };
    let latest = ValidatorSets::<T>::keys(set).map(|keys| keys.0);
    let prior = if set.session.0 != 0 {
      set.session.0 -= 1;
      ValidatorSets::<T>::keys(set).map(|keys| keys.0)
    } else {
      None
    };
    if prior.is_none() && latest.is_none() {
      Err(InvalidTransaction::BadProof)?;
    }
    Ok((session, prior, latest))
  }

  #[pallet::call]
  impl<T: Config> Pallet<T> {
    #[pallet::call_index(0)]
    #[pallet::weight((0, DispatchClass::Operational))] // TODO
    pub fn execute_batch(origin: OriginFor<T>, batch: SignedBatch) -> DispatchResult {
      ensure_none(origin)?;

      let batch = batch.batch;

      LatestNetworkBlock::<T>::insert(batch.network, batch.block);
      Self::deposit_event(Event::Batch {
        network: batch.network,
        id: batch.id,
        block: batch.block,
        instructions_hash: blake2_256(&batch.instructions.encode()),
      });
      for (i, instruction) in batch.instructions.into_iter().enumerate() {
        if Self::execute(instruction).is_err() {
          Self::deposit_event(Event::InstructionFailure {
            network: batch.network,
            id: batch.id,
            index: u32::try_from(i).unwrap(),
          });
        }
      }

      Ok(())
    }
  }

  #[pallet::validate_unsigned]
  impl<T: Config> ValidateUnsigned for Pallet<T> {
    type Call = Call<T>;

    fn validate_unsigned(_: TransactionSource, call: &Self::Call) -> TransactionValidity {
      // Match to be exhaustive
      let batch = match call {
        Call::execute_batch { ref batch } => batch,
        Call::__Ignore(_, _) => unreachable!(),
      };

      // verify the batch size
      // TODO: Merge this encode with the one done by batch_message
      if batch.batch.encode().len() > MAX_BATCH_SIZE {
        Err(InvalidTransaction::ExhaustsResources)?;
      }
      let network = batch.batch.network;

      // verify the signature
      let (current_session, prior, current) = keys_for_network::<T>(network)?;
      let batch_message = batch_message(&batch.batch);
      // Check the prior key first since only a single `Batch` (the last one) will be when prior is
      // Some yet prior wasn't the signing key
      let valid_by_prior =
        if let Some(key) = prior { key.verify(&batch_message, &batch.signature) } else { false };
      let valid = valid_by_prior ||
        (if let Some(key) = current {
          key.verify(&batch_message, &batch.signature)
        } else {
          false
        });
      if !valid {
        Err(InvalidTransaction::BadProof)?;
      }

      if Halted::<T>::contains_key(network) {
        Err(InvalidTransaction::Custom(1))?;
      }

      // If it wasn't valid by the prior key, meaning it was valid by the current key, the current
      // key is publishing `Batch`s. This should only happen once the current key has verified all
      // `Batch`s published by the prior key, meaning they are accepting the hand-over.
      if prior.is_some() && (!valid_by_prior) {
        ValidatorSets::<T>::retire_set(ValidatorSet {
          network: network.into(),
          session: Session(current_session.0 - 1),
        });
      }

      // check that this validator set isn't publishing a batch more than once per block
      let current_block = <frame_system::Pallet<T>>::block_number();
      let last_block = LastBatchBlock::<T>::get(network).unwrap_or(Zero::zero());
      if last_block >= current_block {
        Err(InvalidTransaction::Future)?;
      }
      LastBatchBlock::<T>::insert(batch.batch.network, frame_system::Pallet::<T>::block_number());

      // Verify the batch is sequential
      // LastBatch has the last ID set. The next ID should be it + 1
      // If there's no ID, the next ID should be 0
      let expected = LastBatch::<T>::get(network).map_or(0, |prev| prev + 1);
      if batch.batch.id < expected {
        Err(InvalidTransaction::Stale)?;
      }
      if batch.batch.id > expected {
        Err(InvalidTransaction::Future)?;
      }
      LastBatch::<T>::insert(batch.batch.network, batch.batch.id);

      // Verify all Balances in this Batch are for this network
      for instruction in &batch.batch.instructions {
        // Verify this coin is for this network
        // If this is ever hit, it means the validator set has turned malicious and should be fully
        // slashed
        // Because we have an error here, no validator set which turns malicious should execute
        // this code path
        // Accordingly, there's no value in writing code to fully slash the network, when such an
        // even would require a runtime upgrade to fully resolve anyways
        if instruction.balance.coin.network() != batch.batch.network {
          Err(InvalidTransaction::Custom(2))?;
        }
      }

      ValidTransaction::with_tag_prefix("in-instructions")
        .and_provides((batch.batch.network, batch.batch.id))
        // Set a 10 block longevity, though this should be included in the next block
        .longevity(10)
        .propagate(true)
        .build()
    }

    // Explicitly provide a pre-dispatch which calls validate_unsigned
    fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> {
      Self::validate_unsigned(TransactionSource::InBlock, call).map(|_| ()).map_err(Into::into)
    }
  }
}

pub use pallet::*;