Skip to content

Commit

Permalink
Fixed some lines
Browse files Browse the repository at this point in the history
  • Loading branch information
18aaddy committed Oct 24, 2024
1 parent 4065a83 commit f5a6c2f
Showing 1 changed file with 25 additions and 15 deletions.
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

0 comments on commit f5a6c2f

Please sign in to comment.