-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory_store.go
73 lines (62 loc) · 1.62 KB
/
memory_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
package memory
import (
"github.com/cosmos/cosmos-sdk/types"
"github.com/bnb-chain/token-recover-approver/internal/store"
"github.com/bnb-chain/token-recover-approver/pkg/util"
)
var _ store.Store = (*MemoryStore)(nil)
func NewMemoryStore(proofsPath string) (*MemoryStore, error) {
errChan := make(chan error, 1)
defer close(errChan)
// load proofs
stream := util.NewJSONStream(func() any {
return &Proof{}
})
proofs := make(map[string]*Proof)
go func() {
for data := range stream.Watch() {
if data.Error != nil {
errChan <- data.Error
return
}
proof := data.Data.(*Proof)
index := proof.Address.String() + ":" + proof.Coin.Denom
proofs[index] = proof
}
errChan <- nil
}()
stream.Start(proofsPath)
err := <-errChan
if err != nil {
return nil, err
}
return &MemoryStore{
proofs: proofs,
}, nil
}
// MemoryStore implements store.Store.
type MemoryStore struct {
proofs map[string]*Proof // address:index:symbol -> proofs
}
// GetAccountProofs implements store.Store.
func (ss *MemoryStore) GetAccountAssetProof(address types.AccAddress, symbol string) (*store.Proof, error) {
index := address.String() + ":" + symbol
proofs, exist := ss.proofs[index]
if !exist {
return nil, ErrProofNotFound
}
return &store.Proof{
Address: proofs.Address,
Denom: proofs.Coin.Denom,
Amount: proofs.Coin.Amount,
Proof: proofs.Proof,
}, nil
}
// CountAccountAssetProofs implements store.Store.
func (ss *MemoryStore) CountAccountAssetProofs() (count int64, err error) {
return int64(len(ss.proofs)), nil
}
// Close implements store.Store.
func (ss *MemoryStore) Close() error {
return nil
}