OSDN Git Service

Hulk did something
[bytom/vapor.git] / test / bench_blockchain_test.go
index 8ccb36b..207696d 100644 (file)
@@ -7,19 +7,17 @@ import (
        "testing"
        "time"
 
-       dbm "github.com/tendermint/tmlibs/db"
-
        "github.com/vapor/account"
        "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/consensus/difficulty"
        "github.com/vapor/crypto/ed25519/chainkd"
-       "github.com/vapor/database/leveldb"
+       "github.com/vapor/database"
+       dbm "github.com/vapor/database/leveldb"
        "github.com/vapor/database/storage"
+       "github.com/vapor/event"
        "github.com/vapor/mining"
        "github.com/vapor/protocol"
        "github.com/vapor/protocol/bc"
@@ -139,15 +137,11 @@ 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)
+       store := database.NewStore(testDB)
+       dispatcher := event.NewDispatcher()
+       txPool := protocol.NewTxPool(store, dispatcher)
+       chain, err := protocol.NewChain(store, txPool)
        if err != nil {
                return nil, nil, nil, err
        }
@@ -164,7 +158,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)
        if err != nil {
                return err
        }
@@ -181,6 +175,15 @@ func InsertChain(chain *protocol.Chain, txPool *protocol.TxPool, txs []*types.Tx
                fmt.Println("txsize:", uint64(block.Transactions[1].SerializedSize))
        }
 
+       seed, err := chain.CalcNextSeed(&block.PreviousBlockHash)
+       if err != nil {
+               return err
+       }
+
+       if err := SolveBlock(seed, block); err != nil {
+               return err
+       }
+
        if _, err := chain.ProcessBlock(block); err != nil {
                return err
        }
@@ -189,11 +192,19 @@ func InsertChain(chain *protocol.Chain, txPool *protocol.TxPool, txs []*types.Tx
 }
 
 func processNewTxch(txPool *protocol.TxPool) {
-       newTxCh := txPool.GetMsgCh()
-       for tx := range newTxCh {
-               if tx == nil {
+}
+
+func SolveBlock(seed *bc.Hash, block *types.Block) error {
+       maxNonce := ^uint64(0) // 2^64 - 1
+       header := &block.BlockHeader
+       for i := uint64(0); i < maxNonce; i++ {
+               header.Nonce = i
+               headerHash := header.Hash()
+               if difficulty.CheckProofOfWork(&headerHash, seed, header.Bits) {
+                       return nil
                }
        }
+       return nil
 }
 
 func MockSimpleUtxo(index uint64, assetID *bc.AssetID, amount uint64, ctrlProg *account.CtrlProgram) *account.UTXO {
@@ -369,7 +380,7 @@ func CreateTxbyNum(txNumber, otherAssetNum int) ([]*types.Tx, error) {
 
 func SetUtxoView(db dbm.DB, view *state.UtxoViewpoint) error {
        batch := db.NewBatch()
-       if err := leveldb.SaveUtxoView(batch, view); err != nil {
+       if err := database.SaveUtxoView(batch, view); err != nil {
                return err
        }
        batch.Write()