// +build functional
package test
-
import (
"os"
"testing"
"time"
- dbm "github.com/tendermint/tmlibs/db"
-
- "github.com/bytom/consensus"
- "github.com/bytom/protocol/bc"
- "github.com/bytom/protocol/bc/types"
- "github.com/bytom/protocol/vm"
+ "github.com/bytom/bytom/consensus"
+ dbm "github.com/bytom/bytom/database/leveldb"
+ "github.com/bytom/bytom/protocol/bc"
+ "github.com/bytom/bytom/protocol/bc/types"
+ "github.com/bytom/bytom/protocol/vm"
)
func TestBlockHeader(t *testing.T) {
prevHeight func() uint64
timestamp func() uint64
prevHash func() *bc.Hash
- bits func() uint64
- solve bool
valid bool
}{
{
- desc: "block version is 0",
- version: func() uint64 { return 0 },
- prevHeight: chain.BestBlockHeight,
- timestamp: func() uint64 { return uint64(time.Now().Unix()) },
- prevHash: chain.BestBlockHash,
- bits: func() uint64 { return chain.BestBlockHeader().Bits },
- solve: true,
- valid: false,
- },
- {
- desc: "block version grater than prevBlock.Version",
- version: func() uint64 { return chain.BestBlockHeader().Version + 10 },
+ desc: "block version is 1",
+ version: func() uint64 { return 1 },
prevHeight: chain.BestBlockHeight,
- timestamp: func() uint64 { return uint64(time.Now().Unix()) },
+ timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
prevHash: chain.BestBlockHash,
- bits: func() uint64 { return chain.BestBlockHeader().Bits },
- solve: true,
valid: true,
},
{
desc: "invalid block, misorder block height",
version: func() uint64 { return chain.BestBlockHeader().Version },
prevHeight: func() uint64 { return chain.BestBlockHeight() + 1 },
- timestamp: func() uint64 { return uint64(time.Now().Unix()) },
+ timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
prevHash: chain.BestBlockHash,
- bits: func() uint64 { return chain.BestBlockHeader().Bits },
- solve: true,
valid: false,
},
{
desc: "invalid prev hash, prev hash dismatch",
version: func() uint64 { return chain.BestBlockHeader().Version },
prevHeight: chain.BestBlockHeight,
- timestamp: func() uint64 { return uint64(time.Now().Unix()) },
+ timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
prevHash: func() *bc.Hash { hash := genesisHeader.Hash(); return &hash },
- bits: func() uint64 { return chain.BestBlockHeader().Bits },
- solve: true,
- valid: false,
- },
- {
- desc: "invalid bits",
- version: func() uint64 { return chain.BestBlockHeader().Version },
- prevHeight: chain.BestBlockHeight,
- timestamp: func() uint64 { return uint64(time.Now().Unix()) },
- prevHash: chain.BestBlockHash,
- bits: func() uint64 { return chain.BestBlockHeader().Bits + 100 },
- solve: true,
valid: false,
},
{
prevHeight: chain.BestBlockHeight,
timestamp: func() uint64 { return uint64(time.Now().Unix()) + consensus.MaxTimeOffsetSeconds + 60 },
prevHash: chain.BestBlockHash,
- bits: func() uint64 { return chain.BestBlockHeader().Bits },
- solve: true,
valid: false,
},
{
prevHeight: chain.BestBlockHeight,
timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 3 },
prevHash: chain.BestBlockHash,
- bits: func() uint64 { return chain.BestBlockHeader().Bits },
- solve: true,
valid: true,
},
{
prevHeight: chain.BestBlockHeight,
timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp - 1 },
prevHash: chain.BestBlockHash,
- bits: func() uint64 { return chain.BestBlockHeader().Bits },
- solve: true,
valid: true,
},
{
prevHeight: chain.BestBlockHeight,
timestamp: func() uint64 { return genesisHeader.Timestamp },
prevHash: chain.BestBlockHash,
- bits: func() uint64 { return chain.BestBlockHeader().Bits },
- solve: true,
valid: false,
},
}
block.Height = c.prevHeight() + 1
block.Timestamp = c.timestamp()
block.PreviousBlockHash = *c.prevHash()
- block.Bits = c.bits()
- seed, err := chain.CalcNextSeed(&block.PreviousBlockHash)
if err != nil && c.valid {
t.Fatal(err)
}
- if c.solve {
- Solve(seed, block)
- }
_, err = chain.ProcessBlock(block)
result := err == nil
if result != c.valid {
t.Fatal(err)
}
- if err := SolveAndUpdate(chain, block); err == nil {
+ if _, err := chain.ProcessBlock(block); err == nil {
t.Fatalf("test max block gas failed")
}
}