Skip to content

Commit

Permalink
Merge pull request #502 from oasisprotocol/mitjat/committeekind-compat
Browse files Browse the repository at this point in the history
nodeapi: Make CommitteeKind compatbile across cobalt and damask
  • Loading branch information
mitjat authored Aug 16, 2023
2 parents 5cdf94e + 82a84be commit 2a30866
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
7 changes: 6 additions & 1 deletion storage/oasis/nodeapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ....................
Expand Down
2 changes: 1 addition & 1 deletion storage/oasis/nodeapi/cobalt/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
56 changes: 56 additions & 0 deletions storage/oasis/nodeapi/compat_types.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 6 additions & 1 deletion storage/oasis/nodeapi/damask/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 2a30866

Please sign in to comment.