Browse Source

drop BlockchainAware

pull/1780/head
Ethan Buchman 7 years ago
parent
commit
b025c13f67
9 changed files with 64 additions and 80 deletions
  1. +6
    -19
      client/local_client.go
  2. +0
    -0
      example/block_aware/block_aware_app.go
  3. +0
    -0
      example/block_aware/block_aware_test.go
  4. +10
    -0
      example/counter/counter.go
  5. +10
    -0
      example/dummy/dummy.go
  6. +2
    -3
      example/dummy/dummy_test.go
  7. +10
    -0
      example/nil/nil_app.go
  8. +4
    -12
      server/socket_server.go
  9. +22
    -46
      types/application.go

+ 6
- 19
client/local_client.go View File

@ -111,9 +111,7 @@ func (app *localClient) CommitAsync() *ReqRes {
func (app *localClient) InitChainAsync(validators []*types.Validator) *ReqRes {
app.mtx.Lock()
if bcApp, ok := app.Application.(types.BlockchainAware); ok {
bcApp.InitChain(validators)
}
app.Application.InitChain(validators)
reqRes := app.callback(
types.ToRequestInitChain(validators),
types.ToResponseInitChain(),
@ -124,9 +122,7 @@ func (app *localClient) InitChainAsync(validators []*types.Validator) *ReqRes {
func (app *localClient) BeginBlockAsync(hash []byte, header *types.Header) *ReqRes {
app.mtx.Lock()
if bcApp, ok := app.Application.(types.BlockchainAware); ok {
bcApp.BeginBlock(hash, header)
}
app.Application.BeginBlock(hash, header)
app.mtx.Unlock()
return app.callback(
types.ToRequestBeginBlock(hash, header),
@ -136,10 +132,7 @@ func (app *localClient) BeginBlockAsync(hash []byte, header *types.Header) *ReqR
func (app *localClient) EndBlockAsync(height uint64) *ReqRes {
app.mtx.Lock()
var resEndBlock types.ResponseEndBlock
if bcApp, ok := app.Application.(types.BlockchainAware); ok {
resEndBlock = bcApp.EndBlock(height)
}
resEndBlock := app.Application.EndBlock(height)
app.mtx.Unlock()
return app.callback(
types.ToRequestEndBlock(height),
@ -201,27 +194,21 @@ func (app *localClient) CommitSync() (res types.Result) {
func (app *localClient) InitChainSync(validators []*types.Validator) (err error) {
app.mtx.Lock()
if bcApp, ok := app.Application.(types.BlockchainAware); ok {
bcApp.InitChain(validators)
}
app.Application.InitChain(validators)
app.mtx.Unlock()
return nil
}
func (app *localClient) BeginBlockSync(hash []byte, header *types.Header) (err error) {
app.mtx.Lock()
if bcApp, ok := app.Application.(types.BlockchainAware); ok {
bcApp.BeginBlock(hash, header)
}
app.Application.BeginBlock(hash, header)
app.mtx.Unlock()
return nil
}
func (app *localClient) EndBlockSync(height uint64) (resEndBlock types.ResponseEndBlock, err error) {
app.mtx.Lock()
if bcApp, ok := app.Application.(types.BlockchainAware); ok {
resEndBlock = bcApp.EndBlock(height)
}
resEndBlock = app.Application.EndBlock(height)
app.mtx.Unlock()
return resEndBlock, nil
}


example/chain_aware/chain_aware_app.go → example/block_aware/block_aware_app.go View File


example/chain_aware/chain_aware_test.go → example/block_aware/block_aware_test.go View File


+ 10
- 0
example/counter/counter.go View File

@ -80,3 +80,13 @@ func (app *CounterApplication) Query(reqQuery types.RequestQuery) types.Response
return types.ResponseQuery{Log: Fmt("Invalid query path. Expected hash or tx, got %v", reqQuery.Path)}
}
}
func (app *CounterApplication) InitChain(validators []*types.Validator) {
}
func (app *CounterApplication) BeginBlock(hash []byte, header *types.Header) {
}
func (app *CounterApplication) EndBlock(height uint64) types.ResponseEndBlock {
return types.ResponseEndBlock{}
}

+ 10
- 0
example/dummy/dummy.go View File

@ -70,3 +70,13 @@ func (app *DummyApplication) Query(reqQuery types.RequestQuery) (resQuery types.
return
}
}
func (app *DummyApplication) InitChain(validators []*types.Validator) {
}
func (app *DummyApplication) BeginBlock(hash []byte, header *types.Header) {
}
func (app *DummyApplication) EndBlock(height uint64) types.ResponseEndBlock {
return types.ResponseEndBlock{}
}

+ 2
- 3
example/dummy/dummy_test.go View File

@ -180,14 +180,13 @@ func makeApplyBlock(t *testing.T, dummy types.Application, heightInt int, diff [
Height: height,
}
dummyChain := dummy.(types.BlockchainAware) // hmm...
dummyChain.BeginBlock(hash, header)
dummy.BeginBlock(hash, header)
for _, tx := range txs {
if r := dummy.DeliverTx(tx); r.IsErr() {
t.Fatal(r)
}
}
resEndBlock := dummyChain.EndBlock(height)
resEndBlock := dummy.EndBlock(height)
dummy.Commit()
valsEqual(t, diff, resEndBlock.Diffs)


+ 10
- 0
example/nil/nil_app.go View File

@ -34,3 +34,13 @@ func (app *NilApplication) Commit() types.Result {
func (app *NilApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
return resQuery
}
func (app *NilApplication) InitChain(validators []*types.Validator) {
}
func (app *NilApplication) BeginBlock(hash []byte, header *types.Header) {
}
func (app *NilApplication) EndBlock(height uint64) types.ResponseEndBlock {
return types.ResponseEndBlock{}
}

+ 4
- 12
server/socket_server.go View File

@ -187,22 +187,14 @@ func (s *SocketServer) handleRequest(req *types.Request, responses chan<- *types
resQuery := s.app.Query(*r.Query)
responses <- types.ToResponseQuery(resQuery)
case *types.Request_InitChain:
if app, ok := s.app.(types.BlockchainAware); ok {
app.InitChain(r.InitChain.Validators)
}
s.app.InitChain(r.InitChain.Validators)
responses <- types.ToResponseInitChain()
case *types.Request_BeginBlock:
if app, ok := s.app.(types.BlockchainAware); ok {
app.BeginBlock(r.BeginBlock.Hash, r.BeginBlock.Header)
}
s.app.BeginBlock(r.BeginBlock.Hash, r.BeginBlock.Header)
responses <- types.ToResponseBeginBlock()
case *types.Request_EndBlock:
if app, ok := s.app.(types.BlockchainAware); ok {
resEndBlock := app.EndBlock(r.EndBlock.Height)
responses <- types.ToResponseEndBlock(resEndBlock)
} else {
responses <- types.ToResponseEndBlock(types.ResponseEndBlock{})
}
resEndBlock := s.app.EndBlock(r.EndBlock.Height)
responses <- types.ToResponseEndBlock(resEndBlock)
default:
responses <- types.ToResponseException("Unknown request")
}


+ 22
- 46
types/application.go View File

@ -6,42 +6,25 @@ import (
// Applications
type Application interface {
// Return application info
Info() ResponseInfo
// Set application option (e.g. mode=mempool, mode=consensus)
SetOption(key string, value string) (log string)
// Deliver a tx
DeliverTx(tx []byte) Result
// Validate a tx for the mempool
CheckTx(tx []byte) Result
// Query for state
Query(reqQuery RequestQuery) ResponseQuery
// Return the application Merkle root hash
Commit() Result
}
// Some applications can choose to implement BlockchainAware
type BlockchainAware interface {
// Initialize blockchain
// validators: genesis validators from TendermintCore
InitChain(validators []*Validator)
// Signals the beginning of a block
BeginBlock(hash []byte, header *Header)
// Signals the end of a block
// diffs: changed validators from app to TendermintCore
EndBlock(height uint64) ResponseEndBlock
// Info/Query Connection
Info() ResponseInfo // Return application info
SetOption(key string, value string) (log string) // Set application option
Query(reqQuery RequestQuery) ResponseQuery // Query for state
// Mempool Connection
CheckTx(tx []byte) Result // Validate a tx for the mempool
// Consensus Connection
InitChain(validators []*Validator) // Initialize blockchain with validators from TendermintCore
BeginBlock(hash []byte, header *Header) // Signals the beginning of a block
DeliverTx(tx []byte) Result // Deliver a tx for full processing
EndBlock(height uint64) ResponseEndBlock // Signals the end of a block, returns changes to the validator set
Commit() Result // Commit the state and return the application Merkle root hash
}
//------------------------------------
// GRPC wrapper for application
type GRPCApplication struct {
app Application
}
@ -88,23 +71,16 @@ func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*Re
}
func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
if chainAware, ok := app.app.(BlockchainAware); ok {
chainAware.InitChain(req.Validators)
}
return &ResponseInitChain{}, nil
app.app.InitChain(req.Validators)
return &ResponseInitChain{}, nil // NOTE: empty return
}
func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
if chainAware, ok := app.app.(BlockchainAware); ok {
chainAware.BeginBlock(req.Hash, req.Header)
}
return &ResponseBeginBlock{}, nil
app.app.BeginBlock(req.Hash, req.Header)
return &ResponseBeginBlock{}, nil // NOTE: empty return
}
func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
if chainAware, ok := app.app.(BlockchainAware); ok {
resEndBlock := chainAware.EndBlock(req.Height)
return &resEndBlock, nil
}
return &ResponseEndBlock{}, nil
resEndBlock := app.app.EndBlock(req.Height)
return &resEndBlock, nil
}

Loading…
Cancel
Save