|
| 1 | +import { expect } from 'chai' |
| 2 | +import { executeAssembly } from "../helpers" |
| 3 | +import { Address } from "../../src/Address" |
| 4 | +import { State } from "../../src/State" |
| 5 | +import { getContractAddress } from '../../src/getContractAddress' |
| 6 | +import { Bytes32 } from '../../src/Bytes32' |
| 7 | + |
| 8 | +describe('CREATE opcode', () => { |
| 9 | + const assembly = ` |
| 10 | + PUSH1 05 // size of the code |
| 11 | + PUSH1 12 // code offset |
| 12 | + PUSH1 00 // memory offset of the code |
| 13 | + CODECOPY |
| 14 | +
|
| 15 | + PUSH1 05 // size of the code |
| 16 | + PUSH1 00 // memory offset of the code |
| 17 | + PUSH1 69 // value passed |
| 18 | + CREATE |
| 19 | +
|
| 20 | + PUSH1 00 |
| 21 | + SSTORE // save the address of the created contract |
| 22 | + STOP |
| 23 | +
|
| 24 | + // code of the contract |
| 25 | + PUSH1 01 |
| 26 | + PUSH1 00 |
| 27 | + SSTORE // save 1 at address 0 in the storage of the new contract |
| 28 | + ` |
| 29 | + const account = 'abcd'.repeat(10) as Address |
| 30 | + |
| 31 | + it('results in the creation of a new contract', () => { |
| 32 | + const state = new State() |
| 33 | + state.setNonce(account, 42) |
| 34 | + state.setBalance(account, Bytes32.fromNumber(0x100)) |
| 35 | + |
| 36 | + const result = executeAssembly(assembly, { account }, state) |
| 37 | + |
| 38 | + if (result.type !== 'ExecutionSuccess') { |
| 39 | + expect(result.type).to.equal('ExecutionSuccess') |
| 40 | + } else { |
| 41 | + // increments nonce |
| 42 | + expect(result.state.getNonce(account)).to.equal(43) |
| 43 | + |
| 44 | + // subtracts balance |
| 45 | + const balance = result.state.getBalance(account) |
| 46 | + expect(balance.eq(Bytes32.fromNumber(0x100 - 0x69))).to.equal(true) |
| 47 | + |
| 48 | + // returns correct address |
| 49 | + const expectedAddress = getContractAddress(account, 42) |
| 50 | + const actualAddress = result.state |
| 51 | + .getStorage(account, Bytes32.ZERO) |
| 52 | + .toAddress() |
| 53 | + expect(actualAddress).to.equal(expectedAddress) |
| 54 | + |
| 55 | + // actually runs the contract code |
| 56 | + const stored = result.state.getStorage(actualAddress, Bytes32.ZERO) |
| 57 | + expect(stored.eq(Bytes32.ONE)).to.equal(true) |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + it('account does not have the balance', () => { |
| 62 | + const state = new State() |
| 63 | + state.setNonce(account, 42) |
| 64 | + state.setBalance(account, Bytes32.fromNumber(0x68)) |
| 65 | + |
| 66 | + const result = executeAssembly(assembly, { account }, state) |
| 67 | + |
| 68 | + if (result.type !== 'ExecutionSuccess') { |
| 69 | + expect(result.type).to.equal('ExecutionSuccess') |
| 70 | + } else { |
| 71 | + // does not increment the nonce |
| 72 | + expect(result.state.getNonce(account)).to.equal(42) |
| 73 | + |
| 74 | + // does not subtract the balance |
| 75 | + const balance = result.state.getBalance(account) |
| 76 | + expect(balance.eq(Bytes32.fromNumber(0x68))).to.equal(true) |
| 77 | + |
| 78 | + // returns zero |
| 79 | + const returnValue = result.state.getStorage(account, Bytes32.ZERO) |
| 80 | + expect(returnValue).to.equal(Bytes32.ZERO) |
| 81 | + } |
| 82 | + }) |
| 83 | +}) |
0 commit comments