2020-11-15 13:46:58 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2020-12-07 16:01:10 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
types2 "github.com/Secured-Finance/dione/consensus/types"
|
2020-11-15 13:46:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PreparePool struct {
|
2020-12-07 16:01:10 +00:00
|
|
|
mut sync.RWMutex
|
2020-11-18 19:53:52 +00:00
|
|
|
prepareMsgs map[string][]*types2.Message
|
2020-11-15 13:46:58 +00:00
|
|
|
privateKey []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPreparePool() *PreparePool {
|
|
|
|
return &PreparePool{
|
2020-11-18 19:53:52 +00:00
|
|
|
prepareMsgs: map[string][]*types2.Message{},
|
2020-11-15 13:46:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (pp *PreparePool) CreatePrepare(prePrepareMsg *types2.Message, privateKey []byte) (*types2.Message, error) {
|
|
|
|
var message types2.Message
|
|
|
|
message.Type = types2.MessageTypePrepare
|
2020-11-27 16:16:08 +00:00
|
|
|
newCMsg := prePrepareMsg.Payload
|
|
|
|
message.Payload = newCMsg
|
2020-11-15 13:46:58 +00:00
|
|
|
return &message, nil
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (pp *PreparePool) IsExistingPrepare(prepareMsg *types2.Message) bool {
|
2020-12-07 16:01:10 +00:00
|
|
|
pp.mut.RLock()
|
|
|
|
defer pp.mut.RUnlock()
|
|
|
|
|
2020-11-15 13:46:58 +00:00
|
|
|
consensusMessage := prepareMsg.Payload
|
|
|
|
var exists bool
|
2021-03-15 20:39:52 +00:00
|
|
|
for _, v := range pp.prepareMsgs[consensusMessage.Task.ConsensusID] {
|
2020-11-15 13:46:58 +00:00
|
|
|
if v.From == prepareMsg.From {
|
|
|
|
exists = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (pp *PreparePool) IsValidPrepare(prepare *types2.Message) bool {
|
2020-11-15 13:46:58 +00:00
|
|
|
consensusMsg := prepare.Payload
|
2020-11-27 16:16:08 +00:00
|
|
|
err := verifyTaskSignature(consensusMsg)
|
2020-11-15 13:46:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (pp *PreparePool) AddPrepare(prepare *types2.Message) {
|
2020-12-07 16:01:10 +00:00
|
|
|
pp.mut.Lock()
|
|
|
|
defer pp.mut.Unlock()
|
|
|
|
|
2021-03-15 20:39:52 +00:00
|
|
|
consensusID := prepare.Payload.Task.ConsensusID
|
2020-11-15 13:46:58 +00:00
|
|
|
if _, ok := pp.prepareMsgs[consensusID]; !ok {
|
2020-11-18 19:53:52 +00:00
|
|
|
pp.prepareMsgs[consensusID] = []*types2.Message{}
|
2020-11-15 13:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pp.prepareMsgs[consensusID] = append(pp.prepareMsgs[consensusID], prepare)
|
|
|
|
}
|
|
|
|
|
2020-12-07 16:01:10 +00:00
|
|
|
func (pp *PreparePool) PreparePoolSize(consensusID string) int {
|
|
|
|
pp.mut.RLock()
|
|
|
|
defer pp.mut.RUnlock()
|
|
|
|
|
2020-11-15 13:46:58 +00:00
|
|
|
if v, ok := pp.prepareMsgs[consensusID]; ok {
|
|
|
|
return len(v)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|