github.com/codingfuture/orig-energi3@v0.8.4/energi/common/testutils/migration_utils.go (about) 1 // Copyright 2019 The Energi Core Authors 2 // This file is part of the Energi Core library. 3 // 4 // The Energi Core 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 Energi Core 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 Energi Core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package testutils 18 19 import ( 20 "crypto/rand" 21 "crypto/sha256" 22 "encoding/json" 23 "fmt" 24 "io/ioutil" 25 "math/big" 26 "os" 27 28 "github.com/ethereum/go-ethereum/params" 29 "github.com/shengdoushi/base58" 30 ) 31 32 type snapshotItem struct { 33 Owner string `json:"owner"` 34 Amount *big.Int `json:"amount"` 35 Atype string `json:"type"` 36 } 37 38 type snapshot struct { 39 Txouts []snapshotItem `json:"snapshot_utxos"` 40 Blacklist []string `json:"snapshot_blacklist"` 41 Hash string `json:"snapshot_hash"` 42 } 43 44 type TestGen2Migration struct { 45 tempFile *os.File 46 } 47 48 // NewTestGen2Migration returns a testGen2Migration instance 49 func NewTestGen2Migration() *TestGen2Migration { 50 tmpFile, err := ioutil.TempFile(os.TempDir(), "node-simulation-") 51 if err != nil { 52 panic(fmt.Errorf("Cannot create temporary file: %v", err)) 53 } 54 return &TestGen2Migration{tmpFile} 55 } 56 57 // CleanUp deletes the temporary file created. This should be called once the 58 // test is complete. 59 func (tg *TestGen2Migration) CleanUp() error { 60 if tg == nil { 61 return nil 62 } 63 return os.Remove(tg.tempFile.Name()) 64 } 65 66 // TempFileName returns the temp migrations file path. 67 func (tg *TestGen2Migration) TempFileName() string { 68 if tg == nil { 69 return "" 70 } 71 return tg.tempFile.Name() 72 } 73 74 // PrepareTestGen2Migration creates a gen2 migration temp file. 75 func (tg *TestGen2Migration) PrepareTestGen2Migration(chainID uint64) error { 76 if tg == nil { 77 return nil 78 } 79 prefix := byte(33) 80 if chainID == 49797 { 81 prefix = byte(127) 82 } 83 84 res := make([]byte, 20) 85 _, err := rand.Read(res) 86 if err != nil { 87 return err 88 } 89 90 owner := make([]byte, 25) 91 owner[0] = prefix 92 copy(owner[1:], res[:]) 93 ownerhash := sha256.Sum256(owner[:21]) 94 copy(owner[21:], ownerhash[:4]) 95 96 items := int(params.MinGasLimit / 100000) 97 snapshotItems := make([]snapshotItem, 0, items) 98 for i := 0; i < items; i++ { 99 snapshotItems = append(snapshotItems, snapshotItem{ 100 Owner: base58.Encode(owner, base58.BitcoinAlphabet), 101 Amount: big.NewInt(1000), 102 Atype: "pubkeyhash", 103 }) 104 } 105 106 migrations := snapshot{ 107 Txouts: snapshotItems, 108 Blacklist: []string{"tWFyUdwGxEkcam2aikVsDMPDpvMNKfP2XV"}, 109 Hash: "778d7a438e3b86e0e754c4e46af802f852eb7c051d268c8599aa17c0cb9ce819", 110 } 111 112 data, err := json.Marshal(migrations) 113 if err != nil { 114 return err 115 } 116 117 _, err = tg.tempFile.Write(data) 118 return err 119 }