|
5 | 5 | "fmt"
|
6 | 6 | "math/big"
|
7 | 7 |
|
| 8 | + hex "github.com/tmthrgd/go-hex" |
| 9 | + |
8 | 10 | "github.com/hyperledger/burrow/execution/evm"
|
9 | 11 | "github.com/hyperledger/burrow/execution/exec"
|
10 | 12 |
|
@@ -79,6 +81,140 @@ func (ctx *context) ResolveGlobal(module, field string) int64 {
|
79 | 81 | }
|
80 | 82 |
|
81 | 83 | func (ctx *context) ResolveFunc(module, field string) lifeExec.FunctionImport {
|
| 84 | + if module == "debug" { |
| 85 | + // See https://github.com/ewasm/hera#interfaces |
| 86 | + switch field { |
| 87 | + case "print32": |
| 88 | + return func(vm *lifeExec.VirtualMachine) int64 { |
| 89 | + n := int32(vm.GetCurrentFrame().Locals[0]) |
| 90 | + |
| 91 | + s := fmt.Sprintf("%d", n) |
| 92 | + |
| 93 | + err := ctx.state.EventSink.Print(&exec.PrintEvent{ |
| 94 | + Address: ctx.params.Callee, |
| 95 | + Data: []byte(s), |
| 96 | + }) |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + panic(fmt.Sprintf(" => print32 failed: %v", err)) |
| 100 | + } |
| 101 | + |
| 102 | + return Success |
| 103 | + } |
| 104 | + |
| 105 | + case "print64": |
| 106 | + return func(vm *lifeExec.VirtualMachine) int64 { |
| 107 | + n := int64(vm.GetCurrentFrame().Locals[0]) |
| 108 | + |
| 109 | + s := fmt.Sprintf("%d", n) |
| 110 | + |
| 111 | + err := ctx.state.EventSink.Print(&exec.PrintEvent{ |
| 112 | + Address: ctx.params.Callee, |
| 113 | + Data: []byte(s), |
| 114 | + }) |
| 115 | + |
| 116 | + if err != nil { |
| 117 | + panic(fmt.Sprintf(" => print32 failed: %v", err)) |
| 118 | + } |
| 119 | + |
| 120 | + return Success |
| 121 | + } |
| 122 | + |
| 123 | + case "printMem": |
| 124 | + return func(vm *lifeExec.VirtualMachine) int64 { |
| 125 | + dataPtr := int(uint32(vm.GetCurrentFrame().Locals[0])) |
| 126 | + dataLen := int(uint32(vm.GetCurrentFrame().Locals[1])) |
| 127 | + |
| 128 | + s := vm.Memory[dataPtr : dataPtr+dataLen] |
| 129 | + |
| 130 | + err := ctx.state.EventSink.Print(&exec.PrintEvent{ |
| 131 | + Address: ctx.params.Callee, |
| 132 | + Data: s, |
| 133 | + }) |
| 134 | + |
| 135 | + if err != nil { |
| 136 | + panic(fmt.Sprintf(" => printMem failed: %v", err)) |
| 137 | + } |
| 138 | + |
| 139 | + return Success |
| 140 | + } |
| 141 | + |
| 142 | + case "printMemHex": |
| 143 | + return func(vm *lifeExec.VirtualMachine) int64 { |
| 144 | + dataPtr := int(uint32(vm.GetCurrentFrame().Locals[0])) |
| 145 | + dataLen := int(uint32(vm.GetCurrentFrame().Locals[1])) |
| 146 | + |
| 147 | + s := hex.EncodeToString(vm.Memory[dataPtr : dataPtr+dataLen]) |
| 148 | + |
| 149 | + err := ctx.state.EventSink.Print(&exec.PrintEvent{ |
| 150 | + Address: ctx.params.Callee, |
| 151 | + Data: []byte(s), |
| 152 | + }) |
| 153 | + |
| 154 | + if err != nil { |
| 155 | + panic(fmt.Sprintf(" => printMemHex failed: %v", err)) |
| 156 | + } |
| 157 | + |
| 158 | + return Success |
| 159 | + } |
| 160 | + |
| 161 | + case "printStorage": |
| 162 | + return func(vm *lifeExec.VirtualMachine) int64 { |
| 163 | + keyPtr := int(uint32(vm.GetCurrentFrame().Locals[0])) |
| 164 | + |
| 165 | + key := bin.Word256{} |
| 166 | + |
| 167 | + copy(key[:], vm.Memory[keyPtr:keyPtr+32]) |
| 168 | + |
| 169 | + val, err := ctx.state.GetStorage(ctx.params.Callee, key) |
| 170 | + if err != nil { |
| 171 | + panic(err) |
| 172 | + } |
| 173 | + |
| 174 | + err = ctx.state.EventSink.Print(&exec.PrintEvent{ |
| 175 | + Address: ctx.params.Callee, |
| 176 | + Data: val, |
| 177 | + }) |
| 178 | + |
| 179 | + if err != nil { |
| 180 | + panic(fmt.Sprintf(" => printStorage failed: %v", err)) |
| 181 | + } |
| 182 | + |
| 183 | + return Success |
| 184 | + } |
| 185 | + |
| 186 | + case "printStorageHex": |
| 187 | + return func(vm *lifeExec.VirtualMachine) int64 { |
| 188 | + keyPtr := int(uint32(vm.GetCurrentFrame().Locals[0])) |
| 189 | + |
| 190 | + key := bin.Word256{} |
| 191 | + |
| 192 | + copy(key[:], vm.Memory[keyPtr:keyPtr+32]) |
| 193 | + |
| 194 | + val, err := ctx.state.GetStorage(ctx.params.Callee, key) |
| 195 | + if err != nil { |
| 196 | + panic(err) |
| 197 | + } |
| 198 | + |
| 199 | + s := hex.EncodeToString(val) |
| 200 | + |
| 201 | + err = ctx.state.EventSink.Print(&exec.PrintEvent{ |
| 202 | + Address: ctx.params.Callee, |
| 203 | + Data: []byte(s), |
| 204 | + }) |
| 205 | + |
| 206 | + if err != nil { |
| 207 | + panic(fmt.Sprintf(" => printStorage failed: %v", err)) |
| 208 | + } |
| 209 | + |
| 210 | + return Success |
| 211 | + } |
| 212 | + |
| 213 | + default: |
| 214 | + panic(fmt.Sprintf("function %s unknown for debug module", field)) |
| 215 | + } |
| 216 | + } |
| 217 | + |
82 | 218 | if module != "ethereum" {
|
83 | 219 | panic(fmt.Sprintf("unknown module %s", module))
|
84 | 220 | }
|
|
0 commit comments