Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a struct over global vars in default_syncer_test #2700

Merged
merged 5 commits into from May 23, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

Use a struct over global vars in default_syncer_test

Closes #2116.
  • Loading branch information...
mslipper committed May 2, 2019
commit 5e25fa698367ceadc7dde6a56bb53f7cdffa89ab
@@ -21,45 +21,48 @@ import (
)

// Note: many of these tests use the test chain defined in the init function of default_syncer_test.
func initStoreTest(ctx context.Context, t *testing.T) {
func initStoreTest(ctx context.Context, t *testing.T, dstP *DefaultSyncerTestParams) {
powerTable := &th.TestView{}
r := repo.NewInMemoryRepo()
bs := bstore.NewBlockstore(r.Datastore())
cst := hamt.NewCborStore()
con := consensus.NewExpected(cst, bs, th.NewTestProcessor(), powerTable, genCid, proofs.NewFakeVerifier(true, nil))
initSyncTest(t, con, initGenesis, cst, bs, r)
requireSetTestChain(t, con, true)
con := consensus.NewExpected(cst, bs, th.NewTestProcessor(), powerTable, dstP.genCid, proofs.NewFakeVerifier(true, nil))
initGenesisWrapper := func(cst *hamt.CborIpldStore, bs bstore.Blockstore) (*types.Block, error) {
return initGenesis(dstP.minerAddress, dstP.minerOwnerAddress, dstP.minerPeerID, cst, bs)
}
initSyncTest(t, con, initGenesisWrapper, cst, bs, r, dstP)
requireSetTestChain(t, con, true, dstP)
}

func newChainStore() chain.Store {
func newChainStore(dstP *DefaultSyncerTestParams) chain.Store {
r := repo.NewInMemoryRepo()
ds := r.Datastore()
return chain.NewDefaultStore(ds, genCid)
return chain.NewDefaultStore(ds, dstP.genCid)
}

// requirePutTestChain adds all test chain tipsets to the passed in chain store.
func requirePutTestChain(t *testing.T, chainStore chain.Store) {
func requirePutTestChain(t *testing.T, chainStore chain.Store, dstP *DefaultSyncerTestParams) {
ctx := context.Background()
genTsas := &chain.TipSetAndState{
TipSet: genTS,
TipSetStateRoot: genStateRoot,
TipSet: dstP.genTS,
TipSetStateRoot: dstP.genStateRoot,
}
link1Tsas := &chain.TipSetAndState{
TipSet: link1,
TipSetStateRoot: link1State,
TipSet: dstP.link1,
TipSetStateRoot: dstP.link1State,
}
link2Tsas := &chain.TipSetAndState{
TipSet: link2,
TipSetStateRoot: link2State,
TipSet: dstP.link2,
TipSetStateRoot: dstP.link2State,
}
link3Tsas := &chain.TipSetAndState{
TipSet: link3,
TipSetStateRoot: link3State,
TipSet: dstP.link3,
TipSetStateRoot: dstP.link3State,
}

link4Tsas := &chain.TipSetAndState{
TipSet: link4,
TipSetStateRoot: link4State,
TipSet: dstP.link4,
TipSetStateRoot: dstP.link4State,
}
th.RequirePutTsas(ctx, t, chainStore, genTsas)
th.RequirePutTsas(ctx, t, chainStore, link1Tsas)
@@ -84,14 +87,15 @@ func requireHeadTipset(t *testing.T, chain chain.Store) types.TipSet {

// Adding tipsets to the store doesn't error.
func TestPutTipSet(t *testing.T) {
dstP := initDSTParams()
tf.BadUnitTestWithSideEffects(t)

ctx := context.Background()
initStoreTest(ctx, t)
cs := newChainStore()
initStoreTest(ctx, t, dstP)
cs := newChainStore(dstP)
genTsas := &chain.TipSetAndState{
TipSet: genTS,
TipSetStateRoot: genStateRoot,
TipSet: dstP.genTS,
TipSetStateRoot: dstP.genStateRoot,
}
err := cs.PutTipSetAndState(ctx, genTsas)
assert.NoError(t, err)
@@ -136,64 +140,66 @@ func TestGetByKey(t *testing.T) {

// Tipsets can be retrieved by parent key (all block cids of parents).
func TestGetByParent(t *testing.T) {
dstP := initDSTParams()
tf.BadUnitTestWithSideEffects(t)

ctx := context.Background()
initStoreTest(ctx, t)
chain := newChainStore()
initStoreTest(ctx, t, dstP)
chain := newChainStore(dstP)

requirePutTestChain(t, chain)
pkg := types.SortedCidSet{}.String() // empty cid set is genesis pIDs
pk1 := genTS.String()
pk2 := link1.String()
pk3 := link2.String()
pk4 := link3.String()
requirePutTestChain(t, chain, dstP)
pkg := types.SortedCidSet{}.String() // empty cid set is dstP.genesis pIDs
pk1 := dstP.genTS.String()
pk2 := dstP.link1.String()
pk3 := dstP.link2.String()
pk4 := dstP.link3.String()

gotG := requireGetTsasByParentAndHeight(t, chain, pkg, uint64(0))
got1 := requireGetTsasByParentAndHeight(t, chain, pk1, uint64(1))
got2 := requireGetTsasByParentAndHeight(t, chain, pk2, uint64(2))
got3 := requireGetTsasByParentAndHeight(t, chain, pk3, uint64(3))
got4 := requireGetTsasByParentAndHeight(t, chain, pk4, uint64(6)) // two null blocks in between 3 and 4!

assert.Equal(t, genTS, gotG[0].TipSet)
assert.Equal(t, link1, got1[0].TipSet)
assert.Equal(t, link2, got2[0].TipSet)
assert.Equal(t, link3, got3[0].TipSet)
assert.Equal(t, link4, got4[0].TipSet)

assert.Equal(t, genStateRoot, gotG[0].TipSetStateRoot)
assert.Equal(t, link1State, got1[0].TipSetStateRoot)
assert.Equal(t, link2State, got2[0].TipSetStateRoot)
assert.Equal(t, link3State, got3[0].TipSetStateRoot)
assert.Equal(t, link4State, got4[0].TipSetStateRoot)
assert.Equal(t, dstP.genTS, gotG[0].TipSet)
assert.Equal(t, dstP.link1, got1[0].TipSet)
assert.Equal(t, dstP.link2, got2[0].TipSet)
assert.Equal(t, dstP.link3, got3[0].TipSet)
assert.Equal(t, dstP.link4, got4[0].TipSet)

assert.Equal(t, dstP.genStateRoot, gotG[0].TipSetStateRoot)
assert.Equal(t, dstP.link1State, got1[0].TipSetStateRoot)
assert.Equal(t, dstP.link2State, got2[0].TipSetStateRoot)
assert.Equal(t, dstP.link3State, got3[0].TipSetStateRoot)
assert.Equal(t, dstP.link4State, got4[0].TipSetStateRoot)
}

func TestGetMultipleByParent(t *testing.T) {
dstP := initDSTParams()
tf.BadUnitTestWithSideEffects(t)

ctx := context.Background()
initStoreTest(ctx, t)
chainStore := newChainStore()
initStoreTest(ctx, t, dstP)
chainStore := newChainStore(dstP)

mockSigner, ki := types.NewMockSignersAndKeyInfo(2)
mockSignerPubKey := ki[0].PublicKey()

fakeChildParams := th.FakeChildParams{
Parent: genTS,
GenesisCid: genCid,
StateRoot: genStateRoot,
MinerAddr: minerAddress,
Parent: dstP.genTS,
GenesisCid: dstP.genCid,
StateRoot: dstP.genStateRoot,
MinerAddr: dstP.minerAddress,
Nonce: uint64(5),
Signer: mockSigner,
MinerPubKey: mockSignerPubKey,
}

requirePutTestChain(t, chainStore)
pk1 := genTS.String()
requirePutTestChain(t, chainStore, dstP)
pk1 := dstP.genTS.String()
// give one parent multiple children and then query
newBlk := th.RequireMkFakeChild(t, fakeChildParams)
newChild := th.RequireNewTipSet(t, newBlk)
newRoot := cidGetter()
newRoot := dstP.cidGetter()
newChildTsas := &chain.TipSetAndState{
TipSet: newChild,
TipSetStateRoot: newRoot,
@@ -205,25 +211,26 @@ func TestGetMultipleByParent(t *testing.T) {
if len(tsas.TipSet) == 1 {
assert.Equal(t, newRoot, tsas.TipSetStateRoot)
} else {
assert.Equal(t, link1State, tsas.TipSetStateRoot)
assert.Equal(t, dstP.link1State, tsas.TipSetStateRoot)
}
}
}

// All blocks of a tipset can be retrieved after putting their wrapping tipset.
func TestGetBlocks(t *testing.T) {
dstP := initDSTParams()
tf.BadUnitTestWithSideEffects(t)

ctx := context.Background()
initStoreTest(ctx, t)
chain := newChainStore()
initStoreTest(ctx, t, dstP)
chain := newChainStore(dstP)

blks := []*types.Block{genesis, link1blk1, link1blk2, link2blk1,
link2blk2, link2blk3, link3blk1, link4blk1, link4blk2}
_, err := chain.GetBlock(ctx, genCid)
blks := []*types.Block{dstP.genesis, dstP.link1blk1, dstP.link1blk2, dstP.link2blk1,
dstP.link2blk2, dstP.link2blk3, dstP.link3blk1, dstP.link4blk1, dstP.link4blk2}
_, err := chain.GetBlock(ctx, dstP.genCid)
assert.Error(t, err) // Get errors before put

requirePutTestChain(t, chain)
requirePutTestChain(t, chain, dstP)

var cids types.SortedCidSet
for _, blk := range blks {
@@ -240,17 +247,18 @@ func TestGetBlocks(t *testing.T) {

// chain.Store correctly indicates that is has all blocks in put tipsets
func TestHasAllBlocks(t *testing.T) {
dstP := initDSTParams()
tf.BadUnitTestWithSideEffects(t)

ctx := context.Background()
initStoreTest(ctx, t)
chain := newChainStore()
initStoreTest(ctx, t, dstP)
chain := newChainStore(dstP)

blks := []*types.Block{genesis, link1blk1, link1blk2, link2blk1,
link2blk2, link2blk3, link3blk1, link4blk1, link4blk2}
assert.False(t, chain.HasBlock(ctx, genCid)) // Has returns false before put
blks := []*types.Block{dstP.genesis, dstP.link1blk1, dstP.link1blk2, dstP.link2blk1,
dstP.link2blk2, dstP.link2blk3, dstP.link3blk1, dstP.link4blk1, dstP.link4blk2}
assert.False(t, chain.HasBlock(ctx, dstP.genCid)) // Has returns false before put

requirePutTestChain(t, chain)
requirePutTestChain(t, chain, dstP)

var cids []cid.Cid
for _, blk := range blks {
@@ -263,15 +271,16 @@ func TestHasAllBlocks(t *testing.T) {

/* Head and its State is set and notified properly. */

// The constructor call sets the genesis block for the chain store.
// The constructor call sets the dstP.genesis block for the chain store.
func TestSetGenesis(t *testing.T) {
dstP := initDSTParams()
tf.BadUnitTestWithSideEffects(t)

ctx := context.Background()
initStoreTest(ctx, t)
chain := newChainStore()
requirePutTestChain(t, chain)
require.Equal(t, genCid, chain.GenesisCid())
initStoreTest(ctx, t, dstP)
chain := newChainStore(dstP)
requirePutTestChain(t, chain, dstP)
require.Equal(t, dstP.genCid, chain.GenesisCid())
}

func assertSetHead(t *testing.T, chainStore chain.Store, ts types.TipSet) {
@@ -282,27 +291,28 @@ func assertSetHead(t *testing.T, chainStore chain.Store, ts types.TipSet) {

// Set and Get Head.
func TestHead(t *testing.T) {
dstP := initDSTParams()
tf.BadUnitTestWithSideEffects(t)

ctx := context.Background()
initStoreTest(ctx, t)
chain := newChainStore()
requirePutTestChain(t, chain)
initStoreTest(ctx, t, dstP)
chain := newChainStore(dstP)
requirePutTestChain(t, chain, dstP)

// Head starts as an empty cid set
assert.Equal(t, types.SortedCidSet{}, chain.GetHead())

// Set Head
assertSetHead(t, chain, genTS)
assert.Equal(t, genTS.ToSortedCidSet(), chain.GetHead())
assertSetHead(t, chain, dstP.genTS)
assert.Equal(t, dstP.genTS.ToSortedCidSet(), chain.GetHead())

// Move head forward
assertSetHead(t, chain, link4)
assert.Equal(t, link4.ToSortedCidSet(), chain.GetHead())
assertSetHead(t, chain, dstP.link4)
assert.Equal(t, dstP.link4.ToSortedCidSet(), chain.GetHead())

// Move head back
assertSetHead(t, chain, genTS)
assert.Equal(t, genTS.ToSortedCidSet(), chain.GetHead())
assertSetHead(t, chain, dstP.genTS)
assert.Equal(t, dstP.genTS.ToSortedCidSet(), chain.GetHead())
}

func assertEmptyCh(t *testing.T, ch <-chan interface{}) {
@@ -315,28 +325,29 @@ func assertEmptyCh(t *testing.T, ch <-chan interface{}) {

// Head events are propagated on HeadEvents.
func TestHeadEvents(t *testing.T) {
dstP := initDSTParams()
tf.BadUnitTestWithSideEffects(t)

ctx := context.Background()
initStoreTest(ctx, t)
chainStore := newChainStore()
requirePutTestChain(t, chainStore)
initStoreTest(ctx, t, dstP)
chainStore := newChainStore(dstP)
requirePutTestChain(t, chainStore, dstP)

ps := chainStore.HeadEvents()
chA := ps.Sub(chain.NewHeadTopic)
chB := ps.Sub(chain.NewHeadTopic)

assertSetHead(t, chainStore, genTS)
assertSetHead(t, chainStore, link1)
assertSetHead(t, chainStore, link2)
assertSetHead(t, chainStore, link3)
assertSetHead(t, chainStore, link4)
assertSetHead(t, chainStore, link3)
assertSetHead(t, chainStore, link2)
assertSetHead(t, chainStore, link1)
assertSetHead(t, chainStore, genTS)
heads := []types.TipSet{genTS, link1, link2, link3, link4, link3,
link2, link1, genTS}
assertSetHead(t, chainStore, dstP.genTS)
assertSetHead(t, chainStore, dstP.link1)
assertSetHead(t, chainStore, dstP.link2)
assertSetHead(t, chainStore, dstP.link3)
assertSetHead(t, chainStore, dstP.link4)
assertSetHead(t, chainStore, dstP.link3)
assertSetHead(t, chainStore, dstP.link2)
assertSetHead(t, chainStore, dstP.link1)
assertSetHead(t, chainStore, dstP.genTS)
heads := []types.TipSet{dstP.genTS, dstP.link1, dstP.link2, dstP.link3, dstP.link4, dstP.link3,
dstP.link2, dstP.link1, dstP.genTS}

// Heads arrive in the expected order
for i := 0; i < 9; i++ {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.