-
Notifications
You must be signed in to change notification settings - Fork 9
/
sequence.go
156 lines (135 loc) · 4.37 KB
/
sequence.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
package commitment
import (
"errors"
"time"
commitmenttypes "github.com/cosmos/ibc-go/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
"github.com/cosmos/ibc-go/modules/core/exported"
proto "github.com/gogo/protobuf/proto"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/hyperledger/fabric-chaincode-go/shim"
tmtime "github.com/tendermint/tendermint/types/time"
)
func NewSequence(value uint64, tm int64) Sequence {
return Sequence{Value: value, Timestamp: tm}
}
func (seq *Sequence) Bytes() []byte {
bz, err := proto.Marshal(seq)
if err != nil {
panic(err)
}
return bz
}
type SequenceManager interface {
InitSequence(stub shim.ChaincodeStubInterface) (*Sequence, error)
GetCurrentSequence(stub shim.ChaincodeStubInterface) (*Sequence, error)
GetSequence(stub shim.ChaincodeStubInterface, seq uint64) (*Sequence, error)
UpdateSequence(stub shim.ChaincodeStubInterface) (*Sequence, error)
ValidateTimestamp(now time.Time, prevSec int64, next *timestamp.Timestamp) error
SetClock(clock func() time.Time)
}
type sequenceManager struct {
config CommitmentConfig
prefix exported.Prefix
clock func() time.Time
}
func NewSequenceManager(config CommitmentConfig, prefix exported.Prefix) SequenceManager {
return &sequenceManager{config: config, prefix: prefix, clock: tmtime.Now}
}
func NewDefaultSequenceManager() SequenceManager {
return NewSequenceManager(DefaultConfig(), commitmenttypes.NewMerklePrefix([]byte(host.StoreKey)))
}
func (m sequenceManager) InitSequence(stub shim.ChaincodeStubInterface) (*Sequence, error) {
tm, err := stub.GetTxTimestamp()
if err != nil {
return nil, err
}
if err := m.ValidateTimestamp(m.clock(), 0, tm); err != nil {
return nil, err
}
seq := NewSequence(1, tm.GetSeconds())
if err = m.updateSequence(stub, seq); err != nil {
return nil, err
}
return &seq, nil
}
func (m sequenceManager) GetCurrentSequence(stub shim.ChaincodeStubInterface) (*Sequence, error) {
return m.getCurrentSequence(stub)
}
func (m sequenceManager) GetSequence(stub shim.ChaincodeStubInterface, seq uint64) (*Sequence, error) {
return m.getSequence(stub, seq)
}
func (m sequenceManager) UpdateSequence(stub shim.ChaincodeStubInterface) (*Sequence, error) {
current, err := m.getCurrentSequence(stub)
if err != nil {
return nil, err
}
tm, err := stub.GetTxTimestamp()
if err != nil {
return nil, err
}
if err := m.ValidateTimestamp(m.clock(), current.Timestamp, tm); err != nil {
return nil, err
}
next := NewSequence(current.Value+1, tm.GetSeconds())
if err := m.updateSequence(stub, next); err != nil {
return nil, err
}
return &next, nil
}
func (m sequenceManager) ValidateTimestamp(now time.Time, prevSec int64, next *timestamp.Timestamp) error {
if now.Unix()+int64(m.config.MaxTimestampDiff/time.Second) < next.GetSeconds() {
return errors.New("the next timestamp indicates future time")
}
if now.Unix()-int64(m.config.MaxTimestampDiff/time.Second) > next.GetSeconds() {
return errors.New("the next timestamp indicates past time")
}
if prevSec > 0 && prevSec+int64(m.config.MinTimeInterval/time.Second) > next.GetSeconds() {
return errors.New("the next timestamp is less than the minimum time for advancing the sequence")
}
return nil
}
func (m *sequenceManager) SetClock(clock func() time.Time) {
m.clock = clock
}
func (m sequenceManager) getCurrentSequence(stub shim.ChaincodeStubInterface) (*Sequence, error) {
bz, err := stub.GetState(MakeCurrentSequenceKey(m.prefix))
if err != nil {
return nil, err
} else if bz == nil {
return nil, errors.New("key not found")
}
var seq Sequence
if err := proto.Unmarshal(bz, &seq); err != nil {
return nil, err
}
return &seq, nil
}
func (m sequenceManager) getSequence(stub shim.ChaincodeStubInterface, value uint64) (*Sequence, error) {
bz, err := stub.GetState(MakeSequenceKey(m.prefix, value))
if err != nil {
return nil, err
} else if bz == nil {
return nil, errors.New("key not found")
}
var seq Sequence
if err := proto.Unmarshal(bz, &seq); err != nil {
return nil, err
}
return &seq, nil
}
func (m sequenceManager) updateSequence(stub shim.ChaincodeStubInterface, nextSeq Sequence) error {
if err := stub.PutState(
MakeCurrentSequenceKey(m.prefix),
nextSeq.Bytes(),
); err != nil {
return err
}
if err := stub.PutState(
MakeSequenceKey(m.prefix, nextSeq.Value),
nextSeq.Bytes(),
); err != nil {
return err
}
return nil
}