2020-11-27 16:16:08 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-06-03 21:15:32 +00:00
|
|
|
|
2021-07-11 23:23:00 +00:00
|
|
|
"github.com/fxamacker/cbor/v2"
|
|
|
|
|
2021-06-03 21:15:32 +00:00
|
|
|
"github.com/Secured-Finance/dione/pubsub"
|
|
|
|
|
2021-04-30 19:55:12 +00:00
|
|
|
types2 "github.com/Secured-Finance/dione/consensus/types"
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2020-11-27 16:16:08 +00:00
|
|
|
)
|
|
|
|
|
2021-07-29 23:04:27 +00:00
|
|
|
func NewMessage(cmsg *types2.ConsensusMessage, privKey crypto.PrivKey) (*pubsub.PubSubMessage, error) {
|
2021-07-11 23:23:00 +00:00
|
|
|
var message pubsub.PubSubMessage
|
2021-07-29 23:04:27 +00:00
|
|
|
switch cmsg.Type {
|
2021-06-08 21:30:23 +00:00
|
|
|
case types2.ConsensusMessageTypePrePrepare:
|
|
|
|
{
|
|
|
|
message.Type = pubsub.PrePrepareMessageType
|
2021-07-11 23:23:00 +00:00
|
|
|
msg := types2.PrePrepareMessage{
|
2021-06-08 21:30:23 +00:00
|
|
|
Block: cmsg.Block,
|
|
|
|
}
|
2021-07-11 23:23:00 +00:00
|
|
|
data, err := cbor.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to convert message to map: %s", err.Error())
|
|
|
|
}
|
|
|
|
message.Payload = data
|
2021-06-08 21:30:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case types2.ConsensusMessageTypePrepare:
|
|
|
|
{
|
|
|
|
message.Type = pubsub.PrepareMessageType
|
|
|
|
signature, err := privKey.Sign(cmsg.Blockhash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create signature: %v", err)
|
|
|
|
}
|
2021-07-11 23:23:00 +00:00
|
|
|
pm := types2.PrepareMessage{
|
2021-07-29 23:04:27 +00:00
|
|
|
Blockhash: cmsg.Blockhash,
|
2021-07-11 23:23:00 +00:00
|
|
|
Signature: signature,
|
|
|
|
}
|
|
|
|
data, err := cbor.Marshal(pm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to convert message to map: %s", err.Error())
|
|
|
|
}
|
|
|
|
message.Payload = data
|
2021-06-08 21:30:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case types2.ConsensusMessageTypeCommit:
|
|
|
|
{
|
|
|
|
message.Type = pubsub.CommitMessageType
|
|
|
|
signature, err := privKey.Sign(cmsg.Blockhash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create signature: %v", err)
|
|
|
|
}
|
2021-07-29 23:04:27 +00:00
|
|
|
pm := types2.CommitMessage{
|
|
|
|
Blockhash: cmsg.Blockhash,
|
|
|
|
Signature: signature,
|
|
|
|
}
|
2021-07-11 23:23:00 +00:00
|
|
|
data, err := cbor.Marshal(pm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to convert message to map: %s", err.Error())
|
|
|
|
}
|
|
|
|
message.Payload = data
|
2021-06-08 21:30:23 +00:00
|
|
|
break
|
|
|
|
}
|
2021-06-03 21:15:32 +00:00
|
|
|
}
|
2021-06-08 21:30:23 +00:00
|
|
|
|
2021-04-30 19:55:12 +00:00
|
|
|
return &message, nil
|
|
|
|
}
|