-
Notifications
You must be signed in to change notification settings - Fork 5
/
transaction.go
189 lines (161 loc) · 5.73 KB
/
transaction.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package store
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/MixinNetwork/safe/apps/bitcoin"
"github.com/MixinNetwork/safe/common"
)
type Transaction struct {
TransactionHash string
RawTransaction string
Holder string
Chain byte
State int
Data string
RequestId string
CreatedAt time.Time
UpdatedAt time.Time
}
var transactionCols = []string{"transaction_hash", "raw_transaction", "holder", "chain", "state", "data", "request_id", "created_at", "updated_at"}
func (s *SQLite3Store) ReadTransactionByRequestId(ctx context.Context, requestId string) (*Transaction, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
var hash string
query := "SELECT transaction_hash FROM transactions WHERE request_id=?"
row := tx.QueryRowContext(ctx, query, requestId)
err = row.Scan(&hash)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, err
}
return s.readTransaction(ctx, tx, hash)
}
func (s *SQLite3Store) ReadTransaction(ctx context.Context, hash string) (*Transaction, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
return s.readTransaction(ctx, tx, hash)
}
func (s *SQLite3Store) CountUnfinishedTransactionsByHolder(ctx context.Context, holder string) (int, error) {
query := "SELECT COUNT(*) FROM transactions WHERE holder=? AND state=? ORDER BY created_at ASC"
row := s.db.QueryRowContext(ctx, query, holder, common.RequestStateInitial)
var count int
err := row.Scan(&count)
if err == sql.ErrNoRows {
return 0, nil
}
return count, err
}
func (s *SQLite3Store) CloseAccountByTransactionWithRequest(ctx context.Context, trx *Transaction, utxos []*bitcoin.Input) error {
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
err = s.execOne(ctx, tx, "UPDATE safes SET state=?, updated_at=? WHERE holder=? AND state=?",
common.RequestStateFailed, trx.CreatedAt, trx.Holder, common.RequestStateDone)
if err != nil {
return fmt.Errorf("UPDATE safes %v", err)
}
err = s.writeTransactionWithRequest(ctx, tx, trx, utxos)
if err != nil {
return err
}
return tx.Commit()
}
func (s *SQLite3Store) WriteTransactionWithRequest(ctx context.Context, trx *Transaction, utxos []*bitcoin.Input) error {
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
err = s.writeTransactionWithRequest(ctx, tx, trx, utxos)
if err != nil {
return err
}
return tx.Commit()
}
func (s *SQLite3Store) writeTransactionWithRequest(ctx context.Context, tx *sql.Tx, trx *Transaction, utxos []*bitcoin.Input) error {
vals := []any{trx.TransactionHash, trx.RawTransaction, trx.Holder, trx.Chain, trx.State, trx.Data, trx.RequestId, trx.CreatedAt, trx.UpdatedAt}
err := s.execOne(ctx, tx, buildInsertionSQL("transactions", transactionCols), vals...)
if err != nil {
return fmt.Errorf("INSERT transactions %v", err)
}
err = s.execOne(ctx, tx, "UPDATE requests SET state=?, updated_at=? WHERE request_id=?",
common.RequestStateDone, time.Now().UTC(), trx.RequestId)
if err != nil {
return fmt.Errorf("UPDATE requests %v", err)
}
for _, utxo := range utxos {
err = s.execOne(ctx, tx, "UPDATE bitcoin_outputs SET state=?, spent_by=?, updated_at=? WHERE transaction_hash=? AND output_index=?",
common.RequestStatePending, trx.TransactionHash, trx.UpdatedAt, utxo.TransactionHash, utxo.Index)
if err != nil {
return fmt.Errorf("UPDATE bitcoin_outputs %v", err)
}
}
return nil
}
func (s *SQLite3Store) RevokeTransactionWithRequest(ctx context.Context, trx *Transaction, safe *Safe, req *common.Request) error {
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
psbt, _ := bitcoin.UnmarshalPartiallySignedTransaction(common.DecodeHexOrPanic(trx.RawTransaction))
for _, in := range psbt.UnsignedTx.TxIn {
pop := in.PreviousOutPoint
err = s.execOne(ctx, tx, "UPDATE bitcoin_outputs SET state=?, spent_by=?, updated_at=? WHERE transaction_hash=? AND output_index=? AND spent_by=?",
common.RequestStateInitial, nil, req.CreatedAt, pop.Hash.String(), pop.Index, trx.TransactionHash)
if err != nil {
return fmt.Errorf("UPDATE bitcoin_outputs %v", err)
}
row := tx.QueryRowContext(ctx, "SELECT address,satoshi FROM bitcoin_outputs WHERE transaction_hash=? AND output_index=?",
pop.Hash.String(), pop.Index)
var u bitcoin.Input
err = row.Scan(&u.TransactionHash, &u.Satoshi)
if err != nil {
return err
}
switch u.TransactionHash {
case safe.Address:
default:
panic(trx.TransactionHash)
}
}
err = s.execOne(ctx, tx, "UPDATE transactions SET state=?, updated_at=? WHERE transaction_hash=? AND state=?",
common.RequestStateFailed, req.CreatedAt, trx.TransactionHash, common.RequestStateInitial)
if err != nil {
return fmt.Errorf("UPDATE transactions %v", err)
}
err = s.execOne(ctx, tx, "UPDATE requests SET state=?, updated_at=? WHERE request_id=?",
common.RequestStateDone, time.Now().UTC(), req.Id)
if err != nil {
return fmt.Errorf("UPDATE requests %v", err)
}
return tx.Commit()
}
func (s *SQLite3Store) readTransaction(ctx context.Context, tx *sql.Tx, transactionHash string) (*Transaction, error) {
query := fmt.Sprintf("SELECT %s FROM transactions WHERE transaction_hash=?", strings.Join(transactionCols, ","))
row := tx.QueryRowContext(ctx, query, transactionHash)
var trx Transaction
err := row.Scan(&trx.TransactionHash, &trx.RawTransaction, &trx.Holder, &trx.Chain, &trx.State, &trx.Data, &trx.RequestId, &trx.CreatedAt, &trx.UpdatedAt)
if err == sql.ErrNoRows {
return nil, nil
}
return &trx, err
}