move some interfaces to types/services.go

This commit is contained in:
Ethan Buchman 2017-02-20 20:09:15 -05:00
parent 0765613778
commit f9df4294f3
7 changed files with 79 additions and 75 deletions

View File

@ -242,7 +242,7 @@ FOR_LOOP:
// NOTE: we could improve performance if we // NOTE: we could improve performance if we
// didn't make the app commit to disk every block // didn't make the app commit to disk every block
// ... but we would need a way to get the hash without it persisting // ... but we would need a way to get the hash without it persisting
err := bcR.state.ApplyBlock(bcR.evsw, bcR.proxyAppConn, first, firstPartsHeader, sm.MockMempool{}) err := bcR.state.ApplyBlock(bcR.evsw, bcR.proxyAppConn, first, firstPartsHeader, types.MockMempool{})
if err != nil { if err != nil {
// TODO This is bad, are we zombie? // TODO This is bad, are we zombie?
PanicQ(Fmt("Failed to process committed block (%d:%X): %v", first.Height, first.Hash(), err)) PanicQ(Fmt("Failed to process committed block (%d:%X): %v", first.Height, first.Hash(), err))

View File

@ -175,8 +175,8 @@ func makeHeightSearchFunc(height int) auto.SearchFunc {
// we were last and using the WAL to recover there // we were last and using the WAL to recover there
// Replay the last block through the consensus and return the AppHash from after Commit. // Replay the last block through the consensus and return the AppHash from after Commit.
func replayLastBlock(config cfg.Config, state *sm.State, proxyApp proxy.AppConnConsensus, blockStore sm.BlockStore) ([]byte, error) { func replayLastBlock(config cfg.Config, state *sm.State, proxyApp proxy.AppConnConsensus, blockStore types.BlockStore) ([]byte, error) {
mempool := sm.MockMempool{} mempool := types.MockMempool{}
cs := NewConsensusState(config, state, proxyApp, blockStore, mempool) cs := NewConsensusState(config, state, proxyApp, blockStore, mempool)
evsw := types.NewEventSwitch() evsw := types.NewEventSwitch()
@ -196,12 +196,12 @@ func replayLastBlock(config cfg.Config, state *sm.State, proxyApp proxy.AppConnC
type Handshaker struct { type Handshaker struct {
config cfg.Config config cfg.Config
state *sm.State state *sm.State
store sm.BlockStore store types.BlockStore
nBlocks int // number of blocks applied to the state nBlocks int // number of blocks applied to the state
} }
func NewHandshaker(config cfg.Config, state *sm.State, store sm.BlockStore) *Handshaker { func NewHandshaker(config cfg.Config, state *sm.State, store types.BlockStore) *Handshaker {
return &Handshaker{config, state, store, 0} return &Handshaker{config, state, store, 0}
} }

View File

@ -254,7 +254,7 @@ func testReplayCrashBeforeWriteVote(t *testing.T, thisCase *testCase, lineNum in
var ( var (
NUM_BLOCKS = 6 // number of blocks in the test_data/many_blocks.cswal NUM_BLOCKS = 6 // number of blocks in the test_data/many_blocks.cswal
mempool = sm.MockMempool{} mempool = types.MockMempool{}
testPartSize int testPartSize int
) )

View File

@ -224,8 +224,8 @@ type ConsensusState struct {
config cfg.Config config cfg.Config
proxyAppConn proxy.AppConnConsensus proxyAppConn proxy.AppConnConsensus
blockStore sm.BlockStore blockStore types.BlockStore
mempool sm.Mempool mempool types.Mempool
privValidator PrivValidator // for signing votes privValidator PrivValidator // for signing votes
@ -253,7 +253,7 @@ type ConsensusState struct {
done chan struct{} done chan struct{}
} }
func NewConsensusState(config cfg.Config, state *sm.State, proxyAppConn proxy.AppConnConsensus, blockStore sm.BlockStore, mempool sm.Mempool) *ConsensusState { func NewConsensusState(config cfg.Config, state *sm.State, proxyAppConn proxy.AppConnConsensus, blockStore types.BlockStore, mempool types.Mempool) *ConsensusState {
cs := &ConsensusState{ cs := &ConsensusState{
config: config, config: config,
proxyAppConn: proxyAppConn, proxyAppConn: proxyAppConn,

View File

@ -5,36 +5,19 @@ import (
"github.com/tendermint/go-crypto" "github.com/tendermint/go-crypto"
"github.com/tendermint/go-p2p" "github.com/tendermint/go-p2p"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/tendermint/consensus" "github.com/tendermint/tendermint/consensus"
"github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/proxy"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
) )
//----------------------------------------------------- //----------------------------------------------
// Interfaces for use by RPC // These interfaces are used by RPC and must be thread safe
// NOTE: these methods must be thread safe!
type BlockStore interface {
Height() int
LoadBlockMeta(height int) *types.BlockMeta
LoadBlock(height int) *types.Block
LoadSeenCommit(height int) *types.Commit
LoadBlockCommit(height int) *types.Commit
}
type Consensus interface { type Consensus interface {
GetValidators() (int, []*types.Validator) GetValidators() (int, []*types.Validator)
GetRoundState() *consensus.RoundState GetRoundState() *consensus.RoundState
} }
type Mempool interface {
Size() int
CheckTx(types.Tx, func(*abci.Response)) error
Reap(int) types.Txs
Flush()
}
type P2P interface { type P2P interface {
Listeners() []p2p.Listener Listeners() []p2p.Listener
Peers() p2p.IPeerSet Peers() p2p.IPeerSet
@ -44,16 +27,18 @@ type P2P interface {
DialSeeds([]string) DialSeeds([]string)
} }
//----------------------------------------------
var ( var (
// external, thread safe interfaces // external, thread safe interfaces
eventSwitch types.EventSwitch eventSwitch types.EventSwitch
proxyAppQuery proxy.AppConnQuery proxyAppQuery proxy.AppConnQuery
config cfg.Config config cfg.Config
// interfaces defined above // interfaces defined in types and above
blockStore BlockStore blockStore types.BlockStore
mempool types.Mempool
consensusState Consensus consensusState Consensus
mempool Mempool
p2pSwitch P2P p2pSwitch P2P
// objects // objects
@ -69,18 +54,18 @@ func SetEventSwitch(evsw types.EventSwitch) {
eventSwitch = evsw eventSwitch = evsw
} }
func SetBlockStore(bs BlockStore) { func SetBlockStore(bs types.BlockStore) {
blockStore = bs blockStore = bs
} }
func SetMempool(mem types.Mempool) {
mempool = mem
}
func SetConsensusState(cs Consensus) { func SetConsensusState(cs Consensus) {
consensusState = cs consensusState = cs
} }
func SetMempool(mem Mempool) {
mempool = mem
}
func SetSwitch(sw P2P) { func SetSwitch(sw P2P) {
p2pSwitch = sw p2pSwitch = sw
} }

View File

@ -223,7 +223,7 @@ func (s *State) validateBlock(block *types.Block) error {
// Execute and commit block against app, save block and state // Execute and commit block against app, save block and state
func (s *State) ApplyBlock(eventCache types.Fireable, proxyAppConn proxy.AppConnConsensus, func (s *State) ApplyBlock(eventCache types.Fireable, proxyAppConn proxy.AppConnConsensus,
block *types.Block, partsHeader types.PartSetHeader, mempool Mempool) error { block *types.Block, partsHeader types.PartSetHeader, mempool types.Mempool) error {
// Run the block on the State: // Run the block on the State:
// + update validator sets // + update validator sets
@ -244,7 +244,7 @@ func (s *State) ApplyBlock(eventCache types.Fireable, proxyAppConn proxy.AppConn
// mempool must be locked during commit and update // mempool must be locked during commit and update
// because state is typically reset on Commit and old txs must be replayed // because state is typically reset on Commit and old txs must be replayed
// against committed state before new txs are run in the mempool, lest they be invalid // against committed state before new txs are run in the mempool, lest they be invalid
func (s *State) CommitStateUpdateMempool(proxyAppConn proxy.AppConnConsensus, block *types.Block, mempool Mempool) error { func (s *State) CommitStateUpdateMempool(proxyAppConn proxy.AppConnConsensus, block *types.Block, mempool types.Mempool) error {
mempool.Lock() mempool.Lock()
defer mempool.Unlock() defer mempool.Unlock()
@ -288,40 +288,3 @@ func ApplyBlock(appConnConsensus proxy.AppConnConsensus, block *types.Block) ([]
} }
return res.Data, nil return res.Data, nil
} }
//------------------------------------------------------
// blockchain services types
// Updates to the mempool need to be synchronized with committing a block
// so apps can reset their transient state on Commit
type Mempool interface {
Lock()
Unlock()
CheckTx(types.Tx, func(*abci.Response)) error
Reap(int) types.Txs
Update(height int, txs types.Txs)
}
type MockMempool struct {
}
func (m MockMempool) Lock() {}
func (m MockMempool) Unlock() {}
func (m MockMempool) CheckTx(tx types.Tx, cb func(*abci.Response)) error { return nil }
func (m MockMempool) Reap(n int) types.Txs { return types.Txs{} }
func (m MockMempool) Update(height int, txs types.Txs) {}
// TODO: Should we move blockchain/store.go to its own package?
type BlockStore interface {
Height() int
LoadBlockMeta(height int) *types.BlockMeta
LoadBlock(height int) *types.Block
LoadBlockPart(height int, index int) *types.Part
SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit)
LoadBlockCommit(height int) *types.Commit
LoadSeenCommit(height int) *types.Commit
}

56
types/services.go Normal file
View File

@ -0,0 +1,56 @@
package types
import (
abci "github.com/tendermint/abci/types"
)
//------------------------------------------------------
// blockchain services types
// NOTE: Interfaces used by RPC must be thread safe!
//------------------------------------------------------
//------------------------------------------------------
// mempool
// Updates to the mempool need to be synchronized with committing a block
// so apps can reset their transient state on Commit
type Mempool interface {
Lock()
Unlock()
Size() int
CheckTx(Tx, func(*abci.Response)) error
Reap(int) Txs
Update(height int, txs Txs)
Flush()
}
type MockMempool struct {
}
func (m MockMempool) Lock() {}
func (m MockMempool) Unlock() {}
func (m MockMempool) Size() int { return 0 }
func (m MockMempool) CheckTx(tx Tx, cb func(*abci.Response)) error { return nil }
func (m MockMempool) Reap(n int) Txs { return Txs{} }
func (m MockMempool) Update(height int, txs Txs) {}
func (m MockMempool) Flush() {}
//------------------------------------------------------
// blockstore
type BlockStoreRPC interface {
Height() int
LoadBlockMeta(height int) *BlockMeta
LoadBlock(height int) *Block
LoadBlockPart(height int, index int) *Part
LoadBlockCommit(height int) *Commit
LoadSeenCommit(height int) *Commit
}
type BlockStore interface {
BlockStoreRPC
SaveBlock(block *Block, blockParts *PartSet, seenCommit *Commit)
}