forked from elastos/Elastos.ELA.SPV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stxosbatch.go
46 lines (37 loc) · 1006 Bytes
/
stxosbatch.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
41
42
43
44
45
46
package sqlite
import (
"database/sql"
"sync"
"github.com/elastos/Elastos.ELA.SPV/util"
"github.com/elastos/Elastos.ELA.SPV/wallet/sutil"
)
// Ensure stxos implement STXOs interface.
var _ STXOsBatch = (*stxosBatch)(nil)
type stxosBatch struct {
*sync.RWMutex
*sql.Tx
}
// Put save a UTXO into database
func (s *stxosBatch) Put(stxo *sutil.STXO) error {
s.Lock()
defer s.Unlock()
valueBytes, err := stxo.Value.Bytes()
if err != nil {
return err
}
sql := `INSERT OR REPLACE INTO STXOs(OutPoint, Value, LockTime, AtHeight, SpendHash, SpendHeight, Address)
VALUES(?,?,?,?,?,?,?)`
_, err = s.Exec(sql, stxo.Op.Bytes(), valueBytes, stxo.LockTime, stxo.AtHeight,
stxo.SpendTxId.Bytes(), stxo.SpendHeight, stxo.Address.Bytes())
return err
}
// delete a stxo from database
func (sb *stxosBatch) Del(outPoint *util.OutPoint) error {
sb.Lock()
defer sb.Unlock()
_, err := sb.Exec("DELETE FROM STXOs WHERE OutPoint=?", outPoint.Bytes())
if err != nil {
return err
}
return nil
}