Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #154 from LiskHQ/153-update-supportedtokenssubstor…
Browse files Browse the repository at this point in the history
…e-entry

Update supportedTokensSubstore entry in addTokenModuleEntry
  • Loading branch information
sameersubudhi authored Sep 15, 2023
2 parents 8ab3875 + 326d739 commit ee0dc3d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/assets/pos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ export const formatInt = (num: number | bigint): string => {
export const getValidatorKeys = async (
accounts: Account[],
snapshotHeight: number,
snapshotHeightPrevious: number,
prevSnapshotBlockHeight: number,
db: Database,
): Promise<Record<string, string>> => {
const keys: Record<string, string> = {};

const blocksStream = db.createReadStream({
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeightPrevious + 1)}`),
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(prevSnapshotBlockHeight + 1)}`),
lte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeight)}`),
});

Expand Down
5 changes: 2 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const MAX_BFT_WEIGHT_CAP = 1000;

export const SNAPSHOT_BLOCK_VERSION = 0;
export const SNAPSHOT_TIME_GAP = 0; // TODO: Update once decided
export const ALL_SUPPORTED_TOKENS_KEY = Buffer.alloc(0);

export const ADDRESS_LEGACY_RESERVE = hash(Buffer.from('legacyReserve', 'utf8')).slice(0, 20);
export const INVALID_BLS_KEY = Buffer.alloc(48, 0).toString('hex');
Expand All @@ -65,12 +64,12 @@ export const NETWORK_CONSTANT: { [key: string]: NetworkConfigLocal } = {
'4c09e6a781fc4c7bdb936ee815de8f94190f8a7519becd9de2081832be309a99': {
name: 'mainnet',
tokenID: TOKEN_ID_LSK.MAINNET,
snapshotHeightPrevious: HEIGHT_PREVIOUS_SNAPSHOT_BLOCK.MAINNET,
prevSnapshotBlockHeight: HEIGHT_PREVIOUS_SNAPSHOT_BLOCK.MAINNET,
},
'15f0dacc1060e91818224a94286b13aa04279c640bd5d6f193182031d133df7c': {
name: 'testnet',
tokenID: TOKEN_ID_LSK.TESTNET,
snapshotHeightPrevious: HEIGHT_PREVIOUS_SNAPSHOT_BLOCK.TESTNET,
prevSnapshotBlockHeight: HEIGHT_PREVIOUS_SNAPSHOT_BLOCK.TESTNET,
},
};

Expand Down
15 changes: 4 additions & 11 deletions src/createAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
DB_KEY_ACCOUNTS_ADDRESS,
BINARY_ADDRESS_LENGTH,
ADDRESS_LEGACY_RESERVE,
ALL_SUPPORTED_TOKENS_KEY,
} from './constants';
import { accountSchema, voteWeightsSchema } from './schemas';
import {
Expand Down Expand Up @@ -76,7 +75,7 @@ export class CreateAsset {

public init = async (
snapshotHeight: number,
snapshotHeightPrevious: number,
prevSnapshotBlockHeight: number,
tokenID: string,
): Promise<GenesisAssetEntry[]> => {
const authSubstoreEntries: AuthStoreEntryBuffer[] = [];
Expand Down Expand Up @@ -130,7 +129,7 @@ export class CreateAsset {
const validatorKeys = await getValidatorKeys(
accounts,
snapshotHeight,
snapshotHeightPrevious,
prevSnapshotBlockHeight,
this._db,
);

Expand All @@ -140,11 +139,11 @@ export class CreateAsset {
authSubstoreEntries.push(authModuleAsset);

// genesis asset for token module
// Create user subtore entries
// Create user substore entries
const userSubstoreEntry = await createUserSubstoreArrayEntry(account, tokenID);
if (userSubstoreEntry) userSubstoreEntries.push(userSubstoreEntry);

// Create total lisk supply for supply subtore
// Create total lisk supply for supply substore
totalLSKSupply += BigInt(account.token.balance);
const lockedBalances = await getLockedBalances(account);
totalLSKSupply = lockedBalances.reduce(
Expand Down Expand Up @@ -187,12 +186,6 @@ export class CreateAsset {
totalSupply: String(totalLSKSupply + legacyReserveAmount),
});

// Update supported tokens substore to support all tokens by default
supportedTokensSubstoreEntries.push({
chainID: ALL_SUPPORTED_TOKENS_KEY,
supportedTokenIDs: [],
});

// Sort validators substore entries in lexicographical order
const sortedValidators = validators.sort(addressComparator).map(({ address, ...entry }) => ({
...entry,
Expand Down
14 changes: 9 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ import {
import {
observeChainHeight,
getTokenIDLsk,
getHeightPrevSnapshotBlock,
getPrevSnapshotBlockHeight,
setTokenIDLskByNetID,
setHeightPrevSnapshotBlockByNetID,
setPrevSnapshotBlockHeightByNetID,
} from './utils/chain';
import { createGenesisBlock, writeGenesisAssets } from './utils/genesis_block';
import { CreateAsset } from './createAsset';
Expand Down Expand Up @@ -168,7 +168,7 @@ class LiskMigrator extends Command {
: await getConfig(liskCoreV3DataPath);

await setTokenIDLskByNetID(networkIdentifier);
await setHeightPrevSnapshotBlockByNetID(networkIdentifier);
await setPrevSnapshotBlockHeightByNetID(networkIdentifier);

await observeChainHeight({
label: 'Waiting for snapshot height to be finalized',
Expand All @@ -188,8 +188,12 @@ class LiskMigrator extends Command {
cli.action.start('Creating genesis assets');
const createAsset = new CreateAsset(db);
const tokenID = getTokenIDLsk();
const snapshotHeightPrevious = getHeightPrevSnapshotBlock();
const genesisAssets = await createAsset.init(snapshotHeight, snapshotHeightPrevious, tokenID);
const prevSnapshotBlockHeight = getPrevSnapshotBlockHeight();
const genesisAssets = await createAsset.init(
snapshotHeight,
prevSnapshotBlockHeight,
tokenID,
);
cli.action.stop();

// Create an app instance for creating genesis block
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export interface GenesisInteroperability {
export interface NetworkConfigLocal {
name: string;
tokenID: string;
snapshotHeightPrevious: number;
prevSnapshotBlockHeight: number;
}

export interface RegisteredModule {
Expand Down
9 changes: 4 additions & 5 deletions src/utils/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getAPIClient } from '../client';
import { NETWORK_CONSTANT } from '../constants';

let tokenIDLsk: string;
let heightPreviousSnapshotBlock: number;
let prevSnapshotBlockHeight: number;

interface ObserveParams {
readonly label: string;
Expand All @@ -32,13 +32,12 @@ export const setTokenIDLskByNetID = async (networkIdentifier: string): Promise<v
tokenIDLsk = NETWORK_CONSTANT[networkIdentifier].tokenID as string;
};

export const getHeightPrevSnapshotBlock = (): number => heightPreviousSnapshotBlock;
export const getPrevSnapshotBlockHeight = (): number => prevSnapshotBlockHeight;

export const setHeightPrevSnapshotBlockByNetID = async (
export const setPrevSnapshotBlockHeightByNetID = async (
networkIdentifier: string,
): Promise<void> => {
heightPreviousSnapshotBlock = NETWORK_CONSTANT[networkIdentifier]
.snapshotHeightPrevious as number;
prevSnapshotBlockHeight = NETWORK_CONSTANT[networkIdentifier].prevSnapshotBlockHeight as number;
};

export const getNodeInfo = async (
Expand Down
14 changes: 7 additions & 7 deletions test/unit/assets/pos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Build assets/pos', () => {
let blockIDsStream: { value: Buffer }[];
let delegates: VoteWeightsWrapper;
const snapshotHeight = 10815;
const snapshotHeightPrevious = 5000;
const prevSnapshotBlockHeight = 5000;

beforeAll(async () => {
db = new Database('testDB');
Expand Down Expand Up @@ -150,7 +150,7 @@ describe('Build assets/pos', () => {
it('should create createValidatorsArrayEntry', async () => {
when(db.createReadStream)
.calledWith({
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeightPrevious + 1)}`),
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(prevSnapshotBlockHeight + 1)}`),
lte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeight)}`),
})
.mockReturnValue(Readable.from(blockIDsStream));
Expand All @@ -169,7 +169,7 @@ describe('Build assets/pos', () => {
const validatorKeys = await getValidatorKeys(
accounts,
snapshotHeight,
snapshotHeightPrevious,
prevSnapshotBlockHeight,
db,
);

Expand Down Expand Up @@ -243,20 +243,20 @@ describe('Build assets/pos', () => {
it('should throw error when creating stream with invalid file path', async () => {
when(db.createReadStream)
.calledWith({
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeightPrevious + 1)}`),
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(prevSnapshotBlockHeight + 1)}`),
lte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeight)}`),
})
.mockReturnValue(createReadStream('test.txt') as never);

await expect(
getValidatorKeys(accounts, snapshotHeight, snapshotHeightPrevious, db),
getValidatorKeys(accounts, snapshotHeight, prevSnapshotBlockHeight, db),
).rejects.toThrow();
});

it('should create PoS module asset', async () => {
when(db.createReadStream)
.calledWith({
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeightPrevious + 1)}`),
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(prevSnapshotBlockHeight + 1)}`),
lte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeight)}`),
})
.mockReturnValue(Readable.from(blockIDsStream));
Expand All @@ -275,7 +275,7 @@ describe('Build assets/pos', () => {
const validatorKeys = await getValidatorKeys(
accounts,
snapshotHeight,
snapshotHeightPrevious,
prevSnapshotBlockHeight,
db,
);

Expand Down
14 changes: 7 additions & 7 deletions test/unit/createAsset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Build assets/legacy', () => {
let delegates: VoteWeightsWrapper;
let encodedVoteWeights: Buffer;
const snapshotHeight = 10815;
const snapshotHeightPrevious = 5000;
const prevSnapshotBlockHeight = 5000;
const tokenID = '0400000000000000';

interface Accounts {
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('Build assets/legacy', () => {

when(db.createReadStream)
.calledWith({
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeightPrevious + 1)}`),
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(prevSnapshotBlockHeight + 1)}`),
lte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeight)}`),
})
.mockReturnValue(Readable.from([]));
Expand All @@ -218,7 +218,7 @@ describe('Build assets/legacy', () => {
.calledWith(Buffer.from(`${DB_KEY_CHAIN_STATE}:${CHAIN_STATE_DELEGATE_VOTE_WEIGHTS}`))
.mockResolvedValue(encodedVoteWeights as never);

const response = await createAsset.init(snapshotHeight, snapshotHeightPrevious, tokenID);
const response = await createAsset.init(snapshotHeight, prevSnapshotBlockHeight, tokenID);

const moduleList = [
MODULE_NAME_LEGACY,
Expand Down Expand Up @@ -246,20 +246,20 @@ describe('Build assets/legacy', () => {
.mockReturnValue(undefined);

await expect(
createAsset.init(snapshotHeight, snapshotHeightPrevious, tokenID),
createAsset.init(snapshotHeight, prevSnapshotBlockHeight, tokenID),
).rejects.toThrow();
});

it('should throw error when block stream is undefined', async () => {
when(db.createReadStream)
.calledWith({
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeightPrevious + 1)}`),
gte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(prevSnapshotBlockHeight + 1)}`),
lte: Buffer.from(`${DB_KEY_BLOCKS_HEIGHT}:${formatInt(snapshotHeight)}`),
})
.mockReturnValue(undefined);

await expect(
createAsset.init(snapshotHeight, snapshotHeightPrevious, tokenID),
createAsset.init(snapshotHeight, prevSnapshotBlockHeight, tokenID),
).rejects.toThrow();
});

Expand All @@ -278,7 +278,7 @@ describe('Build assets/legacy', () => {
.mockReturnValue(createReadStream('test.txt') as never);

await expect(
createAsset.init(snapshotHeight, snapshotHeightPrevious, tokenID),
createAsset.init(snapshotHeight, prevSnapshotBlockHeight, tokenID),
).rejects.toThrow();
});
});
Expand Down

0 comments on commit ee0dc3d

Please sign in to comment.