-
Notifications
You must be signed in to change notification settings - Fork 28
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
feat: add asCallsOnlyArg to reduce size of metadata #149
Changes from 15 commits
4a07395
3150687
4e54587
145e655
0545ca8
8bad72d
3634807
5dbafa8
6c1e60b
b52c455
2ecba0d
7fe2d64
b355a06
17a8806
cc3ad6f
72b44fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Metadata } from '@polkadot/types'; | ||
import { MetadataVersioned } from '@polkadot/types/metadata/MetadataVersioned'; | ||
|
||
import { polkadotV9122MetadataHex } from '../../test-helpers/metadata/polkadotV9122MetadataHex'; | ||
import { polkadotRegistryV9122 } from '../../test-helpers/registries'; | ||
import { createMetadata, createMetadataUnmemoized } from './createMetadata'; | ||
|
||
describe('createMetadata', () => { | ||
const unmemoizedMetadata: Metadata = createMetadataUnmemoized( | ||
polkadotRegistryV9122, | ||
polkadotV9122MetadataHex | ||
); | ||
const unmemoizedMetadataAsCalls: MetadataVersioned = createMetadataUnmemoized( | ||
polkadotRegistryV9122, | ||
polkadotV9122MetadataHex, | ||
true | ||
); | ||
const memoizedMetadata: Metadata = createMetadata( | ||
polkadotRegistryV9122, | ||
polkadotV9122MetadataHex | ||
); | ||
const memoizedMetadataAsCalls: MetadataVersioned = createMetadata( | ||
polkadotRegistryV9122, | ||
polkadotV9122MetadataHex, | ||
true | ||
); | ||
|
||
it('Metadata should decrease in byte size when `asCallsOnlyArg` is true with `createMetadataUnmemoized`', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the curious the reason we are testing both the unmemoized and memoized |
||
const metadataBuffer = Buffer.from(unmemoizedMetadata.toHex(), 'utf-8'); | ||
const metadataAsCallsBuffer = Buffer.from( | ||
unmemoizedMetadataAsCalls.toHex(), | ||
'utf-8' | ||
); | ||
|
||
expect(metadataAsCallsBuffer.byteLength).toBeGreaterThan(0); | ||
expect(metadataBuffer.byteLength).toBeGreaterThan( | ||
metadataAsCallsBuffer.byteLength | ||
); | ||
}); | ||
|
||
it('Metadata should decrease in byte size when `asCallsOnlyArg` is true with `createMetadata`', () => { | ||
const metadataBuffer = Buffer.from(memoizedMetadata.toHex(), 'utf-8'); | ||
const metadataAsCallsBuffer = Buffer.from( | ||
memoizedMetadataAsCalls.toHex(), | ||
'utf-8' | ||
); | ||
|
||
expect(metadataAsCallsBuffer.byteLength).toBeGreaterThan(0); | ||
expect(metadataBuffer.byteLength).toBeGreaterThan( | ||
metadataAsCallsBuffer.byteLength | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { Metadata } from '@polkadot/types'; | ||
import { TypeRegistry } from '@polkadot/types'; | ||
import { MetadataVersioned } from '@polkadot/types/metadata/MetadataVersioned'; | ||
import memoizee from 'memoizee'; | ||
|
||
/** | ||
|
@@ -10,12 +11,15 @@ import memoizee from 'memoizee'; | |
* @ignore | ||
* @param registry - The registry of the metadata. | ||
* @param metadata - The metadata as hex string. | ||
* @param asCallsOnlyArg - Option to decreases the metadata to calls only | ||
*/ | ||
function createMetadataUnmemoized( | ||
export function createMetadataUnmemoized( | ||
registry: TypeRegistry, | ||
metadataRpc: string | ||
): Metadata { | ||
return new Metadata(registry, metadataRpc); | ||
metadataRpc: string, | ||
asCallsOnlyArg = false | ||
): Metadata | MetadataVersioned { | ||
const metadata = new Metadata(registry, metadataRpc); | ||
return asCallsOnlyArg ? metadata.asCallsOnly : metadata; | ||
} | ||
|
||
/** | ||
|
@@ -26,7 +30,8 @@ function createMetadataUnmemoized( | |
* @ignore | ||
* @param registry - The registry of the metadata. | ||
* @param metadata - The metadata as hex string. | ||
* @param asCallsOnlyArg - Option to decreases the metadata to calls only | ||
*/ | ||
export const createMetadata = memoizee(createMetadataUnmemoized, { | ||
length: 2, | ||
length: 3, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. briefly reading over the docs (https://github.com/medikoo/memoizee#arguments-length), I wonder if this would work: "Dynamic length behavior can be forced by setting length to false, that means memoize will work with any number of arguments." I am also a little confused how default args work in terms of length because of this note "Parameters predefined with default values (ES2015+ feature) are not reflected in function's length, therefore if you want to memoize them as well, you need to tweak length setting accordingly". Either way though sounds like this works, so maybe not worth looking into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It works with both |
||
}); |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './polkadotRegistry'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { getRegistryPolkadot } from '../getRegistryPolkadot'; | ||
import { polkadotV9122MetadataHex } from '../metadata/polkadotV9122MetadataHex'; | ||
|
||
/** | ||
* Polkadot v9122 TypeRegistry | ||
*/ | ||
export const polkadotRegistryV9122 = getRegistryPolkadot( | ||
9122, | ||
polkadotV9122MetadataHex | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,26 @@ async function main(): Promise<void> { | |
'state_getRuntimeVersion' | ||
); | ||
|
||
// Create Polkadot's type registry. | ||
/** | ||
* Create Polkadot's type registry. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @emostov This is what i wrote for the examples. Let me know if you think there should be more than this. |
||
* | ||
* When creating a type registry, it accepts a `asCallsOnlyArg` option which | ||
* defaults to false. When true this will minimize the size of the metadata | ||
* to only include the calls. This removes storage, events, etc. This will | ||
* ultimately decrease the size of the unsigned transaction. | ||
* | ||
* Example: | ||
* | ||
* ``` | ||
* const registry = getRegistry({ | ||
* chainName: 'Polkadot', | ||
* specName, | ||
* specVersion, | ||
* metadataRpc, | ||
* asCallsOnlyArg: true, | ||
* }); | ||
* ``` | ||
*/ | ||
const registry = getRegistry({ | ||
chainName: 'Polkadot', | ||
specName, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,5 +67,5 @@ export function signWith( | |
}) | ||
.sign(pair); | ||
|
||
return signature; | ||
return signature as unknown as `0x${string}`; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit/Question: Why the suffix "Arg"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea being that
asCallsOnly
is the name of the getter for the metadata in polkadot-js so I wanted to make it have a separate name to reduce confusion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation!