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

Fixes #77 (NewConsensusClient function) #78

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
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
40 changes: 25 additions & 15 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ type GenericUpdate struct {
FinalizedHeader consensus_core.Header
FinalityBranch []consensus_core.Bytes32
}

// The consensus client fields are updated because state takes chan *common.Block,
// not common.Block
type ConsensusClient struct {
BlockRecv *common.Block
FinalizedBlockRecv *common.Block
BlockRecv chan *common.Block
FinalizedBlockRecv chan *common.Block
CheckpointRecv *[]byte
genesisTime uint64
db Database
Expand All @@ -71,7 +72,7 @@ type Inner struct {
RPC rpc.ConsensusRpc
Store LightClientStore
lastCheckpoint *[]byte
blockSend chan common.Block
blockSend chan *common.Block
finalizedBlockSend chan *common.Block
checkpointSend chan *[]byte
Config *config.Config
Expand All @@ -86,22 +87,33 @@ type LightClientStore struct {
}

func (con ConsensusClient) New(rpc *string, config config.Config) ConsensusClient {
blockSend := make(chan common.Block, 256)
blockSend := make(chan *common.Block, 256)
finalizedBlockSend := make(chan *common.Block)
checkpointSend := make(chan *[]byte)

db, err := con.db.New(&config)
// Here the initialisation of database was wrong as it was pointing to an interface,
// not a types that implements the interface. This resulted in an invalid address error
_db, err := (&ConfigDB{}).New(&config)
if err != nil {
panic(err)
}

// Doing this makes the db variable a concrete type (ConfigDB here)
// It can also be switched to FileDB if needed
db, ok := _db.(*ConfigDB)
if !ok {
panic(errors.New("Expected ConfigDB instance"))
}
var initialCheckpoint [32]byte

// There was a problem in this assignment initially as it was setting the checkpoint
// to db.LoadCheckpoint() when the user inputed a checkpoint. This updated one is
// according to Helios.
if config.Checkpoint != nil {
initialCheckpoint = *config.Checkpoint
} else {
initialNewCheckpoint, errorWhileLoadingCheckpoint := db.LoadCheckpoint()
copy(initialCheckpoint[:], initialNewCheckpoint)
if errorWhileLoadingCheckpoint != nil {
log.Printf("error while loading checkpoint: %v", errorWhileLoadingCheckpoint)
initialCheckpoint = config.DefaultCheckpoint
}
}
if initialCheckpoint == [32]byte{} {
Expand Down Expand Up @@ -150,13 +162,11 @@ func (con ConsensusClient) New(rpc *string, config config.Config) ConsensusClien
}
}()

blocksReceived := <-blockSend
finalizedBlocksReceived := <-finalizedBlockSend
checkpointsReceived := <-checkpointSend

return ConsensusClient{
BlockRecv: &blocksReceived,
FinalizedBlockRecv: finalizedBlocksReceived,
BlockRecv: blockSend,
FinalizedBlockRecv: finalizedBlockSend,
CheckpointRecv: checkpointsReceived,
genesisTime: config.Chain.GenesisTime,
db: db,
Expand Down Expand Up @@ -225,7 +235,7 @@ func sync_all_fallback(inner *Inner, chainID uint64) error {
return nil
}

func (in *Inner) New(rpcURL string, blockSend chan common.Block, finalizedBlockSend chan *common.Block, checkpointSend chan *[]byte, config *config.Config) *Inner {
func (in *Inner) New(rpcURL string, blockSend chan *common.Block, finalizedBlockSend chan *common.Block, checkpointSend chan *[]byte, config *config.Config) *Inner {
rpcClient := rpc.NewConsensusRpc(rpcURL)

return &Inner{
Expand Down Expand Up @@ -459,7 +469,7 @@ func (in *Inner) send_blocks() error {
log.Printf("Error converting payload to block: %v", err)
return
}
in.blockSend <- *block
in.blockSend <- block
}()

go func() {
Expand Down
Loading