Sort receipts list

pull/1359/head
Rongjian Lan 5 years ago
parent f16c11b3df
commit f20520ffa5
  1. 1
      cmd/client/wallet/main.go
  2. 6
      node/node_cross_shard.go
  3. 58
      node/node_newblock.go

@ -161,6 +161,7 @@ func main() {
fmt.Println(" --to - The receiver account's address")
fmt.Println(" --amount - The amount of token to transfer")
fmt.Println(" --shardID - The shard Id for the transfer")
fmt.Println(" --toShardID - The destination shard Id for the transfer")
fmt.Println(" --inputData - Base64-encoded input data to embed in the transaction")
fmt.Println(" --pass - Passphrase of sender's private key")
fmt.Println(" 8. export - Export account key to a new file")

@ -212,7 +212,7 @@ func (node *Node) ProcessCrossShardTx(blocks []*types.Block) {
}
// ProposeCrossLinkDataForBeaconchain propose cross links for beacon chain new block
func (node *Node) ProposeCrossLinkDataForBeaconchain() ([]byte, error) {
func (node *Node) ProposeCrossLinkDataForBeaconchain() (types.CrossLinks, error) {
curBlock := node.Blockchain().CurrentBlock()
numShards := core.ShardingSchedule.InstanceForEpoch(curBlock.Header().Epoch).NumShards()
@ -259,7 +259,7 @@ func (node *Node) ProposeCrossLinkDataForBeaconchain() ([]byte, error) {
if len(crossLinksToPropose) != 0 {
crossLinksToPropose.Sort()
return rlp.EncodeToBytes(crossLinksToPropose)
return crossLinksToPropose, nil
}
return []byte{}, errors.New("No cross link to propose")
return types.CrossLinks{}, errors.New("No cross link to propose")
}

@ -2,8 +2,11 @@ package node
import (
"math/big"
"sort"
"time"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/core"
@ -88,21 +91,7 @@ func (node *Node) WaitForConsensusReadyv2(readySignal chan struct{}, stopChan ch
}
// Propose cross shard receipts
receiptsList := []types.CXReceipts{}
node.pendingCXMutex.Lock()
for _, receiptMsg := range node.pendingCXReceipts {
sourceShardID := receiptMsg.MerkleProof.ShardID
sourceBlockNum := receiptMsg.MerkleProof.BlockNum
beaconChain := node.Blockchain() // TODO: read from real beacon chain
crossLink, err := beaconChain.ReadCrossLink(sourceShardID, sourceBlockNum.Uint64(), false)
if err == nil {
if crossLink.ChainHeader.Hash() == receiptMsg.MerkleProof.BlockHash && crossLink.ChainHeader.OutgoingReceiptHash == receiptMsg.MerkleProof.CXReceiptHash {
receiptsList = append(receiptsList, receiptMsg.Receipts)
}
}
}
node.pendingCXMutex.Unlock()
receiptsList := node.proposeReceipts()
if len(receiptsList) != 0 {
if err := node.Worker.CommitReceipts(receiptsList, coinbase); err != nil {
ctxerror.Log15(utils.GetLogger().Error,
@ -112,13 +101,16 @@ func (node *Node) WaitForConsensusReadyv2(readySignal chan struct{}, stopChan ch
}
if node.NodeConfig.ShardID == 0 {
data, err := node.ProposeCrossLinkDataForBeaconchain()
crossLinksToPropose, err := node.ProposeCrossLinkDataForBeaconchain()
if err == nil {
newBlock, err = node.Worker.CommitWithCrossLinks(sig, mask, viewID, coinbase, data)
utils.Logger().Debug().
Uint64("blockNum", newBlock.NumberU64()).
Int("numCrossLinks", len(data)).
Msg("Successfully added cross links into new block")
data, err := rlp.EncodeToBytes(crossLinksToPropose)
if err == nil {
newBlock, err = node.Worker.CommitWithCrossLinks(sig, mask, viewID, coinbase, data)
utils.Logger().Debug().
Uint64("blockNum", newBlock.NumberU64()).
Int("numCrossLinks", len(data)).
Msg("Successfully added cross links into new block")
}
} else {
newBlock, err = node.Worker.Commit(sig, mask, viewID, coinbase)
}
@ -219,3 +211,27 @@ func (node *Node) proposeLocalShardState(block *types.Block) {
logger.Error().Err(err).Msg("Failed proposin local shard state")
}
}
func (node *Node) proposeReceipts() []types.CXReceipts {
receiptsList := []types.CXReceipts{}
node.pendingCXMutex.Lock()
sort.Slice(node.pendingCXReceipts, func(i, j int) bool {
return node.pendingCXReceipts[i].MerkleProof.ShardID < node.pendingCXReceipts[j].MerkleProof.ShardID || (node.pendingCXReceipts[i].MerkleProof.ShardID == node.pendingCXReceipts[j].MerkleProof.ShardID && node.pendingCXReceipts[i].MerkleProof.BlockNum.Cmp(node.pendingCXReceipts[j].MerkleProof.BlockNum) < 0)
})
for _, receiptMsg := range node.pendingCXReceipts {
sourceShardID := receiptMsg.MerkleProof.ShardID
sourceBlockNum := receiptMsg.MerkleProof.BlockNum
beaconChain := node.Blockchain() // TODO: read from real beacon chain
crossLink, err := beaconChain.ReadCrossLink(sourceShardID, sourceBlockNum.Uint64(), false)
if err == nil {
if crossLink.ChainHeader.Hash() == receiptMsg.MerkleProof.BlockHash && crossLink.ChainHeader.OutgoingReceiptHash == receiptMsg.MerkleProof.CXReceiptHash {
receiptsList = append(receiptsList, receiptMsg.Receipts)
}
}
}
node.pendingCXMutex.Unlock()
return receiptsList
}

Loading…
Cancel
Save