Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
corverroos committed Oct 14, 2022
1 parent a0a0d4e commit 1e9a476
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cluster/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions cluster/test_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
30 changes: 30 additions & 0 deletions cluster/test_cluster_test.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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())
}

0 comments on commit 1e9a476

Please sign in to comment.