0

I have a 'Context' Object which contains a stack. I have a Run function that reads bytes and returns a new Context object after interpreting those bytes and manipulating the stack. I have a script that in a loop calls this Run function with different byte inputs. for some reason, the stack does not appear to be reset on each call.

here is the ExecutionContext object

from .Stack import Stack

class ExecutionContext:
    def __init__(self,code=bytes() ,pc=0,stack=Stack(), memory=Memory()) ->None:
        self.code = code
        self.stack = stack
        self.pc = pc
        self.stopped = False
  
           .......

My Run function:


def run(code: bytes) -> ExecutionContext:
    
    context = ExecutionContext(code=code)

    while not context.stopped:
        pc_before = context.pc
        instruction.execute(context)

    return context

And finally in the looping script

for i, test in enumerate(data):
#             # Note: as the test cases get more complex, you'll need to modify this
#             # to pass down more arguments to the evm function
            code = bytes.fromhex(test['code']['bin'])
        
            cntxt = run(code)
           
           #make sure to test return value to? maybe not feasible
            
            print("stack: ", cntxt.stack.stack)
            expected_stack = [int(x, 16) for x in test['expect']['stack']]
            del cntxt
            gc.collect()

                  ........
            

the problem pertains to cntxt = run(code) in the last script above. the stack in the second iteration does not appear to be reset, as the items that were on the cntxt.stack in the previous run(code) call is still on cntxt.stack, I even tried using the garbage collector and del keyword.

Sujith Kumar
  • 872
  • 6
  • 19
ara13
  • 1

0 Answers0