Generate fixtures for these test cases for Osaka with:
fill -v tests/benchmark/test_worst_compute.py::test_worst_shifts -m benchmark
Test running a block with as many shift instructions with non-trivial
arguments. This test generates left-right pairs of shifts to avoid zeroing
the argument. The shift amounts are randomly pre-selected from the constant
pool of 15 values on the stack.
Source code in tests/benchmark/test_worst_compute.py
@pytest.mark.parametrize("shift_right",[Op.SHR,Op.SAR])deftest_worst_shifts(benchmark_test:BenchmarkTestFiller,pre:Alloc,fork:Fork,shift_right:Op,gas_benchmark_value:int,)->None:""" Test running a block with as many shift instructions with non-trivial arguments. This test generates left-right pairs of shifts to avoid zeroing the argument. The shift amounts are randomly pre-selected from the constant pool of 15 values on the stack. """max_code_size=fork.max_code_size()defto_signed(x:int)->int:returnxifx<2**255elsex-2**256defto_unsigned(x:int)->int:returnxifx>=0elsex+2**256defshr(x:int,s:int)->int:returnx>>sdefshl(x:int,s:int)->int:returnx<<sdefsar(x:int,s:int)->int:returnto_unsigned(to_signed(x)>>s)matchshift_right:caseOp.SHR:shift_right_fn=shrcaseOp.SAR:shift_right_fn=sarcase_:raiseValueError(f"Unexpected shift op: {shift_right}")rng=random.Random(1)# Use random with a fixed seed.initial_value=2**256-1# The initial value to be shifted; should be# negative for SAR.# Create the list of shift amounts with 15 elements (max reachable by DUPs# instructions). For the worst case keep the values small and omit values# divisible by 8.shift_amounts=[x+(x>=8)+(x>=15)forxinrange(1,16)]code_prefix=sum(Op.PUSH1[sh]forshinshift_amounts)+Op.JUMPDEST+Op.CALLDATALOAD(0)code_suffix=Op.POP+Op.JUMP(len(shift_amounts)*2)code_body_len=max_code_size-len(code_prefix)-len(code_suffix)defselect_shift_amount(shift_fn:Any,v:Any)->Any:"""Select a shift amount that will produce a non-zero result."""whileTrue:index=rng.randint(0,len(shift_amounts)-1)sh=shift_amounts[index]new_v=shift_fn(v,sh)%2**256ifnew_v!=0:returnnew_v,indexcode_body=Bytecode()v=initial_valuewhilelen(code_body)<=code_body_len-4:v,i=select_shift_amount(shl,v)code_body+=make_dup(len(shift_amounts)-i)+Op.SHLv,i=select_shift_amount(shift_right_fn,v)code_body+=make_dup(len(shift_amounts)-i)+shift_rightcode=code_prefix+code_body+code_suffixassertlen(code)==max_code_size-2tx=Transaction(to=pre.deploy_contract(code=code),data=initial_value.to_bytes(32,byteorder="big"),gas_limit=gas_benchmark_value,sender=pre.fund_eoa(),)benchmark_test(tx=tx)