@ -428,15 +428,29 @@ func (consensus *Consensus) getLeaderPubKeyFromCoinbase(header *block.Header) (*
)
)
}
}
committerKey := new ( bls . PublicKey )
committerKey := new ( bls . PublicKey )
isStaking := consensus . ChainReader . Config ( ) . IsStaking ( header . Epoch ( ) )
for _ , member := range committee . Slots {
for _ , member := range committee . Slots {
if member . EcdsaAddress == header . Coinbase ( ) {
if isStaking {
err := member . BlsPublicKey . ToLibBLSPublicKey ( committerKey )
// After staking the coinbase address will be the address of bls public key
if err != nil {
if utils . GetAddressFromBlsPubKeyBytes ( member . BlsPublicKey [ : ] ) == header . Coinbase ( ) {
return nil , ctxerror . New ( "cannot convert BLS public key" ,
err := member . BlsPublicKey . ToLibBLSPublicKey ( committerKey )
"blsPublicKey" , member . BlsPublicKey ,
if err != nil {
"coinbaseAddr" , header . Coinbase ( ) ) . WithCause ( err )
return nil , ctxerror . New ( "cannot convert BLS public key" ,
"blsPublicKey" , member . BlsPublicKey ,
"coinbaseAddr" , header . Coinbase ( ) ) . WithCause ( err )
}
return committerKey , nil
}
} else {
if member . EcdsaAddress == header . Coinbase ( ) {
err := member . BlsPublicKey . ToLibBLSPublicKey ( committerKey )
if err != nil {
return nil , ctxerror . New ( "cannot convert BLS public key" ,
"blsPublicKey" , member . BlsPublicKey ,
"coinbaseAddr" , header . Coinbase ( ) ) . WithCause ( err )
}
return committerKey , nil
}
}
return committerKey , nil
}
}
}
}
return nil , ctxerror . New ( "cannot find corresponding BLS Public Key" , "coinbaseAddr" , header . Coinbase ( ) )
return nil , ctxerror . New ( "cannot find corresponding BLS Public Key" , "coinbaseAddr" , header . Coinbase ( ) )
@ -453,25 +467,94 @@ func (consensus *Consensus) getLeaderPubKeyFromCoinbase(header *block.Header) (*
// (b) node in committed but has any err during processing: Syncing mode
// (b) node in committed but has any err during processing: Syncing mode
// (c) node in committed and everything looks good: Normal mode
// (c) node in committed and everything looks good: Normal mode
func ( consensus * Consensus ) UpdateConsensusInformation ( ) Mode {
func ( consensus * Consensus ) UpdateConsensusInformation ( ) Mode {
curHeader := consensus . ChainReader . CurrentHeader ( )
curEpoch := curHeader . Epoch ( )
nextEpoch := new ( big . Int ) . Add ( curHeader . Epoch ( ) , common . Big1 )
if ( consensus . ChainReader . Config ( ) . IsStaking ( nextEpoch ) && len ( curHeader . ShardState ( ) ) > 0 && ! consensus . ChainReader . Config ( ) . IsStaking ( curEpoch ) ) ||
( consensus . ChainReader . Config ( ) . IsStaking ( curEpoch ) && consensus . Decider . Policy ( ) != quorum . SuperMajorityStake ) {
prevSubCommitteeDump := consensus . Decider . JSON ( )
consensus . Decider = quorum . NewDecider ( quorum . SuperMajorityStake )
consensus . Decider . SetShardIDProvider ( func ( ) ( uint32 , error ) {
return consensus . ShardID , nil
} )
s , err := committee . WithStakingEnabled . ReadFromDB (
nextEpoch , consensus . ChainReader ,
)
if err != nil {
utils . Logger ( ) . Error ( ) .
Err ( err ) .
Uint32 ( "shard" , consensus . ShardID ) .
Msg ( "Error when reading staking committee" )
return Syncing
}
if _ , err := consensus . Decider . SetVoters (
s . FindCommitteeByID ( consensus . ShardID ) . Slots ,
) ; err != nil {
utils . Logger ( ) . Error ( ) .
Err ( err ) .
Uint32 ( "shard" , consensus . ShardID ) .
Msg ( "Error when updating voting power" )
return Syncing
}
utils . Logger ( ) . Info ( ) .
Uint64 ( "block-number" , curHeader . Number ( ) . Uint64 ( ) ) .
Uint64 ( "curEpoch" , curHeader . Epoch ( ) . Uint64 ( ) ) .
Uint32 ( "shard-id" , consensus . ShardID ) .
RawJSON ( "prev-subcommittee" , [ ] byte ( prevSubCommitteeDump ) ) .
RawJSON ( "current-subcommittee" , [ ] byte ( consensus . Decider . JSON ( ) ) ) .
Msg ( "changing committee" )
}
pubKeys := [ ] * bls . PublicKey { }
pubKeys := [ ] * bls . PublicKey { }
hasError := false
hasError := false
header := consensus . ChainReader . CurrentHeader ( )
epoch := header . Epoch ( )
// TODO: change GetCommitteePublicKeys to read from DB
curPubKeys := committee . WithStakingEnabled . ComputePublicKeys (
curShardState , err := committee . WithStakingEnabled . ReadFromDB (
epoch , consensus . ChainReader ,
curEpoch , consensus . ChainReader ,
) [ int ( header . ShardID ( ) ) ]
)
if err != nil {
utils . Logger ( ) . Error ( ) .
Err ( err ) .
Uint32 ( "shard" , consensus . ShardID ) .
Msg ( "Error retrieving current shard state" )
return Syncing
}
curCommittee := curShardState . FindCommitteeByID ( curHeader . ShardID ( ) )
curPubKeys := committee . WithStakingEnabled . GetCommitteePublicKeys (
curCommittee ,
)
consensus . numPrevPubKeys = len ( curPubKeys )
consensus . numPrevPubKeys = len ( curPubKeys )
consensus . getLogger ( ) . Info ( ) . Msg ( "[UpdateConsensusInformation] Updating....." )
consensus . getLogger ( ) . Info ( ) . Msg ( "[UpdateConsensusInformation] Updating....." )
if shard . Schedule . IsLastBlock ( header . Number ( ) . Uint64 ( ) ) {
if len ( curHeader . ShardState ( ) ) > 0 {
// increase epoch by one if it's the last block
// increase curEpoch by one if it's the last block
consensus . SetEpochNum ( epoch . Uint64 ( ) + 1 )
consensus . SetEpochNum ( curEpoch . Uint64 ( ) + 1 )
consensus . getLogger ( ) . Info ( ) . Uint64 ( "headerNum" , header . Number ( ) . Uint64 ( ) ) .
consensus . getLogger ( ) . Info ( ) . Uint64 ( "headerNum" , curHeader . Number ( ) . Uint64 ( ) ) .
Msg ( "[UpdateConsensusInformation] Epoch updated for next epoch" )
Msg ( "[UpdateConsensusInformation] Epoch updated for nextEpoch curEpoch" )
pubKeys = committee . WithStakingEnabled . ComputePublicKeys (
new ( big . Int ) . Add ( epoch , common . Big1 ) , consensus . ChainReader ,
nextShardState , err := committee . WithStakingEnabled . ReadFromDB (
) [ int ( header . ShardID ( ) ) ]
nextEpoch , consensus . ChainReader ,
)
if err != nil {
utils . Logger ( ) . Error ( ) .
Err ( err ) .
Uint32 ( "shard" , consensus . ShardID ) .
Msg ( "Error retrieving nextEpoch shard state" )
return Syncing
}
nextCommittee := nextShardState . FindCommitteeByID ( curHeader . ShardID ( ) )
pubKeys = committee . WithStakingEnabled . GetCommitteePublicKeys (
nextCommittee ,
)
} else {
} else {
consensus . SetEpochNum ( epoch . Uint64 ( ) )
consensus . SetEpochNum ( curE poch. Uint64 ( ) )
pubKeys = curPubKeys
pubKeys = curPubKeys
}
}
@ -488,10 +571,10 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode {
Msg ( "[UpdateConsensusInformation] Successfully updated public keys" )
Msg ( "[UpdateConsensusInformation] Successfully updated public keys" )
consensus . UpdatePublicKeys ( pubKeys )
consensus . UpdatePublicKeys ( pubKeys )
// take care of possible leader change during the e poch
// take care of possible leader change during the curE poch
if ! shard . Schedule . IsLastBlock ( h eader. Number ( ) . Uint64 ( ) ) &&
if ! shard . Schedule . IsLastBlock ( curH eader. Number ( ) . Uint64 ( ) ) &&
h eader. Number ( ) . Uint64 ( ) != 0 {
curH eader. Number ( ) . Uint64 ( ) != 0 {
leaderPubKey , err := consensus . getLeaderPubKeyFromCoinbase ( h eader)
leaderPubKey , err := consensus . getLeaderPubKeyFromCoinbase ( curH eader)
if err != nil || leaderPubKey == nil {
if err != nil || leaderPubKey == nil {
consensus . getLogger ( ) . Debug ( ) . Err ( err ) .
consensus . getLogger ( ) . Debug ( ) . Err ( err ) .
Msg ( "[SYNC] Unable to get leaderPubKey from coinbase" )
Msg ( "[SYNC] Unable to get leaderPubKey from coinbase" )