github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/extstorages/extstorages_init_test.go (about) 1 package extstorages 2 3 import ( 4 "math/big" 5 "os" 6 "testing" 7 8 "github.com/sixexorg/magnetic-ring/common" 9 orgtypes "github.com/sixexorg/magnetic-ring/core/orgchain/types" 10 "github.com/sixexorg/magnetic-ring/mock" 11 "github.com/sixexorg/magnetic-ring/store/mainchain/extstates" 12 "github.com/sixexorg/magnetic-ring/store/storelaw" 13 ) 14 15 var ( 16 leadger *LedgerStoreImp 17 store *ExternalLeagueBlock 18 accountStore *ExternalLeague 19 ) 20 21 func TestMain(m *testing.M) { 22 ledgerPath := "test" 23 blockPath := "test/testBlock" 24 accountPath := "test/testAccount" 25 initLedger(ledgerPath) 26 initLeagueBlock(blockPath) 27 initAccountState(accountPath) 28 os.RemoveAll("./test") 29 m.Run() 30 } 31 32 func initLedger(dbdir string) { 33 var err error 34 leadger, err = NewLedgerStore(dbdir) 35 if err != nil { 36 panic(err) 37 } 38 exts := storelaw.AccountStaters{ 39 &extstates.LeagueAccountState{ 40 Address: mock.Address_1, 41 LeagueId: mock.LeagueAddress1, 42 Height: 1, 43 Data: &extstates.Account{ 44 Nonce: 1, 45 Balance: big.NewInt(100), 46 EnergyBalance: big.NewInt(200), 47 }, 48 }, 49 &extstates.LeagueAccountState{ 50 Address: mock.Address_2, 51 LeagueId: mock.LeagueAddress1, 52 Height: 1, 53 Data: &extstates.Account{ 54 Nonce: 1, 55 Balance: big.NewInt(50000), 56 EnergyBalance: big.NewInt(100000), 57 }, 58 }, 59 &extstates.LeagueAccountState{ 60 Address: mock.Address_3, 61 LeagueId: mock.LeagueAddress1, 62 Height: 1, 63 Data: &extstates.Account{ 64 Nonce: 1, 65 Balance: big.NewInt(232031), 66 EnergyBalance: big.NewInt(912312), 67 }, 68 }} 69 obi := &storelaw.OrgBlockInfo{ 70 Block: &orgtypes.Block{ 71 Header: &orgtypes.Header{ 72 Version: 0x01, 73 PrevBlockHash: common.Hash{}, 74 Height: 1, 75 LeagueId: mock.LeagueAddress1, 76 Difficulty: big.NewInt(10), 77 }, 78 Transactions: mock.Block2.Transactions, 79 }, 80 AccStates: exts, 81 FeeSum: big.NewInt(20000), 82 } 83 mainTxUsed := common.HashArray{common.Hash{1, 2, 3}, common.Hash{4, 5, 6}, common.Hash{7, 8, 9}} 84 err = ledgerStore.SaveAll(obi, mainTxUsed) 85 if err != nil { 86 panic(err) 87 } 88 } 89 func initAccountState(dbdir string) { 90 var err error 91 accountStore, err = NewExternalLeague(dbdir, false) 92 if err != nil { 93 panic(err) 94 } 95 exts := storelaw.AccountStaters{ 96 &extstates.LeagueAccountState{ 97 Address: mock.Address_1, 98 LeagueId: mock.LeagueAddress1, 99 Height: 1, 100 Data: &extstates.Account{ 101 Nonce: 1, 102 Balance: big.NewInt(100), 103 EnergyBalance: big.NewInt(200), 104 }, 105 }, 106 &extstates.LeagueAccountState{ 107 Address: mock.Address_2, 108 LeagueId: mock.LeagueAddress1, 109 Height: 1, 110 Data: &extstates.Account{ 111 Nonce: 1, 112 Balance: big.NewInt(50000), 113 EnergyBalance: big.NewInt(100000), 114 }, 115 }, 116 &extstates.LeagueAccountState{ 117 Address: mock.Address_3, 118 LeagueId: mock.LeagueAddress1, 119 Height: 1, 120 Data: &extstates.Account{ 121 Nonce: 1, 122 Balance: big.NewInt(232031), 123 EnergyBalance: big.NewInt(912312), 124 }, 125 }} 126 accountStore.NewBatch() 127 err = accountStore.BatchSave(exts) 128 if err != nil { 129 panic(err) 130 } 131 err = accountStore.CommitTo() 132 if err != nil { 133 panic(err) 134 } 135 } 136 func initLeagueBlock(dbdir string) { 137 var err error 138 store, err = NewExternalLeagueBlock(dbdir, false) 139 if err != nil { 140 panic(err) 141 } 142 block1 := &extstates.LeagueBlockSimple{ 143 Header: &orgtypes.Header{ 144 Version: 0x01, 145 PrevBlockHash: common.Hash{}, 146 Height: 1, 147 LeagueId: mock.LeagueAddress1, 148 Difficulty: big.NewInt(10), 149 }, 150 EnergyUsed: big.NewInt(10), 151 } 152 153 block2 := &extstates.LeagueBlockSimple{ 154 Header: &orgtypes.Header{ 155 Version: 0x01, 156 Height: 2, 157 PrevBlockHash: block1.Header.Hash(), 158 LeagueId: mock.LeagueAddress1, 159 Difficulty: big.NewInt(1), 160 }, 161 EnergyUsed: big.NewInt(20), 162 } 163 block3 := &extstates.LeagueBlockSimple{ 164 Header: &orgtypes.Header{ 165 Version: 0x01, 166 Height: 3, 167 PrevBlockHash: block2.Header.Hash(), 168 LeagueId: mock.LeagueAddress1, 169 Difficulty: big.NewInt(1), 170 }, 171 EnergyUsed: big.NewInt(30), 172 } 173 block4 := &extstates.LeagueBlockSimple{ 174 Header: &orgtypes.Header{ 175 Version: 0x01, 176 Height: 4, 177 PrevBlockHash: block3.Header.Hash(), 178 LeagueId: mock.LeagueAddress1, 179 Difficulty: big.NewInt(1), 180 }, 181 EnergyUsed: big.NewInt(30), 182 } 183 /*fmt.Println("block1 hash:", block1.Header.Hash().String()) 184 fmt.Println("block2 hash:", block2.Header.Hash().String()) 185 fmt.Println("block3 hash:", block3.Header.Hash().String()) 186 fmt.Println("block4 hash:", block4.Header.Hash().String())*/ 187 store.NewBatch() 188 err = store.BatchSave([]*extstates.LeagueBlockSimple{block1, block2, block3, block4}) 189 if err != nil { 190 panic(err) 191 } 192 err = store.CommitTo() 193 if err != nil { 194 panic(err) 195 } 196 }