• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revisionc71db6df4fac0f5206aac5b0582f7e46f89babf2 (tree)
Time2019-07-09 22:15:47
AuthorChengcheng Zhang <943420582@qq.c...>
CommiterChengcheng Zhang

Log Message

remove loop

Change Summary

Incremental Difference

--- a/test/wallet_test.go
+++ b/test/wallet_test.go
@@ -1,7 +1,6 @@
11 package test
22
33 import (
4- "fmt"
54 "io/ioutil"
65 "os"
76 "reflect"
@@ -26,260 +25,240 @@ import (
2625 )
2726
2827 func TestWalletUpdate(t *testing.T) {
29- update := func() {
30- dirPath, err := ioutil.TempDir(".", "")
31- if err != nil {
32- t.Fatal(err)
33- }
34- defer os.RemoveAll(dirPath)
35-
36- config.CommonConfig = config.DefaultConfig()
37- testDB := dbm.NewDB("testdb", "leveldb", "temp")
38- defer func() {
39- testDB.Close()
40- os.RemoveAll("temp")
41- }()
42-
43- store := database.NewStore(testDB)
44- walletStore := database.NewWalletStore(testDB)
45- dispatcher := event.NewDispatcher()
46- txPool := protocol.NewTxPool(store, dispatcher)
47-
48- chain, err := protocol.NewChain(store, txPool, dispatcher)
49- if err != nil {
50- t.Fatal(err)
51- }
52-
53- accountStore := database.NewAccountStore(testDB)
54- accountManager := account.NewManager(accountStore, chain)
55- hsm, err := pseudohsm.New(dirPath)
56- if err != nil {
57- t.Fatal(err)
58- }
59-
60- xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
61- if err != nil {
62- t.Fatal(err)
63- }
28+ dirPath, err := ioutil.TempDir(".", "")
29+ if err != nil {
30+ t.Fatal(err)
31+ }
32+ defer os.RemoveAll(dirPath)
33+
34+ config.CommonConfig = config.DefaultConfig()
35+ testDB := dbm.NewDB("testdb", "leveldb", "temp")
36+ defer func() {
37+ testDB.Close()
38+ os.RemoveAll("temp")
39+ }()
40+
41+ store := database.NewStore(testDB)
42+ walletStore := database.NewWalletStore(testDB)
43+ dispatcher := event.NewDispatcher()
44+ txPool := protocol.NewTxPool(store, dispatcher)
45+
46+ chain, err := protocol.NewChain(store, txPool, dispatcher)
47+ if err != nil {
48+ t.Fatal(err)
49+ }
6450
65- testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
66- if err != nil {
67- t.Fatal(err)
68- }
51+ accountStore := database.NewAccountStore(testDB)
52+ accountManager := account.NewManager(accountStore, chain)
53+ hsm, err := pseudohsm.New(dirPath)
54+ if err != nil {
55+ t.Fatal(err)
56+ }
6957
70- controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
71- if err != nil {
72- t.Fatal(err)
73- }
58+ xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
59+ if err != nil {
60+ t.Fatal(err)
61+ }
7462
75- controlProg.KeyIndex = 1
63+ testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
64+ if err != nil {
65+ t.Fatal(err)
66+ }
7667
77- reg := asset.NewRegistry(testDB, chain)
78- asset := bc.AssetID{V0: 5}
68+ controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
69+ if err != nil {
70+ t.Fatal(err)
71+ }
7972
80- utxos := []*account.UTXO{}
81- btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
82- utxos = append(utxos, btmUtxo)
83- OtherUtxo := mockUTXO(controlProg, &asset)
84- utxos = append(utxos, OtherUtxo)
73+ controlProg.KeyIndex = 1
8574
86- _, txData, err := mockTxData(utxos, testAccount)
87- if err != nil {
88- t.Fatal(err)
89- }
75+ reg := asset.NewRegistry(testDB, chain)
76+ asset := bc.AssetID{V0: 5}
9077
91- tx := types.NewTx(*txData)
92- block := mockSingleBlock(tx)
93- txStatus := bc.NewTransactionStatus()
94- txStatus.SetStatus(0, false)
95- txStatus.SetStatus(1, false)
96- store.SaveBlock(block, txStatus)
78+ utxos := []*account.UTXO{}
79+ btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
80+ utxos = append(utxos, btmUtxo)
81+ OtherUtxo := mockUTXO(controlProg, &asset)
82+ utxos = append(utxos, OtherUtxo)
9783
98- w := newMockWallet(walletStore, accountManager, reg, chain, dispatcher, true)
99- err = w.AttachBlock(block)
100- if err != nil {
101- t.Fatal(err)
102- }
84+ _, txData, err := mockTxData(utxos, testAccount)
85+ if err != nil {
86+ t.Fatal(err)
87+ }
10388
104- if _, err := w.GetTransactionByTxID(tx.ID.String()); err != nil {
105- t.Fatal(err)
106- }
89+ tx := types.NewTx(*txData)
90+ block := mockSingleBlock(tx)
91+ txStatus := bc.NewTransactionStatus()
92+ txStatus.SetStatus(0, false)
93+ txStatus.SetStatus(1, false)
94+ store.SaveBlock(block, txStatus)
95+
96+ w := newMockWallet(walletStore, accountManager, reg, chain, dispatcher, true)
97+ err = w.AttachBlock(block)
98+ if err != nil {
99+ t.Fatal(err)
100+ }
107101
108- wants, err := w.GetTransactions(testAccount.ID, "", 1, false)
109- if len(wants) != 1 {
110- t.Fatal(err)
111- }
102+ if _, err := w.GetTransactionByTxID(tx.ID.String()); err != nil {
103+ t.Fatal(err)
104+ }
112105
113- if wants[0].ID != tx.ID {
114- t.Fatal("account txID mismatch")
115- }
106+ wants, err := w.GetTransactions(testAccount.ID, "", 1, false)
107+ if len(wants) != 1 {
108+ t.Fatal(err)
109+ }
116110
117- for position, tx := range block.Transactions {
118- get := testDB.Get(database.CalcGlobalTxIndexKey(tx.ID.String()))
119- bh := block.BlockHeader.Hash()
120- expect := database.CalcGlobalTxIndex(&bh, uint64(position))
121- if !reflect.DeepEqual(get, expect) {
122- t.Fatalf("position#%d: compare retrieved globalTxIdx err", position)
123- }
124- }
111+ if wants[0].ID != tx.ID {
112+ t.Fatal("account txID mismatch")
125113 }
126114
127- for i := 0; i < 100; i++ {
128- fmt.Printf("case i: %v\n", i)
129- update()
115+ for position, tx := range block.Transactions {
116+ get := testDB.Get(database.CalcGlobalTxIndexKey(tx.ID.String()))
117+ bh := block.BlockHeader.Hash()
118+ expect := database.CalcGlobalTxIndex(&bh, uint64(position))
119+ if !reflect.DeepEqual(get, expect) {
120+ t.Fatalf("position#%d: compare retrieved globalTxIdx err", position)
121+ }
130122 }
131123 }
132124
133125 func TestRescanWallet(t *testing.T) {
134- rescan := func() {
135- // prepare wallet & db.
136- dirPath, err := ioutil.TempDir(".", "")
137- if err != nil {
138- t.Fatal(err)
139- }
140- defer os.RemoveAll(dirPath)
141-
142- config.CommonConfig = config.DefaultConfig()
143- testDB := dbm.NewDB("testdb", "leveldb", "temp")
144- walletStore := database.NewWalletStore(testDB)
145- defer func() {
146- testDB.Close()
147- os.RemoveAll("temp")
148- }()
149-
150- store := database.NewStore(testDB)
151- dispatcher := event.NewDispatcher()
152- txPool := protocol.NewTxPool(store, dispatcher)
153- chain, err := protocol.NewChain(store, txPool, dispatcher)
154- if err != nil {
155- t.Fatal(err)
156- }
157-
158- statusInfo := wt.StatusInfo{
159- Version: uint(1),
160- WorkHash: bc.Hash{V0: 0xff},
161- }
162- if err := walletStore.SetWalletInfo(&statusInfo); err != nil {
163- t.Fatal(err)
164- }
165- walletInfo, err := walletStore.GetWalletInfo()
166- if err != nil {
167- t.Fatal(err)
168- }
126+ // prepare wallet & db.
127+ dirPath, err := ioutil.TempDir(".", "")
128+ if err != nil {
129+ t.Fatal(err)
130+ }
131+ defer os.RemoveAll(dirPath)
132+
133+ config.CommonConfig = config.DefaultConfig()
134+ testDB := dbm.NewDB("testdb", "leveldb", "temp")
135+ walletStore := database.NewWalletStore(testDB)
136+ defer func() {
137+ testDB.Close()
138+ os.RemoveAll("temp")
139+ }()
140+
141+ store := database.NewStore(testDB)
142+ dispatcher := event.NewDispatcher()
143+ txPool := protocol.NewTxPool(store, dispatcher)
144+ chain, err := protocol.NewChain(store, txPool, dispatcher)
145+ if err != nil {
146+ t.Fatal(err)
147+ }
169148
170- accountStore := database.NewAccountStore(testDB)
171- accountManager := account.NewManager(accountStore, chain)
172- w := newMockWallet(walletStore, accountManager, nil, chain, dispatcher, false)
173- if err != nil {
174- t.Fatal(err)
175- }
176- w.Status = *walletInfo
149+ statusInfo := wt.StatusInfo{
150+ Version: uint(1),
151+ WorkHash: bc.Hash{V0: 0xff},
152+ }
153+ if err := walletStore.SetWalletInfo(&statusInfo); err != nil {
154+ t.Fatal(err)
155+ }
156+ walletInfo, err := walletStore.GetWalletInfo()
157+ if err != nil {
158+ t.Fatal(err)
159+ }
177160
178- // rescan wallet.
179- if err := w.LoadWalletInfo(); err != nil {
180- t.Fatal(err)
181- }
161+ accountStore := database.NewAccountStore(testDB)
162+ accountManager := account.NewManager(accountStore, chain)
163+ w := newMockWallet(walletStore, accountManager, nil, chain, dispatcher, false)
164+ if err != nil {
165+ t.Fatal(err)
166+ }
167+ w.Status = *walletInfo
182168
183- block := config.GenesisBlock()
184- if w.Status.WorkHash != block.Hash() {
185- t.Fatal("reattach from genesis block")
186- }
169+ // rescan wallet.
170+ if err := w.LoadWalletInfo(); err != nil {
171+ t.Fatal(err)
187172 }
188173
189- for i := 0; i < 100; i++ {
190- fmt.Printf("case i: %v\n", i)
191- rescan()
174+ block := config.GenesisBlock()
175+ if w.Status.WorkHash != block.Hash() {
176+ t.Fatal("reattach from genesis block")
192177 }
193178 }
194179
195180 func TestMemPoolTxQueryLoop(t *testing.T) {
196- query := func() {
197- dirPath, err := ioutil.TempDir(".", "")
198- if err != nil {
199- t.Fatal(err)
200- }
201- config.CommonConfig = config.DefaultConfig()
202- testDB := dbm.NewDB("testdb", "leveldb", dirPath)
203- defer func() {
204- testDB.Close()
205- os.RemoveAll(dirPath)
206- }()
207-
208- store := database.NewStore(testDB)
209- dispatcher := event.NewDispatcher()
210- txPool := protocol.NewTxPool(store, dispatcher)
211-
212- chain, err := protocol.NewChain(store, txPool, dispatcher)
213- if err != nil {
214- t.Fatal(err)
215- }
216-
217- accountStore := database.NewAccountStore(testDB)
218- accountManager := account.NewManager(accountStore, chain)
219- hsm, err := pseudohsm.New(dirPath)
220- if err != nil {
221- t.Fatal(err)
222- }
223-
224- xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
225- if err != nil {
226- t.Fatal(err)
227- }
181+ dirPath, err := ioutil.TempDir(".", "")
182+ if err != nil {
183+ t.Fatal(err)
184+ }
185+ config.CommonConfig = config.DefaultConfig()
186+ testDB := dbm.NewDB("testdb", "leveldb", dirPath)
187+ defer func() {
188+ testDB.Close()
189+ os.RemoveAll(dirPath)
190+ }()
191+
192+ store := database.NewStore(testDB)
193+ dispatcher := event.NewDispatcher()
194+ txPool := protocol.NewTxPool(store, dispatcher)
195+
196+ chain, err := protocol.NewChain(store, txPool, dispatcher)
197+ if err != nil {
198+ t.Fatal(err)
199+ }
228200
229- testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
230- if err != nil {
231- t.Fatal(err)
232- }
201+ accountStore := database.NewAccountStore(testDB)
202+ accountManager := account.NewManager(accountStore, chain)
203+ hsm, err := pseudohsm.New(dirPath)
204+ if err != nil {
205+ t.Fatal(err)
206+ }
233207
234- controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
235- if err != nil {
236- t.Fatal(err)
237- }
208+ xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
209+ if err != nil {
210+ t.Fatal(err)
211+ }
238212
239- controlProg.KeyIndex = 1
213+ testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
214+ if err != nil {
215+ t.Fatal(err)
216+ }
240217
241- reg := asset.NewRegistry(testDB, chain)
242- asset := bc.AssetID{V0: 5}
218+ controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
219+ if err != nil {
220+ t.Fatal(err)
221+ }
243222
244- utxos := []*account.UTXO{}
245- btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
246- utxos = append(utxos, btmUtxo)
247- OtherUtxo := mockUTXO(controlProg, &asset)
248- utxos = append(utxos, OtherUtxo)
223+ controlProg.KeyIndex = 1
249224
250- _, txData, err := mockTxData(utxos, testAccount)
251- if err != nil {
252- t.Fatal(err)
253- }
225+ reg := asset.NewRegistry(testDB, chain)
226+ asset := bc.AssetID{V0: 5}
254227
255- tx := types.NewTx(*txData)
256- txStatus := bc.NewTransactionStatus()
257- txStatus.SetStatus(0, false)
258- walletStore := database.NewWalletStore(testDB)
259- w := newMockWallet(walletStore, accountManager, reg, chain, dispatcher, false)
260- go w.MemPoolTxQueryLoop()
261- w.EventDispatcher.Post(protocol.TxMsgEvent{TxMsg: &protocol.TxPoolMsg{TxDesc: &protocol.TxDesc{Tx: tx}, MsgType: protocol.MsgNewTx}})
262- time.Sleep(time.Millisecond * 10)
263- if _, err := w.GetUnconfirmedTxByTxID(tx.ID.String()); err != nil {
264- t.Fatal("dispatch new tx msg error:", err)
265- }
266- w.EventDispatcher.Post(protocol.TxMsgEvent{TxMsg: &protocol.TxPoolMsg{TxDesc: &protocol.TxDesc{Tx: tx}, MsgType: protocol.MsgRemoveTx}})
267- time.Sleep(time.Millisecond * 10)
268- txs, err := w.GetUnconfirmedTxs(testAccount.ID)
269- if err != nil {
270- t.Fatal("get unconfirmed tx error:", err)
271- }
228+ utxos := []*account.UTXO{}
229+ btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
230+ utxos = append(utxos, btmUtxo)
231+ OtherUtxo := mockUTXO(controlProg, &asset)
232+ utxos = append(utxos, OtherUtxo)
272233
273- if len(txs) != 0 {
274- t.Fatal("dispatch remove tx msg error")
275- }
234+ _, txData, err := mockTxData(utxos, testAccount)
235+ if err != nil {
236+ t.Fatal(err)
237+ }
276238
277- w.EventDispatcher.Post(protocol.TxMsgEvent{TxMsg: &protocol.TxPoolMsg{TxDesc: &protocol.TxDesc{Tx: tx}, MsgType: 2}})
239+ tx := types.NewTx(*txData)
240+ txStatus := bc.NewTransactionStatus()
241+ txStatus.SetStatus(0, false)
242+ walletStore := database.NewWalletStore(testDB)
243+ w := newMockWallet(walletStore, accountManager, reg, chain, dispatcher, false)
244+ go w.MemPoolTxQueryLoop()
245+ w.EventDispatcher.Post(protocol.TxMsgEvent{TxMsg: &protocol.TxPoolMsg{TxDesc: &protocol.TxDesc{Tx: tx}, MsgType: protocol.MsgNewTx}})
246+ time.Sleep(time.Millisecond * 10)
247+ if _, err := w.GetUnconfirmedTxByTxID(tx.ID.String()); err != nil {
248+ t.Fatal("dispatch new tx msg error:", err)
278249 }
279- for i := 0; i < 100; i++ {
280- fmt.Printf("case i: %v\n", i)
281- query()
250+ w.EventDispatcher.Post(protocol.TxMsgEvent{TxMsg: &protocol.TxPoolMsg{TxDesc: &protocol.TxDesc{Tx: tx}, MsgType: protocol.MsgRemoveTx}})
251+ time.Sleep(time.Millisecond * 10)
252+ txs, err := w.GetUnconfirmedTxs(testAccount.ID)
253+ if err != nil {
254+ t.Fatal("get unconfirmed tx error:", err)
282255 }
256+
257+ if len(txs) != 0 {
258+ t.Fatal("dispatch remove tx msg error")
259+ }
260+
261+ w.EventDispatcher.Post(protocol.TxMsgEvent{TxMsg: &protocol.TxPoolMsg{TxDesc: &protocol.TxDesc{Tx: tx}, MsgType: 2}})
283262 }
284263
285264 func mockUTXO(controlProg *account.CtrlProgram, assetID *bc.AssetID) *account.UTXO {
Show on old repository browser