Skip to content

Commit

Permalink
Merge branch 'development' into get-node-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
noot authored Jun 15, 2021
2 parents 37abcac + f5bd761 commit 051b679
Show file tree
Hide file tree
Showing 48 changed files with 1,664 additions and 772 deletions.
8 changes: 8 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ Changes that only affect a single file can be tested with
$ go test <file_you_are_working_on>
```

Sometimes you may need to create mocks for interfaces, in that case you will just execute:

```
$ make mock
```

The above command will generate mocks for all the interfaces inside the project! This command does not affect unchanged interfaces.

**8. Lint your changes.**

Before opening a pull request be sure to run the linter
Expand Down
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,16 @@ gossamer: clean
## install: install the gossamer binary in $GOPATH/bin
install:
GOBIN=$(GOPATH)/bin go run scripts/ci.go install

MOCKGEN := $(shell command -v $(GOPATH)/bin/mockery 2> /dev/null)
mock:
ifndef MOCKGEN
@echo "> Installing mockery ..."
@go get github.com/vektra/mockery/v2/.../
endif
@echo "> Generating mocks at ./tests/mocks ..."
$(GOPATH)/bin/mockery --all --recursive --inpackage --case underscore




7 changes: 3 additions & 4 deletions dot/core/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,20 +404,19 @@ func newGrandpaChange(raw []*types.GrandpaAuthoritiesRaw, delay uint32, currBloc
}

func (h *DigestHandler) handleBABEOnDisabled(d *types.ConsensusDigest, header *types.Header) error {
od := &types.BABEOnDisabled{}
dec, err := scale.Decode(d.Data[1:], od)
od := new(types.BABEOnDisabled)
_, err := scale.Decode(d.Data[1:], od)
if err != nil {
return err
}
od = dec.(*types.BABEOnDisabled)

logger.Debug("handling BABEOnDisabled", "data", od)

err = h.verifier.SetOnDisabled(od.ID, header)

if err != nil {
return err
}

h.babe.SetOnDisabled(od.ID)
return nil
}
Expand Down
36 changes: 28 additions & 8 deletions dot/core/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/keystore"

. "github.com/ChainSafe/gossamer/dot/core/mocks"

log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand All @@ -45,13 +48,17 @@ func newTestDigestHandler(t *testing.T, withBABE, withGrandpa bool) *DigestHandl
err = stateSrvc.Start()
require.NoError(t, err)

var bp BlockProducer
var bp *MockBlockProducer
if withBABE {
bp = &mockBlockProducer{}
bp = new(MockBlockProducer)
blockC := make(chan types.Block)
bp.On("GetBlockChannel", nil).Return(blockC)
}

time.Sleep(time.Second)
dh, err := NewDigestHandler(stateSrvc.Block, stateSrvc.Epoch, stateSrvc.Grandpa, bp, &mockVerifier{})
verifier := new(MockVerifier)
verifier.On("SetOnDisabled", mock.Anything, mock.Anything).Return(nil)

dh, err := NewDigestHandler(stateSrvc.Block, stateSrvc.Epoch, stateSrvc.Grandpa, nil, nil)
require.NoError(t, err)
return dh
}
Expand Down Expand Up @@ -314,8 +321,19 @@ func TestNextGrandpaAuthorityChange_MultipleChanges(t *testing.T) {

func TestDigestHandler_HandleBABEOnDisabled(t *testing.T) {
handler := newTestDigestHandler(t, true, false)
handler.Start()
defer handler.Stop()

babemock := new(MockBlockProducer)
babemock.On("SetOnDisabled", uint32(7))

header := &types.Header{
Number: big.NewInt(1),
}

verifier := new(MockVerifier)
verifier.On("SetOnDisabled", uint32(7), header).Return(nil)

handler.babe = babemock
handler.verifier = verifier

digest := &types.BABEOnDisabled{
ID: 7,
Expand All @@ -329,9 +347,11 @@ func TestDigestHandler_HandleBABEOnDisabled(t *testing.T) {
Data: data,
}

err = handler.HandleConsensusDigest(d, nil)
err = handler.HandleConsensusDigest(d, header)

require.NoError(t, err)
require.Equal(t, uint32(7), handler.babe.(*mockBlockProducer).disabled)

babemock.AssertCalled(t, "SetOnDisabled", uint32(7))
}

func createHeaderWithPreDigest(slotNumber uint64) *types.Header {
Expand Down
41 changes: 25 additions & 16 deletions dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

. "github.com/ChainSafe/gossamer/dot/core/mocks"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
Expand All @@ -36,7 +37,8 @@ import (

func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
// TODO: move to sync package
net := new(mockNetwork)
net := new(MockNetwork)

newBlocks := make(chan types.Block)

cfg := &Config{
Expand All @@ -50,28 +52,31 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
err := s.Start()
require.Nil(t, err)

expected := &network.BlockAnnounceMessage{
Number: big.NewInt(1),
ParentHash: s.blockState.BestBlockHash(),
StateRoot: common.Hash{},
ExtrinsicsRoot: common.Hash{},
Digest: nil,
BestBlock: true,
}

// simulate block sent from BABE session
newBlocks <- types.Block{
newBlock := types.Block{
Header: &types.Header{
Number: big.NewInt(1),
ParentHash: s.blockState.BestBlockHash(),
},
Body: types.NewBody([]byte{}),
}

time.Sleep(testMessageTimeout)
require.NotNil(t, net.Message)
require.Equal(t, network.BlockAnnounceMsgType, net.Message.(network.NotificationsMessage).Type())
require.Equal(t, expected, net.Message)
expected := &network.BlockAnnounceMessage{
ParentHash: newBlock.Header.ParentHash,
Number: newBlock.Header.Number,
StateRoot: newBlock.Header.StateRoot,
ExtrinsicsRoot: newBlock.Header.ExtrinsicsRoot,
Digest: newBlock.Header.Digest,
BestBlock: true,
}

//setup the SendMessage function
net.On("SendMessage", expected)
newBlocks <- newBlock

time.Sleep(time.Second * 2)

net.AssertCalled(t, "SendMessage", expected)
}

func createExtrinsics(t *testing.T, rt runtime.Instance, genHash common.Hash, nonce uint64) types.Extrinsic {
Expand Down Expand Up @@ -129,11 +134,15 @@ func TestService_HandleTransactionMessage(t *testing.T) {
ks := keystore.NewGlobalKeystore()
ks.Acco.Insert(kp)

bp := new(MockBlockProducer)
blockC := make(chan types.Block)
bp.On("GetBlockChannel", nil).Return(blockC)

cfg := &Config{
Keystore: ks,
TransactionState: state.NewTransactionState(),
IsBlockProducer: true,
BlockProducer: &mockBlockProducer{},
BlockProducer: bp,
}

s := NewTestService(t, cfg)
Expand Down
34 changes: 34 additions & 0 deletions dot/core/mocks/block_producer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions dot/core/mocks/network.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions dot/core/mocks/verifier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"github.com/ChainSafe/gossamer/lib/utils"
log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/require"

coremocks "github.com/ChainSafe/gossamer/dot/core/mocks"
)

func addTestBlocksToState(t *testing.T, depth int, blockState BlockState) []*types.Header {
Expand Down Expand Up @@ -97,7 +99,7 @@ func TestStartService(t *testing.T) {
}

func TestAnnounceBlock(t *testing.T) {
net := new(mockNetwork)
net := new(coremocks.MockNetwork)
newBlocks := make(chan types.Block)

cfg := &Config{
Expand All @@ -111,17 +113,29 @@ func TestAnnounceBlock(t *testing.T) {
defer s.Stop()

// simulate block sent from BABE session
newBlocks <- types.Block{
newBlock := types.Block{
Header: &types.Header{
ParentHash: s.blockState.BestBlockHash(),
Number: big.NewInt(1),
},
Body: &types.Body{},
}

time.Sleep(testMessageTimeout)
require.NotNil(t, net.Message)
require.Equal(t, network.BlockAnnounceMsgType, net.Message.(network.NotificationsMessage).Type())
expected := &network.BlockAnnounceMessage{
ParentHash: newBlock.Header.ParentHash,
Number: newBlock.Header.Number,
StateRoot: newBlock.Header.StateRoot,
ExtrinsicsRoot: newBlock.Header.ExtrinsicsRoot,
Digest: newBlock.Header.Digest,
BestBlock: true,
}

net.On("SendMessage", expected)

newBlocks <- newBlock
time.Sleep(time.Second * 2)

net.AssertCalled(t, "SendMessage", expected)
}

func TestService_HasKey(t *testing.T) {
Expand Down
Loading

0 comments on commit 051b679

Please sign in to comment.