forked from polkadot-js/api
-
Notifications
You must be signed in to change notification settings - Fork 0
Passing the era #2
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
Open
KarishmaBothara
wants to merge
19
commits into
master
Choose a base branch
from
passing_the_era
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
67625a0
Initial changes for ERA
KarishmaBothara 80f587d
WIP
KarishmaBothara 9fb26b5
WIP
KarishmaBothara 80f1486
Merge branch 'master' of https://github.com/polkadot-js/api into pass…
KarishmaBothara 6f7efd4
WIP
KarishmaBothara c5130df
Era changes
KarishmaBothara a500fd1
as per review comments
KarishmaBothara c36cb9d
WIP
KarishmaBothara eaf0f3e
Merge branch 'master' of https://github.com/polkadot-js/api into pass…
KarishmaBothara bb443f6
Extrinsic ERA changes
KarishmaBothara 49139f7
As per code review
KarishmaBothara f654f3c
ERA changes
KarishmaBothara db2b56e
as per review
KarishmaBothara b32ef44
as per review comment
KarishmaBothara 14e9825
as per review comments
KarishmaBothara 60d88ef
as per review comment
KarishmaBothara 09d5c25
as per review
KarishmaBothara 1a3931d
review comment
KarishmaBothara 48da3e8
fix
KarishmaBothara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright 2017-2019 @polkadot/types authors & contributors | ||
| // This software may be modified and distributed under the terms | ||
| // of the Apache-2.0 license. See the LICENSE file for details. | ||
|
|
||
| import ExtrinsicEra from './ExtrinsicEra'; | ||
|
|
||
| describe('ExtrinsicEra', () => { | ||
|
|
||
| it('decodes an Extrinsic Era with immortal', () => { | ||
| const extrinsicEra = new ExtrinsicEra(new Uint8Array([0])); | ||
|
|
||
| expect(extrinsicEra.asImmortalEra).toBeDefined(); | ||
| }); | ||
|
|
||
| it('decodes an Extrinsic Era from u8 as mortal', () => { | ||
| const extrinsicEra = new ExtrinsicEra(new Uint8Array([1, 78, 156])); | ||
|
|
||
| expect(extrinsicEra.asMortalEra.period.toNumber()).toEqual(32768); | ||
| expect(extrinsicEra.asMortalEra.phase.toNumber()).toEqual(20000); | ||
| }); | ||
|
|
||
| it('encode an Extrinsic Era from Object with blocknumber & period as mortal instance', () => { | ||
| const mortalIndex = 1; | ||
| const extrinsicEra = new ExtrinsicEra({ current: 1400, period: 200 }, mortalIndex); | ||
|
|
||
| expect(extrinsicEra.asMortalEra.period.toNumber()).toEqual(256); | ||
| expect(extrinsicEra.asMortalEra.phase.toNumber()).toEqual(120); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,33 +2,180 @@ | |
| // This software may be modified and distributed under the terms | ||
| // of the Apache-2.0 license. See the LICENSE file for details. | ||
|
|
||
| import EnumType from '../codec/EnumType'; | ||
| import { isHex, isU8a, hexToU8a, isObject, assert, u8aToBn } from '@polkadot/util'; | ||
| import Tuple from '../codec/Tuple'; | ||
| import U8 from '../primitive/U8'; | ||
| import { AnyU8a } from '../types'; | ||
|
|
||
| import { u8aToU8a } from '@polkadot/util'; | ||
|
|
||
| import U8a from '../codec/U8a'; | ||
|
|
||
| /** | ||
| * @name ExtrinsicEra | ||
| * @description | ||
| * The era for an extrinsic, indicating either a mortal or immortal extrinsic | ||
| */ | ||
| export default class ExtrinsicEra extends U8a { | ||
| export default class ExtrinsicEra extends EnumType<ImmortalEra | MortalEra> { | ||
| constructor (value?: any, index?: number) { | ||
| super({ ImmortalEra, MortalEra }, value, index); | ||
| } | ||
|
|
||
| /** | ||
| * @description Returns the item as a [[ImmortalEra]] | ||
| */ | ||
| get asImmortalEra (): ImmortalEra { | ||
| assert(this.isImmortalEra, `Cannot convert '${this.type}' via asImmortalEra`); | ||
| return this.value as ImmortalEra; | ||
| } | ||
|
|
||
| /** | ||
| * @description Returns the item as a [[MortalEra]] | ||
| */ | ||
| get asMortalEra (): MortalEra { | ||
| assert(this.isMortalEra, `Cannot convert '${this.type}' via asMortalEra`); | ||
| return this.value as MortalEra; | ||
| } | ||
|
|
||
| /** | ||
| * @description `true` if Immortal | ||
| */ | ||
| get isImmortalEra (): boolean { | ||
| return this.index === 0; | ||
| } | ||
|
|
||
| /** | ||
| * @description `true` if Mortal | ||
| */ | ||
| get isMortalEra (): boolean { | ||
| return this.index === 1; | ||
| } | ||
|
|
||
| /** | ||
| * @description Encodes the value as a Uint8Array as per the parity-codec specifications | ||
| * @param isBare true when the value has none of the type-specific prefixes (internal) | ||
| */ | ||
| toU8a (isBare?: boolean): Uint8Array { | ||
| if (this.index === 0) { | ||
| return super.toU8a(); | ||
| } else { | ||
| return this.asMortalEra.toU8a(isBare); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const VALID_IMMORTAL = new U8a([]); | ||
| /** | ||
| * @name ImmortalEra | ||
| * @description | ||
| * The ImmortalEra for an extrinsic | ||
| */ | ||
| export class ImmortalEra extends U8a { | ||
| constructor (value?: AnyU8a) { | ||
| super( | ||
| ExtrinsicEra.decodeExtrinsicEra(value) | ||
| ); | ||
| super(value); | ||
KarishmaBothara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| assert(this.eq(VALID_IMMORTAL), `IMMORTAL: expected ${VALID_IMMORTAL.toHex()}, found ${this.toHex()}`); | ||
|
Collaborator
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. can you try |
||
| } | ||
| } | ||
|
|
||
| export type MortalEraValue = [U8, U8]; | ||
|
|
||
| static decodeExtrinsicEra (value?: AnyU8a): Uint8Array { | ||
| if (value) { | ||
| const u8a = u8aToU8a(value); | ||
| interface MortalMethod { | ||
| current: number; | ||
| period: number; | ||
| } | ||
|
|
||
| /** | ||
| * @name MortalEra | ||
| * @description | ||
| * The MortalEra for an extrinsic, indicating period and phase | ||
| */ | ||
| export class MortalEra extends Tuple { | ||
ianhe8x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| constructor (value?: any) { | ||
| super({ | ||
| period : U8, phase : U8 | ||
| }, MortalEra.decodeMortalEra(value)); | ||
| } | ||
|
|
||
| // If we have a zero byte, it is immortal (1 byte in length), otherwise we have | ||
| // the era details following as another byte | ||
| return u8a.subarray(0, (u8a[0] === 0) ? 1 : 2); | ||
| private static decodeMortalEra (value: MortalMethod | Uint8Array | string): MortalEraValue { | ||
| if (isHex(value)) { | ||
| return MortalEra.decodeMortalEra(hexToU8a(value.toString())); | ||
| } else if (isU8a(value)) { | ||
| const first = u8aToBn(value.subarray(0, 1)).toNumber(); | ||
| let second = u8aToBn(value.subarray(1, 2)).toNumber(); | ||
| const encoded: number = first + (second << 8); | ||
| const period = 2 << (encoded % (1 << 4)); | ||
| const quantizeFactor = Math.max(period >> 12, 1); | ||
| let phase = (encoded >> 4) * quantizeFactor; | ||
| if (period >= 4 && phase < period) { | ||
| return [new U8(period), new U8(phase)]; | ||
| } | ||
| throw new Error('Invalid data passed to Mortal era'); | ||
| } else if (isObject(value)) { | ||
| const { current } = value; | ||
| const { period } = value; | ||
| let calPeriod = Math.pow(2, Math.ceil(Math.log2(period))); | ||
| calPeriod = Math.min(Math.max(calPeriod, 4), 1 << 16); | ||
| const phase = current % calPeriod; | ||
| const quantizeFactor = Math.max(calPeriod >> 12, 1); | ||
| const quantizedPhase = phase / quantizeFactor * quantizeFactor; | ||
| return [new U8(calPeriod), new U8(quantizedPhase)]; | ||
| } | ||
| throw new Error('Invalid data passed to Mortal era'); | ||
| } | ||
| /** | ||
| * @description The period of this Mortal wraps as a [[U8]] | ||
| */ | ||
| get period (): U8 { | ||
| return this[0] as U8; | ||
| } | ||
|
|
||
| return new Uint8Array([0]); | ||
| /** | ||
| * @description The phase of this Mortal wraps as a [[U8]] | ||
| */ | ||
| get phase (): U8 { | ||
| return this[1] as U8; | ||
| } | ||
|
|
||
| /** | ||
| * @description Encodes the value as a Uint8Array as per the parity-codec specifications | ||
| * @param isBare true when the value has none of the type-specific prefixes (internal) | ||
| */ | ||
| toU8a (isBare?: boolean): Uint8Array { | ||
| const period = this.period.toNumber(); | ||
| const phase = this.phase.toNumber(); | ||
| const quantizeFactor = Math.max(period >> 12, 1); | ||
| const trailingZeros = this.getTrailingZeros(period); | ||
| const encoded = Math.min(15, Math.max(1, trailingZeros - 1)) + (((phase / quantizeFactor) << 4)); | ||
| const first = encoded >> 8; | ||
| const second = encoded & 0xff; | ||
| return new Uint8Array([second, first]); | ||
| } | ||
|
|
||
| /** | ||
| * @description Get the block number of the start of the era whose properties this object describes that `current` belongs to. | ||
| */ | ||
| birth (current: number) { | ||
| return Math.floor((Math.max(current,this.phase.toNumber()) - this.phase.toNumber()) / this.period.toNumber()) * this.period.toNumber() + this.phase.toNumber(); | ||
| } | ||
|
|
||
| /** | ||
| * @description Get the block number of the first block at which the era has ended. | ||
| */ | ||
| death (current: number) { | ||
| return this.birth(current) + this.period.toNumber(); | ||
| } | ||
|
|
||
| /** | ||
| * @description convert the number to binary and get the trailing zero's. | ||
| */ | ||
| private getTrailingZeros (period: number) { | ||
| const zeros: number[] = []; | ||
| let periodN = period; | ||
| periodN = parseInt(periodN.toString(2), 10); | ||
|
|
||
| while (periodN % 10 === 0) { | ||
| periodN = periodN /= 10; | ||
| zeros.push(0); | ||
| } | ||
| return zeros.length; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.