From 3162169916c61ce3e67d0257daecc13e5bd03e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20Buko=C5=A1ek?= Date: Wed, 6 May 2020 10:48:03 +0200 Subject: [PATCH] go/consensus: Add GetGenesisDocument --- .changelog/2889.feature.2.md | 4 +++ go/consensus/api/api.go | 3 ++ go/consensus/api/grpc.go | 33 +++++++++++++++++++ .../tendermint/epochtime/epochtime.go | 7 +++- .../epochtime_mock/epochtime_mock.go | 7 +++- go/consensus/tendermint/service/service.go | 4 --- go/consensus/tendermint/tendermint.go | 20 ++++------- go/consensus/tests/tester.go | 4 +++ 8 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 .changelog/2889.feature.2.md diff --git a/.changelog/2889.feature.2.md b/.changelog/2889.feature.2.md new file mode 100644 index 00000000000..724961c5347 --- /dev/null +++ b/.changelog/2889.feature.2.md @@ -0,0 +1,4 @@ +go/consensus: Add GetGenesisDocument + +The consensus client now has a new method to return the original +genesis document. diff --git a/go/consensus/api/api.go b/go/consensus/api/api.go index 1060196646f..c83287b7fad 100644 --- a/go/consensus/api/api.go +++ b/go/consensus/api/api.go @@ -80,6 +80,9 @@ type ClientBackend interface { // WatchBlocks returns a channel that produces a stream of consensus // blocks as they are being finalized. WatchBlocks(ctx context.Context) (<-chan *Block, pubsub.ClosableSubscription, error) + + // GetGenesisDocument returns the original genesis document. + GetGenesisDocument(ctx context.Context) (*genesis.Document, error) } // Block is a consensus block. diff --git a/go/consensus/api/grpc.go b/go/consensus/api/grpc.go index 61b9d4d0566..edf993bba97 100644 --- a/go/consensus/api/grpc.go +++ b/go/consensus/api/grpc.go @@ -34,6 +34,8 @@ var ( methodGetBlock = serviceName.NewMethod("GetBlock", int64(0)) // methodGetTransactions is the GetTransactions method. methodGetTransactions = serviceName.NewMethod("GetTransactions", int64(0)) + // methodGetGenesisDocument is the GetGenesisDocument method. + methodGetGenesisDocument = serviceName.NewMethod("GetGenesisDocument", nil) // methodWatchBlocks is the WatchBlocks method. methodWatchBlocks = serviceName.NewMethod("WatchBlocks", nil) @@ -82,6 +84,10 @@ var ( MethodName: methodGetTransactions.ShortName(), Handler: handlerGetTransactions, }, + { + MethodName: methodGetGenesisDocument.ShortName(), + Handler: handlerGetGenesisDocument, + }, }, Streams: []grpc.StreamDesc{ { @@ -297,6 +303,25 @@ func handlerGetTransactions( // nolint: golint return interceptor(ctx, height, info, handler) } +func handlerGetGenesisDocument( // nolint: golint + srv interface{}, + ctx context.Context, + dec func(interface{}) error, + interceptor grpc.UnaryServerInterceptor, +) (interface{}, error) { + if interceptor == nil { + return srv.(Backend).GetGenesisDocument(ctx) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: methodGetGenesisDocument.FullName(), + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(Backend).GetGenesisDocument(ctx) + } + return interceptor(ctx, nil, info, handler) +} + func handlerWatchBlocks(srv interface{}, stream grpc.ServerStream) error { if err := stream.RecvMsg(nil); err != nil { return err @@ -498,6 +523,14 @@ func (c *consensusClient) GetTransactions(ctx context.Context, height int64) ([] return rsp, nil } +func (c *consensusClient) GetGenesisDocument(ctx context.Context) (*genesis.Document, error) { + var rsp genesis.Document + if err := c.conn.Invoke(ctx, methodGetGenesisDocument.FullName(), nil, &rsp); err != nil { + return nil, err + } + return &rsp, nil +} + func (c *consensusClient) WatchBlocks(ctx context.Context) (<-chan *Block, pubsub.ClosableSubscription, error) { ctx, sub := pubsub.NewContextSubscription(ctx) diff --git a/go/consensus/tendermint/epochtime/epochtime.go b/go/consensus/tendermint/epochtime/epochtime.go index 9837fb07e25..3cb353e65e1 100644 --- a/go/consensus/tendermint/epochtime/epochtime.go +++ b/go/consensus/tendermint/epochtime/epochtime.go @@ -125,7 +125,12 @@ func (t *tendermintBackend) updateCached(ctx context.Context, block *tmtypes.Blo // New constructs a new tendermint backed epochtime Backend instance, // with the specified epoch interval. func New(ctx context.Context, service service.TendermintService, interval int64) (api.Backend, error) { - base := service.GetGenesis().EpochTime.Base + genDoc, err := service.GetGenesisDocument(ctx) + if err != nil { + return nil, err + } + + base := genDoc.EpochTime.Base r := &tendermintBackend{ logger: logging.GetLogger("epochtime/tendermint"), service: service, diff --git a/go/consensus/tendermint/epochtime_mock/epochtime_mock.go b/go/consensus/tendermint/epochtime_mock/epochtime_mock.go index 0e074cc2dbc..50df46dca52 100644 --- a/go/consensus/tendermint/epochtime_mock/epochtime_mock.go +++ b/go/consensus/tendermint/epochtime_mock/epochtime_mock.go @@ -249,7 +249,12 @@ func New(ctx context.Context, service service.TendermintService) (api.SetableBac } }) - if base := service.GetGenesis().EpochTime.Base; base != 0 { + genDoc, err := service.GetGenesisDocument(ctx) + if err != nil { + return nil, err + } + + if base := genDoc.EpochTime.Base; base != 0 { r.logger.Warn("ignoring non-zero base genesis epoch", "base", base, ) diff --git a/go/consensus/tendermint/service/service.go b/go/consensus/tendermint/service/service.go index a7a0729681e..4de1e7156f1 100644 --- a/go/consensus/tendermint/service/service.go +++ b/go/consensus/tendermint/service/service.go @@ -12,7 +12,6 @@ import ( "github.com/oasislabs/oasis-core/go/common/service" consensus "github.com/oasislabs/oasis-core/go/consensus/api" "github.com/oasislabs/oasis-core/go/consensus/tendermint/abci" - genesis "github.com/oasislabs/oasis-core/go/genesis/api" ) // TendermintService provides Tendermint access to Oasis core backends. @@ -33,9 +32,6 @@ type TendermintService interface { // ABCI multiplexer. SetTransactionAuthHandler(abci.TransactionAuthHandler) error - // GetGenesis will return the oasis genesis document. - GetGenesis() *genesis.Document - // GetHeight returns the Tendermint block height. GetHeight(ctx context.Context) (int64, error) diff --git a/go/consensus/tendermint/tendermint.go b/go/consensus/tendermint/tendermint.go index 98951bfab1a..136b378c82a 100644 --- a/go/consensus/tendermint/tendermint.go +++ b/go/consensus/tendermint/tendermint.go @@ -56,7 +56,6 @@ import ( tmstaking "github.com/oasislabs/oasis-core/go/consensus/tendermint/staking" epochtimeAPI "github.com/oasislabs/oasis-core/go/epochtime/api" genesisAPI "github.com/oasislabs/oasis-core/go/genesis/api" - "github.com/oasislabs/oasis-core/go/genesis/file" keymanagerAPI "github.com/oasislabs/oasis-core/go/keymanager/api" cmbackground "github.com/oasislabs/oasis-core/go/oasis-node/cmd/common/background" cmflags "github.com/oasislabs/oasis-core/go/oasis-node/cmd/common/flags" @@ -361,16 +360,9 @@ func (t *tendermintService) StateToGenesis(ctx context.Context, blockHeight int6 blockHeight = blk.Header.Height // Get initial genesis doc. - genesisFileProvider, err := file.DefaultFileProvider() + genesisDoc, err := t.GetGenesisDocument(ctx) if err != nil { - t.Logger.Error("failed getting genesis file provider", - "err", err, - ) - return nil, err - } - genesisDoc, err := genesisFileProvider.GetGenesisDocument() - if err != nil { - t.Logger.Error("failed getting genesis document from file provider", + t.Logger.Error("failed getting genesis document", "err", err, ) return nil, err @@ -449,6 +441,10 @@ func (t *tendermintService) StateToGenesis(ctx context.Context, blockHeight int6 }, nil } +func (t *tendermintService) GetGenesisDocument(ctx context.Context) (*genesisAPI.Document, error) { + return t.genesis, nil +} + func (t *tendermintService) RegisterHaltHook(hook func(context.Context, int64, epochtimeAPI.EpochTime)) { if !t.initialized() { return @@ -619,10 +615,6 @@ func (t *tendermintService) SetTransactionAuthHandler(handler abci.TransactionAu return t.mux.SetTransactionAuthHandler(handler) } -func (t *tendermintService) GetGenesis() *genesisAPI.Document { - return t.genesis -} - func (t *tendermintService) TransactionAuthHandler() consensusAPI.TransactionAuthHandler { return t.mux.TransactionAuthHandler() } diff --git a/go/consensus/tests/tester.go b/go/consensus/tests/tester.go index 2ad2e005628..b6209671061 100644 --- a/go/consensus/tests/tester.go +++ b/go/consensus/tests/tester.go @@ -28,6 +28,10 @@ func ConsensusImplementationTests(t *testing.T, backend consensus.ClientBackend) ctx, cancel := context.WithTimeout(context.Background(), recvTimeout) defer cancel() + genDoc, err := backend.GetGenesisDocument(ctx) + require.NoError(err, "GetGenesisDocument") + require.NotNil(genDoc, "returned genesis document should not be nil") + blk, err := backend.GetBlock(ctx, consensus.HeightLatest) require.NoError(err, "GetBlock") require.NotNil(blk, "returned block should not be nil")