-
Notifications
You must be signed in to change notification settings - Fork 773
/
eip-5656.spec.ts
107 lines (94 loc) · 2.91 KB
/
eip-5656.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'
import { createEVM } from '../../src/index.js'
import type { PrefixedHexString } from '@ethereumjs/util'
type Situation = {
pre: string
post: string
dst: number
src: number
length: number
}
// Taken from EIP
const situations: Situation[] = [
{
pre: '0000000000000000000000000000000000000000000000000000000000000000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
post: '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
dst: 0,
src: 32,
length: 32,
},
{
pre: '0101010101010101010101010101010101010101010101010101010101010101',
post: '0101010101010101010101010101010101010101010101010101010101010101',
dst: 0,
src: 0,
length: 32,
},
// For the situation below, pre/post have 1 byte less than in the current state of the EIP
{
pre: '0001020304050607080000000000000000000000000000000000000000000000',
post: '0102030405060708080000000000000000000000000000000000000000000000',
dst: 0,
src: 1,
length: 8,
},
// For the situation below, pre/post have 1 byte less than in the current state of the EIP
{
pre: '0001020304050607080000000000000000000000000000000000000000000000',
post: '0000010203040506070000000000000000000000000000000000000000000000',
dst: 1,
src: 0,
length: 8,
},
]
function numToOpcode(num: number) {
return num.toString(16).padStart(2, '0')
}
const PUSH1 = '60'
const MSTORE8 = '53'
const MCOPY = '5E'
const STOP = '00'
describe('should test mcopy', () => {
for (const situation of situations) {
it('should produce correct output', async () => {
// create bytecode
let bytecode: PrefixedHexString = '0x'
// prepare the memory
for (let i = 0; i < situation.pre.length / 2; i++) {
const start = i * 2
const hexNum = situation.pre.slice(start, start + 2)
bytecode += PUSH1 + hexNum + PUSH1 + numToOpcode(i) + MSTORE8
}
// mcopy
bytecode +=
PUSH1 +
numToOpcode(situation.length) +
PUSH1 +
numToOpcode(situation.src) +
PUSH1 +
numToOpcode(situation.dst)
bytecode += MCOPY + STOP
const common = new Common({
chain: Mainnet,
hardfork: Hardfork.Shanghai,
eips: [5656],
})
const evm = await createEVM({
common,
})
let currentMem = ''
evm.events.on('step', (e) => {
if (e.opcode.name === 'STOP') {
currentMem = bytesToHex(e.memory)
assert.equal(currentMem, '0x' + situation.post, 'post-memory correct')
}
})
await evm.runCall({
data: hexToBytes(bytecode as PrefixedHexString),
gasLimit: BigInt(0xffffff),
})
})
}
})