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

Conversation

@eggplantzzz
Copy link
Contributor

Currently Ganache sets all block headers to have difficulty and totalDifficulty hard-coded to 0. This PR causes Ganache to set the difficulty of each block (by default) to 1 (the user can specify their own block difficulty on the command line). The totalDifficulty is also now calculated based on the previous block's totalDifficulty and the block difficulty.

Copy link
Contributor

@davidmurdoch davidmurdoch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really Awesome work. I committed a change, if you wouldn't mind reviewing it.

The gist of the change I made was because:

  • totalDifficulty shouldn't be included in block size calculations
  • totalDifficulty shouldn't be included in the block hash (which it previously was, because it was included in the raw array)

It seems to work now, and as a bonus, I figured out why our block size calculations were always off a little (PR for that coming soon!)

I also requested some more tests :-D

Thanks

Quantity.from(timestamp)
Quantity.from(timestamp),
this.#options.miner.difficulty,
Quantity.from(0) // we start the totalDifficulty at 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use

Suggested change
Quantity.from(0) // we start the totalDifficulty at 0
RPCQUANTITY_ZERO // we start the totalDifficulty at 0

davidmurdoch added a commit that referenced this pull request Feb 13, 2021
Comment on lines 12 to 16
await provider.send("eth_subscribe", ["newHeads"]);

await provider.send("eth_sendTransaction", [{ from, to: from }]);

await provider.once("message");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't need the transaction itself, you can do await provider.send("evm_mine") instead.

Oh, actually... now that #793 is merged (as of yesterday), you can do await provider.send("evm_mine", [{blocks: number}]), no (backwards 😄) loop needed!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh cool! I'll merge in develop and update this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like deleting code.

const block = await provider.send("eth_getBlockByNumber", ["0x0"]);
assert.strictEqual(
block.totalDifficulty,
`0x${DEFAULT_DIFFICULTY}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If DEFAULT_DIFFICULTY is ever greater than 9 in the future this test will fail.

Other ways of solving this:

assert.strictEqual(
  block.totalDifficulty,
  Quantity.from(DEFAULT_DIFFICULTY).toString()
);

or

assert.strictEqual(
  parseInt(block.totalDifficulty, 16),
  DEFAULT_DIFFICULTY
);

or

assert.strictEqual(
  BigInt(block.totalDifficulty),
  DEFAULT_DIFFICULTY // change DEFAULT_DIFFICULTY to a BigInt
);

and lots of others, I'm sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update other places that use the 0x{number} pattern in these tests? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant to update these but must have overlooked them. Updated!

@eggplantzzz eggplantzzz force-pushed the develop-difficulty-fields branch from f27b613 to 609c25c Compare February 18, 2021 14:53
@eggplantzzz eggplantzzz force-pushed the develop-difficulty-fields branch from 609c25c to 1c90485 Compare February 18, 2021 16:10
Copy link
Contributor

@davidmurdoch davidmurdoch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two more little "suggestions" and it's good to go!

@davidmurdoch davidmurdoch deleted the develop-difficulty-fields branch February 22, 2021 22:04
@davidmurdoch davidmurdoch restored the develop-difficulty-fields branch February 22, 2021 22:04
@davidmurdoch davidmurdoch reopened this Feb 22, 2021
@davidmurdoch davidmurdoch changed the title Update difficulty and totalDifficulty fix: set difficulty default to 1 and add totalDifficulty Feb 22, 2021
@davidmurdoch davidmurdoch merged commit 7d4f43d into develop Feb 22, 2021
davidmurdoch added a commit that referenced this pull request Feb 22, 2021
@davidmurdoch davidmurdoch deleted the develop-difficulty-fields branch February 26, 2021 02:24
sambacha pushed a commit to contractshark/ganache-core that referenced this pull request Apr 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants