forked from elastos/Elastos.ELA.SPV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utxosbatch.go
40 lines (32 loc) · 882 Bytes
/
utxosbatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package sqlite
import (
"database/sql"
"sync"
"github.com/elastos/Elastos.ELA.SPV/util"
"github.com/elastos/Elastos.ELA.SPV/wallet/sutil"
)
// Ensure utxos implement UTXOs interface.
var _ UTXOsBatch = (*utxosBatch)(nil)
type utxosBatch struct {
*sync.RWMutex
*sql.Tx
}
// put a utxo to database
func (db *utxosBatch) Put(utxo *sutil.UTXO) error {
db.Lock()
defer db.Unlock()
valueBytes, err := utxo.Value.Bytes()
if err != nil {
return err
}
sql := "INSERT OR REPLACE INTO UTXOs(OutPoint, Value, LockTime, AtHeight, Address) VALUES(?,?,?,?,?)"
_, err = db.Exec(sql, utxo.Op.Bytes(), valueBytes, utxo.LockTime, utxo.AtHeight, utxo.Address.Bytes())
return err
}
// delete a utxo from database
func (db *utxosBatch) Del(op *util.OutPoint) error {
db.Lock()
defer db.Unlock()
_, err := db.Exec("DELETE FROM UTXOs WHERE OutPoint=?", op.Bytes())
return err
}