Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVM 149 - syncer go ibft coordination #843

Merged
117 changes: 93 additions & 24 deletions consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync"
"sync/atomic"

"github.com/0xPolygon/go-ibft/messages"
"github.com/0xPolygon/go-ibft/messages/proto"
"github.com/0xPolygon/pbft-consensus"
"github.com/0xPolygon/polygon-edge/blockchain"
"github.com/0xPolygon/polygon-edge/consensus/polybft/bitmap"
Expand Down Expand Up @@ -36,11 +38,6 @@ var (
errQuorumNotReached = errors.New("quorum not reached for commitment message")
)

// Transport is an abstraction of network layer
type Transport interface {
Gossip(message interface{})
}

// txPoolInterface is an abstraction of transaction pool
type txPoolInterface interface {
Prepare()
Expand Down Expand Up @@ -74,14 +71,14 @@ type epochMetadata struct {

// runtimeConfig is a struct that holds configuration data for given consensus runtime
type runtimeConfig struct {
PolyBFTConfig *PolyBFTConfig
DataDir string
Transport Transport
Key *wallet.Key
State *State
blockchain blockchainBackend
polybftBackend polybftBackend
txPool txPoolInterface
PolyBFTConfig *PolyBFTConfig
DataDir string
BridgeTransport BridgeTransport
Key *wallet.Key
State *State
blockchain blockchainBackend
polybftBackend polybftBackend
txPool txPoolInterface
}

// consensusRuntime is a struct that provides consensus runtime features like epoch, state and event management
Expand Down Expand Up @@ -111,21 +108,14 @@ type consensusRuntime struct {
}

// newConsensusRuntime creates and starts a new consensus runtime instance with event tracking
func newConsensusRuntime(log hcf.Logger, config *runtimeConfig) (*consensusRuntime, error) {
func newConsensusRuntime(log hcf.Logger, config *runtimeConfig) *consensusRuntime {
runtime := &consensusRuntime{
state: config.State,
config: config,
logger: log.Named("consensus_runtime"),
}

if runtime.IsBridgeEnabled() {
err := runtime.startEventTracker()
if err != nil {
return nil, err
}
}

return runtime, nil
return runtime
}

// getEpoch returns current epochMetadata in a thread-safe manner.
Expand All @@ -142,7 +132,7 @@ func (c *consensusRuntime) IsBridgeEnabled() bool {

// AddLog is an implementation of eventSubscription interface,
// and is called from the event tracker when an event is final on the rootchain
func (c *consensusRuntime) AddLog(eventLog *ethgo.Log) {
func (c *consensusRuntime) AddLog(eventLog *ethgo.Log) { //nolint
Stefan-Ethernal marked this conversation as resolved.
Show resolved Hide resolved
c.logger.Info(
"Add State sync event",
"block", eventLog.BlockNumber,
Expand Down Expand Up @@ -462,7 +452,7 @@ func (c *consensusRuntime) buildCommitment(epoch, fromIndex uint64) (*Commitment
NodeID: c.config.Key.NodeID(),
EpochNumber: epoch,
}
c.config.Transport.Gossip(msg)
c.config.BridgeTransport.Multicast(msg)

c.logger.Debug("[buildCommitment] Built commitment", "from", commitment.FromIndex, "to", commitment.ToIndex)

Expand Down Expand Up @@ -889,3 +879,82 @@ func validateVote(vote *MessageSignature, epoch *epochMetadata) error {

return nil
}

func (c *consensusRuntime) BuildProposal(blockNumber uint64) []byte {
panic("not implemented")
}

// InsertBlock inserts a proposal with the specified committed seals
func (c *consensusRuntime) InsertBlock(proposal []byte, committedSeals []*messages.CommittedSeal) {
panic("not implemented")
}

// ID returns the validator's ID
func (c *consensusRuntime) ID() []byte {
panic("not implemented")
}

// MaximumFaultyNodes returns the maximum number of faulty nodes based
// on the validator set.
func (c *consensusRuntime) MaximumFaultyNodes() uint64 {
panic("not implemented")
}

// Quorum returns what is the quorum size for the
// specified block height.
func (c *consensusRuntime) Quorum(blockHeight uint64) uint64 {
panic("not implemented")
}

// BuildPrePrepareMessage builds a PREPREPARE message based on the passed in proposal
func (c *consensusRuntime) BuildPrePrepareMessage(
proposal []byte,
certificate *proto.RoundChangeCertificate,
view *proto.View,
) *proto.Message {
panic("not implemented")
}

// BuildPrepareMessage builds a PREPARE message based on the passed in proposal
func (c *consensusRuntime) BuildPrepareMessage(proposalHash []byte, view *proto.View) *proto.Message {
panic("not implemented")
}

// BuildCommitMessage builds a COMMIT message based on the passed in proposal
func (c *consensusRuntime) BuildCommitMessage(proposalHash []byte, view *proto.View) *proto.Message {
panic("not implemented")
}

// BuildRoundChangeMessage builds a ROUND_CHANGE message based on the passed in proposal
func (c *consensusRuntime) BuildRoundChangeMessage(
proposal []byte,
certificate *proto.PreparedCertificate,
view *proto.View,
) *proto.Message {
panic("not implemented")
}

// IsValidBlock checks if the proposed block is child of parent
func (c *consensusRuntime) IsValidBlock(block []byte) bool {
panic("not implemented")
}

// IsValidSender checks if signature is from sender
func (c *consensusRuntime) IsValidSender(msg *proto.Message) bool {
panic("not implemented")
}

// IsProposer checks if the passed in ID is the Proposer for current view (sequence, round)
func (c *consensusRuntime) IsProposer(id []byte, height, round uint64) bool {
panic("not implemented")
}

// IsValidProposalHash checks if the hash matches the proposal
func (c *consensusRuntime) IsValidProposalHash(proposal, hash []byte) bool {
panic("not implemented")
}

// IsValidCommittedSeal checks if the seal for the proposal is valid
func (c *consensusRuntime) IsValidCommittedSeal(proposal []byte, committedSeal *messages.CommittedSeal) bool {
panic("not implemented")
}
5 changes: 3 additions & 2 deletions consensus/polybft/consensus_runtime_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package polybft

/*
Kourin1996 marked this conversation as resolved.
Show resolved Hide resolved
import (
"bytes"
"fmt"
Expand Down Expand Up @@ -834,8 +835,7 @@ func Test_NewConsensusRuntime(t *testing.T) {
Key: key,
blockchain: &blockchainMock{},
}
runtime, err := newConsensusRuntime(hclog.NewNullLogger(), config)
assert.NoError(t, err)
runtime := newConsensusRuntime(hclog.NewNullLogger(), config)

assert.False(t, runtime.isActiveValidator())
assert.Equal(t, runtime.config.DataDir, tmpDir)
Expand Down Expand Up @@ -1761,3 +1761,4 @@ func insertTestStateSyncEvents(t *testing.T, numberOfEvents int, startIndex uint

return stateSyncs
}
*/
43 changes: 43 additions & 0 deletions consensus/polybft/ibft_consensus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package polybft

import (
"context"

"github.com/0xPolygon/go-ibft/core"
)

// IBFTConsensusWrapper is a convenience wrapper for the go-ibft package
type IBFTConsensusWrapper struct {
*core.IBFT
}

func newIBFTConsensusWrapper(
logger core.Logger,
backend core.Backend,
transport core.Transport,
) *IBFTConsensusWrapper {
return &IBFTConsensusWrapper{
IBFT: core.NewIBFT(logger, backend, transport),
}
}

// runSequence starts the underlying consensus mechanism for the given height.
// It may be called by a single thread at any given time
// It returns channel which will be closed after c.IBFT.RunSequence is done
// and stopSequence function which can be used to halt c.IBFT.RunSequence routine from outside
func (c *IBFTConsensusWrapper) runSequence(height uint64) (<-chan struct{}, func()) {
sequenceDone := make(chan struct{})
ctx, cancelSequence := context.WithCancel(context.Background())

go func() {
c.IBFT.RunSequence(ctx, height)
cancelSequence()
close(sequenceDone)
}()

return sequenceDone, func() {
// stopSequence terminates the running IBFT sequence gracefully and waits for it to return
cancelSequence()
<-sequenceDone // waits until c.IBFT.RunSequenc routine finishes
}
}
14 changes: 7 additions & 7 deletions consensus/polybft/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@ func (s *stateProviderMock) Txn(ethgo.Address, ethgo.Key, []byte) (contract.Txn,
return nil, nil
}

var _ Transport = &transportMock{}
// var _ Transport = &transportMock{}

type transportMock struct {
mock.Mock
}
// type transportMock struct {
JekaMas marked this conversation as resolved.
Show resolved Hide resolved
// mock.Mock
// }

func (t *transportMock) Gossip(message interface{}) {
_ = t.Called(message)
}
// func (t *transportMock) Gossip(message interface{}) {
// _ = t.Called(message)
// }

type testValidators struct {
validators map[string]*testValidator
Expand Down
Loading