|
3 | 3 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
4 | 4 | import { LegacyTransaction } from '@ethereumjs/tx'
|
5 | 5 | import { Account as EthAccount, Address } from '@ethereumjs/util'
|
| 6 | +import { generateKey } from '@ironfish/rust-nodejs' |
6 | 7 | import { Assert } from '../assert'
|
7 | 8 | import { createNodeTest, useAccountFixture } from '../testUtilities'
|
8 | 9 | import { EvmStateEncoding, HexStringEncoding } from './database'
|
@@ -66,4 +67,48 @@ describe('IronfishEvm', () => {
|
66 | 67 | expect(dbSizeAfter).toEqual(dbSizeBefore)
|
67 | 68 | })
|
68 | 69 | })
|
| 70 | + |
| 71 | + describe('getBalance', () => { |
| 72 | + const nodeTest = createNodeTest() |
| 73 | + |
| 74 | + it('fetches the account balance at the current state root', async () => { |
| 75 | + const key = generateKey() |
| 76 | + |
| 77 | + const address = Address.fromPrivateKey(Buffer.from(key.spendingKey, 'hex')) |
| 78 | + |
| 79 | + const { node } = nodeTest |
| 80 | + |
| 81 | + await node.chain.blockchainDb.stateManager.checkpoint() |
| 82 | + await node.chain.blockchainDb.stateManager.putAccount(address, new EthAccount(0n, 10n)) |
| 83 | + await node.chain.blockchainDb.stateManager.commit() |
| 84 | + |
| 85 | + const balance = await node.chain.evm.getBalance(address) |
| 86 | + |
| 87 | + expect(balance).toEqual(10n) |
| 88 | + }) |
| 89 | + |
| 90 | + it('fetches the account balance at the past state roots', async () => { |
| 91 | + const key = generateKey() |
| 92 | + |
| 93 | + const address = Address.fromPrivateKey(Buffer.from(key.spendingKey, 'hex')) |
| 94 | + |
| 95 | + const { node } = nodeTest |
| 96 | + |
| 97 | + await node.chain.blockchainDb.stateManager.checkpoint() |
| 98 | + await node.chain.blockchainDb.stateManager.putAccount(address, new EthAccount(0n, 10n)) |
| 99 | + await node.chain.blockchainDb.stateManager.commit() |
| 100 | + |
| 101 | + const stateRoot = await node.chain.blockchainDb.stateManager.getStateRoot() |
| 102 | + |
| 103 | + await node.chain.blockchainDb.stateManager.checkpoint() |
| 104 | + await node.chain.blockchainDb.stateManager.putAccount(address, new EthAccount(0n, 20n)) |
| 105 | + await node.chain.blockchainDb.stateManager.commit() |
| 106 | + |
| 107 | + const pastBalance = await node.chain.evm.getBalance(address, stateRoot) |
| 108 | + expect(pastBalance).toEqual(10n) |
| 109 | + |
| 110 | + const currentBalance = await node.chain.evm.getBalance(address) |
| 111 | + expect(currentBalance).toEqual(20n) |
| 112 | + }) |
| 113 | + }) |
69 | 114 | })
|
0 commit comments