-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1677 from ethereum-optimism/genesis-json-provider
core-utils: add GenesisJsonProvider and fix tests
- Loading branch information
Showing
15 changed files
with
670 additions
and
26 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@eth-optimism/core-utils': patch | ||
--- | ||
|
||
Add bytes32ify |
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
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
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,39 @@ | ||
// Optimism PBC 2021 | ||
|
||
// Represents the ethereum state | ||
export interface State { | ||
[address: string]: { | ||
nonce: number | ||
balance: string | ||
codeHash: string | ||
root: string | ||
code?: string | ||
storage?: { | ||
[key: string]: string | ||
} | ||
} | ||
} | ||
|
||
// Represents a genesis file that geth can consume | ||
export interface Genesis { | ||
config: { | ||
chainId: number | ||
homesteadBlock: number | ||
eip150Block: number | ||
eip155Block: number | ||
eip158Block: number | ||
byzantiumBlock: number | ||
constantinopleBlock: number | ||
petersburgBlock: number | ||
istanbulBlock: number | ||
muirGlacierBlock: number | ||
clique: { | ||
period: number | ||
epoch: number | ||
} | ||
} | ||
difficulty: string | ||
gasLimit: string | ||
extraData: string | ||
alloc: State | ||
} |
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
4 changes: 2 additions & 2 deletions
4
packages/core-utils/test/fees/fees.spec.ts → packages/core-utils/test/fees.spec.ts
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
4 changes: 2 additions & 2 deletions
4
packages/core-utils/test/common/misc.spec.ts → packages/core-utils/test/misc.spec.ts
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
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,121 @@ | ||
import { expect } from '@eth-optimism/core-utils/test/setup' | ||
import { ethers, BigNumber } from 'ethers' | ||
import { GenesisJsonProvider } from './provider' | ||
import { Genesis } from '@eth-optimism/core-utils/src/types' | ||
import { | ||
remove0x, | ||
add0x, | ||
} from '@eth-optimism/core-utils/src/common/hex-strings' | ||
import { KECCAK256_RLP_S, KECCAK256_NULL_S } from 'ethereumjs-util' | ||
|
||
const account = '0x66a84544bed4ca45b3c024776812abf87728fbaf' | ||
|
||
const genesis: Genesis = { | ||
config: { | ||
chainId: 0, | ||
homesteadBlock: 0, | ||
eip150Block: 0, | ||
eip155Block: 0, | ||
eip158Block: 0, | ||
byzantiumBlock: 0, | ||
constantinopleBlock: 0, | ||
petersburgBlock: 0, | ||
istanbulBlock: 0, | ||
muirGlacierBlock: 0, | ||
clique: { | ||
period: 0, | ||
epoch: 0, | ||
}, | ||
}, | ||
difficulty: '0x', | ||
gasLimit: '0x', | ||
extraData: '0x', | ||
alloc: { | ||
[remove0x(account)]: { | ||
nonce: 101, | ||
balance: '234', | ||
codeHash: ethers.utils.keccak256('0x6080'), | ||
root: '0x', | ||
code: '6080', | ||
storage: { | ||
'0000000000000000000000000000000000000000000000000000000000000002': | ||
'989680', | ||
'0000000000000000000000000000000000000000000000000000000000000003': | ||
'536f6d65205265616c6c7920436f6f6c20546f6b656e204e616d650000000036', | ||
'7d55c28652d09dd36b33c69e81e67cbe8d95f51dc46ab5b17568d616d481854d': | ||
'989680', | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
describe.only('GenesisJsonProvider', () => { | ||
let provider | ||
before(() => { | ||
provider = new GenesisJsonProvider(genesis) | ||
}) | ||
|
||
it('should get nonce', async () => { | ||
const nonce = await provider.getTransactionCount(account) | ||
expect(nonce).to.deep.eq(101) | ||
}) | ||
|
||
it('should get nonce on missing account', async () => { | ||
const nonce = await provider.getTransactionCount('0x') | ||
expect(nonce).to.deep.eq(0) | ||
}) | ||
|
||
it('should get code', async () => { | ||
const code = await provider.getCode(account) | ||
expect(code).to.deep.eq('0x6080') | ||
}) | ||
|
||
it('should get code on missing account', async () => { | ||
const code = await provider.getCode('0x') | ||
expect(code).to.deep.eq('0x') | ||
}) | ||
|
||
it('should get balance', async () => { | ||
const balance = await provider.getBalance(account) | ||
expect(balance.toString()).to.deep.eq(BigNumber.from(234).toString()) | ||
}) | ||
|
||
it('should get balance on missing account', async () => { | ||
const balance = await provider.getBalance('0x') | ||
expect(balance.toString()).to.deep.eq('0') | ||
}) | ||
|
||
it('should get storage', async () => { | ||
const storage = await provider.getStorageAt(account, 2) | ||
expect(storage).to.deep.eq('0x989680') | ||
}) | ||
|
||
it('should get storage of missing account', async () => { | ||
const storage = await provider.getStorageAt('0x', 0) | ||
expect(storage).to.deep.eq('0x') | ||
}) | ||
|
||
it('should get storage of missing slot', async () => { | ||
const storage = await provider.getStorageAt(account, 9999999999999) | ||
expect(storage).to.deep.eq('0x') | ||
}) | ||
|
||
it('should call eth_getProof', async () => { | ||
const proof = await provider.send('eth_getProof', [account]) | ||
// There is code at the account, so it shouldn't be the null code hash | ||
expect(proof.codeHash).to.not.eq(add0x(KECCAK256_NULL_S)) | ||
// There is storage so it should not be the null storage hash | ||
expect(proof.storageHash).to.not.eq(add0x(KECCAK256_RLP_S)) | ||
}) | ||
|
||
it('should call eth_getProof on missing account', async () => { | ||
const proof = await provider.send('eth_getProof', ['0x']) | ||
expect(proof.codeHash).to.eq(add0x(KECCAK256_NULL_S)) | ||
expect(proof.storageHash).to.eq(add0x(KECCAK256_RLP_S)) | ||
}) | ||
|
||
it('should also initialize correctly with state dump', async () => { | ||
provider = new GenesisJsonProvider(genesis.alloc) | ||
expect(provider).to.be.instanceOf(GenesisJsonProvider) | ||
}) | ||
}) |
Oops, something went wrong.