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

Commit

Permalink
Merge pull request #89 from sunshine-protocol/clight-light-work
Browse files Browse the repository at this point in the history
slight client light work
  • Loading branch information
4meta5 authored Jun 16, 2020
2 parents d8386c1 + 69763b2 commit c1ddd78
Show file tree
Hide file tree
Showing 21 changed files with 1,264 additions and 1,069 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ cache/
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
client/tmp/

.DS_Store
12 changes: 8 additions & 4 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,31 @@ readme = "README.md"
description = "Submit extrinsics (transactions) to the sunshine node via RPC"
keywords = ["sunshine", "substrate", "blockchain"]

[features]
light-client = ["substrate-subxt-light-client", "sunshine-node"]

[dependencies]
async-std = "=1.5.0"
ipld-block-builder = "0.3.0"
libipld = { version = "0.3.0", features = ["dag-json"] }
serde = { version = "1.0.111", features = ["derive"] }
# TODO export error in libipld
sled = "0.32.0-rc1"
serde_json = "1.0.53"
ipfs-embed = "0.1.0"
# point at git master, could be done with patch.crates.io = giturl
substrate-subxt = "0.8.0"
surf = "1.0.3"
thiserror = "1.0.19"
utils-identity = { version = "0.1.0", path = "../utils" }
# substrate
sp-runtime = { version = "2.0.0-rc3", default-features = false }
sp-core = { version = "2.0.0-rc3", default-features = false }
codec = { version = "1.3.0", package = "parity-scale-codec" }
frame-support = "2.0.0-rc3"
pallet-indices = "2.0.0-rc3"
derive-new = {version = "0.5", default-features=false}
substrate-subxt-light-client = { package = "substrate-subxt", git = "https://github.com/dvc94ch/substrate-subxt", branch = "lightclient", optional = true }
sunshine-node = { path = "../node", default-features = false, optional = true }
keystore = {package = "keybase-keystore", git = "https://github.com/sunshine-protocol/substrate-identity"}
# local deps
utils-identity = { version = "0.1.0", path = "../utils" }
util = { package = "sunshine-util", path = "../modules/util", default-features = false }
org = {package = "sunshine-org", path = "../modules/org", default-features=false }
vote = { package = "sunshine-vote", path = "../modules/vote", default-features=false}
Expand Down
42 changes: 42 additions & 0 deletions client/examples/org.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use sp_keyring::AccountKeyring;
//#[cfg(feature = "light-client")]
//use sunshine_client::ChainType;
use ipfs_embed::{Config, Store};
use ipld_block_builder::{BlockBuilder, Codec};
use keystore::{DeviceKey, KeyStore, Password};
use sp_core::crypto::Pair;
use sunshine_client::{ClientBuilder, Error, Runtime, SunClient};
// use libipld::cid::{Cid, Codec};
// use libipld::multihash::Sha2_256;
// use utils_identity::cid::CidBytes;

#[async_std::main]
async fn main() -> Result<(), Error> {
env_logger::init();
let subxt = ClientBuilder::new().build().await.unwrap();
let db = sled::open("tmp/db")?;
let ipld_tree = db.open_tree("ipld_tree")?;
let config = Config::from_tree(ipld_tree);
let store = Store::new(config)?;
let codec = Codec::new();
let ipld = BlockBuilder::new(store, codec);
let keystore = KeyStore::new("/tmp/keystore");
let alice_seed: [u8; 32] = AccountKeyring::Alice.into();
let _ = keystore.initialize(
&DeviceKey::from_seed(alice_seed),
&Password::from("password".to_string()),
)?;
// //#[cfg(not(feature = "light-client"))]
let client = SunClient::new(subxt, keystore, ipld);
// #[cfg(feature = "light-client")]
// let client = Sunshine::new("/tmp/db", signer, ChainType::Development).await?;
let account_id = sp_keyring::AccountKeyring::Alice.to_account_id();
client.issue_shares(1u64, account_id, 10u64).await?;

// println!(
// "Account {:?} was issued {:?} shares for organization {:?}",
// event.who, event.shares, event.organization,
// );

Ok(())
}
23 changes: 0 additions & 23 deletions client/examples/reserve_shares_and_watch.rs

This file was deleted.

6 changes: 6 additions & 0 deletions client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ pub enum Error {
Sled(#[from] sled::Error),
#[error("{0}")]
Ipfs(#[from] ipfs_embed::Error),
#[error(transparent)]
Keystore(#[from] keystore::Error),
#[error("keystore already initialized")]
KeystoreInitialized,
#[error("event not found")]
EventNotFound,
}

pub type Result<T> = core::result::Result<T, Error>;
6 changes: 5 additions & 1 deletion client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#[macro_use]
extern crate substrate_subxt;

#[macro_use]
extern crate derive_new;

mod error;
#[cfg(feature = "light-client")]
mod light_client;
Expand All @@ -13,4 +16,5 @@ mod sunshine;
pub use error::Error;
#[cfg(feature = "light-client")]
pub use light_client::ChainType;
pub use sunshine::Sunshine;
pub use runtime::{ClientBuilder, Runtime};
pub use sunshine::SunClient;
14 changes: 10 additions & 4 deletions client/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ use sp_runtime::{
use std::marker::PhantomData;
use substrate_subxt::{
balances::{AccountData, Balances},
system::System,
CheckEra, CheckGenesis, CheckNonce, CheckSpecVersion, CheckTxVersion, CheckWeight, SignedExtra,
system::System, //sp_core::crypto::Pair,
CheckEra,
CheckGenesis,
CheckNonce,
CheckSpecVersion,
CheckTxVersion,
CheckWeight,
SignedExtra,
};
use utils_identity::cid::CidBytes;

pub type Pair = sp_core::sr25519::Pair;
pub type ClientBuilder = substrate_subxt::ClientBuilder<Runtime, MultiSignature, RuntimeExtra>;
pub type Client = substrate_subxt::Client<Runtime, MultiSignature, RuntimeExtra>;
//pub type XtBuilder = substrate_subxt::XtBuilder<Runtime, Pair, MultiSignature, RuntimeExtra>;
pub type PairSigner = substrate_subxt::PairSigner<Runtime, MultiSignature, RuntimeExtra, Pair>;

/// Concrete type definitions compatible w/ sunshine's runtime aka `suntime`
#[derive(Debug, Clone, Eq, PartialEq)]
Expand All @@ -30,7 +36,7 @@ impl System for Runtime {
type Hash = sp_core::H256;
type Hashing = BlakeTwo256;
type AccountId = <<MultiSignature as Verify>::Signer as IdentifyAccount>::AccountId;
type Address = pallet_indices::address::Address<Self::AccountId, u32>;
type Address = pallet_indices::address::Address<Self::AccountId, u64>;
type Header = Header<Self::BlockNumber, BlakeTwo256>;
type Extrinsic = OpaqueExtrinsic;
type AccountData = AccountData<<Self as Balances>::Balance>;
Expand Down
224 changes: 224 additions & 0 deletions client/src/srml/bank.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
use crate::srml::{
org::{Org, OrgEventsDecoder},
vote::{Vote, VoteEventsDecoder},
};
use codec::{Codec, Decode, Encode};
use frame_support::Parameter;
use sp_runtime::traits::{AtLeast32Bit, MaybeSerializeDeserialize, Member, Zero};
use std::fmt::Debug;
use substrate_subxt::system::{System, SystemEventsDecoder};
use util::bank::{
BankState, DepositInfo, InternalTransferInfo, OnChainTreasuryID, ReservationInfo,
};

pub type BalanceOf<T> = <T as Bank>::Currency; // as Currency<<T as System>::AccountId>>::Balance;

/// The subset of the bank trait and its inherited traits that the client must inherit
#[module]
pub trait Bank: System + Org + Vote {
/// Identifier for bank-related maps
type BankAssociatedId: Parameter
+ Member
+ AtLeast32Bit
+ Codec
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ Debug
+ PartialOrd
+ PartialEq
+ Zero;

/// The currency type for on-chain transactions
type Currency: Parameter
+ Member
+ AtLeast32Bit
+ Codec
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ Debug
+ PartialOrd
+ PartialEq
+ Zero; // + Currency<<Self as System>::AccountId> // commented out until #93 is resolved
}

// ~~ Values (Constants) ~~

#[derive(Clone, Debug, Eq, PartialEq, Encode)]
pub struct MinimumInitialDepositStore<T: Bank> {
pub amount: BalanceOf<T>,
}

// ~~ Maps ~~

#[derive(Clone, Debug, Eq, PartialEq, Store, Encode)]
pub struct BankStoresStore<T: Bank> {
#[store(returns = BankState<<T as Org>::OrgId, BalanceOf<T>>)]
pub id: OnChainTreasuryID,
phantom: std::marker::PhantomData<T>,
}

#[derive(Clone, Debug, Eq, PartialEq, Store, Encode)]
pub struct DepositsStore<T: Bank> {
#[store(returns = DepositInfo<<T as System>::AccountId, <T as Org>::IpfsReference, BalanceOf<T>>)]
pub bank_id: OnChainTreasuryID,
pub deposit_id: T::BankAssociatedId,
}

#[derive(Clone, Debug, Eq, PartialEq, Store, Encode)]
pub struct SpendReservationsStore<T: Bank> {
#[store(returns = ReservationInfo<<T as Org>::IpfsReference, BalanceOf<T>, <T as Org>::OrgId>)]
pub bank_id: OnChainTreasuryID,
pub reservation_id: T::BankAssociatedId,
}

#[derive(Clone, Debug, Eq, PartialEq, Store, Encode)]
pub struct InternalTransfersStore<T: Bank> {
#[store(returns = InternalTransferInfo<T::BankAssociatedId, <T as Org>::IpfsReference, BalanceOf<T>, <T as Org>::OrgId>)]
pub bank_id: OnChainTreasuryID,
pub transfer_id: T::BankAssociatedId,
}

// ~~ (Calls, Events) ~~

#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct DepositFromSignerForBankAccountCall<T: Bank> {
pub bank_id: OnChainTreasuryID,
pub amount: BalanceOf<T>,
pub reason: <T as Org>::IpfsReference,
}

#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct CapitalDepositedIntoOnChainBankAccountEvent<T: Bank> {
pub depositer: <T as System>::AccountId,
pub bank_id: OnChainTreasuryID,
pub amount: BalanceOf<T>,
pub reason: <T as Org>::IpfsReference,
}

#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct RegisterAndSeedForBankAccountCall<T: Bank> {
pub seed: BalanceOf<T>,
pub hosting_org: <T as Org>::OrgId,
pub bank_operator: Option<<T as Org>::OrgId>,
}

#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct RegisteredNewOnChainBankEvent<T: Bank> {
pub seeder: <T as System>::AccountId,
pub new_bank_id: OnChainTreasuryID,
pub seed: BalanceOf<T>,
pub hosting_org: <T as Org>::OrgId,
pub bank_operator: Option<<T as Org>::OrgId>,
}

#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct ReserveSpendForBankAccountCall<T: Bank> {
pub bank_id: OnChainTreasuryID,
pub reason: <T as Org>::IpfsReference,
pub amount: BalanceOf<T>,
pub controller: <T as Org>::OrgId,
}

#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct SpendReservedForBankAccountEvent<T: Bank> {
pub bank_id: OnChainTreasuryID,
pub new_reservation_id: T::BankAssociatedId,
pub reason: <T as Org>::IpfsReference,
pub amount: BalanceOf<T>,
pub controller: <T as Org>::OrgId,
}

#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct CommitReserveSpendForTransferInsideBankAccountCall<T: Bank> {
pub bank_id: OnChainTreasuryID,
pub reservation_id: T::BankAssociatedId,
pub reason: <T as Org>::IpfsReference,
pub amount: BalanceOf<T>,
}

#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct CommitSpendBeforeInternalTransferEvent<T: Bank> {
pub committer: <T as System>::AccountId,
pub bank_id: OnChainTreasuryID,
pub reservation_id: T::BankAssociatedId,
pub amount: BalanceOf<T>,
}

#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct UnreserveUncommittedReservationToMakeFreeCall<T: Bank> {
pub bank_id: OnChainTreasuryID,
pub reservation_id: T::BankAssociatedId,
pub amount: BalanceOf<T>,
}

#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct UnreserveUncommittedReservationToMakeFreeEvent<T: Bank> {
pub qualified_bank_controller: <T as System>::AccountId,
pub bank_id: OnChainTreasuryID,
pub reservation_id: T::BankAssociatedId,
pub amount: BalanceOf<T>,
}

#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct UnreserveCommittedReservationToMakeFreeCall<T: Bank> {
pub bank_id: OnChainTreasuryID,
pub reservation_id: T::BankAssociatedId,
pub amount: BalanceOf<T>,
}

#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct UnreserveCommittedReservationToMakeFreeEvent<T: Bank> {
pub qualified_spend_reservation_controller: <T as System>::AccountId,
pub bank_id: OnChainTreasuryID,
pub reservation_id: T::BankAssociatedId,
pub amount: BalanceOf<T>,
}

#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct TransferSpendingPowerForSpendCommitmentCall<T: Bank> {
pub bank_id: OnChainTreasuryID,
pub reason: <T as Org>::IpfsReference,
pub reservation_id: T::BankAssociatedId,
pub amount: BalanceOf<T>,
pub committed_controller: <T as Org>::OrgId,
}

#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct InternalTransferExecutedAndSpendingPowerDoledOutToControllerEvent<T: Bank> {
pub qualified_spend_reservation_controller: <T as System>::AccountId,
pub bank_id: OnChainTreasuryID,
pub reason: <T as Org>::IpfsReference,
pub reservation_id: T::BankAssociatedId,
pub amount: BalanceOf<T>,
pub committed_controller: <T as Org>::OrgId,
}

#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct WithdrawByReferencingInternalTransferCall<T: Bank> {
pub bank_id: OnChainTreasuryID,
pub transfer_id: T::BankAssociatedId,
pub amount: BalanceOf<T>,
}

#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct SpendRequestForInternalTransferApprovedAndExecutedEvent<T: Bank> {
pub bank_id: OnChainTreasuryID,
pub requester: <T as System>::AccountId,
pub amount: BalanceOf<T>,
pub transfer_id: T::BankAssociatedId,
}

#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct BurnAllSharesToLeaveWeightedMembershipBankAndWithdrawRelatedFreeCapitalCall<T: Bank> {
pub bank_id: OnChainTreasuryID,
phantom: std::marker::PhantomData<T>,
}

#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct AccountLeftMembershipAndWithdrewProportionOfFreeCapitalInBankEvent<T: Bank> {
pub bank_id: OnChainTreasuryID,
pub leaving_member: <T as System>::AccountId,
pub amount_withdrawn_by_burning_shares: BalanceOf<T>,
}
Loading

0 comments on commit c1ddd78

Please sign in to comment.