"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"
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)
}
"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"
}
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
}
"consensus":{
"consensus_type": "dpos" ,
"period": 3,
- "max_signers_count": 2,
+ "max_signers_count": 7,
"min_boter_balance": 10,
"genesis_timestamp": 1524549600,
"coinbase": "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep",
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
GDpos.irreversibleBlockFileName = filepath.Join(config.CommonConfig.RootDir, "dpos", "irreversible_block.dat")
GDpos.irreversibleBlockInfo = *newIrreversibleBlockInfo()
- if err := GDpos.ReadIrreversibleBlockInfo(&GDpos.irreversibleBlockInfo); err != nil {
- return err
- }
-
+ GDpos.ReadIrreversibleBlockInfo(&GDpos.irreversibleBlockInfo)
header, _ := c.GetHeaderByHeight(d.DposStartHeight)
d.setStartTime(header.Timestamp)
- return nil
+ return err
}
func (d *DposType) setStartTime(t uint64) {
if currentLoopIndex > prevLoopIndex {
delegateInfo := d.GetNextDelegates(t)
cDelegateInfo := delegateInfo.(*DelegateInfo)
+ if uint64(len(cDelegateInfo.Delegates)) < currentDelegateIndex+1 {
+ return nil, errors.New("Out of the block node list")
+ }
if cDelegateInfo.Delegates[currentDelegateIndex].DelegateAddress == address.EncodeAddress() {
return delegateInfo, nil
}
}
delegates = append(delegates, delegate)
delegateInfo := DelegateInfo{}
- delegateInfo.Delegates = SortDelegate(delegates, t)
+ delegateInfo.Delegates = delegates //SortDelegate(delegates, t)
return &delegateInfo
}
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")
package dpos
import (
+ "bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
- "strings"
"sync"
cmn "github.com/tendermint/tmlibs/common"
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),
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 {
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 {
result = append(result, Delegate{k, 0})
}
}
-
+ if uint64(len(result)) <= delegateNum {
+ return result
+ }
result = result[:delegateNum]
return result
}
if val, ok := v.AddressBalances[address]; ok {
banlance := int64(val) + value
if banlance < 0 {
- cmn.Exit("The balance was negative")
+ cmn.Exit(fmt.Sprintf("The balance was negative: %s: %d", address, value))
}
if banlance == 0 {
delete(v.AddressBalances, address)
}
} else {
if value < 0 {
- cmn.Exit("The balance was negative")
+ cmn.Exit(fmt.Sprintf("The balance was negative: %s: %d", address, value))
}
if value > 0 {
v.AddressBalances[address] = uint64(value)
"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"
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,
numWorkers: defaultNumWorkers,
updateNumWorkers: make(chan struct{}),
newBlockCh: newBlockCh,
- engine: engine,
}
}
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)
}
// 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 {
Version: 1,
Height: nextBlockHeight,
PreviousBlockHash: preBlockHash,
- Timestamp: uint64(time.Now().Unix()),
+ Timestamp: blockTime,
BlockCommitment: types.BlockCommitment{},
}
"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"
miningEnable bool
newBlockCh chan *bc.Hash
-
- engine engine.Engine
}
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))
}
+ switch config.Consensus.Type {
+ case "dpos":
+ initDpos(chain, config)
+ }
+
var accounts *account.Manager = nil
var assets *asset.Registry = nil
var wallet *w.Wallet = nil
}
}()
}
-
- switch config.Consensus.Type {
- case "dpos":
- initDpos(chain, config)
- }
-
node := &Node{
config: config,
syncManager: syncManager,
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
}
func (n *Node) OnStop() {
- if err := n.engine.Finish(); err != nil {
+ if err := n.chain.Engine.Finish(); err != nil {
log.Errorf("OnStop: %v", err)
}
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))
}
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 {
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 {
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)
}
}
}
mapTxFee[tx.Tx.ID] = fee
}
- c.engine.UpdateAddressBalance(addressBalances)
+ c.Engine.UpdateAddressBalance(addressBalances)
return mapTxFee
}
if block.Height != oldBlockHeight {
return errors.New("The module vote records data with a problem")
}
- for i := block.Height + 1; i < c.bestNode.Height; i++ {
+ for i := block.Height + 1; i <= c.bestNode.Height; i++ {
b, err := c.GetBlockByHeight(i)
if err != nil {
return err
"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"
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)
return c, nil
}
-func (c *Chain) SetConsensusEngine(engine engine.Engine) {
- c.engine = engine
-}
-
func (c *Chain) initChainStatus() error {
genesisBlock := config.GenesisBlock()
txStatus := bc.NewTransactionStatus()
"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"
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
}
}
}
- 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
}
"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"
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
}
{
"consensus":{
- "dpos": {
- "period": 1,
- "epoch": 300,
- "maxSignersCount": 1,
- "minVoterBalance": 0,
- "genesisTimestamp": 1524549600,
- "coinbase": "vsm1qy7ml46ehvsny47l7cl76ejeye7ngkvh3tkp7n7",
- "xprv": "c0d652077ae5d056c143ef94d0fa2042d9083d13174ae47d88ece000c4cc2d4d7200dba78d5f21e1c3d35b351fbd410b4cf453d7cd425f8d47acec25e8a271e9",
+ "consensus_type": "dpos" ,
+ "period": 3,
+ "max_signers_count": 2,
+ "min_boter_balance": 10,
+ "genesis_timestamp": 1524549600,
+ "coinbase": "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep",
+ "xprv": "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445",
"signers": [
- "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep",
- "vsm1qy7ml46ehvsny47l7cl76ejeye7ngkvh3tkp7n7"
+ "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep"
]
- }
}
}
\ No newline at end of file
"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"
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)
}