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

Auto-incremental CollectionId #11796

Merged

Conversation

Szegoo
Copy link
Contributor

@Szegoo Szegoo commented Jul 7, 2022

This PR solves the auto-incremental collections ids task from #11783

cumulus companion: paritytech/cumulus#1460

polkadot address: 126X27SbhrV19mBFawys3ovkyBS87SGfYwtwa8J2FjHrtbmA

@Szegoo
Copy link
Contributor Author

Szegoo commented Jul 8, 2022

@jsidorenko Could you please review this?

@shawntabrizi
Copy link
Member

The general stuff in this PR is correct, but we need something to handle the fact that Statemine and Statemint already have various IDs at different values taken.

I think a clean solution would be:

  • an extrinsic which is "try_increment_id" which checks if the next id already exists, and then increments the next id
  • keeping the check that ensures the same id isnt used again when creating a new asset

let me know if this makes sense.

@Szegoo
Copy link
Contributor Author

Szegoo commented Jul 11, 2022

The general stuff in this PR is correct, but we need something to handle the fact that Statemine and Statemint already have various IDs at different values taken.

I think a clean solution would be:

  • an extrinsic which is "try_increment_id" which checks if the next id already exists, and then increments the next id
  • keeping the check that ensures the same id isnt used again when creating a new asset

let me know if this makes sense.

Ok, we could probably make a try_increment_id and we increment the id until we find one that is not being used. Could that work?

@shawntabrizi
Copy link
Member

@Szegoo we should avoid loops in runtime code actually, so better it only increments once, and we call it multiple times.

This is a weight / runtime safety consideration :)

@Szegoo
Copy link
Contributor Author

Szegoo commented Jul 11, 2022

@shawntabrizi I have added try_increment_id, and this function is not used to generate the id for the next collection, but for its own. Because of this the id now starts from 1 instead of 0, so there are new changes in the tests file because of this. Also, it is important to increment the id before checking if the id is used, because this way we could get stuck, cause we could never get over that id.

@shawntabrizi
Copy link
Member

shawntabrizi commented Jul 11, 2022

@Szegoo i think you misunderstood the solution here.

there should be a completely new extrinsic which does the try_increment_id. This can be called by a user to get the pallet out of a deadlock state where a user would not be able to create a new asset with the next id, because it may already exist.

Existing logic should just make sure that the next id to be used is not already in use. If it is in use, then the whole function fails, and the user must call try_increment_id before trying to create the next asset.

@shawntabrizi
Copy link
Member

shawntabrizi commented Jul 11, 2022

Here is some pseudo code:

next_id: u32;

#[call]
fn create_asset() {
    // Make sure that the asset does not already exist before we try to create it
    // If this check fails, the whole call fails, and the user must manually increment the next_id
    ensure(!asset(next_id).exists());
    do_create_asset();
    next_id ++;
}

#[call]
fn try_increment_id() {
    // Check the next id is ALREADY used. This means we are allowed to increment the id.
    // If this check fails, then the next id is already available, and they should use `create_asset`
    ensure(asset(next_id).exists());
    next_id ++;
}

@Szegoo
Copy link
Contributor Author

Szegoo commented Jul 11, 2022

@shawntabrizi I have fixed my code, I had a different idea so I did this in a totally different way. Now it is fixed, I am not done here, but I wanted you to check if this is what you wanted. I will add more tests, and I will run a benchmark to get the weight for the try_increment_id extrinsic.

@shawntabrizi
Copy link
Member

/tip medium

@substrate-tip-bot
Copy link

@shawntabrizi A medium tip was successfully submitted for Szegoo (126X27SbhrV19mBFawys3ovkyBS87SGfYwtwa8J2FjHrtbmA on polkadot).

https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polkadot.io#/treasury/tips

@paritytech paritytech deleted a comment from substrate-tip-bot bot Jul 30, 2022
@shawntabrizi
Copy link
Member

bot merge

@paritytech-processbot
Copy link

Error: "Check reviews" status is not passing for paritytech/cumulus#1460

@Szegoo
Copy link
Contributor Author

Szegoo commented Jul 30, 2022

/tip medium

It seems like the tip bot is getting the tip :P (https://www.dotreasury.com/dot/tips)

@shawntabrizi
Copy link
Member

bot merge

@paritytech-processbot
Copy link

Error: "Check reviews" status is not passing for paritytech/cumulus#1460

@Szegoo
Copy link
Contributor Author

Szegoo commented Jul 30, 2022

@shawntabrizi Should be able to merge now, the "Check reviews" is passing on the companion.

@shawntabrizi
Copy link
Member

@Szegoo thanks for the report, should be fixed now

@shawntabrizi
Copy link
Member

/tip medium

@substrate-tip-bot
Copy link

@shawntabrizi A medium tip was successfully submitted for Szegoo (126X27SbhrV19mBFawys3ovkyBS87SGfYwtwa8J2FjHrtbmA on polkadot).

https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polkadot.io#/treasury/tips

@shawntabrizi
Copy link
Member

bot merge

@paritytech-processbot paritytech-processbot bot merged commit 7d8e5a1 into paritytech:master Jul 30, 2022
+ MaxEncodedLen
+ Copy
+ Default
+ AtLeast32BitUnsigned;
Copy link
Contributor

Choose a reason for hiding this comment

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

@jsidorenko @Szegoo This trait bound here is breaking XCMv3, as we're using a MultiLocation for the CollectionId, which isn't an unsigned integer (but contains variants that can accept unsigned ints).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KiChjang Hmm, I will look more into this, but I am not even sure if it would be possible to have this auto-increment feature since as you said the MultiLocation is used for CollectionId, and not all variants store a uint.

Copy link
Contributor

Choose a reason for hiding this comment

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

In that case, we should revert this change and really think about how exactly we should implement it. As it is, it's breaking code that was originally working without this PR.

Copy link
Member

@ggwpez ggwpez Aug 15, 2022

Choose a reason for hiding this comment

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

Looks like it is ordered, we could let the caller increment it off-chain.
PS: Not really, since the caller could set it to the highest value...

Copy link
Contributor Author

@Szegoo Szegoo Aug 15, 2022

Choose a reason for hiding this comment

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

Maybe we could for now make this feature optional for some chains that don't support XCM and don't use MultiLocation for theCollectionId.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will continue working on this in #12057

Copy link
Contributor

Choose a reason for hiding this comment

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

Super! And pls point into js/uniques-v2-main-branch afterwards

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to make sure I understand this correctly, we should add an Incrementable trait bound to the CollectionId as I suggested earlier, so this means that we are not going to store this internal ref_id field, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is how I understood it as well. @KiChjang Pls correct us if we're wrong

Copy link
Contributor

Choose a reason for hiding this comment

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

That's correct, the idea is to let runtime configuration figure out what the next ID is, instead of having it become a concern for the uniques pallet itself.

ggwpez added a commit that referenced this pull request Aug 17, 2022
paritytech-processbot bot pushed a commit that referenced this pull request Aug 19, 2022
This reverts commit 7d8e5a1.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
DaviRain-Su pushed a commit to octopus-network/substrate that referenced this pull request Aug 23, 2022
* autoincrementing CollectionId

* fix

* benchmarking fix

* fmt

* fix

* update before checking

* fmt

* fix

* fmt

* commit

* tests & fix

* fix

* commit

* docs

* safe math

* unexpose function

* benchmark

* fmt

* better naming

* fix?

* merge fixes

* fmt

* ".git/.scripts/bench-bot.sh" pallet dev pallet_uniques

* wrong weight

* Update frame/uniques/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/uniques/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* using substrate trait instead of num-traits

* remove unnecessary trait

* emit NextCollectionIdIncremented in do_create_collection

* fix in benchmarks

* check for event & group import

* docs

Co-authored-by: Sergej Sakač <sergejsakac@Sergejs-MacBook-Air.local>
Co-authored-by: command-bot <>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@Szegoo Szegoo mentioned this pull request Sep 8, 2022
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* autoincrementing CollectionId

* fix

* benchmarking fix

* fmt

* fix

* update before checking

* fmt

* fix

* fmt

* commit

* tests & fix

* fix

* commit

* docs

* safe math

* unexpose function

* benchmark

* fmt

* better naming

* fix?

* merge fixes

* fmt

* ".git/.scripts/bench-bot.sh" pallet dev pallet_uniques

* wrong weight

* Update frame/uniques/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/uniques/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* using substrate trait instead of num-traits

* remove unnecessary trait

* emit NextCollectionIdIncremented in do_create_collection

* fix in benchmarks

* check for event & group import

* docs

Co-authored-by: Sergej Sakač <sergejsakac@Sergejs-MacBook-Air.local>
Co-authored-by: command-bot <>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
…#12059)

This reverts commit 7d8e5a1.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C1-low PR touches the given topic and has a low impact on builders. D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants