Skip to content

Commit

Permalink
Increase precision of UCompact type to scale max (#85)
Browse files Browse the repository at this point in the history
* Revert "Add ExtrinsicSignatureV5 (#68)"

This reverts commit 461cf42.

* Increase precision ucompact to max

* remove test codee

* Move to scale and make Reverse function public

* Clean event record SomeOffline test

* imports

* review comments
  • Loading branch information
mikiquantum authored Jul 14, 2020
1 parent e238735 commit 53c1093
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 103 deletions.
12 changes: 6 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func Example_makeASimpleTransfer() {
panic(err)
}

c, err := types.NewCall(meta, "Balances.transfer", bob, types.UCompact(12345))
c, err := types.NewCall(meta, "Balances.transfer", bob, types.NewUCompactFromUInt(12345))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -238,9 +238,9 @@ func Example_makeASimpleTransfer() {
BlockHash: genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: genesisHash,
Nonce: types.UCompact(nonce),
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
SpecVersion: rv.SpecVersion,
Tip: 0,
Tip: types.NewUCompactFromUInt(0),
}

// Sign the transaction using Alice's default account
Expand Down Expand Up @@ -413,7 +413,7 @@ func Example_transactionWithEvents() {
panic(err)
}

amount := types.UCompact(12345)
amount := types.NewUCompactFromUInt(12345)

c, err := types.NewCall(meta, "Balances.transfer", bob, amount)
if err != nil {
Expand Down Expand Up @@ -454,9 +454,9 @@ func Example_transactionWithEvents() {
BlockHash: genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: genesisHash,
Nonce: types.UCompact(nonce),
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
SpecVersion: rv.SpecVersion,
Tip: 0,
Tip: types.NewUCompactFromUInt(0),
}

fmt.Printf("Sending %v from %#x to %#x with nonce %v", amount, signature.TestKeyringPairAlice.PublicKey, bob.AsAccountID, nonce)
Expand Down
2 changes: 1 addition & 1 deletion rpc/author/pending_extrinsics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ import (
func TestAuthor_PendingExtrinsics(t *testing.T) {
res, err := author.PendingExtrinsics()
assert.NoError(t, err)
assert.Equal(t, []types.Extrinsic{types.Extrinsic{Version: 0x84, Signature: types.ExtrinsicSignatureV4{Signer: types.Address{IsAccountID: true, AsAccountID: types.AccountID{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d}, IsAccountIndex: false, AsAccountIndex: 0x0}, Signature: types.MultiSignature{IsEd25519: true, AsEd25519: types.Signature{0xa0, 0x23, 0xbb, 0xe8, 0x83, 0x40, 0x5b, 0x5f, 0xac, 0x2a, 0xa1, 0x14, 0x9, 0x3f, 0xcf, 0x3d, 0x8, 0x2, 0xd2, 0xf3, 0xd3, 0x71, 0x5e, 0x9, 0x12, 0x9b, 0x0, 0xa4, 0xbf, 0x74, 0x10, 0x48, 0xca, 0xf5, 0x3d, 0x8c, 0x7d, 0x97, 0xe8, 0x72, 0xca, 0xa7, 0x3, 0xe7, 0xd0, 0x4f, 0x17, 0x4a, 0x4e, 0x2e, 0xd4, 0xac, 0xad, 0xee, 0x41, 0x73, 0xa8, 0xb6, 0xba, 0xb7, 0xe4, 0x5c, 0xa, 0x6}, IsSr25519: false, AsSr25519: types.Signature{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, IsEcdsa: false, AsEcdsa: types.Bytes(nil)}, Era: types.ExtrinsicEra{IsImmortalEra: true, IsMortalEra: false, AsMortalEra: types.MortalEra{First: 0x0, Second: 0x0}}, Nonce: 0x3, Tip: 0x0}, Method: types.Call{CallIndex: types.CallIndex{SectionIndex: 0x6, MethodIndex: 0x0}, Args: types.Args{0xff, 0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48, 0xe5, 0x6c}}}}, res) //nolint:lll,dupl
assert.Equal(t, []types.Extrinsic{types.Extrinsic{Version: 0x84, Signature: types.ExtrinsicSignatureV4{Signer: types.Address{IsAccountID: true, AsAccountID: types.AccountID{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d}, IsAccountIndex: false, AsAccountIndex: 0x0}, Signature: types.MultiSignature{IsEd25519: true, AsEd25519: types.Signature{0xa0, 0x23, 0xbb, 0xe8, 0x83, 0x40, 0x5b, 0x5f, 0xac, 0x2a, 0xa1, 0x14, 0x9, 0x3f, 0xcf, 0x3d, 0x8, 0x2, 0xd2, 0xf3, 0xd3, 0x71, 0x5e, 0x9, 0x12, 0x9b, 0x0, 0xa4, 0xbf, 0x74, 0x10, 0x48, 0xca, 0xf5, 0x3d, 0x8c, 0x7d, 0x97, 0xe8, 0x72, 0xca, 0xa7, 0x3, 0xe7, 0xd0, 0x4f, 0x17, 0x4a, 0x4e, 0x2e, 0xd4, 0xac, 0xad, 0xee, 0x41, 0x73, 0xa8, 0xb6, 0xba, 0xb7, 0xe4, 0x5c, 0xa, 0x6}, IsSr25519: false, AsSr25519: types.Signature{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, IsEcdsa: false, AsEcdsa: types.Bytes(nil)}, Era: types.ExtrinsicEra{IsImmortalEra: true, IsMortalEra: false, AsMortalEra: types.MortalEra{First: 0x0, Second: 0x0}}, Nonce: types.NewUCompactFromUInt(0x3), Tip: types.NewUCompactFromUInt(0x0)}, Method: types.Call{CallIndex: types.CallIndex{SectionIndex: 0x6, MethodIndex: 0x0}, Args: types.Args{0xff, 0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48, 0xe5, 0x6c}}}}, res) //nolint:lll,dupl
}
2 changes: 1 addition & 1 deletion rpc/author/submit_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestAuthor_SubmitExtrinsic(t *testing.T) {
ext := types.Extrinsic{Version: 0x84, Signature: types.ExtrinsicSignatureV4{Signer: types.Address{IsAccountID: true, AsAccountID: types.AccountID{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d}, IsAccountIndex: false, AsAccountIndex: 0x0}, Signature: types.MultiSignature{IsSr25519: true, AsSr25519: types.Signature{0xc0, 0x42, 0x19, 0x5f, 0x93, 0x25, 0xd, 0x3e, 0xda, 0xa2, 0xe4, 0xa4, 0x2d, 0xcf, 0x4e, 0x41, 0xc1, 0x6c, 0xa7, 0x1c, 0xfc, 0x3a, 0x2b, 0x23, 0x99, 0x8a, 0xd4, 0xec, 0x97, 0x4f, 0x8b, 0x1a, 0xcd, 0xcd, 0xad, 0x97, 0xd1, 0x4b, 0x6d, 0xf5, 0xcb, 0x89, 0x6, 0xff, 0x61, 0xc8, 0x92, 0x17, 0x96, 0x54, 0xa5, 0xec, 0xcc, 0xb, 0x66, 0x85, 0xf6, 0xc1, 0x7f, 0xed, 0x49, 0x21, 0x94, 0x0}}, Era: types.ExtrinsicEra{IsImmortalEra: true, IsMortalEra: false, AsMortalEra: types.MortalEra{First: 0x0, Second: 0x0}}, Nonce: 0x1, Tip: 0x0}, Method: types.Call{CallIndex: types.CallIndex{SectionIndex: 0x6, MethodIndex: 0x0}, Args: types.Args{0xff, 0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48, 0xe5, 0x6c}}} //nolint:lll,dupl
ext := types.Extrinsic{Version: 0x84, Signature: types.ExtrinsicSignatureV4{Signer: types.Address{IsAccountID: true, AsAccountID: types.AccountID{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d}, IsAccountIndex: false, AsAccountIndex: 0x0}, Signature: types.MultiSignature{IsSr25519: true, AsSr25519: types.Signature{0xc0, 0x42, 0x19, 0x5f, 0x93, 0x25, 0xd, 0x3e, 0xda, 0xa2, 0xe4, 0xa4, 0x2d, 0xcf, 0x4e, 0x41, 0xc1, 0x6c, 0xa7, 0x1c, 0xfc, 0x3a, 0x2b, 0x23, 0x99, 0x8a, 0xd4, 0xec, 0x97, 0x4f, 0x8b, 0x1a, 0xcd, 0xcd, 0xad, 0x97, 0xd1, 0x4b, 0x6d, 0xf5, 0xcb, 0x89, 0x6, 0xff, 0x61, 0xc8, 0x92, 0x17, 0x96, 0x54, 0xa5, 0xec, 0xcc, 0xb, 0x66, 0x85, 0xf6, 0xc1, 0x7f, 0xed, 0x49, 0x21, 0x94, 0x0}}, Era: types.ExtrinsicEra{IsImmortalEra: true, IsMortalEra: false, AsMortalEra: types.MortalEra{First: 0x0, Second: 0x0}}, Nonce: types.NewUCompactFromUInt(0x1), Tip: types.NewUCompactFromUInt(0x0)}, Method: types.Call{CallIndex: types.CallIndex{SectionIndex: 0x6, MethodIndex: 0x0}, Args: types.Args{0xff, 0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48, 0xe5, 0x6c}}} //nolint:lll,dupl
res, err := author.SubmitExtrinsic(ext)
assert.NoError(t, err)
hex, err := types.Hex(res)
Expand Down
109 changes: 61 additions & 48 deletions scale/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"log"
"math"
"math/big"
"reflect"
)

Expand Down Expand Up @@ -70,47 +71,50 @@ func (pe Encoder) PushByte(b byte) error {
// zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM (2**14 ... 2**30 - 1) (u16, u32) low LMMH high
// nn nn nn 11 [ / zz zz zz zz ]{4 + n} (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
// Rust implementation: see impl<'a> Encode for CompactRef<'a, u64>
func (pe Encoder) EncodeUintCompact(v uint64) error {

// TODO: handle numbers wider than 64 bits (byte slices?)
// Currently, Rust implementation only seems to support u128
func (pe Encoder) EncodeUintCompact(v big.Int) error {
if v.Sign() == -1 {
return errors.New("Assertion error: EncodeUintCompact cannot process negative numbers")
}

if v < 1<<30 {
if v < 1<<6 {
err := pe.PushByte(byte(v) << 2)
if err != nil {
return err
}
} else if v < 1<<14 {
err := binary.Write(pe.writer, binary.LittleEndian, uint16(v<<2)+1)
if err != nil {
return err
}
} else {
err := binary.Write(pe.writer, binary.LittleEndian, uint32(v<<2)+2)
if err != nil {
return err
if v.IsUint64() {
if v.Uint64() < 1<<30 {
if v.Uint64() < 1<<6 {
err := pe.PushByte(byte(v.Uint64()) << 2)
if err != nil {
return err
}
} else if v.Uint64() < 1<<14 {
err := binary.Write(pe.writer, binary.LittleEndian, uint16(v.Uint64()<<2)+1)
if err != nil {
return err
}
} else {
err := binary.Write(pe.writer, binary.LittleEndian, uint32(v.Uint64()<<2)+2)
if err != nil {
return err
}
}
return nil
}
return nil
}

n := byte(0)
limit := uint64(1 << 32)
for v >= limit && limit > 256 { // when overflows, limit will be < 256
n++
limit <<= 8
numBytes := len(v.Bytes())
if numBytes > 255 {
return errors.New("Assertion error: numBytes>255 exeeds allowed for length prefix")
}
if n > 4 {
return errors.New("Assertion error: n>4 needed to compact-encode uint64")
topSixBits := uint8(numBytes - 4)
lengthByte := topSixBits<<2 + 3

if topSixBits > 63 {
return errors.New("Assertion error: n<=63 needed to compact-encode substrate unsigned big integer")
}
err := pe.PushByte((n << 2) + 3)
err := pe.PushByte(lengthByte)
if err != nil {
return err
}
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, v)
err = pe.Write(buf[:4+n])
buf := v.Bytes()
Reverse(buf)
err = pe.Write(buf)
if err != nil {
return err
}
Expand Down Expand Up @@ -199,7 +203,7 @@ func (pe Encoder) Encode(value interface{}) error {
if len64 > math.MaxUint32 {
return errors.New("Attempted to serialize a collection with too many elements.")
}
err := pe.EncodeUintCompact(len64)
err := pe.EncodeUintCompact(*big.NewInt(0).SetUint64(len64))
if err != nil {
return err
}
Expand Down Expand Up @@ -419,13 +423,13 @@ func (pd Decoder) DecodeIntoReflectValue(target reflect.Value) error {
// Slices: first compact-encode length, then each item individually
case reflect.Slice:
codedLen64, _ := pd.DecodeUintCompact()
if codedLen64 > math.MaxUint32 {
if codedLen64.Uint64() > math.MaxUint32 {
return errors.New("Encoded array length is higher than allowed by the protocol (32-bit unsigned integer)")
}
if codedLen64 > uint64(maxInt) {
if codedLen64.Uint64() > uint64(maxInt) {
return errors.New("Encoded array length is higher than allowed by the platform")
}
codedLen := int(codedLen64)
codedLen := int(codedLen64.Uint64())
targetLen := target.Len()
if codedLen != targetLen {
if int(codedLen) > target.Cap() {
Expand Down Expand Up @@ -487,51 +491,60 @@ func (pd Decoder) DecodeIntoReflectValue(target reflect.Value) error {
}

// DecodeUintCompact decodes a compact-encoded integer. See EncodeUintCompact method.
func (pd Decoder) DecodeUintCompact() (uint64, error) {
func (pd Decoder) DecodeUintCompact() (*big.Int, error) {
b, _ := pd.ReadOneByte()
mode := b & 3
switch mode {
case 0:
// right shift to remove mode bits
return uint64(b >> 2), nil
return big.NewInt(0).SetUint64(uint64(b >> 2)), nil
case 1:
bb, err := pd.ReadOneByte()
if err != nil {
return 0, err
return nil, err
}
r := uint64(bb)
// * 2^6
r <<= 6
// right shift to remove mode bits and add to prev
r += uint64(b >> 2)
return r, nil
return big.NewInt(0).SetUint64(r), nil
case 2:
// value = 32 bits + mode
buf := make([]byte, 4)
buf[0] = b
err := pd.Read(buf[1:4])
if err != nil {
return 0, err
return nil, err
}
// set the buffer in little endian order
r := binary.LittleEndian.Uint32(buf)
// remove the last 2 mode bits
r >>= 2
return uint64(r), nil
return big.NewInt(0).SetUint64(uint64(r)) , nil
case 3:
// remove mode bits
l := b >> 2
if l > 4 {
return 0, errors.New("Not supported: l>4 encountered when decoding a compact-encoded uint")

if l > 63 { // Max upper bound of 536 is (67 - 4)
return nil, errors.New("Not supported: l>63 encountered when decoding a compact-encoded uint")
}
buf := make([]byte, 8)
err := pd.Read(buf[:l+4])
buf := make([]byte, l+4)
err := pd.Read(buf)
if err != nil {
return 0, err
return nil, err
}
return binary.LittleEndian.Uint64(buf), nil
Reverse(buf)
return new(big.Int).SetBytes(buf), nil
default:
return 0, errors.New("Code should be unreachable")
return nil, errors.New("Code should be unreachable")
}
}

// Reverse reverses bytes in place (manipulates the underlying array)
func Reverse(b []byte) {
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
}

Expand Down
6 changes: 4 additions & 2 deletions scale/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"fmt"
"math"
"math/big"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -248,10 +249,11 @@ func TestCompactIntegersEncodedAsExpected(t *testing.T) {
math.MaxUint64: "13 ff ff ff ff ff ff ff ff"}
for value, expectedHex := range tests {
var buffer = bytes.Buffer{}
err := Encoder{writer: &buffer}.EncodeUintCompact(value)
valueBig := new(big.Int).SetUint64(value)
err := Encoder{writer: &buffer}.EncodeUintCompact(*valueBig)
assert.NoError(t, err)
assertEqual(t, hexify(buffer.Bytes()), expectedHex)
decoded, _ := Decoder{reader: &buffer}.DecodeUintCompact()
assertEqual(t, decoded, value)
assertEqual(t, decoded, big.NewInt(0).SetUint64(value))
}
}
6 changes: 3 additions & 3 deletions teste2e/author_submit_and_watch_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestAuthor_SubmitAndWatchExtrinsic(t *testing.T) {
panic(err)
}

c, err := types.NewCall(meta, "Balances.transfer", bob, types.UCompact(6969))
c, err := types.NewCall(meta, "Balances.transfer", bob, types.NewUCompactFromUInt(6969))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -93,9 +93,9 @@ func TestAuthor_SubmitAndWatchExtrinsic(t *testing.T) {
BlockHash: genesisHash, // BlockHash needs to == GenesisHash if era is immortal. // TODO: add an error?
Era: era,
GenesisHash: genesisHash,
Nonce: types.UCompact(nonce),
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
SpecVersion: rv.SpecVersion,
Tip: 0,
Tip: types.NewUCompactFromUInt(0),
}


Expand Down
6 changes: 3 additions & 3 deletions teste2e/author_submit_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestChain_SubmitExtrinsic(t *testing.T) {
panic(err)
}

c, err := types.NewCall(meta, "Balances.transfer", bob, types.UCompact(6969))
c, err := types.NewCall(meta, "Balances.transfer", bob, types.NewUCompactFromUInt(6969))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -95,9 +95,9 @@ func TestChain_SubmitExtrinsic(t *testing.T) {
BlockHash: genesisHash, // BlockHash needs to == GenesisHash if era is immortal. // TODO: add an error?
Era: era,
GenesisHash: genesisHash,
Nonce: types.UCompact(nonce + i),
Nonce: types.NewUCompactFromUInt(uint64(nonce + i)),
SpecVersion: rv.SpecVersion,
Tip: 0,
Tip: types.NewUCompactFromUInt(0),
}

extI := ext
Expand Down
2 changes: 1 addition & 1 deletion types/event_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (e EventRecordsRaw) DecodeEventRecords(m *Metadata, t interface{}) error {
log.Debug(fmt.Sprintf("found %v events", n))

// iterate over events
for i := uint64(0); i < n; i++ {
for i := uint64(0); i < n.Uint64(); i++ {
log.Debug(fmt.Sprintf("decoding event #%v", i))

// decode Phase
Expand Down
27 changes: 27 additions & 0 deletions types/event_record_test.go

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions types/extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"io"
"math/big"
"strings"

"github.com/centrifuge/go-substrate-rpc-client/scale"
Expand Down Expand Up @@ -90,7 +91,7 @@ func (e *Extrinsic) UnmarshalJSON(bz []byte) error {
if err != nil {
return err
}
length := UCompact(len(dec))
length := NewUCompactFromUInt(uint64(len(dec)))
bprefix, err := EncodeToBytes(length)
if err != nil {
return err
Expand Down Expand Up @@ -234,7 +235,7 @@ func (e Extrinsic) Encode(encoder scale.Encoder) error {

// take the temporary buffer to determine length, write that as prefix
eb := bb.Bytes()
err = encoder.EncodeUintCompact(uint64(len(eb)))
err = encoder.EncodeUintCompact(*big.NewInt(0).SetUint64(uint64(len(eb))))
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion types/extrinsic_examplary.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@

package types

var ExamplaryExtrinsic = Extrinsic{Version: 0x84, Signature: ExtrinsicSignatureV4{Signer: Address{IsAccountID: true, AsAccountID: AccountID{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d}, IsAccountIndex: false, AsAccountIndex: 0x0}, Signature: MultiSignature{IsSr25519: true, AsSr25519: Signature{0x5c, 0x77, 0x1d, 0xd5, 0x6a, 0xe0, 0xce, 0xed, 0x68, 0xd, 0xb3, 0xbb, 0x4c, 0x40, 0x7a, 0x38, 0x96, 0x99, 0x97, 0xae, 0xb6, 0xa, 0x2c, 0x62, 0x39, 0x1, 0x6, 0x2f, 0x7f, 0x8e, 0xbf, 0x2f, 0xe7, 0x73, 0x3a, 0x61, 0x3c, 0xf1, 0x6b, 0x78, 0xf6, 0x10, 0xc6, 0x52, 0x32, 0xa2, 0x3c, 0xc5, 0xce, 0x25, 0xda, 0x29, 0xa3, 0xd5, 0x84, 0x85, 0xd8, 0x7b, 0xd8, 0x3d, 0xb8, 0x18, 0x3f, 0x8}}, Era: ExtrinsicEra{IsImmortalEra: true, IsMortalEra: false, AsMortalEra: MortalEra{First: 0x0, Second: 0x0}}, Nonce: 0x1, Tip: 0x2}, Method: Call{CallIndex: CallIndex{SectionIndex: 0x3, MethodIndex: 0x0}, Args: Args{0xff, 0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48, 0xe5, 0x6c}}} //nolint:lll
import "math/big"

var ExamplaryExtrinsic = Extrinsic{Version: 0x84, Signature: ExtrinsicSignatureV4{Signer: Address{IsAccountID: true, AsAccountID: AccountID{0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x4, 0xa9, 0x9f, 0xd6, 0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d}, IsAccountIndex: false, AsAccountIndex: 0x0}, Signature: MultiSignature{IsSr25519: true, AsSr25519: Signature{0x5c, 0x77, 0x1d, 0xd5, 0x6a, 0xe0, 0xce, 0xed, 0x68, 0xd, 0xb3, 0xbb, 0x4c, 0x40, 0x7a, 0x38, 0x96, 0x99, 0x97, 0xae, 0xb6, 0xa, 0x2c, 0x62, 0x39, 0x1, 0x6, 0x2f, 0x7f, 0x8e, 0xbf, 0x2f, 0xe7, 0x73, 0x3a, 0x61, 0x3c, 0xf1, 0x6b, 0x78, 0xf6, 0x10, 0xc6, 0x52, 0x32, 0xa2, 0x3c, 0xc5, 0xce, 0x25, 0xda, 0x29, 0xa3, 0xd5, 0x84, 0x85, 0xd8, 0x7b, 0xd8, 0x3d, 0xb8, 0x18, 0x3f, 0x8}}, Era: ExtrinsicEra{IsImmortalEra: true, IsMortalEra: false, AsMortalEra: MortalEra{First: 0x0, Second: 0x0}}, Nonce: UCompact(*big.NewInt(1)), Tip: UCompact(*big.NewInt(2))}, Method: Call{CallIndex: CallIndex{SectionIndex: 0x3, MethodIndex: 0x0}, Args: Args{0xff, 0x8e, 0xaf, 0x4, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52, 0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48, 0xe5, 0x6c}}} //nolint:lll
Loading

0 comments on commit 53c1093

Please sign in to comment.