github.com/annchain/OG@v0.0.9/vm/ovm/memory_state_db.go (about) 1 package ovm 2 3 import ( 4 "fmt" 5 math2 "github.com/annchain/OG/arefactor/common/math" 6 "github.com/annchain/OG/arefactor/og/types" 7 ogcrypto2 "github.com/annchain/OG/deprecated/ogcrypto" 8 9 "github.com/annchain/OG/common" 10 "github.com/annchain/OG/common/math" 11 vmtypes "github.com/annchain/OG/vm/types" 12 "math/big" 13 "sort" 14 "strings" 15 ) 16 17 type MemoryStateDB struct { 18 soLedger map[common.Address]*vmtypes.StateObject 19 kvLedger map[common.Address]vmtypes.Storage 20 refund uint64 21 } 22 23 func (m *MemoryStateDB) GetStateObject(addr common.Address) *vmtypes.StateObject { 24 if v, ok := m.soLedger[addr]; ok { 25 return v 26 } 27 return nil 28 } 29 30 func (m *MemoryStateDB) SetStateObject(addr common.Address, stateObject *vmtypes.StateObject) { 31 m.soLedger[addr] = stateObject 32 } 33 34 func NewMemoryStateDB() *MemoryStateDB { 35 return &MemoryStateDB{ 36 soLedger: make(map[common.Address]*vmtypes.StateObject), 37 kvLedger: make(map[common.Address]vmtypes.Storage), 38 } 39 } 40 41 func (m *MemoryStateDB) CreateAccount(addr common.Address) { 42 if _, ok := m.soLedger[addr]; !ok { 43 m.soLedger[addr] = vmtypes.NewStateObject() 44 } 45 } 46 47 func (m *MemoryStateDB) SubBalance(addr common.Address, v *math.BigInt) { 48 m.soLedger[addr].Balance = new(big.Int).Sub(m.soLedger[addr].Balance, v.Value) 49 } 50 51 func (m *MemoryStateDB) AddBalance(addr common.Address, v *math.BigInt) { 52 m.soLedger[addr].Balance = new(big.Int).Add(m.soLedger[addr].Balance, v.Value) 53 } 54 55 func (m *MemoryStateDB) GetBalance(addr common.Address) *math.BigInt { 56 if v, ok := m.soLedger[addr]; ok { 57 return math.NewBigIntFromBigInt(v.Balance) 58 } 59 return math.NewBigIntFromBigInt(math2.Big0) 60 } 61 62 func (m *MemoryStateDB) GetNonce(addr common.Address) uint64 { 63 if v, ok := m.soLedger[addr]; ok { 64 return v.Nonce 65 } 66 return 0 67 } 68 69 func (m *MemoryStateDB) SetNonce(addr common.Address, nonce uint64) { 70 if v, ok := m.soLedger[addr]; ok { 71 v.Nonce = nonce 72 } 73 } 74 75 func (m *MemoryStateDB) GetCodeHash(addr common.Address) types.Hash { 76 if v, ok := m.soLedger[addr]; ok { 77 return v.CodeHash 78 } 79 return types.Hash{} 80 } 81 82 func (m *MemoryStateDB) GetCode(addr common.Address) []byte { 83 if v, ok := m.soLedger[addr]; ok { 84 return v.Code 85 } 86 return nil 87 } 88 89 func (m *MemoryStateDB) SetCode(addr common.Address, code []byte) { 90 if v, ok := m.soLedger[addr]; ok { 91 v.Code = code 92 v.CodeHash = ogcrypto2.Keccak256Hash(code) 93 v.DirtyCode = true 94 } 95 } 96 97 func (m *MemoryStateDB) GetCodeSize(addr common.Address) int { 98 if v, ok := m.soLedger[addr]; ok { 99 return len(v.Code) 100 } 101 return 0 102 } 103 104 func (m *MemoryStateDB) AddRefund(v uint64) { 105 m.refund += v 106 } 107 108 func (m *MemoryStateDB) SubRefund(v uint64) { 109 if v > m.refund { 110 panic("Refund counter below zero") 111 } 112 m.refund -= v 113 } 114 115 func (m *MemoryStateDB) GetRefund() uint64 { 116 return m.refund 117 } 118 119 func (m *MemoryStateDB) GetCommittedState(addr common.Address, hash types.Hash) types.Hash { 120 panic("implement me") 121 } 122 123 func (m *MemoryStateDB) GetState(addr common.Address, key types.Hash) types.Hash { 124 if kv, ok := m.kvLedger[addr]; ok { 125 if v, ok := kv[key]; ok { 126 return v 127 } 128 } 129 return types.Hash{} 130 } 131 132 func (m *MemoryStateDB) SetState(addr common.Address, key types.Hash, value types.Hash) { 133 if _, ok := m.kvLedger[addr]; !ok { 134 m.kvLedger[addr] = vmtypes.NewStorage() 135 } 136 m.kvLedger[addr][key] = value 137 } 138 139 func (m *MemoryStateDB) Suicide(addr common.Address) bool { 140 panic("implement me") 141 } 142 143 func (m *MemoryStateDB) HasSuicided(addr common.Address) bool { 144 panic("implement me") 145 } 146 147 func (m *MemoryStateDB) Exist(addr common.Address) bool { 148 _, ok := m.soLedger[addr] 149 return ok 150 } 151 152 func (m *MemoryStateDB) Empty(addr common.Address) bool { 153 panic("implement me") 154 } 155 156 func (m *MemoryStateDB) RevertToSnapshot(int) { 157 panic("implement me") 158 } 159 160 func (m *MemoryStateDB) Snapshot() int { 161 return 0 162 } 163 164 func (m *MemoryStateDB) AddLog(*vmtypes.Log) { 165 // Maybe we don't care about logs sinces we have layerdb. 166 return 167 } 168 169 func (m *MemoryStateDB) AddPreimage(hash types.Hash, preImage []byte) { 170 // Any usage? 171 return 172 } 173 174 func (m *MemoryStateDB) ForEachStorage(addr common.Address, cb func(key, value types.Hash) bool) { 175 panic("implement me") 176 } 177 178 func (m *MemoryStateDB) String() string { 179 b := strings.Builder{} 180 181 for k, v := range m.soLedger { 182 b.WriteString(fmt.Sprintf("%s: %s\n", k.String(), v)) 183 184 } 185 for k, v := range m.kvLedger { 186 b.WriteString(fmt.Sprintf("%s: -->\n", k.String())) 187 var keys types.Hashes 188 for sk := range v { 189 keys = append(keys, sk) 190 } 191 sort.Sort(keys) 192 193 for _, key := range keys { 194 b.WriteString(fmt.Sprintf("--> %s: %s\n", key.Hex(), v[key].Hex())) 195 196 } 197 } 198 return b.String() 199 }