forked from opengsn/gsn
/
leveldb_store.go
173 lines (141 loc) · 3.89 KB
/
leveldb_store.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
package txstore
import (
"encoding/binary"
"fmt"
"sync"
"code.cloudfoundry.org/clock"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/syndtr/goleveldb/leveldb"
)
type LevelDbTxStore struct {
*leveldb.DB
clock clock.Clock
mutex *sync.Mutex
}
func (tx *TimestampedTransaction) Encode() ([]byte, error) {
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, uint64(tx.Timestamp))
txBytes, err := rlp.EncodeToBytes(tx.Transaction)
if err != nil {
return nil, err
}
bytes = append(bytes, txBytes...)
return bytes, nil
}
func DecodeTimestampedTransaction(bytes []byte) (*TimestampedTransaction, error) {
var tx types.Transaction
err := rlp.DecodeBytes(bytes[8:], &tx)
if err != nil {
return nil, err
}
timestamp := int64(binary.BigEndian.Uint64(bytes[:8]))
timedtx := TimestampedTransaction{&tx, timestamp}
return &timedtx, nil
}
func NewLevelDbTxStore(file string, clk clock.Clock) (store *LevelDbTxStore, err error) {
if clk == nil {
clk = clock.NewClock()
}
db, err := leveldb.OpenFile(file, nil)
if err != nil {
return nil, err
}
return &LevelDbTxStore{db, clk, &sync.Mutex{}}, nil
}
// ListTransactions returns all transactions on the store, useful for testing
func (store *LevelDbTxStore) ListTransactions() (txs []*TimestampedTransaction, err error) {
store.mutex.Lock()
defer store.mutex.Unlock()
txs = make([]*TimestampedTransaction, 0, 20)
iter := store.NewIterator(nil, nil)
defer iter.Release()
for iter.Next() {
value := iter.Value()
tx, err := DecodeTimestampedTransaction(value)
if err != nil {
return nil, err
}
txs = append(txs, tx)
}
return
}
// GetFirstTransaction returns transaction with lowest nonce
func (store *LevelDbTxStore) GetFirstTransaction() (tx *TimestampedTransaction, err error) {
iter := store.NewIterator(nil, nil)
defer iter.Release()
if iter.Next() {
value := iter.Value()
tx, err := DecodeTimestampedTransaction(value)
if err != nil {
return nil, err
}
return tx, nil
}
return nil, nil
}
// SaveTransaction dates and stores transaction sorted by ascending nonce
func (store *LevelDbTxStore) SaveTransaction(tx *types.Transaction) (err error) {
store.mutex.Lock()
defer store.mutex.Unlock()
timedtx := &TimestampedTransaction{tx, store.clock.Now().Unix()}
txbytes, err := timedtx.Encode()
if err != nil {
return err
}
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, tx.Nonce())
err = store.Put(key, txbytes, nil)
if err != nil {
return err
}
return
}
// UpdateTransactionByNonce updates a transaction given its nonce, returns error if tx with same nonce does not exist
func (store *LevelDbTxStore) UpdateTransactionByNonce(tx *types.Transaction) (err error) {
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, tx.Nonce())
_, err = store.Get(key, nil)
if err == leveldb.ErrNotFound {
return fmt.Errorf("Could not find transaction with nonce %d", tx.Nonce())
} else if err != nil {
return err
}
return store.SaveTransaction(tx)
}
// RemoveTransactionsLessThanNonce removes all transactions with nonce values up to the specified value inclusive
func (store *LevelDbTxStore) RemoveTransactionsLessThanNonce(nonce uint64) (err error) {
store.mutex.Lock()
defer store.mutex.Unlock()
batch := new(leveldb.Batch)
iter := store.NewIterator(nil, nil)
for iter.Next() {
key := iter.Key()
value := iter.Value()
tx, err := DecodeTimestampedTransaction(value)
if err != nil {
iter.Release()
return err
}
if tx.Nonce() < nonce {
batch.Delete(key)
} else {
break
}
}
iter.Release()
return store.Write(batch, nil)
}
// Clear removes all transactions stored
func (store *LevelDbTxStore) Clear() (err error) {
store.mutex.Lock()
defer store.mutex.Unlock()
batch := new(leveldb.Batch)
iter := store.NewIterator(nil, nil)
for iter.Next() {
key := iter.Key()
batch.Delete(key)
}
iter.Release()
return store.Write(batch, nil)
}