2021-04-30 19:55:12 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2021-06-08 21:30:23 +00:00
|
|
|
"bytes"
|
|
|
|
|
2021-04-30 19:55:12 +00:00
|
|
|
types2 "github.com/Secured-Finance/dione/consensus/types"
|
|
|
|
mapset "github.com/Secured-Finance/golang-set"
|
|
|
|
)
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
type ConsensusMessageLog struct {
|
|
|
|
messages mapset.Set
|
|
|
|
maxLogSize int
|
2021-04-30 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func NewConsensusMessageLog() *ConsensusMessageLog {
|
|
|
|
msgLog := &ConsensusMessageLog{
|
2021-04-30 19:55:12 +00:00
|
|
|
messages: mapset.NewSet(),
|
|
|
|
maxLogSize: 0, // TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgLog
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func (ml *ConsensusMessageLog) AddMessage(msg types2.ConsensusMessage) {
|
2021-04-30 19:55:12 +00:00
|
|
|
ml.messages.Add(msg)
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func (ml *ConsensusMessageLog) Exists(msg types2.ConsensusMessage) bool {
|
2021-04-30 19:55:12 +00:00
|
|
|
return ml.messages.Contains(msg)
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func (ml *ConsensusMessageLog) Get(typ types2.ConsensusMessageType, blockhash []byte) []*types2.ConsensusMessage {
|
2021-06-02 21:19:52 +00:00
|
|
|
var result []*types2.ConsensusMessage
|
2021-04-30 19:55:12 +00:00
|
|
|
|
|
|
|
for v := range ml.messages.Iter() {
|
2021-06-02 21:19:52 +00:00
|
|
|
msg := v.(types2.ConsensusMessage)
|
2021-06-08 21:30:23 +00:00
|
|
|
if msg.Block != nil {
|
|
|
|
|
|
|
|
}
|
|
|
|
if msg.Type == typ {
|
|
|
|
var msgBlockHash []byte
|
|
|
|
if msg.Block != nil {
|
|
|
|
msgBlockHash = msg.Block.Header.Hash
|
|
|
|
} else {
|
|
|
|
msgBlockHash = msg.Blockhash
|
|
|
|
}
|
|
|
|
if bytes.Compare(msgBlockHash, blockhash) == 0 {
|
|
|
|
result = append(result, &msg)
|
|
|
|
}
|
2021-04-30 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|