Unverified Commit 90d2f148 authored by vipwzw's avatar vipwzw Committed by GitHub

Merge pull request #761 from icehawk-hyb/issues-768

add by hyb for cmpbestblock
parents 1e0f7be7 57808324
......@@ -3,7 +3,7 @@ module github.com/33cn/plugin
go 1.12
require (
github.com/33cn/chain33 v0.0.0-20200103093520-2bdc0cea1093
github.com/33cn/chain33 v0.0.0-20200108042336-2dda2dfb7e0e
github.com/BurntSushi/toml v0.3.1
github.com/NebulousLabs/Sia v1.3.7
github.com/beorn7/perks v1.0.1 // indirect
......
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/33cn/chain33 v0.0.0-20200103093520-2bdc0cea1093 h1:ztzQN4V2DJL9b/rgx5lX3cip6A0er4560sKvt4vYBMM=
github.com/33cn/chain33 v0.0.0-20200103093520-2bdc0cea1093/go.mod h1:4I8n+Zyf3t0UKM5jjpqJY627Tub62oXkLsdzIv4r6rQ=
github.com/33cn/chain33 v0.0.0-20200108042336-2dda2dfb7e0e h1:FMnJCJUDEsjHURlHv3zqiiftvNCtTbsMSjXSykG7htc=
github.com/33cn/chain33 v0.0.0-20200108042336-2dda2dfb7e0e/go.mod h1:4I8n+Zyf3t0UKM5jjpqJY627Tub62oXkLsdzIv4r6rQ=
github.com/AndreasBriese/bbloom v0.0.0-20180913140656-343706a395b7 h1:PqzgE6kAMi81xWQA2QIVxjWkFHptGgC547vchpUbtFo=
github.com/AndreasBriese/bbloom v0.0.0-20180913140656-343706a395b7/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
......
......@@ -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,29 @@ 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)
//数字越小难度越大
return newBlockDiff.Cmp(cmpBlockDiff) < 0
}
......@@ -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