-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
169 additions
and
1 deletion.
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,54 @@ | ||
import { ExecutionContext } from '../ExecutionContext' | ||
import { Bytes32 } from '../Bytes32' | ||
import { getContractAddress } from '../getContractAddress' | ||
import { executeCode } from '../executeCode' | ||
|
||
export function opCREATE (ctx: ExecutionContext) { | ||
const value = ctx.stack.pop() | ||
const memoryOffset = ctx.stack.pop().toUnsignedNumber() | ||
const memoryBytes = ctx.stack.pop().toUnsignedNumber() | ||
|
||
const gasLeft = ctx.message.gasLimit - ctx.gasUsed | ||
const gasLimit = allButOne64th(gasLeft) | ||
const initCode = ctx.memory.getBytes(memoryOffset, memoryBytes) | ||
|
||
const nonce = ctx.state.getNonce(ctx.message.account) | ||
const balance = ctx.state.getBalance(ctx.message.account) | ||
|
||
const contract = getContractAddress(ctx.message.account, nonce) | ||
|
||
if (balance.lt(value)) { | ||
ctx.stack.push(Bytes32.ZERO) | ||
return | ||
} | ||
|
||
ctx.state.setNonce(ctx.message.account, nonce + 1) | ||
ctx.state.setBalance(ctx.message.account, balance.sub(value)) | ||
|
||
const result = executeCode({ | ||
account: contract, | ||
callDepth: ctx.message.callDepth + 1, | ||
sender: ctx.message.account, | ||
origin: ctx.message.origin, | ||
gasLimit, | ||
gasPrice: ctx.message.gasPrice, | ||
code: initCode, | ||
data: [], | ||
enableStateModifications: ctx.message.enableStateModifications, | ||
value, | ||
}, ctx.state.clone()) | ||
|
||
if (result.type === 'ExecutionSuccess') { | ||
ctx.stack.push(Bytes32.fromAddress(contract)) | ||
ctx.state = result.state | ||
|
||
ctx.useGas(result.gasUsed) | ||
ctx.refund(result.gasRefund) | ||
} else { | ||
ctx.stack.push(Bytes32.ZERO) | ||
} | ||
} | ||
|
||
function allButOne64th (value: number) { | ||
return value - Math.floor(value / 64) | ||
} |
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,83 @@ | ||
import { expect } from 'chai' | ||
import { executeAssembly } from "../helpers" | ||
import { Address } from "../../src/Address" | ||
import { State } from "../../src/State" | ||
import { getContractAddress } from '../../src/getContractAddress' | ||
import { Bytes32 } from '../../src/Bytes32' | ||
|
||
describe('CREATE opcode', () => { | ||
const assembly = ` | ||
PUSH1 05 // size of the code | ||
PUSH1 12 // code offset | ||
PUSH1 00 // memory offset of the code | ||
CODECOPY | ||
PUSH1 05 // size of the code | ||
PUSH1 00 // memory offset of the code | ||
PUSH1 69 // value passed | ||
CREATE | ||
PUSH1 00 | ||
SSTORE // save the address of the created contract | ||
STOP | ||
// code of the contract | ||
PUSH1 01 | ||
PUSH1 00 | ||
SSTORE // save 1 at address 0 in the storage of the new contract | ||
` | ||
const account = 'abcd'.repeat(10) as Address | ||
|
||
it('results in the creation of a new contract', () => { | ||
const state = new State() | ||
state.setNonce(account, 42) | ||
state.setBalance(account, Bytes32.fromNumber(0x100)) | ||
|
||
const result = executeAssembly(assembly, { account }, state) | ||
|
||
if (result.type !== 'ExecutionSuccess') { | ||
expect(result.type).to.equal('ExecutionSuccess') | ||
} else { | ||
// increments nonce | ||
expect(result.state.getNonce(account)).to.equal(43) | ||
|
||
// subtracts balance | ||
const balance = result.state.getBalance(account) | ||
expect(balance.eq(Bytes32.fromNumber(0x100 - 0x69))).to.equal(true) | ||
|
||
// returns correct address | ||
const expectedAddress = getContractAddress(account, 42) | ||
const actualAddress = result.state | ||
.getStorage(account, Bytes32.ZERO) | ||
.toAddress() | ||
expect(actualAddress).to.equal(expectedAddress) | ||
|
||
// actually runs the contract code | ||
const stored = result.state.getStorage(actualAddress, Bytes32.ZERO) | ||
expect(stored.eq(Bytes32.ONE)).to.equal(true) | ||
} | ||
}) | ||
|
||
it('account does not have the balance', () => { | ||
const state = new State() | ||
state.setNonce(account, 42) | ||
state.setBalance(account, Bytes32.fromNumber(0x68)) | ||
|
||
const result = executeAssembly(assembly, { account }, state) | ||
|
||
if (result.type !== 'ExecutionSuccess') { | ||
expect(result.type).to.equal('ExecutionSuccess') | ||
} else { | ||
// does not increment the nonce | ||
expect(result.state.getNonce(account)).to.equal(42) | ||
|
||
// does not subtract the balance | ||
const balance = result.state.getBalance(account) | ||
expect(balance.eq(Bytes32.fromNumber(0x68))).to.equal(true) | ||
|
||
// returns zero | ||
const returnValue = result.state.getStorage(account, Bytes32.ZERO) | ||
expect(returnValue).to.equal(Bytes32.ZERO) | ||
} | ||
}) | ||
}) |