Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
179 commits
Select commit Hold shift + click to select a range
a880635
state_sync server init
gokutheengineer Jan 6, 2023
affc3a1
feat: state_sync and consensus interaction init
gokutheengineer Jan 6, 2023
ee550a5
state sync server initial placeholder for HandleStateSyncMetadataRequest
gokutheengineer Jan 6, 2023
5472099
state_sync (consensus): update how to send placeholder function
gokutheengineer Jan 6, 2023
4347ee1
refactor: clean extra lines
gokutheengineer Jan 8, 2023
7ef1579
refactor: proper proto definition for enum
gokutheengineer Jan 8, 2023
de5c9d6
refactor: remove IsServerModEnabled function from consensus module
gokutheengineer Jan 8, 2023
94d1c82
init sendToPeer and handleStateSyncMetaDataRequest functions
gokutheengineer Jan 9, 2023
a2f1bec
initial get block implementation
gokutheengineer Jan 9, 2023
35e9f6b
consensus WIP: update test utils for state sync tests
gokutheengineer Jan 10, 2023
1397f88
consensus WIP: testing
gokutheengineer Jan 11, 2023
606fdb5
consensus (refactor): new shared interface of consensus for pacemaker
gokutheengineer Jan 3, 2023
91b8b32
refactor (consensus): WIP, seperate interfaces, remove consensusMod f…
gokutheengineer Jan 4, 2023
7ffd4fe
refactor (consensus): create pacemaker submodule
gokutheengineer Jan 4, 2023
a82f0a9
refactor (consensus): clean some comments
gokutheengineer Jan 4, 2023
467c81e
consensus (refactor): WIP cleanup
gokutheengineer Jan 5, 2023
8339d95
refactor (consensus): WIP more cleanup
gokutheengineer Jan 5, 2023
e68b9ac
consensus (refactor): address remaining comments on PR
gokutheengineer Jan 5, 2023
8411b85
rafactor: remove unused function
gokutheengineer Jan 7, 2023
5c9e28c
refactor: address PR comments
gokutheengineer Jan 7, 2023
4ab88d2
Update shared/modules/consensus_module.go
gokutheengineer Jan 7, 2023
db8350d
Update shared/modules/consensus_module.go
gokutheengineer Jan 7, 2023
b803a38
Update consensus/pacemaker_consensus.go
gokutheengineer Jan 7, 2023
f6e81d9
Update shared/modules/consensus_module.go
gokutheengineer Jan 7, 2023
d3d479c
refactor (consensus): return error in GetPrepareQC
gokutheengineer Jan 7, 2023
e1a454c
Update shared/modules/consensus_module.go
gokutheengineer Jan 10, 2023
5795d08
initial rebased version, WIP
gokutheengineer Jan 11, 2023
1a4a682
refactor (consensus): clean and add some comments
gokutheengineer Jan 12, 2023
b05a7b5
WIP: clean some comments, start updating CHANGELOG
gokutheengineer Jan 12, 2023
c57197e
clean few comments, update CHANGELOGS, remove incomplete byzantine te…
gokutheengineer Jan 13, 2023
193786c
Update consensus/pacemaker/pacemaker.go
gokutheengineer Jan 14, 2023
3e518f0
Update consensus/doc/CHANGELOG.md
gokutheengineer Jan 14, 2023
cc8705f
clean, address comments
gokutheengineer Jan 14, 2023
ad80249
move pacemaker
gokutheengineer Jan 15, 2023
01f2efd
refactor: bring back pacemaker impl
gokutheengineer Jan 15, 2023
1949251
merge with issue/395
gokutheengineer Jan 15, 2023
1e0f143
rename state_sync module.go
gokutheengineer Jan 15, 2023
bd603a1
fix comments
gokutheengineer Jan 16, 2023
59e1ac7
remove pacemaker
gokutheengineer Jan 16, 2023
760ce07
add updated pacemaker
gokutheengineer Jan 16, 2023
bfed5c1
Bring back old pacemaker
Olshansk Jan 16, 2023
fc1bcfb
Move file
Olshansk Jan 16, 2023
aacef7e
Apply changes
Olshansk Jan 16, 2023
e49daf3
WIP: structre test for state sync
gokutheengineer Jan 16, 2023
907f5df
refactor: remove shouldHandleHotstuffMessage
gokutheengineer Jan 17, 2023
1887c02
Update consensus/module.go
gokutheengineer Jan 17, 2023
3351d9b
refactor: update prepareQC
gokutheengineer Jan 17, 2023
38798ee
refactor: address comments
gokutheengineer Jan 17, 2023
cfece55
Update consensus/pacemaker/pacemaker.go
gokutheengineer Jan 17, 2023
8e0b9a9
fix comment
gokutheengineer Jan 17, 2023
dbf3775
merge
gokutheengineer Jan 17, 2023
cd9c74e
update changelog in consensus
gokutheengineer Jan 17, 2023
81fa9e2
fix
gokutheengineer Jan 17, 2023
d5f8c87
fix round assertion
gokutheengineer Jan 17, 2023
3386f18
WIP able to receive req in server node
gokutheengineer Jan 18, 2023
509ce2e
fix leader id issue
gokutheengineer Jan 18, 2023
d038950
fix localnet issue
gokutheengineer Jan 19, 2023
398ba44
refactor: add comments, fix
gokutheengineer Jan 19, 2023
9b5c396
Merge branch 'main' into issue/395-decouple-consensus-and-pacemaker
gokutheengineer Jan 19, 2023
6c9ba7e
Revert "Merge branch 'main' into issue/395-decouple-consensus-and-pac…
gokutheengineer Jan 19, 2023
92e9a8d
update changelogs
gokutheengineer Jan 19, 2023
bfe8adc
update changelog of persistance
gokutheengineer Jan 19, 2023
536a588
feat: server test passes
gokutheengineer Jan 20, 2023
cd03619
testing: add max height test statement
gokutheengineer Jan 20, 2023
988aad1
merge: recent main
gokutheengineer Jan 20, 2023
205fd45
fix changelog date
gokutheengineer Jan 20, 2023
5c8f1c5
checkout makefile from master branch
gokutheengineer Jan 21, 2023
244cd66
add block.proto
gokutheengineer Jan 21, 2023
82ef4d8
add consensus state_sync.proto
gokutheengineer Jan 21, 2023
405b231
rm persistance block_persistance.proto
gokutheengineer Jan 21, 2023
fbfb238
update block type
gokutheengineer Jan 21, 2023
ef38cbe
update CHANGELOGs
gokutheengineer Jan 21, 2023
4d5d8d7
fix hotstuff_leader
gokutheengineer Jan 21, 2023
5bb8f14
fix hotstuff_replica
gokutheengineer Jan 21, 2023
6b95583
fix messages
gokutheengineer Jan 21, 2023
06a4cf5
fix state_sync module
gokutheengineer Jan 21, 2023
5061efe
remove extra comment line added by viscose
gokutheengineer Jan 21, 2023
1e727b2
fix changes of vscode
gokutheengineer Jan 21, 2023
ef7bf90
fix pacemaker test
gokutheengineer Jan 21, 2023
89b226e
fix persistance state test
gokutheengineer Jan 21, 2023
b1bcad0
add persistance files
gokutheengineer Jan 21, 2023
f8a12a3
merge main
gokutheengineer Jan 21, 2023
18632fb
add learning doc
gokutheengineer Jan 21, 2023
e96561d
udpate outdated file
gokutheengineer Jan 21, 2023
e49b2e2
udpate changelog date
gokutheengineer Jan 21, 2023
75f24b6
remove extra space
gokutheengineer Jan 21, 2023
78b57db
Merge branch 'issue/395-decouple-consensus-and-pacemaker' into issue/…
gokutheengineer Jan 23, 2023
bb1cdd0
merge with pacemaker changes
gokutheengineer Jan 23, 2023
5bd473e
WIP refactor, update tests, failing
gokutheengineer Jan 23, 2023
3bae0e6
udpate mocks
gokutheengineer Jan 24, 2023
cf03187
test: update tests, getblock is not passing yet
gokutheengineer Jan 24, 2023
1747674
clean debug comments
gokutheengineer Jan 24, 2023
21ee701
add TODOs
gokutheengineer Jan 24, 2023
fd74b98
add mock folder
gokutheengineer Jan 25, 2023
5ce1e6a
fix
gokutheengineer Jan 25, 2023
d39c4eb
Mock KV store to avoid nil blocks
Olshansk Jan 25, 2023
1d3fe8e
merge with main
gokutheengineer Jan 26, 2023
8d1512c
clean kvstore, mocks in persistence
gokutheengineer Jan 26, 2023
98fa477
clean Makefile
gokutheengineer Jan 26, 2023
06043a7
update getblockresponse, state sync tests are barely passting
gokutheengineer Jan 26, 2023
cf9d8b8
Revert "clean Makefile"
gokutheengineer Jan 26, 2023
7a51e96
Revert "clean kvstore, mocks in persistence"
gokutheengineer Jan 26, 2023
80e251c
consensus: refactor comments, tests
gokutheengineer Jan 26, 2023
5d8268a
update tests
gokutheengineer Jan 26, 2023
b366b77
refactor: clean code
gokutheengineer Jan 27, 2023
e6d52a7
refactor: clean some nits
gokutheengineer Jan 27, 2023
35b7e4b
clean state_sync test
gokutheengineer Jan 27, 2023
b4be181
refactor: more cleaning
gokutheengineer Jan 27, 2023
7ef66d4
small nits
gokutheengineer Jan 27, 2023
d26035f
Merge branch 'main' into issue/409-state-sync-server-to-advertise-blocks
gokutheengineer Jan 27, 2023
1ebf348
update changelogs
gokutheengineer Jan 27, 2023
3871218
fix some nits
gokutheengineer Jan 29, 2023
84d9dbd
fix nits
gokutheengineer Jan 30, 2023
3bd5e82
merge main
gokutheengineer Jan 30, 2023
acaf06c
update changeleg, fix nits, add comments
gokutheengineer Jan 30, 2023
f8b1305
update state sync initial status
gokutheengineer Jan 30, 2023
9100626
Update consensus/doc/CHANGELOG.md
gokutheengineer Jan 31, 2023
4c1d708
Update consensus/doc/CHANGELOG.md
gokutheengineer Jan 31, 2023
9635c25
Update persistence/docs/CHANGELOG.md
gokutheengineer Jan 31, 2023
2dc5306
Update consensus/module.go
gokutheengineer Jan 31, 2023
51ebc6b
Update consensus/doc/CHANGELOG.md
gokutheengineer Jan 31, 2023
1d62977
Update consensus/types/errors.go
gokutheengineer Jan 31, 2023
cc853ac
Update consensus/module.go
gokutheengineer Jan 31, 2023
1e2aaef
Update shared/modules/consensus_module.go
gokutheengineer Jan 31, 2023
0e96a8c
Update consensus/state_sync/helpers.go
gokutheengineer Jan 31, 2023
cd4ee45
Update consensus/module.go
gokutheengineer Jan 31, 2023
9cdb7e1
Update consensus/state_sync_handler.go
gokutheengineer Jan 31, 2023
1c61593
Update consensus/state_sync/server.go
gokutheengineer Jan 31, 2023
1d1887c
Update consensus/state_sync/server.go
gokutheengineer Jan 31, 2023
d20c8db
Update consensus/state_sync_handler.go
gokutheengineer Jan 31, 2023
610c016
Update consensus/state_sync_handler.go
gokutheengineer Jan 31, 2023
12923e2
Merge branch 'main' into issue/409-state-sync-server-to-advertise-blocks
gokutheengineer Jan 31, 2023
0adacea
unify height functions, fix comments
gokutheengineer Jan 31, 2023
3216b20
address comments
gokutheengineer Feb 1, 2023
c4dcacf
update state sync error message
gokutheengineer Feb 1, 2023
8d4fdb2
fix comments remove persistence/mocks.go
gokutheengineer Feb 1, 2023
1f8c156
remove msgtype from state sync message
gokutheengineer Feb 1, 2023
445a3bc
udpate app to handle getblock and get metadata functions
gokutheengineer Feb 3, 2023
cc5a6aa
update consensus
gokutheengineer Feb 3, 2023
c67b21d
update shared module
gokutheengineer Feb 3, 2023
098897a
update persistence module
gokutheengineer Feb 3, 2023
ab13bc4
fix test
gokutheengineer Feb 3, 2023
3299c43
updates to make debug client work
gokutheengineer Feb 3, 2023
0a008f2
Update consensus/debugging.go
gokutheengineer Feb 4, 2023
2a8c242
Update shared/converters/util.go
gokutheengineer Feb 4, 2023
c114bb8
Update consensus/e2e_tests/state_sync_test.go
gokutheengineer Feb 4, 2023
b33d858
update makefile
gokutheengineer Feb 5, 2023
19c1367
update configs
gokutheengineer Feb 5, 2023
3bde322
update config test and reading
gokutheengineer Feb 5, 2023
cae5bf0
address comments
gokutheengineer Feb 5, 2023
360ac54
merge with main
gokutheengineer Feb 5, 2023
ab00896
update changelogs, nit on ,akefile
gokutheengineer Feb 5, 2023
4bea633
refactor
gokutheengineer Feb 5, 2023
94aea17
update logging
gokutheengineer Feb 6, 2023
417965f
update changelogs
gokutheengineer Feb 6, 2023
a0cd270
add persistence mock generation
gokutheengineer Feb 6, 2023
94581fd
fix makefile
gokutheengineer Feb 6, 2023
1c6ad13
fix nit
gokutheengineer Feb 6, 2023
911ef8e
Merge branch 'main' into issue/409-state-sync-server-to-advertise-blocks
gokutheengineer Feb 7, 2023
4fb8763
merge with main
gokutheengineer Feb 7, 2023
4ba153b
put mocks placholder file for dynamically generated mocks
gokutheengineer Feb 7, 2023
05fb40c
fix some nits
gokutheengineer Feb 7, 2023
d9c41b7
Merge branch 'main' into issue/409-state-sync-server-to-advertise-blocks
gokutheengineer Feb 7, 2023
c87e263
address golint errors
gokutheengineer Feb 7, 2023
a8eb52d
update makefile
gokutheengineer Feb 7, 2023
064942e
merge with main
gokutheengineer Feb 8, 2023
feac475
update changelog dates
gokutheengineer Feb 8, 2023
d773aef
Update consensus/types/proto/state_sync.proto
gokutheengineer Feb 9, 2023
b6ecff5
Update consensus/state_sync/module.go
gokutheengineer Feb 9, 2023
3a797e4
Update shared/messaging/proto/debug_message.proto
gokutheengineer Feb 9, 2023
e984ae1
Update app/client/doc/CHANGELOG.md
gokutheengineer Feb 9, 2023
51f1833
Update build/docs/CHANGELOG.md
gokutheengineer Feb 9, 2023
3511a46
Update shared/modules/consensus_module.go
gokutheengineer Feb 9, 2023
a304534
Update consensus/state_sync/module.go
gokutheengineer Feb 9, 2023
ad0acc5
address pr comments
gokutheengineer Feb 9, 2023
86efb2e
merge with remote changes
gokutheengineer Feb 9, 2023
e4fe037
update changelog dates
gokutheengineer Feb 9, 2023
92e9c44
Update consensus/pacemaker/module.go
gokutheengineer Feb 9, 2023
fb2e70d
Update consensus/types/proto/state_sync.proto
gokutheengineer Feb 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion consensus/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const (

ByzantineThreshold = float64(2) / float64(3)

HotstuffMessageContentType = "consensus.HotstuffMessage"
HotstuffMessageContentType = "consensus.HotstuffMessage"
StateSyncMessageContentType = "consensus.StateSyncMessage"
)

var (
Expand Down
28 changes: 28 additions & 0 deletions consensus/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/pokt-network/pocket/shared/codec"
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
"github.com/pokt-network/pocket/shared/modules"

"github.com/pokt-network/pocket/consensus/state_sync"
"google.golang.org/protobuf/types/known/anypb"
)

Expand Down Expand Up @@ -73,6 +75,10 @@ type consensusModule struct {
// DEPRECATE: Remove later when we build a shared/proper/injected logger
logPrefix string

// State sync
stateSync state_sync.StateSyncModule
//serverMode bool

// TECHDEBT: Rename this to `consensusMessagePool` or something similar
// and reconsider if an in-memory map is the best approach
messagePool map[typesCons.HotstuffStep][]*typesCons.HotstuffMessage
Expand Down Expand Up @@ -146,6 +152,13 @@ func (*consensusModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, e
address := privateKey.Address().String()
valIdMap, idValMap := typesCons.GetValAddrToIdMap(valMap)

stateSyncMod, err := state_sync.CreateStateSync(runtimeMgr)
if err != nil {
return nil, err
}

stateSync := stateSyncMod.(state_sync.StateSyncModule)

paceMaker := paceMakerMod.(Pacemaker)

m = &consensusModule{
Expand Down Expand Up @@ -174,6 +187,9 @@ func (*consensusModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, e
paceMaker: paceMaker,
leaderElectionMod: leaderElectionMod.(leader_election.LeaderElectionModule),

stateSync: stateSync,
//serverMode: false,

logPrefix: DefaultLogPrefix,
messagePool: make(map[typesCons.HotstuffStep][]*typesCons.HotstuffMessage),
}
Expand Down Expand Up @@ -202,6 +218,10 @@ func (m *consensusModule) Start() error {
return err
}

if err := m.stateSync.Start(); err != nil {
return err
}

if err := m.leaderElectionMod.Start(); err != nil {
return err
}
Expand Down Expand Up @@ -319,3 +339,11 @@ func (m *consensusModule) loadPersistedState() error {

return nil
}

func (m *consensusModule) IsServerModEnabled() bool {
return m.stateSync.IsServerModEnabled()
}

func (m *consensusModule) EnableServerMode() {
m.stateSync.EnableServerMode()
}
100 changes: 100 additions & 0 deletions consensus/state_sync/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package state_sync

import (
typesCons "github.com/pokt-network/pocket/consensus/types"
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
)

type SyncState interface {
// latest local height
LatestHeight() int64
// latest network height (from the aggregation of Peer Sync Meta)
LatestNetworkHeight() int64
// retrieve peer meta (actively updated through churn management)
GetPeers() []PeerSyncMeta
// returns ordered array of missing block heights
GetMissingBlockHeights() []int64
}

type BlockRequestMessage interface {
// the height the peer wants from the block store
GetHeight() int64
}

type BlockResponseMessage interface {
// the bytes of the requested block from the block store
GetBlockBytes() []byte
}

// TODO: needs to be shared between P2P as the Churn Management Process updates this information
type PeerSyncMeta interface {
// the unique identifier associated with the peer
GetPeerID() string
// the maximum height the peer has in the block store
GetMaxHeight() int64
// the minimum height the peer has in the block store
GetMinHeight() int64
}

// ~~~~~ TODO(#362): The interface below is only meant to be used as a guideline and not implemented explicitly. It will be updated & removed over time. ~~~~~
type StateSyncModuleLEGACY interface {
// -- Constructor Setter Functions --

// `HandleStateSync` function:
// - Create a Utility Context
// - Block.ValidateBasic()
// - Consensus Module Replica Path
// - Prepare Block: utilityContext.SetProposalBlock(block)
// - Apply Block: utilityContext.ApplyBlock(block)
// - Validate Block: utilityContext.AppHash == Block.AppHash
// - Store Block: consensusModule.CommitBlock()
HandleStateSyncMessage(msg BlockResponseMessage)

// `GetPeerSyncMeta` function:
// - Retrieve a list of active peers with their metadata (identified and retrieved through P2P's `Churn Management`)
GetPeerMetadata(GetPeerSyncMeta func() (peers []PeerSyncMeta, err error))

// `NetworkSend` function contract:
// - sends data to an address via P2P network
NetworkSend(NetworkSend func(data []byte, address cryptoPocket.Address) error)

// -- Sync modes --

// In the StateSync protocol, the Node fields valid BlockRequests from its peers to help them CatchUp to be Synced.
// This sub-protocol is continuous throughout the lifecycle of StateSync.
RunServerMode()

// In SyncedMode, the Node is caught up to the latest block and is listening & waiting for the latest block to be passed
// to maintain a synchronous state with the global SyncState.
// - UpdatePeerMetadata from P2P module
// - UpdateSyncState
// - Rely on new blocks to be propagated via the P2P network after Validators reach consensus
// - If `localSyncState.Height < globalNetworkSyncState.Height` -> RunSyncMode() // careful about race-conditions
RunSyncedMode()

// Runs sync mode 'service' that continuously runs while `localSyncState.Height < globalNetworkSyncState.Height`
// - UpdatePeerMetadata from P2P module
// - Retrieve missing blocks from peers
// - Process retrieved blocks
// - UpdateSyncState
// - If `localSyncState.Height == globalNetworkSyncState.Height` -> RunSyncedMode()
RunSyncMode()

// Returns the `highest priority aka lowest height` missing block heights up to `max` heights
GetMissingBlockHeights(state SyncState, max int) (blockHeights []int64, err error)

// Random selection of eligilbe peers enables a fair distribution of blockRequests over time via law of large numbers
// An eligible peer is when `PeerMeta.MinHeight <= blockHeight <= PeerMeta.MaxHeight`
GetRandomEligiblePeersForHeight(blockHeight int64) (eligiblePeer PeerSyncMeta, err error)

// Uses `NetworkSend` to send a `BlockRequestMessage` to a specific peer
SendBlockRequest(peerId string) error

// Uses 'NetworkSend' to send a `BlockResponseMessage` to a specific peer
// This function is used in 'ServerMode()'
HandleBlockRequest(message BlockRequestMessage) error

// Uses `HandleBlock` to process retrieved blocks from peers
// Must update sync state using `SetMissingBlockHeight`
ProcessBlock(block *typesCons.Block) error
}
172 changes: 77 additions & 95 deletions consensus/state_sync/module.go
Original file line number Diff line number Diff line change
@@ -1,125 +1,107 @@
package state_sync

import (
"log"

typesCons "github.com/pokt-network/pocket/consensus/types"
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
"github.com/pokt-network/pocket/shared/modules"
)

// This module is responsible for handling requests and business logic that advertises and shares
// local state metadata with other peers synching to the latest block.
type StateSyncServerModule interface {
modules.Module
const (
stateSyncModuleName = "stateSyncModule"
)

// Advertise (send) the local state sync metadata to the requesting peer
HandleStateSyncMetadataRequest(*typesCons.StateSyncMetadataRequest) error
type Sync_Mode string

// Send the block being requested by the peer
HandleGetBlockRequest(*typesCons.GetBlockRequest) error
}
const (
Snyc Sync_Mode = "sync"
Synched Sync_Mode = "synched"
Pacemaker Sync_Mode = "pacemaker"
Server Sync_Mode = "server"
)

type StateSyncModule interface {
modules.Module
StateSyncServerModule

// Handle a metadata response from a peer so this node can update its local view of the state
// sync metadata available from its peers
HandleStateSyncMetadataResponse(*typesCons.StateSyncMetadataResponse) error

// Handle a block response from a peer so this node can update apply it to its local state
// and catch up to the global world state
HandleStateSyncBlockResponse(*typesCons.StateSyncMetadataResponse) error
HandleGetBlockResponse(*typesCons.GetBlockResponse) error

//HandleStateSyncMetadataRequest

IsServerModEnabled() bool
EnableServerMode()
}

var (
_ modules.Module = &stateSyncModule{}
_ StateSyncServerModule = &stateSyncModule{}
)

type stateSyncModule struct {
bus modules.Bus

currentMode Sync_Mode
serverMode bool
}

func CreateStateSync(runtimeMgr modules.RuntimeMgr) (modules.Module, error) {
var m stateSyncModule
return m.Create(runtimeMgr)
}

func (*stateSyncModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) {
//! TODO: think about what must be the default state?
return &stateSyncModule{
bus: nil,
currentMode: Synched,
serverMode: false,
}, nil
}

func (m *stateSyncModule) Start() error {
return nil
}

func (m *stateSyncModule) Stop() error {
return nil
}

func (m *stateSyncModule) SetBus(pocketBus modules.Bus) {
m.bus = pocketBus
}

func (m *stateSyncModule) GetBus() modules.Bus {
if m.bus == nil {
log.Fatalf("PocketBus is not initialized")
}
return m.bus
}

// ~~~~~ TODO(#362): The interface below is only meant to be used as a guideline and not implemented explicitly. It will updated & removed over time. ~~~~~
type StateSyncModuleLEGACY interface {
// -- Constructor Setter Functions --

// `HandleStateSync` function:
// - Create a Utility Context
// - Block.ValidateBasic()
// - Consensus Module Replica Path
// - Prepare Block: utilityContext.SetProposalBlock(block)
// - Apply Block: utilityContext.ApplyBlock(block)
// - Validate Block: utilityContext.AppHash == Block.AppHash
// - Store Block: consensusModule.CommitBlock()
HandleStateSyncMessage(msg BlockResponseMessage)

// `GetPeerSyncMeta` function:
// - Retrieve a list of active peers with their metadata (identified and retrieved through P2P's `Churn Management`)
GetPeerMetadata(GetPeerSyncMeta func() (peers []PeerSyncMeta, err error))

// `NetworkSend` function contract:
// - sends data to an address via P2P network
NetworkSend(NetworkSend func(data []byte, address cryptoPocket.Address) error)

// -- Sync modes --

// In the StateSync protocol, the Node fields valid BlockRequests from its peers to help them CatchUp to be Synced.
// This sub-protocol is continuous throughout the lifecycle of StateSync.
RunServerMode()

// In SyncedMode, the Node is caught up to the latest block and is listening & waiting for the latest block to be passed
// to maintain a synchronous state with the global SyncState.
// - UpdatePeerMetadata from P2P module
// - UpdateSyncState
// - Rely on new blocks to be propagated via the P2P network after Validators reach consensus
// - If `localSyncState.Height < globalNetworkSyncState.Height` -> RunSyncMode() // careful about race-conditions
RunSyncedMode()

// Runs sync mode 'service' that continuously runs while `localSyncState.Height < globalNetworkSyncState.Height`
// - UpdatePeerMetadata from P2P module
// - Retrieve missing blocks from peers
// - Process retrieved blocks
// - UpdateSyncState
// - If `localSyncState.Height == globalNetworkSyncState.Height` -> RunSyncedMode()
RunSyncMode()

// Returns the `highest priority aka lowest height` missing block heights up to `max` heights
GetMissingBlockHeights(state SyncState, max int) (blockHeights []int64, err error)

// Random selection of eligilbe peers enables a fair distribution of blockRequests over time via law of large numbers
// An eligible peer is when `PeerMeta.MinHeight <= blockHeight <= PeerMeta.MaxHeight`
GetRandomEligiblePeersForHeight(blockHeight int64) (eligiblePeer PeerSyncMeta, err error)

// Uses `NetworkSend` to send a `BlockRequestMessage` to a specific peer
SendBlockRequest(peerId string) error

// Uses 'NetworkSend' to send a `BlockResponseMessage` to a specific peer
// This function is used in 'ServerMode()'
HandleBlockRequest(message BlockRequestMessage) error

// Uses `HandleBlock` to process retrieved blocks from peers
// Must update sync state using `SetMissingBlockHeight`
ProcessBlock(block *typesCons.Block) error
func (m *stateSyncModule) GetModuleName() string {
return stateSyncModuleName
}

type SyncState interface {
// latest local height
LatestHeight() int64
// latest network height (from the aggregation of Peer Sync Meta)
LatestNetworkHeight() int64
// retrieve peer meta (actively updated through churn management)
GetPeers() []PeerSyncMeta
// returns ordered array of missing block heights
GetMissingBlockHeights() []int64
func (m *stateSyncModule) IsServerModEnabled() bool {
return m.serverMode
}

type BlockRequestMessage interface {
// the height the peer wants from the block store
GetHeight() int64
func (m *stateSyncModule) EnableServerMode() {
m.currentMode = Server
m.serverMode = true
}

type BlockResponseMessage interface {
// the bytes of the requested block from the block store
GetBlockBytes() []byte
func (m *stateSyncModule) HandleGetBlockResponse(*typesCons.GetBlockResponse) error {
//! TODO implement
return nil
}

// TODO: needs to be shared between P2P as the Churn Management Process updates this information
type PeerSyncMeta interface {
// the unique identifier associated with the peer
GetPeerID() string
// the maximum height the peer has in the block store
GetMaxHeight() int64
// the minimum height the peer has in the block store
GetMinHeight() int64
func (m *stateSyncModule) HandleStateSyncMetadataResponse(*typesCons.StateSyncMetadataResponse) error {
//! TODO implement
return nil
}
Loading