OSDN Git Service

fix bug
authormars <mars@bytom.io>
Sat, 23 Mar 2019 06:07:46 +0000 (14:07 +0800)
committermars <mars@bytom.io>
Mon, 25 Mar 2019 07:28:34 +0000 (15:28 +0800)
13 files changed:
account/accounts_test.go
asset/asset_test.go
cmd/vapor/consensus.json
consensus/consensus/dpos/dpos.go
consensus/consensus/dpos/vote.go
mining/miner/miner.go
mining/mining.go
node/node.go
protocol/block.go
protocol/protocol.go
test/bench_blockchain_test.go
test/util.go
wallet/wallet_test.go

index b1710df..8ef5ea3 100644 (file)
@@ -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)
        }
index fe85e21..33b1445 100644 (file)
@@ -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
        }
index 7ed772f..338639b 100644 (file)
@@ -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",
index 69abaf0..04dfd14 100644 (file)
@@ -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")
index c705674..1fa7e80 100644 (file)
@@ -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 {
index 378a78e..6f4f775 100644 (file)
@@ -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)
index 97590f5..61dfe03 100644 (file)
@@ -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{},
        }
 
index 5774488..c30ce92 100644 (file)
@@ -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))
                }
index f91c33d..f629b8f 100644 (file)
@@ -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
 }
 
index ba60e08..59e4d1a 100644 (file)
@@ -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()
index 8ccb36b..d3827f2 100644 (file)
@@ -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
        }
index 1cfa781..aa9ee7b 100644 (file)
@@ -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
 }
index c9f8b4a..8f325f3 100644 (file)
@@ -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)
        }