Skip to content

Commit

Permalink
Merge pull request #6007 from oasisprotocol/peternose/trivial/fix-leg…
Browse files Browse the repository at this point in the history
…acy-manifest-version

go/runtime/bundle/manifest: Fix legacy manifest version
  • Loading branch information
peternose authored Jan 21, 2025
2 parents 15b996e + e88dc1e commit e694d4f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
Empty file added .changelog/6007.trivial.md
Empty file.
8 changes: 7 additions & 1 deletion go/oasis-node/cmd/node/node_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
cmdFlags "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
p2p "github.com/oasisprotocol/oasis-core/go/p2p/api"
roothash "github.com/oasisprotocol/oasis-core/go/roothash/api"
"github.com/oasisprotocol/oasis-core/go/runtime/bundle/component"
storage "github.com/oasisprotocol/oasis-core/go/storage/api"
upgrade "github.com/oasisprotocol/oasis-core/go/upgrade/api"
keymanagerWorker "github.com/oasisprotocol/oasis-core/go/worker/keymanager/api"
Expand Down Expand Up @@ -359,7 +360,12 @@ func (n *Node) getBundleStatus() ([]control.BundleStatus, error) {
bundles := make([]control.BundleStatus, 0, len(manifests))

for _, manifest := range manifests {
explodedComponents, err := bundleRegistry.GetComponents(manifest.ID, manifest.GetVersion())
ronl := manifest.GetComponentByID(component.ID_RONL)
if ronl == nil {
continue
}

explodedComponents, err := bundleRegistry.GetComponents(manifest.ID, ronl.Version)
if err != nil {
return nil, err
}
Expand Down
10 changes: 0 additions & 10 deletions go/runtime/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,16 +575,6 @@ func Open(fn string, opts ...OpenOption) (_ *Bundle, err error) {
return nil, err
}

// Support legacy manifests where the runtime version is defined at the top level.
if bnd.Manifest.Version.ToU64() > 0 {
for _, comp := range bnd.Manifest.Components {
if comp.ID().IsRONL() {
comp.Version = bnd.Manifest.Version
break
}
}
}

return bnd, nil
}

Expand Down
42 changes: 30 additions & 12 deletions go/runtime/bundle/manifest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bundle

import (
"encoding/json"
"fmt"

"github.com/oasisprotocol/oasis-core/go/common"
Expand Down Expand Up @@ -44,6 +45,19 @@ type Manifest struct {

// Hash returns a cryptographic hash of the CBOR-serialized manifest.
func (m *Manifest) Hash() hash.Hash {
// Support legacy manifests where the runtime version is defined at the top level.
if m.Version.ToU64() > 0 {
for _, comp := range m.Components {
if comp.ID().IsRONL() {
v := comp.Version
comp.Version = version.Version{}
h := hash.NewFrom(m)
comp.Version = v
return h
}
}
}

return hash.NewFrom(m)
}

Expand Down Expand Up @@ -124,20 +138,24 @@ func (m *Manifest) GetComponentByID(id component.ID) *Component {
return nil
}

// GetVersion returns the runtime version.
func (m *Manifest) GetVersion() version.Version {
// We also support legacy manifests which define version at the top-level.
for _, comp := range m.Components {
if !comp.ID().IsRONL() {
continue
}
// UnmarshalJSON customizes the unmarshalling of the manifest.
func (m *Manifest) UnmarshalJSON(b []byte) (err error) {
// Unmarshal into the auxiliary struct to avoid recursion.
type alias Manifest
aux := (*alias)(m)
if err := json.Unmarshal(b, aux); err != nil {
return err
}

if comp.Version.ToU64() > m.Version.ToU64() {
return comp.Version
// Support legacy manifests where the runtime version is defined at the top level.
if m.Version.ToU64() > 0 {
for _, comp := range m.Components {
if comp.ID().IsRONL() {
comp.Version = m.Version
break
}
}

break
}

return m.Version
return nil
}
34 changes: 34 additions & 0 deletions go/runtime/bundle/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ const testManifestLegacyJSON = `
}
`

const testManifestLegacyVersionJSON = `
{
"name": "name",
"id": "0000000000000000000000000000000000000000000000000000000000000000",
"version": {
"major": 1,
"minor": 2,
"patch": 3
},
"components": [
{
"kind": "ronl"
}
]
}
`

func TestManifestHash_EmptyJSON(t *testing.T) {
var manifest Manifest
err := json.Unmarshal([]byte(testManifestEmptyJSON), &manifest)
Expand All @@ -128,6 +145,23 @@ func TestManifestHash_LegacyJSON(t *testing.T) {
require.Equal(t, "bd2987c59d2c672dee5c723b2d01adf6a7030c67efa368c342b54f887c1fe188", manifest.Hash().String())
}

func TestManifestHash_LegacyVersionJSON(t *testing.T) {
var manifest Manifest
err := json.Unmarshal([]byte(testManifestLegacyVersionJSON), &manifest)
require.NoError(t, err)

// Verify that the manifest version is correctly copied to the RONL component.
ronl := manifest.GetComponentByID(component.ID_RONL)
require.NotNil(t, ronl)
require.Equal(t, manifest.Version, ronl.Version)

// Ensure that the manifest hash remains unchanged even if the version is copied.
newHash := manifest.Hash()
ronl.Version = version.Version{}
legacyHash := manifest.Hash()
require.Equal(t, legacyHash, newHash)
}

func TestManifestHash_EmptyManifest(t *testing.T) {
var manifest Manifest
require.Equal(t, "dcc4f7c821d750caf4656cdee9f0bed52bee7697a00bab0aefb836149252cb1f", manifest.Hash().String())
Expand Down

0 comments on commit e694d4f

Please sign in to comment.