github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/storages/ledger_store_test.go (about) 1 package storages_test 2 3 import ( 4 "os" 5 "testing" 6 7 "math/big" 8 "time" 9 10 "fmt" 11 12 "github.com/sixexorg/magnetic-ring/common" 13 "github.com/sixexorg/magnetic-ring/core/mainchain/types" 14 "github.com/sixexorg/magnetic-ring/store/mainchain/states" 15 "github.com/sixexorg/magnetic-ring/store/mainchain/storages" 16 ) 17 18 func TestLedger(t *testing.T) { 19 var err error 20 dbdir := "test/ledger" 21 defer os.RemoveAll(dbdir) 22 ledger, err := storages.NewLedgerStore(dbdir) 23 if err != nil { 24 return 25 } 26 address_1, _ := common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfda") 27 as := &states.AccountState{ 28 Address: address_1, 29 Height: 1, 30 Data: &states.Account{ 31 Nonce: 1, 32 Balance: big.NewInt(0).SetUint64(1000), 33 EnergyBalance: big.NewInt(0).SetUint64(1000), 34 }, 35 } 36 sts := states.AccountStates{as} 37 38 txs := make(types.Transactions, 0, 2) 39 //todo hello world msg 40 block := &types.Block{ 41 Header: &types.Header{ 42 Height: 1, 43 Version: 0x01, 44 PrevBlockHash: common.Hash{}, 45 LeagueRoot: common.Hash{}, 46 ReceiptsRoot: common.Hash{}, 47 StateRoot: sts.GetHashRoot(), 48 TxRoot: txs.GetHashRoot(), 49 Timestamp: uint64(time.Now().Unix()), 50 }, 51 Transactions: txs, 52 } 53 blockInfo := &storages.BlockInfo{ 54 Block: block, 55 AccountStates: sts, 56 } 57 err = ledger.SaveAll(blockInfo) 58 if err != nil { 59 t.Error(err) 60 t.Fail() 61 return 62 } 63 64 block1, err := ledger.GetBlockByHeight(1) 65 if err != nil { 66 t.Error(err) 67 t.Fail() 68 return 69 } 70 fmt.Println(block1.Header.Height) 71 fmt.Println(ledger.GetCurrentBlockHeight(), ledger.GetCurrentBlockHash()) 72 blockHash, err := ledger.GetBlockHashByHeight(1) 73 if err != nil { 74 t.Error(err) 75 t.Fail() 76 return 77 } 78 fmt.Println("blockHash:", blockHash) 79 //fmt.Println(ledger.GetAccountStore().GetAccount(address_1).Data.Balance.Uint64()) 80 } 81 82 func TestAddHeaders(t *testing.T) { 83 var err error 84 dbdir := "test/ledger" 85 defer os.RemoveAll(dbdir) 86 ledger, err := storages.NewLedgerStore(dbdir) 87 if err != nil { 88 return 89 } 90 address_1, _ := common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfda") 91 as := &states.AccountState{ 92 Address: address_1, 93 Height: 1, 94 Data: &states.Account{ 95 Nonce: 1, 96 Balance: big.NewInt(0).SetUint64(1000), 97 EnergyBalance: big.NewInt(0).SetUint64(1000), 98 }, 99 } 100 sts := states.AccountStates{as} 101 102 txs := make(types.Transactions, 0, 2) 103 //todo hello world msg 104 105 Header := &types.Header{ 106 Height: 1, 107 Version: 0x01, 108 PrevBlockHash: common.Hash{}, 109 LeagueRoot: common.Hash{}, 110 ReceiptsRoot: common.Hash{}, 111 StateRoot: sts.GetHashRoot(), 112 TxRoot: txs.GetHashRoot(), 113 Timestamp: uint64(time.Now().Unix()), 114 } 115 err = ledger.AddHeaders([]*types.Header{Header}) 116 if err != nil { 117 t.Error(err) 118 t.Fail() 119 return 120 } 121 blockHash, err := ledger.GetBlockHashByHeight(1) 122 if err != nil { 123 t.Error(err) 124 t.Fail() 125 return 126 } 127 t.Log(blockHash) 128 }