Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 455e5a6

Browse files
committed
Derive Types
This patch adds a set of macros to derive types.json automatically. This is, all in all, a little complicated because the best way to analyze the code is through macros (since we can easily mark types that need to be exported) but macros, for obvious reasons, are run at compile-time and are not _meant_ to be side-effecty. That is, you can't easily just store a bunch of global data and when you're finished just emit it. Instead, we truncate and re-write a file-system temp file each time we encounter a new type and eventually we stop encountering new ones and we're good. We do this separately for each project in `scripts/build_types.json` since partial compilation and project-by-project compilation makes this even harder to do. We also need to clean and rebuild each time to ensure every macro is run. _Also_ there is some compile time slowness added here since I can't seem to remove the macros via features or other settings since this runs in the same layer as those types of tools. All-in-all, it's significantly fast enough and hopefully will make the process of updating our types.json trivial going forward. Note: some of the code is pretty spaghetti in the derive code itself and can be upgraded over time. The issue is that there is a ton of possibilities that could occur in the syntax and we probably don't want to handle all of them. The most complex one that we need to consider is `WIDTH` which, as a const, is not very easy to determine its value, so for now, we just use its value directly, since literals are fairly easy. Note: the goal of `types-derive` is that it is generic and can be added to substrate's core, so we try to avoid "Gateway-specific" features in the code.
1 parent fc632e8 commit 455e5a6

28 files changed

+829
-384
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ releases
4242
# Chains
4343
chains/dev/*
4444
*.gateway_history
45+
46+
# Backups
47+
*.bak

Cargo.lock

+15-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ members = [
1212
'gateway-crypto',
1313
'ethereum-client',
1414
'test-utils/open-oracle-mock-reporter',
15-
'trx-request'
15+
'trx-request',
16+
'types-derive',
1617
]

ethereum-client/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ sp-std = { default-features = false, git = 'https://github.com/compound-finance/
2121

2222
our-std = { path = '../our-std', default-features = false }
2323

24+
types-derive = { path = '../types-derive' }
25+
2426
[features]
2527
default = ['std']
2628
std = [

ethereum-client/src/events.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use codec::{Decode, Encode};
33
use our_std::convert::TryInto;
44
use our_std::RuntimeDebug;
55

6-
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
6+
use types_derive::Types;
7+
8+
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
79
pub enum EthereumEvent {
810
Lock {
911
asset: [u8; 20],

ethereum-client/src/lib.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use our_std::RuntimeDebug;
1313
use serde::Deserialize;
1414
use sp_runtime::offchain::{http, Duration};
1515

16-
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
16+
use types_derive::Types;
17+
18+
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
1719
pub enum EthereumClientError {
1820
HttpIoError,
1921
HttpTimeout,
@@ -22,27 +24,27 @@ pub enum EthereumClientError {
2224
JsonParseError,
2325
}
2426

25-
#[derive(Deserialize, RuntimeDebug, PartialEq)]
27+
#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
2628
pub struct ResponseError {
2729
pub message: Option<String>,
2830
pub code: Option<i64>,
2931
}
3032

31-
#[derive(Deserialize, RuntimeDebug, PartialEq)]
33+
#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
3234
pub struct EventsResponse<T> {
3335
pub id: Option<u64>,
3436
pub result: Option<Vec<T>>,
3537
pub error: Option<ResponseError>,
3638
}
3739

38-
#[derive(Deserialize, RuntimeDebug, PartialEq)]
40+
#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
3941
pub struct BlockResponse {
4042
pub id: Option<u64>,
4143
pub result: Option<String>,
4244
pub error: Option<ResponseError>,
4345
}
4446

45-
#[derive(Deserialize, RuntimeDebug, PartialEq)]
47+
#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
4648
#[serde(rename_all = "camelCase")]
4749
pub struct LogObject {
4850
/// true when the log was removed, due to a chain reorganization. false if it's a valid log.

pallets/cash/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ gateway-crypto = { path = '../../gateway-crypto', default-features = false }
4646
trx-request = { path = '../../trx-request', default-features = false }
4747
our-std = { path = '../../our-std', default-features = false }
4848

49+
types-derive = { path = '../../types-derive' }
50+
4951
[dev-dependencies]
5052
frame-benchmarking = { git = 'https://github.com/compound-finance/substrate', branch = 'master'}
5153
serial_test = "*"

pallets/cash/src/chains.rs

+42-7
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ use codec::{Decode, Encode};
99
use gateway_crypto::public_key_bytes_to_eth_address;
1010
use our_std::{str::FromStr, Debuggable, Deserialize, RuntimeDebug, Serialize};
1111

12+
use types_derive::{type_alias, Types};
13+
1214
/// Type for representing the selection of a supported chain.
1315
#[derive(Serialize, Deserialize)] // used in config
14-
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
16+
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
1517
pub enum ChainId {
1618
Gate,
1719
Eth,
@@ -99,7 +101,7 @@ impl Default for ChainId {
99101
}
100102

101103
/// Type for an account tied to a chain.
102-
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
104+
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
103105
pub enum ChainAccount {
104106
Gate(<Gateway as Chain>::Address),
105107
Eth(<Ethereum as Chain>::Address),
@@ -143,7 +145,7 @@ impl From<ChainAccount> for String {
143145
}
144146

145147
/// Type for an asset tied to a chain.
146-
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
148+
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
147149
pub enum ChainAsset {
148150
Gate(<Gateway as Chain>::Address),
149151
Eth(<Ethereum as Chain>::Address),
@@ -187,7 +189,7 @@ impl From<ChainAsset> for String {
187189
}
188190

189191
/// Type for a signature and account tied to a chain.
190-
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
192+
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
191193
pub enum ChainAccountSignature {
192194
Gate(<Gateway as Chain>::Address, <Gateway as Chain>::Signature),
193195
Eth(<Ethereum as Chain>::Address, <Ethereum as Chain>::Signature),
@@ -223,7 +225,7 @@ impl ChainAccountSignature {
223225
}
224226

225227
/// Type for an hash tied to a chain.
226-
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
228+
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
227229
pub enum ChainHash {
228230
Gate(<Gateway as Chain>::Hash),
229231
Eth(<Ethereum as Chain>::Hash),
@@ -233,7 +235,7 @@ pub enum ChainHash {
233235
}
234236

235237
/// Type for a signature tied to a chain.
236-
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
238+
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
237239
pub enum ChainSignature {
238240
Gate(<Gateway as Chain>::Signature),
239241
Eth(<Ethereum as Chain>::Signature),
@@ -262,7 +264,7 @@ impl ChainSignature {
262264
}
263265

264266
/// Type for a list of chain signatures.
265-
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
267+
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
266268
pub enum ChainSignatureList {
267269
Gate(Vec<(<Gateway as Chain>::Address, <Gateway as Chain>::Signature)>),
268270
Eth(Vec<(<Ethereum as Chain>::Address, <Ethereum as Chain>::Signature)>),
@@ -371,7 +373,34 @@ impl Chain for Gateway {
371373
impl Chain for Ethereum {
372374
const ID: ChainId = ChainId::Eth;
373375

376+
#[type_alias("Ethereum__Chain__")]
377+
type Address = [u8; 20];
378+
379+
#[type_alias("Ethereum__Chain__")]
380+
type Amount = u128;
381+
382+
#[type_alias("Ethereum__Chain__")]
383+
type CashIndex = u128;
384+
385+
#[type_alias("Ethereum__Chain__")]
386+
type Rate = u128;
387+
388+
#[type_alias("Ethereum__Chain__")]
389+
type Timestamp = u64;
390+
391+
#[type_alias("Ethereum__Chain__")]
392+
type Hash = [u8; 32];
393+
394+
#[type_alias("Ethereum__Chain__")]
395+
type PublicKey = [u8; 64];
396+
397+
#[type_alias("Ethereum__Chain__")]
398+
type Signature = [u8; 65];
399+
400+
#[type_alias("Ethereum__Chain__")]
374401
type EventId = eth::EventId;
402+
403+
#[type_alias("Ethereum__Chain__")]
375404
type Event = eth::Event;
376405

377406
fn zero_hash() -> Self::Hash {
@@ -582,14 +611,20 @@ pub mod eth {
582611
use codec::{Decode, Encode};
583612
use our_std::RuntimeDebug;
584613

614+
use types_derive::type_alias;
615+
585616
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
586617
pub enum RecoveryError {
587618
SignatureRecoveryError,
588619
}
589620

621+
#[type_alias("Eth__")]
590622
pub type BlockNumber = u64;
623+
624+
#[type_alias("Eth__")]
591625
pub type LogIndex = u64;
592626

627+
#[type_alias("Eth__")]
593628
pub type EventId = (BlockNumber, LogIndex);
594629

595630
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]

pallets/cash/src/events.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ use crate::reason::Reason;
44
use crate::types::SignersSet;
55
use codec::alloc::string::String;
66
use codec::{Decode, Encode};
7+
use ethereum_client::EthereumClientError;
78
use our_std::{vec::Vec, RuntimeDebug};
89

10+
use types_derive::Types;
11+
912
extern crate ethereum_client;
1013

11-
#[derive(RuntimeDebug)]
14+
#[derive(RuntimeDebug, Types)]
1215
pub struct EventInfo {
1316
pub latest_eth_block: u64,
1417
pub events: Vec<(ChainLogId, ChainLogEvent)>,
1518
}
1619

17-
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
20+
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
1821
pub enum ChainLogId {
1922
Eth(eth::BlockNumber, eth::LogIndex),
2023
}
@@ -29,7 +32,7 @@ impl ChainLogId {
2932
}
3033
}
3134

32-
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
35+
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
3336
pub enum ChainLogEvent {
3437
Eth(ethereum_client::EthereumLogEvent),
3538
}
@@ -47,7 +50,7 @@ impl ChainLogEvent {
4750
}
4851

4952
/// Type for the status of an event on the queue.
50-
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
53+
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
5154
pub enum EventState {
5255
Pending { signers: SignersSet },
5356
Failed { reason: Reason },
@@ -62,12 +65,12 @@ impl Default for EventState {
6265
}
6366
}
6467

65-
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
68+
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
6669
pub enum EventError {
6770
EthRpcUrlMissing,
6871
EthRpcUrlInvalid,
6972
StarportAddressInvalid,
70-
EthereumClientError(ethereum_client::EthereumClientError),
73+
EthereumClientError(EthereumClientError),
7174
ErrorDecodingHex,
7275
}
7376

pallets/cash/src/factor.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::{
88
types::{Decimals, Int, Uint},
99
};
1010

11+
use types_derive::Types;
12+
1113
/// Type for wrapping intermediate signed calculations.
1214
pub struct BigInt(pub BigI);
1315

@@ -92,7 +94,7 @@ impl BigUint {
9294

9395
/// Type for an unsigned factor, with a large fixed number of decimals.
9496
#[derive(Serialize, Deserialize)] // used in config
95-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug)]
97+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, Types)]
9698
pub struct Factor(pub Uint);
9799

98100
impl Factor {

pallets/cash/src/internal/set_yield_next.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use codec::{Decode, Encode};
1212
use frame_support::storage::StorageValue;
1313
use our_std::Debuggable;
1414

15-
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, Debuggable)]
15+
use types_derive::Types;
16+
17+
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, Debuggable, Types)]
1618
pub enum SetYieldNextError {
1719
TimestampTooSoonToNow,
1820
TimestampTooSoonToNext,

pallets/cash/src/internal/validate_trx.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use frame_support::storage::{IterableStorageMap, StorageDoubleMap, StorageValue}
1212
use our_std::RuntimeDebug;
1313
use sp_runtime::transaction_validity::{TransactionSource, TransactionValidity, ValidTransaction};
1414

15-
#[derive(Eq, PartialEq, RuntimeDebug)]
15+
use types_derive::Types;
16+
17+
#[derive(Eq, PartialEq, RuntimeDebug, Types)]
1618
pub enum ValidationError {
1719
InvalidInternalOnly,
1820
InvalidNextCode,

pallets/cash/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use pallet_oracle;
4444
use pallet_oracle::ticker::Ticker;
4545
use pallet_session;
4646
use pallet_timestamp;
47+
use types_derive::type_alias;
4748

4849
#[macro_use]
4950
extern crate lazy_static;
@@ -74,6 +75,7 @@ pub mod benchmarking;
7475
mod tests;
7576

7677
/// Type for linking sessions to validators.
78+
#[type_alias]
7779
pub type SubstrateId = AccountId32;
7880

7981
/// Configure the pallet by specifying the parameters and types on which it depends.

0 commit comments

Comments
 (0)