From 82a84be283304245ea817599671dbe6c72b5c3e8 Mon Sep 17 00:00:00 2001 From: Mitja T Date: Fri, 11 Aug 2023 12:18:50 -0700 Subject: [PATCH] nodeapi: Make CommitteeKind compatbile across cobalt and damask --- storage/oasis/nodeapi/api.go | 7 +++- storage/oasis/nodeapi/cobalt/convert.go | 2 +- storage/oasis/nodeapi/compat_types.go | 56 +++++++++++++++++++++++++ storage/oasis/nodeapi/damask/node.go | 7 +++- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 storage/oasis/nodeapi/compat_types.go diff --git a/storage/oasis/nodeapi/api.go b/storage/oasis/nodeapi/api.go index aee86270f..e5be724b2 100644 --- a/storage/oasis/nodeapi/api.go +++ b/storage/oasis/nodeapi/api.go @@ -190,7 +190,12 @@ type ( type ( Validator scheduler.Validator - Committee scheduler.Committee + Committee struct { + Kind CommitteeKind + Members []*scheduler.CommitteeNode + RuntimeID coreCommon.Namespace + ValidFor beacon.EpochTime + } ) // .................... Governance .................... diff --git a/storage/oasis/nodeapi/cobalt/convert.go b/storage/oasis/nodeapi/cobalt/convert.go index ba31f057b..6b1612935 100644 --- a/storage/oasis/nodeapi/cobalt/convert.go +++ b/storage/oasis/nodeapi/cobalt/convert.go @@ -394,7 +394,7 @@ func convertCommittee(c schedulerCobalt.Committee) nodeapi.Committee { } } return nodeapi.Committee{ - Kind: scheduler.CommitteeKind(c.Kind), // We assume the enum is backwards-compatible. + Kind: nodeapi.CommitteeKind(c.Kind), // The enum is compatible between Cobalt and Damask. Members: members, RuntimeID: c.RuntimeID, ValidFor: c.ValidFor, diff --git a/storage/oasis/nodeapi/compat_types.go b/storage/oasis/nodeapi/compat_types.go new file mode 100644 index 000000000..fdcb3144e --- /dev/null +++ b/storage/oasis/nodeapi/compat_types.go @@ -0,0 +1,56 @@ +// The nodeapi package provides a low-level interface to the Oasis node API. +// The types that it exposes are simplified versions of the types exposed by +// oasis-core Damask: The top-level type structs are defined in api.go, and +// the types of their fields are almost universally directly the types exposed +// by oasis-core Damask. The reason is that as oasis-core evolves, Damask types +// are mostly able to represent all the information from Cobalt, plus some. +// +// This file contains the exceptions to the above rule: It provides substitute +// types for those Damask types that cannot express Cobalt information. + +package nodeapi + +import "fmt" + +// Copy-pasted from Cobalt; Damask does not support the "storage" kind. +type CommitteeKind uint8 + +const ( + KindInvalid CommitteeKind = 0 + KindComputeExecutor CommitteeKind = 1 + KindStorage CommitteeKind = 2 + + // MaxCommitteeKind is a dummy value used for iterating all committee kinds. + MaxCommitteeKind = 3 + + KindInvalidName = "invalid" + KindComputeExecutorName = "executor" + KindStorageName = "storage" +) + +// MarshalText encodes a CommitteeKind into text form. +func (k CommitteeKind) MarshalText() ([]byte, error) { + switch k { + case KindInvalid: + return []byte(KindInvalidName), nil + case KindComputeExecutor: + return []byte(KindComputeExecutorName), nil + case KindStorage: + return []byte(KindStorageName), nil + default: + return nil, fmt.Errorf("invalid role: %d", k) + } +} + +// UnmarshalText decodes a text slice into a CommitteeKind. +func (k *CommitteeKind) UnmarshalText(text []byte) error { + switch string(text) { + case KindComputeExecutorName: + *k = KindComputeExecutor + case KindStorageName: + *k = KindStorage + default: + return fmt.Errorf("invalid role: %s", string(text)) + } + return nil +} diff --git a/storage/oasis/nodeapi/damask/node.go b/storage/oasis/nodeapi/damask/node.go index 8fb0e5cbd..2f3fdc3f8 100644 --- a/storage/oasis/nodeapi/damask/node.go +++ b/storage/oasis/nodeapi/damask/node.go @@ -174,7 +174,12 @@ func (c *DamaskConsensusApiLite) GetCommittees(ctx context.Context, height int64 } committees := make([]nodeapi.Committee, len(rsp)) for i, c := range rsp { - committees[i] = nodeapi.Committee(*c) + committees[i] = nodeapi.Committee{ + Kind: nodeapi.CommitteeKind(c.Kind), // The enum is compatible between Cobalt and Damask. + Members: c.Members, + RuntimeID: c.RuntimeID, + ValidFor: c.ValidFor, + } } return committees, nil }