github.com/annchain/OG@v0.0.9/arefactor_core/core/accessor_test.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package core_test 15 16 import ( 17 "fmt" 18 "github.com/annchain/OG/common" 19 "github.com/annchain/OG/types/tx_types" 20 "io/ioutil" 21 "math/rand" 22 "os" 23 "testing" 24 25 "github.com/annchain/OG/common/crypto" 26 "github.com/annchain/OG/common/math" 27 "github.com/annchain/OG/core" 28 "github.com/annchain/OG/og" 29 "github.com/annchain/OG/ogdb" 30 "github.com/annchain/OG/types" 31 ) 32 33 var ( 34 // Secp256k1 Address "0x7349f7a6f622378d5fb0e2c16b9d4a3e5237c187" 35 testPkSecp0 = "0x0170E6B713CD32904D07A55B3AF5784E0B23EB38589EBF975F0AB89E6F8D786F00" 36 37 // Secp256k1 Address "0x96f4ac2f3215b80ea3a6466ebc1f268f6f1d5406" 38 testPkSecp1 = "0x0170E6B713CD32904D07A55B3AF5784E0B23EB38589EBF975F0AB89E6F8D786F01" 39 40 // Secp256k1 Address "0xa70c8a9485441f6fa2141d550c26d793107d3dea" 41 testPkSecp2 = "0x0170E6B713CD32904D07A55B3AF5784E0B23EB38589EBF975F0AB89E6F8D786F02" 42 ) 43 44 func newTestAddress(priv crypto.PrivateKey) common.Address { 45 signer := crypto.NewSigner(priv.Type) 46 pubkey := signer.PubKey(priv) 47 return signer.Address(pubkey) 48 } 49 50 func newTestLDB(dirPrefix string) (*ogdb.LevelDB, func()) { 51 dirname, err := ioutil.TempDir(os.TempDir(), "ogdb_test_"+dirPrefix+"_") 52 if err != nil { 53 panic("failed to create test file: " + err.Error()) 54 } 55 db, err := ogdb.NewLevelDB(dirname, 0, 0) 56 if err != nil { 57 panic("failed to create test database: " + err.Error()) 58 } 59 60 return db, func() { 61 db.Close() 62 os.RemoveAll(dirname) 63 } 64 } 65 66 func newTestUnsealTx(nonce uint64) *tx_types.Tx { 67 txCreator := &og.TxCreator{} 68 pk, _ := crypto.PrivateKeyFromString(testPkSecp0) 69 addr := newTestAddress(pk) 70 71 tx := txCreator.NewSignedTx(addr, addr, math.NewBigInt(0), nonce, pk, 0) 72 tx.SetHash(tx.CalcTxHash()) 73 74 return tx.(*tx_types.Tx) 75 } 76 77 func newTestSeq(nonce uint64) *tx_types.Sequencer { 78 txCreator := &og.TxCreator{} 79 pk, _ := crypto.PrivateKeyFromString(testPkSecp1) 80 addr := newTestAddress(pk) 81 82 seq := txCreator.NewSignedSequencer(addr, nonce, nonce, pk) 83 seq.SetHash(seq.CalcTxHash()) 84 85 return seq.(*tx_types.Sequencer) 86 } 87 88 func compareTxi(tx1, tx2 types.Txi) bool { 89 return tx1.Compare(tx2) 90 } 91 92 func TestTransactionStorage(t *testing.T) { 93 t.Parallel() 94 95 db, remove := newTestLDB("TestTransactionStorage") 96 defer remove() 97 98 var err error 99 acc := core.NewAccessor(db) 100 101 // test tx read write 102 tx := newTestUnsealTx(0) 103 err = acc.WriteTransaction(nil, tx) 104 if err != nil { 105 t.Fatalf("write tx %s failed: %v", tx.GetTxHash().String(), err) 106 } 107 txRead := acc.ReadTransaction(tx.GetTxHash()) 108 if txRead == nil { 109 t.Fatalf("cannot read tx %s from db", tx.GetTxHash().String()) 110 } 111 if !compareTxi(tx, txRead) { 112 t.Fatalf("the tx from db is not equal to the base tx") 113 } 114 // test tx delete 115 err = acc.DeleteTransaction(tx.GetTxHash()) 116 if err != nil { 117 t.Fatalf("delete tx %s failed: %v", tx.GetTxHash().String(), err) 118 } 119 txDeleted := acc.ReadTransaction(tx.GetTxHash()) 120 if txDeleted != nil { 121 t.Fatalf("tx %s have not deleted yet", tx.GetTxHash().String()) 122 } 123 124 // test sequencer read write 125 seq := newTestSeq(1) 126 err = acc.WriteTransaction(nil, seq) 127 if err != nil { 128 t.Fatalf("write seq %s failed: %v", seq.GetTxHash().String(), err) 129 } 130 seqRead := acc.ReadTransaction(seq.GetTxHash()) 131 if seqRead == nil { 132 t.Fatalf("cannot read seq %s from db", seq.GetTxHash().String()) 133 } 134 if !compareTxi(seq, seqRead) { 135 t.Fatalf("the seq from db is not equal to the base seq") 136 } 137 } 138 139 func TestGenesisStorage(t *testing.T) { 140 t.Parallel() 141 142 db, remove := newTestLDB("TestGenesisStorage") 143 defer remove() 144 145 var err error 146 acc := core.NewAccessor(db) 147 148 genesis := newTestSeq(0) 149 err = acc.WriteGenesis(genesis) 150 if err != nil { 151 t.Fatalf("write genesis error: %v", err) 152 } 153 genesisRead := acc.ReadGenesis() 154 if genesisRead == nil { 155 t.Fatalf("read genesis error") 156 } 157 if !compareTxi(genesis, genesisRead) { 158 t.Fatalf("genesis initialized is not the same as genesis stored") 159 } 160 } 161 162 func TestLatestSeqStorage(t *testing.T) { 163 t.Parallel() 164 165 db, remove := newTestLDB("TestLatestSeqStorage") 166 defer remove() 167 168 var err error 169 acc := core.NewAccessor(db) 170 171 latestSeq := newTestSeq(0) 172 err = acc.WriteLatestSequencer(nil, latestSeq) 173 if err != nil { 174 t.Fatalf("write latest sequencer error: %v", err) 175 } 176 latestSeqRead := acc.ReadLatestSequencer() 177 if latestSeqRead == nil { 178 t.Fatalf("read latest sequencer error") 179 } 180 if !compareTxi(latestSeq, latestSeqRead) { 181 t.Fatalf("latest sequencer initialized is not the same as latest sequencer stored") 182 } 183 } 184 185 func TestBalanceStorage(t *testing.T) { 186 t.Parallel() 187 188 db, remove := newTestLDB("TestBalanceStorage") 189 defer remove() 190 191 var err error 192 acc := core.NewAccessor(db) 193 pk, _ := crypto.PrivateKeyFromString(testPkSecp1) 194 addr := newTestAddress(pk) 195 196 balance := acc.ReadBalance(addr) 197 if balance == nil { 198 t.Fatalf("read balance failed") 199 } 200 if balance.Value.Cmp(math.NewBigInt(0).Value) != 0 { 201 t.Fatalf("the balance of addr %s is not 0", addr.String()) 202 } 203 204 newBalance := math.NewBigInt(int64(rand.Intn(10000) + 10000)) 205 err = acc.SetBalance(nil, addr, newBalance) 206 if err != nil { 207 t.Fatalf("set new balance failed: %v", err) 208 } 209 bFromDb := acc.ReadBalance(addr) 210 if bFromDb == nil { 211 t.Fatalf("read balance after seting failed") 212 } 213 if bFromDb.Value.Cmp(newBalance.Value) != 0 { 214 t.Fatalf("the balance in db is not equal the balance we set") 215 } 216 217 addAmount := math.NewBigInt(int64(rand.Intn(10000))) 218 err = acc.AddBalance(nil, addr, addAmount) 219 if err != nil { 220 t.Fatalf("add balance failed") 221 } 222 bFromDb = acc.ReadBalance(addr) 223 if bFromDb == nil { 224 t.Fatalf("read balance after adding failed") 225 } 226 if newBalance.Value.Add(newBalance.Value, addAmount.Value).Cmp(bFromDb.Value) != 0 { 227 t.Fatalf("the balance after add is not as expected") 228 } 229 230 subAmount := math.NewBigInt(int64(rand.Intn(10000))) 231 err = acc.SubBalance(nil, addr, subAmount) 232 if err != nil { 233 t.Fatalf("sub balance failed") 234 } 235 bFromDb = acc.ReadBalance(addr) 236 if bFromDb == nil { 237 t.Fatalf("read balance after sub failed") 238 } 239 if newBalance.Value.Sub(newBalance.Value, subAmount.Value).Cmp(bFromDb.Value) != 0 { 240 t.Fatalf("the balance after sub is not as expected") 241 } 242 } 243 244 // Note that nonce is now managed by statedb, so no need to test 245 // latest nonce in accessor part. 246 247 func TestDag_Start(t *testing.T) { 248 db, remove := newTestLDB("TestTransactionStorage") 249 defer remove() 250 251 acc := core.NewAccessor(db) 252 seq := tx_types.RandomSequencer() 253 height := seq.Height 254 //acc.WriteLatestConfirmedSeq(nil,seq) 255 batch := acc.NewBatch() 256 acc.WriteSequencerByHeight(batch, seq) 257 fmt.Println(seq) 258 err := batch.Write() 259 if err != nil { 260 t.Fatal(err) 261 } 262 readSeq, err := acc.ReadSequencerByHeight(height) 263 if err != nil { 264 fmt.Println(batch.ValueSize()) 265 t.Fatal(err) 266 } 267 if readSeq.Height != height { 268 t.Fatal(readSeq, seq) 269 } 270 fmt.Println(seq, readSeq) 271 }