/
dsmlog.go
102 lines (91 loc) · 2.55 KB
/
dsmlog.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
/*
* Copyright 2023 ICON Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package consensus
import (
"github.com/icon-project/goloop/common"
"github.com/icon-project/goloop/common/cache"
"github.com/icon-project/goloop/module"
)
type dsmCacheKey struct {
Type string // DST*
VoteType VoteType // 0 for proposal
Address common.Address
Height int64
Round int32
}
type dsmLog struct {
c cache.CosterRandom[dsmCacheKey, cache.Coster]
}
func makeDSMLog(cap int) dsmLog {
return dsmLog{
c: cache.MakeCosterRandom[dsmCacheKey, cache.Coster](cap, nil),
}
}
func (c *dsmLog) putVoteMessage(msg *VoteMessage) {
c.c.Put(dsmCacheKey{
module.DSTVote, msg.Type, *msg.address(),
msg.Height, msg.Round,
}, msg)
}
func (c *dsmLog) putProposalMessage(msg *ProposalMessage) {
c.c.Put(dsmCacheKey{
module.DSTProposal, 0, *msg.address(),
msg.Height, msg.Round,
}, msg)
}
func (c *dsmLog) getVoteMessage(vt VoteType, addr common.Address, h int64, r int32) *VoteMessage {
val, _ := c.c.Get(dsmCacheKey{
module.DSTVote, vt, addr, h, r,
})
if val == nil {
return nil
}
return val.(*VoteMessage)
}
func (c *dsmLog) getProposalMessage(addr common.Address, h int64, r int32) *ProposalMessage {
val, _ := c.c.Get(dsmCacheKey{
module.DSTProposal, 0, addr, h, r,
})
if val == nil {
return nil
}
return val.(*ProposalMessage)
}
func (c *dsmLog) LogAndCheckVoteMessage(msg *VoteMessage) []module.DoubleSignData {
omsg := c.getVoteMessage(msg.Type, *msg.address(), msg.Height, msg.Round)
if omsg != nil {
dsv1 := dsVote{omsg}
dsv2 := dsVote{msg}
if dsv1.IsConflictWith(&dsv2) {
return []module.DoubleSignData{&dsv1, &dsv2}
}
}
c.putVoteMessage(msg)
return nil
}
func (c *dsmLog) LogAndCheckProposalMessage(msg *ProposalMessage) []module.DoubleSignData {
omsg := c.getProposalMessage(*msg.address(), msg.Height, msg.Round)
if omsg != nil {
dsv1 := dsProposal{omsg}
dsv2 := dsProposal{msg}
if dsv1.IsConflictWith(&dsv2) {
return []module.DoubleSignData{&dsv1, &dsv2}
}
return nil
}
c.putProposalMessage(msg)
return nil
}