11 dbm "github.com/tendermint/tmlibs/db"
13 "github.com/bytom/blockchain/pseudohsm"
14 "github.com/bytom/consensus"
15 "github.com/bytom/crypto/ed25519/chainkd"
16 "github.com/bytom/database/leveldb"
17 "github.com/bytom/database/storage"
18 "github.com/bytom/protocol"
19 "github.com/bytom/protocol/bc"
20 "github.com/bytom/protocol/state"
23 func TestCancelReservation(t *testing.T) {
24 dirPath, err := ioutil.TempDir(".", "")
28 defer os.RemoveAll(dirPath)
30 testDB := dbm.NewDB("testdb", "leveldb", "temp")
31 defer os.RemoveAll("temp")
33 chain, err := mockChain(testDB)
38 accountManager := NewManager(testDB, chain)
39 hsm, err := pseudohsm.New(dirPath)
44 xpub1, err := hsm.XCreate("test_pub", "password")
49 testAccount, err := accountManager.Create(nil, []chainkd.XPub{xpub1.XPub}, 1, "testAccount")
54 controlProg, err := accountManager.CreateAddress(nil, testAccount.ID, false)
59 utxo := mockUTXO(controlProg)
61 batch := testDB.NewBatch()
65 utxoEntry *storage.UtxoEntry
69 utxoEntry: storage.NewUtxoEntry(true, 0, false),
73 view := state.NewUtxoViewpoint()
74 view.Entries[utxoE.hash] = utxoE.utxoEntry
76 leveldb.SaveUtxoView(batch, view)
79 utxoDB := newReserver(chain, testDB)
81 batch = utxoDB.db.NewBatch()
83 data, err := json.Marshal(utxo)
88 batch.Set(StandardUTXOKey(utxo.OutputID), data)
91 outid := utxo.OutputID
93 ctx := context.Background()
94 res, err := utxoDB.ReserveUTXO(ctx, outid, nil, time.Now())
99 // Verify that the UTXO is reserved.
100 _, err = utxoDB.ReserveUTXO(ctx, outid, nil, time.Now())
101 if err != ErrReserved {
102 t.Fatalf("got=%s want=%s", err, ErrReserved)
105 // Cancel the reservation.
106 err = utxoDB.Cancel(ctx, res.ID)
111 // Reserving again should succeed.
112 _, err = utxoDB.ReserveUTXO(ctx, outid, nil, time.Now())
118 func mockChain(testDB dbm.DB) (*protocol.Chain, error) {
119 store := leveldb.NewStore(testDB)
120 txPool := protocol.NewTxPool()
121 chain, err := protocol.NewChain(store, txPool)
128 func mockUTXO(controlProg *CtrlProgram) *UTXO {
130 utxo.OutputID = bc.Hash{V0: 1}
131 utxo.SourceID = bc.Hash{V0: 2}
132 utxo.AssetID = *consensus.BTMAssetID
133 utxo.Amount = 1000000000
135 utxo.ControlProgram = controlProg.ControlProgram
136 utxo.AccountID = controlProg.AccountID
137 utxo.Address = controlProg.Address
138 utxo.ControlProgramIndex = controlProg.KeyIndex