-
Notifications
You must be signed in to change notification settings - Fork 2
/
memstorage.go
162 lines (148 loc) · 3.96 KB
/
memstorage.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
package ethtxmanager
import (
"context"
"encoding/json"
"os"
"sync"
"time"
"github.com/0xPolygonHermez/zkevm-ethtx-manager/log"
"github.com/ethereum/go-ethereum/common"
)
// MemStorage hold txs to be managed
type MemStorage struct {
TxsMutex sync.RWMutex
FileMutex sync.RWMutex
Transactions map[common.Hash]monitoredTx
PersistenceFilename string
}
// NewMemStorage creates a new instance of storage
func NewMemStorage(persistenceFilename string) *MemStorage {
transactions := make(map[common.Hash]monitoredTx)
if persistenceFilename != "" {
// Check if the file exists
if _, err := os.Stat(persistenceFilename); os.IsNotExist(err) {
log.Infof("Persistence file %s does not exist", persistenceFilename)
} else {
ReadFile, err := os.ReadFile(persistenceFilename)
if err != nil {
log.Error(err)
os.Exit(1)
}
err = json.Unmarshal(ReadFile, &transactions)
if err != nil {
log.Error(err)
os.Exit(1)
}
log.Infof("Persistence file %s loaded", persistenceFilename)
}
}
return &MemStorage{TxsMutex: sync.RWMutex{},
Transactions: transactions,
PersistenceFilename: persistenceFilename,
}
}
// Persist the memory storage
func (s *MemStorage) persist() {
if s.PersistenceFilename != "" {
s.TxsMutex.RLock()
defer s.TxsMutex.RUnlock()
s.FileMutex.Lock()
defer s.FileMutex.Unlock()
jsonFile, _ := json.Marshal(s.Transactions)
err := os.WriteFile(s.PersistenceFilename+".tmp", jsonFile, 0644) // nolint:gosec, gomnd
if err != nil {
log.Error(err)
}
err = os.Rename(s.PersistenceFilename+".tmp", s.PersistenceFilename)
if err != nil {
log.Error(err)
}
}
}
// Add persist a monitored tx
func (s *MemStorage) Add(ctx context.Context, mTx monitoredTx) error {
mTx.CreatedAt = time.Now()
s.TxsMutex.Lock()
if _, exists := s.Transactions[mTx.ID]; exists {
return ErrAlreadyExists
}
s.Transactions[mTx.ID] = mTx
s.TxsMutex.Unlock()
s.persist()
return nil
}
// Remove a persisted monitored tx
func (s *MemStorage) Remove(ctx context.Context, id common.Hash) error {
s.TxsMutex.Lock()
if _, exists := s.Transactions[id]; !exists {
return ErrNotFound
}
delete(s.Transactions, id)
s.TxsMutex.Unlock()
s.persist()
return nil
}
// Get loads a persisted monitored tx
func (s *MemStorage) Get(ctx context.Context, id common.Hash) (monitoredTx, error) {
s.TxsMutex.RLock()
defer s.TxsMutex.RUnlock()
if mTx, exists := s.Transactions[id]; exists {
return mTx, nil
}
return monitoredTx{}, ErrNotFound
}
// GetByStatus loads all monitored tx that match the provided status
func (s *MemStorage) GetByStatus(ctx context.Context, statuses []MonitoredTxStatus) ([]monitoredTx, error) {
mTxs := []monitoredTx{}
s.TxsMutex.RLock()
defer s.TxsMutex.RUnlock()
for _, mTx := range s.Transactions {
if len(statuses) > 0 {
for _, status := range statuses {
if mTx.Status == status {
mTxs = append(mTxs, mTx)
}
}
} else {
mTxs = append(mTxs, mTx)
}
}
return mTxs, nil
}
// GetByBlock loads all monitored tx that have the blockNumber between
// fromBlock and toBlock
func (s *MemStorage) GetByBlock(ctx context.Context, fromBlock, toBlock *uint64) ([]monitoredTx, error) {
mTxs := []monitoredTx{}
s.TxsMutex.RLock()
defer s.TxsMutex.RUnlock()
for _, mTx := range s.Transactions {
if fromBlock != nil && mTx.BlockNumber.Uint64() < *fromBlock {
continue
}
if toBlock != nil && mTx.BlockNumber.Uint64() > *toBlock {
continue
}
mTxs = append(mTxs, mTx)
}
return mTxs, nil
}
// Update a persisted monitored tx
func (s *MemStorage) Update(ctx context.Context, mTx monitoredTx) error {
mTx.UpdatedAt = time.Now()
s.TxsMutex.Lock()
if _, exists := s.Transactions[mTx.ID]; !exists {
return ErrNotFound
}
s.Transactions[mTx.ID] = mTx
s.TxsMutex.Unlock()
s.persist()
return nil
}
// Empty the storage
func (s *MemStorage) Empty(ctx context.Context) error {
s.TxsMutex.Lock()
s.Transactions = make(map[common.Hash]monitoredTx)
s.TxsMutex.Unlock()
s.persist()
return nil
}