Skip to content

Commit

Permalink
Merge pull request #43 from thetatoken/add_tests_04092019
Browse files Browse the repository at this point in the history
add unit tests
  • Loading branch information
jieyilong committed Apr 10, 2019
2 parents 971087e + 85dcdf9 commit c105e45
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
50 changes: 50 additions & 0 deletions blockchain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,56 @@ func TestFinalizePreviousBlocks(t *testing.T) {
assert.False(block.Status.IsFinalized())
}

}
func TestFinalizePreviousBlocks2(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
core.ResetTestBlocks()

ch := CreateTestChainByBlocks([]string{
"a1", "a0",
"a2", "a1",
"a3", "a2",
"a4", "a3",
"a5", "a4",
"b2", "a1",
"b3", "b2",
"b4", "b3",
"b5", "b4",
"b6", "b5",
"b7", "b6",
"c1", "a0",
})
block, err := ch.FindBlock(core.GetTestBlock("b3").Hash())
require.Nil(err)

ch.FinalizePreviousBlocks(block.Hash())

for _, name := range []string{"a0", "a1", "b2", "b3"} {
block, err = ch.FindBlock(core.GetTestBlock(name).Hash())
assert.Nil(err)
assert.True(block.Status.IsFinalized())
}

for _, name := range []string{"b7", "b6", "b5", "b4", "c1", "a2", "a3"} {
block, err = ch.FindBlock(core.GetTestBlock(name).Hash())
assert.False(block.Status.IsFinalized())
}

block, err = ch.FindBlock(core.GetTestBlock("a5").Hash())
require.Nil(err)
ch.FinalizePreviousBlocks(block.Hash())

for _, name := range []string{"a0", "a1", "a2", "a3", "a4", "a5", "b2", "b3"} {
block, err = ch.FindBlock(core.GetTestBlock(name).Hash())
assert.True(block.Status.IsFinalized())
}

for _, name := range []string{"b7", "b6", "b5", "b4", "c1"} {
block, err = ch.FindBlock(core.GetTestBlock(name).Hash())
assert.False(block.Status.IsFinalized())
}

}

func TestBlockIndex(t *testing.T) {
Expand Down
79 changes: 79 additions & 0 deletions blockchain/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,82 @@ func TestTxIndexDuplicateTx(t *testing.T) {
assert.NotNil(block)
assert.Equal(block.Hash(), block2.Hash())
}
func TestTxIndexDuplicateTx2(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

tx1 := common.Bytes("tx1")
tx2 := common.Bytes("tx2")
tx3 := common.Bytes("tx3")
tx4 := common.Bytes("tx4")

core.ResetTestBlocks()
chain := CreateTestChain()

block1 := core.CreateTestBlock("b1", "")
block1.Height = 10
block1.Txs = []common.Bytes{tx1, tx2}

block2 := core.CreateTestBlock("b2", "")
block2.Height = 20
block2.Txs = []common.Bytes{tx2, tx3}

block3 := core.CreateTestBlock("b3", "")
block3.Height = 30
block3.Txs = []common.Bytes{tx3, tx4}

block4 := core.CreateTestBlock("b4", "")
block4.Height = 15
block4.Txs = []common.Bytes{tx4, tx1}

_, err := chain.AddBlock(block1)
require.Nil(err)

_, err = chain.AddBlock(block2)
require.Nil(err)

_, err = chain.AddBlock(block3)
require.Nil(err)

_, err = chain.AddBlock(block4)
require.Nil(err)

tx, block, found := chain.FindTxByHash(crypto.Keccak256Hash(tx1))
assert.True(found)
assert.NotNil(tx)
assert.Equal(tx1, tx)
assert.NotNil(block)
assert.Equal(block.Hash(), block1.Hash())

// Tx2 should be linked with block1 instead of block2.
tx, block, found = chain.FindTxByHash(crypto.Keccak256Hash(tx2))
assert.True(found)
assert.NotNil(tx)
assert.Equal(tx2, tx)
assert.NotNil(block)
assert.Equal(block.Hash(), block1.Hash())

tx, block, found = chain.FindTxByHash(crypto.Keccak256Hash(tx3))
assert.True(found)
assert.NotNil(tx)
assert.Equal(tx3, tx)
assert.NotNil(block)
assert.Equal(block.Hash(), block2.Hash())

tx, block, found = chain.FindTxByHash(crypto.Keccak256Hash(tx4))
assert.True(found)
assert.NotNil(tx)
assert.Equal(tx4, tx)
assert.NotNil(block)
assert.Equal(block.Hash(), block3.Hash())

// Tx2 should be linked with block2 after force insert.
eb := &core.ExtendedBlock{Block: block2}
chain.AddTxsToIndex(eb, true)
tx, block, found = chain.FindTxByHash(crypto.Keccak256Hash(tx2))
assert.True(found)
assert.NotNil(tx)
assert.Equal(tx2, tx)
assert.NotNil(block)
assert.Equal(block.Hash(), block2.Hash())
}

0 comments on commit c105e45

Please sign in to comment.