github.com/amazechain/amc@v0.1.3/tests/state_test.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 tests 18 19 import ( 20 "embed" 21 "encoding/json" 22 "fmt" 23 "github.com/amazechain/amc/conf" 24 ) 25 26 //go:embed allocs 27 var allocs embed.FS 28 29 func ReadGenesis(filename string) *conf.Genesis { 30 f, err := allocs.Open(filename) 31 if err != nil { 32 panic("%s not found, use default genesis") 33 } 34 defer f.Close() 35 36 decoder := json.NewDecoder(f) 37 gc := new(conf.Genesis) 38 err = decoder.Decode(gc) 39 if err != nil { 40 panic(fmt.Sprintf("Could not parse genesis preallocation for %s: %v", filename, err)) 41 } 42 return gc 43 } 44 45 //func TestStateRoot(t *testing.T) { 46 // 47 // tmpDB := mdbx.NewMDBX(nil).InMem("").MapSize(2 * datasize.GB).MustOpen() 48 // defer tmpDB.Close() 49 // tx, err := tmpDB.BeginRw(context.Background()) 50 // if err != nil { 51 // panic(err) 52 // } 53 // defer tx.Rollback() 54 // 55 // r, roop := state.NewPlainStateReader(tx), state.NewNoopWriter() 56 // statedb := state.New(r) 57 // 58 // Genesis := ReadGenesis("allocs/genesis_inline.json") 59 // for _, a := range Genesis.Alloc { 60 // addr, err := types.HexToString(a.Address) 61 // if err != nil { 62 // panic(err) 63 // } 64 // 65 // b, ok := new(big.Int).SetString(a.Balance, 10) 66 // balance, _ := uint256.FromBig(b) 67 // if !ok { 68 // panic("overflow at genesis allocs") 69 // } 70 // 71 // statedb.AddBalance(addr, balance) 72 // statedb.SetCode(addr, a.Code) 73 // statedb.SetNonce(addr, a.Nonce) 74 // for key, value := range a.Storage { 75 // k := key 76 // val := uint256.NewInt(0).SetBytes(value.Bytes()) 77 // statedb.SetState(addr, &k, *val) 78 // } 79 // if len(a.Code) > 0 || len(a.Storage) > 0 { 80 // statedb.SetIncarnation(addr, state.FirstContractIncarnation) 81 // } 82 // } 83 // 84 // if err := statedb.FinalizeTx(Genesis.Config.Rules(0), roop); err != nil { 85 // panic(err) 86 // } 87 // 88 // //for i := 0; i < 1000; i++ { 89 // // root := statedb.GenerateRootHash().Hex() 90 // // if root != "0x1995d438cfe70662e278ed8b0d92c154f152e418b7d86c616fc538d969ba5eca" { 91 // // t.Error("not same", root) 92 // // } 93 // //} 94 //}