dione/consensus/pre_prepare_pool.go

67 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"
2020-11-18 18:14:56 +00:00
"github.com/sirupsen/logrus"
2020-11-15 13:46:58 +00:00
)
type PrePreparePool struct {
prePrepareMsgs map[string][]*types2.Message
2020-11-15 13:46:58 +00:00
}
func NewPrePreparePool() *PrePreparePool {
return &PrePreparePool{
prePrepareMsgs: map[string][]*types2.Message{},
2020-11-15 13:46:58 +00:00
}
}
func (pp *PrePreparePool) CreatePrePrepare(consensusID, data string, requestID string, callbackAddress string, privateKey []byte) (*types2.Message, error) {
var message types2.Message
message.Type = types2.MessageTypePrePrepare
var consensusMsg types2.ConsensusMessage
2020-11-15 13:46:58 +00:00
consensusMsg.ConsensusID = consensusID
consensusMsg.RequestID = requestID
consensusMsg.CallbackAddress = callbackAddress
consensusMsg.Data = data
signature, err := sigs.Sign(types.SigTypeEd25519, privateKey, []byte(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 (ppp *PrePreparePool) IsExistingPrePrepare(prepareMsg *types2.Message) bool {
2020-11-15 13:46:58 +00:00
consensusMessage := prepareMsg.Payload
var exists bool
for _, v := range ppp.prePrepareMsgs[consensusMessage.ConsensusID] {
if v.From == prepareMsg.From {
exists = true
}
}
return exists
}
func (ppp *PrePreparePool) IsValidPrePrepare(prePrepare *types2.Message) bool {
2020-11-15 13:46:58 +00:00
// TODO here we need to do validation of tx itself
consensusMsg := prePrepare.Payload
2020-11-18 18:14:56 +00:00
err := sigs.Verify(&types.Signature{Type: types.SigTypeEd25519, Data: consensusMsg.Signature}, prePrepare.From, []byte(consensusMsg.Data))
2020-11-15 13:46:58 +00:00
if err != nil {
2020-11-18 18:14:56 +00:00
logrus.Errorf("unable to verify signature: %v", err)
2020-11-15 13:46:58 +00:00
return false
}
return true
}
func (ppp *PrePreparePool) AddPrePrepare(prePrepare *types2.Message) {
2020-11-15 13:46:58 +00:00
consensusID := prePrepare.Payload.ConsensusID
if _, ok := ppp.prePrepareMsgs[consensusID]; !ok {
ppp.prePrepareMsgs[consensusID] = []*types2.Message{}
2020-11-15 13:46:58 +00:00
}
ppp.prePrepareMsgs[consensusID] = append(ppp.prePrepareMsgs[consensusID], prePrepare)
}