core/vm, core/state: added storage to structured vm logging
This commit is contained in:
		| @@ -336,6 +336,22 @@ func (self *StateObject) Nonce() uint64 { | |||||||
| 	return self.nonce | 	return self.nonce | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (self *StateObject) EachStorage(cb func(key, value []byte)) { | ||||||
|  | 	// When iterating over the storage check the cache first | ||||||
|  | 	for h, v := range self.storage { | ||||||
|  | 		cb([]byte(h), v.Bytes()) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	it := self.State.trie.Iterator() | ||||||
|  | 	for it.Next() { | ||||||
|  | 		// ignore cached values | ||||||
|  | 		key := self.State.trie.GetKey(it.Key) | ||||||
|  | 		if _, ok := self.storage[string(key)]; !ok { | ||||||
|  | 			cb(key, it.Value) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| // | // | ||||||
| // Encoding | // Encoding | ||||||
| // | // | ||||||
|   | |||||||
| @@ -34,11 +34,12 @@ type Environment interface { | |||||||
| } | } | ||||||
|  |  | ||||||
| type StructLog struct { | type StructLog struct { | ||||||
| 	Pc     uint64 | 	Pc      uint64 | ||||||
| 	Op     OpCode | 	Op      OpCode | ||||||
| 	Gas    *big.Int | 	Gas     *big.Int | ||||||
| 	Memory []byte | 	Memory  []byte | ||||||
| 	Stack  []*big.Int | 	Stack   []*big.Int | ||||||
|  | 	Storage map[common.Hash][]byte | ||||||
| } | } | ||||||
|  |  | ||||||
| type Account interface { | type Account interface { | ||||||
|   | |||||||
| @@ -95,7 +95,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { | |||||||
| 		// Get the memory location of pc | 		// Get the memory location of pc | ||||||
| 		op = context.GetOp(pc) | 		op = context.GetOp(pc) | ||||||
|  |  | ||||||
| 		self.log(pc, op, context.Gas, mem, stack) | 		self.log(pc, op, context.Gas, mem, stack, context) | ||||||
|  |  | ||||||
| 		newMemSize, gas, err := self.calculateGasAndSize(context, caller, op, statedb, mem, stack) | 		newMemSize, gas, err := self.calculateGasAndSize(context, caller, op, statedb, mem, stack) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| @@ -778,13 +778,20 @@ func (self *Vm) RunPrecompiled(p *PrecompiledAccount, callData []byte, context * | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (self *Vm) log(pc uint64, op OpCode, gas *big.Int, memory *Memory, stack *Stack) { | func (self *Vm) log(pc uint64, op OpCode, gas *big.Int, memory *Memory, stack *Stack, context *Context) { | ||||||
| 	if Debug { | 	if Debug { | ||||||
| 		mem := make([]byte, len(memory.Data())) | 		mem := make([]byte, len(memory.Data())) | ||||||
| 		copy(mem, memory.Data()) | 		copy(mem, memory.Data()) | ||||||
| 		stck := make([]*big.Int, len(stack.Data())) | 		stck := make([]*big.Int, len(stack.Data())) | ||||||
| 		copy(stck, stack.Data()) | 		copy(stck, stack.Data()) | ||||||
| 		self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), mem, stck}) |  | ||||||
|  | 		object := context.self.(*state.StateObject) | ||||||
|  | 		storage := make(map[common.Hash][]byte) | ||||||
|  | 		object.EachStorage(func(k, v []byte) { | ||||||
|  | 			storage[common.BytesToHash(k)] = v | ||||||
|  | 		}) | ||||||
|  |  | ||||||
|  | 		self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), mem, stck, storage}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ import ( | |||||||
| func VmStdErrFormat(logs []vm.StructLog) { | func VmStdErrFormat(logs []vm.StructLog) { | ||||||
| 	fmt.Fprintf(os.Stderr, "VM Stats %d ops\n", len(logs)) | 	fmt.Fprintf(os.Stderr, "VM Stats %d ops\n", len(logs)) | ||||||
| 	for _, log := range logs { | 	for _, log := range logs { | ||||||
| 		fmt.Fprintf(os.Stderr, "PC %-3d - %-14s\n", log.Pc, log.Op) | 		fmt.Fprintf(os.Stderr, "PC %08d: %s\n", log.Pc, log.Op) | ||||||
| 		fmt.Fprintln(os.Stderr, "STACK =", len(log.Stack)) | 		fmt.Fprintln(os.Stderr, "STACK =", len(log.Stack)) | ||||||
| 		for i, item := range log.Stack { | 		for i, item := range log.Stack { | ||||||
| 			fmt.Fprintf(os.Stderr, "%04d: %x\n", i, common.LeftPadBytes(item.Bytes(), 32)) | 			fmt.Fprintf(os.Stderr, "%04d: %x\n", i, common.LeftPadBytes(item.Bytes(), 32)) | ||||||
| @@ -36,5 +36,11 @@ func VmStdErrFormat(logs []vm.StructLog) { | |||||||
| 			addr++ | 			addr++ | ||||||
| 			fmt.Fprintln(os.Stderr, str) | 			fmt.Fprintln(os.Stderr, str) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		fmt.Fprintln(os.Stderr, "STORAGE =", len(log.Storage)) | ||||||
|  | 		for h, item := range log.Storage { | ||||||
|  | 			fmt.Fprintf(os.Stderr, "%x: %x\n", h, common.LeftPadBytes(item, 32)) | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user