use-ink / contracts-ui

Web application for deploying wasm smart contracts on Substrate chains that include the FRAME contracts pallet
https://contracts-ui.substrate.io/
GNU General Public License v3.0
61 stars 45 forks source link

refactoring: unique constraint for `contract.address` and `codeBundle.codeHash` #437

Closed peetzweg closed 1 year ago

peetzweg commented 1 year ago

Adds unique constraint(&) to contract.address and codeBundle.codeHash to prevent duplicate inside these tables by the actual db schema.

We could potentially change this schema still to use the address/codeHash as the primary key instead of an autoincremented number.

Happy to discuss if we want to go with a change like this or leave the database as is.

netlify[bot] commented 1 year ago

Deploy Preview for contracts-ui ready!

Name Link
Latest commit 7bb1cca03ea7c73bf366f0f03046e3002621baf8
Latest deploy log https://app.netlify.com/sites/contracts-ui/deploys/641892fbaff6e700071a5f79
Deploy Preview https://deploy-preview-437--contracts-ui.netlify.app/
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

peetzweg commented 1 year ago

@statictype

In order to test this a bit I've added this function to the db/index.ts. It uses some fixtures I've added to the branch as well to add contracts and code bundles in the style of v1 into a fresh indexeddb. This populate will only run if there is no database available.

https://github.com/paritytech/contracts-ui/blob/7bb1cca03ea7c73bf366f0f03046e3002621baf8/src/db/index.ts#L78-L92

So to test you should comment out version(2) and version(3) and add the populate line directly under version(1) in the constructor. So it looks like this:

constructor(genesisHash: string) {
    super(`contracts-ui__${genesisHash}`);

    this.version(1).stores({
      codeBundles: '++id, codeHash, name, date',
      contracts: '++id, address, codeHash, name, date',
    });
    this._populateWithMockDataV1()
    // version 2 & 3 not executing.
}

Than drop all your indexedDB databases and refresh. It should restore the fixtures as a fresh database needs to be created. Check the contents of the database it contains duplicates of contracts and codeBundles now.

After that you can remove the this._populateWithMockDataV1() line again and comment back in the version 2 & 3 to execute the migration on your next visit.

A version 3 is needed to just add the unique constraint on the cleaned up data from the version 2 migration. I wasn't able to achieve this in a single migration, dexie crashed on this.

statictype commented 1 year ago

will close this for now due to limited availability