@ -339,59 +339,13 @@ func (node *Node) BroadcastCXReceipts(newBlock *types.Block) {
func ( node * Node ) VerifyNewBlock ( newBlock * types . Block ) error {
func ( node * Node ) VerifyNewBlock ( newBlock * types . Block ) error {
// TODO ek – where do we verify parent-child invariants,
// TODO ek – where do we verify parent-child invariants,
// e.g. "child.Number == child.IsGenesis() ? 0 : parent.Number+1"?
// e.g. "child.Number == child.IsGenesis() ? 0 : parent.Number+1"?
// Verify lastCommitSig
if newBlock . NumberU64 ( ) > 1 {
header := newBlock . Header ( )
parentBlock := node . Blockchain ( ) . GetBlockByNumber ( newBlock . NumberU64 ( ) - 1 )
if parentBlock == nil {
return ctxerror . New ( "[VerifyNewBlock] Failed to get parent block" , "shardID" , header . ShardID , "blockNum" , header . Number )
}
parentHeader := parentBlock . Header ( )
shardState , err := node . Blockchain ( ) . ReadShardState ( parentHeader . Epoch )
committee := shardState . FindCommitteeByID ( parentHeader . ShardID )
if err != nil || committee == nil {
return ctxerror . New ( "[VerifyNewBlock] Failed to read shard state for cross link header" , "shardID" , header . ShardID , "blockNum" , header . Number ) . WithCause ( err )
}
var committerKeys [ ] * bls . PublicKey
parseKeysSuccess := true
for _ , member := range committee . NodeList {
committerKey := new ( bls . PublicKey )
err = member . BlsPublicKey . ToLibBLSPublicKey ( committerKey )
if err != nil {
parseKeysSuccess = false
break
}
committerKeys = append ( committerKeys , committerKey )
}
if ! parseKeysSuccess {
return ctxerror . New ( "[VerifyNewBlock] cannot convert BLS public key" , "shardID" , header . ShardID , "blockNum" , header . Number ) . WithCause ( err )
}
mask , err := bls_cosi . NewMask ( committerKeys , nil )
if err != nil {
return ctxerror . New ( "[VerifyNewBlock] cannot create group sig mask" , "shardID" , header . ShardID , "blockNum" , header . Number ) . WithCause ( err )
}
if err := mask . SetMask ( header . LastCommitBitmap ) ; err != nil {
return ctxerror . New ( "[VerifyNewBlock] cannot set group sig mask bits" , "shardID" , header . ShardID , "blockNum" , header . Number ) . WithCause ( err )
}
aggSig := bls . Sign { }
if newBlock . NumberU64 ( ) > 1 {
err = aggSig . Deserialize ( header . LastCommitSignature [ : ] )
err := node . VerifyBlockLastCommitSigs ( newBlock )
if err != nil {
if err != nil {
return ctxerror . New ( "[VerifyNewBlock] unable to deserialize multi-signature from payload" ) . WithCause ( err )
return err
}
blockNumBytes := make ( [ ] byte , 8 )
binary . LittleEndian . PutUint64 ( blockNumBytes , header . Number . Uint64 ( ) - 1 )
commitPayload := append ( blockNumBytes , header . ParentHash [ : ] ... )
if ! aggSig . VerifyHash ( mask . AggregatePublic , commitPayload ) {
return ctxerror . New ( "[VerifyNewBlock] Failed to verify the signature for last commit sig" , "shardID" , header . ShardID , "blockNum" , header . Number )
}
}
}
}
// End Verify lastCommitSig
if newBlock . ShardID ( ) != node . Blockchain ( ) . ShardID ( ) {
if newBlock . ShardID ( ) != node . Blockchain ( ) . ShardID ( ) {
return ctxerror . New ( "wrong shard ID" ,
return ctxerror . New ( "wrong shard ID" ,
"my shard ID" , node . Blockchain ( ) . ShardID ( ) ,
"my shard ID" , node . Blockchain ( ) . ShardID ( ) ,
@ -406,63 +360,138 @@ func (node *Node) VerifyNewBlock(newBlock *types.Block) error {
}
}
// Verify cross links
// Verify cross links
if node . NodeConfig . ShardID == 0 && len ( newBlock . Header ( ) . CrossLinks ) > 0 {
if node . NodeConfig . ShardID == 0 {
err := node . VerifyBlockCrossLinks ( newBlock )
if err != nil {
return err
}
}
// TODO: verify the vrf randomness
// _ = newBlock.Header().Vrf
// TODO: uncomment 4 lines after we finish staking mechanism
//err = node.validateNewShardState(newBlock, &node.CurrentStakes)
// if err != nil {
// return ctxerror.New("failed to verify sharding state").WithCause(err)
// }
return nil
}
// VerifyBlockCrossLinks verifies the cross links of the block
func ( node * Node ) VerifyBlockCrossLinks ( block * types . Block ) error {
if len ( block . Header ( ) . CrossLinks ) == 0 {
return nil
}
crossLinks := & types . CrossLinks { }
crossLinks := & types . CrossLinks { }
err := rlp . DecodeBytes ( newBlock . Header ( ) . CrossLinks , crossLinks )
err := rlp . DecodeBytes ( b lock. Header ( ) . CrossLinks , crossLinks )
if err != nil {
if err != nil {
return ctxerror . New ( "[CrossLinkVerification] failed to decode cross links" ,
return ctxerror . New ( "[CrossLinkVerification] failed to decode cross links" ,
"blockHash" , newBlock . Hash ( ) ,
"blockHash" , b lock. Hash ( ) ,
"crossLinks" , len ( newBlock . Header ( ) . CrossLinks ) ,
"crossLinks" , len ( b lock. Header ( ) . CrossLinks ) ,
) . WithCause ( err )
) . WithCause ( err )
}
}
if ! crossLinks . IsSorted ( ) {
return ctxerror . New ( "[CrossLinkVerification] cross links are not sorted" ,
"blockHash" , block . Hash ( ) ,
"crossLinks" , len ( block . Header ( ) . CrossLinks ) ,
)
}
firstCrossLinkBlock := core . ShardingSchedule . FirstCrossLinkBlock ( )
for i , crossLink := range * crossLinks {
for i , crossLink := range * crossLinks {
lastLink := & types . CrossLink { }
lastLink := & types . CrossLink { }
if i == 0 {
if i == 0 {
if crossLink . BlockNum ( ) . Uint64 ( ) > 0 {
if crossLink . BlockNum ( ) . Uint64 ( ) > firstCrossLinkBlock {
lastLink , err = node . Blockchain ( ) . ReadShardLastCrossLink ( crossLink . ShardID ( ) )
lastLink , err = node . Blockchain ( ) . ReadShardLastCrossLink ( crossLink . ShardID ( ) )
if err != nil {
if err != nil {
return ctxerror . New ( "[CrossLinkVerification] no last cross link found 1" ,
return ctxerror . New ( "[CrossLinkVerification] no last cross link found 1" ,
"blockHash" , newBlock . Hash ( ) ,
"blockHash" , b lock. Hash ( ) ,
"crossLink" , lastLink ,
"crossLink" , lastLink ,
) . WithCause ( err )
) . WithCause ( err )
}
}
} else {
lastLink = & crossLink
}
}
} else {
} else {
if ( * crossLinks ) [ i - 1 ] . Header ( ) . ShardID != crossLink . Header ( ) . ShardID {
if ( * crossLinks ) [ i - 1 ] . Header ( ) . ShardID != crossLink . Header ( ) . ShardID {
if crossLink . BlockNum ( ) . Uint64 ( ) > firstCrossLinkBlock {
lastLink , err = node . Blockchain ( ) . ReadShardLastCrossLink ( crossLink . ShardID ( ) )
lastLink , err = node . Blockchain ( ) . ReadShardLastCrossLink ( crossLink . ShardID ( ) )
if err != nil {
if err != nil {
return ctxerror . New ( "[CrossLinkVerification] no last cross link found 2" ,
return ctxerror . New ( "[CrossLinkVerification] no last cross link found 2" ,
"blockHash" , newB lock. Hash ( ) ,
"blockHash" , b lock. Hash ( ) ,
"crossLink" , lastLink ,
"crossLink" , lastLink ,
) . WithCause ( err )
) . WithCause ( err )
}
}
}
} else {
} else {
lastLink = & ( * crossLinks ) [ i - 1 ]
lastLink = & ( * crossLinks ) [ i - 1 ]
}
}
}
}
if crossLink . BlockNum ( ) . Uint64 ( ) != 0 { // TODO: verify genesis block
if crossLink . BlockNum ( ) . Uint64 ( ) > firstCrossLinkBlock { // TODO: verify genesis block
err = node . VerifyCrosslinkHeader ( lastLink . Header ( ) , crossLink . Header ( ) )
err = node . VerifyCrosslinkHeader ( lastLink . Header ( ) , crossLink . Header ( ) )
if err != nil {
if err != nil {
return ctxerror . New ( "cannot ValidateNewBlock" ,
return ctxerror . New ( "cannot ValidateNewBlock" ,
"blockHash" , newB lock. Hash ( ) ,
"blockHash" , b lock. Hash ( ) ,
"numTx" , len ( newB lock. Transactions ( ) ) ,
"numTx" , len ( b lock. Transactions ( ) ) ,
) . WithCause ( err )
) . WithCause ( err )
}
}
}
}
}
}
return nil
}
}
// TODO: verify the vrf randomness
// VerifyBlockLastCommitSigs verifies the last commit sigs of the block
// _ = newBlock.Header().Vrf
func ( node * Node ) VerifyBlockLastCommitSigs ( block * types . Block ) error {
header := block . Header ( )
parentBlock := node . Blockchain ( ) . GetBlockByNumber ( block . NumberU64 ( ) - 1 )
if parentBlock == nil {
return ctxerror . New ( "[VerifyNewBlock] Failed to get parent block" , "shardID" , header . ShardID , "blockNum" , header . Number )
}
parentHeader := parentBlock . Header ( )
shardState , err := node . Blockchain ( ) . ReadShardState ( parentHeader . Epoch )
committee := shardState . FindCommitteeByID ( parentHeader . ShardID )
// TODO: uncomment 4 lines after we finish staking mechanism
if err != nil || committee == nil {
//err = node.validateNewShardState(newBlock, &node.CurrentStakes)
return ctxerror . New ( "[VerifyNewBlock] Failed to read shard state for cross link header" , "shardID" , header . ShardID , "blockNum" , header . Number ) . WithCause ( err )
// if err != nil {
}
// return ctxerror.New("failed to verify sharding state").WithCause(err)
var committerKeys [ ] * bls . PublicKey
// }
parseKeysSuccess := true
for _ , member := range committee . NodeList {
committerKey := new ( bls . PublicKey )
err = member . BlsPublicKey . ToLibBLSPublicKey ( committerKey )
if err != nil {
parseKeysSuccess = false
break
}
committerKeys = append ( committerKeys , committerKey )
}
if ! parseKeysSuccess {
return ctxerror . New ( "[VerifyNewBlock] cannot convert BLS public key" , "shardID" , header . ShardID , "blockNum" , header . Number ) . WithCause ( err )
}
mask , err := bls_cosi . NewMask ( committerKeys , nil )
if err != nil {
return ctxerror . New ( "[VerifyNewBlock] cannot create group sig mask" , "shardID" , header . ShardID , "blockNum" , header . Number ) . WithCause ( err )
}
if err := mask . SetMask ( header . LastCommitBitmap ) ; err != nil {
return ctxerror . New ( "[VerifyNewBlock] cannot set group sig mask bits" , "shardID" , header . ShardID , "blockNum" , header . Number ) . WithCause ( err )
}
aggSig := bls . Sign { }
err = aggSig . Deserialize ( header . LastCommitSignature [ : ] )
if err != nil {
return ctxerror . New ( "[VerifyNewBlock] unable to deserialize multi-signature from payload" ) . WithCause ( err )
}
blockNumBytes := make ( [ ] byte , 8 )
binary . LittleEndian . PutUint64 ( blockNumBytes , header . Number . Uint64 ( ) - 1 )
commitPayload := append ( blockNumBytes , header . ParentHash [ : ] ... )
if ! aggSig . VerifyHash ( mask . AggregatePublic , commitPayload ) {
return ctxerror . New ( "[VerifyNewBlock] Failed to verify the signature for last commit sig" , "shardID" , header . ShardID , "blockNum" , header . Number )
}
return nil
return nil
}
}