2023-09-25 02:43:35 +00:00
|
|
|
package ceremony
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2024-03-14 07:18:14 +00:00
|
|
|
|
2023-09-25 02:43:35 +00:00
|
|
|
"github.com/iden3/go-iden3-crypto/poseidon"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/go-libp2p-blossomsub/pb"
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
|
|
|
|
)
|
|
|
|
|
2024-03-14 07:18:14 +00:00
|
|
|
func (e *CeremonyDataClockConsensusEngine) handleMessage(
|
|
|
|
message *pb.Message,
|
|
|
|
) error {
|
|
|
|
e.logger.Debug(
|
|
|
|
"received message",
|
|
|
|
zap.Binary("data", message.Data),
|
|
|
|
zap.Binary("from", message.From),
|
|
|
|
zap.Binary("signature", message.Signature),
|
|
|
|
)
|
2023-09-25 02:43:35 +00:00
|
|
|
|
2024-03-14 07:18:14 +00:00
|
|
|
go func() {
|
|
|
|
e.messageProcessorCh <- message
|
|
|
|
}()
|
2023-09-25 02:43:35 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *CeremonyDataClockConsensusEngine) publishProof(
|
|
|
|
frame *protobufs.ClockFrame,
|
|
|
|
) error {
|
2024-02-13 07:04:56 +00:00
|
|
|
e.logger.Debug(
|
|
|
|
"publishing frame and aggregations",
|
|
|
|
zap.Uint64("frame_number", frame.FrameNumber),
|
|
|
|
)
|
2024-03-15 04:28:03 +00:00
|
|
|
head, err := e.dataTimeReel.Head()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
peers, max, err := e.GetMostAheadPeer()
|
|
|
|
if err != nil || len(peers) == 0 || head.FrameNumber+3 > max {
|
|
|
|
if err := e.publishMessage(e.filter, frame); err != nil {
|
|
|
|
return errors.Wrap(
|
|
|
|
err,
|
|
|
|
"publish proof",
|
|
|
|
)
|
|
|
|
}
|
2023-09-25 02:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *CeremonyDataClockConsensusEngine) publishMessage(
|
|
|
|
filter []byte,
|
|
|
|
message proto.Message,
|
|
|
|
) error {
|
|
|
|
any := &anypb.Any{}
|
|
|
|
if err := any.MarshalFrom(message); err != nil {
|
|
|
|
return errors.Wrap(err, "publish message")
|
|
|
|
}
|
|
|
|
|
|
|
|
any.TypeUrl = strings.Replace(
|
|
|
|
any.TypeUrl,
|
|
|
|
"type.googleapis.com",
|
|
|
|
"types.quilibrium.com",
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
|
|
|
|
payload, err := proto.Marshal(any)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "publish message")
|
|
|
|
}
|
|
|
|
|
|
|
|
h, err := poseidon.HashBytes(payload)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "publish message")
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &protobufs.Message{
|
|
|
|
Hash: h.Bytes(),
|
|
|
|
Address: e.provingKeyAddress,
|
|
|
|
Payload: payload,
|
|
|
|
}
|
|
|
|
data, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "publish message")
|
|
|
|
}
|
|
|
|
return e.pubSub.PublishToBitmask(filter, data)
|
|
|
|
}
|