github.com/amazechain/amc@v0.1.3/modules/state/plain_state_writer.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package state 18 19 import ( 20 "encoding/binary" 21 "github.com/amazechain/amc/common/account" 22 "github.com/amazechain/amc/common/types" 23 "github.com/amazechain/amc/modules" 24 "github.com/holiman/uint256" 25 "github.com/ledgerwatch/erigon-lib/kv" 26 "google.golang.org/protobuf/proto" 27 ) 28 29 var _ WriterWithChangeSets = (*PlainStateWriter)(nil) 30 31 type putDel interface { 32 kv.Putter 33 kv.Deleter 34 } 35 type PlainStateWriter struct { 36 db putDel 37 csw *ChangeSetWriter 38 //accumulator *shards.Accumulator 39 } 40 41 func NewPlainStateWriter(db putDel, changeSetsDB kv.RwTx, blockNumber uint64) *PlainStateWriter { 42 return &PlainStateWriter{ 43 db: db, 44 csw: NewChangeSetWriterPlain(changeSetsDB, blockNumber), 45 } 46 } 47 48 func NewPlainStateWriterNoHistory(db putDel) *PlainStateWriter { 49 return &PlainStateWriter{ 50 db: db, 51 } 52 } 53 54 //func (w *PlainStateWriter) SetAccumulator(accumulator *shards.Accumulator) *PlainStateWriter { 55 // w.accumulator = accumulator 56 // return w 57 //} 58 59 func (w *PlainStateWriter) UpdateAccountData(address types.Address, original, account *account.StateAccount) error { 60 //fmt.Printf("balance,%x,%d\n", address, &account.Balance) 61 if w.csw != nil { 62 if err := w.csw.UpdateAccountData(address, original, account); err != nil { 63 return err 64 } 65 } 66 //var value []byte 67 //account.EncodeForStorage(value) 68 pb := account.ToProtoMessage() 69 data, _ := proto.Marshal(pb) 70 //if w.accumulator != nil { 71 // w.accumulator.ChangeAccount(address, account.Incarnation, value) 72 //} 73 // defer fmt.Printf("Write Account address: %s, balance: %d \n", address, data) 74 return w.db.Put(modules.Account, address[:], data) 75 } 76 77 func (w *PlainStateWriter) UpdateAccountCode(address types.Address, incarnation uint16, codeHash types.Hash, code []byte) error { 78 //fmt.Printf("code,%x,%x\n", address, code) 79 if w.csw != nil { 80 if err := w.csw.UpdateAccountCode(address, incarnation, codeHash, code); err != nil { 81 return err 82 } 83 } 84 //if w.accumulator != nil { 85 // w.accumulator.ChangeCode(address, incarnation, code) 86 //} 87 if err := w.db.Put(modules.Code, codeHash[:], code); err != nil { 88 return err 89 } 90 return w.db.Put(modules.PlainContractCode, modules.PlainGenerateStoragePrefix(address[:], incarnation), codeHash[:]) 91 } 92 93 func (w *PlainStateWriter) DeleteAccount(address types.Address, original *account.StateAccount) error { 94 //fmt.Printf("delete,%x\n", address) 95 if w.csw != nil { 96 if err := w.csw.DeleteAccount(address, original); err != nil { 97 return err 98 } 99 } 100 //if w.accumulator != nil { 101 // w.accumulator.DeleteAccount(address) 102 //} 103 if err := w.db.Delete(modules.Account, address[:]); err != nil { 104 return err 105 } 106 if original.Incarnation > 0 { 107 var b [8]byte 108 binary.BigEndian.PutUint16(b[:], original.Incarnation) 109 if err := w.db.Put(modules.IncarnationMap, address[:], b[:]); err != nil { 110 return err 111 } 112 } 113 return nil 114 } 115 116 func (w *PlainStateWriter) WriteAccountStorage(address types.Address, incarnation uint16, key *types.Hash, original, value *uint256.Int) error { 117 //fmt.Printf("storage,%x,%x,%x\n", address, *key, value.Bytes()) 118 if w.csw != nil { 119 if err := w.csw.WriteAccountStorage(address, incarnation, key, original, value); err != nil { 120 return err 121 } 122 } 123 if *original == *value { 124 return nil 125 } 126 compositeKey := modules.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes()) 127 128 v := value.Bytes() 129 //if w.accumulator != nil { 130 // w.accumulator.ChangeStorage(address, incarnation, *key, v) 131 //} 132 if len(v) == 0 { 133 return w.db.Delete(modules.Storage, compositeKey) 134 } 135 return w.db.Put(modules.Storage, compositeKey, v) 136 } 137 138 func (w *PlainStateWriter) CreateContract(address types.Address) error { 139 if w.csw != nil { 140 if err := w.csw.CreateContract(address); err != nil { 141 return err 142 } 143 } 144 return nil 145 } 146 147 func (w *PlainStateWriter) WriteChangeSets() error { 148 if w.csw != nil { 149 return w.csw.WriteChangeSets() 150 } 151 152 return nil 153 } 154 155 func (w *PlainStateWriter) WriteHistory() error { 156 if w.csw != nil { 157 return w.csw.WriteHistory() 158 } 159 160 return nil 161 } 162 163 func (w *PlainStateWriter) ChangeSetWriter() *ChangeSetWriter { 164 return w.csw 165 }