Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double map and plain storage support, introduce macros #93

Merged
merged 14 commits into from
Apr 28, 2020

Conversation

dvc94ch
Copy link
Contributor

@dvc94ch dvc94ch commented Apr 21, 2020

Simplifies trait bounds.
Allows setting a custom client.
Adds support for plain and double-map storage items.

Copy link
Contributor

@ascjones ascjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff! Could you add a couple of tests for the double map and the plain storage?

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Apr 22, 2020

I added a proc-macro to generate client code for substrate modules. I'll look into generating test cases too which should fix your comment.

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Apr 25, 2020

Sorry, this PR has gotten a little out of hand. It still needs more field testing, and the substrate-subxt crate should probably be split into substrate-subxt-core and substrate-subxt, so the proc macros can be used for the default modules.

#[module]
pub trait Balances: System {
    type Balance: Parameter
        + Member
        + AtLeast32Bit
        + codec::Codec
        + Default
        + Copy
        + MaybeSerialize
        + Debug
        + From<<Self as System>::BlockNumber>;
}

#[derive(Clone, Decode, Default)]
pub struct AccountData<Balance> {
    pub free: Balance,
    pub reserved: Balance,
    pub misc_frozen: Balance,
    pub fee_frozen: Balance,
}

#[derive(Encode, Store)]
pub struct AccountStore<'a, T: Balances> {
    #[store(returns = AccountData<T::Balance>)]
    pub account_id: &'a <T as System>::AccountId,
}

#[derive(Call, Encode)]
pub struct TransferCall<'a, T: Balances> {
    pub to: &'a <T as System>::Address,
    #[codec(compact)]
    pub amount: T::Balance,
}

#[derive(Debug, Decode, Eq, Event, PartialEq)]
pub struct TransferEvent<T: Balances> {
    pub from: <T as System>::AccountId,
    pub to: <T as System>::AccountId,
    pub amount: T::Balance,
}

impl Balances for KusamaRuntime {
    type Balance = u128;
}

subxt_test!({
    name: transfer_test_case,
    runtime: KusamaRuntime,
    account: Alice,
    step: {
        state: {
            alice: AccountStore { account_id: &alice },
            bob: AccountStore { account_id: &bob },
        },
        call: TransferCall {
            to: &bob,
            amount: 10_000,
        },
        event: TransferEvent {
            from: alice.clone(),
            to: bob.clone(),
            amount: 10_000,
        },
        assert: {
            assert_eq!(pre.alice.free, post.alice.free - 10_000);
            assert_eq!(pre.bob.free, post.bob.free + 10_000);
        },
    },
});

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Apr 26, 2020

Mmh one problem is that the nonces are wrong when running multiple tests in parallel. Is it possible to disable nonce checking on dev chains?

@4meta5
Copy link
Contributor

4meta5 commented Apr 26, 2020

@dvc94ch
Copy link
Contributor Author

dvc94ch commented Apr 26, 2020

Due to possible races it looks like it's the only way to do it. If we shared the client and set the account based on the thread id, arbitrary interleaving of tests will cause failures.

@dvc94ch dvc94ch requested a review from ascjones April 27, 2020 09:55
Copy link
Contributor

@ascjones ascjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the proc macros. Though still need a test case for double_map which is the ostensible purpose of this PR 😄

proc-macro/src/utils.rs Outdated Show resolved Hide resolved
@dvc94ch
Copy link
Contributor Author

dvc94ch commented Apr 28, 2020

Is it ok if I just add a test for plain? The system, balances and contracts modules don't contain any double_maps. I can follow up with splitting subxt into a core module and update system, balances and contracts to use the proc-macros. the generic-asset module contains some double_map's, I could add support for it.

Copy link
Contributor

@ascjones ascjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ascjones ascjones changed the title Double map support Double map and plain storage support, introduce macros Apr 28, 2020
@ascjones ascjones merged commit 6f27489 into paritytech:master Apr 28, 2020
@ascjones ascjones mentioned this pull request May 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants