dione/consensus/prepare_pool.go

73 lines
2.0 KiB
Go
Raw Normal View History

2020-11-15 13:46:58 +00:00
package consensus
import (
types2 "github.com/Secured-Finance/dione/consensus/types"
2020-11-15 13:46:58 +00:00
"github.com/Secured-Finance/dione/sigs"
"github.com/Secured-Finance/dione/types"
)
type PreparePool struct {
prepareMsgs map[string][]*types2.Message
2020-11-15 13:46:58 +00:00
privateKey []byte
}
func NewPreparePool() *PreparePool {
return &PreparePool{
prepareMsgs: map[string][]*types2.Message{},
2020-11-15 13:46:58 +00:00
}
}
func (pp *PreparePool) CreatePrepare(prePrepareMsg *types2.Message, privateKey []byte) (*types2.Message, error) {
var message types2.Message
message.Type = types2.MessageTypePrepare
var consensusMsg types2.ConsensusMessage
2020-11-15 13:46:58 +00:00
prepareCMessage := prePrepareMsg.Payload
consensusMsg.ConsensusID = prepareCMessage.ConsensusID
consensusMsg.RequestID = prePrepareMsg.Payload.RequestID
consensusMsg.CallbackAddress = prePrepareMsg.Payload.CallbackAddress
consensusMsg.Data = prepareCMessage.Data
signature, err := sigs.Sign(types.SigTypeEd25519, privateKey, []byte(prepareCMessage.Data))
if err != nil {
return nil, err
}
2020-11-18 18:14:56 +00:00
consensusMsg.Signature = signature.Data
2020-11-15 13:46:58 +00:00
message.Payload = consensusMsg
return &message, nil
}
func (pp *PreparePool) IsExistingPrepare(prepareMsg *types2.Message) bool {
2020-11-15 13:46:58 +00:00
consensusMessage := prepareMsg.Payload
var exists bool
for _, v := range pp.prepareMsgs[consensusMessage.ConsensusID] {
if v.From == prepareMsg.From {
exists = true
}
}
return exists
}
func (pp *PreparePool) IsValidPrepare(prepare *types2.Message) bool {
2020-11-15 13:46:58 +00:00
consensusMsg := prepare.Payload
2020-11-18 18:14:56 +00:00
err := sigs.Verify(&types.Signature{Type: types.SigTypeEd25519, Data: consensusMsg.Signature}, prepare.From, []byte(consensusMsg.Data))
2020-11-15 13:46:58 +00:00
if err != nil {
return false
}
return true
}
func (pp *PreparePool) AddPrepare(prepare *types2.Message) {
2020-11-15 13:46:58 +00:00
consensusID := prepare.Payload.ConsensusID
if _, ok := pp.prepareMsgs[consensusID]; !ok {
pp.prepareMsgs[consensusID] = []*types2.Message{}
2020-11-15 13:46:58 +00:00
}
pp.prepareMsgs[consensusID] = append(pp.prepareMsgs[consensusID], prepare)
}
func (pp *PreparePool) PrepareSize(consensusID string) int {
if v, ok := pp.prepareMsgs[consensusID]; ok {
return len(v)
}
return 0
}