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

Tests/gossip #515

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions gossip/emitter/emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ func TestEmitter(t *testing.T) {
tx1 := types.NewTransaction(1, common.Address{}, big.NewInt(1), 1, big.NewInt(1), nil)
tx2 := types.NewTransaction(2, common.Address{}, big.NewInt(2), 2, big.NewInt(2), nil)

external.EXPECT().IsBusy().
Return(true).
AnyTimes()

txtime.Saw(tx1.Hash(), time.Unix(1, 0))

require.Equal(time.Unix(1, 0), txtime.Of(tx1.Hash()))
Expand All @@ -112,7 +108,37 @@ func TestEmitter(t *testing.T) {
require.Equal(time.Unix(3, 0), txtime.Of(tx2.Hash()))
})

external.EXPECT().IsBusy().Return(true).Times(1)
t.Run("tick", func(t *testing.T) {
em.tick()
})

t.Run("tick", func(t *testing.T) {
tx1 := types.NewTransaction(1, common.Address{}, big.NewInt(1), 1, big.NewInt(1), nil)
tx2 := types.NewTransaction(2, common.Address{}, big.NewInt(2), 2, big.NewInt(2), nil)

txPool.EXPECT().Count().
Return(1).
AnyTimes()
txPool.EXPECT().Pending(true).
Return(map[common.Address]types.Transactions{
common.Address{}: {tx1, tx2},
}, nil).AnyTimes()

external.EXPECT().IsBusy().
Return(false).
AnyTimes()
external.EXPECT().GetLatestBlockIndex().
Return(idx.Block(0)).
AnyTimes()

txSigner.EXPECT().Sender(tx1).
Return(common.Address{}, nil).
AnyTimes()
txSigner.EXPECT().Sender(tx2).
Return(common.Address{}, nil).
AnyTimes()
em.tick()
})

}
132 changes: 132 additions & 0 deletions gossip/emitter/parents_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package emitter

import (
"testing"
"time"

"github.com/Fantom-foundation/lachesis-base/emitter/ancestor"
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/inter/pos"
"github.com/Fantom-foundation/lachesis-base/kvdb/memorydb"
"github.com/golang/mock/gomock"

"github.com/Fantom-foundation/go-opera/gossip/emitter/mock"
"github.com/Fantom-foundation/go-opera/integration/makefakegenesis"
"github.com/Fantom-foundation/go-opera/inter"
"github.com/Fantom-foundation/go-opera/opera"
"github.com/Fantom-foundation/go-opera/vecmt"
)

func TestParents(t *testing.T) {
cfg := DefaultConfig()
gValidators := makefakegenesis.GetFakeValidators(3)
vv := pos.NewBuilder()
for _, v := range gValidators {
vv.Set(v.ID, pos.Weight(1))
}
validators := vv.Build()
cfg.Validator.ID = gValidators[0].ID

ctrl := gomock.NewController(t)
external := mock.NewMockExternal(ctrl)
txPool := mock.NewMockTxPool(ctrl)
signer := mock.NewMockSigner(ctrl)
txSigner := mock.NewMockTxSigner(ctrl)

external.EXPECT().Lock().
AnyTimes()
external.EXPECT().Unlock().
AnyTimes()
external.EXPECT().DagIndex().
Return(func() *vecmt.Index {
vi := vecmt.NewIndex(func(err error) { panic(err) }, vecmt.LiteConfig())
vi.Reset(validators, memorydb.New(), nil)
return vi
}()).
AnyTimes()
external.EXPECT().IsSynced().
Return(true).
AnyTimes()
external.EXPECT().PeersNum().
Return(int(3)).
AnyTimes()
external.EXPECT().StateDB().
Return(nil).
AnyTimes()

em := NewEmitter(cfg, World{
External: external,
TxPool: txPool,
Signer: signer,
TxSigner: txSigner,
})

t.Run("init", func(t *testing.T) {
external.EXPECT().GetRules().
Return(opera.FakeNetRules()).
AnyTimes()

external.EXPECT().GetEpochValidators().
Return(validators, idx.Epoch(1)).
AnyTimes()

external.EXPECT().GetLastEvent(idx.Epoch(1), cfg.Validator.ID).
Return((*hash.Event)(nil)).
AnyTimes()

external.EXPECT().GetLastEvent(idx.Epoch(2), cfg.Validator.ID).
Return(new(hash.Event)).
AnyTimes()

external.EXPECT().GetLastEvent(idx.Epoch(2), gValidators[1].ID).
Return((*hash.Event)(nil)).
AnyTimes()

external.EXPECT().GetHeads(idx.Epoch(1)).
Return(hash.Events{}).
AnyTimes()

external.EXPECT().GetHeads(idx.Epoch(2)).
Return(hash.Events{}).
AnyTimes()

external.EXPECT().GetGenesisTime().
Return(inter.Timestamp(uint64(time.Now().UnixNano()))).
AnyTimes()

em.init()
})

t.Run("build strategies 0 events", func(t *testing.T) {
em.buildSearchStrategies(idx.Event(0))
})

t.Run("build strategies 1 event", func(t *testing.T) {
em.buildSearchStrategies(idx.Event(1))
})

t.Run("build strategies 4 event", func(t *testing.T) {
em.buildSearchStrategies(idx.Event(4))
})

t.Run("build strategies with fcIndexer", func(t *testing.T) {
gValidator := makefakegenesis.GetFakeValidators(1)
vvNew := pos.NewBuilder()
vvNew.Set(gValidator[0].ID, pos.Weight(1))
newValidators := vvNew.Build()

em.quorumIndexer = nil
em.fcIndexer = ancestor.NewFCIndexer(newValidators, em.world.DagIndex(), em.config.Validator.ID)

em.buildSearchStrategies(idx.Event(4))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if the returned value is equal to expected.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

})

t.Run("choose parent not selfParent", func(t *testing.T) {
em.chooseParents(idx.Epoch(1), em.config.Validator.ID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if the returned value is equal to expected.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

})

t.Run("choose parent selfParent", func(t *testing.T) {
em.chooseParents(idx.Epoch(2), em.config.Validator.ID)
})
}
16 changes: 16 additions & 0 deletions gossip/evmstore/store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package evmstore

import (
"testing"

"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/assert"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be better to use "github.com/stretchr/testify/require" instead, for uniformity purpose.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, changed


"github.com/Fantom-foundation/lachesis-base/kvdb/memorydb"
)

Expand All @@ -15,3 +20,14 @@ func nonCachedStore() *Store {

return NewStore(memorydb.NewProducer(""), cfg)
}

func TestStoreSetTx(t *testing.T) {
store := cachedStore()

tx := types.NewTx(&types.LegacyTx{Data: []byte("test")})

store.SetTx(tx.Hash(), tx)

txFromStore := store.GetTx(tx.Hash())
assert.Equal(t, tx.Data(), txFromStore.Data())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about comparing of the rest of tx's public fields?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, added

}