Skip to content

Commit

Permalink
Fix misspell behaviour to behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
tnasu committed Feb 4, 2022
1 parent 988c76f commit 667f138
Show file tree
Hide file tree
Showing 19 changed files with 128 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ linters:
# - interfacer
- lll
# - maligned
# - misspell
- misspell
- nakedret
- nolintlint
- prealloc
Expand Down
6 changes: 3 additions & 3 deletions behaviour/doc.go → behavior/doc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Package Behaviour provides a mechanism for reactors to report behaviour of peers.
Package Behaviour provides a mechanism for reactors to report behavior of peers.
Instead of a reactor calling the switch directly it will call the behaviour module which will
Instead of a reactor calling the switch directly it will call the behavior module which will
handle the stoping and marking peer as good on behalf of the reactor.
There are four different behaviours a reactor can report.
Expand Down Expand Up @@ -39,4 +39,4 @@ type blockPart struct {
This message will request the peer be marked as good
*/
package behaviour
package behavior
49 changes: 49 additions & 0 deletions behavior/peer_behavior.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package behavior

import (
"github.com/line/ostracon/p2p"
)

// PeerBehavior is a struct describing a behavior a peer performed.
// `peerID` identifies the peer and reason characterizes the specific
// behavior performed by the peer.
type PeerBehavior struct {
peerID p2p.ID
reason interface{}
}

type badMessage struct {
explanation string
}

// BadMessage returns a badMessage PeerBehavior.
func BadMessage(peerID p2p.ID, explanation string) PeerBehavior {
return PeerBehavior{peerID: peerID, reason: badMessage{explanation}}
}

type messageOutOfOrder struct {
explanation string
}

// MessageOutOfOrder returns a messagOutOfOrder PeerBehavior.
func MessageOutOfOrder(peerID p2p.ID, explanation string) PeerBehavior {
return PeerBehavior{peerID: peerID, reason: messageOutOfOrder{explanation}}
}

type consensusVote struct {
explanation string
}

// ConsensusVote returns a consensusVote PeerBehavior.
func ConsensusVote(peerID p2p.ID, explanation string) PeerBehavior {
return PeerBehavior{peerID: peerID, reason: consensusVote{explanation}}
}

type blockPart struct {
explanation string
}

// BlockPart returns blockPart PeerBehavior.
func BlockPart(peerID p2p.ID, explanation string) PeerBehavior {
return PeerBehavior{peerID: peerID, reason: blockPart{explanation}}
}
28 changes: 14 additions & 14 deletions behaviour/reporter.go → behavior/reporter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package behaviour
package behavior

import (
"errors"
Expand All @@ -7,13 +7,13 @@ import (
"github.com/line/ostracon/p2p"
)

// Reporter provides an interface for reactors to report the behaviour
// Reporter provides an interface for reactors to report the behavior
// of peers synchronously to other components.
type Reporter interface {
Report(behaviour PeerBehaviour) error
Report(behaviour PeerBehavior) error
}

// SwitchReporter reports peer behaviour to an internal Switch.
// SwitchReporter reports peer behavior to an internal Switch.
type SwitchReporter struct {
sw *p2p.Switch
}
Expand All @@ -25,8 +25,8 @@ func NewSwitchReporter(sw *p2p.Switch) *SwitchReporter {
}
}

// Report reports the behaviour of a peer to the Switch.
func (spbr *SwitchReporter) Report(behaviour PeerBehaviour) error {
// Report reports the behavior of a peer to the Switch.
func (spbr *SwitchReporter) Report(behaviour PeerBehavior) error {
peer := spbr.sw.Peers().Get(behaviour.peerID)
if peer == nil {
return errors.New("peer not found")
Expand All @@ -48,22 +48,22 @@ func (spbr *SwitchReporter) Report(behaviour PeerBehaviour) error {

// MockReporter is a concrete implementation of the Reporter
// interface used in reactor tests to ensure reactors report the correct
// behaviour in manufactured scenarios.
// behavior in manufactured scenarios.
type MockReporter struct {
mtx tmsync.RWMutex
pb map[p2p.ID][]PeerBehaviour
pb map[p2p.ID][]PeerBehavior
}

// NewMockReporter returns a Reporter which records all reported
// behaviours in memory.
func NewMockReporter() *MockReporter {
return &MockReporter{
pb: map[p2p.ID][]PeerBehaviour{},
pb: map[p2p.ID][]PeerBehavior{},
}
}

// Report stores the PeerBehaviour produced by the peer identified by peerID.
func (mpbr *MockReporter) Report(behaviour PeerBehaviour) error {
// Report stores the PeerBehavior produced by the peer identified by peerID.
func (mpbr *MockReporter) Report(behaviour PeerBehavior) error {
mpbr.mtx.Lock()
defer mpbr.mtx.Unlock()
mpbr.pb[behaviour.peerID] = append(mpbr.pb[behaviour.peerID], behaviour)
Expand All @@ -72,15 +72,15 @@ func (mpbr *MockReporter) Report(behaviour PeerBehaviour) error {
}

// GetBehaviours returns all behaviours reported on the peer identified by peerID.
func (mpbr *MockReporter) GetBehaviours(peerID p2p.ID) []PeerBehaviour {
func (mpbr *MockReporter) GetBehaviours(peerID p2p.ID) []PeerBehavior {
mpbr.mtx.RLock()
defer mpbr.mtx.RUnlock()
if items, ok := mpbr.pb[peerID]; ok {
result := make([]PeerBehaviour, len(items))
result := make([]PeerBehavior, len(items))
copy(result, items)

return result
}

return []PeerBehaviour{}
return []PeerBehavior{}
}
58 changes: 29 additions & 29 deletions behaviour/reporter_test.go → behavior/reporter_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package behaviour_test
package behavior_test

import (
"sync"
"testing"

bh "github.com/line/ostracon/behaviour"
bh "github.com/line/ostracon/behavior"
"github.com/line/ostracon/p2p"
)

// TestMockReporter tests the MockReporter's ability to store reported
// peer behaviour in memory indexed by the peerID.
// peer behavior in memory indexed by the peerID.
func TestMockReporter(t *testing.T) {
var peerID p2p.ID = "MockPeer"
pr := bh.NewMockReporter()
Expand All @@ -25,7 +25,7 @@ func TestMockReporter(t *testing.T) {
}
behaviours = pr.GetBehaviours(peerID)
if len(behaviours) != 1 {
t.Error("Expected the peer have one reported behaviour")
t.Error("Expected the peer have one reported behavior")
}

if behaviours[0] != badMessage {
Expand All @@ -35,14 +35,14 @@ func TestMockReporter(t *testing.T) {

type scriptItem struct {
peerID p2p.ID
behaviour bh.PeerBehaviour
behaviour bh.PeerBehavior
}

// equalBehaviours returns true if a and b contain the same PeerBehaviours with
// the same freequencies and otherwise false.
func equalBehaviours(a []bh.PeerBehaviour, b []bh.PeerBehaviour) bool {
aHistogram := map[bh.PeerBehaviour]int{}
bHistogram := map[bh.PeerBehaviour]int{}
func equalBehaviours(a []bh.PeerBehavior, b []bh.PeerBehavior) bool {
aHistogram := map[bh.PeerBehavior]int{}
bHistogram := map[bh.PeerBehavior]int{}

for _, behaviour := range a {
aHistogram[behaviour]++
Expand Down Expand Up @@ -80,31 +80,31 @@ func TestEqualPeerBehaviours(t *testing.T) {
consensusVote = bh.ConsensusVote(peerID, "voted")
blockPart = bh.BlockPart(peerID, "blocked")
equals = []struct {
left []bh.PeerBehaviour
right []bh.PeerBehaviour
left []bh.PeerBehavior
right []bh.PeerBehavior
}{
// Empty sets
{[]bh.PeerBehaviour{}, []bh.PeerBehaviour{}},
{[]bh.PeerBehavior{}, []bh.PeerBehavior{}},
// Single behaviours
{[]bh.PeerBehaviour{consensusVote}, []bh.PeerBehaviour{consensusVote}},
{[]bh.PeerBehavior{consensusVote}, []bh.PeerBehavior{consensusVote}},
// Equal Frequencies
{[]bh.PeerBehaviour{consensusVote, consensusVote},
[]bh.PeerBehaviour{consensusVote, consensusVote}},
{[]bh.PeerBehavior{consensusVote, consensusVote},
[]bh.PeerBehavior{consensusVote, consensusVote}},
// Equal frequencies different orders
{[]bh.PeerBehaviour{consensusVote, blockPart},
[]bh.PeerBehaviour{blockPart, consensusVote}},
{[]bh.PeerBehavior{consensusVote, blockPart},
[]bh.PeerBehavior{blockPart, consensusVote}},
}
unequals = []struct {
left []bh.PeerBehaviour
right []bh.PeerBehaviour
left []bh.PeerBehavior
right []bh.PeerBehavior
}{
// Comparing empty sets to non empty sets
{[]bh.PeerBehaviour{}, []bh.PeerBehaviour{consensusVote}},
{[]bh.PeerBehavior{}, []bh.PeerBehavior{consensusVote}},
// Different behaviours
{[]bh.PeerBehaviour{consensusVote}, []bh.PeerBehaviour{blockPart}},
// Same behaviour with different frequencies
{[]bh.PeerBehaviour{consensusVote},
[]bh.PeerBehaviour{consensusVote, consensusVote}},
{[]bh.PeerBehavior{consensusVote}, []bh.PeerBehavior{blockPart}},
// Same behavior with different frequencies
{[]bh.PeerBehavior{consensusVote},
[]bh.PeerBehavior{consensusVote, consensusVote}},
}
)

Expand All @@ -129,25 +129,25 @@ func TestMockPeerBehaviourReporterConcurrency(t *testing.T) {
var (
behaviourScript = []struct {
peerID p2p.ID
behaviours []bh.PeerBehaviour
behaviours []bh.PeerBehavior
}{
{"1", []bh.PeerBehaviour{bh.ConsensusVote("1", "")}},
{"2", []bh.PeerBehaviour{bh.ConsensusVote("2", ""), bh.ConsensusVote("2", ""), bh.ConsensusVote("2", "")}},
{"1", []bh.PeerBehavior{bh.ConsensusVote("1", "")}},
{"2", []bh.PeerBehavior{bh.ConsensusVote("2", ""), bh.ConsensusVote("2", ""), bh.ConsensusVote("2", "")}},
{
"3",
[]bh.PeerBehaviour{bh.BlockPart("3", ""),
[]bh.PeerBehavior{bh.BlockPart("3", ""),
bh.ConsensusVote("3", ""),
bh.BlockPart("3", ""),
bh.ConsensusVote("3", "")}},
{
"4",
[]bh.PeerBehaviour{bh.ConsensusVote("4", ""),
[]bh.PeerBehavior{bh.ConsensusVote("4", ""),
bh.ConsensusVote("4", ""),
bh.ConsensusVote("4", ""),
bh.ConsensusVote("4", "")}},
{
"5",
[]bh.PeerBehaviour{bh.BlockPart("5", ""),
[]bh.PeerBehavior{bh.BlockPart("5", ""),
bh.ConsensusVote("5", ""),
bh.BlockPart("5", ""),
bh.ConsensusVote("5", "")}},
Expand Down
49 changes: 0 additions & 49 deletions behaviour/peer_behaviour.go

This file was deleted.

14 changes: 7 additions & 7 deletions blockchain/v1/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
"time"

"github.com/line/ostracon/behaviour"
"github.com/line/ostracon/behavior"
bc "github.com/line/ostracon/blockchain"
"github.com/line/ostracon/libs/log"
"github.com/line/ostracon/p2p"
Expand Down Expand Up @@ -66,7 +66,7 @@ type BlockchainReactor struct {
// the switch.
eventsFromFSMCh chan bcFsmMessage

swReporter *behaviour.SwitchReporter
swReporter *behavior.SwitchReporter
}

// NewBlockchainReactor returns new reactor instance.
Expand Down Expand Up @@ -100,7 +100,7 @@ func NewBlockchainReactor(state sm.State, blockExec *sm.BlockExecutor, store *st
fsm := NewFSM(startHeight, bcR)
bcR.fsm = fsm
bcR.BaseReactor = *p2p.NewBaseReactor("BlockchainReactor", bcR, async, recvBufSize)
// bcR.swReporter = behaviour.NewSwitchReporter(bcR.BaseReactor.Switch)
// bcR.swReporter = behavior.NewSwitchReporter(bcR.BaseReactor.Switch)

return bcR
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func (bcR *BlockchainReactor) SetLogger(l log.Logger) {

// OnStart implements service.Service.
func (bcR *BlockchainReactor) OnStart() error {
bcR.swReporter = behaviour.NewSwitchReporter(bcR.BaseReactor.Switch)
bcR.swReporter = behavior.NewSwitchReporter(bcR.BaseReactor.Switch)

// call BaseReactor's OnStart()
err := bcR.BaseReactor.OnStart()
Expand Down Expand Up @@ -261,13 +261,13 @@ func (bcR *BlockchainReactor) Receive(chID byte, src p2p.Peer, msgBytes []byte)
msg, err := bc.DecodeMsg(msgBytes)
if err != nil {
bcR.Logger.Error("error decoding message", "src", src, "chId", chID, "err", err)
_ = bcR.swReporter.Report(behaviour.BadMessage(src.ID(), err.Error()))
_ = bcR.swReporter.Report(behavior.BadMessage(src.ID(), err.Error()))
return
}

if err = bc.ValidateMsg(msg); err != nil {
bcR.Logger.Error("peer sent us invalid msg", "peer", src, "msg", msg, "err", err)
_ = bcR.swReporter.Report(behaviour.BadMessage(src.ID(), err.Error()))
_ = bcR.swReporter.Report(behavior.BadMessage(src.ID(), err.Error()))
return
}

Expand Down Expand Up @@ -458,7 +458,7 @@ ForLoop:
func (bcR *BlockchainReactor) reportPeerErrorToSwitch(err error, peerID p2p.ID) {
peer := bcR.Switch.Peers().Get(peerID)
if peer != nil {
_ = bcR.swReporter.Report(behaviour.BadMessage(peerID, err.Error()))
_ = bcR.swReporter.Report(behavior.BadMessage(peerID, err.Error()))
}
}

Expand Down
Loading

0 comments on commit 667f138

Please sign in to comment.