OSDN Git Service

fix bug for end to end test
authorpaladz <453256728@qq.com>
Tue, 24 Oct 2017 08:43:29 +0000 (16:43 +0800)
committerpaladz <453256728@qq.com>
Tue, 24 Oct 2017 08:43:29 +0000 (16:43 +0800)
blockchain/txdb/mainchain.go
consensus/general.go
mining/mining_test.go
node/node.go
protocol/block.go
protocol/protocol.go

index df3ac11..ba39496 100644 (file)
@@ -28,7 +28,7 @@ func DecodeMainchain(data []byte) (map[uint64]*bc.Hash, error) {
                if err := h.UnmarshalJSON(rawHash.Key); err != nil {
                        return nil, errors.Wrap(err, "unmarshaling Mainchain hash")
                }
-               mainchain[uint64(i)] = h
+               mainchain[uint64(i+1)] = h
        }
 
        return mainchain, nil
@@ -36,7 +36,7 @@ func DecodeMainchain(data []byte) (map[uint64]*bc.Hash, error) {
 
 func saveMainchain(db dbm.DB, mainchain map[uint64]*bc.Hash, hash *bc.Hash) error {
        var mainchainList storage.Mainchain
-       for i := 0; i < len(mainchain); i++ {
+       for i := 1; i <= len(mainchain); i++ {
                rawHash := &storage.Mainchain_Hash{Key: mainchain[uint64(i)].Bytes()}
                mainchainList.Hashs = append(mainchainList.Hashs, rawHash)
        }
index c694fb0..a450df9 100644 (file)
@@ -29,12 +29,12 @@ var BTMAssetID = &bc.AssetID{
 }
 
 func BlockSubsidy(height uint64) uint64 {
-       if height == 0 {
+       if height == 1 {
                return initialBlockSubsidy
        }
        return baseSubsidy >> uint(height/subsidyReductionInterval)
 }
 
 func InitBlock() []byte {
-       return []byte("0301000000000000000000000000000000000000000000000000000000000000000000ece090e7eb2b4078a79ed5c640a026361c4af77a37342e503cc68493229996e11dd9be38b18f5b492159980684155da19e87de0d1b37b35c1a1123770ec1dcc710aabe77607cce00b1c5a181808080802e0107010700ece090e7eb2b000001012cffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8080ccdee2a69fb314010151000000")
+       return []byte("0301010000000000000000000000000000000000000000000000000000000000000000cecccaebf42b406b03545ed2b38a578e5e6b0796d4ebdd8a6dd72210873fcc026c7319de578ffc492159980684155da19e87de0d1b37b35c1a1123770ec1dcc710aabe77607cced7bb1993fcb680808080801e0107010700cecccaebf42b000001012cffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8080ccdee2a69fb314010151000000")
 }
index bdab99c..d2cb11a 100644 (file)
@@ -9,13 +9,14 @@ import (
        "testing"
        "time"
 
+       "github.com/bytom/consensus"
        "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/bc/legacy"
        "github.com/bytom/protocol/state"
 )
 
 func TestNewInitBlock(t *testing.T) {
-       coinbaseTx, err := createCoinbaseTx(0, 0, []byte{})
+       coinbaseTx, err := createCoinbaseTx(0, 1, []byte{})
        if err != nil {
                t.Error(err)
        }
@@ -31,19 +32,27 @@ func TestNewInitBlock(t *testing.T) {
        b := &legacy.Block{
                BlockHeader: legacy.BlockHeader{
                        Version:           1,
-                       Height:            0,
+                       Height:            1,
                        PreviousBlockHash: bc.Hash{},
                        TimestampMS:       bc.Millis(time.Now()),
                        BlockCommitment: legacy.BlockCommitment{
                                TransactionsMerkleRoot: merkleRoot,
                                AssetsMerkleRoot:       snap.Tree.RootHash(),
                        },
-                       Bits:  uint64(3314649325747331761),
-                       Nonce: 0,
+                       Bits: uint64(2161727821138738707),
                },
                Transactions: []*legacy.Tx{coinbaseTx},
        }
 
+       for i := uint64(0); i <= 10000000000000; i++ {
+               b.Nonce = i
+               hash := b.Hash()
+
+               if consensus.CheckProofOfWork(&hash, b.Bits) {
+                       break
+               }
+       }
+
        rawBlock, err := b.MarshalText()
        if err != nil {
                t.Error(err)
index 8f04249..1a40c05 100644 (file)
@@ -190,9 +190,12 @@ func NewNode(config *cfg.Config, logger log.Logger) *Node {
        txPool := protocol.NewTxPool()
        chain, err := protocol.NewChain(genesisBlock.Hash(), store, txPool)
 
-       if chain.Height() < 1 {
-               if _, err := chain.ProcessBlock(genesisBlock); err != nil {
-                       cmn.Exit(cmn.Fmt("Failed to add genesisBlock to Chain: %v", err))
+       if chain.Height() == 0 {
+               if err := chain.SaveBlock(genesisBlock); err != nil {
+                       cmn.Exit(cmn.Fmt("Failed to save genesisBlock to store: %v", err))
+               }
+               if err := chain.ConnectBlock(genesisBlock); err != nil {
+                       cmn.Exit(cmn.Fmt("Failed to connect genesisBlock to chain: %v", err))
                }
        }
 
index 5663bcd..aafb60c 100644 (file)
@@ -34,7 +34,7 @@ func (c *Chain) GetBlockByHeight(height uint64) (*legacy.Block, error) {
        hash, ok := c.state.mainChain[height]
        c.state.cond.L.Unlock()
        if !ok {
-               return nil, nil
+               return nil, errors.New("can't find block in given hight")
        }
        return c.GetBlockByHash(hash)
 }
@@ -53,7 +53,7 @@ func (c *Chain) ValidateBlock(block, prev *legacy.Block) error {
 
 // ApplyValidBlock creates an updated snapshot without validating the
 // block.
-func (c *Chain) connectBlock(block *legacy.Block) error {
+func (c *Chain) ConnectBlock(block *legacy.Block) error {
        newSnapshot := state.Copy(c.state.snapshot)
        if err := newSnapshot.ApplyBlock(legacy.MapBlock(block)); err != nil {
                return err
@@ -110,14 +110,13 @@ func (c *Chain) reorganizeChain(block *legacy.Block) error {
 }
 
 func (c *Chain) SaveBlock(block *legacy.Block) error {
-       preBlock, err := c.GetBlockByHash(&block.PreviousBlockHash)
-       if err != nil {
+       preBlock, _ := c.GetBlockByHash(&block.PreviousBlockHash)
+       if err := c.ValidateBlock(block, preBlock); err != nil {
                return err
        }
-       if err := c.ValidateBlock(block, preBlock); err != nil {
+       if err := c.store.SaveBlock(block); err != nil {
                return err
        }
-       c.store.SaveBlock(block)
 
        preorphans, ok := c.orphanManage.preOrphans[block.Hash()]
        if !ok {
@@ -149,7 +148,7 @@ func (c *Chain) ProcessBlock(block *legacy.Block) (bool, error) {
        c.state.cond.L.Lock()
        if c.state.block.Hash() == block.PreviousBlockHash {
                defer c.state.cond.L.Unlock()
-               return false, c.connectBlock(block)
+               return false, c.ConnectBlock(block)
        }
 
        if block.Height > c.state.height && block.Bits >= c.state.block.Bits {
index d0b4488..da91ea7 100644 (file)
@@ -140,7 +140,7 @@ func NewChain(initialBlockHash bc.Hash, store Store, txPool *TxPool) (*Chain, er
        storeStatus := store.GetStoreStatus()
        c.state.height = storeStatus.Height
 
-       if c.state.height < 1 {
+       if c.state.height == 0 {
                c.state.snapshot = state.Empty()
                c.state.mainChain = make(map[uint64]*bc.Hash)
                return c, nil