From: mars Date: Sat, 23 Mar 2019 06:07:46 +0000 (+0800) Subject: fix bug X-Git-Tag: v1.0.5~218^2~1 X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=commitdiff_plain;h=6375289bdf8d263dbfa00833ef982714c9c8c87f fix bug --- diff --git a/account/accounts_test.go b/account/accounts_test.go index b1710df1..8ef5ea32 100644 --- a/account/accounts_test.go +++ b/account/accounts_test.go @@ -14,8 +14,6 @@ import ( "github.com/vapor/common" "github.com/vapor/config" "github.com/vapor/consensus" - engine "github.com/vapor/consensus/consensus" - dpos "github.com/vapor/consensus/consensus/dpos" "github.com/vapor/crypto/ed25519/chainkd" "github.com/vapor/database/leveldb" "github.com/vapor/errors" @@ -233,12 +231,7 @@ func mockAccountManager(t *testing.T) *Manager { store := leveldb.NewStore(testDB) txPool := protocol.NewTxPool(store) - var engine engine.Engine - switch config.CommonConfig.Consensus.Type { - case "dpos": - engine = dpos.GDpos - } - chain, err := protocol.NewChain(store, txPool, engine) + chain, err := protocol.NewChain(store, txPool) if err != nil { t.Fatal(err) } diff --git a/asset/asset_test.go b/asset/asset_test.go index fe85e21b..33b1445e 100644 --- a/asset/asset_test.go +++ b/asset/asset_test.go @@ -14,8 +14,6 @@ import ( "github.com/vapor/common" "github.com/vapor/config" "github.com/vapor/consensus" - engine "github.com/vapor/consensus/consensus" - "github.com/vapor/consensus/consensus/dpos" "github.com/vapor/crypto/ed25519/chainkd" "github.com/vapor/database/leveldb" "github.com/vapor/protocol" @@ -155,14 +153,9 @@ func TestListAssets(t *testing.T) { } func mockChain(testDB dbm.DB) (*protocol.Chain, error) { - var engine engine.Engine - switch config.CommonConfig.Consensus.Type { - case "dpos": - engine = dpos.GDpos - } store := leveldb.NewStore(testDB) txPool := protocol.NewTxPool(store) - chain, err := protocol.NewChain(store, txPool, engine) + chain, err := protocol.NewChain(store, txPool) if err != nil { return nil, err } diff --git a/cmd/vapor/consensus.json b/cmd/vapor/consensus.json index 7ed772f4..338639b5 100644 --- a/cmd/vapor/consensus.json +++ b/cmd/vapor/consensus.json @@ -2,7 +2,7 @@ "consensus":{ "consensus_type": "dpos" , "period": 3, - "max_signers_count": 2, + "max_signers_count": 7, "min_boter_balance": 10, "genesis_timestamp": 1524549600, "coinbase": "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep", diff --git a/consensus/consensus/dpos/dpos.go b/consensus/consensus/dpos/dpos.go index 69abaf01..04dfd142 100644 --- a/consensus/consensus/dpos/dpos.go +++ b/consensus/consensus/dpos/dpos.go @@ -97,9 +97,6 @@ var GDpos = &DposType{ func (d *DposType) Init(c chain.Chain, delegateNumber, intervalTime, blockHeight uint64, blockHash bc.Hash) error { d.c = c vote, err := newVote(blockHeight, blockHash) - if err != nil { - return err - } d.vote = vote d.MaxDelegateNumber = delegateNumber d.BlockIntervalTime = intervalTime @@ -112,7 +109,7 @@ func (d *DposType) Init(c chain.Chain, delegateNumber, intervalTime, blockHeight GDpos.ReadIrreversibleBlockInfo(&GDpos.irreversibleBlockInfo) header, _ := c.GetHeaderByHeight(d.DposStartHeight) d.setStartTime(header.Timestamp) - return nil + return err } func (d *DposType) setStartTime(t uint64) { @@ -191,7 +188,7 @@ func (d *DposType) GetNextDelegates(t uint64) interface{} { } delegates = append(delegates, delegate) delegateInfo := DelegateInfo{} - delegateInfo.Delegates = SortDelegate(delegates, t) + delegateInfo.Delegates = delegates //SortDelegate(delegates, t) return &delegateInfo } @@ -373,6 +370,7 @@ func (d *DposType) CheckBlockDelegate(block types.Block) error { if len(delegateInfo.Delegates) != len(nextDelegateInfo.Delegates) { return errors.New("The delegates num is not correct in block") } + for index, v := range delegateInfo.Delegates { if v.DelegateAddress != nextDelegateInfo.Delegates[index].DelegateAddress { return errors.New("The delegates address is not correct in block") diff --git a/consensus/consensus/dpos/vote.go b/consensus/consensus/dpos/vote.go index c7056743..1fa7e806 100644 --- a/consensus/consensus/dpos/vote.go +++ b/consensus/consensus/dpos/vote.go @@ -1,13 +1,13 @@ package dpos import ( + "bytes" "encoding/json" "fmt" "io/ioutil" "os" "path/filepath" "sort" - "strings" "sync" cmn "github.com/tendermint/tmlibs/common" @@ -55,17 +55,6 @@ type Vote struct { oldBlockHash bc.Hash } -/* -var DposVote = Vote{ - DelegateVoters: make(map[string]map[string]bool), - VoterDelegates: make(map[string]map[string]bool), - DelegateName: make(map[string]string), - NameDelegate: make(map[string]string), - HashHeightInvalidVote: make(map[bc.Hash]uint64), - AddressBalances: make(map[string]uint64), - DelegateMultiaddress: make(map[string]uint64), -} -*/ func newVote(blockHeight uint64, blockHash bc.Hash) (*Vote, error) { vote := &Vote{ DelegateVoters: make(map[string]map[string]bool), @@ -77,11 +66,8 @@ func newVote(blockHeight uint64, blockHash bc.Hash) (*Vote, error) { DelegateMultiaddress: make(map[string]uint64), } - if err := vote.New(blockHeight, blockHash); err != nil { - return nil, err - } - - return vote, nil + err := vote.New(blockHeight, blockHash) + return vote, err } func (v *Vote) New(blockHeight uint64, blockHash bc.Hash) error { @@ -377,12 +363,8 @@ func (v *Vote) GetTopDelegateInfo(minHoldBalance uint64, delegateNum uint64) []D return false } else if p.Votes > q.Votes { return true - } else { - if strings.Compare(p.DelegateAddress, p.DelegateAddress) >= 0 { - return false - } } - return true + return bytes.Compare([]byte(p.DelegateAddress), []byte(q.DelegateAddress)) > 0 }}) for k := range v.DelegateName { diff --git a/mining/miner/miner.go b/mining/miner/miner.go index 378a78e5..6f4f7753 100644 --- a/mining/miner/miner.go +++ b/mining/miner/miner.go @@ -11,7 +11,6 @@ import ( "github.com/vapor/account" "github.com/vapor/common" "github.com/vapor/consensus" - engine "github.com/vapor/consensus/consensus" "github.com/vapor/mining" "github.com/vapor/protocol" "github.com/vapor/protocol/bc" @@ -37,10 +36,9 @@ type Miner struct { updateNumWorkers chan struct{} quit chan struct{} newBlockCh chan *bc.Hash - engine engine.Engine } -func NewMiner(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool, newBlockCh chan *bc.Hash, engine engine.Engine) *Miner { +func NewMiner(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool, newBlockCh chan *bc.Hash) *Miner { return &Miner{ chain: c, accountManager: accountManager, @@ -48,7 +46,6 @@ func NewMiner(c *protocol.Chain, accountManager *account.Manager, txPool *protoc numWorkers: defaultNumWorkers, updateNumWorkers: make(chan struct{}), newBlockCh: newBlockCh, - engine: engine, } } @@ -73,12 +70,13 @@ out: err error ) address, _ := common.DecodeAddress(config.CommonConfig.Consensus.Coinbase, &consensus.ActiveNetParams) - if delegateInfo, err = m.engine.IsMining(address, uint64(time.Now().Unix())); err != nil { + blockTime := uint64(time.Now().Unix()) + if delegateInfo, err = m.chain.Engine.IsMining(address, blockTime); err != nil { time.Sleep(1 * time.Second) continue } - block, err := mining.NewBlockTemplate(m.chain, m.txPool, m.accountManager, m.engine, delegateInfo) + block, err := mining.NewBlockTemplate(m.chain, m.txPool, m.accountManager, m.chain.Engine, delegateInfo, blockTime) if err != nil { log.Errorf("Mining: failed on create NewBlockTemplate: %v", err) time.Sleep(1 * time.Second) diff --git a/mining/mining.go b/mining/mining.go index 97590f5d..61dfe032 100644 --- a/mining/mining.go +++ b/mining/mining.go @@ -108,7 +108,7 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh } // NewBlockTemplate returns a new block template that is ready to be solved -func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager, engine engine.Engine, delegateInfo interface{}) (b *types.Block, err error) { +func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager, engine engine.Engine, delegateInfo interface{}, blockTime uint64) (b *types.Block, err error) { view := state.NewUtxoViewpoint() txStatus := bc.NewTransactionStatus() if err := txStatus.SetStatus(0, false); err != nil { @@ -127,7 +127,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager Version: 1, Height: nextBlockHeight, PreviousBlockHash: preBlockHash, - Timestamp: uint64(time.Now().Unix()), + Timestamp: blockTime, BlockCommitment: types.BlockCommitment{}, } diff --git a/node/node.go b/node/node.go index 57744880..c30ce928 100644 --- a/node/node.go +++ b/node/node.go @@ -27,8 +27,6 @@ import ( "github.com/vapor/common" cfg "github.com/vapor/config" "github.com/vapor/consensus" - engine "github.com/vapor/consensus/consensus" - dpos "github.com/vapor/consensus/consensus/dpos" "github.com/vapor/crypto/ed25519/chainkd" "github.com/vapor/database/leveldb" "github.com/vapor/env" @@ -67,8 +65,6 @@ type Node struct { miningEnable bool newBlockCh chan *bc.Hash - - engine engine.Engine } func NewNode(config *cfg.Config) *Node { @@ -93,14 +89,8 @@ func NewNode(config *cfg.Config) *Node { tokenDB := dbm.NewDB("accesstoken", config.DBBackend, config.DBDir()) accessTokens := accesstoken.NewStore(tokenDB) - var engine engine.Engine - switch config.Consensus.Type { - case "dpos": - engine = dpos.GDpos - } - txPool := protocol.NewTxPool(store) - chain, err := protocol.NewChain(store, txPool, engine) + chain, err := protocol.NewChain(store, txPool) if err != nil { cmn.Exit(cmn.Fmt("Failed to create chain structure: %v", err)) } @@ -177,10 +167,9 @@ func NewNode(config *cfg.Config) *Node { newBlockCh: newBlockCh, notificationMgr: notificationMgr, - engine: engine, } - node.miner = miner.NewMiner(chain, accounts, txPool, newBlockCh, engine) + node.miner = miner.NewMiner(chain, accounts, txPool, newBlockCh) node.BaseService = *cmn.NewBaseService(nil, "Node", node) return node @@ -305,7 +294,7 @@ func (n *Node) OnStart() error { } func (n *Node) OnStop() { - if err := n.engine.Finish(); err != nil { + if err := n.chain.Engine.Finish(); err != nil { log.Errorf("OnStop: %v", err) } @@ -390,13 +379,17 @@ func initDpos(chain *protocol.Chain, config *cfg.Config) { hash := header.Hash() maxSignerCount := config.Consensus.MaxSignerCount period := config.Consensus.Period - if err := dpos.GDpos.Init(chain, maxSignerCount, period, height, hash); err != nil { - cmn.Exit(cmn.Fmt("initVote: Dpos new: %v", err)) - } + err := chain.Engine.Init(chain, maxSignerCount, period, height, hash) if height > 0 { - oldBlockHeight := dpos.GDpos.GetOldBlockHeight() - oldBlockHash := dpos.GDpos.GetOldBlockHash() + oldBlockHeight := chain.Engine.GetOldBlockHeight() + oldBlockHash := chain.Engine.GetOldBlockHash() + if err != nil { + oldBlockHeight = 0 + header, _ = chain.GetHeaderByHeight(oldBlockHeight) + oldBlockHash = header.Hash() + } + if err := chain.RepairDPoSData(oldBlockHeight, oldBlockHash); err != nil { cmn.Exit(cmn.Fmt("initVote failed: %v", err)) } diff --git a/protocol/block.go b/protocol/block.go index f91c33d9..f629b8f2 100644 --- a/protocol/block.go +++ b/protocol/block.go @@ -329,7 +329,7 @@ func (c *Chain) DoVoting(block *types.Block, mapTxFee map[bc.Hash]uint64) error if err := json.Unmarshal(msg.Data, data); err != nil { return err } - c.engine.ProcessRegister(address.EncodeAddress(), data.Name, hash, height) + c.Engine.ProcessRegister(address.EncodeAddress(), data.Name, hash, height) } case vm.OP_VOTE: if mapTxFee[tx.Tx.ID] >= consensus.VoteForgerFee { @@ -337,7 +337,7 @@ func (c *Chain) DoVoting(block *types.Block, mapTxFee map[bc.Hash]uint64) error if err := json.Unmarshal(msg.Data, data); err != nil { return err } - c.engine.ProcessVote(address.EncodeAddress(), data.Forgers, hash, height) + c.Engine.ProcessVote(address.EncodeAddress(), data.Forgers, hash, height) } case vm.OP_REVOKE: if mapTxFee[tx.Tx.ID] >= consensus.CancelVoteForgerFee { @@ -345,7 +345,7 @@ func (c *Chain) DoVoting(block *types.Block, mapTxFee map[bc.Hash]uint64) error if err := json.Unmarshal(msg.Data, data); err != nil { return err } - c.engine.ProcessCancelVote(address.EncodeAddress(), data.Forgers, hash, height) + c.Engine.ProcessCancelVote(address.EncodeAddress(), data.Forgers, hash, height) } } } @@ -403,7 +403,7 @@ func (c *Chain) CalculateBalance(block *types.Block, fIsAdd bool) map[bc.Hash]ui mapTxFee[tx.Tx.ID] = fee } - c.engine.UpdateAddressBalance(addressBalances) + c.Engine.UpdateAddressBalance(addressBalances) return mapTxFee } diff --git a/protocol/protocol.go b/protocol/protocol.go index ba60e08d..59e4d1a3 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -7,6 +7,7 @@ import ( "github.com/vapor/config" engine "github.com/vapor/consensus/consensus" + dpos "github.com/vapor/consensus/consensus/dpos" "github.com/vapor/errors" "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" @@ -25,17 +26,24 @@ type Chain struct { cond sync.Cond bestNode *state.BlockNode - engine engine.Engine + Engine engine.Engine } // NewChain returns a new Chain using store as the underlying storage. -func NewChain(store Store, txPool *TxPool, engine engine.Engine) (*Chain, error) { +func NewChain(store Store, txPool *TxPool) (*Chain, error) { + + var engine engine.Engine + switch config.CommonConfig.Consensus.Type { + case "dpos": + engine = dpos.GDpos + } + c := &Chain{ orphanManage: NewOrphanManage(), txPool: txPool, store: store, processBlockCh: make(chan *processBlockMsg, maxProcessBlockChSize), - engine: engine, + Engine: engine, } c.cond.L = new(sync.Mutex) @@ -58,10 +66,6 @@ func NewChain(store Store, txPool *TxPool, engine engine.Engine) (*Chain, error) return c, nil } -func (c *Chain) SetConsensusEngine(engine engine.Engine) { - c.engine = engine -} - func (c *Chain) initChainStatus() error { genesisBlock := config.GenesisBlock() txStatus := bc.NewTransactionStatus() diff --git a/test/bench_blockchain_test.go b/test/bench_blockchain_test.go index 8ccb36b7..d3827f29 100644 --- a/test/bench_blockchain_test.go +++ b/test/bench_blockchain_test.go @@ -13,10 +13,7 @@ import ( "github.com/vapor/blockchain/pseudohsm" "github.com/vapor/blockchain/signers" "github.com/vapor/blockchain/txbuilder" - "github.com/vapor/config" "github.com/vapor/consensus" - engine "github.com/vapor/consensus/consensus" - "github.com/vapor/consensus/consensus/dpos" "github.com/vapor/crypto/ed25519/chainkd" "github.com/vapor/database/leveldb" "github.com/vapor/database/storage" @@ -139,15 +136,10 @@ func GenerateChainData(dirPath string, testDB dbm.DB, txNumber, otherAssetNum in if err := SetUtxoView(testDB, utxoView); err != nil { return nil, nil, nil, err } - var engine engine.Engine - switch config.CommonConfig.Consensus.Type { - case "dpos": - engine = dpos.GDpos - } store := leveldb.NewStore(testDB) txPool := protocol.NewTxPool(store) - chain, err := protocol.NewChain(store, txPool, engine) + chain, err := protocol.NewChain(store, txPool) if err != nil { return nil, nil, nil, err } @@ -164,7 +156,7 @@ func InsertChain(chain *protocol.Chain, txPool *protocol.TxPool, txs []*types.Tx } } - block, err := mining.NewBlockTemplate(chain, txPool, nil, nil, nil) + block, err := mining.NewBlockTemplate(chain, txPool, nil, nil, nil, uint64(time.Now().Unix())) if err != nil { return err } diff --git a/test/util.go b/test/util.go index 1cfa781a..aa9ee7b2 100644 --- a/test/util.go +++ b/test/util.go @@ -12,8 +12,6 @@ import ( "github.com/vapor/common" "github.com/vapor/config" "github.com/vapor/consensus" - engine "github.com/vapor/consensus/consensus" - "github.com/vapor/consensus/consensus/dpos" "github.com/vapor/crypto/ed25519/chainkd" "github.com/vapor/database/leveldb" "github.com/vapor/protocol" @@ -43,13 +41,8 @@ func MockChain(testDB dbm.DB) (*protocol.Chain, *leveldb.Store, *protocol.TxPool store := leveldb.NewStore(testDB) txPool := protocol.NewTxPool(store) - var engine engine.Engine - switch config.CommonConfig.Consensus.Type { - case "dpos": - engine = dpos.GDpos - } - chain, err := protocol.NewChain(store, txPool, engine) + chain, err := protocol.NewChain(store, txPool) consensus.ActiveNetParams.Signer = "78673764e0ba91a4c5ba9ec0c8c23c69e3d73bf27970e05e0a977e81e13bde475264d3b177a96646bc0ce517ae7fd63504c183ab6d330dea184331a4cf5912d5" return chain, store, txPool, err } diff --git a/wallet/wallet_test.go b/wallet/wallet_test.go index c9f8b4a1..8f325f3a 100644 --- a/wallet/wallet_test.go +++ b/wallet/wallet_test.go @@ -15,8 +15,6 @@ import ( "github.com/vapor/common" "github.com/vapor/config" "github.com/vapor/consensus" - engine "github.com/vapor/consensus/consensus" - "github.com/vapor/consensus/consensus/dpos" "github.com/vapor/crypto/ed25519/chainkd" "github.com/vapor/database/leveldb" "github.com/vapor/protocol" @@ -54,12 +52,7 @@ func TestWalletUpdate(t *testing.T) { store := leveldb.NewStore(testDB) txPool := protocol.NewTxPool(store) - var engine engine.Engine - switch config.CommonConfig.Consensus.Type { - case "dpos": - engine = dpos.GDpos - } - chain, err := protocol.NewChain(store, txPool, engine) + chain, err := protocol.NewChain(store, txPool) if err != nil { t.Fatal(err) }