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

Commit b50cbd8

Browse files
committed
Ensure new types seems to match old types as much as possible
1 parent 455e5a6 commit b50cbd8

File tree

13 files changed

+224
-198
lines changed

13 files changed

+224
-198
lines changed

Cargo.lock

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

base_types.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"AccountId32": "[u8;32]",
3+
"Authorities": "Vec<AccountId32>",
4+
"Address": "MultiAddress",
5+
"Keys": "SessionKeys",
6+
"SignedPayload": "Vec<u8>",
7+
"VersionedAuthorityList": {
8+
"authorityList": "AuthorityList",
9+
"version": "u8"
10+
},
11+
"LookupSource": "MultiAddress",
12+
"SessionKeys": {
13+
"aura": "[u8;32]",
14+
"grandpa": "[u8;32]"
15+
}
16+
}

ethereum-client/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use sp_runtime::offchain::{http, Duration};
1515

1616
use types_derive::Types;
1717

18-
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
18+
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
1919
pub enum EthereumClientError {
2020
HttpIoError,
2121
HttpTimeout,
@@ -24,27 +24,27 @@ pub enum EthereumClientError {
2424
JsonParseError,
2525
}
2626

27-
#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
27+
#[derive(Deserialize, RuntimeDebug, PartialEq)]
2828
pub struct ResponseError {
2929
pub message: Option<String>,
3030
pub code: Option<i64>,
3131
}
3232

33-
#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
33+
#[derive(Deserialize, RuntimeDebug, PartialEq)]
3434
pub struct EventsResponse<T> {
3535
pub id: Option<u64>,
3636
pub result: Option<Vec<T>>,
3737
pub error: Option<ResponseError>,
3838
}
3939

40-
#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
40+
#[derive(Deserialize, RuntimeDebug, PartialEq)]
4141
pub struct BlockResponse {
4242
pub id: Option<u64>,
4343
pub result: Option<String>,
4444
pub error: Option<ResponseError>,
4545
}
4646

47-
#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
47+
#[derive(Deserialize, RuntimeDebug, PartialEq)]
4848
#[serde(rename_all = "camelCase")]
4949
pub struct LogObject {
5050
/// true when the log was removed, due to a chain reorganization. false if it's a valid log.
@@ -79,7 +79,7 @@ fn deserialize_get_block_number_response(
7979
serde_json::from_str(response)
8080
}
8181

82-
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
82+
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
8383
pub struct EthereumLogEvent {
8484
pub block_hash: [u8; 32],
8585
pub block_number: u64,

gateway-crypto/Cargo.toml

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

2424
our-std = { path = "../our-std", default-features = false }
2525

26+
types-derive = { path = "../types-derive" }
27+
2628
[features]
2729
default = ['std']
2830
std = [

gateway-crypto/src/no_std.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use codec::{Decode, Encode};
22
use our_std::{convert::TryInto, RuntimeDebug};
33
use tiny_keccak::Hasher;
44

5+
use types_derive::Types;
6+
57
pub type SignatureBytes = [u8; 65];
68

79
pub type AddressBytes = [u8; 20];
@@ -19,7 +21,7 @@ pub type HashedMessageBytes = [u8; 32];
1921
/// * The key id provided is unknown
2022
/// * The HSM is not available
2123
/// * The HSM failed to sign this request for some other reason
22-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug)]
24+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, Types)]
2325
pub enum CryptoError {
2426
Unknown,
2527
KeyNotFound,

node/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ pallet-cash-runtime-api = { path = '../pallets/cash/runtime-api' }
8181
pallet-oracle = { path = '../pallets/oracle' }
8282
runtime-interfaces = { path = '../pallets/runtime-interfaces' }
8383

84+
types-derive = { path = '../types-derive' }
85+
8486
[dev-dependencies]
8587
tempfile = "3.1.0"
8688

@@ -92,7 +94,7 @@ sp-timestamp = { default-features = false, git = 'https://github.com/compound-fi
9294
frame-system = { git = 'https://github.com/compound-finance/substrate', branch = 'master' }
9395

9496
[features]
95-
default = ['with-rocks-db', 'runtime-benchmarks']
97+
default = ['with-parity-db']
9698
with-parity-db=['sc-client-db/with-parity-db']
9799
with-rocks-db=['sc-client-db/with-kvdb-rocksdb']
98100
runtime-benchmarks = [

node/src/api.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@ use pallet_cash::{
1818
use pallet_cash_runtime_api::CashApi as CashRuntimeApi;
1919
use pallet_oracle::types::AssetPrice;
2020

21+
use types_derive::{type_alias, Types};
22+
2123
const RUNTIME_ERROR: i64 = 1;
2224
const CHAIN_ERROR: i64 = 2;
2325

2426
// Note: no 128 bit integers for the moment
2527
// due to issues with serde/serde_json
28+
#[type_alias]
2629
pub type ApiAPR = u64;
30+
31+
#[type_alias]
2732
pub type ApiRates = (ApiAPR, ApiAPR);
2833

29-
#[derive(Deserialize, Serialize)]
34+
#[derive(Deserialize, Serialize, Types)]
3035
pub struct ApiAssetData {
3136
asset: ChainAsset,
3237
balance: String,
@@ -38,7 +43,7 @@ pub struct ApiAssetData {
3843
price: String,
3944
}
4045

41-
#[derive(Deserialize, Serialize)]
46+
#[derive(Deserialize, Serialize, Types)]
4247
pub struct ApiCashData {
4348
balance: String,
4449
cash_yield: String,

pallets/cash/src/events.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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;
7+
use ethereum_client::{EthereumClientError, EthereumLogEvent};
88
use our_std::{vec::Vec, RuntimeDebug};
99

1010
use types_derive::Types;
@@ -34,7 +34,7 @@ impl ChainLogId {
3434

3535
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
3636
pub enum ChainLogEvent {
37-
Eth(ethereum_client::EthereumLogEvent),
37+
Eth(EthereumLogEvent),
3838
}
3939

4040
impl ChainLogEvent {
@@ -65,7 +65,7 @@ impl Default for EventState {
6565
}
6666
}
6767

68-
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
68+
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
6969
pub enum EventError {
7070
EthRpcUrlMissing,
7171
EthRpcUrlInvalid,

pallets/cash/src/internal/validate_trx.rs

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

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

scripts/build_types.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ types_json="./types.json"
88

99
cp "$types_json" "$types_json.bak"
1010

11-
projects=("pallet-cash" "pallet-oracle" "ethereum-client")
11+
projects=("gateway" "pallet-cash" "pallet-oracle" "ethereum-client" "gateway-crypto")
1212

1313
set -x
1414

15-
json_files=()
15+
json_files=(./base_types.json)
1616

1717
for project in ${projects[@]}; do
1818
json_file="$(mktemp)"
@@ -22,6 +22,6 @@ for project in ${projects[@]}; do
2222
json_files[${#json_files[@]}]="$json_file"
2323
done
2424

25-
jq -sS 'add' ${json_files[@]} > $types_json
25+
jq -s 'add' ${json_files[@]} | jq -r 'to_entries|sort|from_entries' > $types_json
2626

2727
echo "Built $types_json"

types-derive/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ edition = "2018"
1010
syn = { version = "1.0", features=["full"] }
1111
quote = "1.0"
1212
lazy_static = '1.4.0'
13-
serde_json = "1.0.64"
13+
serde_json = { version = "1.0.64", features=["preserve_order"] }
1414

1515
[lib]
1616
proc-macro = true

types-derive/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn derive_types(input: TokenStream) -> TokenStream {
5252
}
5353

5454
fn write_types(new_types: Vec<(String, serde_json::Value)>) {
55-
let data = serde_json::to_string_pretty(&json!(new_types
55+
let data = serde_json::to_string(&json!(new_types
5656
.clone()
5757
.into_iter()
5858
.collect::<serde_json::Map<_, _>>()))
@@ -168,7 +168,7 @@ fn process_fields(
168168
.collect::<Vec<_>>());
169169
match prefix_opt {
170170
Some(prefix) => {
171-
let new_type_name = format!("{}Type", prefix);
171+
let new_type_name = format!("{}", prefix);
172172
new_types.push((new_type_name.clone(), ty));
173173
json!(new_type_name)
174174
}
@@ -187,7 +187,7 @@ fn process_fields(
187187
.collect::<serde_json::Map<_, _>>());
188188
match prefix_opt {
189189
Some(prefix) => {
190-
let new_type_name = format!("{}Type", prefix);
190+
let new_type_name = format!("{}", prefix);
191191
new_types.push((new_type_name.clone(), ty));
192192
json!(new_type_name)
193193
}

0 commit comments

Comments
 (0)