This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 683
Initialize persisted state root on startup #249
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
const Web3 = require("web3"); | ||
const Ganache = require("../index.js"); | ||
const assert = require("assert"); | ||
const pify = require("pify"); | ||
const BN = require("bn.js"); | ||
const temp = require("temp").track(); | ||
|
||
describe("Persistence ", function() { | ||
describe("simple value transfer transactions", function() { | ||
const dbPath = temp.mkdirSync("ganache-core-tests-"); | ||
const mnemonic = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat"; | ||
|
||
// use these to validate state | ||
let newProvider = null; | ||
let web3 = null; | ||
|
||
// initial state; | ||
let expectedLatestBlock = null; | ||
let expectedAccounts = null; | ||
let expectedNonces = null; | ||
let expectedBalances = null; | ||
|
||
before("Set up initial persisted blockchain", async function() { | ||
const initialProvider = Ganache.provider({ | ||
db_path: dbPath, | ||
mnemonic | ||
}); | ||
|
||
const initialWeb3 = new Web3(initialProvider); | ||
|
||
expectedAccounts = await initialWeb3.eth.getAccounts(); | ||
|
||
// we send 1 ether to get some state on the chain | ||
const receipt = await initialWeb3.eth.sendTransaction({ | ||
from: expectedAccounts[0], | ||
to: expectedAccounts[1], | ||
value: initialWeb3.utils.toWei("1", "ether") | ||
}); | ||
|
||
assert.strictEqual(receipt.status, true); | ||
|
||
expectedLatestBlock = await initialWeb3.eth.getBlock("latest", true); | ||
|
||
expectedNonces = expectedAccounts.map(async(account) => initialWeb3.eth.getTransactionCount(account)); | ||
|
||
expectedBalances = expectedAccounts.map(async(account) => initialWeb3.eth.getBalance(account)); | ||
|
||
// clean up after ourselves to make sure we don't have any lingering background tasks | ||
initialWeb3.setProvider(null); | ||
await pify(initialProvider.close)(); | ||
}); | ||
|
||
before("Open new provider using state persisted by last chain", async function() { | ||
newProvider = Ganache.provider({ | ||
db_path: dbPath, | ||
mnemonic | ||
}); | ||
|
||
web3 = new Web3(newProvider); | ||
}); | ||
|
||
it("should have the same latestBlock", async function() { | ||
const latestBlock = await web3.eth.getBlock("latest", true); | ||
assert.deepStrictEqual(latestBlock, expectedLatestBlock); | ||
}); | ||
|
||
it("should have the same accounts", async function() { | ||
const accounts = await web3.eth.getAccounts(); | ||
assert.deepStrictEqual(accounts, expectedAccounts); | ||
}); | ||
|
||
it("should have the same nonces for known accounts", async function() { | ||
const nonces = expectedAccounts.map(async(account) => web3.eth.getTransactionCount(account)); | ||
assert.deepStrictEqual(nonces, expectedNonces); | ||
}); | ||
|
||
it("should have the same balances for known accounts", async function() { | ||
const balances = expectedAccounts.map(async(account) => web3.eth.getBalance(account)); | ||
assert.deepStrictEqual(balances, expectedBalances); | ||
}); | ||
|
||
it("should have a lower balance for account 0 as compared to account 1", async function() { | ||
const acct0Balance = await web3.eth.getBalance(expectedAccounts[0]); | ||
const acct1Balance = await web3.eth.getBalance(expectedAccounts[1]); | ||
|
||
assert(new BN(acct0Balance).lt(new BN(acct1Balance))); | ||
}); | ||
|
||
it("should allow further simple value transactions with the same accounts", async function() { | ||
const account0InitialBalance = new BN(await web3.eth.getBalance(expectedAccounts[0])); | ||
const account1InitialBalance = new BN(await web3.eth.getBalance(expectedAccounts[1])); | ||
|
||
// returns BN because we pass a BN | ||
const value = web3.utils.toWei(new BN(1), "ether"); | ||
|
||
// first, send the transaction | ||
const receipt = await web3.eth.sendTransaction({ | ||
from: expectedAccounts[0], | ||
to: expectedAccounts[1], | ||
value: web3.utils.toWei("1", "ether") | ||
}); | ||
|
||
assert.strictEqual(receipt.status, true); | ||
|
||
const txData = await web3.eth.getTransaction(receipt.transactionHash); | ||
const gasPrice = new BN(txData.gasPrice); | ||
const gasUsed = new BN(receipt.gasUsed); | ||
|
||
const gasCost = gasUsed.mul(gasPrice); | ||
|
||
const expectedAccount0FinalBalance = account0InitialBalance.sub(value).sub(gasCost); | ||
const expectedAccount1FinalBalance = account1InitialBalance.add(value); | ||
|
||
const account0FinalBalance = new BN(await web3.eth.getBalance(expectedAccounts[0])); | ||
const account1FinalBalance = new BN(await web3.eth.getBalance(expectedAccounts[1])); | ||
|
||
assert( | ||
account0FinalBalance.eq(expectedAccount0FinalBalance), | ||
"Account 0 final balance doesn't match expected final balance" | ||
); | ||
assert( | ||
account1FinalBalance.eq(expectedAccount1FinalBalance), | ||
"Account 1 final balance doesn't match expected final balance" | ||
); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only line in this file that changed. The others are formatting changes care of our good friend prettier.