github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/orgchain/storages/block_store_test.go (about) 1 package storages 2 3 import ( 4 "crypto/sha256" 5 "math/big" 6 "testing" 7 8 "fmt" 9 "os" 10 11 "time" 12 13 "github.com/sixexorg/magnetic-ring/common" 14 "github.com/sixexorg/magnetic-ring/core/orgchain/types" 15 "github.com/sixexorg/magnetic-ring/mock" 16 ) 17 18 var ( 19 testBlockStore *BlockStore 20 testAccountStore *AccountStore 21 ) 22 23 var ( 24 // tx *types.Transaction 25 txData *types.TxData 26 address_1 common.Address 27 address_2 common.Address 28 ) 29 30 func init() { 31 address_1, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfdy") 32 address_2, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfdz") 33 froms := &common.TxIns{} 34 froms.Tis = append(froms.Tis, 35 &common.TxIn{ 36 Address: address_1, 37 Nonce: 100, 38 Amount: big.NewInt(200), 39 }, 40 &common.TxIn{ 41 Address: address_2, 42 Nonce: 200, 43 Amount: big.NewInt(300), 44 }, 45 ) 46 tos := &common.TxOuts{} 47 tos.Tos = append(tos.Tos, 48 &common.TxOut{ 49 Address: address_1, 50 Amount: big.NewInt(200), 51 }, 52 &common.TxOut{ 53 Address: address_2, 54 Amount: big.NewInt(300), 55 }, 56 ) 57 txData = &types.TxData{ 58 Froms: froms, 59 Tos: tos, 60 Fee: big.NewInt(100), 61 } 62 63 // tx := &types.Transaction{ 64 // Version: 0x01, 65 // TxType: types.TransferUT, 66 // TxData: txData, 67 // } 68 } 69 70 func TestMain(m *testing.M) { 71 var err error 72 testBlockDir := "test/block" 73 testBlockStore, err = NewBlockStore(testBlockDir, false) 74 if err != nil { 75 fmt.Fprintf(os.Stderr, "NewBlockStore error %s\n", err) 76 return 77 } 78 testStateDir := "test/account" 79 testAccountStore, err = NewAccountStore(testStateDir) 80 if err != nil { 81 fmt.Fprintf(os.Stderr, "NewAccoutStore error %s\n", err) 82 return 83 } 84 m.Run() 85 err = testBlockStore.Close() 86 if err != nil { 87 fmt.Fprintf(os.Stderr, "testBlockStore.Close error %s\n", err) 88 return 89 } 90 err = os.RemoveAll("./test") 91 if err != nil { 92 fmt.Fprintf(os.Stderr, "os.RemoveAll error %s\n", err) 93 return 94 } 95 os.RemoveAll("ActorLog") 96 } 97 func TestVersion(t *testing.T) { 98 testBlockStore.NewBatch() 99 version := byte(1) 100 err := testBlockStore.SaveVersion(version) 101 if err != nil { 102 t.Errorf("SaveVersion error %s", err) 103 return 104 } 105 err = testBlockStore.CommitTo() 106 if err != nil { 107 t.Errorf("CommitTo error %s", err) 108 return 109 } 110 v, err := testBlockStore.GetVersion() 111 if err != nil { 112 t.Errorf("GetVersion error %s", err) 113 return 114 } 115 if version != v { 116 t.Errorf("TestVersion failed version %d != %d", v, version) 117 return 118 } 119 } 120 121 func TestCurrentBlock(t *testing.T) { 122 blockHash := common.Hash(sha256.Sum256([]byte("123456789"))) 123 t.Logf("%x", blockHash) 124 blockHeight := uint64(1) 125 testBlockStore.NewBatch() 126 err := testBlockStore.SaveCurrentBlock(blockHeight, blockHash) 127 if err != nil { 128 t.Errorf("SaveCurrentBlock error %s", err) 129 return 130 } 131 err = testBlockStore.CommitTo() 132 if err != nil { 133 t.Errorf("CommitTo error %s", err) 134 return 135 } 136 hash, height, err := testBlockStore.GetCurrentBlock() 137 if hash != blockHash { 138 t.Errorf("TestCurrentBlock BlockHash %x != %x", hash, blockHash) 139 return 140 } 141 if height != blockHeight { 142 t.Errorf("TestCurrentBlock BlockHeight %x != %x", height, blockHeight) 143 return 144 } 145 } 146 147 func TestBlockHash(t *testing.T) { 148 blockHash := common.Hash(sha256.Sum256([]byte("123456789"))) 149 blockHeight := uint64(1) 150 testBlockStore.NewBatch() 151 testBlockStore.SaveBlockHash(blockHeight, blockHash) 152 blockHash = common.Hash(sha256.Sum256([]byte("234567890"))) 153 blockHeight = uint64(2) 154 testBlockStore.SaveBlockHash(blockHeight, blockHash) 155 err := testBlockStore.CommitTo() 156 if err != nil { 157 t.Errorf("CommitTo error %s", err) 158 return 159 } 160 hash, err := testBlockStore.GetBlockHash(blockHeight) 161 if err != nil { 162 t.Errorf("GetBlockHash error %s", err) 163 return 164 } 165 if hash != blockHash { 166 t.Errorf("TestBlockHash failed BlockHash %x != %x", hash, blockHash) 167 return 168 } 169 } 170 171 func TestSaveTransaction(t *testing.T) { 172 tx := &types.Transaction{} 173 common.DeepCopy(&tx, mock.OrgTx1) 174 blockHeight := uint64(1) 175 txHash := tx.Hash() 176 177 exist, err := testBlockStore.ContainTransaction(txHash) 178 if err != nil { 179 t.Errorf("ContainTransaction error %s", err) 180 return 181 } 182 if exist { 183 t.Errorf("TestSaveTransaction ContainTransaction should be false.") 184 return 185 } 186 testBlockStore.NewBatch() 187 err = testBlockStore.SaveTransaction(tx, blockHeight) 188 if err != nil { 189 t.Errorf("SaveTransaction error %s", err) 190 return 191 } 192 err = testBlockStore.CommitTo() 193 if err != nil { 194 t.Errorf("CommitTo error %s", err) 195 return 196 } 197 198 tx1, height, err := testBlockStore.GetTransaction(txHash) 199 if err != nil { 200 t.Errorf("GetTransaction error %s", err) 201 return 202 } 203 if blockHeight != height { 204 t.Errorf("TestSaveTransaction failed BlockHeight %d != %d", height, blockHeight) 205 return 206 } 207 if tx1.TxType != tx.TxType { 208 t.Errorf("TestSaveTransaction failed TxType %d != %d", tx1.TxType, tx.TxType) 209 return 210 } 211 tx1Hash := tx1.Hash() 212 if txHash != tx1Hash { 213 t.Errorf("TestSaveTransaction failed TxHash %x != %x", tx1Hash, txHash) 214 return 215 } 216 217 exist, err = testBlockStore.ContainTransaction(txHash) 218 if err != nil { 219 t.Errorf("ContainTransaction error %s", err) 220 return 221 } 222 if !exist { 223 t.Errorf("TestSaveTransaction ContainTransaction should be true.") 224 return 225 } 226 } 227 228 func TestHeaderIndexList(t *testing.T) { 229 testBlockStore.NewBatch() 230 startHeight := uint64(0) 231 size := uint64(100) 232 indexMap := make(map[uint64]common.Hash, size) 233 indexList := make([]common.Hash, 0) 234 for i := startHeight; i < size; i++ { 235 hash := common.Hash(sha256.Sum256([]byte(fmt.Sprintf("%v", i)))) 236 indexMap[i] = hash 237 indexList = append(indexList, hash) 238 } 239 err := testBlockStore.SaveHeaderIndexList(startHeight, indexList) 240 if err != nil { 241 t.Errorf("SaveHeaderIndexList error %s", err) 242 return 243 } 244 startHeight = uint64(100) 245 indexMap = make(map[uint64]common.Hash, size) 246 for i := startHeight; i < size; i++ { 247 hash := common.Hash(sha256.Sum256([]byte(fmt.Sprintf("%v", i)))) 248 indexMap[i] = hash 249 indexList = append(indexList, hash) 250 } 251 err = testBlockStore.CommitTo() 252 if err != nil { 253 t.Errorf("CommitTo error %s", err) 254 return 255 } 256 257 totalMap, err := testBlockStore.GetHeaderIndexList() 258 if err != nil { 259 t.Errorf("GetHeaderIndexList error %s", err) 260 return 261 } 262 263 for height, hash := range indexList { 264 h, ok := totalMap[uint64(height)] 265 if !ok { 266 t.Errorf("TestHeaderIndexList failed height:%d hash not exist", height) 267 return 268 } 269 if hash != h { 270 t.Errorf("TestHeaderIndexList failed height:%d hash %x != %x", height, hash, h) 271 return 272 } 273 } 274 } 275 276 func TestSaveHeader(t *testing.T) { 277 header := &types.Header{ 278 Version: 123, 279 PrevBlockHash: common.Hash{}, 280 TxRoot: common.Hash{}, 281 Timestamp: uint64(time.Date(2017, time.February, 23, 0, 0, 0, 0, time.UTC).Unix()), 282 Height: uint64(1), 283 } 284 block := &types.Block{ 285 Header: header, 286 Transactions: []*types.Transaction{}, 287 } 288 blockHash := block.Hash() 289 290 testBlockStore.NewBatch() 291 err := testBlockStore.SaveHeader(block) 292 if err != nil { 293 t.Errorf("SaveHeader error %s", err) 294 return 295 } 296 err = testBlockStore.CommitTo() 297 if err != nil { 298 t.Errorf("CommitTo error %s", err) 299 return 300 } 301 302 h, err := testBlockStore.GetHeader(blockHash) 303 if err != nil { 304 t.Errorf("GetHeader error %s", err) 305 return 306 } 307 308 headerHash := h.Hash() 309 if blockHash != headerHash { 310 t.Errorf("TestSaveHeader failed HeaderHash \r\n %x \r\n %x", headerHash, blockHash) 311 return 312 } 313 314 if header.Height != h.Height { 315 t.Errorf("TestSaveHeader failed Height %d \r\n %d", h.Height, header.Height) 316 return 317 } 318 } 319 320 func TestBlock(t *testing.T) { 321 322 header := &types.Header{ 323 Version: 123, 324 PrevBlockHash: common.Hash{}, 325 TxRoot: common.Hash{}, 326 Timestamp: uint64(time.Date(2017, time.February, 23, 0, 0, 0, 0, time.UTC).Unix()), 327 Height: 2, 328 } 329 tx := &types.Transaction{} 330 common.DeepCopy(&tx, mock.OrgTx1) 331 block := &types.Block{ 332 Header: header, 333 Transactions: []*types.Transaction{tx}, 334 } 335 blockHash := block.Hash() 336 tx1Hash := tx.Hash() 337 338 testBlockStore.NewBatch() 339 340 err := testBlockStore.SaveBlock(block) 341 if err != nil { 342 t.Errorf("SaveHeader error %s", err) 343 return 344 } 345 err = testBlockStore.CommitTo() 346 if err != nil { 347 t.Errorf("CommitTo error %s", err) 348 return 349 } 350 351 b, err := testBlockStore.GetBlock(blockHash) 352 if err != nil { 353 t.Errorf("GetBlock error %s", err) 354 return 355 } 356 357 hash := b.Hash() 358 if hash != blockHash { 359 t.Errorf("TestBlock failed BlockHash %x != %x ", hash, blockHash) 360 return 361 } 362 exist, err := testBlockStore.ContainTransaction(tx1Hash) 363 if err != nil { 364 t.Errorf("ContainTransaction error %s", err) 365 return 366 } 367 if !exist { 368 t.Errorf("TestBlock failed transaction %x should exist", tx1Hash) 369 return 370 } 371 372 if len(block.Transactions) != len(b.Transactions) { 373 t.Errorf("TestBlock failed Transaction size %d != %d ", len(b.Transactions), len(block.Transactions)) 374 return 375 } 376 if b.Transactions[0].Hash() != tx1Hash { 377 t.Errorf("TestBlock failed transaction1 hash %x != %x", b.Transactions[0].Hash(), tx1Hash) 378 return 379 } 380 }