Skip to content

Commit

Permalink
Merge pull request #586 from niklr/ledger_tests
Browse files Browse the repository at this point in the history
fix: resolve import cycle in ledger tests
  • Loading branch information
viteshan authored May 6, 2022
2 parents a28fab7 + 8e04fed commit 00fd800
Show file tree
Hide file tree
Showing 24 changed files with 587 additions and 337 deletions.
2 changes: 1 addition & 1 deletion common/types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestCreateContractAddress(t *testing.T) {
addr := CreateContractAddress([]byte{1, 2, 3}, []byte{1, 2, 3})
fmt.Println(addr)
if _, err := ValidHexAddress(addr.String()); err == nil {
if _, err := ValidHexAddress(addr.String()); err != nil {
t.Fatal("Not valid")
}
}
Expand Down
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
2 changes: 1 addition & 1 deletion interfaces/core/account_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestAccountBlock_ComputeHash(t *testing.T) {
}
fmt.Printf("%s\n", str)

if block.ComputeHash().String() != "6d54436d78a3bae0b4aacbeb91a0af3c666c6ed3339fbcc6610e12844736d091" {
if block.ComputeHash().String() != "57410f8496598113220fd7f8780cedec5f23f13bbe0d4852e1c0e782092b3a46" {
t.Fatal(block.ComputeHash().String())
}

Expand Down
41 changes: 30 additions & 11 deletions ledger/chain/db/flush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,56 @@ package chain_db

import (
"bytes"
"fmt"
"os"
"path"
"sync"
"testing"

"github.com/stretchr/testify/assert"

"github.com/vitelabs/go-vite/v2/common/db/xleveldb"
"github.com/vitelabs/go-vite/v2/ledger/chain/flusher"
leveldb "github.com/vitelabs/go-vite/v2/common/db/xleveldb"
chain_flusher "github.com/vitelabs/go-vite/v2/ledger/chain/flusher"
"github.com/vitelabs/go-vite/v2/ledger/chain/test_tools"
)

func newStore() *Store {
store, err := NewStore(path.Join(test_tools.DefaultDataDir(), "test_store", "store"), "store")
func newStore(dirName string, clear bool) (*Store, string) {
tempDir := path.Join(test_tools.DefaultDataDir(), dirName)
fmt.Printf("tempDir: %s\n", tempDir)
if clear {
os.RemoveAll(tempDir)
}
dataDir := path.Join(tempDir, "test_store")
store, err := NewStore(dataDir, "test_store")
if err != nil {
panic(err)
}

return store
return store, tempDir
}

func clearStore(tempDir string) {
err := os.RemoveAll(tempDir)
if err != nil {
panic(err)
}
}

func TestRedoLog(t *testing.T) {
store := newStore()
store, tempDir := newStore(t.Name(), true)
defer clearStore(tempDir)

var mu sync.RWMutex
flusher, err := chain_flusher.NewFlusher([]chain_flusher.Storage{store}, &mu, path.Join(test_tools.DefaultDataDir(), "test_store"))
flusher, err := chain_flusher.NewFlusher([]chain_flusher.Storage{store}, &mu, path.Join(test_tools.DefaultDataDir(), t.Name()))
// assert flusher
assert.NoError(t, err)
flusher.Flush()

}

func TestFlush(t *testing.T) {
store := newStore()
store, tempDir := newStore(t.Name(), true)
defer clearStore(tempDir)

batch := store.NewBatch()

batch.Put([]byte("key1"), []byte("value1"))
Expand Down Expand Up @@ -119,7 +137,9 @@ func TestFlush(t *testing.T) {
}

func TestRecover(t *testing.T) {
store := newStore()
store, tempDir := newStore(t.Name(), true)
defer clearStore(tempDir)

batch := store.NewBatch()

batch.Put([]byte("key1"), []byte("value1"))
Expand Down Expand Up @@ -190,7 +210,6 @@ func TestRecover(t *testing.T) {
v3, err := store.db.Get([]byte("key3"), nil)
assert.NoError(t, err)
assert.Equal(t, v3, []byte("value3"))

}

const (
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
27 changes: 0 additions & 27 deletions ledger/chain/test_tools/chain.go

This file was deleted.

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
47 changes: 12 additions & 35 deletions ledger/consensus/chain_rw_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package consensus

import (
"os"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/vitelabs/go-vite/v2/common/config"
"github.com/vitelabs/go-vite/v2/common/types"
ledger "github.com/vitelabs/go-vite/v2/interfaces/core"
"github.com/vitelabs/go-vite/v2/ledger/chain"
"github.com/vitelabs/go-vite/v2/ledger/chain/test_tools"
"github.com/vitelabs/go-vite/v2/ledger/pool/lock"
"github.com/vitelabs/go-vite/v2/ledger/test_tools"
"github.com/vitelabs/go-vite/v2/log15"
)

Expand All @@ -38,44 +37,21 @@ func GetConsensusGroupList() ([]*types.ConsensusGroupInfo, error) {
return []*types.ConsensusGroupInfo{info}, nil
}

func testDataDir() string {
return "testdata-consensus"
}

func prepareChain() chain.Chain {
clearChain(nil)

c, err := test_tools.NewChainInstanceFromDir(testDataDir(), false, "")
if err != nil {
panic(err)
}
return c
}

func clearChain(c chain.Chain) {
if c != nil {
c.Stop()
}
err := os.RemoveAll(testDataDir())
if err != nil {
panic(err)
}
}

func Test_chainRw(t *testing.T) {
c := prepareChain()
defer clearChain(c)

c, tempDir := test_tools.NewTestChainInstance(t, true, nil)
defer test_tools.ClearChain(c, tempDir)
}

func TestChainRw_GetMemberInfo(t *testing.T) {
c, tempDir := test_tools.NewTestChainInstance(t, true, nil)
defer test_tools.ClearChain(c, tempDir)

ctrl := gomock.NewController(t)
// Assert that Bar() is invoked.
defer ctrl.Finish()

dir := "testdata-consensus"
db := NewDb(t, dir)
defer ClearDb(t, dir)
db := NewDb(t, tempDir)
defer ClearDb(t, tempDir)

mch := NewMockChain(ctrl)
genesisBlock := &ledger.SnapshotBlock{Height: uint64(1), Timestamp: &simpleGenesis}
Expand All @@ -97,8 +73,9 @@ func TestChainRw_GetMemberInfo(t *testing.T) {
}

func TestChainRw_GetMemberInfo2(t *testing.T) {
c := prepareChain()
defer clearChain(c)
c, tempDir := test_tools.NewTestChainInstance(t, true, config.MockGenesis())
defer test_tools.ClearChain(c, tempDir)

genesis := c.GetGenesisSnapshotBlock()
infos, err := c.GetConsensusGroupList(genesis.Hash)
if err != nil {
Expand Down
Loading

0 comments on commit 00fd800

Please sign in to comment.