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

Commit

Permalink
move currentTime logic to setTimeDiff method
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Jun 3, 2022
1 parent 9c75580 commit 7afd2ab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
8 changes: 1 addition & 7 deletions src/chains/ethereum/ethereum/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,7 @@ export default class EthereumApi implements Api {
break;
}
const blockchain = this.#blockchain;
// when using clock time use Date.now(), otherwise use the timestamp of the
// current latest block
const currentTime =
this.#options.miner.timestampIncrement === "clock"
? Date.now()
: blockchain.blocks.latest.header.timestamp.toNumber() * 1000;
const offsetMilliseconds = blockchain.setTimeDiff(timestamp, currentTime);
const offsetMilliseconds = blockchain.setTimeDiff(timestamp);
// convert offsetMilliseconds to seconds:
return Math.floor(offsetMilliseconds / 1000);
}
Expand Down
13 changes: 9 additions & 4 deletions src/chains/ethereum/ethereum/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
// if we are using clock time we need to record the time offset so
// other blocks can have timestamps relative to our initial time.
if (options.miner.timestampIncrement === "clock") {
this.setTimeDiff(timestamp, Date.now());
this.#timeAdjustment = timestamp - Date.now();
}

// if we don't already have a latest block, create a genesis block!
Expand Down Expand Up @@ -824,11 +824,16 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {

/**
* @param newTime - the number of milliseconds to adjust the time by. Can be negative.
* @param sourceTime - the current time in milliseconds
* @returns the total time offset *in milliseconds*
*/
public setTimeDiff(newTime: number, sourceTime: number) {
return (this.#timeAdjustment = newTime - sourceTime);
public setTimeDiff(newTime: number) {
// when using clock time use Date.now(), otherwise use the timestamp of the
// current latest block
const currentTime =
this.#options.miner.timestampIncrement === "clock"
? Date.now()
: this.blocks.latest.header.timestamp.toNumber() * 1000;
return (this.#timeAdjustment = newTime - currentTime);
}

#deleteBlockData = async (
Expand Down

0 comments on commit 7afd2ab

Please sign in to comment.