From af7e20073bc6e5acb51e25b713d777f5268c51a5 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 22 Jun 2022 13:21:03 +0300 Subject: [PATCH] [#277] netmap: Allow more uint64 config values NEO VM uses little-endian format for integers, however the resulting byte slice can contain less than 8 bytes. Signed-off-by: Evgenii Stratonikov --- netmap/network_info.go | 11 +++++++++-- netmap/network_info_decode_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 netmap/network_info_decode_test.go diff --git a/netmap/network_info.go b/netmap/network_info.go index eb4ebb1..bc73fd4 100644 --- a/netmap/network_info.go +++ b/netmap/network_info.go @@ -257,12 +257,19 @@ func (x *NetworkInfo) setConfigUint64(name string, num uint64) { x.setConfig(name, val) } +// decodeConfigValueUint64 parses val as little-endian uint64. +// val must be less than 8 bytes in size. func decodeConfigValueUint64(val []byte) (uint64, error) { - if ln := len(val); ln != 8 { + if ln := len(val); ln > 8 { return 0, fmt.Errorf("invalid uint64 parameter length %d", ln) } - return binary.LittleEndian.Uint64(val), nil + res := uint64(0) + for i := len(val) - 1; i >= 0; i-- { + res = res*256 + uint64(val[i]) + } + + return res, nil } func (x NetworkInfo) configUint64(name string) uint64 { diff --git a/netmap/network_info_decode_test.go b/netmap/network_info_decode_test.go new file mode 100644 index 0000000..5929667 --- /dev/null +++ b/netmap/network_info_decode_test.go @@ -0,0 +1,28 @@ +package netmap + +import ( + "math/big" + "testing" + + "github.com/nspcc-dev/neo-go/pkg/encoding/bigint" + "github.com/stretchr/testify/require" +) + +func TestDecodeUint64(t *testing.T) { + testCases := []uint64{ + 0, + 12, + 129, + 0x1234, + 0x12345678, + 0x1234567891011, + } + + for _, expected := range testCases { + val := bigint.ToBytes(big.NewInt(int64(expected))) + + actual, err := decodeConfigValueUint64(val) + require.NoError(t, err) + require.Equal(t, expected, actual) + } +}