github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/uint_test.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package types_test
    18  
    19  import (
    20  	"bytes"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale"
    25  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestU8_EncodeDecode(t *testing.T) {
    30  	assertRoundtrip(t, NewU8(0))
    31  	assertRoundtrip(t, NewU8(12))
    32  }
    33  
    34  func TestU8_EncodedLength(t *testing.T) {
    35  	assertEncodedLength(t, []encodedLengthAssert{{NewU8(13), 1}})
    36  }
    37  
    38  func TestU8_Encode(t *testing.T) {
    39  	assertEncode(t, []encodingAssert{
    40  		{NewU8(29), MustHexDecodeString("0x1d")},
    41  	})
    42  }
    43  
    44  func TestU8_Hash(t *testing.T) {
    45  	assertHash(t, []hashAssert{
    46  		{NewU8(29), MustHexDecodeString("0x6a9843ae0195ae1e6f95c7fbd34a42414c77e243aa18a959b5912a1f0f391b54")},
    47  	})
    48  }
    49  
    50  func TestU8_Hex(t *testing.T) {
    51  	assertEncodeToHex(t, []encodeToHexAssert{
    52  		{NewU8(29), "0x1d"},
    53  	})
    54  }
    55  
    56  func TestU8_String(t *testing.T) {
    57  	assertString(t, []stringAssert{
    58  		{NewU8(29), "29"},
    59  	})
    60  }
    61  
    62  func TestU8_Eq(t *testing.T) {
    63  	assertEq(t, []eqAssert{
    64  		{NewU8(23), NewU8(23), true},
    65  		{NewU8(23), NewBool(false), false},
    66  	})
    67  }
    68  
    69  func TestU16_EncodeDecode(t *testing.T) {
    70  	assertRoundtrip(t, NewU16(0))
    71  	assertRoundtrip(t, NewU16(12))
    72  }
    73  
    74  func TestU16_EncodedLength(t *testing.T) {
    75  	assertEncodedLength(t, []encodedLengthAssert{{NewU16(13), 2}})
    76  }
    77  
    78  func TestU16_Encode(t *testing.T) {
    79  	assertEncode(t, []encodingAssert{
    80  		{NewU16(29), MustHexDecodeString("0x1d00")},
    81  	})
    82  }
    83  
    84  func TestU16_Hash(t *testing.T) {
    85  	assertHash(t, []hashAssert{
    86  		{NewU16(29), MustHexDecodeString("0x4e59f743a8e19ecb3022652bdef4343e62793d1f7378a688a82741b5d029d3d5")},
    87  	})
    88  }
    89  
    90  func TestU16_Hex(t *testing.T) {
    91  	assertEncodeToHex(t, []encodeToHexAssert{
    92  		{NewU16(29), "0x1d00"},
    93  	})
    94  }
    95  
    96  func TestU16_String(t *testing.T) {
    97  	assertString(t, []stringAssert{
    98  		{NewU16(29), "29"},
    99  	})
   100  }
   101  
   102  func TestU16_Eq(t *testing.T) {
   103  	assertEq(t, []eqAssert{
   104  		{NewU16(23), NewU16(23), true},
   105  		{NewU16(23), NewBool(false), false},
   106  	})
   107  }
   108  
   109  func TestU32_EncodeDecode(t *testing.T) {
   110  	assertRoundtrip(t, NewU32(0))
   111  	assertRoundtrip(t, NewU32(12))
   112  }
   113  
   114  func TestU32_EncodedLength(t *testing.T) {
   115  	assertEncodedLength(t, []encodedLengthAssert{{NewU32(13), 4}})
   116  }
   117  
   118  func TestU32_Encode(t *testing.T) {
   119  	assertEncode(t, []encodingAssert{
   120  		{NewU32(29), MustHexDecodeString("0x1d000000")},
   121  	})
   122  }
   123  
   124  func TestU32_Hash(t *testing.T) {
   125  	assertHash(t, []hashAssert{
   126  		{NewU32(29), MustHexDecodeString("0x60ebb66f09bc7fdd21772ab1ed0efb1fd1208e3f5cd20d2d9a29a2a79b6f953f")},
   127  	})
   128  }
   129  
   130  func TestU32_Hex(t *testing.T) {
   131  	assertEncodeToHex(t, []encodeToHexAssert{
   132  		{NewU32(29), "0x1d000000"},
   133  	})
   134  }
   135  
   136  func TestU32_String(t *testing.T) {
   137  	assertString(t, []stringAssert{
   138  		{NewU32(29), "29"},
   139  	})
   140  }
   141  
   142  func TestU32_Eq(t *testing.T) {
   143  	assertEq(t, []eqAssert{
   144  		{NewU32(23), NewU32(23), true},
   145  		{NewU32(23), NewBool(false), false},
   146  	})
   147  }
   148  
   149  func TestU64_EncodeDecode(t *testing.T) {
   150  	assertRoundtrip(t, NewU64(0))
   151  	assertRoundtrip(t, NewU64(12))
   152  }
   153  
   154  func TestU64_EncodedLength(t *testing.T) {
   155  	assertEncodedLength(t, []encodedLengthAssert{{NewU64(13), 8}})
   156  }
   157  
   158  func TestU64_Encode(t *testing.T) {
   159  	assertEncode(t, []encodingAssert{
   160  		{NewU64(29), MustHexDecodeString("0x1d00000000000000")},
   161  	})
   162  }
   163  
   164  func TestU64_Hash(t *testing.T) {
   165  	assertHash(t, []hashAssert{
   166  		{NewU64(29), MustHexDecodeString("0x83e168a13a013e6d47b0778f046aaa05d6c01d6857d044d9e9b658a6d85eb865")},
   167  	})
   168  }
   169  
   170  func TestU64_Hex(t *testing.T) {
   171  	assertEncodeToHex(t, []encodeToHexAssert{
   172  		{NewU64(29), "0x1d00000000000000"},
   173  	})
   174  }
   175  
   176  func TestUCompact_EncodeDecode(t *testing.T) {
   177  	bn := MustHexDecodeString("0x5C2D3BE75CEF559F050") //27205758526767196926032
   178  	uc := NewUCompact(big.NewInt(0).SetBytes(bn))
   179  
   180  	// Encode
   181  	var buffer = bytes.Buffer{}
   182  	err := scale.NewEncoder(&buffer).Encode(uc)
   183  	assert.NoError(t, err)
   184  	assert.Equal(t, buffer.Bytes(), MustHexDecodeString("0x1b50f059f5ce75bed3c205")) // Encoded number above
   185  
   186  	// Decode
   187  	dec := scale.NewDecoder(bytes.NewReader(buffer.Bytes()))
   188  	var res UCompact
   189  	err = dec.Decode(&res)
   190  	assert.NoError(t, err)
   191  	assert.Equal(t, uc, res)
   192  }
   193  
   194  func TestUCompact_EncodeDecode_MaxValue(t *testing.T) {
   195  	// Valid Max Number Encode/Decode [ 67 bytes -> MAX = (2**536)-1 ]
   196  	bigNumber := []byte{
   197  		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   198  		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   199  		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   200  		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   201  		0xff, 0xff, 0xff,
   202  	}
   203  	maxValue := NewUCompact(new(big.Int).SetBytes(bigNumber))
   204  	expectedEncoded := MustHexDecodeString("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
   205  
   206  	var buffer = bytes.Buffer{}
   207  	err := scale.NewEncoder(&buffer).Encode(maxValue)
   208  	assert.NoError(t, err)
   209  	assert.Equal(t, buffer.Bytes(), expectedEncoded)
   210  
   211  	dec := scale.NewDecoder(bytes.NewReader(buffer.Bytes()))
   212  	var res UCompact
   213  	err = dec.Decode(&res)
   214  	assert.NoError(t, err)
   215  	assert.Equal(t, maxValue, res)
   216  
   217  	// Invalid max number Encode/Decode [ 68 bytes ]
   218  	bigNumber = append(bigNumber, []byte{0xff}...)
   219  	maxValue = NewUCompact(new(big.Int).SetBytes(bigNumber))
   220  
   221  	buffer = bytes.Buffer{}
   222  	err = scale.NewEncoder(&buffer).Encode(maxValue)
   223  	assert.Error(t, err)
   224  
   225  	// Invalid big number max length field max 256 - 272
   226  	reallyBigNumber := append(bigNumber, append(bigNumber, append(bigNumber, bigNumber...)...)...)
   227  	maxValue = NewUCompact(new(big.Int).SetBytes(reallyBigNumber))
   228  
   229  	buffer = bytes.Buffer{}
   230  	err = scale.NewEncoder(&buffer).Encode(maxValue)
   231  	assert.Error(t, err)
   232  
   233  	// Decoding truncates at max length
   234  	expectedEncoded = append(expectedEncoded, []byte{0xab, 0xff, 0x34}...)
   235  	dec = scale.NewDecoder(bytes.NewReader(expectedEncoded))
   236  	var res1 UCompact
   237  	err = dec.Decode(&res1)
   238  	assert.NoError(t, err)
   239  	assert.Equal(t, res, res1)
   240  }
   241  
   242  func TestUCompact_EncodeNegative(t *testing.T) {
   243  	negNumber := NewUCompact(big.NewInt(-100))
   244  	var buffer = bytes.Buffer{}
   245  	err := scale.NewEncoder(&buffer).Encode(negNumber)
   246  	assert.Error(t, err)
   247  }
   248  
   249  func TestU64_String(t *testing.T) {
   250  	assertString(t, []stringAssert{
   251  		{NewU64(29), "29"},
   252  	})
   253  }
   254  
   255  func TestU64_Eq(t *testing.T) {
   256  	assertEq(t, []eqAssert{
   257  		{NewU64(23), NewU64(23), true},
   258  		{NewU64(23), NewBool(false), false},
   259  	})
   260  }
   261  
   262  func TestU128_EncodeDecode(t *testing.T) {
   263  	assertRoundtrip(t, NewU128(*big.NewInt(0)))
   264  	assertRoundtrip(t, NewU128(*big.NewInt(12)))
   265  
   266  	bigPos := big.NewInt(0)
   267  	bigPos.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
   268  	assertRoundtrip(t, NewU128(*bigPos))
   269  }
   270  
   271  func TestU128_EncodedLength(t *testing.T) {
   272  	assertEncodedLength(t, []encodedLengthAssert{{NewU128(*big.NewInt(13)), 16}})
   273  }
   274  
   275  func TestU128_Encode(t *testing.T) {
   276  	a := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   277  	b := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
   278  	c := big.NewInt(0).SetBytes([]byte{255, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
   279  
   280  	assertEncode(t, []encodingAssert{
   281  		{NewU128(*big.NewInt(0)), MustHexDecodeString("0x00000000000000000000000000000000")},
   282  		{NewU128(*big.NewInt(29)), MustHexDecodeString("0x1d000000000000000000000000000000")},
   283  		{NewU128(*a), MustHexDecodeString("0x34120000000000000000000000000000")},
   284  		{NewU128(*b), MustHexDecodeString("0x100f0e0d0c0b0a090807060504030201")},
   285  		{NewU128(*c), MustHexDecodeString("0x100f0e0d0c0b0a0908070605040302ff")},
   286  	})
   287  }
   288  
   289  func TestU128_Decode(t *testing.T) {
   290  	a := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   291  	b := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
   292  	c := big.NewInt(0).SetBytes([]byte{255, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
   293  
   294  	assertDecode(t, []decodingAssert{
   295  		{MustHexDecodeString("0x00000000000000000000000000000000"), NewU128(*big.NewInt(0))},
   296  		{MustHexDecodeString("0x1d000000000000000000000000000000"), NewU128(*big.NewInt(29))},
   297  		{MustHexDecodeString("0x34120000000000000000000000000000"), NewU128(*a)},
   298  		{MustHexDecodeString("0x100f0e0d0c0b0a090807060504030201"), NewU128(*b)},
   299  		{MustHexDecodeString("0x100f0e0d0c0b0a0908070605040302ff"), NewU128(*c)},
   300  	})
   301  }
   302  
   303  func TestU128_Hash(t *testing.T) {
   304  	assertHash(t, []hashAssert{
   305  		{NewU128(*big.NewInt(29)), MustHexDecodeString(
   306  			"0x139bd9153bbc4913d4161f7a5dd39912b5d22b57a8b557f0a24078a11f943174")},
   307  	})
   308  }
   309  
   310  func TestU128_Hex(t *testing.T) {
   311  	assertEncodeToHex(t, []encodeToHexAssert{
   312  		{NewU128(*big.NewInt(29)), "0x1d000000000000000000000000000000"},
   313  	})
   314  }
   315  
   316  func TestU128_String(t *testing.T) {
   317  	assertString(t, []stringAssert{
   318  		{NewU128(*big.NewInt(29)), "29"},
   319  	})
   320  }
   321  
   322  func TestU128_Eq(t *testing.T) {
   323  	assertEq(t, []eqAssert{
   324  		{NewU128(*big.NewInt(23)), NewU128(*big.NewInt(23)), true},
   325  		{NewU128(*big.NewInt(23)), NewU64(23), false},
   326  		{NewU128(*big.NewInt(23)), NewBool(false), false},
   327  	})
   328  }
   329  
   330  func TestU256_EncodeDecode(t *testing.T) {
   331  	assertRoundtrip(t, NewU256(*big.NewInt(0)))
   332  	assertRoundtrip(t, NewU256(*big.NewInt(12)))
   333  
   334  	bigPos := big.NewInt(0)
   335  	bigPos.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
   336  		17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32})
   337  	assertRoundtrip(t, NewU256(*bigPos))
   338  }
   339  
   340  func TestU256_EncodedLength(t *testing.T) {
   341  	assertEncodedLength(t, []encodedLengthAssert{{NewU256(*big.NewInt(13)), 32}})
   342  }
   343  
   344  func TestU256_Encode(t *testing.T) {
   345  	a := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   346  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   347  	b := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
   348  		17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32})
   349  
   350  	assertEncode(t, []encodingAssert{
   351  		{NewU256(*big.NewInt(0)), MustHexDecodeString(
   352  			"0x0000000000000000000000000000000000000000000000000000000000000000")},
   353  		{NewU256(*big.NewInt(29)), MustHexDecodeString(
   354  			"0x1d00000000000000000000000000000000000000000000000000000000000000")},
   355  		{NewU256(*a), MustHexDecodeString("0x3412000000000000000000000000000000000000000000000000000000000000")},
   356  		{NewU256(*b), MustHexDecodeString("0x201f1e1d1c1b1a1817161514131211100f0e0d0c0b0a09080706050403020100")},
   357  	})
   358  }
   359  
   360  func TestU256_Decode(t *testing.T) {
   361  	a := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   362  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   363  	b := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
   364  		17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32})
   365  
   366  	assertDecode(t, []decodingAssert{
   367  		{MustHexDecodeString("0x0000000000000000000000000000000000000000000000000000000000000000"),
   368  			NewU256(*big.NewInt(0))},
   369  		{MustHexDecodeString("0x1d00000000000000000000000000000000000000000000000000000000000000"),
   370  			NewU256(*big.NewInt(29))},
   371  		{MustHexDecodeString("0x3412000000000000000000000000000000000000000000000000000000000000"), NewU256(*a)},
   372  		{MustHexDecodeString("0x201f1e1d1c1b1a1817161514131211100f0e0d0c0b0a09080706050403020100"), NewU256(*b)},
   373  	})
   374  }
   375  
   376  func TestU256_Hash(t *testing.T) {
   377  	assertHash(t, []hashAssert{
   378  		{NewU256(*big.NewInt(29)), MustHexDecodeString(
   379  			"0x92d6618c3e5941a74d1e805e1a8485469229f9a9c58145761bd9209bc2f4360d")},
   380  	})
   381  }
   382  
   383  func TestU256_Hex(t *testing.T) {
   384  	assertEncodeToHex(t, []encodeToHexAssert{
   385  		{NewU256(*big.NewInt(29)), "0x1d00000000000000000000000000000000000000000000000000000000000000"},
   386  	})
   387  }
   388  
   389  func TestU256_String(t *testing.T) {
   390  	assertString(t, []stringAssert{
   391  		{NewU256(*big.NewInt(29)), "29"},
   392  	})
   393  }
   394  
   395  func TestU256_Eq(t *testing.T) {
   396  	assertEq(t, []eqAssert{
   397  		{NewU256(*big.NewInt(23)), NewU256(*big.NewInt(23)), true},
   398  		{NewU256(*big.NewInt(23)), NewU128(*big.NewInt(23)), false},
   399  		{NewU256(*big.NewInt(23)), NewI256(*big.NewInt(23)), false},
   400  		{NewU256(*big.NewInt(23)), NewU64(23), false},
   401  		{NewU256(*big.NewInt(23)), NewBool(false), false},
   402  	})
   403  }
   404  
   405  func TestBigIntToUintBytes(t *testing.T) {
   406  	res, err := BigIntToUintBytes(big.NewInt(4), 2)
   407  	assert.NoError(t, err)
   408  	assert.Equal(t, MustHexDecodeString("0x0004"), res)
   409  
   410  	_, err = BigIntToUintBytes(big.NewInt(0).Neg(big.NewInt(4)), 2)
   411  	assert.EqualError(t, err, "cannot encode a negative big.Int into an unsigned integer")
   412  
   413  	_, err = BigIntToUintBytes(big.NewInt(266), 1)
   414  	assert.EqualError(t, err, "cannot encode big.Int to []byte: given big.Int exceeds highest number 256 for an "+
   415  		"uint with 8 bits")
   416  }
   417  
   418  func TestUintBytesToBigInt(t *testing.T) {
   419  	res, err := UintBytesToBigInt(MustHexDecodeString("0x0004"))
   420  	assert.NoError(t, err)
   421  	assert.Equal(t, big.NewInt(4), res)
   422  
   423  	res, err = UintBytesToBigInt(MustHexDecodeString("0xfffc"))
   424  	assert.NoError(t, err)
   425  	assert.Equal(t, big.NewInt(65532), res)
   426  
   427  	_, err = UintBytesToBigInt([]byte{})
   428  	assert.EqualError(t, err, "cannot decode an empty byte slice")
   429  }
   430  
   431  func TestBigIntToUintBytes_128(t *testing.T) {
   432  	b := big.NewInt(0)
   433  	b.SetBytes([]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x12, 0x34})
   434  
   435  	res, err := BigIntToUintBytes(b, 16)
   436  	assert.NoError(t, err)
   437  	assert.Equal(t, MustHexDecodeString("0x00000000000000000000000000001234"), res)
   438  }