Commit f35ed63b authored by heyubin's avatar heyubin

add by hyb for cmpbestblock

parent cf531070
......@@ -784,3 +784,8 @@ func (client *Client) SetTestFlag() {
func (client *Client) GetNode() *Node {
return client.node
}
//比较newBlock是不是最优区块
func (client *Client) CmpBestBlock(newBlock *types.Block, cmpBlock *types.Block) bool {
return false
}
......@@ -473,3 +473,8 @@ func (client *client) Query_WalletStatus(walletStatus *types.WalletStatus) (type
client.commitMsgClient.onWalletStatus(walletStatus)
return &types.Reply{IsOk: true, Msg: []byte("OK")}, nil
}
//比较newBlock是不是最优区块
func (client *client) CmpBestBlock(newBlock *types.Block, cmpBlock *types.Block) bool {
return false
}
......@@ -146,3 +146,8 @@ func (client *Client) readReply() {
client.SetCurrentBlock(data.Result.Value)
}
//比较newBlock是不是最优区块
func (client *Client) CmpBestBlock(newBlock *types.Block, cmpBlock *types.Block) bool {
return false
}
......@@ -270,3 +270,8 @@ func (client *Client) pollingTask() {
}
}
}
//比较newBlock是不是最优区块
func (client *Client) CmpBestBlock(newBlock *types.Block, cmpBlock *types.Block) bool {
return false
}
......@@ -635,3 +635,8 @@ func (client *Client) Query_NodeInfo(req *types.ReqNil) (types.Message, error) {
}
return &tmtypes.ValidatorSet{Validators: validators, Proposer: &tmtypes.Validator{}}, nil
}
//比较newBlock是不是最优区块
func (client *Client) CmpBestBlock(newBlock *types.Block, cmpBlock *types.Block) bool {
return false
}
......@@ -72,6 +72,7 @@ minerstart=true
genesisBlockTime=1514533394
genesis="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
minerExecs=["ticket", "autonomy"]
enableBestBlockCmp=true
[mver.consensus]
fundKeyAddr = "1BQXS6TxaYYG5mADaWij4AxhZZUTpw95a5"
......
......@@ -795,3 +795,32 @@ func getTxHashes(txs []*types.Transaction) (hashes [][]byte) {
}
return hashes
}
//比较newBlock是不是最优区块,目前ticket主要是比较挖矿交易的难度系数
func (client *Client) CmpBestBlock(newBlock *types.Block, cmpBlock *types.Block) bool {
cfg := client.GetAPI().GetConfig()
//newblock挖矿交易的难度系数
newBlockTicket, err := client.getMinerTx(newBlock)
if err != nil {
tlog.Error("CmpBestBlock:getMinerTx", "newBlockHash", common.ToHex(newBlock.Hash(cfg)))
return false
}
newBlockMiner := newBlockTicket.GetMiner()
newBlockDiff := client.getCurrentTarget(newBlock.BlockTime, newBlockMiner.TicketId, newBlockMiner.Modify, newBlockMiner.PrivHash)
//cmpBlock挖矿交易的难度系数
cmpBlockTicket, err := client.getMinerTx(cmpBlock)
if err != nil {
tlog.Error("CmpBestBlock:getMinerTx", "cmpBlockHash", common.ToHex(cmpBlock.Hash(cfg)))
return false
}
cmpBlockMiner := cmpBlockTicket.GetMiner()
cmpBlockDiff := client.getCurrentTarget(cmpBlock.BlockTime, cmpBlockMiner.TicketId, cmpBlockMiner.Modify, cmpBlockMiner.PrivHash)
//数字越小难度越大
if newBlockDiff.Cmp(cmpBlockDiff) < 0 {
return true
}
return false
}
......@@ -22,6 +22,7 @@ import (
"github.com/stretchr/testify/assert"
apimocks "github.com/33cn/chain33/client/mocks"
"github.com/33cn/chain33/common/merkle"
_ "github.com/33cn/chain33/system"
drivers "github.com/33cn/chain33/system/consensus"
_ "github.com/33cn/plugin/plugin/dapp/init"
......@@ -103,6 +104,16 @@ func testTicket(t *testing.T) {
accounts, err = acc.GetBalance(mock33.GetAPI(), &types.ReqBalance{Execer: "ticket", Addresses: []string{addr}})
assert.Nil(t, err)
fmt.Println(accounts[0])
//测试最优节点的选择,难度相同
lastBlock := mock33.GetLastBlock()
temblock := types.Clone(lastBlock)
newblock := temblock.(*types.Block)
newblock.GetTxs()[0].Nonce = newblock.GetTxs()[0].Nonce + 1
newblock.TxHash = merkle.CalcMerkleRoot(cfg, newblock.GetHeight(), newblock.GetTxs())
isbestBlock := util.CmpBestBlock(mock33.GetClient(), newblock, lastBlock.Hash(cfg))
assert.Equal(t, isbestBlock, false)
}
func createBindMiner(cfg *types.Chain33Config, t *testing.T, m, r string, priv crypto.PrivKey) *types.Transaction {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment