Browse Source

use request structs for InitChain and BeginBlock

pull/1780/head
Ethan Buchman 7 years ago
parent
commit
f279171a28
12 changed files with 56 additions and 54 deletions
  1. +6
    -6
      client/client.go
  2. +8
    -8
      client/grpc_client.go
  3. +10
    -10
      client/local_client.go
  4. +8
    -8
      client/socket_client.go
  5. +1
    -1
      example/block_aware/block_aware_app.go
  6. +1
    -1
      example/block_aware/block_aware_test.go
  7. +4
    -4
      example/dummy/persistent_dummy.go
  8. +2
    -2
      server/socket_server.go
  9. +9
    -8
      types/application.go
  10. +2
    -2
      types/base_app.go
  11. +4
    -4
      types/messages.go
  12. +1
    -0
      types/result.go

+ 6
- 6
client/client.go View File

@ -20,7 +20,7 @@ type Client interface {
SetOptionAsync(key string, value string) *ReqRes
DeliverTxAsync(tx []byte) *ReqRes
CheckTxAsync(tx []byte) *ReqRes
QueryAsync(reqQuery types.RequestQuery) *ReqRes
QueryAsync(types.RequestQuery) *ReqRes
CommitAsync() *ReqRes
FlushSync() error
@ -29,15 +29,15 @@ type Client interface {
SetOptionSync(key string, value string) (res types.Result)
DeliverTxSync(tx []byte) (res types.Result)
CheckTxSync(tx []byte) (res types.Result)
QuerySync(reqQuery types.RequestQuery) (resQuery types.ResponseQuery, err error)
QuerySync(types.RequestQuery) (resQuery types.ResponseQuery, err error)
CommitSync() (res types.Result)
InitChainAsync(validators []*types.Validator) *ReqRes
BeginBlockAsync(hash []byte, header *types.Header) *ReqRes
InitChainAsync(types.RequestInitChain) *ReqRes
BeginBlockAsync(types.RequestBeginBlock) *ReqRes
EndBlockAsync(height uint64) *ReqRes
InitChainSync(validators []*types.Validator) (err error)
BeginBlockSync(hash []byte, header *types.Header) (err error)
InitChainSync(types.RequestInitChain) (err error)
BeginBlockSync(types.RequestBeginBlock) (err error)
EndBlockSync(height uint64) (resEndBlock types.ResponseEndBlock, err error)
}


+ 8
- 8
client/grpc_client.go View File

@ -190,8 +190,8 @@ func (cli *grpcClient) CommitAsync() *ReqRes {
return cli.finishAsyncCall(req, &types.Response{&types.Response_Commit{res}})
}
func (cli *grpcClient) InitChainAsync(validators []*types.Validator) *ReqRes {
req := types.ToRequestInitChain(validators)
func (cli *grpcClient) InitChainAsync(params types.RequestInitChain) *ReqRes {
req := types.ToRequestInitChain(params)
res, err := cli.client.InitChain(context.Background(), req.GetInitChain(), grpc.FailFast(true))
if err != nil {
cli.StopForError(err)
@ -199,8 +199,8 @@ func (cli *grpcClient) InitChainAsync(validators []*types.Validator) *ReqRes {
return cli.finishAsyncCall(req, &types.Response{&types.Response_InitChain{res}})
}
func (cli *grpcClient) BeginBlockAsync(hash []byte, header *types.Header) *ReqRes {
req := types.ToRequestBeginBlock(hash, header)
func (cli *grpcClient) BeginBlockAsync(params types.RequestBeginBlock) *ReqRes {
req := types.ToRequestBeginBlock(params)
res, err := cli.client.BeginBlock(context.Background(), req.GetBeginBlock(), grpc.FailFast(true))
if err != nil {
cli.StopForError(err)
@ -319,13 +319,13 @@ func (cli *grpcClient) CommitSync() (res types.Result) {
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
}
func (cli *grpcClient) InitChainSync(validators []*types.Validator) (err error) {
cli.InitChainAsync(validators)
func (cli *grpcClient) InitChainSync(params types.RequestInitChain) (err error) {
cli.InitChainAsync(params)
return cli.Error()
}
func (cli *grpcClient) BeginBlockSync(hash []byte, header *types.Header) (err error) {
cli.BeginBlockAsync(hash, header)
func (cli *grpcClient) BeginBlockSync(params types.RequestBeginBlock) (err error) {
cli.BeginBlockAsync(params)
return cli.Error()
}


+ 10
- 10
client/local_client.go View File

@ -109,23 +109,23 @@ func (app *localClient) CommitAsync() *ReqRes {
)
}
func (app *localClient) InitChainAsync(validators []*types.Validator) *ReqRes {
func (app *localClient) InitChainAsync(params types.RequestInitChain) *ReqRes {
app.mtx.Lock()
app.Application.InitChain(validators)
app.Application.InitChain(params)
reqRes := app.callback(
types.ToRequestInitChain(validators),
types.ToRequestInitChain(params),
types.ToResponseInitChain(),
)
app.mtx.Unlock()
return reqRes
}
func (app *localClient) BeginBlockAsync(hash []byte, header *types.Header) *ReqRes {
func (app *localClient) BeginBlockAsync(params types.RequestBeginBlock) *ReqRes {
app.mtx.Lock()
app.Application.BeginBlock(hash, header)
app.Application.BeginBlock(params)
app.mtx.Unlock()
return app.callback(
types.ToRequestBeginBlock(hash, header),
types.ToRequestBeginBlock(params),
types.ToResponseBeginBlock(),
)
}
@ -192,16 +192,16 @@ func (app *localClient) CommitSync() (res types.Result) {
return res
}
func (app *localClient) InitChainSync(validators []*types.Validator) (err error) {
func (app *localClient) InitChainSync(params types.RequestInitChain) (err error) {
app.mtx.Lock()
app.Application.InitChain(validators)
app.Application.InitChain(params)
app.mtx.Unlock()
return nil
}
func (app *localClient) BeginBlockSync(hash []byte, header *types.Header) (err error) {
func (app *localClient) BeginBlockSync(params types.RequestBeginBlock) (err error) {
app.mtx.Lock()
app.Application.BeginBlock(hash, header)
app.Application.BeginBlock(params)
app.mtx.Unlock()
return nil
}


+ 8
- 8
client/socket_client.go View File

@ -255,12 +255,12 @@ func (cli *socketClient) CommitAsync() *ReqRes {
return cli.queueRequest(types.ToRequestCommit())
}
func (cli *socketClient) InitChainAsync(validators []*types.Validator) *ReqRes {
return cli.queueRequest(types.ToRequestInitChain(validators))
func (cli *socketClient) InitChainAsync(params types.RequestInitChain) *ReqRes {
return cli.queueRequest(types.ToRequestInitChain(params))
}
func (cli *socketClient) BeginBlockAsync(hash []byte, header *types.Header) *ReqRes {
return cli.queueRequest(types.ToRequestBeginBlock(hash, header))
func (cli *socketClient) BeginBlockAsync(params types.RequestBeginBlock) *ReqRes {
return cli.queueRequest(types.ToRequestBeginBlock(params))
}
func (cli *socketClient) EndBlockAsync(height uint64) *ReqRes {
@ -352,8 +352,8 @@ func (cli *socketClient) CommitSync() (res types.Result) {
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
}
func (cli *socketClient) InitChainSync(validators []*types.Validator) (err error) {
cli.queueRequest(types.ToRequestInitChain(validators))
func (cli *socketClient) InitChainSync(params types.RequestInitChain) (err error) {
cli.queueRequest(types.ToRequestInitChain(params))
cli.FlushSync()
if err := cli.Error(); err != nil {
return err
@ -361,8 +361,8 @@ func (cli *socketClient) InitChainSync(validators []*types.Validator) (err error
return nil
}
func (cli *socketClient) BeginBlockSync(hash []byte, header *types.Header) (err error) {
cli.queueRequest(types.ToRequestBeginBlock(hash, header))
func (cli *socketClient) BeginBlockSync(params types.RequestBeginBlock) (err error) {
cli.queueRequest(types.ToRequestBeginBlock(params))
cli.FlushSync()
if err := cli.Error(); err != nil {
return err


+ 1
- 1
example/block_aware/block_aware_app.go View File

@ -55,7 +55,7 @@ func (app *ChainAwareApplication) Query(reqQuery types.RequestQuery) (resQuery t
}
}
func (app *ChainAwareApplication) BeginBlock(hash []byte, header *types.Header) {
func (app *ChainAwareApplication) BeginBlock(reqBeginBlock types.RequestBeginBlock) {
app.beginCount++
return
}


+ 1
- 1
example/block_aware/block_aware_test.go View File

@ -37,7 +37,7 @@ func TestChainAware(t *testing.T) {
hash := []byte("fake block hash")
header := &types.Header{}
for i := uint64(0); i < n; i++ {
client.BeginBlockSync(hash, header)
client.BeginBlockSync(&types.RequestBeginBlock{hash, header})
client.EndBlockSync(i)
client.CommitSync()
}


+ 4
- 4
example/dummy/persistent_dummy.go View File

@ -106,8 +106,8 @@ func (app *PersistentDummyApplication) Query(reqQuery types.RequestQuery) types.
}
// Save the validators in the merkle tree
func (app *PersistentDummyApplication) InitChain(validators []*types.Validator) {
for _, v := range validators {
func (app *PersistentDummyApplication) InitChain(params types.RequestInitChain) {
for _, v := range params.Validators {
r := app.updateValidator(v)
if r.IsErr() {
app.logger.Error("Error updating validators", "r", r)
@ -116,9 +116,9 @@ func (app *PersistentDummyApplication) InitChain(validators []*types.Validator)
}
// Track the block hash and header information
func (app *PersistentDummyApplication) BeginBlock(hash []byte, header *types.Header) {
func (app *PersistentDummyApplication) BeginBlock(params types.RequestBeginBlock) {
// update latest block info
app.blockHeader = header
app.blockHeader = params.Header
// reset valset changes
app.changes = make([]*types.Validator, 0)


+ 2
- 2
server/socket_server.go View File

@ -186,10 +186,10 @@ func (s *SocketServer) handleRequest(req *types.Request, responses chan<- *types
resQuery := s.app.Query(*r.Query)
responses <- types.ToResponseQuery(resQuery)
case *types.Request_InitChain:
s.app.InitChain(r.InitChain.Validators)
s.app.InitChain(*r.InitChain)
responses <- types.ToResponseInitChain()
case *types.Request_BeginBlock:
s.app.BeginBlock(r.BeginBlock.Hash, r.BeginBlock.Header)
s.app.BeginBlock(*r.BeginBlock)
responses <- types.ToResponseBeginBlock()
case *types.Request_EndBlock:
resEndBlock := s.app.EndBlock(r.EndBlock.Height)


+ 9
- 8
types/application.go View File

@ -4,27 +4,28 @@ import (
context "golang.org/x/net/context"
)
// Applications
// Application is an interface that enables any finite, deterministic state machine
// to be driven by a blockchain-based replication engine via the ABCI
type Application interface {
// 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
Query(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
InitChain(RequestInitChain) // Initialize blockchain with validators and other info from TendermintCore
BeginBlock(RequestBeginBlock) // 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
// GRPCApplication is a GRPC wrapper for Application
type GRPCApplication struct {
app Application
}
@ -71,12 +72,12 @@ func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*Re
}
func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
app.app.InitChain(req.Validators)
app.app.InitChain(*req)
return &ResponseInitChain{}, nil // NOTE: empty return
}
func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
app.app.BeginBlock(req.Hash, req.Header)
app.app.BeginBlock(*req)
return &ResponseBeginBlock{}, nil // NOTE: empty return
}


+ 2
- 2
types/base_app.go View File

@ -31,10 +31,10 @@ func (app *BaseApplication) Query(reqQuery RequestQuery) (resQuery ResponseQuery
return
}
func (app *BaseApplication) InitChain(validators []*Validator) {
func (app *BaseApplication) InitChain(reqInitChain RequestInitChain) {
}
func (app *BaseApplication) BeginBlock(hash []byte, header *Header) {
func (app *BaseApplication) BeginBlock(reqBeginBlock RequestBeginBlock) {
}
func (app *BaseApplication) EndBlock(height uint64) (resEndBlock ResponseEndBlock) {


+ 4
- 4
types/messages.go View File

@ -55,15 +55,15 @@ func ToRequestQuery(reqQuery RequestQuery) *Request {
}
}
func ToRequestInitChain(validators []*Validator) *Request {
func ToRequestInitChain(reqInitChain RequestInitChain) *Request {
return &Request{
Value: &Request_InitChain{&RequestInitChain{validators}},
Value: &Request_InitChain{&reqInitChain},
}
}
func ToRequestBeginBlock(hash []byte, header *Header) *Request {
func ToRequestBeginBlock(reqBeginBlock RequestBeginBlock) *Request {
return &Request{
Value: &Request_BeginBlock{&RequestBeginBlock{hash, header}},
Value: &Request_BeginBlock{&reqBeginBlock},
}
}


+ 1
- 0
types/result.go View File

@ -6,6 +6,7 @@ import (
"github.com/tendermint/go-wire/data"
)
// Result is a common result object for ABCI calls.
// CONTRACT: a zero Result is OK.
type Result struct {
Code CodeType `json:"code"`


Loading…
Cancel
Save