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

Commit

Permalink
Ensure new types seems to match old types as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
hayesgm committed Apr 7, 2021
1 parent 455e5a6 commit b50cbd8
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 198 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions base_types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"AccountId32": "[u8;32]",
"Authorities": "Vec<AccountId32>",
"Address": "MultiAddress",
"Keys": "SessionKeys",
"SignedPayload": "Vec<u8>",
"VersionedAuthorityList": {
"authorityList": "AuthorityList",
"version": "u8"
},
"LookupSource": "MultiAddress",
"SessionKeys": {
"aura": "[u8;32]",
"grandpa": "[u8;32]"
}
}
12 changes: 6 additions & 6 deletions ethereum-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sp_runtime::offchain::{http, Duration};

use types_derive::Types;

#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
pub enum EthereumClientError {
HttpIoError,
HttpTimeout,
Expand All @@ -24,27 +24,27 @@ pub enum EthereumClientError {
JsonParseError,
}

#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
#[derive(Deserialize, RuntimeDebug, PartialEq)]
pub struct ResponseError {
pub message: Option<String>,
pub code: Option<i64>,
}

#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
#[derive(Deserialize, RuntimeDebug, PartialEq)]
pub struct EventsResponse<T> {
pub id: Option<u64>,
pub result: Option<Vec<T>>,
pub error: Option<ResponseError>,
}

#[derive(Deserialize, RuntimeDebug, PartialEq, Types)]
#[derive(Deserialize, RuntimeDebug, PartialEq)]
pub struct BlockResponse {
pub id: Option<u64>,
pub result: Option<String>,
pub error: Option<ResponseError>,
}

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

#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
pub struct EthereumLogEvent {
pub block_hash: [u8; 32],
pub block_number: u64,
Expand Down
2 changes: 2 additions & 0 deletions gateway-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ sp-core = { default-features = false, git = 'https://github.com/compound-finance

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

types-derive = { path = "../types-derive" }

[features]
default = ['std']
std = [
Expand Down
4 changes: 3 additions & 1 deletion gateway-crypto/src/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use codec::{Decode, Encode};
use our_std::{convert::TryInto, RuntimeDebug};
use tiny_keccak::Hasher;

use types_derive::Types;

pub type SignatureBytes = [u8; 65];

pub type AddressBytes = [u8; 20];
Expand All @@ -19,7 +21,7 @@ pub type HashedMessageBytes = [u8; 32];
/// * The key id provided is unknown
/// * The HSM is not available
/// * The HSM failed to sign this request for some other reason
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, Types)]
pub enum CryptoError {
Unknown,
KeyNotFound,
Expand Down
4 changes: 3 additions & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pallet-cash-runtime-api = { path = '../pallets/cash/runtime-api' }
pallet-oracle = { path = '../pallets/oracle' }
runtime-interfaces = { path = '../pallets/runtime-interfaces' }

types-derive = { path = '../types-derive' }

[dev-dependencies]
tempfile = "3.1.0"

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

[features]
default = ['with-rocks-db', 'runtime-benchmarks']
default = ['with-parity-db']
with-parity-db=['sc-client-db/with-parity-db']
with-rocks-db=['sc-client-db/with-kvdb-rocksdb']
runtime-benchmarks = [
Expand Down
9 changes: 7 additions & 2 deletions node/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ use pallet_cash::{
use pallet_cash_runtime_api::CashApi as CashRuntimeApi;
use pallet_oracle::types::AssetPrice;

use types_derive::{type_alias, Types};

const RUNTIME_ERROR: i64 = 1;
const CHAIN_ERROR: i64 = 2;

// Note: no 128 bit integers for the moment
// due to issues with serde/serde_json
#[type_alias]
pub type ApiAPR = u64;

#[type_alias]
pub type ApiRates = (ApiAPR, ApiAPR);

#[derive(Deserialize, Serialize)]
#[derive(Deserialize, Serialize, Types)]
pub struct ApiAssetData {
asset: ChainAsset,
balance: String,
Expand All @@ -38,7 +43,7 @@ pub struct ApiAssetData {
price: String,
}

#[derive(Deserialize, Serialize)]
#[derive(Deserialize, Serialize, Types)]
pub struct ApiCashData {
balance: String,
cash_yield: String,
Expand Down
6 changes: 3 additions & 3 deletions pallets/cash/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::reason::Reason;
use crate::types::SignersSet;
use codec::alloc::string::String;
use codec::{Decode, Encode};
use ethereum_client::EthereumClientError;
use ethereum_client::{EthereumClientError, EthereumLogEvent};
use our_std::{vec::Vec, RuntimeDebug};

use types_derive::Types;
Expand Down Expand Up @@ -34,7 +34,7 @@ impl ChainLogId {

#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
pub enum ChainLogEvent {
Eth(ethereum_client::EthereumLogEvent),
Eth(EthereumLogEvent),
}

impl ChainLogEvent {
Expand Down Expand Up @@ -65,7 +65,7 @@ impl Default for EventState {
}
}

#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, Types)]
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
pub enum EventError {
EthRpcUrlMissing,
EthRpcUrlInvalid,
Expand Down
4 changes: 1 addition & 3 deletions pallets/cash/src/internal/validate_trx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use frame_support::storage::{IterableStorageMap, StorageDoubleMap, StorageValue}
use our_std::RuntimeDebug;
use sp_runtime::transaction_validity::{TransactionSource, TransactionValidity, ValidTransaction};

use types_derive::Types;

#[derive(Eq, PartialEq, RuntimeDebug, Types)]
#[derive(Eq, PartialEq, RuntimeDebug)]
pub enum ValidationError {
InvalidInternalOnly,
InvalidNextCode,
Expand Down
6 changes: 3 additions & 3 deletions scripts/build_types.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ types_json="./types.json"

cp "$types_json" "$types_json.bak"

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

set -x

json_files=()
json_files=(./base_types.json)

for project in ${projects[@]}; do
json_file="$(mktemp)"
Expand All @@ -22,6 +22,6 @@ for project in ${projects[@]}; do
json_files[${#json_files[@]}]="$json_file"
done

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

echo "Built $types_json"
2 changes: 1 addition & 1 deletion types-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2018"
syn = { version = "1.0", features=["full"] }
quote = "1.0"
lazy_static = '1.4.0'
serde_json = "1.0.64"
serde_json = { version = "1.0.64", features=["preserve_order"] }

[lib]
proc-macro = true
6 changes: 3 additions & 3 deletions types-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn derive_types(input: TokenStream) -> TokenStream {
}

fn write_types(new_types: Vec<(String, serde_json::Value)>) {
let data = serde_json::to_string_pretty(&json!(new_types
let data = serde_json::to_string(&json!(new_types
.clone()
.into_iter()
.collect::<serde_json::Map<_, _>>()))
Expand Down Expand Up @@ -168,7 +168,7 @@ fn process_fields(
.collect::<Vec<_>>());
match prefix_opt {
Some(prefix) => {
let new_type_name = format!("{}Type", prefix);
let new_type_name = format!("{}", prefix);
new_types.push((new_type_name.clone(), ty));
json!(new_type_name)
}
Expand All @@ -187,7 +187,7 @@ fn process_fields(
.collect::<serde_json::Map<_, _>>());
match prefix_opt {
Some(prefix) => {
let new_type_name = format!("{}Type", prefix);
let new_type_name = format!("{}", prefix);
new_types.push((new_type_name.clone(), ty));
json!(new_type_name)
}
Expand Down
Loading

0 comments on commit b50cbd8

Please sign in to comment.