diff --git a/client/client.go b/client/client.go index f4af5256..25de047f 100644 --- a/client/client.go +++ b/client/client.go @@ -215,6 +215,14 @@ func (cli *Client) QueryAsync(query []byte) *ReqRes { return cli.queueRequest(types.RequestQuery(query)) } +func (cli *Client) InitValidatorsAsync(validators []*types.Validator) *ReqRes { + return cli.queueRequest(types.RequestInitValidators(validators)) +} + +func (cli *Client) SyncValidatorsAsync() *ReqRes { + return cli.queueRequest(types.RequestSyncValidators()) +} + //---------------------------------------- func (cli *Client) FlushSync() error { @@ -280,6 +288,24 @@ func (cli *Client) QuerySync(query []byte) (code types.CodeType, result []byte, return res.Code, res.Data, res.Log, nil } +func (cli *Client) InitValidatorsSync(validators []*types.Validator) (err error) { + cli.queueRequest(types.RequestInitValidators(validators)) + cli.FlushSync() + if cli.err != nil { + return cli.err + } + return nil +} + +func (cli *Client) SyncValidatorsSync() (validators []*types.Validator, err error) { + reqres := cli.queueRequest(types.RequestSyncValidators()) + cli.FlushSync() + if cli.err != nil { + return nil, cli.err + } + return reqres.Response.Validators, nil +} + //---------------------------------------- func (cli *Client) queueRequest(req *types.Request) *ReqRes { diff --git a/server/server.go b/server/server.go index e3ac8a37..47a64dcd 100644 --- a/server/server.go +++ b/server/server.go @@ -148,6 +148,20 @@ func (s *Server) handleRequest(req *types.Request, responses chan<- *types.Respo case types.MessageType_Query: code, result, logStr := s.app.Query(req.Data) responses <- types.ResponseQuery(code, result, logStr) + case types.MessageType_InitValidators: + if app, ok := s.app.(types.ValidatorAware); ok { + app.InitValidators(req.Validators) + responses <- types.ResponseInitValidators() + } else { + responses <- types.ResponseInitValidators() + } + case types.MessageType_SyncValidators: + if app, ok := s.app.(types.ValidatorAware); ok { + validators := app.SyncValidators() + responses <- types.ResponseSyncValidators(validators) + } else { + responses <- types.ResponseSyncValidators(nil) + } default: responses <- types.ResponseException("Unknown request") } diff --git a/types/application.go b/types/application.go index d0379df0..74a2a5cc 100644 --- a/types/application.go +++ b/types/application.go @@ -1,5 +1,6 @@ package types +// Applications type Application interface { // Return application info @@ -20,3 +21,13 @@ type Application interface { // Query for state Query(query []byte) (code CodeType, result []byte, log string) } + +// Some applications can choose to implement ValidatorAware +type ValidatorAware interface { + + // Give app initial list of validators upon genesis + InitValidators([]*Validator) + + // Receive updates to validators from app, prior to commit + SyncValidators() []*Validator +} diff --git a/types/messages.go b/types/messages.go index 49c02bea..71a86c7d 100644 --- a/types/messages.go +++ b/types/messages.go @@ -61,6 +61,19 @@ func RequestQuery(queryBytes []byte) *Request { } } +func RequestInitValidators(validators []*Validator) *Request { + return &Request{ + Type: MessageType_InitValidators, + Validators: validators, + } +} + +func RequestSyncValidators() *Request { + return &Request{ + Type: MessageType_SyncValidators, + } +} + //---------------------------------------- func ResponseException(errStr string) *Response { @@ -132,6 +145,19 @@ func ResponseQuery(code CodeType, result []byte, log string) *Response { } } +func ResponseInitValidators() *Response { + return &Response{ + Type: MessageType_InitValidators, + } +} + +func ResponseSyncValidators(validators []*Validator) *Response { + return &Response{ + Type: MessageType_SyncValidators, + Validators: validators, + } +} + //---------------------------------------- // Write proto message, length delimited diff --git a/types/types.pb.go b/types/types.pb.go index a51b5123..81267107 100644 --- a/types/types.pb.go +++ b/types/types.pb.go @@ -37,8 +37,8 @@ const ( MessageType_CheckTx MessageType = 18 MessageType_Commit MessageType = 19 MessageType_Query MessageType = 20 - MessageType_InitValdiators MessageType = 21 - MessageType_SyncValdiators MessageType = 22 + MessageType_InitValidators MessageType = 21 + MessageType_SyncValidators MessageType = 22 ) var MessageType_name = map[int32]string{ @@ -52,8 +52,8 @@ var MessageType_name = map[int32]string{ 18: "CheckTx", 19: "Commit", 20: "Query", - 21: "InitValdiators", - 22: "SyncValdiators", + 21: "InitValidators", + 22: "SyncValidators", } var MessageType_value = map[string]int32{ "NullMessage": 0, @@ -66,8 +66,8 @@ var MessageType_value = map[string]int32{ "CheckTx": 18, "Commit": 19, "Query": 20, - "InitValdiators": 21, - "SyncValdiators": 22, + "InitValidators": 21, + "SyncValidators": 22, } func (x MessageType) String() string { @@ -177,22 +177,22 @@ func init() { } var fileDescriptor0 = []byte{ - // 486 bytes of a gzipped FileDescriptorProto + // 484 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xa4, 0x53, 0xcd, 0x6e, 0xd3, 0x40, 0x10, 0xc6, 0x89, 0xed, 0x24, 0x93, 0x34, 0xdd, 0x2c, 0x69, 0xe5, 0x63, 0x55, 0x24, 0x54, 0xf5, 0x50, 0x50, 0x38, 0x71, 0x2c, 0x51, 0x2a, 0x45, 0x15, 0xad, 0x70, 0x5b, 0xee, 0xae, 0x3d, 0x49, 0xac, 0xb8, 0xbb, 0xc6, 0xbb, 0x4b, 0x1b, 0x5e, 0x86, 0x27, 0xe0, 0xc8, 0x23, 0xf0, 0x5e, 0xcc, - 0xae, 0x1d, 0x14, 0x38, 0x21, 0xf5, 0x12, 0xcd, 0xf7, 0xcd, 0xdf, 0xf7, 0xcd, 0xc6, 0x30, 0xd2, + 0xae, 0x9d, 0x2a, 0x70, 0x42, 0xe2, 0x12, 0xcd, 0xf7, 0xcd, 0xdf, 0xf7, 0xcd, 0xc6, 0x30, 0xd2, 0x9b, 0x12, 0xd5, 0x1b, 0xf7, 0x7b, 0x56, 0x56, 0x52, 0x4b, 0x1e, 0x38, 0x70, 0xfc, 0xdd, 0x83, 0x4e, 0x8c, 0x5f, 0x0c, 0x2a, 0xcd, 0x5f, 0x83, 0x6f, 0xc9, 0xc8, 0x3b, 0xf2, 0x4e, 0x86, 0x13, 0x7e, 0x56, 0x97, 0x7f, 0x44, 0xa5, 0x92, 0x25, 0xde, 0x12, 0x88, 0x5d, 0x9e, 0x73, 0xf0, 0xb3, 0x44, 0x27, 0x51, 0x8b, 0xea, 0x06, 0xb1, 0x8b, 0x39, 0x83, 0xf6, 0x1a, 0x37, 0x51, 0x9b, 0xa8, 0x5e, 0x6c, 0x43, 0x3e, 0x86, 0xe0, 0x6b, 0x52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x51, 0x34, 0x14, 0x7b, 0xc1, 0xbb, 0xe0, 0xcf, 0xd2, 0x95, 0x64, 0x1e, 0xef, 0x41, 0x70, 0x51, 0x18, 0xb5, 0x62, @@ -200,13 +200,13 @@ var fileDescriptor0 = []byte{ 0x30, 0xdf, 0xc2, 0xd9, 0x53, 0x8a, 0x35, 0x0c, 0xf8, 0x00, 0xba, 0xe7, 0x65, 0x89, 0x22, 0xbb, 0x7d, 0x62, 0x23, 0xde, 0x87, 0xce, 0x74, 0x85, 0xe9, 0x9a, 0x00, 0x5d, 0x11, 0xc2, 0xa9, 0x7c, 0x78, 0xc8, 0x35, 0x7b, 0x69, 0x27, 0x7f, 0x32, 0x58, 0x6d, 0xd8, 0x98, 0xf8, 0xe1, 0x5c, 0xe4, - 0x9a, 0xec, 0x64, 0xb9, 0x33, 0xc7, 0x0e, 0x2c, 0x77, 0xb3, 0x11, 0xe9, 0x0e, 0x77, 0x78, 0xfa, - 0x83, 0x9e, 0x6e, 0x7b, 0x5f, 0x1e, 0x42, 0xeb, 0xfa, 0x92, 0xb4, 0x8e, 0x60, 0x6f, 0x2e, 0x34, - 0x56, 0x22, 0x29, 0x66, 0xf6, 0xb8, 0x24, 0x9a, 0xc1, 0xe0, 0x4e, 0x24, 0x46, 0xaf, 0x64, 0x95, - 0x7f, 0xc3, 0x8c, 0xb4, 0x8f, 0x81, 0xcd, 0x85, 0x32, 0x8b, 0x45, 0x9e, 0xe6, 0x28, 0xf4, 0x05, - 0xa2, 0x22, 0x1f, 0xb4, 0xe3, 0x4e, 0xac, 0x85, 0x7c, 0x14, 0xcd, 0x5f, 0x96, 0xcc, 0xd0, 0xb8, - 0x99, 0xa0, 0xa7, 0xcb, 0xc5, 0xb2, 0x1e, 0xe7, 0x0c, 0x7d, 0x48, 0xb2, 0x2b, 0x29, 0x52, 0x64, - 0xe1, 0x4e, 0xd3, 0x79, 0x9a, 0x4a, 0x23, 0x34, 0xeb, 0xf0, 0x03, 0x18, 0xfd, 0x35, 0xde, 0x88, - 0x4c, 0xb1, 0xee, 0x7d, 0xe8, 0x3e, 0x8d, 0x77, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0xa5, 0x71, - 0x5f, 0x3f, 0x2f, 0x03, 0x00, 0x00, + 0xfa, 0xd9, 0x8e, 0x62, 0x07, 0x96, 0xbb, 0xd9, 0x88, 0x74, 0x87, 0x3b, 0x3c, 0xfd, 0x41, 0x4f, + 0xb7, 0xbd, 0x2f, 0x0f, 0xa1, 0x75, 0x7d, 0x49, 0x5a, 0x47, 0xb0, 0x37, 0x17, 0x1a, 0x2b, 0x91, + 0x14, 0x33, 0x7b, 0x5c, 0x12, 0xcd, 0x60, 0x70, 0x27, 0x12, 0xa3, 0x57, 0xb2, 0xca, 0xbf, 0x61, + 0x46, 0xda, 0xc7, 0xc0, 0xe6, 0x42, 0x99, 0xc5, 0x22, 0x4f, 0x73, 0x14, 0xfa, 0x02, 0x51, 0x91, + 0x0f, 0xda, 0x71, 0x27, 0xd6, 0x42, 0x3e, 0x8a, 0xe6, 0x2f, 0x4b, 0x66, 0x68, 0xdc, 0x4c, 0xd0, + 0xd3, 0xe5, 0x62, 0x59, 0x8f, 0x73, 0x86, 0x3e, 0x24, 0xd9, 0x95, 0x14, 0x29, 0xb2, 0x70, 0xa7, + 0xe9, 0x3c, 0x4d, 0xa5, 0x11, 0x9a, 0x75, 0xf8, 0x01, 0x8c, 0xfe, 0x18, 0x6f, 0x44, 0xa6, 0x58, + 0xf7, 0x3e, 0x74, 0x9f, 0xc6, 0xbb, 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0x33, 0x74, 0xc9, 0x67, + 0x2f, 0x03, 0x00, 0x00, } diff --git a/types/types.proto b/types/types.proto index 000062a0..349fb701 100644 --- a/types/types.proto +++ b/types/types.proto @@ -18,8 +18,8 @@ enum MessageType { CheckTx = 0x12; Commit = 0x13; Query = 0x14; - InitValdiators = 0x15; - SyncValdiators = 0x16; + InitValidators = 0x15; + SyncValidators = 0x16; } //----------------------------------------