compiler: enforce Hash160 and Hash256 size in literals

Can be useful to prevent small typos.
This commit is contained in:
Evgenii Stratonikov 2020-12-10 14:08:42 +03:00
parent d828096cbf
commit ff4880249d
3 changed files with 78 additions and 21 deletions

View file

@ -15,6 +15,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
cinterop "github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/vm"
@ -23,6 +24,42 @@ import (
"go.uber.org/zap/zaptest"
)
func TestTypeConstantSize(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop"
var a %T // type declaration is always ok
func Main() interface{} {
return %#v
}`
t.Run("Hash160", func(t *testing.T) {
t.Run("good", func(t *testing.T) {
a := make(cinterop.Hash160, 20)
src := fmt.Sprintf(src, a, a)
eval(t, src, []byte(a))
})
t.Run("bad", func(t *testing.T) {
a := make(cinterop.Hash160, 19)
src := fmt.Sprintf(src, a, a)
_, err := compiler.Compile("foo.go", strings.NewReader(src))
require.Error(t, err)
})
})
t.Run("Hash256", func(t *testing.T) {
t.Run("good", func(t *testing.T) {
a := make(cinterop.Hash256, 32)
src := fmt.Sprintf(src, a, a)
eval(t, src, []byte(a))
})
t.Run("bad", func(t *testing.T) {
a := make(cinterop.Hash256, 31)
src := fmt.Sprintf(src, a, a)
_, err := compiler.Compile("foo.go", strings.NewReader(src))
require.Error(t, err)
})
})
}
func TestFromAddress(t *testing.T) {
as1 := "NQRLhCpAru9BjGsMwk67vdMwmzKMRgsnnN"
addr1, err := address.StringToUint160(as1)