github.com/quinndk/ethereum_read@v0.0.0-20181211143958-29c55eec3237/go-ethereum-master_read/core/vm/memory.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "fmt" 21 "math/big" 22 23 "github.com/ethereum/go-ethereum/common/math" 24 ) 25 26 // Memory implements a simple memory model for the ethereum virtual machine. 27 type Memory struct { 28 // 内存 29 store []byte 30 // 最后一次的gas花费 31 lastGasCost uint64 32 } 33 34 // NewMemory returns a new memory memory model. 35 func NewMemory() *Memory { 36 return &Memory{} 37 } 38 39 // Set sets offset + size to value 40 func (m *Memory) Set(offset, size uint64, value []byte) { 41 // It's possible the offset is greater than 0 and size equals 0. This is because 42 // the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP) 43 if size > 0 { 44 // length of store may never be less than offset + size. 45 // The store should be resized PRIOR to setting the memory 46 if offset+size > uint64(len(m.store)) { 47 panic("invalid memory: store empty") 48 } 49 copy(m.store[offset:offset+size], value) 50 } 51 } 52 53 // Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to 54 // 32 bytes. 55 func (m *Memory) Set32(offset uint64, val *big.Int) { 56 // length of store may never be less than offset + size. 57 // The store should be resized PRIOR to setting the memory 58 if offset+32 > uint64(len(m.store)) { 59 panic("invalid memory: store empty") 60 } 61 // Zero the memory area 62 copy(m.store[offset:offset+32], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) 63 // Fill in relevant bits 64 math.ReadBits(val, m.store[offset:offset+32]) 65 } 66 67 // Resize resizes the memory to size 68 func (m *Memory) Resize(size uint64) { 69 if uint64(m.Len()) < size { 70 m.store = append(m.store, make([]byte, size-uint64(m.Len()))...) 71 } 72 } 73 74 // Get returns offset + size as a new slice 75 func (m *Memory) Get(offset, size int64) (cpy []byte) { 76 if size == 0 { 77 return nil 78 } 79 80 if len(m.store) > int(offset) { 81 cpy = make([]byte, size) 82 copy(cpy, m.store[offset:offset+size]) 83 84 return 85 } 86 87 return 88 } 89 90 // GetPtr returns the offset + size 91 func (m *Memory) GetPtr(offset, size int64) []byte { 92 if size == 0 { 93 return nil 94 } 95 96 if len(m.store) > int(offset) { 97 return m.store[offset : offset+size] 98 } 99 100 return nil 101 } 102 103 // Len returns the length of the backing slice 104 func (m *Memory) Len() int { 105 return len(m.store) 106 } 107 108 // Data returns the backing slice 109 func (m *Memory) Data() []byte { 110 return m.store 111 } 112 113 // Print dumps the content of the memory. 114 func (m *Memory) Print() { 115 fmt.Printf("### mem %d bytes ###\n", len(m.store)) 116 if len(m.store) > 0 { 117 addr := 0 118 for i := 0; i+32 <= len(m.store); i += 32 { 119 fmt.Printf("%03d: % x\n", addr, m.store[i:i+32]) 120 addr++ 121 } 122 } else { 123 fmt.Println("-- empty --") 124 } 125 fmt.Println("####################") 126 }