X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=protocol%2Fstate%2Fblockindex_test.go;h=28e2281a57f202e6f25851879e056f58b9f23ea3;hb=2cf5801b2e693a45de9b51ec9aa9c1f787d57105;hp=95c742815a80c3d3e68925df65ec969b278dbc14;hpb=db158dcf09436b003defd333f1a665e7e051d820;p=bytom%2Fvapor.git diff --git a/protocol/state/blockindex_test.go b/protocol/state/blockindex_test.go index 95c74281..28e2281a 100644 --- a/protocol/state/blockindex_test.go +++ b/protocol/state/blockindex_test.go @@ -1,104 +1,14 @@ package state import ( - "math" - "math/big" - "reflect" "testing" "github.com/davecgh/go-spew/spew" - "github.com/vapor/consensus" - "github.com/vapor/consensus/difficulty" "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" - "github.com/vapor/testutil" ) -func stringToBigInt(s string, base int) *big.Int { - result, _ := new(big.Int).SetString(s, base) - return result -} - -func TestNewBlockNode(t *testing.T) { - cases := []struct { - blockHeader *types.BlockHeader - parentNode *BlockNode - wantBlockNode *BlockNode - }{ - { - blockHeader: &types.BlockHeader{ - Height: uint64(0), - Timestamp: 0, - Bits: 1000, - }, - parentNode: &BlockNode{ - WorkSum: &big.Int{}, - }, - wantBlockNode: &BlockNode{ - Bits: 1000, - Hash: testutil.MustDecodeHash("f1a5a6ddebad7285928a07ce1534104a8d1cd435fc80e90bb9f0034bbe5f8109"), - Seed: consensus.InitialSeed, - WorkSum: new(big.Int).SetInt64(0), - Parent: &BlockNode{ - WorkSum: &big.Int{}, - }, - }, - }, - { - blockHeader: &types.BlockHeader{ - Height: uint64(100), - Timestamp: 0, - Bits: 10000000000, - }, - parentNode: &BlockNode{ - WorkSum: new(big.Int).SetInt64(100), - }, - 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), - }, - }, - }, - { - blockHeader: &types.BlockHeader{ - Height: uint64(100), - Timestamp: 0, - Bits: 10000000000, - }, - parentNode: &BlockNode{ - WorkSum: new(big.Int).SetInt64(math.MaxInt64), - }, - 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), - }, - }, - }, - } - - for i, c := range cases { - blockNode, err := NewBlockNode(c.blockHeader, c.parentNode) - if err != nil { - t.Fatal(err) - } - - if !reflect.DeepEqual(blockNode, c.wantBlockNode) { - t.Fatal("NewBlockNode test error, index:", i, "want:", spew.Sdump(c.wantBlockNode), "got:", spew.Sdump(blockNode)) - } - } -} - func TestCalcPastMedianTime(t *testing.T) { cases := []struct { Timestamps []uint64 @@ -152,98 +62,6 @@ func TestCalcPastMedianTime(t *testing.T) { } } -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 - seed *bc.Hash - }{ - { - node: &BlockNode{ - Height: 0, - }, - seed: consensus.InitialSeed, - }, - { - node: &BlockNode{ - Height: consensus.SeedPerRetarget - 1, - Seed: &bc.Hash{V1: 100}, - }, - seed: &bc.Hash{V1: 100}, - }, - { - node: &BlockNode{ - Height: consensus.SeedPerRetarget, - Seed: &bc.Hash{V2: 200}, - Hash: bc.Hash{V3: 300}, - }, - seed: &bc.Hash{V3: 300}, - }, - } - - for i, c := range cases { - seed := c.node.CalcNextSeed() - if *seed != *c.seed { - t.Fatalf("calc next seed failed, index: %d, expected: %v, have: %v", i, c.seed, seed) - } - } -} - func TestSetMainChain(t *testing.T) { blockIndex := NewBlockIndex() var lastNode *BlockNode