add crosslink verification

pull/1402/head
chao 5 years ago
parent d48e1472e1
commit 68b7c7c4c5
  1. 1
      core/blockchain.go
  2. 12
      core/types/block.go
  3. 1
      core/types/cx_receipt.go
  4. 20
      node/node_cross_shard.go
  5. 31
      node/node_newblock.go

@ -1137,6 +1137,7 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
return n, errors.New("proposed cross links are not sorted")
}
for _, crossLink := range *crossLinks {
utils.Logger().Info().Interface("crosslink", crossLink).Msg("hehehe")
bc.WriteCrossLinks(types.CrossLinks{crossLink}, false)
bc.WriteShardLastCrossLink(crossLink.ShardID(), crossLink)
}

@ -301,10 +301,14 @@ func CopyHeader(h *Header) *Header {
cpy.Vdf = make([]byte, len(h.Vdf))
copy(cpy.Vdf, h.Vdf)
}
//if len(h.CrossLinks) > 0 {
// cpy.CrossLinks = make([]byte, len(h.CrossLinks))
// copy(cpy.CrossLinks, h.CrossLinks)
//}
if len(h.CrossLinks) > 0 {
cpy.CrossLinks = make([]byte, len(h.CrossLinks))
copy(cpy.CrossLinks, h.CrossLinks)
}
if len(h.LastCommitBitmap) > 0 {
cpy.LastCommitBitmap = make([]byte, len(h.LastCommitBitmap))
copy(cpy.LastCommitBitmap, h.LastCommitBitmap)
}
return &cpy
}

@ -132,7 +132,6 @@ func (cxp *CXReceiptsProof) GetToShardID() (uint32, error) {
}
// IsValidCXReceiptsProof checks whether the given CXReceiptsProof is consistency with itself
// Remaining to check whether there is a corresonding block finalized
func (cxp *CXReceiptsProof) IsValidCXReceiptsProof() error {
toShardID, err := cxp.GetToShardID()
if err != nil {

@ -101,10 +101,26 @@ func (node *Node) verifyIncomingReceipts(block *types.Block) error {
if _, ok := m[hash]; ok {
return ctxerror.New("[verifyIncomingReceipts] Double Spent!")
}
m[hash] = true
sourceShardID := cxp.MerkleProof.ShardID
sourceBlockNum := cxp.MerkleProof.BlockNum
beaconChain := node.Beaconchain() // TODO: read from real beacon chain
crossLink, err := beaconChain.ReadCrossLink(sourceShardID, sourceBlockNum.Uint64(), false)
if err == nil {
// verify the source block hash is from a finalized block
if crossLink.ChainHeader.Hash() == cxp.MerkleProof.BlockHash && crossLink.ChainHeader.OutgoingReceiptHash == cxp.MerkleProof.CXReceiptHash {
utils.Logger().Debug().Msg("hehe00")
continue
} else {
utils.Logger().Debug().Msg("hehe11")
return ctxerror.New("[verifyIncomingReceipts] crosslink verification failed")
}
} else {
utils.Logger().Debug().Msg("hehe22")
return ctxerror.New("[verifyIncomingReceipts] crosslink not exists yet")
}
}
// TODO: add crosslink blockHeaderHash checking
return nil
}

@ -216,18 +216,6 @@ func (node *Node) proposeReceiptsProof() []*types.CXReceiptsProof {
m := make(map[common.Hash]bool)
for _, cxp := range node.pendingCXReceipts {
//sourceShardID := cxp.MerkleProof.ShardID
//sourceBlockNum := cxp.MerkleProof.BlockNum
//
// beaconChain := node.Blockchain() // TODO: read from real beacon chain
// crossLink, err := beaconChain.ReadCrossLink(sourceShardID, sourceBlockNum.Uint64(), false)
// if err == nil {
// // verify the source block hash is from a finalized block
// if crossLink.ChainHeader.Hash() == cxp.MerkleProof.BlockHash && crossLink.ChainHeader.OutgoingReceiptHash == cxp.MerkleProof.CXReceiptHash {
// receiptsList = append(receiptsList, cxp.Receipts)
// }
// }
// check double spent
if node.Blockchain().IsSpent(cxp) {
continue
@ -240,8 +228,23 @@ func (node *Node) proposeReceiptsProof() []*types.CXReceiptsProof {
m[hash] = true
}
// TODO: remove it after beacon chain sync is ready, for pass the test only
validReceiptsList = append(validReceiptsList, cxp)
sourceShardID := cxp.MerkleProof.ShardID
sourceBlockNum := cxp.MerkleProof.BlockNum
beaconChain := node.Beaconchain() // TODO: read from real beacon chain
crossLink, err := beaconChain.ReadCrossLink(sourceShardID, sourceBlockNum.Uint64(), false)
if err == nil {
// verify the source block hash is from a finalized block
if crossLink.ChainHeader.Hash() == cxp.MerkleProof.BlockHash && crossLink.ChainHeader.OutgoingReceiptHash == cxp.MerkleProof.CXReceiptHash {
utils.Logger().Debug().Msg("hehe0")
validReceiptsList = append(validReceiptsList, cxp)
} else {
utils.Logger().Debug().Msg("hehe1")
}
} else {
utils.Logger().Debug().Msg("hehe2")
pendingReceiptsList = append(pendingReceiptsList, cxp)
}
}
node.pendingCXReceipts = pendingReceiptsList
node.pendingCXMutex.Unlock()

Loading…
Cancel
Save