From b4aec9846dde14ecc50b24ef82a1703b8db28522 Mon Sep 17 00:00:00 2001 From: Dusan Nosovic <118283942+dusannosovic-ethernal@users.noreply.github.com> Date: Mon, 4 Sep 2023 13:02:20 +0200 Subject: [PATCH] Double signer implementation (testing purposes) (#1864) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * finished code with logs * Update consensus/polybft/transport.go Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> * delete some logs * Update consensus/polybft/transport.go Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> * fix unused imports * fix wrong logger call --------- Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> --- consensus/polybft/polybft.go | 1 + consensus/polybft/transport.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/consensus/polybft/polybft.go b/consensus/polybft/polybft.go index 5a3fa64233..02a75ae66b 100644 --- a/consensus/polybft/polybft.go +++ b/consensus/polybft/polybft.go @@ -442,6 +442,7 @@ func ForkManagerFactory(forks *chain.Forks) error { // Initialize initializes the consensus (e.g. setup data) func (p *Polybft) Initialize() error { p.logger.Info("initializing polybft...") + p.logger.Error("Initializing byzantine node...") // read account account, err := wallet.NewAccountFromSecret(p.config.SecretsManager) diff --git a/consensus/polybft/transport.go b/consensus/polybft/transport.go index 86c8629dff..b7ff9c878e 100644 --- a/consensus/polybft/transport.go +++ b/consensus/polybft/transport.go @@ -1,12 +1,14 @@ package polybft import ( + "crypto/rand" "fmt" ibftProto "github.com/0xPolygon/go-ibft/messages/proto" polybftProto "github.com/0xPolygon/polygon-edge/consensus/polybft/proto" "github.com/0xPolygon/polygon-edge/types" "github.com/libp2p/go-libp2p/core/peer" + "google.golang.org/protobuf/proto" ) // BridgeTransport is an abstraction of network layer for a bridge @@ -71,7 +73,36 @@ func (p *Polybft) createTopics() (err error) { // Multicast is implementation of core.Transport interface func (p *Polybft) Multicast(msg *ibftProto.Message) { + if msg.Type == ibftProto.MessageType_COMMIT { + sender := types.BytesToAddress(msg.From) + localAddr := types.Address(p.key.Address()) + + if sender == localAddr { + tamperedMsg, _ := proto.Clone(msg).(*ibftProto.Message) + + tamperedMsg.GetCommitData().ProposalHash = generateRandomHash() + tamperedMsg.Signature = nil + + tamperedMsg, err := p.key.SignIBFTMessage(tamperedMsg) + + if err != nil { + p.logger.Warn("failed to sign message", "error", err) + } + + if err = p.consensusTopic.Publish(tamperedMsg); err != nil { + p.logger.Warn("failed to multicast second consensus message", "error", err) + } + } + } + if err := p.consensusTopic.Publish(msg); err != nil { p.logger.Warn("failed to multicast consensus message", "error", err) } } + +func generateRandomHash() []byte { + result := make([]byte, types.HashLength) + _, _ = rand.Reader.Read(result) + + return result +}