From 749a5bfb77c18291c9aa2ab3a4779bd2be7a394c Mon Sep 17 00:00:00 2001 From: Dusan Date: Thu, 31 Aug 2023 10:06:24 +0200 Subject: [PATCH 1/6] finished code with logs --- consensus/polybft/polybft.go | 1 + consensus/polybft/transport.go | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/consensus/polybft/polybft.go b/consensus/polybft/polybft.go index 68c47199cc..97b776ae7f 100644 --- a/consensus/polybft/polybft.go +++ b/consensus/polybft/polybft.go @@ -417,6 +417,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 60e41ac15e..e9c6dbc2bd 100644 --- a/consensus/polybft/transport.go +++ b/consensus/polybft/transport.go @@ -1,12 +1,16 @@ 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/crypto" "github.com/0xPolygon/polygon-edge/types" "github.com/libp2p/go-libp2p/core/peer" + "google.golang.org/protobuf/proto" + protobuf "google.golang.org/protobuf/proto" ) // BridgeTransport is an abstraction of network layer for a bridge @@ -59,7 +63,71 @@ func (p *Polybft) createTopics() (err error) { // Multicast is implementation of core.Transport interface func (p *Polybft) Multicast(msg *ibftProto.Message) { + p.logger.Error(fmt.Sprintf("[MulticastPublish]")) + + if msg.Type == ibftProto.MessageType_COMMIT { + sender := types.BytesToAddress(msg.From) + localAddr := types.Address(p.key.Address()) + p.logger.Debug("[Multicast]", "msg sender", sender.String(), "local node address", localAddr.String()) + + if sender == localAddr { + tamperedMsg := proto.Clone(msg).(*ibftProto.Message) + tamperedMsg.GetCommitData().ProposalHash = generateRandomHash() + tamperedMsg.Signature = nil + + tamperedMsgRaw, _ := protobuf.Marshal(tamperedMsg) + + p.logger.Error("MsgRaw", "before", tamperedMsgRaw, "cryptokeccakBefore:", crypto.Keccak256(tamperedMsgRaw)) + + tamperedMsg, err := p.key.SignIBFTMessage(tamperedMsg) + + if err != nil { + p.logger.Error("Error while signing", "error", err) + } + + p.logger.Error(fmt.Sprintf("[Polybft]Multicast(publish1): %+v", tamperedMsg)) + msgNoSigTampered, err := tamperedMsg.PayloadNoSig() + + if err != nil { + p.logger.Error("NoPayload") + } + + pub, _ := crypto.RecoverPubkey(tamperedMsg.Signature, crypto.Keccak256(msgNoSigTampered)) + + p.logger.Error("msgRawAfter", "after", msgNoSigTampered, "CryptoKeccakAfter", crypto.Keccak256(msgNoSigTampered)) + p.logger.Error("PubKey", "after", pub) + signerAddress, err := RecoverAddressFromSignature(tamperedMsg.Signature, msgNoSigTampered) + + if err != nil { + p.logger.Error("failed to recover address from signature: %w", err) + } + + p.logger.Error("Signer", "Address", signerAddress.String()) + + err = p.consensusTopic.Publish(tamperedMsg) + if err != nil { + p.logger.Error("Error while sending byzantian message", "error", err) + } + } + } + + p.logger.Error("[Polybft]Multicast(publish2): %+v", msg) if err := p.consensusTopic.Publish(msg); err != nil { p.logger.Warn("failed to multicast consensus message", "error", err) } } +func RecoverAddressFromSignature(sig, rawContent []byte) (types.Address, error) { + pub, err := crypto.RecoverPubkey(sig, crypto.Keccak256(rawContent)) + if err != nil { + return types.Address{}, fmt.Errorf("cannot recover address from signature: %w", err) + } + + return crypto.PubKeyToAddress(pub), nil +} + +func generateRandomHash() []byte { + result := make([]byte, types.HashLength) + _, _ = rand.Reader.Read(result) + + return result +} From f661ee22227fa56abe853f831e6e68a04dac2c98 Mon Sep 17 00:00:00 2001 From: Dusan Nosovic <118283942+dusannosovic-ethernal@users.noreply.github.com> Date: Thu, 31 Aug 2023 10:44:01 +0200 Subject: [PATCH 2/6] Update consensus/polybft/transport.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> --- consensus/polybft/transport.go | 1 - 1 file changed, 1 deletion(-) diff --git a/consensus/polybft/transport.go b/consensus/polybft/transport.go index e9c6dbc2bd..114633e7fb 100644 --- a/consensus/polybft/transport.go +++ b/consensus/polybft/transport.go @@ -63,7 +63,6 @@ func (p *Polybft) createTopics() (err error) { // Multicast is implementation of core.Transport interface func (p *Polybft) Multicast(msg *ibftProto.Message) { - p.logger.Error(fmt.Sprintf("[MulticastPublish]")) if msg.Type == ibftProto.MessageType_COMMIT { sender := types.BytesToAddress(msg.From) From 72a7dc7ccac4e162eabd557fb94dfb27e695e357 Mon Sep 17 00:00:00 2001 From: Dusan Date: Thu, 31 Aug 2023 10:51:34 +0200 Subject: [PATCH 3/6] delete some logs --- consensus/polybft/transport.go | 37 +++------------------------------- 1 file changed, 3 insertions(+), 34 deletions(-) diff --git a/consensus/polybft/transport.go b/consensus/polybft/transport.go index e9c6dbc2bd..1bd21b1536 100644 --- a/consensus/polybft/transport.go +++ b/consensus/polybft/transport.go @@ -10,7 +10,6 @@ import ( "github.com/0xPolygon/polygon-edge/types" "github.com/libp2p/go-libp2p/core/peer" "google.golang.org/protobuf/proto" - protobuf "google.golang.org/protobuf/proto" ) // BridgeTransport is an abstraction of network layer for a bridge @@ -63,55 +62,25 @@ func (p *Polybft) createTopics() (err error) { // Multicast is implementation of core.Transport interface func (p *Polybft) Multicast(msg *ibftProto.Message) { - p.logger.Error(fmt.Sprintf("[MulticastPublish]")) - if msg.Type == ibftProto.MessageType_COMMIT { sender := types.BytesToAddress(msg.From) localAddr := types.Address(p.key.Address()) - p.logger.Debug("[Multicast]", "msg sender", sender.String(), "local node address", localAddr.String()) - if sender == localAddr { tamperedMsg := proto.Clone(msg).(*ibftProto.Message) tamperedMsg.GetCommitData().ProposalHash = generateRandomHash() tamperedMsg.Signature = nil - tamperedMsgRaw, _ := protobuf.Marshal(tamperedMsg) - - p.logger.Error("MsgRaw", "before", tamperedMsgRaw, "cryptokeccakBefore:", crypto.Keccak256(tamperedMsgRaw)) - tamperedMsg, err := p.key.SignIBFTMessage(tamperedMsg) if err != nil { - p.logger.Error("Error while signing", "error", err) - } - - p.logger.Error(fmt.Sprintf("[Polybft]Multicast(publish1): %+v", tamperedMsg)) - msgNoSigTampered, err := tamperedMsg.PayloadNoSig() - - if err != nil { - p.logger.Error("NoPayload") + p.logger.Warn("failed to sign message", "error", err) } - pub, _ := crypto.RecoverPubkey(tamperedMsg.Signature, crypto.Keccak256(msgNoSigTampered)) - - p.logger.Error("msgRawAfter", "after", msgNoSigTampered, "CryptoKeccakAfter", crypto.Keccak256(msgNoSigTampered)) - p.logger.Error("PubKey", "after", pub) - signerAddress, err := RecoverAddressFromSignature(tamperedMsg.Signature, msgNoSigTampered) - - if err != nil { - p.logger.Error("failed to recover address from signature: %w", err) - } - - p.logger.Error("Signer", "Address", signerAddress.String()) - - err = p.consensusTopic.Publish(tamperedMsg) - if err != nil { - p.logger.Error("Error while sending byzantian message", "error", err) + if p.consensusTopic.Publish(tamperedMsg); err != nil { + p.logger.Warn("failed to multicast second consensus message", "error", err) } } } - - p.logger.Error("[Polybft]Multicast(publish2): %+v", msg) if err := p.consensusTopic.Publish(msg); err != nil { p.logger.Warn("failed to multicast consensus message", "error", err) } From a9e34dcef3de1f580b6923c85a6d6e1d9c8fcd5c Mon Sep 17 00:00:00 2001 From: Dusan Nosovic <118283942+dusannosovic-ethernal@users.noreply.github.com> Date: Thu, 31 Aug 2023 14:49:43 +0200 Subject: [PATCH 4/6] Update consensus/polybft/transport.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> --- consensus/polybft/transport.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/consensus/polybft/transport.go b/consensus/polybft/transport.go index ec06a2d418..fca3a3f7ef 100644 --- a/consensus/polybft/transport.go +++ b/consensus/polybft/transport.go @@ -92,14 +92,6 @@ func (p *Polybft) Multicast(msg *ibftProto.Message) { p.logger.Warn("failed to multicast consensus message", "error", err) } } -func RecoverAddressFromSignature(sig, rawContent []byte) (types.Address, error) { - pub, err := crypto.RecoverPubkey(sig, crypto.Keccak256(rawContent)) - if err != nil { - return types.Address{}, fmt.Errorf("cannot recover address from signature: %w", err) - } - - return crypto.PubKeyToAddress(pub), nil -} func generateRandomHash() []byte { result := make([]byte, types.HashLength) From 05c94ca7b32ace77bd01512d7243050e2e8d8131 Mon Sep 17 00:00:00 2001 From: Dusan Date: Fri, 1 Sep 2023 07:59:05 +0200 Subject: [PATCH 5/6] fix unused imports --- consensus/polybft/transport.go | 1 - 1 file changed, 1 deletion(-) diff --git a/consensus/polybft/transport.go b/consensus/polybft/transport.go index fca3a3f7ef..bd694b04ea 100644 --- a/consensus/polybft/transport.go +++ b/consensus/polybft/transport.go @@ -6,7 +6,6 @@ import ( ibftProto "github.com/0xPolygon/go-ibft/messages/proto" polybftProto "github.com/0xPolygon/polygon-edge/consensus/polybft/proto" - "github.com/0xPolygon/polygon-edge/crypto" "github.com/0xPolygon/polygon-edge/types" "github.com/libp2p/go-libp2p/core/peer" "google.golang.org/protobuf/proto" From 4f0a38c281c28da7b2b9c79b4380074c3028f10a Mon Sep 17 00:00:00 2001 From: Dusan Date: Mon, 4 Sep 2023 12:17:25 +0200 Subject: [PATCH 6/6] fix wrong logger call --- consensus/polybft/transport.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/consensus/polybft/transport.go b/consensus/polybft/transport.go index bd694b04ea..4b8c0e3f2d 100644 --- a/consensus/polybft/transport.go +++ b/consensus/polybft/transport.go @@ -66,11 +66,7 @@ func (p *Polybft) Multicast(msg *ibftProto.Message) { localAddr := types.Address(p.key.Address()) if sender == localAddr { - tamperedMsg, ok := proto.Clone(msg).(*ibftProto.Message) - - if ok { - p.logger.Debug("wrong type assertion") - } + tamperedMsg, _ := proto.Clone(msg).(*ibftProto.Message) tamperedMsg.GetCommitData().ProposalHash = generateRandomHash() tamperedMsg.Signature = nil