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