Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private validateTransaction(transaction: Interfaces.ITransactionData): boolean {
const now: number = Crypto.Slots.getTime();
const lastHeight: number = app
.resolvePlugin("state")
.getStore()
.getLastHeight();
if (transaction.timestamp > now + 3600) {
const secondsInFuture: number = transaction.timestamp - now;
this.pushError(
transaction,
"ERR_FROM_FUTURE",
`Transaction ${transaction.id} is ${secondsInFuture} seconds in the future`,
);
return false;
} else if (transaction.expiration > 0 && transaction.expiration <= lastHeight + 1) {
export const validateGenerator = async (block: Interfaces.IBlock): Promise => {
const database: Database.IDatabaseService = app.resolvePlugin("database");
const logger: Logger.ILogger = app.resolvePlugin("logger");
const roundInfo: Shared.IRoundInfo = roundCalculator.calculateRound(block.data.height);
const delegates: State.IDelegateWallet[] = await database.getActiveDelegates(roundInfo);
const slot: number = Crypto.Slots.getSlotNumber(block.data.timestamp);
const forgingDelegate: State.IDelegateWallet = delegates[slot % delegates.length];
const generatorUsername: string = database.walletManager.findByPublicKey(block.data.generatorPublicKey).username;
if (!forgingDelegate) {
logger.debug(
`Could not decide if delegate ${generatorUsername} (${
block.data.generatorPublicKey
}) is allowed to forge block ${block.data.height.toLocaleString()}`,
);
} else if (forgingDelegate.publicKey !== block.data.generatorPublicKey) {
const forgingUsername = database.walletManager.findByPublicKey(forgingDelegate.publicKey).username;
logger.warn(
`Delegate ${generatorUsername} (${
block.data.generatorPublicKey
export const isBlockChained = (previousBlock: Interfaces.IBlockData, nextBlock: Interfaces.IBlockData): boolean => {
const followsPrevious: boolean = nextBlock.previousBlock === previousBlock.id;
const isPlusOne: boolean = nextBlock.height === previousBlock.height + 1;
const previousSlot: number = Crypto.Slots.getSlotNumber(previousBlock.timestamp);
const nextSlot: number = Crypto.Slots.getSlotNumber(nextBlock.timestamp);
const isAfterPreviousSlot: boolean = previousSlot < nextSlot;
return followsPrevious && isPlusOne && isAfterPreviousSlot;
};
}
const delegate: Delegate = this.isActiveDelegate(this.round.currentForger.publicKey);
if (!delegate) {
if (this.isActiveDelegate(this.round.nextForger.publicKey)) {
const username = this.usernames[this.round.nextForger.publicKey];
this.logger.info(
`Next forging delegate ${username} (${this.round.nextForger.publicKey}) is active on this node.`,
);
await this.client.syncWithNetwork();
}
return this.checkLater(Crypto.Slots.getTimeInMsUntilNextSlot());
}
const networkState: P2P.INetworkState = await this.client.getNetworkState();
if (networkState.nodeHeight !== this.round.lastBlock.height) {
this.logger.warn(
`The NetworkState height (${networkState.nodeHeight}) and round height (${this.round.lastBlock.height}) are out of sync. This indicates delayed blocks on the network.`,
);
}
if (this.isForgingAllowed(networkState, delegate)) {
await this.forgeNewBlock(delegate, this.round, networkState);
}
return this.checkLater(Crypto.Slots.getTimeInMsUntilNextSlot());
} catch (error) {
export const getCurrentRound = async (): Promise => {
const config = app.getConfig();
const databaseService = app.resolvePlugin("database");
const blockchain = app.resolvePlugin("blockchain");
const lastBlock = blockchain.getLastBlock();
const height = lastBlock.data.height + 1;
const roundInfo = roundCalculator.calculateRound(height);
const { maxDelegates, round } = roundInfo;
const blockTime = config.getMilestone(height).blocktime;
const reward = config.getMilestone(height).reward;
const delegates = await databaseService.getActiveDelegates(roundInfo);
const timestamp = Crypto.Slots.getTime();
const blockTimestamp = Crypto.Slots.getSlotNumber(timestamp) * blockTime;
const currentForger = parseInt((timestamp / blockTime) as any) % maxDelegates;
const nextForger = (parseInt((timestamp / blockTime) as any) + 1) % maxDelegates;
return {
current: round,
reward,
timestamp: blockTimestamp,
delegates,
currentForger: delegates[currentForger],
nextForger: delegates[nextForger],
lastBlock: lastBlock.data,
canForge: parseInt((1 + lastBlock.data.timestamp / blockTime) as any) * blockTime < timestamp - 1,
};
};
export const getCurrentRound = async (): Promise => {
const config = app.getConfig();
const databaseService = app.resolvePlugin("database");
const blockchain = app.resolvePlugin("blockchain");
const lastBlock = blockchain.getLastBlock();
const height = lastBlock.data.height + 1;
const roundInfo = roundCalculator.calculateRound(height);
const { maxDelegates, round } = roundInfo;
const blockTime = config.getMilestone(height).blocktime;
const reward = config.getMilestone(height).reward;
const delegates = await databaseService.getActiveDelegates(roundInfo);
const timestamp = Crypto.Slots.getTime();
const blockTimestamp = Crypto.Slots.getSlotNumber(timestamp) * blockTime;
const currentForger = parseInt((timestamp / blockTime) as any) % maxDelegates;
const nextForger = (parseInt((timestamp / blockTime) as any) + 1) % maxDelegates;
return {
current: round,
reward,
timestamp: blockTimestamp,
delegates,
currentForger: delegates[currentForger],
nextForger: delegates[nextForger],
lastBlock: lastBlock.data,
canForge: parseInt((1 + lastBlock.data.timestamp / blockTime) as any) * blockTime < timestamp - 1,
};
};
export const getStatus = async (): Promise => {
const lastBlock: Interfaces.IBlock = app.resolvePlugin("blockchain").getLastBlock();
return {
state: {
height: lastBlock ? lastBlock.data.height : 0,
forgingAllowed: Crypto.Slots.isForgingAllowed(),
currentSlot: Crypto.Slots.getSlotNumber(),
header: lastBlock ? lastBlock.getHeader() : {},
},
config: getPeerConfig(),
};
};
export const isBlockChained = (previousBlock: Interfaces.IBlockData, nextBlock: Interfaces.IBlockData): boolean => {
const followsPrevious: boolean = nextBlock.previousBlock === previousBlock.id;
const isPlusOne: boolean = nextBlock.height === previousBlock.height + 1;
const previousSlot: number = Crypto.Slots.getSlotNumber(previousBlock.timestamp);
const nextSlot: number = Crypto.Slots.getSlotNumber(nextBlock.timestamp);
const isAfterPreviousSlot: boolean = previousSlot < nextSlot;
return followsPrevious && isPlusOne && isAfterPreviousSlot;
};
export const getStatus = async (): Promise => {
const lastBlock: Interfaces.IBlock = app.resolvePlugin("blockchain").getLastBlock();
return {
state: {
height: lastBlock ? lastBlock.data.height : 0,
forgingAllowed: Crypto.Slots.isForgingAllowed(),
currentSlot: Crypto.Slots.getSlotNumber(),
header: lastBlock ? lastBlock.getHeader() : {},
},
config: getPeerConfig(),
};
};