-
Notifications
You must be signed in to change notification settings - Fork 7
/
transaction_repository.go
187 lines (156 loc) · 3.94 KB
/
transaction_repository.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
package dbbadger
import (
"context"
"fmt"
"sync"
"github.com/dgraph-io/badger/v4"
log "github.com/sirupsen/logrus"
"github.com/timshannon/badgerhold/v4"
"github.com/vulpemventures/ocean/internal/core/domain"
)
type transactionRepository struct {
store *badgerhold.Store
chEvents chan domain.TransactionEvent
externalChEvents chan domain.TransactionEvent
lock *sync.Mutex
log func(format string, a ...interface{})
}
func NewTransactionRepository(
store *badgerhold.Store,
) domain.TransactionRepository {
return newTransactionRepository(store)
}
func newTransactionRepository(
store *badgerhold.Store,
) *transactionRepository {
chEvents := make(chan domain.TransactionEvent)
extrernalChEvents := make(chan domain.TransactionEvent)
lock := &sync.Mutex{}
logFn := func(format string, a ...interface{}) {
format = fmt.Sprintf("transaction repository: %s", format)
log.Debugf(format, a...)
}
return &transactionRepository{
store, chEvents, extrernalChEvents, lock, logFn,
}
}
func (r *transactionRepository) AddTransaction(
ctx context.Context, tx *domain.Transaction,
) (bool, error) {
done, err := r.insertTx(ctx, tx)
if done {
go r.publishEvent(domain.TransactionEvent{
EventType: domain.TransactionAdded,
Transaction: tx,
})
}
return done, err
}
func (r *transactionRepository) ConfirmTransaction(
ctx context.Context,
txid, blockHash string, blockheight uint64, blocktime int64,
) (bool, error) {
tx, err := r.getTx(ctx, txid)
if err != nil {
return false, err
}
if tx.IsConfirmed() {
return false, nil
}
tx.Confirm(blockHash, blockheight, blocktime)
if err := r.updateTx(ctx, *tx); err != nil {
return false, err
}
go r.publishEvent(domain.TransactionEvent{
EventType: domain.TransactionConfirmed,
Transaction: tx,
})
return true, nil
}
func (r *transactionRepository) GetTransaction(
ctx context.Context, txid string,
) (*domain.Transaction, error) {
return r.getTx(ctx, txid)
}
func (r *transactionRepository) UpdateTransaction(
ctx context.Context, txid string,
updateFn func(*domain.Transaction) (*domain.Transaction, error),
) error {
tx, err := r.getTx(ctx, txid)
if err != nil {
return err
}
updatedTx, err := updateFn(tx)
if err != nil {
return err
}
return r.updateTx(ctx, *updatedTx)
}
func (r *transactionRepository) GetEventChannel() chan domain.TransactionEvent {
return r.externalChEvents
}
func (r *transactionRepository) insertTx(
ctx context.Context, tx *domain.Transaction,
) (bool, error) {
var err error
if ctx.Value("tx") != nil {
t := ctx.Value("tx").(*badger.Txn)
err = r.store.TxInsert(t, tx.TxID, tx)
} else {
err = r.store.Insert(tx.TxID, tx)
}
if err != nil {
if err == badgerhold.ErrKeyExists {
return false, nil
}
return false, err
}
return true, nil
}
func (r *transactionRepository) getTx(
ctx context.Context, txid string,
) (*domain.Transaction, error) {
var err error
var tx domain.Transaction
if ctx.Value("tx") != nil {
t := ctx.Value("tx").(*badger.Txn)
err = r.store.TxGet(t, txid, &tx)
} else {
err = r.store.Get(txid, &tx)
}
if err != nil {
if err == badgerhold.ErrNotFound {
return nil, fmt.Errorf("transaction not found")
}
return nil, err
}
return &tx, nil
}
func (r *transactionRepository) updateTx(
ctx context.Context, tx domain.Transaction,
) error {
if ctx.Value("tx") != nil {
t := ctx.Value("tx").(*badger.Txn)
return r.store.TxUpdate(t, tx.TxID, tx)
}
return r.store.Update(tx.TxID, tx)
}
func (r *transactionRepository) publishEvent(event domain.TransactionEvent) {
r.lock.Lock()
defer r.lock.Unlock()
r.log("publish event %s", event.EventType)
r.chEvents <- event
// send over channel without blocking in case nobody is listening.
select {
case r.externalChEvents <- event:
default:
}
}
func (r *transactionRepository) reset() {
r.store.Badger().DropAll()
}
func (r *transactionRepository) close() {
r.store.Close()
close(r.chEvents)
close(r.externalChEvents)
}