-
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
90 additions
and
4 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
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,25 @@ | ||
import { ExecutionContext } from '../ExecutionContext' | ||
import { GasCost } from './gasCosts' | ||
import { Bytes32 } from '../Bytes32' | ||
import { Byte } from '../Byte' | ||
|
||
export function opCODESIZE (ctx: ExecutionContext) { | ||
ctx.useGas(GasCost.BASE) | ||
ctx.stack.push(Bytes32.fromNumber(ctx.message.code.length)) | ||
} | ||
|
||
export function opCODECOPY (ctx: ExecutionContext) { | ||
const memoryOffset = ctx.stack.pop().toUnsignedNumber() | ||
const codeOffset = ctx.stack.pop().toUnsignedNumber() | ||
const memorySize = ctx.stack.pop().toUnsignedNumber() | ||
|
||
ctx.useGas(GasCost.VERYLOW + GasCost.COPY * Math.ceil(memorySize / 32)) | ||
// we subtract the gas early in case of OutOfGas | ||
ctx.memory.useGasForAccess(memoryOffset, memorySize) | ||
|
||
const code = ctx.message.code.slice(codeOffset, codeOffset + memorySize) | ||
while (code.length < memorySize) { | ||
code.push(0x00 as Byte) | ||
} | ||
ctx.memory.setBytes(memoryOffset, code) | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ export const GasCost = { | |
ZERO: 0, | ||
BASE: 2, | ||
VERYLOW: 3, | ||
COPY: 3, | ||
LOW: 5, | ||
MID: 8, | ||
HIGH: 10, | ||
|
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,57 @@ | ||
import { | ||
expectStorage, | ||
Int256, | ||
expectGas, | ||
expectReturn, | ||
assemblyToBytecode, | ||
memoryGas, | ||
expectUnderflow, | ||
} from '../helpers' | ||
import { GasCost } from '../../src/opcodes' | ||
|
||
describe('CODESIZE opcode', () => { | ||
it('returns the code size of the current environment', () => { | ||
expectStorage('CODESIZE PUSH1 00 SSTORE', { | ||
[Int256.of(0)]: Int256.of(4), | ||
}) | ||
}) | ||
|
||
it(`costs ${GasCost.BASE} gas`, () => { | ||
expectGas('CODESIZE', GasCost.BASE) | ||
}) | ||
}) | ||
|
||
describe('CODECOPY opcode', () => { | ||
it('copies the code to the memory', () => { | ||
const assembly = ` | ||
PUSH1 0C | ||
PUSH1 00 | ||
PUSH1 42 | ||
CODECOPY | ||
PUSH1 0C | ||
PUSH1 42 | ||
RETURN | ||
` | ||
expectReturn(assembly, assemblyToBytecode(assembly)) | ||
}) | ||
|
||
it('uses a formula to calculate gas cost', () => { | ||
const assembly = ` | ||
PUSH1 42 | ||
PUSH1 00 | ||
PUSH1 69 | ||
CODECOPY | ||
` | ||
const gas = ( | ||
GasCost.VERYLOW * 3 + | ||
GasCost.VERYLOW + | ||
GasCost.COPY * Math.ceil(0x42 / 32) + | ||
memoryGas(0x69 + 0x42) | ||
) | ||
expectGas(assembly, gas) | ||
}) | ||
|
||
it('can cause stack underflow', () => { | ||
expectUnderflow('CODECOPY', 3) | ||
}) | ||
}) |