Skip to content

Commit

Permalink
Merge branch 'master' into engine-node-16
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 authored Aug 17, 2022
2 parents 1af48be + d9b021d commit f25d1af
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/blockchain/src/genesisStates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function genesisStateRoot(genesisState: GenesisState) {
)
await storageTrie.put(storageKey, storageVal)
}
account.stateRoot = storageTrie.root
account.storageRoot = storageTrie.root
}
}
await trie.put(address, account.serialize())
Expand Down
2 changes: 1 addition & 1 deletion packages/evm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ declare type Proof = {
storageProof: StorageProof[]
}

type AccountFields = Partial<Pick<Account, 'nonce' | 'balance' | 'stateRoot' | 'codeHash'>>
type AccountFields = Partial<Pick<Account, 'nonce' | 'balance' | 'storageRoot' | 'codeHash'>>

interface StateAccess {
accountExists(address: Address): Promise<boolean>
Expand Down
4 changes: 2 additions & 2 deletions packages/statemanager/src/baseStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ export abstract class BaseStateManager {
/**
* Gets the account associated with `address`, modifies the given account
* fields, then saves the account into state. Account fields can include
* `nonce`, `balance`, `stateRoot`, and `codeHash`.
* `nonce`, `balance`, `storageRoot`, and `codeHash`.
* @param address - Address of the account to modify
* @param accountFields - Object containing account fields and values to modify
*/
async modifyAccountFields(address: Address, accountFields: AccountFields): Promise<void> {
const account = await this.getAccount(address)
account.nonce = accountFields.nonce ?? account.nonce
account.balance = accountFields.balance ?? account.balance
account.stateRoot = accountFields.stateRoot ?? account.stateRoot
account.storageRoot = accountFields.storageRoot ?? account.storageRoot
account.codeHash = accountFields.codeHash ?? account.codeHash
await this.putAccount(address, account)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/statemanager/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface StorageDump {
[key: string]: string
}

export type AccountFields = Partial<Pick<Account, 'nonce' | 'balance' | 'stateRoot' | 'codeHash'>>
export type AccountFields = Partial<Pick<Account, 'nonce' | 'balance' | 'storageRoot' | 'codeHash'>>

export interface StateAccess {
accountExists(address: Address): Promise<boolean>
Expand Down
12 changes: 6 additions & 6 deletions packages/statemanager/src/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage
// from state trie
const account = await this.getAccount(address)
const storageTrie = this._trie.copy(false)
storageTrie.root = account.stateRoot
storageTrie.root = account.storageRoot
storageTrie.db.checkpoints = []
return storageTrie
}
Expand Down Expand Up @@ -226,9 +226,9 @@ export class DefaultStateManager extends BaseStateManager implements StateManage
const addressHex = address.buf.toString('hex')
this._storageTries[addressHex] = storageTrie

// update contract stateRoot
// update contract storageRoot
const contract = this._cache.get(address)
contract.stateRoot = storageTrie.root
contract.storageRoot = storageTrie.root

await this.putAccount(address, contract)
resolve()
Expand Down Expand Up @@ -347,7 +347,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage
balance: bigIntToHex(account.balance),
codeHash: bufferToHex(account.codeHash),
nonce: bigIntToHex(account.nonce),
storageHash: bufferToHex(account.stateRoot),
storageHash: bufferToHex(account.storageRoot),
accountProof,
storageProof,
}
Expand Down Expand Up @@ -391,15 +391,15 @@ export class DefaultStateManager extends BaseStateManager implements StateManage
}
} else {
const account = Account.fromRlpSerializedAccount(value)
const { nonce, balance, stateRoot, codeHash } = account
const { nonce, balance, storageRoot, codeHash } = account
const invalidErrorMsg = 'Invalid proof provided:'
if (nonce !== BigInt(proof.nonce)) {
throw new Error(`${invalidErrorMsg} nonce does not match`)
}
if (balance !== BigInt(proof.balance)) {
throw new Error(`${invalidErrorMsg} balance does not match`)
}
if (!stateRoot.equals(toBuffer(proof.storageHash))) {
if (!storageRoot.equals(toBuffer(proof.storageHash))) {
throw new Error(`${invalidErrorMsg} storageHash does not match`)
}
if (!codeHash.equals(toBuffer(proof.codeHash))) {
Expand Down
12 changes: 6 additions & 6 deletions packages/statemanager/tests/stateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ tape('StateManager', (t) => {
'd748bf26ab37599c944babfdbeecf6690801bd61bf2670efb0a34adfc6dca10b',
'hex'
),
stateRoot: Buffer.from(
storageRoot: Buffer.from(
'cafd881ab193703b83816c49ff6c2bf6ba6f464a1be560c42106128c8dbc35e7',
'hex'
),
Expand All @@ -207,7 +207,7 @@ tape('StateManager', (t) => {
'd748bf26ab37599c944babfdbeecf6690801bd61bf2670efb0a34adfc6dca10b'
)
st.equal(
res3.stateRoot.toString('hex'),
res3.storageRoot.toString('hex'),
'cafd881ab193703b83816c49ff6c2bf6ba6f464a1be560c42106128c8dbc35e7'
)

Expand All @@ -232,18 +232,18 @@ tape('StateManager', (t) => {
'd748bf26ab37599c944babfdbeecf6690801bd61bf2670efb0a34adfc6dca10b',
'hex'
)
const newStateRoot = Buffer.from(
const newStorageRoot = Buffer.from(
'cafd881ab193703b83816c49ff6c2bf6ba6f464a1be560c42106128c8dbc35e7',
'hex'
)
await stateManager.modifyAccountFields(address, {
codeHash: newCodeHash,
stateRoot: newStateRoot,
storageRoot: newStorageRoot,
})

const res3 = await stateManager.getAccount(address)
st.ok(res3.codeHash.equals(newCodeHash))
st.ok(res3.stateRoot.equals(newStateRoot))
st.ok(res3.storageRoot.equals(newStorageRoot))
st.end()
}
)
Expand Down Expand Up @@ -365,7 +365,7 @@ tape('StateManager', (t) => {
st.ok(slotCode.length === 0, 'code cannot be loaded') // This test fails if no code prefix is used

account = await codeStateManager.getAccount(address1)
account.stateRoot = root
account.storageRoot = root

await codeStateManager.putAccount(address1, account)

Expand Down
6 changes: 3 additions & 3 deletions packages/trie/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ tape('it should create the genesis state root from ethereum', function (tester)
const v = Buffer.from('1e12515ce3e0f817a4ddef9ca55788a1d66bd2df', 'hex')
const a = Buffer.from('1a26338f0d905e295fccb71fa9ea849ffa12aaf4', 'hex')

const stateRoot = Buffer.alloc(32)
stateRoot.fill(0)
const storageRoot = Buffer.alloc(32)
storageRoot.fill(0)

const startAmount = Buffer.alloc(26)
startAmount.fill(0)
startAmount[0] = 1

const account = [startAmount, 0, stateRoot, KECCAK256_NULL]
const account = [startAmount, 0, storageRoot, KECCAK256_NULL]
const rlpAccount = Buffer.from(RLP.encode(bufArrToArr(account as Buffer[])))
const cppRlp =
'f85e9a010000000000000000000000000000000000000000000000000080a00000000000000000000000000000000000000000000000000000000000000000a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
Expand Down
22 changes: 11 additions & 11 deletions packages/util/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ const _0n = BigInt(0)
export interface AccountData {
nonce?: BigIntLike
balance?: BigIntLike
stateRoot?: BufferLike
storageRoot?: BufferLike
codeHash?: BufferLike
}

export class Account {
nonce: bigint
balance: bigint
stateRoot: Buffer
storageRoot: Buffer
codeHash: Buffer

static fromAccountData(accountData: AccountData) {
const { nonce, balance, stateRoot, codeHash } = accountData
const { nonce, balance, storageRoot, codeHash } = accountData

return new Account(
isTruthy(nonce) ? bufferToBigInt(toBuffer(nonce)) : undefined,
isTruthy(balance) ? bufferToBigInt(toBuffer(balance)) : undefined,
isTruthy(stateRoot) ? toBuffer(stateRoot) : undefined,
isTruthy(storageRoot) ? toBuffer(storageRoot) : undefined,
isTruthy(codeHash) ? toBuffer(codeHash) : undefined
)
}
Expand All @@ -56,19 +56,19 @@ export class Account {
}

public static fromValuesArray(values: Buffer[]) {
const [nonce, balance, stateRoot, codeHash] = values
const [nonce, balance, storageRoot, codeHash] = values

return new Account(bufferToBigInt(nonce), bufferToBigInt(balance), stateRoot, codeHash)
return new Account(bufferToBigInt(nonce), bufferToBigInt(balance), storageRoot, codeHash)
}

/**
* This constructor assigns and validates the values.
* Use the static factory methods to assist in creating an Account from varying data types.
*/
constructor(nonce = _0n, balance = _0n, stateRoot = KECCAK256_RLP, codeHash = KECCAK256_NULL) {
constructor(nonce = _0n, balance = _0n, storageRoot = KECCAK256_RLP, codeHash = KECCAK256_NULL) {
this.nonce = nonce
this.balance = balance
this.stateRoot = stateRoot
this.storageRoot = storageRoot
this.codeHash = codeHash

this._validate()
Expand All @@ -81,8 +81,8 @@ export class Account {
if (this.balance < _0n) {
throw new Error('balance must be greater than zero')
}
if (this.stateRoot.length !== 32) {
throw new Error('stateRoot must have a length of 32')
if (this.storageRoot.length !== 32) {
throw new Error('storageRoot must have a length of 32')
}
if (this.codeHash.length !== 32) {
throw new Error('codeHash must have a length of 32')
Expand All @@ -96,7 +96,7 @@ export class Account {
return [
bigIntToUnpaddedBuffer(this.nonce),
bigIntToUnpaddedBuffer(this.balance),
this.stateRoot,
this.storageRoot,
this.codeHash,
]
}
Expand Down
30 changes: 15 additions & 15 deletions packages/util/test/account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ tape('Account', function (t) {
st.equal(account.nonce, _0n, 'should have zero nonce')
st.equal(account.balance, _0n, 'should have zero balance')
st.equal(
account.stateRoot.toString('hex'),
account.storageRoot.toString('hex'),
'56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
'should have stateRoot equal to KECCAK256_RLP'
'should have storageRoot equal to KECCAK256_RLP'
)
st.equal(
account.codeHash.toString('hex'),
Expand All @@ -44,16 +44,16 @@ tape('Account', function (t) {
const raw = [
'0x02', // nonce
'0x0384', // balance
'0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', // stateRoot
'0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', // storageRoot
'0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', // codeHash
]
const account = Account.fromValuesArray(raw.map(toBuffer))
st.equal(account.nonce, BigInt(2), 'should have correct nonce')
st.equal(account.balance, BigInt(900), 'should have correct balance')
st.equal(
account.stateRoot.toString('hex'),
account.storageRoot.toString('hex'),
'56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
'should have correct stateRoot'
'should have correct storageRoot'
)
st.equal(
account.codeHash.toString('hex'),
Expand All @@ -67,16 +67,16 @@ tape('Account', function (t) {
const raw = {
nonce: '0x02',
balance: '0x0384',
stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
storageRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
}
const account = Account.fromAccountData(raw)
st.equal(account.nonce, BigInt(2), 'should have correct nonce')
st.equal(account.balance, BigInt(900), 'should have correct balance')
st.equal(
account.stateRoot.toString('hex'),
account.storageRoot.toString('hex'),
'56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
'should have correct stateRoot'
'should have correct storageRoot'
)
st.equal(
account.codeHash.toString('hex'),
Expand All @@ -95,9 +95,9 @@ tape('Account', function (t) {
st.equal(account.nonce, BigInt(2), 'should have correct nonce')
st.equal(account.balance, BigInt(900), 'should have correct balance')
st.equal(
account.stateRoot.toString('hex'),
account.storageRoot.toString('hex'),
'56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
'should have correct stateRoot'
'should have correct storageRoot'
)
st.equal(
account.codeHash.toString('hex'),
Expand All @@ -111,12 +111,12 @@ tape('Account', function (t) {
const raw = {
nonce: '0x01',
balance: '0x42',
stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
storageRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
codeHash: '0xc5d2461236f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
}
const account = Account.fromAccountData(raw)
const accountRlp = Buffer.from(
RLP.encode([raw.nonce, raw.balance, raw.stateRoot, raw.codeHash])
RLP.encode([raw.nonce, raw.balance, raw.storageRoot, raw.codeHash])
)
st.ok(account.serialize().equals(accountRlp), 'should serialize correctly')
st.end()
Expand All @@ -133,7 +133,7 @@ tape('Account', function (t) {
const raw = {
nonce: '0x01',
balance: '0x0042',
stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
storageRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
codeHash: '0xc5d2461236f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
}
account = Account.fromAccountData(raw)
Expand All @@ -148,7 +148,7 @@ tape('Account', function (t) {
const raw = {
nonce: '0x01',
balance: '0x0042',
stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
storageRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
codeHash: '0xd748bf26ab37599c944babfdbeecf6690801bd61bf2670efb0a34adfc6dca10b',
}
account = Account.fromAccountData(raw)
Expand All @@ -159,7 +159,7 @@ tape('Account', function (t) {
t.test('validation', function (st) {
st.throws(() => {
new Account(undefined, undefined, Buffer.from('hey'), undefined)
}, 'should only accept length 32 buffer for stateRoot')
}, 'should only accept length 32 buffer for storageRoot')

st.throws(() => {
new Account(undefined, undefined, undefined, Buffer.from('hey'))
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/examples/run-solidity-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async function main() {
console.log('-------results-------')
console.log('nonce: ' + createdAccount.nonce.toString())
console.log('balance in wei: ', createdAccount.balance.toString())
console.log('stateRoot: 0x' + createdAccount.stateRoot.toString('hex'))
console.log('storageRoot: 0x' + createdAccount.storageRoot.toString('hex'))
console.log('codeHash: 0x' + createdAccount.codeHash.toString('hex'))
console.log('---------------------')

Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class VM extends AsyncEventEmitter<VMEvents> {
// Only do this if it is not overridden in genesis
// Note: in the case that custom genesis has storage fields, this is preserved
if (account.isEmpty()) {
const newAccount = Account.fromAccountData({ balance: 1, stateRoot: account.stateRoot })
const newAccount = Account.fromAccountData({ balance: 1, storageRoot: account.storageRoot })
await this.eei.putAccount(address, newAccount)
}
}
Expand Down
Loading

0 comments on commit f25d1af

Please sign in to comment.