/
sequences_cache.go
65 lines (55 loc) · 1.34 KB
/
sequences_cache.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
package storage
import (
"fmt"
"github.com/alephium/wormhole-fork/node/pkg/vaa"
)
type emitterId struct {
emitterChain vaa.ChainID
emitterAddress vaa.Address
targetChain vaa.ChainID
isGovernanceEmitter bool
// cached value
idStr *string
}
func (e *emitterId) toString() string {
if e.idStr != nil {
return *e.idStr
}
var str string
if e.isGovernanceEmitter {
str = fmt.Sprintf("%d/%s/%d", e.emitterChain, e.emitterAddress.String(), vaa.ChainIDUnset)
} else {
str = fmt.Sprintf("%d/%s/%d", e.emitterChain, e.emitterAddress.String(), e.targetChain)
}
e.idStr = &str
return str
}
func (e *emitterId) toVaaId(seq uint64) string {
return fmt.Sprintf("%s/%d", e.toString(), seq)
}
type sequencesCache struct {
governanceSequence *uint64
sequences map[emitterId]uint64
}
func newSequencesCache() *sequencesCache {
return &sequencesCache{
governanceSequence: nil,
sequences: make(map[emitterId]uint64),
}
}
func (s *sequencesCache) getNextSequence(emitterId *emitterId) *uint64 {
if emitterId.isGovernanceEmitter {
return s.governanceSequence
}
seq, ok := s.sequences[*emitterId]
if !ok {
return nil
}
return &seq
}
func (s *sequencesCache) setNextSequence(emitterId *emitterId, seq uint64) {
if emitterId.isGovernanceEmitter {
s.governanceSequence = &seq
}
s.sequences[*emitterId] = seq
}