manifest: return an error from IsValid

Be more specific.
This commit is contained in:
Roman Khimov 2021-02-08 12:04:57 +03:00
parent 211fe307ee
commit 5ba074b4cf
4 changed files with 21 additions and 14 deletions

View file

@ -265,8 +265,9 @@ func (m *Management) Deploy(d dao.DAO, sender util.Uint160, neff *nef.File, mani
if err != nil {
return nil, err
}
if !manif.IsValid(h) {
return nil, errors.New("invalid manifest for this contract")
err = manif.IsValid(h)
if err != nil {
return nil, fmt.Errorf("invalid manifest: %w", err)
}
newcontract := &state.Contract{
ID: id,
@ -322,8 +323,9 @@ func (m *Management) Update(d dao.DAO, hash util.Uint160, neff *nef.File, manif
if manif.Name != contract.Manifest.Name {
return nil, errors.New("contract name can't be changed")
}
if !manif.IsValid(contract.Hash) {
return nil, errors.New("invalid manifest for this contract")
err = manif.IsValid(contract.Hash)
if err != nil {
return nil, fmt.Errorf("invalid manifest: %w", err)
}
m.markUpdated(hash)
contract.Manifest = *manif

View file

@ -71,13 +71,15 @@ func (m *Manifest) CanCall(hash util.Uint160, toCall *Manifest, method string) b
}
// IsValid checks whether the hash given is correct wrt manifest's groups.
func (m *Manifest) IsValid(hash util.Uint160) bool {
func (m *Manifest) IsValid(hash util.Uint160) error {
var err error
for _, g := range m.Groups {
if !g.IsValid(hash) {
return false
err = g.IsValid(hash)
if err != nil {
break
}
}
return true
return err
}
// ToStackItem converts Manifest to stackitem.Item.

View file

@ -112,7 +112,7 @@ func TestIsValid(t *testing.T) {
m := NewManifest("Test")
t.Run("valid, no groups", func(t *testing.T) {
require.True(t, m.IsValid(contractHash))
require.NoError(t, m.IsValid(contractHash))
})
t.Run("with groups", func(t *testing.T) {
@ -129,11 +129,11 @@ func TestIsValid(t *testing.T) {
}
t.Run("valid", func(t *testing.T) {
require.True(t, m.IsValid(contractHash))
require.NoError(t, m.IsValid(contractHash))
})
t.Run("invalid, wrong contract hash", func(t *testing.T) {
require.False(t, m.IsValid(util.Uint160{4, 5, 6}))
require.Error(t, m.IsValid(util.Uint160{4, 5, 6}))
})
t.Run("invalid, wrong group signature", func(t *testing.T) {
@ -145,7 +145,7 @@ func TestIsValid(t *testing.T) {
// of the contract hash.
Signature: pk.Sign([]byte{1, 2, 3}),
})
require.False(t, m.IsValid(contractHash))
require.Error(t, m.IsValid(contractHash))
})
})
}

View file

@ -56,8 +56,11 @@ func NewParameter(name string, typ smartcontract.ParamType) Parameter {
}
// IsValid checks whether group's signature corresponds to the given hash.
func (g *Group) IsValid(h util.Uint160) bool {
return g.PublicKey.Verify(g.Signature, hash.Sha256(h.BytesBE()).BytesBE())
func (g *Group) IsValid(h util.Uint160) error {
if !g.PublicKey.Verify(g.Signature, hash.Sha256(h.BytesBE()).BytesBE()) {
return errors.New("incorrect group signature")
}
return nil
}
// MarshalJSON implements json.Marshaler interface.