-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.go
55 lines (42 loc) · 1 KB
/
mapper.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
package transaction
import (
"github.com/ci123chain/ci123chain/pkg/abci/types"
"encoding/binary"
)
var (
txIndexKey = []byte("k")
)
type TxIndexMapper interface {
Get(types.Context) uint32
Incr(types.Context) uint32
}
type txIndexMapper struct {
storeKey types.StoreKey
}
func NewTxIndexMapper(k types.StoreKey) TxIndexMapper {
return &txIndexMapper{storeKey: k}
}
func (m *txIndexMapper) Get(ctx types.Context) uint32 {
return m.get(m.getStore(ctx))
}
func (m *txIndexMapper) get(kvs types.KVStore) uint32 {
height := kvs.Get(txIndexKey)
if height == nil {
return 0
}
return binary.BigEndian.Uint32(height)
}
func (m *txIndexMapper) set(kvs types.KVStore, height uint32) {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, height)
kvs.Set(txIndexKey, b)
}
func (m *txIndexMapper) Incr(ctx types.Context) uint32 {
s := m.getStore(ctx)
height := m.get(s) + 1
m.set(s, height)
return height
}
func (m *txIndexMapper) getStore(ctx types.Context) types.KVStore {
return ctx.KVStore(m.storeKey)
}