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

Commit

Permalink
🌱 Update Framework and Lisk-chain to have updated account
Browse files Browse the repository at this point in the history
properties
  • Loading branch information
shuse2 committed Mar 19, 2020
1 parent 0de9560 commit 1e0dc75
Show file tree
Hide file tree
Showing 13 changed files with 510 additions and 121 deletions.
138 changes: 106 additions & 32 deletions elements/lisk-chain/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,116 @@ import { AccountJSON } from './types';

export const accountDefaultValues = {
publicKey: undefined,
// tslint:disable-next-line:no-null-keyword
username: null,
isDelegate: 0,
balance: '0',
// tslint:disable-next-line no-null-keyword
username: null,
nonce: '0',
missedBlocks: 0,
producedBlocks: 0,
fees: '0',
rewards: '0',
voteWeight: '0',
nameExist: false,
// tslint:disable-next-line:no-null-keyword
votedDelegatesPublicKeys: [],
totalVotesReceived: '0',
votes: [],
unlocking: [],
delegate: {
lastForgedHeight: 0,
registeredHeight: 0,
consecutiveMissedBlocks: 0,
isBanned: false,
pomHeights: [],
},
asset: {},
keys: {
mandatoryKeys: [],
optionalKeys: [],
numberOfSignatures: 0,
},
// TODO: Remove once new DPoS implementation is done
missedBlocks: 0,
voteWeight: '0',
nameExist: false,
votedDelegatesPublicKeys: [],
isDelegate: 0,
};

interface Vote {
readonly delegateAddress: string;
readonly amount: bigint;
}

interface Unlocking {
readonly delegateAddress: string;
readonly amount: bigint;
readonly unvoteHeight: number;
}

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

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.missedBlocks = accountInfo.missedBlocks;
this.producedBlocks = accountInfo.producedBlocks;
this.isDelegate = accountInfo.isDelegate;
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.voteWeight = accountInfo.voteWeight
? BigInt(accountInfo.voteWeight)
: BigInt(0);
this.nameExist = accountInfo.nameExist;
this.asset = accountInfo.asset;
this.votedDelegatesPublicKeys =
accountInfo.votedDelegatesPublicKeys === undefined ||
accountInfo.votedDelegatesPublicKeys === null
? []
: accountInfo.votedDelegatesPublicKeys;
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,
}))
: [];
this.totalVotesReceived = BigInt(accountInfo.totalVotesReceived ?? 0);
this.delegate = {
lastForgedHeight: accountInfo.delegate?.lastForgedHeight ?? 0,
registeredHeight: accountInfo.delegate?.registeredHeight ?? 0,
consecutiveMissedBlocks:
accountInfo.delegate?.consecutiveMissedBlocks ?? 0,
isBanned: accountInfo.delegate?.isBanned ?? false,
pomHeights: accountInfo.delegate?.pomHeights ?? [],
};
this.keys = {
mandatoryKeys: accountInfo.keys?.mandatoryKeys?.length
? accountInfo.keys?.mandatoryKeys
Expand All @@ -91,6 +134,19 @@ export class Account {
: [],
numberOfSignatures: accountInfo.keys?.numberOfSignatures || 0,
};

// TODO: Remove once new DPoS implementation is done
this.missedBlocks = accountInfo.missedBlocks;
this.isDelegate = accountInfo.isDelegate;
this.voteWeight = accountInfo.voteWeight
? BigInt(accountInfo.voteWeight)
: BigInt(0);
this.nameExist = accountInfo.nameExist;
this.votedDelegatesPublicKeys =
accountInfo.votedDelegatesPublicKeys === undefined ||
accountInfo.votedDelegatesPublicKeys === null
? []
: accountInfo.votedDelegatesPublicKeys;
}

public static getDefaultAccount = (address: string): Account =>
Expand All @@ -105,26 +161,44 @@ export class Account {
publicKey: this.publicKey,
// tslint:disable-next-line:no-null-keyword
username: this.username,
isDelegate: this.isDelegate,
balance: this.balance.toString(),
nonce: this.nonce.toString(),
missedBlocks: this.missedBlocks,
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,
registeredHeight: this.delegate.registeredHeight,
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 once new DPoS implementation is done
isDelegate: this.isDelegate,
missedBlocks: this.missedBlocks,
voteWeight: this.voteWeight.toString(),
nameExist: this.nameExist,
votedDelegatesPublicKeys:
this.votedDelegatesPublicKeys.length < 1
? // tslint:disable-next-line:no-null-keyword
null
: this.votedDelegatesPublicKeys,
asset: this.asset,
keys: {
mandatoryKeys: this.keys.mandatoryKeys,
optionalKeys: this.keys.optionalKeys,
numberOfSignatures: this.keys.numberOfSignatures,
},
};
}
}
35 changes: 28 additions & 7 deletions elements/lisk-chain/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,49 @@ export type IndexableAccount = Account & Indexable;

export type IndexableTransactionJSON = TransactionJSON & Indexable;

export interface AccountVoteJSON {
readonly delegateAddress: string;
readonly amount: string;
}

export interface AccountUnlockingJSON {
readonly delegateAddress: string;
readonly amount: string;
readonly unvoteHeight: number;
}

export interface AccountJSON {
readonly address: string;
readonly balance: string;
readonly nonce: string;
readonly missedBlocks: number;
readonly producedBlocks: number;
readonly publicKey: string | undefined;
readonly username: string | null;
readonly isDelegate: number;
readonly fees: string;
readonly rewards: string;
// tslint:disable-next-line readonly-keyword
readonly voteWeight: string;
readonly nameExist: boolean;
readonly totalVotesReceived: string;
readonly asset: object;
// tslint:disable-next-line readonly-keyword
readonly votedDelegatesPublicKeys: string[] | null;
readonly keys?: {
readonly mandatoryKeys?: string[];
readonly optionalKeys?: string[];
readonly numberOfSignatures?: number;
};
readonly votes?: AccountVoteJSON[];
readonly unlocking?: AccountUnlockingJSON[];
readonly delegate?: {
readonly lastForgedHeight: number;
readonly registeredHeight: number;
readonly consecutiveMissedBlocks: number;
readonly isBanned: boolean;
readonly pomHeights: number[];
};

// TODO: Remove once new DPoS implementation is done
readonly missedBlocks: number;
readonly isDelegate: number;
readonly voteWeight: string;
readonly nameExist: boolean;
readonly votedDelegatesPublicKeys: string[] | null;
}

export interface Context {
Expand Down
73 changes: 58 additions & 15 deletions elements/lisk-chain/test/unit/account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,25 @@ describe('account', () => {
expect(defaultAccount.balance).toEqual(
BigInt(accountDefaultValues.balance),
);
expect(defaultAccount.totalVotesReceived).toEqual(
BigInt(accountDefaultValues.totalVotesReceived),
);
expect(defaultAccount.fees).toEqual(BigInt(accountDefaultValues.fees));
expect(defaultAccount.voteWeight).toEqual(
BigInt(accountDefaultValues.voteWeight),
);
expect(defaultAccount.rewards).toEqual(
BigInt(accountDefaultValues.rewards),
);
expect(defaultAccount.votes).toEqual([]);
expect(defaultAccount.unlocking).toEqual([]);
expect(defaultAccount.delegate).toEqual({
lastForgedHeight: 0,
registeredHeight: 0,
consecutiveMissedBlocks: 0,
isBanned: false,
pomHeights: [],
});
});
});

Expand All @@ -56,6 +68,7 @@ describe('account', () => {
expect(accountObj.fees).toEqual(BigInt('0'));
expect(accountObj.voteWeight).toEqual(BigInt('0'));
expect(accountObj.rewards).toEqual(BigInt('0'));
expect(accountObj.totalVotesReceived).toEqual(BigInt('0'));
expect(accountObj.votedDelegatesPublicKeys).toEqual([]);
expect(accountObj.username).toBeNull;
expect(accountObj.publicKey).toEqual(undefined);
Expand All @@ -69,30 +82,50 @@ describe('account', () => {
optionalKeys: [],
numberOfSignatures: 0,
});
expect(accountObj.votes).toEqual([]);
expect(accountObj.unlocking).toEqual([]);
expect(accountObj.delegate).toEqual({
lastForgedHeight: 0,
registeredHeight: 0,
consecutiveMissedBlocks: 0,
isBanned: false,
pomHeights: [],
});
});
});

describe('toJSON', () => {
defaultAccount = Account.getDefaultAccount(accountAddress1);
const accountJSON = defaultAccount.toJSON();

it('should return default account JSON object with relevant types', () => {
expect(accountJSON.address).toEqual(accountAddress1);
expect(accountJSON.balance).toBeString;
expect(accountJSON.fees).toBeString;
expect(accountJSON.voteWeight).toBeString;
expect(accountJSON.rewards).toBeString;
expect(accountJSON.votedDelegatesPublicKeys).toBeNull;
expect(accountJSON.username).toBeNull;
expect(accountJSON.publicKey).toBeUndefined;
expect(accountJSON.isDelegate).toBeNumber;
expect(accountJSON.missedBlocks).toBeNumber;
expect(accountJSON.producedBlocks).toBeNumber;
expect(accountJSON.nameExist).toBeBoolean;
expect(accountJSON.asset).toBeObject;
expect(accountJSON.keys?.mandatoryKeys).toBeArray;
expect(accountJSON.keys?.optionalKeys).toBeArray;
expect(accountJSON.keys?.numberOfSignatures).toBeNumber;
expect(accountJSON.balance).toBeString();
expect(accountJSON.fees).toBeString();
expect(accountJSON.voteWeight).toBeString();
expect(accountJSON.rewards).toBeString();
expect(accountJSON.votedDelegatesPublicKeys).toBeNull();
expect(accountJSON.username).toBeNull();
expect(accountJSON.publicKey).toBeUndefined();
expect(accountJSON.isDelegate).toBeNumber();
expect(accountJSON.missedBlocks).toBeNumber();
expect(accountJSON.producedBlocks).toBeNumber();
expect(accountJSON.nameExist).toBeBoolean();
expect(accountJSON.asset).toBeObject();
expect(accountJSON.keys?.mandatoryKeys).toBeArray();
expect(accountJSON.keys?.optionalKeys).toBeArray();
expect(accountJSON.keys?.numberOfSignatures).toBeNumber();
expect(accountJSON.votes).toBeArray();
expect(accountJSON.totalVotesReceived).toBeString();
expect(accountJSON.unlocking).toBeArray();
expect(accountJSON.delegate?.consecutiveMissedBlocks).toBeNumber();
expect(accountJSON.delegate?.lastForgedHeight).toBeNumber();
expect(accountJSON.delegate?.registeredHeight).toBeNumber();
expect(accountJSON.delegate?.consecutiveMissedBlocks).toBeNumber();
expect(accountJSON.delegate?.isBanned).toBeBoolean();
expect(accountJSON.delegate?.pomHeights).toBeArray();
});

it('should return account JSON object with relevant values', () => {
expect(accountJSON.address).toEqual(accountAddress1);
expect(accountJSON.balance).toEqual('0');
Expand All @@ -105,13 +138,23 @@ describe('account', () => {
expect(accountJSON.isDelegate).toEqual(0);
expect(accountJSON.missedBlocks).toEqual(0);
expect(accountJSON.producedBlocks).toEqual(0);
expect(accountJSON.totalVotesReceived).toEqual('0');
expect(accountJSON.nameExist).toEqual(false);
expect(accountJSON.asset).toEqual({});
expect(accountJSON.keys).toEqual({
mandatoryKeys: [],
optionalKeys: [],
numberOfSignatures: 0,
});
expect(accountJSON.votes).toEqual([]);
expect(accountJSON.unlocking).toEqual([]);
expect(accountJSON.delegate).toEqual({
lastForgedHeight: 0,
registeredHeight: 0,
consecutiveMissedBlocks: 0,
isBanned: false,
pomHeights: [],
});
});
});
});
Loading

0 comments on commit 1e0dc75

Please sign in to comment.