|
| 1 | +"""_summary_ |
| 2 | +test `CALLDATACOPY` opcode |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ethereum_test_tools import ( |
| 8 | + Account, |
| 9 | + Alloc, |
| 10 | + Bytecode, |
| 11 | + Environment, |
| 12 | + Hash, |
| 13 | + StateTestFiller, |
| 14 | + Transaction, |
| 15 | +) |
| 16 | +from ethereum_test_tools.vm.opcode import Opcodes as Op |
| 17 | + |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "code,tx_data,code_address_storage,to_address_storage", |
| 20 | + [ |
| 21 | + ( |
| 22 | + ( |
| 23 | + Op.PUSH1[0x2] |
| 24 | + + Op.PUSH1[0x1] |
| 25 | + + Op.PUSH1[0x0] |
| 26 | + + Op.CALLDATACOPY |
| 27 | + + Op.PUSH1[0x0] |
| 28 | + + Op.MLOAD |
| 29 | + + Op.PUSH1[0x0] |
| 30 | + + Op.SSTORE |
| 31 | + + Op.MSIZE |
| 32 | + + Op.PUSH1[0x0] |
| 33 | + + Op.RETURN |
| 34 | + + Op.STOP |
| 35 | + ), |
| 36 | + b"\x00", |
| 37 | + Account(storage={0x00: 0x3456000000000000000000000000000000000000000000000000000000000000}), |
| 38 | + Account(storage={0x00: 0x3456000000000000000000000000000000000000000000000000000000000000}), |
| 39 | + ), |
| 40 | + ( |
| 41 | + ( |
| 42 | + Op.PUSH1[0x1] |
| 43 | + + Op.PUSH1[0x1] |
| 44 | + + Op.PUSH1[0x0] |
| 45 | + + Op.CALLDATACOPY |
| 46 | + + Op.PUSH1[0x0] |
| 47 | + + Op.MLOAD |
| 48 | + + Op.PUSH1[0x0] |
| 49 | + + Op.SSTORE |
| 50 | + + Op.MSIZE |
| 51 | + + Op.PUSH1[0x0] |
| 52 | + + Op.RETURN |
| 53 | + + Op.STOP |
| 54 | + ), |
| 55 | + b"\x01", |
| 56 | + Account(storage={ |
| 57 | + 0x00: 0x3400000000000000000000000000000000000000000000000000000000000000 |
| 58 | + }, |
| 59 | + ), |
| 60 | + Account(storage={ |
| 61 | + 0x00: 0x3400000000000000000000000000000000000000000000000000000000000000 |
| 62 | + }, |
| 63 | + ), |
| 64 | + ), |
| 65 | + ( |
| 66 | + ( |
| 67 | + Op.PUSH1[0x0] |
| 68 | + + Op.PUSH1[0x1] |
| 69 | + + Op.PUSH1[0x0] |
| 70 | + + Op.CALLDATACOPY |
| 71 | + + Op.PUSH1[0x0] |
| 72 | + + Op.MLOAD |
| 73 | + + Op.PUSH1[0x0] |
| 74 | + + Op.SSTORE |
| 75 | + + Op.MSIZE |
| 76 | + + Op.PUSH1[0x0] |
| 77 | + + Op.RETURN |
| 78 | + + Op.STOP |
| 79 | + ), |
| 80 | + b"\x02", |
| 81 | + Account( |
| 82 | + storage={0x00: 0x00}, |
| 83 | + ), |
| 84 | + Account( |
| 85 | + storage={0x00: 0x00}, |
| 86 | + ), |
| 87 | + ), |
| 88 | + ( |
| 89 | + ( |
| 90 | + Op.PUSH1[0x0] |
| 91 | + + Op.PUSH1[0x0] |
| 92 | + + Op.PUSH1[0x0] |
| 93 | + + Op.CALLDATACOPY |
| 94 | + + Op.PUSH1[0x0] |
| 95 | + + Op.MLOAD |
| 96 | + + Op.PUSH1[0x0] |
| 97 | + + Op.SSTORE |
| 98 | + + Op.MSIZE |
| 99 | + + Op.PUSH1[0x0] |
| 100 | + + Op.RETURN |
| 101 | + + Op.STOP |
| 102 | + ), |
| 103 | + b"\x03", |
| 104 | + Account( |
| 105 | + storage={0x00: 0x00}, |
| 106 | + ), |
| 107 | + Account( |
| 108 | + storage={0x00: 0x00}, |
| 109 | + ), |
| 110 | + ), |
| 111 | + ( |
| 112 | + ( |
| 113 | + Op.PUSH1[0xFF] |
| 114 | + + Op.PUSH32[0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA] |
| 115 | + + Op.PUSH1[0x0] |
| 116 | + + Op.CALLDATACOPY |
| 117 | + + Op.PUSH1[0x0] |
| 118 | + + Op.MLOAD |
| 119 | + + Op.PUSH1[0x0] |
| 120 | + + Op.SSTORE |
| 121 | + + Op.MSIZE |
| 122 | + + Op.PUSH1[0x0] |
| 123 | + + Op.RETURN |
| 124 | + + Op.STOP |
| 125 | + ), |
| 126 | + b"\x04", |
| 127 | + Account(storage={0x00: 0x00}), |
| 128 | + Account(storage={0x00: 0x00}), |
| 129 | + ), |
| 130 | + ( |
| 131 | + ( |
| 132 | + Op.PUSH1[0x9] |
| 133 | + + Op.PUSH32[0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA] |
| 134 | + + Op.PUSH1[0x0] |
| 135 | + + Op.CALLDATACOPY |
| 136 | + + Op.PUSH1[0x0] |
| 137 | + + Op.MLOAD |
| 138 | + + Op.PUSH1[0x0] |
| 139 | + + Op.SSTORE |
| 140 | + + Op.MSIZE |
| 141 | + + Op.PUSH1[0x0] |
| 142 | + + Op.RETURN |
| 143 | + + Op.STOP |
| 144 | + ), |
| 145 | + b"\x05", |
| 146 | + Account(storage={0x00: 0x00}), |
| 147 | + Account(storage={0x00: 0x00}), |
| 148 | + ), |
| 149 | + ( |
| 150 | + ( |
| 151 | + Op.PUSH1[0x1] |
| 152 | + + Op.PUSH1[0x1] |
| 153 | + + Op.SSTORE |
| 154 | + + Op.PUSH1[0x1] |
| 155 | + + Op.PUSH1[0x2] |
| 156 | + + Op.CALLDATACOPY |
| 157 | + ), |
| 158 | + b"\x10", |
| 159 | + Account(storage={0x01: 0x00}), |
| 160 | + None, |
| 161 | + ), |
| 162 | + ( |
| 163 | + ( |
| 164 | + Op.PUSH1[0x5] |
| 165 | + + Op.JUMP |
| 166 | + + Op.JUMPDEST |
| 167 | + + Op.STOP |
| 168 | + + Op.JUMPDEST |
| 169 | + + Op.PUSH1[0x42] |
| 170 | + + Op.PUSH1[0x1F] |
| 171 | + + Op.MSTORE8 |
| 172 | + + Op.PUSH2[0x103] |
| 173 | + + Op.PUSH1[0x0] |
| 174 | + + Op.PUSH1[0x1F] |
| 175 | + + Op.CALLDATACOPY |
| 176 | + + Op.PUSH1[0x0] |
| 177 | + + Op.MLOAD |
| 178 | + + Op.DUP1 |
| 179 | + + Op.PUSH1[0x60] |
| 180 | + + Op.EQ |
| 181 | + + Op.PUSH1[0x3] |
| 182 | + + Op.JUMPI |
| 183 | + + Op.PUSH5[0xBADC0FFEE] |
| 184 | + + Op.PUSH1[0xFF] |
| 185 | + + Op.SSTORE |
| 186 | + ), |
| 187 | + b"\x11", |
| 188 | + Account(storage={0xFF: 0xBADC0FFEE}), |
| 189 | + None, |
| 190 | + ), |
| 191 | + ], |
| 192 | + ids=[ |
| 193 | + "cdc 0 1 2", |
| 194 | + "cdc 0 1 1", |
| 195 | + "cdc 0 1 0", |
| 196 | + "cdc 0 0 0", |
| 197 | + "cdc 0 neg6 ff", |
| 198 | + "cdc 0 neg6 9", |
| 199 | + "underflow", |
| 200 | + "sec", |
| 201 | + ], |
| 202 | +) |
| 203 | +def test_calldatacopy( |
| 204 | + state_test: StateTestFiller, |
| 205 | + code: Bytecode, |
| 206 | + tx_data: bytes, |
| 207 | + pre: Alloc, |
| 208 | + code_address_storage: Account, |
| 209 | + to_address_storage: Account, |
| 210 | +): |
| 211 | + code_address = pre.deploy_contract(code) |
| 212 | + to = pre.deploy_contract( |
| 213 | + code=( |
| 214 | + Op.PUSH17[0x1234567890ABCDEF01234567890ABCDEF0] |
| 215 | + + Op.PUSH1[0x0] |
| 216 | + + Op.MSTORE |
| 217 | + + Op.PUSH1[0x40] |
| 218 | + + Op.PUSH1[0x20] |
| 219 | + + Op.PUSH1[0x10] |
| 220 | + + Op.PUSH1[0xF] |
| 221 | + + Op.PUSH1[0x0] |
| 222 | + + Op.PUSH1[0x4] |
| 223 | + + Op.CALLDATALOAD |
| 224 | + + Op.PUSH2[0x1000] |
| 225 | + + Op.ADD |
| 226 | + + Op.PUSH3[0xFFFFFF] |
| 227 | + + Op.CALL |
| 228 | + + Op.POP |
| 229 | + + Op.PUSH1[0x20] |
| 230 | + + Op.MLOAD |
| 231 | + + Op.PUSH1[0x0] |
| 232 | + + Op.SSTORE |
| 233 | + + Op.PUSH1[0x40] |
| 234 | + + Op.MLOAD |
| 235 | + + Op.PUSH1[0x1] |
| 236 | + + Op.SSTORE |
| 237 | + + Op.STOP |
| 238 | + ), |
| 239 | + nonce=0, |
| 240 | + balance=0x0BA1A9CE0BA1A9CE, |
| 241 | + ) |
| 242 | + |
| 243 | + tx = Transaction( |
| 244 | + data=tx_data, |
| 245 | + gas_limit=0x04C4B400, |
| 246 | + gas_price=0x0A, |
| 247 | + nonce=0x00, |
| 248 | + sender=pre.fund_eoa(), |
| 249 | + to=to, |
| 250 | + value=0x01, |
| 251 | + ) |
| 252 | + if to_address_storage: |
| 253 | + post = {code_address: code_address_storage, to: to_address_storage} |
| 254 | + else: |
| 255 | + post = {code_address: code_address_storage} |
| 256 | + state_test(pre=pre, post=post, tx=tx) |
0 commit comments