Skip to content

Commit

Permalink
go/consensus: Add GetGenesisDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
abukosek committed May 6, 2020
1 parent 2566f8e commit 45d3577
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .changelog/2889.feature.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go/consensus: Add GetGenesisDocument

The consensus client now has a new method to return the original
genesis document.
3 changes: 3 additions & 0 deletions go/consensus/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions go/consensus/api/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -82,6 +84,10 @@ var (
MethodName: methodGetTransactions.ShortName(),
Handler: handlerGetTransactions,
},
{
MethodName: methodGetGenesisDocument.ShortName(),
Handler: handlerGetGenesisDocument,
},
},
Streams: []grpc.StreamDesc{
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 6 additions & 1 deletion go/consensus/tendermint/epochtime/epochtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion go/consensus/tendermint/epochtime_mock/epochtime_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 0 additions & 4 deletions go/consensus/tendermint/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand Down
20 changes: 6 additions & 14 deletions go/consensus/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
4 changes: 4 additions & 0 deletions go/consensus/tests/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 45d3577

Please sign in to comment.