package state
import (
- "math"
"math/big"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/bytom/bytom/consensus"
- "github.com/bytom/bytom/consensus/difficulty"
"github.com/bytom/bytom/protocol/bc"
"github.com/bytom/bytom/protocol/bc/types"
"github.com/bytom/bytom/testutil"
blockHeader: &types.BlockHeader{
Height: uint64(0),
Timestamp: 0,
- Bits: 1000,
- },
- parentNode: &BlockNode{
- WorkSum: &big.Int{},
},
+ parentNode: &BlockNode{},
wantBlockNode: &BlockNode{
- Bits: 1000,
- Hash: testutil.MustDecodeHash("f1a5a6ddebad7285928a07ce1534104a8d1cd435fc80e90bb9f0034bbe5f8109"),
- Seed: consensus.InitialSeed,
- WorkSum: new(big.Int).SetInt64(0),
- Parent: &BlockNode{
- WorkSum: &big.Int{},
- },
+ Hash: testutil.MustDecodeHash("f1a5a6ddebad7285928a07ce1534104a8d1cd435fc80e90bb9f0034bbe5f8109"),
+ Seed: consensus.InitialSeed,
+ Parent: &BlockNode{},
},
},
{
blockHeader: &types.BlockHeader{
Height: uint64(100),
Timestamp: 0,
- Bits: 10000000000,
- },
- parentNode: &BlockNode{
- WorkSum: new(big.Int).SetInt64(100),
},
+ parentNode: &BlockNode{},
wantBlockNode: &BlockNode{
- Bits: 10000000000,
- Hash: testutil.MustDecodeHash("b14067726f09d74da89aeb97ca1b15a8b95760b47a0d71549b0aa5ab8c5e724f"),
- Seed: consensus.InitialSeed,
- Height: uint64(100),
- WorkSum: stringToBigInt("193956598387464313942329958138505708296934647681139973265423088790474254103", 10),
- Parent: &BlockNode{
- WorkSum: new(big.Int).SetInt64(100),
- },
+ Hash: testutil.MustDecodeHash("b14067726f09d74da89aeb97ca1b15a8b95760b47a0d71549b0aa5ab8c5e724f"),
+ Seed: consensus.InitialSeed,
+ Height: uint64(100),
+ Parent: &BlockNode{},
},
},
{
blockHeader: &types.BlockHeader{
Height: uint64(100),
Timestamp: 0,
- Bits: 10000000000,
- },
- parentNode: &BlockNode{
- WorkSum: new(big.Int).SetInt64(math.MaxInt64),
},
+ parentNode: &BlockNode{},
wantBlockNode: &BlockNode{
- Bits: 10000000000,
- Hash: testutil.MustDecodeHash("b14067726f09d74da89aeb97ca1b15a8b95760b47a0d71549b0aa5ab8c5e724f"),
- Seed: consensus.InitialSeed,
- Height: uint64(100),
- WorkSum: stringToBigInt("193956598387464313942329958138505708296934647681139973274646460827329029810", 10),
- Parent: &BlockNode{
- WorkSum: new(big.Int).SetInt64(math.MaxInt64),
- },
+ Hash: testutil.MustDecodeHash("b14067726f09d74da89aeb97ca1b15a8b95760b47a0d71549b0aa5ab8c5e724f"),
+ Seed: consensus.InitialSeed,
+ Height: uint64(100),
+ Parent: &BlockNode{},
},
},
}
}
}
-func TestCalcNextBits(t *testing.T) {
- targetTimeSpan := uint64(consensus.BlocksPerRetarget * consensus.TargetSecondsPerBlock)
- cases := []struct {
- parentNode *BlockNode
- currentNode *BlockNode
- bits uint64
- }{
- {
- currentNode: &BlockNode{
- Height: 0,
- Bits: 1000,
- },
- bits: 1000,
- },
- {
- currentNode: &BlockNode{
- Height: consensus.BlocksPerRetarget - 1,
- Bits: 1000,
- },
- bits: 1000,
- },
- {
- parentNode: &BlockNode{
- Height: 0,
- Timestamp: 0,
- },
- currentNode: &BlockNode{
- Height: consensus.BlocksPerRetarget,
- Bits: difficulty.BigToCompact(big.NewInt(1000)),
- Timestamp: targetTimeSpan,
- },
- bits: difficulty.BigToCompact(big.NewInt(1000)),
- },
- {
- parentNode: &BlockNode{
- Height: 0,
- Timestamp: 0,
- },
- currentNode: &BlockNode{
- Height: consensus.BlocksPerRetarget,
- Bits: difficulty.BigToCompact(big.NewInt(1000)),
- Timestamp: targetTimeSpan * 2,
- },
- bits: difficulty.BigToCompact(big.NewInt(2000)),
- },
- }
-
- for i, c := range cases {
- c.currentNode.Parent = c.parentNode
- bits := c.currentNode.CalcNextBits()
- if bits != c.bits {
- t.Fatalf("calc next bit failed, index: %d, expected: %d, have: %d", i, c.bits, bits)
- }
- }
-}
-
func TestCalcNextSeed(t *testing.T) {
cases := []struct {
node *BlockNode