|
| 1 | +package simulation |
| 2 | + |
| 3 | +import ( |
| 4 | + "goevm/evm" |
| 5 | + "unsafe" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common" |
| 8 | + "github.com/ethereum/go-ethereum/log" |
| 9 | + "github.com/holiman/uint256" |
| 10 | +) |
| 11 | + |
| 12 | +func RunSimpleSimulation() { |
| 13 | + // Create a temporary address for simulation |
| 14 | + sender := common.HexToAddress("0x350fbDe850998AAC40f0b9364b4ACeA665a3d08c") |
| 15 | + |
| 16 | + // Create a new tracer |
| 17 | + tracer := evm.NewTracer() |
| 18 | + |
| 19 | + // Create a new storage using tracer |
| 20 | + storage := evm.NewSimpleStorage(tracer) |
| 21 | + defer storage.Close() |
| 22 | + |
| 23 | + // Create a new account and set some balance |
| 24 | + storage.CreateAccount(sender) |
| 25 | + storage.SetBalance(sender, uint256.NewInt(10000)) |
| 26 | + |
| 27 | + // Arithmetic/Comparision/Logical operations |
| 28 | + var opcodes []evm.OpCode = []evm.OpCode{ |
| 29 | + evm.PUSH1, 0x5, // Pushes 5 to stack [0x5] |
| 30 | + evm.PUSH1, 0x6, // Pushes 6 to stack [0x5, 0x6] |
| 31 | + evm.ADD, // Adds the top two elements of the stack [0xb] |
| 32 | + evm.PUSH1, 0x2, // Pushes 2 to stack [0xb, 0x2] |
| 33 | + evm.MUL, // Multiplies the top two elements of the stack [0x16] |
| 34 | + evm.PUSH1, 0x5, // Push key to stack [0x16, 0x5] |
| 35 | + evm.GT, // Greater than [0x0] |
| 36 | + evm.PUSH1, 0x1, // Push key to stack [0x0, 0x1] |
| 37 | + evm.OR, // Bitwise OR [0x1] |
| 38 | + } |
| 39 | + |
| 40 | + // Environment operations |
| 41 | + opcodes = append(opcodes, []evm.OpCode{ |
| 42 | + evm.ADDRESS, // Pushes the address to stack [0x1, address] |
| 43 | + evm.BALANCE, // Pushes the balance to stack [0x1, balance(0x0)] |
| 44 | + evm.POP, // Pops the top element of the stack [0x1] |
| 45 | + }...) |
| 46 | + |
| 47 | + // Memory and storage operations |
| 48 | + withWrite := []evm.OpCode{ |
| 49 | + // Stack: [0x1] (value) |
| 50 | + evm.PUSH1, 0x0, // Pushes 0 to stack [0x1, 0x0] (offset) |
| 51 | + evm.MSTORE, // Store value at offset in memory (total length = 32) |
| 52 | + evm.PUSH1, 0x2, // Pushes 2 to stack [0x2] (value) |
| 53 | + evm.PUSH1, 0x20, // Pushes 32 to stack [0x2, 0x20] (offset) |
| 54 | + evm.MSTORE, // Store second value at offset in memory (total length = 64) |
| 55 | + evm.PUSH1, 0x64, // Pushes 100 to stack [0x64] (value) |
| 56 | + evm.PUSH1, 0x20, // Pushes 32 to stack [0x64, 0x20] (to load value from memory) |
| 57 | + evm.MLOAD, // Load value from memory at offset [0x64, 0x2] (value) |
| 58 | + evm.SSTORE, // Store value at key in storage (key = 0x2, value = 0x64) |
| 59 | + evm.PUSH1, 0x2, // Pushes 2 to stack [0x2] (key) |
| 60 | + evm.SLOAD, // Load value from storage at key |
| 61 | + evm.STOP, |
| 62 | + } |
| 63 | + |
| 64 | + opcodes = append(opcodes, withWrite...) |
| 65 | + code := *(*[]byte)(unsafe.Pointer(&opcodes)) |
| 66 | + |
| 67 | + // Initialise EVM instance |
| 68 | + opts := evm.NewExecutionOpts(common.Address{}, sender, 1, []byte{}, code, 10000) |
| 69 | + evm := evm.NewEVM(storage, opts, tracer) |
| 70 | + |
| 71 | + log.Info("Initialized new evm instance, starting simple simulation", "len", len(code)) |
| 72 | + |
| 73 | + evm.Run() |
| 74 | + |
| 75 | + log.Info("Done execution, exiting") |
| 76 | +} |
| 77 | + |
| 78 | +func RunRemoteSimulation(path string, contractAddress string) { |
| 79 | + // Create a temporary address for simulation |
| 80 | + sender := common.HexToAddress("0x350fbDe850998AAC40f0b9364b4ACeA665a3d08c") |
| 81 | + |
| 82 | + // Initialise the contract address |
| 83 | + contract := common.HexToAddress(contractAddress) |
| 84 | + |
| 85 | + // Create a new tracer |
| 86 | + tracer := evm.NewTracer() |
| 87 | + |
| 88 | + // Create a new storage using tracer |
| 89 | + storage := evm.NewRemoteStorage(path, tracer) |
| 90 | + defer storage.Close() |
| 91 | + |
| 92 | + // Arithmetic/Comparision/Logical operations |
| 93 | + var opcodes []evm.OpCode = []evm.OpCode{ |
| 94 | + evm.PUSH1, 0x5, // Pushes 5 to stack [0x5] |
| 95 | + evm.PUSH1, 0x6, // Pushes 6 to stack [0x5, 0x6] |
| 96 | + evm.ADD, // Adds the top two elements of the stack [0xb] |
| 97 | + evm.PUSH1, 0x2, // Pushes 2 to stack [0xb, 0x2] |
| 98 | + evm.MUL, // Multiplies the top two elements of the stack [0x16] |
| 99 | + evm.PUSH1, 0x5, // Push key to stack [0x16, 0x5] |
| 100 | + evm.GT, // Greater than [0x0] |
| 101 | + evm.PUSH1, 0x1, // Push key to stack [0x0, 0x1] |
| 102 | + evm.OR, // Bitwise OR [0x1] |
| 103 | + } |
| 104 | + |
| 105 | + // Environment operations |
| 106 | + opcodes = append(opcodes, []evm.OpCode{ |
| 107 | + evm.ADDRESS, // Pushes the address to stack [0x1, address] |
| 108 | + evm.BALANCE, // Pushes the balance to stack [0x1, balance(0x0)] |
| 109 | + evm.POP, // Pops the top element of the stack [0x1] |
| 110 | + }...) |
| 111 | + |
| 112 | + // Memory and storage operations |
| 113 | + opcodes = append(opcodes, []evm.OpCode{ |
| 114 | + // Stack: [0x1] (value) |
| 115 | + evm.PUSH1, 0x0, // Pushes 0 to stack [0x1, 0x0] (offset) |
| 116 | + evm.MSTORE, // Store value at offset in memory (total length = 32) |
| 117 | + evm.PUSH1, 0x2, // Pushes 2 to stack [0x2] (value) |
| 118 | + evm.PUSH1, 0x20, // Pushes 32 to stack [0x2, 0x20] (offset) |
| 119 | + evm.MSTORE, // Store second value at offset in memory (total length = 64) |
| 120 | + evm.PUSH1, 0x0, // Pushes 0 to stack [0x0] (key) (val1 in our test contract) |
| 121 | + evm.SLOAD, // Load value from storage at key |
| 122 | + evm.PUSH1, 0x1, // Pushes 2 to stack [0x1] (key) (val2 in our test contract) |
| 123 | + evm.SLOAD, // Load value from storage at key |
| 124 | + evm.STOP, // STOP |
| 125 | + }...) |
| 126 | + |
| 127 | + // Initialise EVM instance |
| 128 | + code := *(*[]byte)(unsafe.Pointer(&opcodes)) |
| 129 | + opts := evm.NewExecutionOpts(contract, sender, 1, []byte{}, code, 10000) |
| 130 | + evm := evm.NewEVM(storage, opts, tracer) |
| 131 | + |
| 132 | + log.Info("Initialized new evm instance, starting remote simulation", "len", len(code)) |
| 133 | + evm.Run() |
| 134 | + log.Info("Done execution, exiting") |
| 135 | +} |
0 commit comments