Sync architecture

Almog De Paz edited this page Jan 7, 2019 · 9 revisions

Package Sync


a node in the spacemesh network must keep its local block database synced in order to participate in the spacemesh protocol.

the sync package contains the modules needed for keeping the local block database synchronised,

sync depends on these packages in the project:

  p2p -  interface for interacting with peers and. 
  mesh - interface for interacting with the local block database.  
  protobuf - message serialisation 

sync protocol sequence diagram


All messages are comprised of Req and Resp couples (marked by suffix)

	message FetchBlockReq {
  		   uint32 Id;

	message FetchBlockResp {
		Block block;

	message LayerHashReq {
		  uint32 layer;

	message LayerHashResp {
		bytes hash;

	message LayerIdsReq {
		  uint32 layer;

	message LayerIdsResp {
	          repeated  uint32 ids;

	message Block {
		uint32 Id;
		uint32 layer;
		repeated uint32 VisibleMesh;

Public API

  • Interface Mesh -

    an interface that represents the methods needed for interacting with the local block database

     type Mesh interface {
        AddLayer(layer *Layer) error
        GetLayer(i LayerID) (*Layer, error)
        GetBlock(id BlockID) (*Block, error)
        AddBlock(block *Block) error
        GetContextualValidity(id BlockID) (bool, error)
        LatestLocalLayer() uint32
        LatestLayer() uint32
        SetLatestLayer(idx uint32)
  • Interface BlockValidator
     type BlockValidator interface {
          CheckSyntacticValidty(block Block) bool


  • Struct Syncer
       type Syncer struct {
        SyncLock  uint32
        startLock uint32
        forceSync chan bool
        exit      chan struct{}


  * func NewSync(srv server.Service, layers mesh.Mesh, bv BlockValidator, conf Configuration, log logging.Logger) *Syncer 

All of Sync's methods.

  * Start() - this starts the service basically running a gorutine that runs the sync when needed (timer / force sync)   
  * Close() - closes all resources related to Sync 

  * ForceSync() chan bool - force a new sync
  * getLayerHashes(index mesh.LayerID) (map[string]Peer, error) - gets hash of layer block ids from each peer

  * getIdsForHash(m map[string]Peer, index mesh.LayerID) (chan mesh.BlockID, error) -gets block ids for each layer hash

  * getBlockById(id string) Block - gets a block by its id 

  * CheckSyntacticValidty(block Block) bool - validates block 
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.