Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Feature - State Sync manager (#1067)
Browse files Browse the repository at this point in the history
* initial work

* Rework on event tracker and tests

* More stuff

* Migrate last test

* More changes

* Remove unused test

* Use correct core-contracts version

* Remove logs

* Use eventTracker from tracker package, code cleanup, bug fixes

* Code cleanup

* Code cleanup

* Comments fix

* Dummy state sync manager when bridge is not enabled

* Comments fix

* Comments fix

* Rebase fix

Co-authored-by: Goran Rojovic <[email protected]>
  • Loading branch information
ferranbt and goran-ethernal authored Jan 3, 2023
1 parent a65d2d0 commit 4c5d0d9
Show file tree
Hide file tree
Showing 8 changed files with 1,047 additions and 1,682 deletions.
477 changes: 63 additions & 414 deletions consensus/polybft/consensus_runtime.go

Large diffs are not rendered by default.

1,243 changes: 85 additions & 1,158 deletions consensus/polybft/consensus_runtime_test.go

Large diffs are not rendered by default.

28 changes: 8 additions & 20 deletions consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ func (p *Polybft) Start() error {
// initRuntime creates consensus runtime
func (p *Polybft) initRuntime() error {
runtimeConfig := &runtimeConfig{
PolyBFTConfig: p.consensusConfig,
Key: p.key,
DataDir: p.dataDir,
BridgeTransport: &runtimeTransportWrapper{p.bridgeTopic, p.logger},
State: p.state,
blockchain: p.blockchain,
polybftBackend: p,
txPool: p.txPool,
PolyBFTConfig: p.consensusConfig,
Key: p.key,
DataDir: p.dataDir,
State: p.state,
blockchain: p.blockchain,
polybftBackend: p,
txPool: p.txPool,
bridgeTopic: p.bridgeTopic,
}

runtime, err := newConsensusRuntime(p.logger, runtimeConfig)
Expand All @@ -267,18 +267,6 @@ func (p *Polybft) initRuntime() error {

// startRuntime starts consensus runtime
func (p *Polybft) startRuntime() error {
if p.runtime.IsBridgeEnabled() {
// start bridge event tracker
if err := p.runtime.startEventTracker(); err != nil {
return fmt.Errorf("starting event tracker failed: %w", err)
}

// subscribe to bridge topic
if err := p.runtime.subscribeToBridgeTopic(p.bridgeTopic); err != nil {
return fmt.Errorf("bridge topic subscription failed: %w", err)
}
}

go p.startConsensusProtocol()

return nil
Expand Down
48 changes: 8 additions & 40 deletions consensus/polybft/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ const (
validatorSnapshotLimit = 100
// numberOfSnapshotsToLeaveInMemory defines a number of validator snapshots to leave in memory
numberOfSnapshotsToLeaveInMemory = 12
// numberOfSnapshotsToLeaveInMemory defines a number of validator snapshots to leave in db
// numberOfSnapshotsToLeaveInDB defines a number of validator snapshots to leave in db
numberOfSnapshotsToLeaveInDB = 20
// number of stateSyncEvents to be processed before a commitment message can be created and gossiped
stateSyncCommitmentSize = 10
)

type exitEventNotFoundError struct {
Expand All @@ -75,43 +73,6 @@ func (e *exitEventNotFoundError) Error() string {
return fmt.Sprintf("could not find any exit event that has an id: %v and epoch: %v", e.exitID, e.epoch)
}

// newStateSyncEvent creates an instance of pending state sync event.
func newStateSyncEvent(
id uint64,
sender ethgo.Address,
target ethgo.Address,
data []byte,
) *types.StateSyncEvent {
return &types.StateSyncEvent{
ID: id,
Sender: sender,
Receiver: target,
Data: data,
}
}

func decodeStateSyncEvent(log *ethgo.Log) (*types.StateSyncEvent, error) {
raw, err := stateTransferEventABI.ParseLog(log)
if err != nil {
return nil, err
}

eventGeneric, err := decodeEventData(raw, log,
func(id *big.Int, sender, receiver ethgo.Address, data []byte) interface{} {
return newStateSyncEvent(id.Uint64(), sender, receiver, data)
})
if err != nil {
return nil, err
}

stateSyncEvent, ok := eventGeneric.(*types.StateSyncEvent)
if !ok {
return nil, errors.New("failed to convert event to StateSyncEvent instance")
}

return stateSyncEvent, nil
}

func decodeExitEvent(log *ethgo.Log, epoch, block uint64) (*ExitEvent, error) {
if !exitEventABI.Match(log) {
// valid case, not an exit event
Expand Down Expand Up @@ -222,6 +183,13 @@ type TransportMessage struct {
EpochNumber uint64
}

func (t *TransportMessage) ToSignature() *MessageSignature {
return &MessageSignature{
Signature: t.Signature,
From: t.NodeID,
}
}

var (
// bucket to store rootchain bridge events
syncStateEventsBucket = []byte("stateSyncEvents")
Expand Down
Loading

0 comments on commit 4c5d0d9

Please sign in to comment.