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 #5367 from LiskHQ/5264-update_chain_codec
Browse files Browse the repository at this point in the history
Update chain to use lisk-codec with new schema - Closes #5264
  • Loading branch information
shuse2 authored Jun 2, 2020
2 parents 0fdc707 + 502cddb commit 5b234e0
Show file tree
Hide file tree
Showing 58 changed files with 3,644 additions and 3,249 deletions.
70 changes: 33 additions & 37 deletions elements/lisk-chain/benchmark/calculate_diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,53 @@ const { calculateDiff, undo } = require('../dist-node/diff');
const suite = new Suite();

const senderAccount = {
address: '5059876081639179984L',
publicKey:
'0fe9a3f1a21b5530f27f87a414b549e79a940bf24fdf2b2f05e7f22aeeecc86a',
username: null,
isDelegate: false,
nonce: '103',
balance: 9897000000000000,
fees: '0',
rewards: '0',
address: '5059876081639179984L',
publicKey: '0fe9a3f1a21b5530f27f87a414b549e79a940bf24fdf2b2f05e7f22aeeecc86a',
username: null,
isDelegate: false,
nonce: '103',
balance: 9897000000000000,
fees: '0',
rewards: '0',
};

const previousSenderStateBuffer = Buffer.from(JSON.stringify(senderAccount));

const multiSignatureAccount = {
...senderAccount,
keys: {
numberOfSignatures: 3,
mandatoryKeys: [
'0b211fce4b615083701cb8a8c99407e464b2f9aa4f367095322de1b77e5fcfbe',
'ff30ef40b7de42114137be46f1009d30e5c19809a73d5a162bc99f7e7681d63d',
],
optionalKeys: [
'57df5c3811961939f8dcfa858c6eaefebfaa4de942f7e703bf88127e0ee9cca4',
'fa406b6952d377f0278920e3eb8da919e4cf5c68b02eeba5d8b3334fdc0369b6',
],
},
...senderAccount,
keys: {
numberOfSignatures: 3,
mandatoryKeys: [
'0b211fce4b615083701cb8a8c99407e464b2f9aa4f367095322de1b77e5fcfbe',
'ff30ef40b7de42114137be46f1009d30e5c19809a73d5a162bc99f7e7681d63d',
],
optionalKeys: [
'57df5c3811961939f8dcfa858c6eaefebfaa4de942f7e703bf88127e0ee9cca4',
'fa406b6952d377f0278920e3eb8da919e4cf5c68b02eeba5d8b3334fdc0369b6',
],
},
};

const multiSignatureAccountBuffer = Buffer.from(
JSON.stringify(multiSignatureAccount),
JSON.stringify(multiSignatureAccount),
);
const diff = calculateDiff(
previousSenderStateBuffer,
multiSignatureAccountBuffer,
previousSenderStateBuffer,
multiSignatureAccountBuffer,
);

/**
* calculateDiff x 102,134 ops/sec ±0.99% (84 runs sampled)
* undo x 50,906 ops/sec ±0.80% (91 runs sampled)
*/
suite
.add('calculateDiff', () => {
calculateDiff(
previousSenderStateBuffer,
multiSignatureAccountBuffer,
);
})
.add('undo', () => {
undo(multiSignatureAccountBuffer, diff);
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.run({ async: true });
.add('calculateDiff', () => {
calculateDiff(previousSenderStateBuffer, multiSignatureAccountBuffer);
})
.add('undo', () => {
undo(multiSignatureAccountBuffer, diff);
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.run({ async: true });
199 changes: 58 additions & 141 deletions elements/lisk-chain/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,170 +11,87 @@
*
* Removal or modification of this copyright notice is prohibited.
*/
// eslint-disable-next-line import/no-cycle
import { AccountJSON } from './types';
// eslint-disable-next-line @typescript-eslint/no-require-imports
import cloneDeep = require('lodash.clonedeep');

export const accountDefaultValues = {
publicKey: undefined,
balance: '0',
username: null,
nonce: '0',
producedBlocks: 0,
fees: '0',
rewards: '0',
totalVotesReceived: '0',
votes: [],
unlocking: [],
delegate: {
lastForgedHeight: 0,
consecutiveMissedBlocks: 0,
isBanned: false,
pomHeights: [],
},
asset: {},
publicKey: Buffer.alloc(0),
balance: BigInt(0),
nonce: BigInt(0),
keys: {
mandatoryKeys: [],
optionalKeys: [],
numberOfSignatures: 0,
},
// TODO: Remove once new DPoS implementation is done
missedBlocks: 0,
isDelegate: 0,
asset: {},
};

interface Vote {
readonly delegateAddress: string;
amount: bigint;
}
type BasicTypes =
| number
| bigint
| boolean
| string
| Buffer
| Array<number | bigint | boolean | string | Buffer>;

interface Unlocking {
readonly delegateAddress: string;
readonly amount: bigint;
readonly unvoteHeight: number;
export interface DefaultAsset {
[key: string]: {
[key: string]: BasicTypes;
};
}

export class Account {
public address: string;
export class Account<T = DefaultAsset> {
public address: Buffer;
public balance: bigint;
public publicKey: Buffer;
public nonce: bigint;
public fees: bigint;
public rewards: bigint;
public producedBlocks: number;
public publicKey: string | undefined;
public totalVotesReceived: bigint;
public username: string | null;
public asset: object;
public votes: Vote[];
public unlocking: Unlocking[];
public delegate: {
lastForgedHeight: number;
consecutiveMissedBlocks: number;
isBanned: boolean;
pomHeights: number[];
};
public keys: {
mandatoryKeys: string[];
optionalKeys: string[];
mandatoryKeys: Buffer[];
optionalKeys: Buffer[];
numberOfSignatures: number;
};
// TODO: Remove once new DPoS implementation is done
public missedBlocks: number;
public isDelegate: number;
public asset: T;

private _stringAddress?: string;

public constructor(accountInfo: AccountJSON) {
this.address = accountInfo.address;
this.balance = accountInfo.balance
? BigInt(accountInfo.balance)
: BigInt(0);
this.nonce = accountInfo.nonce ? BigInt(accountInfo.nonce) : BigInt(0);
this.producedBlocks = accountInfo.producedBlocks;
this.publicKey = accountInfo.publicKey;
this.username = accountInfo.username;
this.fees = accountInfo.fees ? BigInt(accountInfo.fees) : BigInt(0);
this.rewards = accountInfo.rewards
? BigInt(accountInfo.rewards)
: BigInt(0);
this.asset = accountInfo.asset;
this.votes = accountInfo.votes?.length
? accountInfo.votes.map(vote => ({
delegateAddress: vote.delegateAddress,
amount: BigInt(vote.amount),
}))
: [];
this.unlocking = accountInfo.unlocking?.length
? accountInfo.unlocking.map(unlock => ({
delegateAddress: unlock.delegateAddress,
amount: BigInt(unlock.amount),
unvoteHeight: unlock.unvoteHeight,
}))
: [];
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.totalVotesReceived = BigInt(accountInfo.totalVotesReceived ?? 0);
this.delegate = {
lastForgedHeight: accountInfo.delegate?.lastForgedHeight ?? 0,
consecutiveMissedBlocks:
accountInfo.delegate?.consecutiveMissedBlocks ?? 0,
isBanned: accountInfo.delegate?.isBanned ?? false,
pomHeights: accountInfo.delegate?.pomHeights
? [...accountInfo.delegate.pomHeights]
: [],
};
this.keys = {
mandatoryKeys: accountInfo.keys?.mandatoryKeys?.length
? [...accountInfo.keys.mandatoryKeys]
: [],
optionalKeys: accountInfo.keys?.optionalKeys?.length
? [...accountInfo.keys.optionalKeys]
: [],
numberOfSignatures: accountInfo.keys?.numberOfSignatures ?? 0,
};
public constructor(account: Partial<Account<T>>) {
if (!account.address) {
throw new Error('Account must have address');
}
this.address = account.address;
this.balance = account.balance ?? BigInt(0);
this.publicKey = account.publicKey ?? Buffer.alloc(0);
this.nonce = account.nonce ?? BigInt(0);
this.keys = account.keys
? {
mandatoryKeys: [...account.keys.mandatoryKeys],
optionalKeys: [...account.keys.optionalKeys],
numberOfSignatures: account.keys.numberOfSignatures,
}
: {
mandatoryKeys: [],
optionalKeys: [],
numberOfSignatures: 0,
};
this.asset = account.asset ? cloneDeep(account.asset) : ({} as T);
}

// TODO: Remove with https://github.com/LiskHQ/lisk-sdk/issues/5058
this.missedBlocks = accountInfo.missedBlocks;
this.isDelegate = accountInfo.isDelegate;
public get stringAddress(): string {
if (this._stringAddress) {
return this._stringAddress;
}
this._stringAddress = this.address.toString('hex');
return this._stringAddress;
}

public static getDefaultAccount = (address: string): Account =>
new Account({
public static getDefaultAccount<T>(
address: Buffer,
defaultAsset?: T,
): Account<T> {
return new Account<T>({
address,
...accountDefaultValues,
asset: defaultAsset ?? ({} as T),
});

public toJSON(): AccountJSON {
return {
address: this.address,
publicKey: this.publicKey,
username: this.username,
balance: this.balance.toString(),
nonce: this.nonce.toString(),
producedBlocks: this.producedBlocks,
fees: this.fees.toString(),
rewards: this.rewards.toString(),
totalVotesReceived: this.totalVotesReceived.toString(),
asset: this.asset,
votes: this.votes.map(v => ({
delegateAddress: v.delegateAddress,
amount: v.amount.toString(),
})),
unlocking: this.unlocking.map(v => ({
delegateAddress: v.delegateAddress,
amount: v.amount.toString(),
unvoteHeight: v.unvoteHeight,
})),
delegate: {
lastForgedHeight: this.delegate.lastForgedHeight,
consecutiveMissedBlocks: this.delegate.consecutiveMissedBlocks,
isBanned: this.delegate.isBanned,
pomHeights: this.delegate.pomHeights,
},
keys: {
mandatoryKeys: this.keys.mandatoryKeys,
optionalKeys: this.keys.optionalKeys,
numberOfSignatures: this.keys.numberOfSignatures,
},
// TODO: Remove with https://github.com/LiskHQ/lisk-sdk/issues/5058
isDelegate: this.isDelegate,
missedBlocks: this.missedBlocks,
};
}
}
Loading

0 comments on commit 5b234e0

Please sign in to comment.