Commit 172d6289 authored by Kelvin Fichter's avatar Kelvin Fichter

Lots of tests

parent eabb5dd8
...@@ -205,12 +205,12 @@ library Lib_OVMCodec { ...@@ -205,12 +205,12 @@ library Lib_OVMCodec {
// Unfortunately we can't create this array outright because // Unfortunately we can't create this array outright because
// RLPWriter.encodeList will reject fixed-size arrays. Assigning // RLPWriter.encodeList will reject fixed-size arrays. Assigning
// index-by-index circumvents this issue. // index-by-index circumvents this issue.
raw[0] = Lib_RLPWriter.encodeUint(_account.nonce); raw[0] = Lib_RLPWriter.writeUint(_account.nonce);
raw[1] = Lib_RLPWriter.encodeUint(_account.balance); raw[1] = Lib_RLPWriter.writeUint(_account.balance);
raw[2] = _account.storageRoot == 0 ? RLP_NULL_BYTES : Lib_RLPWriter.encodeBytes(abi.encodePacked(_account.storageRoot)); raw[2] = _account.storageRoot == 0 ? RLP_NULL_BYTES : Lib_RLPWriter.writeBytes(abi.encodePacked(_account.storageRoot));
raw[3] = _account.codeHash == 0 ? RLP_NULL_BYTES : Lib_RLPWriter.encodeBytes(abi.encodePacked(_account.codeHash)); raw[3] = _account.codeHash == 0 ? RLP_NULL_BYTES : Lib_RLPWriter.writeBytes(abi.encodePacked(_account.codeHash));
return Lib_RLPWriter.encodeList(raw); return Lib_RLPWriter.writeList(raw);
} }
/** /**
......
...@@ -20,7 +20,7 @@ library Lib_RLPWriter { ...@@ -20,7 +20,7 @@ library Lib_RLPWriter {
* @param _in The byte string to encode. * @param _in The byte string to encode.
* @return _out The RLP encoded string in bytes. * @return _out The RLP encoded string in bytes.
*/ */
function encodeBytes( function writeBytes(
bytes memory _in bytes memory _in
) )
internal internal
...@@ -34,7 +34,7 @@ library Lib_RLPWriter { ...@@ -34,7 +34,7 @@ library Lib_RLPWriter {
if (_in.length == 1 && uint8(_in[0]) < 128) { if (_in.length == 1 && uint8(_in[0]) < 128) {
encoded = _in; encoded = _in;
} else { } else {
encoded = Lib_BytesUtils.concat(encodeLength(_in.length, 128), _in); encoded = Lib_BytesUtils.concat(_writeLength(_in.length, 128), _in);
} }
return encoded; return encoded;
...@@ -45,7 +45,7 @@ library Lib_RLPWriter { ...@@ -45,7 +45,7 @@ library Lib_RLPWriter {
* @param _in The list of RLP encoded byte strings. * @param _in The list of RLP encoded byte strings.
* @return _out The RLP encoded list of items in bytes. * @return _out The RLP encoded list of items in bytes.
*/ */
function encodeList( function writeList(
bytes[] memory _in bytes[] memory _in
) )
internal internal
...@@ -54,8 +54,8 @@ library Lib_RLPWriter { ...@@ -54,8 +54,8 @@ library Lib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
bytes memory list = flatten(_in); bytes memory list = _flatten(_in);
return Lib_BytesUtils.concat(encodeLength(list.length, 192), list); return Lib_BytesUtils.concat(_writeLength(list.length, 192), list);
} }
/** /**
...@@ -63,7 +63,7 @@ library Lib_RLPWriter { ...@@ -63,7 +63,7 @@ library Lib_RLPWriter {
* @param _in The string to encode. * @param _in The string to encode.
* @return _out The RLP encoded string in bytes. * @return _out The RLP encoded string in bytes.
*/ */
function encodeString( function writeString(
string memory _in string memory _in
) )
internal internal
...@@ -72,7 +72,7 @@ library Lib_RLPWriter { ...@@ -72,7 +72,7 @@ library Lib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
return encodeBytes(bytes(_in)); return writeBytes(bytes(_in));
} }
/** /**
...@@ -80,7 +80,7 @@ library Lib_RLPWriter { ...@@ -80,7 +80,7 @@ library Lib_RLPWriter {
* @param _in The address to encode. * @param _in The address to encode.
* @return _out The RLP encoded address in bytes. * @return _out The RLP encoded address in bytes.
*/ */
function encodeAddress( function writeAddress(
address _in address _in
) )
internal internal
...@@ -97,7 +97,7 @@ library Lib_RLPWriter { ...@@ -97,7 +97,7 @@ library Lib_RLPWriter {
inputBytes := m inputBytes := m
} }
return encodeBytes(inputBytes); return writeBytes(inputBytes);
} }
/** /**
...@@ -105,7 +105,7 @@ library Lib_RLPWriter { ...@@ -105,7 +105,7 @@ library Lib_RLPWriter {
* @param _in The uint to encode. * @param _in The uint to encode.
* @return _out The RLP encoded uint in bytes. * @return _out The RLP encoded uint in bytes.
*/ */
function encodeUint( function writeUint(
uint _in uint _in
) )
internal internal
...@@ -114,7 +114,7 @@ library Lib_RLPWriter { ...@@ -114,7 +114,7 @@ library Lib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
return encodeBytes(toBinary(_in)); return writeBytes(_toBinary(_in));
} }
/** /**
...@@ -122,7 +122,7 @@ library Lib_RLPWriter { ...@@ -122,7 +122,7 @@ library Lib_RLPWriter {
* @param _in The int to encode. * @param _in The int to encode.
* @return _out The RLP encoded int in bytes. * @return _out The RLP encoded int in bytes.
*/ */
function encodeInt( function writeInt(
int _in int _in
) )
internal internal
...@@ -131,7 +131,7 @@ library Lib_RLPWriter { ...@@ -131,7 +131,7 @@ library Lib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
return encodeUint(uint(_in)); return writeUint(uint(_in));
} }
/** /**
...@@ -139,7 +139,7 @@ library Lib_RLPWriter { ...@@ -139,7 +139,7 @@ library Lib_RLPWriter {
* @param _in The bool to encode. * @param _in The bool to encode.
* @return _out The RLP encoded bool in bytes. * @return _out The RLP encoded bool in bytes.
*/ */
function encodeBool( function writeBool(
bool _in bool _in
) )
internal internal
...@@ -164,7 +164,7 @@ library Lib_RLPWriter { ...@@ -164,7 +164,7 @@ library Lib_RLPWriter {
* @param _offset 128 if item is string, 192 if item is list. * @param _offset 128 if item is string, 192 if item is list.
* @return _encoded RLP encoded bytes. * @return _encoded RLP encoded bytes.
*/ */
function encodeLength( function _writeLength(
uint _len, uint _len,
uint _offset uint _offset
) )
...@@ -203,7 +203,7 @@ library Lib_RLPWriter { ...@@ -203,7 +203,7 @@ library Lib_RLPWriter {
* @param _x The integer to encode. * @param _x The integer to encode.
* @return _binary RLP encoded bytes. * @return _binary RLP encoded bytes.
*/ */
function toBinary( function _toBinary(
uint _x uint _x
) )
private private
...@@ -239,7 +239,7 @@ library Lib_RLPWriter { ...@@ -239,7 +239,7 @@ library Lib_RLPWriter {
* @param _src Source location. * @param _src Source location.
* @param _len Length of memory to copy. * @param _len Length of memory to copy.
*/ */
function memcpy( function _memcpy(
uint _dest, uint _dest,
uint _src, uint _src,
uint _len uint _len
...@@ -273,7 +273,7 @@ library Lib_RLPWriter { ...@@ -273,7 +273,7 @@ library Lib_RLPWriter {
* @param _list List of byte strings to flatten. * @param _list List of byte strings to flatten.
* @return _flattened The flattened byte string. * @return _flattened The flattened byte string.
*/ */
function flatten( function _flatten(
bytes[] memory _list bytes[] memory _list
) )
private private
...@@ -302,7 +302,7 @@ library Lib_RLPWriter { ...@@ -302,7 +302,7 @@ library Lib_RLPWriter {
uint listPtr; uint listPtr;
assembly { listPtr := add(item, 0x20)} assembly { listPtr := add(item, 0x20)}
memcpy(flattenedPtr, listPtr, item.length); _memcpy(flattenedPtr, listPtr, item.length);
flattenedPtr += _list[i].length; flattenedPtr += _list[i].length;
} }
......
...@@ -739,7 +739,7 @@ library Lib_MerkleTrie { ...@@ -739,7 +739,7 @@ library Lib_MerkleTrie {
TrieNode memory _node TrieNode memory _node
) )
{ {
bytes memory encoded = Lib_RLPWriter.encodeList(_raw); bytes memory encoded = Lib_RLPWriter.writeList(_raw);
return TrieNode({ return TrieNode({
encoded: encoded, encoded: encoded,
...@@ -786,8 +786,8 @@ library Lib_MerkleTrie { ...@@ -786,8 +786,8 @@ library Lib_MerkleTrie {
{ {
bytes[] memory raw = new bytes[](2); bytes[] memory raw = new bytes[](2);
bytes memory key = _addHexPrefix(_key, false); bytes memory key = _addHexPrefix(_key, false);
raw[0] = Lib_RLPWriter.encodeBytes(Lib_BytesUtils.fromNibbles(key)); raw[0] = Lib_RLPWriter.writeBytes(Lib_BytesUtils.fromNibbles(key));
raw[1] = Lib_RLPWriter.encodeBytes(_value); raw[1] = Lib_RLPWriter.writeBytes(_value);
return _makeNode(raw); return _makeNode(raw);
} }
...@@ -812,8 +812,8 @@ library Lib_MerkleTrie { ...@@ -812,8 +812,8 @@ library Lib_MerkleTrie {
{ {
bytes[] memory raw = new bytes[](2); bytes[] memory raw = new bytes[](2);
bytes memory key = _addHexPrefix(_key, true); bytes memory key = _addHexPrefix(_key, true);
raw[0] = Lib_RLPWriter.encodeBytes(Lib_BytesUtils.fromNibbles(key)); raw[0] = Lib_RLPWriter.writeBytes(Lib_BytesUtils.fromNibbles(key));
raw[1] = Lib_RLPWriter.encodeBytes(_value); raw[1] = Lib_RLPWriter.writeBytes(_value);
return _makeNode(raw); return _makeNode(raw);
} }
...@@ -851,7 +851,7 @@ library Lib_MerkleTrie { ...@@ -851,7 +851,7 @@ library Lib_MerkleTrie {
TrieNode memory _updatedNode TrieNode memory _updatedNode
) )
{ {
bytes memory encoded = Lib_RLPWriter.encodeBytes(_value); bytes memory encoded = Lib_RLPWriter.writeBytes(_value);
_branch.decoded[_branch.decoded.length - 1] = Lib_RLPReader.toRLPItem(encoded); _branch.decoded[_branch.decoded.length - 1] = Lib_RLPReader.toRLPItem(encoded);
return _makeNode(_branch.decoded); return _makeNode(_branch.decoded);
} }
...@@ -874,7 +874,7 @@ library Lib_MerkleTrie { ...@@ -874,7 +874,7 @@ library Lib_MerkleTrie {
TrieNode memory _updatedNode TrieNode memory _updatedNode
) )
{ {
bytes memory encoded = _value.length < 32 ? _value : Lib_RLPWriter.encodeBytes(_value); bytes memory encoded = _value.length < 32 ? _value : Lib_RLPWriter.writeBytes(_value);
_branch.decoded[_index] = Lib_RLPReader.toRLPItem(encoded); _branch.decoded[_index] = Lib_RLPReader.toRLPItem(encoded);
return _makeNode(_branch.decoded); return _makeNode(_branch.decoded);
} }
......
...@@ -151,10 +151,10 @@ library Lib_EthUtils { ...@@ -151,10 +151,10 @@ library Lib_EthUtils {
) )
{ {
bytes[] memory encoded = new bytes[](2); bytes[] memory encoded = new bytes[](2);
encoded[0] = Lib_RLPWriter.encodeAddress(_creator); encoded[0] = Lib_RLPWriter.writeAddress(_creator);
encoded[1] = Lib_RLPWriter.encodeUint(_nonce); encoded[1] = Lib_RLPWriter.writeUint(_nonce);
bytes memory encodedList = Lib_RLPWriter.encodeList(encoded); bytes memory encodedList = Lib_RLPWriter.writeList(encoded);
return getAddressFromHash(keccak256(encodedList)); return getAddressFromHash(keccak256(encodedList));
} }
......
...@@ -10,7 +10,7 @@ import { Lib_RLPWriter } from "../../optimistic-ethereum/libraries/rlp/Lib_RLPWr ...@@ -10,7 +10,7 @@ import { Lib_RLPWriter } from "../../optimistic-ethereum/libraries/rlp/Lib_RLPWr
*/ */
contract TestLib_RLPWriter { contract TestLib_RLPWriter {
function encodeBytes( function writeBytes(
bytes memory _in bytes memory _in
) )
public public
...@@ -19,10 +19,10 @@ contract TestLib_RLPWriter { ...@@ -19,10 +19,10 @@ contract TestLib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
return Lib_RLPWriter.encodeBytes(_in); return Lib_RLPWriter.writeBytes(_in);
} }
function encodeList( function writeList(
bytes[] memory _in bytes[] memory _in
) )
public public
...@@ -31,10 +31,10 @@ contract TestLib_RLPWriter { ...@@ -31,10 +31,10 @@ contract TestLib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
return Lib_RLPWriter.encodeList(_in); return Lib_RLPWriter.writeList(_in);
} }
function encodeString( function writeString(
string memory _in string memory _in
) )
public public
...@@ -43,10 +43,10 @@ contract TestLib_RLPWriter { ...@@ -43,10 +43,10 @@ contract TestLib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
return Lib_RLPWriter.encodeString(_in); return Lib_RLPWriter.writeString(_in);
} }
function encodeAddress( function writeAddress(
address _in address _in
) )
public public
...@@ -55,10 +55,10 @@ contract TestLib_RLPWriter { ...@@ -55,10 +55,10 @@ contract TestLib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
return Lib_RLPWriter.encodeAddress(_in); return Lib_RLPWriter.writeAddress(_in);
} }
function encodeUint( function writeUint(
uint _in uint _in
) )
public public
...@@ -67,10 +67,10 @@ contract TestLib_RLPWriter { ...@@ -67,10 +67,10 @@ contract TestLib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
return Lib_RLPWriter.encodeUint(_in); return Lib_RLPWriter.writeUint(_in);
} }
function encodeInt( function writeInt(
int _in int _in
) )
public public
...@@ -79,10 +79,10 @@ contract TestLib_RLPWriter { ...@@ -79,10 +79,10 @@ contract TestLib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
return Lib_RLPWriter.encodeInt(_in); return Lib_RLPWriter.writeInt(_in);
} }
function encodeBool( function writeBool(
bool _in bool _in
) )
public public
...@@ -91,6 +91,6 @@ contract TestLib_RLPWriter { ...@@ -91,6 +91,6 @@ contract TestLib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
return Lib_RLPWriter.encodeBool(_in); return Lib_RLPWriter.writeBool(_in);
} }
} }
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
"build:dump": "ts-node \"bin/take-dump.ts\"", "build:dump": "ts-node \"bin/take-dump.ts\"",
"build:copy": "copyfiles -u 2 \"contracts/optimistic-ethereum/**/*.sol\" \"build/contracts\"", "build:copy": "copyfiles -u 2 \"contracts/optimistic-ethereum/**/*.sol\" \"build/contracts\"",
"test": "yarn run test:contracts", "test": "yarn run test:contracts",
"test:contracts": "buidler test \"test/contracts/libraries/rlp/Lib_RLPReader.spec.ts\" --show-stack-traces", "test:contracts": "buidler test --show-stack-traces",
"lint": "yarn run lint:typescript", "lint": "yarn run lint:typescript",
"lint:typescript": "tslint --format stylish --project .", "lint:typescript": "tslint --format stylish --project .",
"lint:fix": "yarn run lint:fix:typescript", "lint:fix": "yarn run lint:fix:typescript",
......
...@@ -10,8 +10,8 @@ import { DUMMY_ACCOUNTS, DUMMY_BYTES32, ZERO_ADDRESS } from '../../../helpers' ...@@ -10,8 +10,8 @@ import { DUMMY_ACCOUNTS, DUMMY_BYTES32, ZERO_ADDRESS } from '../../../helpers'
const EMPTY_ACCOUNT_CODE_HASH = const EMPTY_ACCOUNT_CODE_HASH =
'0x00004B1DC0DE000000004B1DC0DE000000004B1DC0DE000000004B1DC0DE0000' '0x00004B1DC0DE000000004B1DC0DE000000004B1DC0DE000000004B1DC0DE0000'
const RLP_NULL_HASH = const KECCAK_256_NULL =
'0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421' '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
describe('OVM_StateManager', () => { describe('OVM_StateManager', () => {
let signer1: Signer let signer1: Signer
...@@ -321,7 +321,7 @@ describe('OVM_StateManager', () => { ...@@ -321,7 +321,7 @@ describe('OVM_StateManager', () => {
) )
).to.deep.include({ ).to.deep.include({
nonce: BigNumber.from(1), nonce: BigNumber.from(1),
codeHash: RLP_NULL_HASH, codeHash: KECCAK_256_NULL,
isFresh: true, isFresh: true,
}) })
}) })
......
...@@ -18,7 +18,7 @@ import { ...@@ -18,7 +18,7 @@ import {
import { MockContract, smockit } from '@eth-optimism/smock' import { MockContract, smockit } from '@eth-optimism/smock'
import { keccak256 } from 'ethers/lib/utils' import { keccak256 } from 'ethers/lib/utils'
describe('OVM_StateTransitioner', () => { describe.skip('OVM_StateTransitioner', () => {
let AddressManager: Contract let AddressManager: Contract
before(async () => { before(async () => {
AddressManager = await makeAddressManager() AddressManager = await makeAddressManager()
...@@ -75,34 +75,39 @@ describe('OVM_StateTransitioner', () => { ...@@ -75,34 +75,39 @@ describe('OVM_StateTransitioner', () => {
}) })
describe('proveContractState', () => { describe('proveContractState', () => {
let ovmContractAddress = NON_ZERO_ADDRESS
let ethContractAddress = ZERO_ADDRESS
let account: any let account: any
beforeEach(() => { beforeEach(() => {
Mock__OVM_StateManager.smocked.hasAccount.will.return.with(false)
account = { account = {
nonce: 0, nonce: 0,
balance: 0, balance: 0,
storageRoot: NULL_BYTES32, storageRoot: NULL_BYTES32,
codeHash: NULL_BYTES32, codeHash: NULL_BYTES32,
ethAddress: ZERO_ADDRESS,
isFresh: false,
} }
}) })
describe('when provided an invalid code hash', () => { describe('when provided an invalid code hash', () => {
beforeEach(() => { beforeEach(() => {
account.ethAddress = NON_ZERO_ADDRESS
account.codeHash = NON_NULL_BYTES32 account.codeHash = NON_NULL_BYTES32
}) })
it('should revert', async () => { it('should revert', async () => {
await expect( await expect(
OVM_StateTransitioner.proveContractState(ZERO_ADDRESS, account, '0x') OVM_StateTransitioner.proveContractState(
ovmContractAddress,
ethContractAddress,
account,
'0x'
)
).to.be.revertedWith('Invalid code hash provided.') ).to.be.revertedWith('Invalid code hash provided.')
}) })
}) })
describe('when provided a valid code hash', () => { describe('when provided a valid code hash', () => {
beforeEach(async () => { beforeEach(async () => {
account.ethAddress = OVM_StateTransitioner.address ethContractAddress = OVM_StateTransitioner.address
account.codeHash = keccak256( account.codeHash = keccak256(
await ethers.provider.getCode(OVM_StateTransitioner.address) await ethers.provider.getCode(OVM_StateTransitioner.address)
) )
...@@ -114,7 +119,8 @@ describe('OVM_StateTransitioner', () => { ...@@ -114,7 +119,8 @@ describe('OVM_StateTransitioner', () => {
it('should revert', async () => { it('should revert', async () => {
await expect( await expect(
OVM_StateTransitioner.proveContractState( OVM_StateTransitioner.proveContractState(
ZERO_ADDRESS, ovmContractAddress,
ethContractAddress,
account, account,
proof proof
) )
...@@ -129,13 +135,13 @@ describe('OVM_StateTransitioner', () => { ...@@ -129,13 +135,13 @@ describe('OVM_StateTransitioner', () => {
accounts: [ accounts: [
{ {
...account, ...account,
address: NON_ZERO_ADDRESS, address: ovmContractAddress,
}, },
], ],
secure: true, secure: true,
}) })
const test = await generator.makeAccountProofTest(NON_ZERO_ADDRESS) const test = await generator.makeAccountProofTest(ovmContractAddress)
proof = test.accountTrieWitness proof = test.accountTrieWitness
...@@ -150,7 +156,8 @@ describe('OVM_StateTransitioner', () => { ...@@ -150,7 +156,8 @@ describe('OVM_StateTransitioner', () => {
it('should put the account in the state manager', async () => { it('should put the account in the state manager', async () => {
await expect( await expect(
OVM_StateTransitioner.proveContractState( OVM_StateTransitioner.proveContractState(
NON_ZERO_ADDRESS, ovmContractAddress,
ethContractAddress,
account, account,
proof proof
) )
...@@ -175,6 +182,10 @@ describe('OVM_StateTransitioner', () => { ...@@ -175,6 +182,10 @@ describe('OVM_StateTransitioner', () => {
}) })
describe('proveStorageSlot', () => { describe('proveStorageSlot', () => {
beforeEach(() => {
Mock__OVM_StateManager.smocked.hasContractStorage.will.return.with(false)
})
describe('when the corresponding account is not proven', () => { describe('when the corresponding account is not proven', () => {
beforeEach(() => { beforeEach(() => {
Mock__OVM_StateManager.smocked.hasAccount.will.return.with(false) Mock__OVM_StateManager.smocked.hasAccount.will.return.with(false)
...@@ -186,7 +197,6 @@ describe('OVM_StateTransitioner', () => { ...@@ -186,7 +197,6 @@ describe('OVM_StateTransitioner', () => {
NON_ZERO_ADDRESS, NON_ZERO_ADDRESS,
NON_NULL_BYTES32, NON_NULL_BYTES32,
NON_NULL_BYTES32, NON_NULL_BYTES32,
'0x',
'0x' '0x'
) )
).to.be.revertedWith( ).to.be.revertedWith(
...@@ -209,8 +219,6 @@ describe('OVM_StateTransitioner', () => { ...@@ -209,8 +219,6 @@ describe('OVM_StateTransitioner', () => {
balance: 0, balance: 0,
storageRoot: NULL_BYTES32, storageRoot: NULL_BYTES32,
codeHash: NULL_BYTES32, codeHash: NULL_BYTES32,
ethAddress: ZERO_ADDRESS,
isFresh: false,
} }
const generator = await TrieTestGenerator.fromAccounts({ const generator = await TrieTestGenerator.fromAccounts({
......
...@@ -16,13 +16,13 @@ const encode = async (Lib_RLPWriter: Contract, input: any): Promise<void> => { ...@@ -16,13 +16,13 @@ const encode = async (Lib_RLPWriter: Contract, input: any): Promise<void> => {
}) })
) )
return Lib_RLPWriter.encodeList(elements) return Lib_RLPWriter.writeList(elements)
} else if (Number.isInteger(input)) { } else if (Number.isInteger(input)) {
return Lib_RLPWriter.encodeUint(input) return Lib_RLPWriter.writeUint(input)
} else if (input[0] === '#') { } else if (input[0] === '#') {
return Lib_RLPWriter.encodeInt(input.slice(1)) return Lib_RLPWriter.writeInt(input.slice(1))
} else { } else {
return Lib_RLPWriter.encodeString(input) return Lib_RLPWriter.writeString(input)
} }
} }
......
import { expect } from '../../../setup'
/* External Imports */
import { ethers } from '@nomiclabs/buidler'
import { Contract } from 'ethers'
/* Internal Imports */
import { TrieTestGenerator, NON_NULL_BYTES32 } from '../../../helpers'
import { keccak256 } from 'ethers/lib/utils'
const makeDummyAccounts = (count: number): any[] => {
return [...Array(count)].map((x, idx) => {
return {
address: '0xc0de' + `${idx.toString(16)}`.padStart(36, '0'),
nonce: 0,
balance: 0,
codeHash: null,
storage: [
{
key: keccak256('0x1234'),
val: keccak256('0x5678'),
},
],
}
})
}
const NODE_COUNTS = [1, 2, 128, 256, 512, 1024, 2048, 4096]
describe('Lib_EthMerkleTrie', () => {
let Lib_EthMerkleTrie: Contract
before(async () => {
Lib_EthMerkleTrie = await (
await ethers.getContractFactory('TestLib_EthMerkleTrie')
).deploy()
})
describe('proveAccountStorageSlotValue', () => {})
describe('updateAccountStorageSlotValue', () => {})
describe('proveAccountState', () => {
for (const nodeCount of NODE_COUNTS) {
describe(`inside a trie with ${nodeCount} nodes`, () => {
let generator: TrieTestGenerator
before(async () => {
generator = await TrieTestGenerator.fromAccounts({
accounts: makeDummyAccounts(nodeCount),
secure: true,
})
})
for (
let i = 0;
i < nodeCount;
i += nodeCount / (nodeCount > 8 ? 8 : 1)
) {
it(`should correctly prove inclusion for node #${i}`, async () => {
const test = await generator.makeAccountProofTest(i)
expect(
await Lib_EthMerkleTrie.proveAccountState(
test.address,
test.account,
test.accountTrieWitness,
test.accountTrieRoot
)
).to.equal(true)
})
}
})
}
})
describe.only('updateAccountState', () => {
for (const nodeCount of NODE_COUNTS) {
describe(`inside a trie with ${nodeCount} nodes`, () => {
let generator: TrieTestGenerator
before(async () => {
generator = await TrieTestGenerator.fromAccounts({
accounts: makeDummyAccounts(nodeCount),
secure: true,
})
})
for (
let i = 0;
i < nodeCount;
i += nodeCount / (nodeCount > 8 ? 8 : 1)
) {
it(`should correctly update node #${i}`, async () => {
const test = await generator.makeAccountUpdateTest(i, {
nonce: 1234,
balance: 5678,
codeHash: NON_NULL_BYTES32,
storageRoot: NON_NULL_BYTES32,
})
expect(
await Lib_EthMerkleTrie.updateAccountState(
test.address,
test.account,
test.accountTrieWitness,
test.accountTrieRoot
)
).to.equal(test.newAccountTrieRoot)
})
}
})
}
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment