From 1e9a476e7088eda77e44d8d8cee8bd41a298d5ba Mon Sep 17 00:00:00 2001 From: corverroos Date: Fri, 14 Oct 2022 13:47:13 +0200 Subject: [PATCH] cleanup --- cluster/lock.go | 18 ++++++++++++++++++ cluster/test_cluster.go | 3 +++ cluster/test_cluster_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 cluster/test_cluster_test.go diff --git a/cluster/lock.go b/cluster/lock.go index 74fb534d2..bf324c5d2 100644 --- a/cluster/lock.go +++ b/cluster/lock.go @@ -123,6 +123,24 @@ func (l Lock) SetLockHash() (Lock, error) { return l, nil } +// VerifyHashes returns an error if hashes populated from json object doesn't matches actual hashes. +func (l Lock) VerifyHashes() error { + if err := l.Definition.VerifyHashes(); err != nil { + return errors.Wrap(err, "invalid definition") + } + + lockHash, err := hashLock(l) + if err != nil { + return err + } + + if !bytes.Equal(l.LockHash, lockHash[:]) { + return errors.New("invalid lock hash") + } + + return nil +} + // VerifySignatures returns true if all config signatures are fully populated and valid. // A verified lock is ready for use in charon run. func (l Lock) VerifySignatures() error { diff --git a/cluster/test_cluster.go b/cluster/test_cluster.go index b83a24a3b..56d2bd60c 100644 --- a/cluster/test_cluster.go +++ b/cluster/test_cluster.go @@ -132,6 +132,9 @@ func NewForT(t *testing.T, dv, k, n, seed int, opts ...func(*Definition)) (Lock, def.Operators[i], err = signOperator(p2pKeys[i], def, def.Operators[i]) require.NoError(t, err) } + + def, err = def.SetDefinitionHashes() + require.NoError(t, err) } lock := Lock{ diff --git a/cluster/test_cluster_test.go b/cluster/test_cluster_test.go new file mode 100644 index 000000000..7c9df9ebc --- /dev/null +++ b/cluster/test_cluster_test.go @@ -0,0 +1,30 @@ +// Copyright © 2022 Obol Labs Inc. +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +package cluster_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/obolnetwork/charon/cluster" +) + +func TestNewCluster(t *testing.T) { + lock, _, _ := cluster.NewForT(t, 3, 3, 3, 0) + require.NoError(t, lock.VerifyHashes()) + require.NoError(t, lock.VerifySignatures()) +}