Skip to content

Commit

Permalink
refactor: remove duplicated EventListener interface
Browse files Browse the repository at this point in the history
  • Loading branch information
niklr committed May 4, 2022
1 parent 3decfc2 commit 06e966c
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 160 deletions.
14 changes: 14 additions & 0 deletions interfaces/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import (
"github.com/vitelabs/go-vite/v2/interfaces/core"
)

type EventListener interface {
PrepareInsertAccountBlocks(blocks []*VmAccountBlock) error
InsertAccountBlocks(blocks []*VmAccountBlock) error

PrepareInsertSnapshotBlocks(chunks []*core.SnapshotChunk) error
InsertSnapshotBlocks(chunks []*core.SnapshotChunk) error

PrepareDeleteAccountBlocks(blocks []*core.AccountBlock) error
DeleteAccountBlocks(blocks []*core.AccountBlock) error

PrepareDeleteSnapshotBlocks(chunks []*core.SnapshotChunk) error
DeleteSnapshotBlocks(chunks []*core.SnapshotChunk) error
}

type StorageIterator interface {
Last() bool
Prev() bool
Expand Down
12 changes: 6 additions & 6 deletions ledger/chain/event_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
)

type eventManager struct {
listenerList []EventListener
listenerList []interfaces.EventListener

chain *chain
maxHandlerId uint32
Expand All @@ -35,7 +35,7 @@ func newEventManager(chain *chain) *eventManager {
return &eventManager{
chain: chain,
maxHandlerId: 0,
listenerList: make([]EventListener, 0),
listenerList: make([]interfaces.EventListener, 0),
}
}

Expand Down Expand Up @@ -150,14 +150,14 @@ func (em *eventManager) TriggerDeleteSbs(eventType byte, chunks []*ledger.Snapsh
return nil
}

func (em *eventManager) Register(listener EventListener) {
func (em *eventManager) Register(listener interfaces.EventListener) {
em.mu.Lock()
defer em.mu.Unlock()

em.listenerList = append(em.listenerList, listener)
}

func (em *eventManager) UnRegister(listener EventListener) {
func (em *eventManager) UnRegister(listener interfaces.EventListener) {
em.mu.Lock()
defer em.mu.Unlock()

Expand All @@ -169,10 +169,10 @@ func (em *eventManager) UnRegister(listener EventListener) {
}
}

func (c *chain) Register(listener EventListener) {
func (c *chain) Register(listener interfaces.EventListener) {
c.em.Register(listener)
}

func (c *chain) UnRegister(listener EventListener) {
func (c *chain) UnRegister(listener interfaces.EventListener) {
c.em.UnRegister(listener)
}
18 changes: 2 additions & 16 deletions ledger/chain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@ import (
"github.com/vitelabs/go-vite/v2/vm/contracts/dex"
)

type EventListener interface {
PrepareInsertAccountBlocks(blocks []*interfaces.VmAccountBlock) error
InsertAccountBlocks(blocks []*interfaces.VmAccountBlock) error

PrepareInsertSnapshotBlocks(chunks []*ledger.SnapshotChunk) error
InsertSnapshotBlocks(chunks []*ledger.SnapshotChunk) error

PrepareDeleteAccountBlocks(blocks []*ledger.AccountBlock) error
DeleteAccountBlocks(blocks []*ledger.AccountBlock) error

PrepareDeleteSnapshotBlocks(chunks []*ledger.SnapshotChunk) error
DeleteSnapshotBlocks(chunks []*ledger.SnapshotChunk) error
}

type Consensus interface {
VerifyAccountProducer(block *ledger.AccountBlock) (bool, error)
SBPReader() core.SBPStatReader
Expand All @@ -53,8 +39,8 @@ type Chain interface {
/*
* Event Manager
*/
Register(listener EventListener)
UnRegister(listener EventListener)
Register(listener interfaces.EventListener)
UnRegister(listener interfaces.EventListener)

/*
* C(Create)
Expand Down
14 changes: 0 additions & 14 deletions ledger/chain/state/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@ import (
"github.com/vitelabs/go-vite/v2/ledger/consensus/core"
)

type EventListener interface {
PrepareInsertAccountBlocks(blocks []*interfaces.VmAccountBlock) error
InsertAccountBlocks(blocks []*interfaces.VmAccountBlock) error

PrepareInsertSnapshotBlocks(snapshotBlocks []*ledger.SnapshotBlock) error
InsertSnapshotBlocks(snapshotBlocks []*ledger.SnapshotBlock) error

PrepareDeleteAccountBlocks(blocks []*ledger.AccountBlock) error
DeleteAccountBlocks(blocks []*ledger.AccountBlock) error

PrepareDeleteSnapshotBlocks(chunks []*ledger.SnapshotChunk) error
DeleteSnapshotBlocks(chunks []*ledger.SnapshotChunk) error
}

type Consensus interface {
VerifyAccountProducer(block *ledger.AccountBlock) (bool, error)
SBPReader() core.SBPStatReader
Expand Down
5 changes: 5 additions & 0 deletions ledger/consensus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Generate `MockChain`:

```
~/go/bin/mockgen -source=ledger/consensus/chain_rw.go -destination=ledger/consensus/mock_ch.go -package=consensus
```
9 changes: 5 additions & 4 deletions ledger/consensus/chain_rw.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"sync"
"time"

"github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru"
"github.com/pkg/errors"
"github.com/syndtr/goleveldb/leveldb"

"github.com/vitelabs/go-vite/v2/common/types"
"github.com/vitelabs/go-vite/v2/interfaces"
ledger "github.com/vitelabs/go-vite/v2/interfaces/core"
"github.com/vitelabs/go-vite/v2/ledger/chain"

"github.com/vitelabs/go-vite/v2/ledger/consensus/cdb"
"github.com/vitelabs/go-vite/v2/ledger/consensus/core"
"github.com/vitelabs/go-vite/v2/ledger/pool/lock"
Expand All @@ -28,8 +29,8 @@ type Chain interface {
/*
* Event Manager
*/
Register(listener chain.EventListener)
UnRegister(listener chain.EventListener)
Register(listener interfaces.EventListener)
UnRegister(listener interfaces.EventListener)

GetConsensusGroupList(snapshotHash types.Hash) ([]*types.ConsensusGroupInfo, error) // Get all consensus group
GetRegisterList(snapshotHash types.Hash, gid types.Gid) ([]*types.Registration, error) // Get register for consensus group
Expand Down
Loading

0 comments on commit 06e966c

Please sign in to comment.