github.com/theQRL/go-zond@v0.1.1/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 "github.com/holiman/uint256" 21 ) 22 23 // Memory implements a simple memory model for the ethereum virtual machine. 24 type Memory struct { 25 store []byte 26 lastGasCost uint64 27 } 28 29 // NewMemory returns a new memory model. 30 func NewMemory() *Memory { 31 return &Memory{} 32 } 33 34 // Set sets offset + size to value 35 func (m *Memory) Set(offset, size uint64, value []byte) { 36 // It's possible the offset is greater than 0 and size equals 0. This is because 37 // the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP) 38 if size > 0 { 39 // length of store may never be less than offset + size. 40 // The store should be resized PRIOR to setting the memory 41 if offset+size > uint64(len(m.store)) { 42 panic("invalid memory: store empty") 43 } 44 copy(m.store[offset:offset+size], value) 45 } 46 } 47 48 // Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to 49 // 32 bytes. 50 func (m *Memory) Set32(offset uint64, val *uint256.Int) { 51 // length of store may never be less than offset + size. 52 // The store should be resized PRIOR to setting the memory 53 if offset+32 > uint64(len(m.store)) { 54 panic("invalid memory: store empty") 55 } 56 // Fill in relevant bits 57 b32 := val.Bytes32() 58 copy(m.store[offset:], b32[:]) 59 } 60 61 // Resize resizes the memory to size 62 func (m *Memory) Resize(size uint64) { 63 if uint64(m.Len()) < size { 64 m.store = append(m.store, make([]byte, size-uint64(m.Len()))...) 65 } 66 } 67 68 // GetCopy returns offset + size as a new slice 69 func (m *Memory) GetCopy(offset, size int64) (cpy []byte) { 70 if size == 0 { 71 return nil 72 } 73 74 if len(m.store) > int(offset) { 75 cpy = make([]byte, size) 76 copy(cpy, m.store[offset:offset+size]) 77 78 return 79 } 80 81 return 82 } 83 84 // GetPtr returns the offset + size 85 func (m *Memory) GetPtr(offset, size int64) []byte { 86 if size == 0 { 87 return nil 88 } 89 90 if len(m.store) > int(offset) { 91 return m.store[offset : offset+size] 92 } 93 94 return nil 95 } 96 97 // Len returns the length of the backing slice 98 func (m *Memory) Len() int { 99 return len(m.store) 100 } 101 102 // Data returns the backing slice 103 func (m *Memory) Data() []byte { 104 return m.store 105 } 106 107 // Copy copies data from the src position slice into the dst position. 108 // The source and destination may overlap. 109 // OBS: This operation assumes that any necessary memory expansion has already been performed, 110 // and this method may panic otherwise. 111 func (m *Memory) Copy(dst, src, len uint64) { 112 if len == 0 { 113 return 114 } 115 copy(m.store[dst:], m.store[src:src+len]) 116 }