From 3af873217080db8900dec65c6e38b6c02173115d Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 29 Apr 2021 10:47:06 -0400 Subject: [PATCH 001/245] scale encoding with optionality, struct tag field ordering, uint128 --- pkg/scale/scale.go | 392 +++++++++++++++++ pkg/scale/scale_test.go | 881 ++++++++++++++++++++++++++++++++++++++ pkg/scale/uint128.go | 148 +++++++ pkg/scale/uint128_test.go | 67 +++ 4 files changed, 1488 insertions(+) create mode 100644 pkg/scale/scale.go create mode 100644 pkg/scale/scale_test.go create mode 100644 pkg/scale/uint128.go create mode 100644 pkg/scale/uint128_test.go diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go new file mode 100644 index 0000000000..0570f2afe4 --- /dev/null +++ b/pkg/scale/scale.go @@ -0,0 +1,392 @@ +package scale + +import ( + "bytes" + "encoding/binary" + "fmt" + "math/big" + "reflect" + "sort" + "strings" +) + +func Marshal(v interface{}) (b []byte, err error) { + es := encodeState{} + err = es.marshal(v) + if err != nil { + return + } + b = es.Bytes() + return +} + +type encodeState struct { + bytes.Buffer +} + +func (es *encodeState) marshal(in interface{}) (err error) { + switch in := in.(type) { + case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: + err = es.encodeFixedWidthInt(in) + case *big.Int: + err = es.encodeBigInt(in) + case Uint128: + err = es.encodeUint128(in) + case []byte: + err = es.encodeBytes(in) + case string: + err = es.encodeBytes([]byte(in)) + case bool: + err = es.encodeBool(in) + // TODO: case common.Hash: + // n, err = es.Writer.Write(v.ToBytes()) + default: + switch reflect.TypeOf(in).Kind() { + case reflect.Ptr: + // Assuming that anything that is a pointer is an Option to capture {nil, T} + elem := reflect.ValueOf(in).Elem() + switch elem.IsValid() { + case false: + err = es.WriteByte(0) + default: + err = es.WriteByte(1) + if err != nil { + return + } + err = es.marshal(elem.Interface()) + } + case reflect.Struct: + err = es.encodeStruct(in) + case reflect.Array: + err = es.encodeArray(in) + case reflect.Slice: + err = es.encodeSlice(in) + default: + err = fmt.Errorf("unsupported type: %T", in) + } + } + return +} + +func (es *encodeState) encodeSlice(t interface{}) (err error) { + switch arr := t.(type) { + // int is the only case that handles encoding differently. + // all other cases can recursively call es.marshal() + case []int: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.encodeUint(uint(elem)) + if err != nil { + return + } + } + // the cases below are to avoid using the reflect library + // for commonly used slices in gossamer + case []*big.Int: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.marshal(elem) + if err != nil { + return + } + } + case []bool: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.marshal(elem) + if err != nil { + return + } + } + case [][]byte: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.marshal(elem) + if err != nil { + return + } + } + case [][]int: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.marshal(elem) + if err != nil { + return + } + } + case []string: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.marshal(elem) + if err != nil { + return + } + } + default: + // use reflect package for any other cases + s := reflect.ValueOf(t) + err = es.encodeUint(uint(s.Len())) + if err != nil { + return + } + for i := 0; i < s.Len(); i++ { + err = es.marshal(s.Index(i).Interface()) + if err != nil { + return + } + } + } + return +} + +// encodeArray encodes an interface where the underlying type is an array +// it writes the encoded length of the Array to the Encoder, then encodes and writes each value in the Array +func (es *encodeState) encodeArray(in interface{}) (err error) { + v := reflect.ValueOf(in) + for i := 0; i < v.Len(); i++ { + elem := v.Index(i).Interface() + switch elem := elem.(type) { + case int: + // an array of unsized integers needs to be encoded using scale length encoding + err = es.encodeUint(uint(elem)) + if err != nil { + return + } + default: + err = es.marshal(v.Index(i).Interface()) + if err != nil { + return + } + } + } + return +} + +// encodeBigInt performs the same encoding as encodeInteger, except on a big.Int. +// if 2^30 <= n < 2^536 write [lower 2 bits of first byte = 11] [upper 6 bits of first byte = # of bytes following less 4] +// [append i as a byte array to the first byte] +func (es *encodeState) encodeBigInt(i *big.Int) (err error) { + switch { + case i == nil: + err = fmt.Errorf("nil *big.Int") + case i.Cmp(new(big.Int).Lsh(big.NewInt(1), 6)) < 0: + err = binary.Write(es, binary.LittleEndian, uint8(i.Int64()<<2)) + case i.Cmp(new(big.Int).Lsh(big.NewInt(1), 14)) < 0: + err = binary.Write(es, binary.LittleEndian, uint16(i.Int64()<<2)+1) + case i.Cmp(new(big.Int).Lsh(big.NewInt(1), 30)) < 0: + err = binary.Write(es, binary.LittleEndian, uint32(i.Int64()<<2)+2) + default: + numBytes := len(i.Bytes()) + topSixBits := uint8(numBytes - 4) + lengthByte := topSixBits<<2 + 3 + + // write byte which encodes mode and length + err = binary.Write(es, binary.LittleEndian, lengthByte) + if err == nil { + // write integer itself + err = binary.Write(es, binary.LittleEndian, reverseBytes(i.Bytes())) + } + } + return +} + +// encodeBool performs the following: +// l = true -> write [1] +// l = false -> write [0] +func (es *encodeState) encodeBool(l bool) (err error) { + switch l { + case true: + _, err = es.Write([]byte{0x01}) + case false: + _, err = es.Write([]byte{0x00}) + } + return +} + +// encodeByteArray performs the following: +// b -> [encodeInteger(len(b)) b] +// it writes to the buffer a byte array where the first byte is the length of b encoded with SCALE, followed by the +// byte array b itself +func (es *encodeState) encodeBytes(b []byte) (err error) { + err = es.encodeUint(uint(len(b))) + if err != nil { + return + } + + _, err = es.Write(b) + return +} + +// encodeFixedWidthInteger encodes an int with size < 2**32 by putting it into little endian byte format +func (es *encodeState) encodeFixedWidthInt(i interface{}) (err error) { + switch i := i.(type) { + case int8: + err = binary.Write(es, binary.LittleEndian, byte(i)) + case uint8: + err = binary.Write(es, binary.LittleEndian, i) + case int16: + err = binary.Write(es, binary.LittleEndian, uint16(i)) + case uint16: + err = binary.Write(es, binary.LittleEndian, i) + case int32: + err = binary.Write(es, binary.LittleEndian, uint32(i)) + case uint32: + err = binary.Write(es, binary.LittleEndian, i) + case int64: + err = binary.Write(es, binary.LittleEndian, uint64(i)) + case uint64: + err = binary.Write(es, binary.LittleEndian, i) + case int: + err = binary.Write(es, binary.LittleEndian, int64(i)) + case uint: + err = binary.Write(es, binary.LittleEndian, uint64(i)) + default: + err = fmt.Errorf("could not encode fixed width integer, invalid type: %T", i) + } + return +} + +// encodeStruct reads the number of fields in the struct and their types and writes to the buffer each of the struct fields +// encoded as their respective types +func (es *encodeState) encodeStruct(in interface{}) (err error) { + t := reflect.TypeOf(in) + v := reflect.ValueOf(in) + + if !v.IsValid() { + err = fmt.Errorf("inputted value is not valid: %v", v) + return + } + + type fieldScaleIndex struct { + fieldIndex int + scaleIndex *string + } + // Get the type and kind of our user variable + indices := make([]fieldScaleIndex, 0) + + // Iterate over all available fields and read the tag value + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + + // Get the field tag value + tag := field.Tag.Get("scale") + switch strings.TrimSpace(tag) { + case "": + indices = append(indices, fieldScaleIndex{ + fieldIndex: i, + }) + case "-": + // ignore this field + continue + default: + indices = append(indices, fieldScaleIndex{ + fieldIndex: i, + scaleIndex: &tag, + }) + } + } + + sort.Slice(indices[:], func(i, j int) bool { + switch { + case indices[i].scaleIndex == nil && indices[j].scaleIndex != nil: + return false + case indices[i].scaleIndex != nil && indices[j].scaleIndex == nil: + return true + case indices[i].scaleIndex == nil && indices[j].scaleIndex == nil: + return indices[i].fieldIndex < indices[j].fieldIndex + case indices[i].scaleIndex != nil && indices[j].scaleIndex != nil: + return *indices[i].scaleIndex < *indices[j].scaleIndex + } + return false + }) + + for _, i := range indices { + field := v.Field(i.fieldIndex) + if !field.CanInterface() { + continue + } + err = es.marshal(field.Interface()) + if err != nil { + return + } + } + return +} + +// encodeLength is a helper function that calls encodeUint, which is the scale length encoding +func (es *encodeState) encodeLength(l int) (err error) { + return es.encodeUint(uint(l)) +} + +// encodeUint performs the following on integer i: +// i -> i^0...i^n where n is the length in bits of i +// note that the bit representation of i is in little endian; ie i^0 is the least significant bit of i, +// and i^n is the most significant bit +// if n < 2^6 write [00 i^2...i^8 ] [ 8 bits = 1 byte encoded ] +// if 2^6 <= n < 2^14 write [01 i^2...i^16] [ 16 bits = 2 byte encoded ] +// if 2^14 <= n < 2^30 write [10 i^2...i^32] [ 32 bits = 4 byte encoded ] +// if n >= 2^30 write [lower 2 bits of first byte = 11] [upper 6 bits of first byte = # of bytes following less 4] +// [append i as a byte array to the first byte] +func (es *encodeState) encodeUint(i uint) (err error) { + switch { + case i < 1<<6: + err = binary.Write(es, binary.LittleEndian, byte(i)<<2) + case i < 1<<14: + err = binary.Write(es, binary.LittleEndian, uint16(i<<2)+1) + case i < 1<<30: + err = binary.Write(es, binary.LittleEndian, uint32(i<<2)+2) + default: + o := make([]byte, 8) + m := i + var numBytes int + // calculate the number of bytes needed to store i + // the most significant byte cannot be zero + // each iteration, shift by 1 byte until the number is zero + // then break and save the numBytes needed + for numBytes = 0; numBytes < 256 && m != 0; numBytes++ { + m = m >> 8 + } + + topSixBits := uint8(numBytes - 4) + lengthByte := topSixBits<<2 + 3 + + err = binary.Write(es, binary.LittleEndian, lengthByte) + if err == nil { + binary.LittleEndian.PutUint64(o, uint64(i)) + err = binary.Write(es, binary.LittleEndian, o[0:numBytes]) + } + } + return +} + +// encodeUint128 encodes a Uint128 +func (es *encodeState) encodeUint128(i Uint128) (err error) { + err = binary.Write(es, binary.LittleEndian, i.Bytes()) + return +} + +func reverseBytes(a []byte) []byte { + for i := len(a)/2 - 1; i >= 0; i-- { + opp := len(a) - 1 - i + a[i], a[opp] = a[opp], a[i] + } + return a +} diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go new file mode 100644 index 0000000000..61f82a31ca --- /dev/null +++ b/pkg/scale/scale_test.go @@ -0,0 +1,881 @@ +package scale + +import ( + "bytes" + "math/big" + "reflect" + "strings" + "testing" +) + +func Test_encodeState_encodeFixedWidthInteger(t *testing.T) { + type args struct { + i interface{} + } + tests := []struct { + name string + args args + wantErr bool + want []byte + }{ + { + name: "int(1)", + args: args{ + i: int(1), + }, + want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0}, + }, + { + name: "int(16383)", + args: args{ + i: int(16383), + }, + want: []byte{0xff, 0x3f, 0, 0, 0, 0, 0, 0}, + }, + { + name: "int(1073741823)", + args: args{ + i: int(1073741823), + }, + want: []byte{0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0}, + }, + { + name: "int(9223372036854775807)", + args: args{ + i: int(9223372036854775807), + }, + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, + { + name: "uint(1)", + args: args{ + i: int(1), + }, + want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0}, + }, + { + name: "uint(16383)", + args: args{ + i: uint(16383), + }, + want: []byte{0xff, 0x3f, 0, 0, 0, 0, 0, 0}, + }, + { + name: "uint(1073741823)", + args: args{ + i: uint(1073741823), + }, + want: []byte{0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0}, + }, + { + name: "uint(9223372036854775807)", + args: args{ + i: uint(9223372036854775807), + }, + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, + { + name: "int64(1)", + args: args{ + i: int64(1), + }, + want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0}, + }, + { + name: "int64(16383)", + args: args{ + i: int64(16383), + }, + want: []byte{0xff, 0x3f, 0, 0, 0, 0, 0, 0}, + }, + { + name: "int64(1073741823)", + args: args{ + i: int64(1073741823), + }, + want: []byte{0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0}, + }, + { + name: "int64(9223372036854775807)", + args: args{ + i: int64(9223372036854775807), + }, + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, + { + name: "uint64(1)", + args: args{ + i: uint64(1), + }, + want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0}, + }, + { + name: "uint64(16383)", + args: args{ + i: uint64(16383), + }, + want: []byte{0xff, 0x3f, 0, 0, 0, 0, 0, 0}, + }, + { + name: "uint64(1073741823)", + args: args{ + i: uint64(1073741823), + }, + want: []byte{0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0}, + }, + { + name: "uint64(9223372036854775807)", + args: args{ + i: uint64(9223372036854775807), + }, + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, + { + name: "int32(1)", + args: args{ + i: int32(1), + }, + want: []byte{0x01, 0, 0, 0}, + }, + { + name: "int32(16383)", + args: args{ + i: int32(16383), + }, + want: []byte{0xff, 0x3f, 0, 0}, + }, + { + name: "int32(1073741823)", + args: args{ + i: int32(1073741823), + }, + want: []byte{0xff, 0xff, 0xff, 0x3f}, + }, + { + name: "uint32(1)", + args: args{ + i: uint32(1), + }, + want: []byte{0x01, 0, 0, 0}, + }, + { + name: "uint32(16383)", + args: args{ + i: uint32(16383), + }, + want: []byte{0xff, 0x3f, 0, 0}, + }, + { + name: "uint32(1073741823)", + args: args{ + i: uint32(1073741823), + }, + want: []byte{0xff, 0xff, 0xff, 0x3f}, + }, + { + name: "int8(1)", + args: args{ + i: int8(1), + }, + want: []byte{0x01}, + }, + { + name: "uint8(1)", + args: args{ + i: uint8(1), + }, + want: []byte{0x01}, + }, + { + name: "int16(1)", + args: args{ + i: int16(1), + }, + want: []byte{0x01, 0}, + }, + { + name: "int16(16383)", + args: args{ + i: int16(16383), + }, + want: []byte{0xff, 0x3f}, + }, + { + name: "uint16(1)", + args: args{ + i: uint16(1), + }, + want: []byte{0x01, 0}, + }, + { + name: "uint16(16383)", + args: args{ + i: uint16(16383), + }, + want: []byte{0xff, 0x3f}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{} + if err := es.marshal(tt.args.i); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeFixedWidthInt() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeFixedWidthInt() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_encodeState_encodeBigInteger(t *testing.T) { + type args struct { + i *big.Int + } + tests := []struct { + name string + args args + wantErr bool + want []byte + }{ + { + name: "error nil pointer", + wantErr: true, + }, + { + name: "big.NewInt(0)", + args: args{ + i: big.NewInt(0), + }, + want: []byte{0x00}, + }, + { + name: "big.NewInt(1)", + args: args{ + i: big.NewInt(1), + }, + want: []byte{0x04}, + }, + { + name: "big.NewInt(42)", + args: args{ + i: big.NewInt(42), + }, + want: []byte{0xa8}, + }, + { + name: "big.NewInt(69)", + args: args{ + i: big.NewInt(69), + }, + want: []byte{0x15, 0x01}, + }, + { + name: "big.NewInt(1000)", + args: args{ + i: big.NewInt(1000), + }, + want: []byte{0xa1, 0x0f}, + }, + { + name: "big.NewInt(16383)", + args: args{ + i: big.NewInt(16383), + }, + want: []byte{0xfd, 0xff}, + }, + { + name: "big.NewInt(16384)", + args: args{ + i: big.NewInt(16384), + }, + want: []byte{0x02, 0x00, 0x01, 0x00}, + }, + { + name: "big.NewInt(1073741823)", + args: args{ + i: big.NewInt(1073741823), + }, + want: []byte{0xfe, 0xff, 0xff, 0xff}, + }, + { + name: "big.NewInt(1073741824)", + args: args{ + i: big.NewInt(1073741824), + }, + want: []byte{3, 0, 0, 0, 64}, + }, + { + name: "big.NewInt(1<<32 - 1)", + args: args{ + i: big.NewInt(1<<32 - 1), + }, + want: []byte{0x03, 0xff, 0xff, 0xff, 0xff}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{} + if err := es.marshal(tt.args.i); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeBigInt() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeBigInt() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_encodeState_encodeBytes(t *testing.T) { + var byteArray = func(length int) []byte { + b := make([]byte, length) + for i := 0; i < length; i++ { + b[i] = 0xff + } + return b + } + testString1 := "We love you! We believe in open source as wonderful form of giving." // n = 67 + testString2 := strings.Repeat("We need a longer string to test with. Let's multiple this several times.", 230) // n = 72 * 230 = 16560 + testString3 := "Let's test some special ASCII characters: ~ € · © ÿ" // n = 55 (UTF-8 encoding versus n = 51 with ASCII encoding) + + type args struct { + b interface{} + } + tests := []struct { + name string + args args + wantErr bool + want []byte + }{ + { + name: "[]byte{0x01}", + args: args{ + b: []byte{0x01}, + }, + want: []byte{0x04, 0x01}, + }, + { + name: "[]byte{0xff}", + args: args{ + b: []byte{0xff}, + }, + want: []byte{0x04, 0xff}, + }, + { + name: "[]byte{0x01, 0x01}", + args: args{ + b: []byte{0x01, 0x01}, + }, + want: []byte{0x08, 0x01, 0x01}, + }, + { + name: "byteArray(32)", + args: args{ + b: byteArray(32), + }, + want: append([]byte{0x80}, byteArray(32)...), + }, + { + name: "byteArray(64)", + args: args{ + b: byteArray(64), + }, + want: append([]byte{0x01, 0x01}, byteArray(64)...), + }, + { + name: "byteArray(16384)", + args: args{ + b: byteArray(16384), + }, + want: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), + }, + { + name: "\"a\"", + args: args{ + b: []byte("a"), + }, + want: []byte{0x04, 'a'}, + }, + { + name: "\"go-pre\"", + args: args{ + b: []byte("go-pre"), + }, + want: append([]byte{0x18}, string("go-pre")...), + }, + { + name: "testString1", + args: args{ + b: testString1, + }, + want: append([]byte{0x0D, 0x01}, testString1...), + }, + { + name: "testString2, long string", + args: args{ + b: testString2, + }, + want: append([]byte{0xC2, 0x02, 0x01, 0x00}, testString2...), + }, + { + name: "testString3, special chars", + args: args{ + b: testString3, + }, + want: append([]byte{0xDC}, testString3...), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{} + if err := es.marshal(tt.args.b); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeBytes() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeBytes() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_encodeState_encodeBool(t *testing.T) { + type fields struct { + Buffer bytes.Buffer + } + type args struct { + b bool + } + tests := []struct { + name string + fields fields + args args + wantErr bool + want []byte + }{ + { + name: "false", + args: args{ + b: false, + }, + want: []byte{0x00}, + }, + { + name: "true", + args: args{ + b: true, + }, + want: []byte{0x01}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{ + Buffer: tt.fields.Buffer, + } + if err := es.marshal(tt.args.b); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeBool() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeBool() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_encodeState_encodeStruct(t *testing.T) { + type myStruct struct { + Foo []byte + Bar int32 + Baz bool + } + var nilPtrMyStruct *myStruct + // pointer to + var ptrMystruct *myStruct = &myStruct{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + } + var nilPtrMyStruct2 *myStruct = &myStruct{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + } + nilPtrMyStruct2 = nil + + type args struct { + t interface{} + } + tests := []struct { + name string + args args + wantErr bool + want []byte + }{ + { + name: "nilPtrMyStruct", + args: args{ + nilPtrMyStruct, + }, + want: []byte{0}, + }, + { + name: "ptrMystruct", + args: args{ + ptrMystruct, + }, + want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + }, + { + name: "nilPtrMyStruct2", + args: args{ + nilPtrMyStruct2, + }, + want: []byte{0}, + }, + { + name: "&struct {[]byte, int32}", + args: args{ + t: &struct { + Foo []byte + Bar int32 + Baz bool + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + }, + want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + }, + { + name: "struct {[]byte, int32}", + args: args{ + t: struct { + Foo []byte + Bar int32 + Baz bool + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + }, + want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + }, + { + name: "struct {[]byte, int32, bool}", + args: args{ + t: struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + }, + want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + }, + { + name: "struct {[]byte, int32, bool} with untagged attributes", + args: args{ + t: struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + End1 bool + End2 []byte + End3 []byte + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + End1: false, + End2: []byte{0xff}, + End3: []byte{0x06}, + }, + }, + want: []byte{ + 0x04, 0x01, 0x02, 0, 0, 0, 0x01, + // End1: false + 0x00, + // End2: 0xff + 0x04, 0xff, + // End3: 0x06 + 0x04, 0x06, + }, + }, + { + name: "struct {[]byte, int32, bool} with untagged attributes", + args: args{ + t: struct { + End1 bool + Baz bool `scale:"3"` + End2 []byte + Bar int32 `scale:"2"` + End3 []byte + Foo []byte `scale:"1"` + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + End1: false, + End2: []byte{0xff}, + // End3: 0xff + End3: []byte{0x06}, + }, + }, + want: []byte{ + 0x04, 0x01, 0x02, 0, 0, 0, 0x01, + // End1: false + 0x00, + // End2: 0xff + 0x04, 0xff, + // End3: 0x06 + 0x04, 0x06, + }, + }, + { + name: "struct {[]byte, int32, bool} with private attributes", + args: args{ + t: struct { + priv0 string + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + priv1 []byte + }{ + priv0: "stuff", + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + }, + want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + }, + { + name: "struct {[]byte, int32, bool} with ignored attributes", + args: args{ + t: struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + Ignore string `scale:"-"` + somethingElse *struct { + fields int + } + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + Ignore: "me", + }, + }, + want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{} + err := es.marshal(tt.args.t) + if (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeStruct() = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeStruct() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_encodeState_encodeSlice(t *testing.T) { + type fields struct { + Buffer bytes.Buffer + } + type args struct { + t interface{} + } + tests := []struct { + name string + fields fields + args args + wantErr bool + want []byte + }{ + { + name: "[]int{1, 2, 3, 4}", + args: args{ + []int{1, 2, 3, 4}, + }, + want: []byte{0x10, 0x04, 0x08, 0x0c, 0x10}, + }, + { + name: "[]int{16384, 2, 3, 4}", + args: args{ + []int{16384, 2, 3, 4}, + }, + want: []byte{0x10, 0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, + }, + { + name: "[]int{1073741824, 2, 3, 4}", + args: args{ + []int{1073741824, 2, 3, 4}, + }, + want: []byte{0x10, 0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, + }, + { + name: "[]int{1 << 32, 2, 3, 1 << 32}", + args: args{ + []int{1 << 32, 2, 3, 1 << 32}, + }, + want: []byte{0x10, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, + }, + { + name: "[]bool{true, false, true}", + args: args{ + []bool{true, false, true}, + }, + want: []byte{0x0c, 0x01, 0x00, 0x01}, + }, + { + name: "[][]int{{0, 1}, {1, 0}}", + args: args{ + [][]int{{0, 1}, {1, 0}}, + }, + want: []byte{0x08, 0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, + }, + { + name: "[]*big.Int{big.NewInt(0), big.NewInt(1)}", + args: args{ + []*big.Int{big.NewInt(0), big.NewInt(1)}, + }, + want: []byte{0x08, 0x00, 0x04}, + }, + { + name: "[][]byte{{0x00, 0x01}, {0x01, 0x00}}", + args: args{ + [][]byte{{0x00, 0x01}, {0x01, 0x00}}, + }, + want: []byte{0x08, 0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{ + Buffer: tt.fields.Buffer, + } + err := es.marshal(tt.args.t) + if (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeStruct() = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeStruct() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_encodeState_encodeArray(t *testing.T) { + type fields struct { + Buffer bytes.Buffer + } + type args struct { + t interface{} + } + tests := []struct { + name string + fields fields + args args + wantErr bool + want []byte + }{ + { + name: "[4]int{1, 2, 3, 4}", + args: args{ + [4]int{1, 2, 3, 4}, + }, + want: []byte{0x04, 0x08, 0x0c, 0x10}, + }, + { + name: "[4]int{16384, 2, 3, 4}", + args: args{ + [4]int{16384, 2, 3, 4}, + }, + want: []byte{0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, + }, + { + name: "[4]int{1073741824, 2, 3, 4}", + args: args{ + [4]int{1073741824, 2, 3, 4}, + }, + want: []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, + }, + { + name: "[4]int{1 << 32, 2, 3, 1 << 32}", + args: args{ + [4]int{1 << 32, 2, 3, 1 << 32}, + }, + want: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, + }, + { + name: "[3]bool{true, false, true}", + args: args{ + [3]bool{true, false, true}, + }, + want: []byte{0x01, 0x00, 0x01}, + }, + { + name: "[2][]int{{0, 1}, {1, 0}}", + args: args{ + [2][]int{{0, 1}, {1, 0}}, + }, + want: []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, + }, + { + name: "[2][2]int{{0, 1}, {1, 0}}", + args: args{ + [2][2]int{{0, 1}, {1, 0}}, + }, + want: []byte{0x00, 0x04, 0x04, 0x00}, + }, + { + name: "[2]*big.Int{big.NewInt(0), big.NewInt(1)}", + args: args{ + [2]*big.Int{big.NewInt(0), big.NewInt(1)}, + }, + want: []byte{0x00, 0x04}, + }, + { + name: "[2][]byte{{0x00, 0x01}, {0x01, 0x00}}", + args: args{ + [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, + }, + want: []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, + }, + { + name: "[2][2]byte{{0x00, 0x01}, {0x01, 0x00}}", + args: args{ + [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, + }, + want: []byte{0x00, 0x01, 0x01, 0x00}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{ + Buffer: tt.fields.Buffer, + } + err := es.marshal(tt.args.t) + if (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeStruct() = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeStruct() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go new file mode 100644 index 0000000000..fd9f441964 --- /dev/null +++ b/pkg/scale/uint128.go @@ -0,0 +1,148 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . +package scale + +import ( + "encoding/binary" + "fmt" + "math/big" +) + +// Uint128 represents an unsigned 128 bit integer +type Uint128 struct { + Upper uint64 + Lower uint64 +} + +// MaxUint128 is the maximum uint128 value +var MaxUint128 = &Uint128{ + Upper: ^uint64(0), + Lower: ^uint64(0), +} + +// MustNewUint128 will panic if NewUint128 returns an error +func MustNewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128) { + u, err := NewUint128(in, order...) + if err != nil { + panic(err) + } + return +} + +func padBytes(b []byte, order binary.ByteOrder) []byte { + for len(b) != 16 { + switch order { + case binary.BigEndian: + b = append([]byte{0}, b...) + case binary.LittleEndian: + b = append(b, 0) + } + } + return b +} + +// NewUint128 is constructor for Uint128 that accepts an option binary.ByteOrder +// option is only used when inputted interface{} is of type []byte +// by default binary.LittleEndian is used for []byte since this is SCALE +func NewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128, err error) { + switch in := in.(type) { + case *big.Int: + bytes := in.Bytes() + if len(bytes) < 16 { + bytes = padBytes(bytes, binary.BigEndian) + } + u = &Uint128{ + Upper: binary.BigEndian.Uint64(bytes[:8]), + Lower: binary.BigEndian.Uint64(bytes[8:]), + } + case []byte: + var o binary.ByteOrder = binary.LittleEndian + if len(order) > 0 { + o = order[0] + } + if len(in) < 16 { + in = padBytes(in, o) + } + u = &Uint128{ + Upper: o.Uint64(in[8:]), + Lower: o.Uint64(in[:8]), + } + default: + err = fmt.Errorf("unsupported type: %T", in) + } + return +} + +// Bytes returns the Uint128 in little endian format by default. A variadic paramter +// order can be used to specify the binary.ByteOrder used +func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { + var o binary.ByteOrder = binary.LittleEndian + if len(order) > 0 { + o = order[0] + } + b = make([]byte, 16) + switch o { + case binary.LittleEndian: + o.PutUint64(b[:8], u.Lower) + o.PutUint64(b[8:], u.Upper) + b = u.trimBytes(b, o) + case binary.BigEndian: + o.PutUint64(b[:8], u.Upper) + o.PutUint64(b[8:], u.Lower) + b = u.trimBytes(b, o) + } + return +} + +// Cmp returns 1 if the receiver is greater than other, 0 if they are equal, and -1 otherwise. +func (u *Uint128) Compare(other *Uint128) int { + switch { + case u.Upper > other.Upper, u.Lower > other.Lower: + return 1 + case u.Upper < other.Upper, u.Lower < other.Lower: + return -1 + default: + return 0 + } +} + +func (u *Uint128) trimBytes(b []byte, order binary.ByteOrder) []byte { + switch order { + case binary.LittleEndian: + for { + if len(b) == 0 { + return b + } + if b[len(b)-1] == 0 { + b = b[:len(b)-1] + } else { + break + } + } + case binary.BigEndian: + for { + if len(b) == 0 { + return b + } + if b[0] == 0 { + b = b[1:] + } else { + break + } + } + } + return b +} diff --git a/pkg/scale/uint128_test.go b/pkg/scale/uint128_test.go new file mode 100644 index 0000000000..9936e74de6 --- /dev/null +++ b/pkg/scale/uint128_test.go @@ -0,0 +1,67 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . +package scale + +import ( + "encoding/binary" + "math/big" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestUint128FromBigInt(t *testing.T) { + bytes := []byte{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6} + bi := new(big.Int).SetBytes(bytes) + u, _ := NewUint128(bi) + res := u.Bytes(binary.BigEndian) + require.Equal(t, bytes, res) + + bytes = []byte{1, 2} + bi = new(big.Int).SetBytes(bytes) + u, _ = NewUint128(bi) + res = u.Bytes(binary.BigEndian) + require.Equal(t, bytes, res) +} + +func TestUint128FromLEBytes(t *testing.T) { + bytes := []byte{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6} + u, _ := NewUint128(bytes) + res := u.Bytes() + require.Equal(t, bytes, res) + + bytes = []byte{1, 2} + u, _ = NewUint128(bytes) + res = u.Bytes() + require.Equal(t, bytes, res) +} + +func TestUint128_Cmp(t *testing.T) { + bytes := []byte{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6} + u0, _ := NewUint128(bytes) + u1, _ := NewUint128(bytes) + require.Equal(t, 0, u0.Compare(u1)) + + bytes = []byte{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5} + u2, _ := NewUint128(bytes) + require.Equal(t, 1, u0.Compare(u2)) + require.Equal(t, -1, u2.Compare(u0)) + + bytes = []byte{1, 2, 3} + u3, _ := NewUint128(bytes) + require.Equal(t, 1, u0.Compare(u3)) + require.Equal(t, -1, u3.Compare(u0)) +} From 65388ca6c660bd99da34092fee3de0b1abd5280c Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 29 Apr 2021 22:54:08 -0400 Subject: [PATCH 002/245] add indices caching --- pkg/scale/scale.go | 122 ++++++++++++++++++++++++---------------- pkg/scale/scale_test.go | 9 ++- 2 files changed, 80 insertions(+), 51 deletions(-) diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 0570f2afe4..99312d4f14 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -10,8 +10,13 @@ import ( "strings" ) +// package level cache for fieldScaleIndicies +var cache = make(fieldScaleIndiciesCache) + func Marshal(v interface{}) (b []byte, err error) { - es := encodeState{} + es := encodeState{ + fieldScaleIndiciesCache: cache, + } err = es.marshal(v) if err != nil { return @@ -22,6 +27,7 @@ func Marshal(v interface{}) (b []byte, err error) { type encodeState struct { bytes.Buffer + fieldScaleIndiciesCache } func (es *encodeState) marshal(in interface{}) (err error) { @@ -267,57 +273,10 @@ func (es *encodeState) encodeFixedWidthInt(i interface{}) (err error) { // encodeStruct reads the number of fields in the struct and their types and writes to the buffer each of the struct fields // encoded as their respective types func (es *encodeState) encodeStruct(in interface{}) (err error) { - t := reflect.TypeOf(in) - v := reflect.ValueOf(in) - - if !v.IsValid() { - err = fmt.Errorf("inputted value is not valid: %v", v) + v, indices, err := es.fieldScaleIndicies(in) + if err != nil { return } - - type fieldScaleIndex struct { - fieldIndex int - scaleIndex *string - } - // Get the type and kind of our user variable - indices := make([]fieldScaleIndex, 0) - - // Iterate over all available fields and read the tag value - for i := 0; i < t.NumField(); i++ { - field := t.Field(i) - - // Get the field tag value - tag := field.Tag.Get("scale") - switch strings.TrimSpace(tag) { - case "": - indices = append(indices, fieldScaleIndex{ - fieldIndex: i, - }) - case "-": - // ignore this field - continue - default: - indices = append(indices, fieldScaleIndex{ - fieldIndex: i, - scaleIndex: &tag, - }) - } - } - - sort.Slice(indices[:], func(i, j int) bool { - switch { - case indices[i].scaleIndex == nil && indices[j].scaleIndex != nil: - return false - case indices[i].scaleIndex != nil && indices[j].scaleIndex == nil: - return true - case indices[i].scaleIndex == nil && indices[j].scaleIndex == nil: - return indices[i].fieldIndex < indices[j].fieldIndex - case indices[i].scaleIndex != nil && indices[j].scaleIndex != nil: - return *indices[i].scaleIndex < *indices[j].scaleIndex - } - return false - }) - for _, i := range indices { field := v.Field(i.fieldIndex) if !field.CanInterface() { @@ -383,6 +342,69 @@ func (es *encodeState) encodeUint128(i Uint128) (err error) { return } +type fieldScaleIndex struct { + fieldIndex int + scaleIndex *string +} +type fieldScaleIndicies []fieldScaleIndex +type fieldScaleIndiciesCache map[string]fieldScaleIndicies + +func (fsic fieldScaleIndiciesCache) fieldScaleIndicies(in interface{}) (v reflect.Value, indices fieldScaleIndicies, err error) { + t := reflect.TypeOf(in) + v = reflect.ValueOf(in) + key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) + if key != "." { + var ok bool + indices, ok = fsic[key] + if ok { + return + } + } + + if !v.IsValid() { + err = fmt.Errorf("inputted value is not valid: %v", v) + return + } + + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + tag := field.Tag.Get("scale") + switch strings.TrimSpace(tag) { + case "": + indices = append(indices, fieldScaleIndex{ + fieldIndex: i, + }) + case "-": + // ignore this field + continue + default: + indices = append(indices, fieldScaleIndex{ + fieldIndex: i, + scaleIndex: &tag, + }) + } + } + + sort.Slice(indices[:], func(i, j int) bool { + switch { + case indices[i].scaleIndex == nil && indices[j].scaleIndex != nil: + return false + case indices[i].scaleIndex != nil && indices[j].scaleIndex == nil: + return true + case indices[i].scaleIndex == nil && indices[j].scaleIndex == nil: + return indices[i].fieldIndex < indices[j].fieldIndex + case indices[i].scaleIndex != nil && indices[j].scaleIndex != nil: + return *indices[i].scaleIndex < *indices[j].scaleIndex + } + return false + }) + + // TODO: mutex? + fsic[key] = indices + + return +} + func reverseBytes(a []byte) []byte { for i := len(a)/2 - 1; i >= 0; i-- { opp := len(a) - 1 - i diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index 61f82a31ca..4bf343ebd4 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -525,6 +525,13 @@ func Test_encodeState_encodeStruct(t *testing.T) { }, want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, }, + { + name: "ptrMystruct cache hit", + args: args{ + ptrMystruct, + }, + want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + }, { name: "nilPtrMyStruct2", args: args{ @@ -677,7 +684,7 @@ func Test_encodeState_encodeStruct(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - es := &encodeState{} + es := &encodeState{fieldScaleIndiciesCache: cache} err := es.marshal(tt.args.t) if (err != nil) != tt.wantErr { t.Errorf("encodeState.encodeStruct() = %v, wantErr %v", err, tt.wantErr) From d586e74226dffc9320fd634325f828e8d4aa4275 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 30 Apr 2021 12:11:04 -0400 Subject: [PATCH 003/245] add mtx --- pkg/scale/scale.go | 147 +++++++++++++++++++++------------------- pkg/scale/scale_test.go | 2 +- 2 files changed, 80 insertions(+), 69 deletions(-) diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 99312d4f14..1b62baf6dd 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -8,14 +8,88 @@ import ( "reflect" "sort" "strings" + "sync" ) // package level cache for fieldScaleIndicies -var cache = make(fieldScaleIndiciesCache) +var cache = &fieldScaleIndicesCache{ + cache: make(map[string]fieldScaleIndices), +} + +// fieldScaleIndex is used to map field index to scale index +type fieldScaleIndex struct { + fieldIndex int + scaleIndex *string +} +type fieldScaleIndices []fieldScaleIndex + +// fieldScaleIndic +type fieldScaleIndicesCache struct { + cache map[string]fieldScaleIndices + sync.RWMutex +} + +func (fsic *fieldScaleIndicesCache) fieldScaleIndices(in interface{}) (v reflect.Value, indices fieldScaleIndices, err error) { + t := reflect.TypeOf(in) + v = reflect.ValueOf(in) + key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) + if key != "." { + var ok bool + fsic.RLock() + indices, ok = fsic.cache[key] + fsic.RUnlock() + if ok { + return + } + } + + if !v.IsValid() { + err = fmt.Errorf("inputted value is not valid: %v", v) + return + } + + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + tag := field.Tag.Get("scale") + switch strings.TrimSpace(tag) { + case "": + indices = append(indices, fieldScaleIndex{ + fieldIndex: i, + }) + case "-": + // ignore this field + continue + default: + indices = append(indices, fieldScaleIndex{ + fieldIndex: i, + scaleIndex: &tag, + }) + } + } + + sort.Slice(indices[:], func(i, j int) bool { + switch { + case indices[i].scaleIndex == nil && indices[j].scaleIndex != nil: + return false + case indices[i].scaleIndex != nil && indices[j].scaleIndex == nil: + return true + case indices[i].scaleIndex == nil && indices[j].scaleIndex == nil: + return indices[i].fieldIndex < indices[j].fieldIndex + case indices[i].scaleIndex != nil && indices[j].scaleIndex != nil: + return *indices[i].scaleIndex < *indices[j].scaleIndex + } + return false + }) + + fsic.Lock() + fsic.cache[key] = indices + fsic.Unlock() + return +} func Marshal(v interface{}) (b []byte, err error) { es := encodeState{ - fieldScaleIndiciesCache: cache, + fieldScaleIndicesCache: cache, } err = es.marshal(v) if err != nil { @@ -27,7 +101,7 @@ func Marshal(v interface{}) (b []byte, err error) { type encodeState struct { bytes.Buffer - fieldScaleIndiciesCache + *fieldScaleIndicesCache } func (es *encodeState) marshal(in interface{}) (err error) { @@ -241,7 +315,7 @@ func (es *encodeState) encodeBytes(b []byte) (err error) { return } -// encodeFixedWidthInteger encodes an int with size < 2**32 by putting it into little endian byte format +// encodeFixedWidthInt encodes an int with size < 2**32 by putting it into little endian byte format func (es *encodeState) encodeFixedWidthInt(i interface{}) (err error) { switch i := i.(type) { case int8: @@ -273,7 +347,7 @@ func (es *encodeState) encodeFixedWidthInt(i interface{}) (err error) { // encodeStruct reads the number of fields in the struct and their types and writes to the buffer each of the struct fields // encoded as their respective types func (es *encodeState) encodeStruct(in interface{}) (err error) { - v, indices, err := es.fieldScaleIndicies(in) + v, indices, err := es.fieldScaleIndices(in) if err != nil { return } @@ -342,69 +416,6 @@ func (es *encodeState) encodeUint128(i Uint128) (err error) { return } -type fieldScaleIndex struct { - fieldIndex int - scaleIndex *string -} -type fieldScaleIndicies []fieldScaleIndex -type fieldScaleIndiciesCache map[string]fieldScaleIndicies - -func (fsic fieldScaleIndiciesCache) fieldScaleIndicies(in interface{}) (v reflect.Value, indices fieldScaleIndicies, err error) { - t := reflect.TypeOf(in) - v = reflect.ValueOf(in) - key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) - if key != "." { - var ok bool - indices, ok = fsic[key] - if ok { - return - } - } - - if !v.IsValid() { - err = fmt.Errorf("inputted value is not valid: %v", v) - return - } - - for i := 0; i < t.NumField(); i++ { - field := t.Field(i) - tag := field.Tag.Get("scale") - switch strings.TrimSpace(tag) { - case "": - indices = append(indices, fieldScaleIndex{ - fieldIndex: i, - }) - case "-": - // ignore this field - continue - default: - indices = append(indices, fieldScaleIndex{ - fieldIndex: i, - scaleIndex: &tag, - }) - } - } - - sort.Slice(indices[:], func(i, j int) bool { - switch { - case indices[i].scaleIndex == nil && indices[j].scaleIndex != nil: - return false - case indices[i].scaleIndex != nil && indices[j].scaleIndex == nil: - return true - case indices[i].scaleIndex == nil && indices[j].scaleIndex == nil: - return indices[i].fieldIndex < indices[j].fieldIndex - case indices[i].scaleIndex != nil && indices[j].scaleIndex != nil: - return *indices[i].scaleIndex < *indices[j].scaleIndex - } - return false - }) - - // TODO: mutex? - fsic[key] = indices - - return -} - func reverseBytes(a []byte) []byte { for i := len(a)/2 - 1; i >= 0; i-- { opp := len(a) - 1 - i diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index 4bf343ebd4..f91e62c70a 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -684,7 +684,7 @@ func Test_encodeState_encodeStruct(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - es := &encodeState{fieldScaleIndiciesCache: cache} + es := &encodeState{fieldScaleIndicesCache: cache} err := es.marshal(tt.args.t) if (err != nil) != tt.wantErr { t.Errorf("encodeState.encodeStruct() = %v, wantErr %v", err, tt.wantErr) From c1d0da107a590e9b7d8c028b642f9b2f224713bb Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 4 May 2021 10:57:34 -0400 Subject: [PATCH 004/245] add variable data type interface, test to compare old vs new encoding --- pkg/scale/comparison_test.go | 144 ++++++++ pkg/scale/decode.go | 1 + pkg/scale/encode.go | 357 ++++++++++++++++++++ pkg/scale/{scale_test.go => encode_test.go} | 2 +- pkg/scale/scale.go | 339 +------------------ 5 files changed, 510 insertions(+), 333 deletions(-) create mode 100644 pkg/scale/comparison_test.go create mode 100644 pkg/scale/decode.go create mode 100644 pkg/scale/encode.go rename pkg/scale/{scale_test.go => encode_test.go} (99%) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go new file mode 100644 index 0000000000..56a02df047 --- /dev/null +++ b/pkg/scale/comparison_test.go @@ -0,0 +1,144 @@ +package scale + +import ( + "encoding/binary" + "reflect" + "testing" + + "github.com/ChainSafe/gossamer/dot/types" + "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/lib/crypto/sr25519" +) + +// BabePrimaryPreDigest as defined in Polkadot RE Spec, definition 5.10 in section 5.1.4 +type BabePrimaryPreDigest struct { + AuthorityIndex uint32 + SlotNumber uint64 + VrfOutput [sr25519.VrfOutputLength]byte + VrfProof [sr25519.VrfProofLength]byte +} + +// Encode performs SCALE encoding of a BABEPrimaryPreDigest +func (d *BabePrimaryPreDigest) Encode() []byte { + enc := []byte{byte(1)} + + buf := make([]byte, 4) + binary.LittleEndian.PutUint32(buf, d.AuthorityIndex) + enc = append(enc, buf...) + + buf = make([]byte, 8) + binary.LittleEndian.PutUint64(buf, d.SlotNumber) + enc = append(enc, buf...) + enc = append(enc, d.VrfOutput[:]...) + enc = append(enc, d.VrfProof[:]...) + return enc +} + +func TestOldVsNewEncoding(t *testing.T) { + bh := &BabePrimaryPreDigest{ + VrfOutput: [sr25519.VrfOutputLength]byte{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, + VrfProof: [sr25519.VrfProofLength]byte{120, 23, 235, 159, 115, 122, 207, 206, 123, 232, 75, 243, 115, 255, 131, 181, 219, 241, 200, 206, 21, 22, 238, 16, 68, 49, 86, 99, 76, 139, 39, 0, 102, 106, 181, 136, 97, 141, 187, 1, 234, 183, 241, 28, 27, 229, 133, 8, 32, 246, 245, 206, 199, 142, 134, 124, 226, 217, 95, 30, 176, 246, 5, 3}, + AuthorityIndex: 17, + SlotNumber: 420, + } + oldEncode := bh.Encode() + newEncode, err := Marshal(bh) + if err != nil { + t.Errorf("unexpected err: %v", err) + return + } + if !reflect.DeepEqual(oldEncode, newEncode) { + t.Errorf("encodeState.encodeStruct() = %v, want %v", oldEncode, newEncode) + } +} + +// ChangesTrieRootDigest contains the root of the changes trie at a given block, if the runtime supports it. +type ChangesTrieRootDigest struct { + Hash common.Hash +} + +func (ctrd ChangesTrieRootDigest) Index() uint { + return 2 +} + +// PreRuntimeDigest contains messages from the consensus engine to the runtime. +type PreRuntimeDigest struct { + ConsensusEngineID types.ConsensusEngineID + Data []byte +} + +func (prd PreRuntimeDigest) Index() uint { + return 6 +} + +// ConsensusDigest contains messages from the runtime to the consensus engine. +type ConsensusDigest struct { + ConsensusEngineID types.ConsensusEngineID + Data []byte +} + +func (prd ConsensusDigest) Index() uint { + return 4 +} + +// SealDigest contains the seal or signature. This is only used by native code. +type SealDigest struct { + ConsensusEngineID types.ConsensusEngineID + Data []byte +} + +func (prd SealDigest) Index() uint { + return 5 +} + +func TestOldVsNewEncoding2(t *testing.T) { + oldDigest := types.Digest{ + &types.ChangesTrieRootDigest{ + Hash: common.Hash{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, + }, + &types.PreRuntimeDigest{ + ConsensusEngineID: types.BabeEngineID, + Data: []byte{1, 3, 5, 7}, + }, + &types.ConsensusDigest{ + ConsensusEngineID: types.BabeEngineID, + Data: []byte{1, 3, 5, 7}, + }, + &types.SealDigest{ + ConsensusEngineID: types.BabeEngineID, + Data: []byte{1, 3, 5, 7}, + }, + } + oldEncode, err := oldDigest.Encode() + if err != nil { + t.Errorf("unexpected err: %v", err) + return + } + + newDigest := VaryingDataType{ + ChangesTrieRootDigest{ + Hash: common.Hash{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, + }, + PreRuntimeDigest{ + ConsensusEngineID: types.BabeEngineID, + Data: []byte{1, 3, 5, 7}, + }, + ConsensusDigest{ + ConsensusEngineID: types.BabeEngineID, + Data: []byte{1, 3, 5, 7}, + }, + SealDigest{ + ConsensusEngineID: types.BabeEngineID, + Data: []byte{1, 3, 5, 7}, + }, + } + + newEncode, err := Marshal(newDigest) + if err != nil { + t.Errorf("unexpected err: %v", err) + return + } + if !reflect.DeepEqual(oldEncode, newEncode) { + t.Errorf("encodeState.encodeStruct() = %v, want %v", oldEncode, newEncode) + } +} diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go new file mode 100644 index 0000000000..fa7973da4c --- /dev/null +++ b/pkg/scale/decode.go @@ -0,0 +1 @@ +package scale diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go new file mode 100644 index 0000000000..1ba1a0af11 --- /dev/null +++ b/pkg/scale/encode.go @@ -0,0 +1,357 @@ +package scale + +import ( + "bytes" + "encoding/binary" + "fmt" + "math/big" + "reflect" +) + +func Marshal(v interface{}) (b []byte, err error) { + es := encodeState{ + fieldScaleIndicesCache: cache, + } + err = es.marshal(v) + if err != nil { + return + } + b = es.Bytes() + return +} + +type encodeState struct { + bytes.Buffer + *fieldScaleIndicesCache +} + +func (es *encodeState) marshal(in interface{}) (err error) { + switch in := in.(type) { + case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: + err = es.encodeFixedWidthInt(in) + case *big.Int: + err = es.encodeBigInt(in) + case Uint128: + err = es.encodeUint128(in) + case []byte: + err = es.encodeBytes(in) + case string: + err = es.encodeBytes([]byte(in)) + case bool: + err = es.encodeBool(in) + case VaryingDataType: + err = es.encodeVaryingDataType(in) + // TODO: case common.Hash: + // n, err = es.Writer.Write(v.ToBytes()) + default: + switch reflect.TypeOf(in).Kind() { + case reflect.Ptr: + // Assuming that anything that is a pointer is an Option to capture {nil, T} + elem := reflect.ValueOf(in).Elem() + switch elem.IsValid() { + case false: + err = es.WriteByte(0) + default: + err = es.WriteByte(1) + if err != nil { + return + } + err = es.marshal(elem.Interface()) + } + case reflect.Struct: + err = es.encodeStruct(in) + case reflect.Array: + err = es.encodeArray(in) + case reflect.Slice: + err = es.encodeSlice(in) + default: + err = fmt.Errorf("unsupported type: %T", in) + } + } + return +} + +func (es *encodeState) encodeVaryingDataType(values VaryingDataType) (err error) { + // err = es.encodeLength(len(types)) + err = es.encodeLength(len(values)) + if err != nil { + return + } + for _, t := range values { + // encode type.Index (idx) for varying data type + err = es.WriteByte(byte(t.Index())) + if err != nil { + return + } + err = es.marshal(t) + } + return +} + +func (es *encodeState) encodeSlice(t interface{}) (err error) { + switch arr := t.(type) { + // int is the only case that handles encoding differently. + // all other cases can recursively call es.marshal() + case []int: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.encodeUint(uint(elem)) + if err != nil { + return + } + } + // the cases below are to avoid using the reflect library + // for commonly used slices in gossamer + case []*big.Int: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.marshal(elem) + if err != nil { + return + } + } + case []bool: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.marshal(elem) + if err != nil { + return + } + } + case [][]byte: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.marshal(elem) + if err != nil { + return + } + } + case [][]int: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.marshal(elem) + if err != nil { + return + } + } + case []string: + err = es.encodeLength(len(arr)) + if err != nil { + return + } + for _, elem := range arr { + err = es.marshal(elem) + if err != nil { + return + } + } + default: + // use reflect package for any other cases + s := reflect.ValueOf(t) + err = es.encodeUint(uint(s.Len())) + if err != nil { + return + } + for i := 0; i < s.Len(); i++ { + err = es.marshal(s.Index(i).Interface()) + if err != nil { + return + } + } + } + return +} + +// encodeArray encodes an interface where the underlying type is an array +// it writes the encoded length of the Array to the Encoder, then encodes and writes each value in the Array +func (es *encodeState) encodeArray(in interface{}) (err error) { + v := reflect.ValueOf(in) + for i := 0; i < v.Len(); i++ { + elem := v.Index(i).Interface() + switch elem := elem.(type) { + case int: + // an array of unsized integers needs to be encoded using scale length encoding + err = es.encodeUint(uint(elem)) + if err != nil { + return + } + default: + err = es.marshal(v.Index(i).Interface()) + if err != nil { + return + } + } + } + return +} + +// encodeBigInt performs the same encoding as encodeInteger, except on a big.Int. +// if 2^30 <= n < 2^536 write [lower 2 bits of first byte = 11] [upper 6 bits of first byte = # of bytes following less 4] +// [append i as a byte array to the first byte] +func (es *encodeState) encodeBigInt(i *big.Int) (err error) { + switch { + case i == nil: + err = fmt.Errorf("nil *big.Int") + case i.Cmp(new(big.Int).Lsh(big.NewInt(1), 6)) < 0: + err = binary.Write(es, binary.LittleEndian, uint8(i.Int64()<<2)) + case i.Cmp(new(big.Int).Lsh(big.NewInt(1), 14)) < 0: + err = binary.Write(es, binary.LittleEndian, uint16(i.Int64()<<2)+1) + case i.Cmp(new(big.Int).Lsh(big.NewInt(1), 30)) < 0: + err = binary.Write(es, binary.LittleEndian, uint32(i.Int64()<<2)+2) + default: + numBytes := len(i.Bytes()) + topSixBits := uint8(numBytes - 4) + lengthByte := topSixBits<<2 + 3 + + // write byte which encodes mode and length + err = binary.Write(es, binary.LittleEndian, lengthByte) + if err == nil { + // write integer itself + err = binary.Write(es, binary.LittleEndian, reverseBytes(i.Bytes())) + } + } + return +} + +// encodeBool performs the following: +// l = true -> write [1] +// l = false -> write [0] +func (es *encodeState) encodeBool(l bool) (err error) { + switch l { + case true: + _, err = es.Write([]byte{0x01}) + case false: + _, err = es.Write([]byte{0x00}) + } + return +} + +// encodeByteArray performs the following: +// b -> [encodeInteger(len(b)) b] +// it writes to the buffer a byte array where the first byte is the length of b encoded with SCALE, followed by the +// byte array b itself +func (es *encodeState) encodeBytes(b []byte) (err error) { + err = es.encodeUint(uint(len(b))) + if err != nil { + return + } + + _, err = es.Write(b) + return +} + +// encodeFixedWidthInt encodes an int with size < 2**32 by putting it into little endian byte format +func (es *encodeState) encodeFixedWidthInt(i interface{}) (err error) { + switch i := i.(type) { + case int8: + err = binary.Write(es, binary.LittleEndian, byte(i)) + case uint8: + err = binary.Write(es, binary.LittleEndian, i) + case int16: + err = binary.Write(es, binary.LittleEndian, uint16(i)) + case uint16: + err = binary.Write(es, binary.LittleEndian, i) + case int32: + err = binary.Write(es, binary.LittleEndian, uint32(i)) + case uint32: + err = binary.Write(es, binary.LittleEndian, i) + case int64: + err = binary.Write(es, binary.LittleEndian, uint64(i)) + case uint64: + err = binary.Write(es, binary.LittleEndian, i) + case int: + err = binary.Write(es, binary.LittleEndian, int64(i)) + case uint: + err = binary.Write(es, binary.LittleEndian, uint64(i)) + default: + err = fmt.Errorf("could not encode fixed width integer, invalid type: %T", i) + } + return +} + +// encodeStruct reads the number of fields in the struct and their types and writes to the buffer each of the struct fields +// encoded as their respective types +func (es *encodeState) encodeStruct(in interface{}) (err error) { + v, indices, err := es.fieldScaleIndices(in) + if err != nil { + return + } + for _, i := range indices { + field := v.Field(i.fieldIndex) + if !field.CanInterface() { + continue + } + err = es.marshal(field.Interface()) + if err != nil { + return + } + } + return +} + +// encodeLength is a helper function that calls encodeUint, which is the scale length encoding +func (es *encodeState) encodeLength(l int) (err error) { + return es.encodeUint(uint(l)) +} + +// encodeUint performs the following on integer i: +// i -> i^0...i^n where n is the length in bits of i +// note that the bit representation of i is in little endian; ie i^0 is the least significant bit of i, +// and i^n is the most significant bit +// if n < 2^6 write [00 i^2...i^8 ] [ 8 bits = 1 byte encoded ] +// if 2^6 <= n < 2^14 write [01 i^2...i^16] [ 16 bits = 2 byte encoded ] +// if 2^14 <= n < 2^30 write [10 i^2...i^32] [ 32 bits = 4 byte encoded ] +// if n >= 2^30 write [lower 2 bits of first byte = 11] [upper 6 bits of first byte = # of bytes following less 4] +// [append i as a byte array to the first byte] +func (es *encodeState) encodeUint(i uint) (err error) { + switch { + case i < 1<<6: + err = binary.Write(es, binary.LittleEndian, byte(i)<<2) + case i < 1<<14: + err = binary.Write(es, binary.LittleEndian, uint16(i<<2)+1) + case i < 1<<30: + err = binary.Write(es, binary.LittleEndian, uint32(i<<2)+2) + default: + o := make([]byte, 8) + m := i + var numBytes int + // calculate the number of bytes needed to store i + // the most significant byte cannot be zero + // each iteration, shift by 1 byte until the number is zero + // then break and save the numBytes needed + for numBytes = 0; numBytes < 256 && m != 0; numBytes++ { + m = m >> 8 + } + + topSixBits := uint8(numBytes - 4) + lengthByte := topSixBits<<2 + 3 + + err = binary.Write(es, binary.LittleEndian, lengthByte) + if err == nil { + binary.LittleEndian.PutUint64(o, uint64(i)) + err = binary.Write(es, binary.LittleEndian, o[0:numBytes]) + } + } + return +} + +// encodeUint128 encodes a Uint128 +func (es *encodeState) encodeUint128(i Uint128) (err error) { + err = binary.Write(es, binary.LittleEndian, i.Bytes()) + return +} diff --git a/pkg/scale/scale_test.go b/pkg/scale/encode_test.go similarity index 99% rename from pkg/scale/scale_test.go rename to pkg/scale/encode_test.go index f91e62c70a..5791436b47 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/encode_test.go @@ -573,7 +573,7 @@ func Test_encodeState_encodeStruct(t *testing.T) { name: "struct {[]byte, int32, bool}", args: args{ t: struct { - Baz bool `scale:"3"` + Baz bool `scale:"3,enum"` Bar int32 `scale:"2"` Foo []byte `scale:"1"` }{ diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 1b62baf6dd..035f156a9a 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -1,16 +1,20 @@ package scale import ( - "bytes" - "encoding/binary" "fmt" - "math/big" "reflect" "sort" "strings" "sync" ) +type VaryingDataType []VaryingDataTypeValue + +// VaryingDataType is used to represent scale encodable types +type VaryingDataTypeValue interface { + Index() uint +} + // package level cache for fieldScaleIndicies var cache = &fieldScaleIndicesCache{ cache: make(map[string]fieldScaleIndices), @@ -87,335 +91,6 @@ func (fsic *fieldScaleIndicesCache) fieldScaleIndices(in interface{}) (v reflect return } -func Marshal(v interface{}) (b []byte, err error) { - es := encodeState{ - fieldScaleIndicesCache: cache, - } - err = es.marshal(v) - if err != nil { - return - } - b = es.Bytes() - return -} - -type encodeState struct { - bytes.Buffer - *fieldScaleIndicesCache -} - -func (es *encodeState) marshal(in interface{}) (err error) { - switch in := in.(type) { - case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: - err = es.encodeFixedWidthInt(in) - case *big.Int: - err = es.encodeBigInt(in) - case Uint128: - err = es.encodeUint128(in) - case []byte: - err = es.encodeBytes(in) - case string: - err = es.encodeBytes([]byte(in)) - case bool: - err = es.encodeBool(in) - // TODO: case common.Hash: - // n, err = es.Writer.Write(v.ToBytes()) - default: - switch reflect.TypeOf(in).Kind() { - case reflect.Ptr: - // Assuming that anything that is a pointer is an Option to capture {nil, T} - elem := reflect.ValueOf(in).Elem() - switch elem.IsValid() { - case false: - err = es.WriteByte(0) - default: - err = es.WriteByte(1) - if err != nil { - return - } - err = es.marshal(elem.Interface()) - } - case reflect.Struct: - err = es.encodeStruct(in) - case reflect.Array: - err = es.encodeArray(in) - case reflect.Slice: - err = es.encodeSlice(in) - default: - err = fmt.Errorf("unsupported type: %T", in) - } - } - return -} - -func (es *encodeState) encodeSlice(t interface{}) (err error) { - switch arr := t.(type) { - // int is the only case that handles encoding differently. - // all other cases can recursively call es.marshal() - case []int: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.encodeUint(uint(elem)) - if err != nil { - return - } - } - // the cases below are to avoid using the reflect library - // for commonly used slices in gossamer - case []*big.Int: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.marshal(elem) - if err != nil { - return - } - } - case []bool: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.marshal(elem) - if err != nil { - return - } - } - case [][]byte: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.marshal(elem) - if err != nil { - return - } - } - case [][]int: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.marshal(elem) - if err != nil { - return - } - } - case []string: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.marshal(elem) - if err != nil { - return - } - } - default: - // use reflect package for any other cases - s := reflect.ValueOf(t) - err = es.encodeUint(uint(s.Len())) - if err != nil { - return - } - for i := 0; i < s.Len(); i++ { - err = es.marshal(s.Index(i).Interface()) - if err != nil { - return - } - } - } - return -} - -// encodeArray encodes an interface where the underlying type is an array -// it writes the encoded length of the Array to the Encoder, then encodes and writes each value in the Array -func (es *encodeState) encodeArray(in interface{}) (err error) { - v := reflect.ValueOf(in) - for i := 0; i < v.Len(); i++ { - elem := v.Index(i).Interface() - switch elem := elem.(type) { - case int: - // an array of unsized integers needs to be encoded using scale length encoding - err = es.encodeUint(uint(elem)) - if err != nil { - return - } - default: - err = es.marshal(v.Index(i).Interface()) - if err != nil { - return - } - } - } - return -} - -// encodeBigInt performs the same encoding as encodeInteger, except on a big.Int. -// if 2^30 <= n < 2^536 write [lower 2 bits of first byte = 11] [upper 6 bits of first byte = # of bytes following less 4] -// [append i as a byte array to the first byte] -func (es *encodeState) encodeBigInt(i *big.Int) (err error) { - switch { - case i == nil: - err = fmt.Errorf("nil *big.Int") - case i.Cmp(new(big.Int).Lsh(big.NewInt(1), 6)) < 0: - err = binary.Write(es, binary.LittleEndian, uint8(i.Int64()<<2)) - case i.Cmp(new(big.Int).Lsh(big.NewInt(1), 14)) < 0: - err = binary.Write(es, binary.LittleEndian, uint16(i.Int64()<<2)+1) - case i.Cmp(new(big.Int).Lsh(big.NewInt(1), 30)) < 0: - err = binary.Write(es, binary.LittleEndian, uint32(i.Int64()<<2)+2) - default: - numBytes := len(i.Bytes()) - topSixBits := uint8(numBytes - 4) - lengthByte := topSixBits<<2 + 3 - - // write byte which encodes mode and length - err = binary.Write(es, binary.LittleEndian, lengthByte) - if err == nil { - // write integer itself - err = binary.Write(es, binary.LittleEndian, reverseBytes(i.Bytes())) - } - } - return -} - -// encodeBool performs the following: -// l = true -> write [1] -// l = false -> write [0] -func (es *encodeState) encodeBool(l bool) (err error) { - switch l { - case true: - _, err = es.Write([]byte{0x01}) - case false: - _, err = es.Write([]byte{0x00}) - } - return -} - -// encodeByteArray performs the following: -// b -> [encodeInteger(len(b)) b] -// it writes to the buffer a byte array where the first byte is the length of b encoded with SCALE, followed by the -// byte array b itself -func (es *encodeState) encodeBytes(b []byte) (err error) { - err = es.encodeUint(uint(len(b))) - if err != nil { - return - } - - _, err = es.Write(b) - return -} - -// encodeFixedWidthInt encodes an int with size < 2**32 by putting it into little endian byte format -func (es *encodeState) encodeFixedWidthInt(i interface{}) (err error) { - switch i := i.(type) { - case int8: - err = binary.Write(es, binary.LittleEndian, byte(i)) - case uint8: - err = binary.Write(es, binary.LittleEndian, i) - case int16: - err = binary.Write(es, binary.LittleEndian, uint16(i)) - case uint16: - err = binary.Write(es, binary.LittleEndian, i) - case int32: - err = binary.Write(es, binary.LittleEndian, uint32(i)) - case uint32: - err = binary.Write(es, binary.LittleEndian, i) - case int64: - err = binary.Write(es, binary.LittleEndian, uint64(i)) - case uint64: - err = binary.Write(es, binary.LittleEndian, i) - case int: - err = binary.Write(es, binary.LittleEndian, int64(i)) - case uint: - err = binary.Write(es, binary.LittleEndian, uint64(i)) - default: - err = fmt.Errorf("could not encode fixed width integer, invalid type: %T", i) - } - return -} - -// encodeStruct reads the number of fields in the struct and their types and writes to the buffer each of the struct fields -// encoded as their respective types -func (es *encodeState) encodeStruct(in interface{}) (err error) { - v, indices, err := es.fieldScaleIndices(in) - if err != nil { - return - } - for _, i := range indices { - field := v.Field(i.fieldIndex) - if !field.CanInterface() { - continue - } - err = es.marshal(field.Interface()) - if err != nil { - return - } - } - return -} - -// encodeLength is a helper function that calls encodeUint, which is the scale length encoding -func (es *encodeState) encodeLength(l int) (err error) { - return es.encodeUint(uint(l)) -} - -// encodeUint performs the following on integer i: -// i -> i^0...i^n where n is the length in bits of i -// note that the bit representation of i is in little endian; ie i^0 is the least significant bit of i, -// and i^n is the most significant bit -// if n < 2^6 write [00 i^2...i^8 ] [ 8 bits = 1 byte encoded ] -// if 2^6 <= n < 2^14 write [01 i^2...i^16] [ 16 bits = 2 byte encoded ] -// if 2^14 <= n < 2^30 write [10 i^2...i^32] [ 32 bits = 4 byte encoded ] -// if n >= 2^30 write [lower 2 bits of first byte = 11] [upper 6 bits of first byte = # of bytes following less 4] -// [append i as a byte array to the first byte] -func (es *encodeState) encodeUint(i uint) (err error) { - switch { - case i < 1<<6: - err = binary.Write(es, binary.LittleEndian, byte(i)<<2) - case i < 1<<14: - err = binary.Write(es, binary.LittleEndian, uint16(i<<2)+1) - case i < 1<<30: - err = binary.Write(es, binary.LittleEndian, uint32(i<<2)+2) - default: - o := make([]byte, 8) - m := i - var numBytes int - // calculate the number of bytes needed to store i - // the most significant byte cannot be zero - // each iteration, shift by 1 byte until the number is zero - // then break and save the numBytes needed - for numBytes = 0; numBytes < 256 && m != 0; numBytes++ { - m = m >> 8 - } - - topSixBits := uint8(numBytes - 4) - lengthByte := topSixBits<<2 + 3 - - err = binary.Write(es, binary.LittleEndian, lengthByte) - if err == nil { - binary.LittleEndian.PutUint64(o, uint64(i)) - err = binary.Write(es, binary.LittleEndian, o[0:numBytes]) - } - } - return -} - -// encodeUint128 encodes a Uint128 -func (es *encodeState) encodeUint128(i Uint128) (err error) { - err = binary.Write(es, binary.LittleEndian, i.Bytes()) - return -} - func reverseBytes(a []byte) []byte { for i := len(a)/2 - 1; i >= 0; i-- { opp := len(a) - 1 - i From 1ddf6aff6642950c2924f81499fc87a6d74b4510 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 4 May 2021 22:37:31 -0400 Subject: [PATCH 005/245] fix width int decode and tests --- pkg/scale/decode.go | 118 ++++++++ pkg/scale/decode_test.go | 565 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 683 insertions(+) create mode 100644 pkg/scale/decode_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index fa7973da4c..6bd2e01069 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -1 +1,119 @@ package scale + +import ( + "bytes" + "encoding/binary" + "fmt" + "reflect" +) + +func Unmarshal(data []byte, dst interface{}) (err error) { + buf := &bytes.Buffer{} + ds := decodeState{} + _, err = buf.Write(data) + if err != nil { + return + } + ds.Buffer = *buf + err = ds.unmarshal(dst) + return +} + +type decodeState struct { + bytes.Buffer +} + +func (ds *decodeState) unmarshal(dst interface{}) (err error) { + rv := reflect.ValueOf(dst) + if rv.Kind() != reflect.Ptr || rv.IsNil() { + err = fmt.Errorf("unsupported dst: %T", dst) + } + + in := rv.Elem().Interface() + switch in.(type) { + case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: + in, err = ds.decodeFixedWidthInt(in) + } + if err != nil { + return + } + rv.Elem().Set(reflect.ValueOf(in)) + return +} + +// DdcodeFixedWidthInt decodes integers < 2**32 by reading the bytes in little endian +func (ds *decodeState) decodeFixedWidthInt(in interface{}) (out interface{}, err error) { + switch in.(type) { + case int8: + var b byte + b, err = ds.ReadByte() + if err != nil { + break + } + out = int8(b) + case uint8: + var b byte + b, err = ds.ReadByte() + if err != nil { + break + } + out = uint8(b) + case int16: + buf := make([]byte, 2) + _, err = ds.Read(buf) + if err != nil { + break + } + out = int16(binary.LittleEndian.Uint16(buf)) + case uint16: + buf := make([]byte, 2) + _, err = ds.Read(buf) + if err != nil { + break + } + out = binary.LittleEndian.Uint16(buf) + case int32: + buf := make([]byte, 4) + _, err = ds.Read(buf) + if err != nil { + break + } + out = int32(binary.LittleEndian.Uint32(buf)) + case uint32: + buf := make([]byte, 4) + _, err = ds.Read(buf) + if err != nil { + break + } + out = binary.LittleEndian.Uint32(buf) + case int64: + buf := make([]byte, 8) + _, err = ds.Read(buf) + if err != nil { + break + } + out = int64(binary.LittleEndian.Uint64(buf)) + case uint64: + buf := make([]byte, 8) + _, err = ds.Read(buf) + if err != nil { + break + } + out = binary.LittleEndian.Uint64(buf) + case int: + buf := make([]byte, 8) + _, err = ds.Read(buf) + if err != nil { + break + } + out = int(binary.LittleEndian.Uint64(buf)) + case uint: + buf := make([]byte, 8) + _, err = ds.Read(buf) + if err != nil { + break + } + out = uint(binary.LittleEndian.Uint64(buf)) + } + return +} diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go new file mode 100644 index 0000000000..3c98986dbe --- /dev/null +++ b/pkg/scale/decode_test.go @@ -0,0 +1,565 @@ +package scale + +import ( + "reflect" + "testing" +) + +func Test_decodeState_unmarshal(t *testing.T) { + var ( + i int + ui uint + i8 int8 + ui8 uint8 + i16 int16 + ui16 uint16 + i32 int32 + ui32 uint32 + i64 int64 + ui64 uint64 + ) + + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + // int8 + { + args: args{ + data: []byte{0x00}, + dst: &i8, + }, + want: int8(0), + }, + { + args: args{ + data: []byte{0x01}, + dst: &i8, + }, + want: int8(1), + }, + { + args: args{ + data: []byte{0x2a}, + dst: &i8, + }, + want: int8(42), + }, + { + args: args{ + data: []byte{0x40}, + dst: &i8, + }, + want: int8(64), + }, + // uint8 + { + args: args{ + data: []byte{0x00}, + dst: &ui8, + }, + want: uint8(0), + }, + { + args: args{ + data: []byte{0x01}, + dst: &ui8, + }, + want: uint8(1), + }, + { + args: args{ + data: []byte{0x2a}, + dst: &ui8, + }, + want: uint8(42), + }, + { + args: args{ + data: []byte{0x40}, + dst: &ui8, + }, + want: uint8(64), + }, + { + args: args{ + data: []byte{0x45}, + dst: &ui8, + }, + want: uint8(69), + }, + // int + { + args: args{ + data: []byte{0x00}, + dst: &i, + }, + want: int(0), + }, + { + args: args{ + data: []byte{0x01}, + dst: &i, + }, + want: int(1), + }, + { + args: args{ + data: []byte{0x2a}, + dst: &i, + }, + want: int(42), + }, + { + args: args{ + data: []byte{0x40}, + dst: &i, + }, + want: int(64), + }, + { + args: args{ + data: []byte{0x45}, + dst: &i, + }, + want: int(69), + }, + // uint + { + args: args{ + data: []byte{0x00}, + dst: &ui, + }, + want: uint(0), + }, + { + args: args{ + data: []byte{0x01}, + dst: &ui, + }, + want: uint(1), + }, + { + args: args{ + data: []byte{0x2a}, + dst: &ui, + }, + want: uint(42), + }, + { + args: args{ + data: []byte{0x40}, + dst: &ui, + }, + want: uint(64), + }, + { + args: args{ + data: []byte{0x45}, + dst: &ui, + }, + want: uint(69), + }, + // int16 + { + args: args{ + data: []byte{0x00}, + dst: &i16, + }, + want: int16(0), + }, + { + args: args{ + data: []byte{0x01}, + dst: &i16, + }, + want: int16(1), + }, + { + args: args{ + data: []byte{0x2a}, + dst: &i16, + }, + want: int16(42), + }, + { + args: args{ + data: []byte{0x40}, + dst: &i16, + }, + want: int16(64), + }, + { + args: args{ + data: []byte{0x45}, + dst: &i16, + }, + want: int16(69), + }, + { + args: args{ + data: []byte{0xff, 0x3f}, + dst: &i16, + }, + want: int16(16383), + }, + { + args: args{ + data: []byte{0x00, 0x40}, + dst: &i16, + }, + want: int16(16384), + }, + // uint16 + { + args: args{ + data: []byte{0x00}, + dst: &ui16, + }, + want: uint16(0), + }, + { + args: args{ + data: []byte{0x01}, + dst: &ui16, + }, + want: uint16(1), + }, + { + args: args{ + data: []byte{0x2a}, + dst: &ui16, + }, + want: uint16(42), + }, + { + args: args{ + data: []byte{0x40}, + dst: &ui16, + }, + want: uint16(64), + }, + { + args: args{ + data: []byte{0x45}, + dst: &ui16, + }, + want: uint16(69), + }, + { + args: args{ + data: []byte{0xff, 0x3f}, + dst: &ui16, + }, + want: uint16(16383), + }, + { + args: args{ + data: []byte{0x00, 0x40}, + dst: &ui16, + }, + want: uint16(16384), + }, + // int32 + { + args: args{ + data: []byte{0x00}, + dst: &i32, + }, + want: int32(0), + }, + { + args: args{ + data: []byte{0x01}, + dst: &i32, + }, + want: int32(1), + }, + { + args: args{ + data: []byte{0x2a}, + dst: &i32, + }, + want: int32(42), + }, + { + args: args{ + data: []byte{0x40}, + dst: &i32, + }, + want: int32(64), + }, + { + args: args{ + data: []byte{0x45}, + dst: &i32, + }, + want: int32(69), + }, + { + args: args{ + data: []byte{0xff, 0x3f}, + dst: &i32, + }, + want: int32(16383), + }, + { + args: args{ + data: []byte{0x00, 0x40}, + dst: &i32, + }, + want: int32(16384), + }, + { + args: args{ + data: []byte{0xff, 0xff, 0xff, 0x3f}, + dst: &i32, + }, + want: int32(1073741823), + }, + { + args: args{ + data: []byte{0x00, 0x00, 0x00, 0x40}, + dst: &i32, + }, + want: int32(1073741824), + }, + // uint32 + { + args: args{ + data: []byte{0x00}, + dst: &ui32, + }, + want: uint32(0), + }, + { + args: args{ + data: []byte{0x01}, + dst: &ui32, + }, + want: uint32(1), + }, + { + args: args{ + data: []byte{0x2a}, + dst: &ui32, + }, + want: uint32(42), + }, + { + args: args{ + data: []byte{0x40}, + dst: &ui32, + }, + want: uint32(64), + }, + { + args: args{ + data: []byte{0x45}, + dst: &ui32, + }, + want: uint32(69), + }, + { + args: args{ + data: []byte{0xff, 0x3f}, + dst: &ui32, + }, + want: uint32(16383), + }, + { + args: args{ + data: []byte{0x00, 0x40}, + dst: &ui32, + }, + want: uint32(16384), + }, + { + args: args{ + data: []byte{0xff, 0xff, 0xff, 0x3f}, + dst: &ui32, + }, + want: uint32(1073741823), + }, + { + args: args{ + data: []byte{0x00, 0x00, 0x00, 0x40}, + dst: &ui32, + }, + want: uint32(1073741824), + }, + // int64 + { + args: args{ + data: []byte{0x00}, + dst: &i64, + }, + want: int64(0), + }, + { + args: args{ + data: []byte{0x01}, + dst: &i64, + }, + want: int64(1), + }, + { + args: args{ + data: []byte{0x2a}, + dst: &i64, + }, + want: int64(42), + }, + { + args: args{ + data: []byte{0x40}, + dst: &i64, + }, + want: int64(64), + }, + { + args: args{ + data: []byte{0x45}, + dst: &i64, + }, + want: int64(69), + }, + { + args: args{ + data: []byte{0xff, 0x3f}, + dst: &i64, + }, + want: int64(16383), + }, + { + args: args{ + data: []byte{0x00, 0x40}, + dst: &i64, + }, + want: int64(16384), + }, + { + args: args{ + data: []byte{0xff, 0xff, 0xff, 0x3f}, + dst: &i64, + }, + want: int64(1073741823), + }, + { + args: args{ + data: []byte{0x00, 0x00, 0x00, 0x40}, + dst: &i64, + }, + want: int64(1073741824), + }, + { + args: args{ + data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, + dst: &i64, + }, + want: int64(274877906947), + }, + { + args: args{ + data: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, + dst: &i64, + }, + want: int64(-1), + }, + // uint64 + { + args: args{ + data: []byte{0x00}, + dst: &ui64, + }, + want: uint64(0), + }, + { + args: args{ + data: []byte{0x01}, + dst: &ui64, + }, + want: uint64(1), + }, + { + args: args{ + data: []byte{0x2a}, + dst: &ui64, + }, + want: uint64(42), + }, + { + args: args{ + data: []byte{0x40}, + dst: &ui64, + }, + want: uint64(64), + }, + { + args: args{ + data: []byte{0x45}, + dst: &ui64, + }, + want: uint64(69), + }, + { + args: args{ + data: []byte{0xff, 0x3f}, + dst: &ui64, + }, + want: uint64(16383), + }, + { + args: args{ + data: []byte{0x00, 0x40}, + dst: &ui64, + }, + want: uint64(16384), + }, + { + args: args{ + data: []byte{0xff, 0xff, 0xff, 0x3f}, + dst: &ui64, + }, + want: uint64(1073741823), + }, + { + args: args{ + data: []byte{0x00, 0x00, 0x00, 0x40}, + dst: &ui64, + }, + want: uint64(1073741824), + }, + { + args: args{ + data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, + dst: &ui64, + }, + want: uint64(274877906947), + }, + { + args: args{ + data: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, + dst: &ui64, + }, + want: uint64(1<<64 - 1), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := Unmarshal(tt.args.data, tt.args.dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From 8787be5c4431e4a5faec5c6a4421ee07a51333b0 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 6 May 2021 11:29:34 -0400 Subject: [PATCH 006/245] wip --- pkg/scale/decode.go | 150 ++++++++++++++++++++++++++++++++++++++- pkg/scale/decode_test.go | 122 ++++++++++++++++++++++++++++++- pkg/scale/encode.go | 4 +- 3 files changed, 271 insertions(+), 5 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 6bd2e01069..6e1d33ed3f 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -3,7 +3,9 @@ package scale import ( "bytes" "encoding/binary" + "errors" "fmt" + "math/big" "reflect" ) @@ -27,13 +29,21 @@ func (ds *decodeState) unmarshal(dst interface{}) (err error) { rv := reflect.ValueOf(dst) if rv.Kind() != reflect.Ptr || rv.IsNil() { err = fmt.Errorf("unsupported dst: %T", dst) + return } in := rv.Elem().Interface() switch in.(type) { + case *big.Int: + in, err = ds.decodeBigInt() + case *Uint128: + in, err = ds.decodeUint128() case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: in, err = ds.decodeFixedWidthInt(in) + case []byte, string: + in, err = ds.decodeBytes() } + if err != nil { return } @@ -41,7 +51,133 @@ func (ds *decodeState) unmarshal(dst interface{}) (err error) { return } -// DdcodeFixedWidthInt decodes integers < 2**32 by reading the bytes in little endian +// DecodeUnsignedInteger will decode unsigned integer +func (ds *decodeState) decodeUint() (o uint64, err error) { + b, err := ds.ReadByte() + if err != nil { + return 0, err + } + + // check mode of encoding, stored at 2 least significant bits + mode := b & 3 + if mode <= 2 { + val, e := ds.decodeSmallInt(b, mode) + return uint64(val), e + } + + // >4 byte mode + topSixBits := b >> 2 + byteLen := uint(topSixBits) + 4 + + buf := make([]byte, byteLen) + _, err = ds.Read(buf) + if err != nil { + return 0, err + } + + if byteLen == 4 { + o = uint64(binary.LittleEndian.Uint32(buf)) + } else if byteLen > 4 && byteLen < 8 { + tmp := make([]byte, 8) + copy(tmp, buf) + o = binary.LittleEndian.Uint64(tmp) + } else { + err = errors.New("could not decode invalid integer") + } + + return o, err +} + +// DecodeInteger accepts a byte array representing a SCALE encoded integer and performs SCALE decoding of the int +// if the encoding is valid, it then returns (o, bytesDecoded, err) where o is the decoded integer, bytesDecoded is the +// number of input bytes decoded, and err is nil +// otherwise, it returns 0, 0, and error +func (ds *decodeState) decodeInt() (_ int64, err error) { + o, err := ds.decodeUint() + + return int64(o), err +} + +// DecodeByteArray accepts a byte array representing a SCALE encoded byte array and performs SCALE decoding +// of the byte array +// if the encoding is valid, it then returns the decoded byte array, the total number of input bytes decoded, and nil +// otherwise, it returns nil, 0, and error +func (ds *decodeState) decodeBytes() (o []byte, err error) { + length, err := ds.decodeInt() + if err != nil { + return nil, err + } + + b := make([]byte, length) + _, err = ds.Read(b) + if err != nil { + return nil, errors.New("could not decode invalid byte array: reached early EOF") + } + + return b, nil +} + +// decodeSmallInt is used in the DecodeInteger and decodeBigInt functions when the mode is <= 2 +// need to pass in the first byte, since we assume it's already been read +func (ds *decodeState) decodeSmallInt(firstByte, mode byte) (out int64, err error) { + switch mode { + case 0: + out = int64(firstByte >> 2) + case 1: + var buf byte + buf, err = ds.ReadByte() + if err != nil { + break + } + out = int64(binary.LittleEndian.Uint16([]byte{firstByte, buf}) >> 2) + case 2: + buf := make([]byte, 3) + _, err = ds.Read(buf) + if err != nil { + break + } + out = int64(binary.LittleEndian.Uint32(append([]byte{firstByte}, buf...)) >> 2) + } + return +} + +// decodeBigInt decodes a SCALE encoded byte array into a *big.Int +// Works for all integers, including ints > 2**64 +func (ds *decodeState) decodeBigInt() (output *big.Int, err error) { + b, err := ds.ReadByte() + if err != nil { + return + } + + // check mode of encoding, stored at 2 least significant bits + mode := b & 0x03 + switch { + case mode <= 2: + var tmp int64 + tmp, err = ds.decodeSmallInt(b, mode) + if err != nil { + break + } + output = big.NewInt(tmp) + + default: + // >4 byte mode + topSixBits := b >> 2 + byteLen := uint(topSixBits) + 4 + + buf := make([]byte, byteLen) + _, err = ds.Read(buf) + if err == nil { + o := reverseBytes(buf) + output = big.NewInt(0).SetBytes(o) + } else { + err = errors.New("could not decode invalid big.Int: reached early EOF") + } + } + return +} + +// decodeFixedWidthInt decodes integers < 2**32 by reading the bytes in little endian func (ds *decodeState) decodeFixedWidthInt(in interface{}) (out interface{}, err error) { switch in.(type) { case int8: @@ -117,3 +253,15 @@ func (ds *decodeState) decodeFixedWidthInt(in interface{}) (out interface{}, err } return } + +// decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 +// if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise +// it returns nil and error +func (ds *decodeState) decodeUint128() (out *Uint128, err error) { + buf := make([]byte, 16) + err = binary.Read(ds, binary.LittleEndian, buf) + if err != nil { + return nil, err + } + return NewUint128(buf) +} diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 3c98986dbe..e9df31e987 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -1,11 +1,12 @@ package scale import ( + "math/big" "reflect" "testing" ) -func Test_decodeState_unmarshal(t *testing.T) { +func Test_decodeState_decodeFixedWidthInt(t *testing.T) { var ( i int ui uint @@ -18,7 +19,6 @@ func Test_decodeState_unmarshal(t *testing.T) { i64 int64 ui64 uint64 ) - type args struct { data []byte dst interface{} @@ -563,3 +563,121 @@ func Test_decodeState_unmarshal(t *testing.T) { }) } } + +func Test_decodeState_decodeBigInt(t *testing.T) { + var ( + bi *big.Int + ) + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + name: "error case, ensure **big.Int", + args: args{ + data: []byte{0x00}, + dst: bi, + }, + wantErr: true, + }, + { + args: args{ + data: []byte{0x00}, + dst: &bi, + }, + want: big.NewInt(0), + }, + { + args: args{ + data: []byte{0x04}, + dst: &bi, + }, + want: big.NewInt(1), + }, + { + args: args{ + data: []byte{0xa8}, + dst: &bi, + }, + want: big.NewInt(42), + }, + { + args: args{ + data: []byte{0x01, 0x01}, + dst: &bi, + }, + want: big.NewInt(64), + }, + { + args: args{ + data: []byte{0x15, 0x01}, + dst: &bi, + }, + want: big.NewInt(69), + }, + { + args: args{ + data: []byte{0xfd, 0xff}, + dst: &bi, + }, + want: big.NewInt(16383), + }, + { + args: args{ + data: []byte{0x02, 0x00, 0x01, 0x00}, + dst: &bi, + }, + want: big.NewInt(16384), + }, + { + args: args{ + data: []byte{0xfe, 0xff, 0xff, 0xff}, + dst: &bi, + }, + want: big.NewInt(1073741823), + }, + { + args: args{ + data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, + dst: &bi, + }, + want: big.NewInt(1073741824), + }, + { + args: args{ + data: []byte{0x03, 0xff, 0xff, 0xff, 0xff}, + dst: &bi, + }, + want: big.NewInt(1<<32 - 1), + }, + { + args: args{ + data: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, + dst: &bi, + }, + want: big.NewInt(1 << 32), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 1ba1a0af11..90477ea71f 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -31,7 +31,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeFixedWidthInt(in) case *big.Int: err = es.encodeBigInt(in) - case Uint128: + case *Uint128: err = es.encodeUint128(in) case []byte: err = es.encodeBytes(in) @@ -351,7 +351,7 @@ func (es *encodeState) encodeUint(i uint) (err error) { } // encodeUint128 encodes a Uint128 -func (es *encodeState) encodeUint128(i Uint128) (err error) { +func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) return } From 751f02099a2fa230ab39980dbc5794e47cd11dc0 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 14 May 2021 12:14:16 -0400 Subject: [PATCH 007/245] []byte, string decoding --- pkg/scale/decode.go | 27 +++++---- pkg/scale/decode_test.go | 117 +++++++++++++++++++++++++++++++++++++++ pkg/scale/encode_test.go | 15 ++--- 3 files changed, 140 insertions(+), 19 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 6e1d33ed3f..2e69dfa0a9 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -40,8 +40,12 @@ func (ds *decodeState) unmarshal(dst interface{}) (err error) { in, err = ds.decodeUint128() case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: in, err = ds.decodeFixedWidthInt(in) - case []byte, string: + case []byte: in, err = ds.decodeBytes() + case string: + var b []byte + b, err = ds.decodeBytes() + in = string(b) } if err != nil { @@ -88,33 +92,32 @@ func (ds *decodeState) decodeUint() (o uint64, err error) { return o, err } -// DecodeInteger accepts a byte array representing a SCALE encoded integer and performs SCALE decoding of the int +// decodeLength accepts a byte array representing a SCALE encoded integer and performs SCALE decoding of the int // if the encoding is valid, it then returns (o, bytesDecoded, err) where o is the decoded integer, bytesDecoded is the // number of input bytes decoded, and err is nil // otherwise, it returns 0, 0, and error -func (ds *decodeState) decodeInt() (_ int64, err error) { - o, err := ds.decodeUint() - - return int64(o), err +func (ds *decodeState) decodeLength() (l int, err error) { + ui, err := ds.decodeUint() + l = int(ui) + return } // DecodeByteArray accepts a byte array representing a SCALE encoded byte array and performs SCALE decoding // of the byte array // if the encoding is valid, it then returns the decoded byte array, the total number of input bytes decoded, and nil // otherwise, it returns nil, 0, and error -func (ds *decodeState) decodeBytes() (o []byte, err error) { - length, err := ds.decodeInt() +func (ds *decodeState) decodeBytes() (b []byte, err error) { + length, err := ds.decodeLength() if err != nil { return nil, err } - b := make([]byte, length) + b = make([]byte, length) _, err = ds.Read(b) if err != nil { return nil, errors.New("could not decode invalid byte array: reached early EOF") } - - return b, nil + return } // decodeSmallInt is used in the DecodeInteger and decodeBigInt functions when the mode is <= 2 @@ -257,7 +260,7 @@ func (ds *decodeState) decodeFixedWidthInt(in interface{}) (out interface{}, err // decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 // if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise // it returns nil and error -func (ds *decodeState) decodeUint128() (out *Uint128, err error) { +func (ds *decodeState) decodeUint128() (ui *Uint128, err error) { buf := make([]byte, 16) err = binary.Read(ds, binary.LittleEndian, buf) if err != nil { diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index e9df31e987..aa2ea12f4e 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -681,3 +681,120 @@ func Test_decodeState_decodeBigInt(t *testing.T) { }) } } + +func Test_decodeState_decodeBytes(t *testing.T) { + var b []byte + var s string + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + args: args{ + data: []byte{0x04, 0x01}, + dst: &b, + }, + want: []byte{0x01}, + }, + { + args: args{ + data: []byte{0x04, 0xff}, + dst: &b, + }, + want: []byte{0xff}, + }, + { + args: args{ + data: []byte{0x08, 0x01, 0x01}, + dst: &b, + }, + want: []byte{0x01, 0x01}, + }, + { + args: args{ + data: append([]byte{0x01, 0x01}, byteArray(64)...), + dst: &b, + }, + want: byteArray(64), + }, + { + args: args{ + data: append([]byte{0xfd, 0xff}, byteArray(16383)...), + dst: &b, + }, + want: byteArray(16383), + }, + { + args: args{ + data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), + dst: &b, + }, + want: byteArray(16384), + }, + // string + { + args: args{ + data: []byte{0x04, 0x01}, + dst: &s, + }, + want: string([]byte{0x01}), + }, + { + args: args{ + data: []byte{0x04, 0xff}, + dst: &s, + }, + want: string([]byte{0xff}), + }, + { + args: args{ + data: []byte{0x08, 0x01, 0x01}, + dst: &s, + }, + want: string([]byte{0x01, 0x01}), + }, + { + args: args{ + data: append([]byte{0x01, 0x01}, byteArray(64)...), + dst: &s, + }, + want: string(byteArray(64)), + }, + { + args: args{ + data: append([]byte{0xfd, 0xff}, byteArray(16383)...), + dst: &s, + }, + want: string(byteArray(16383)), + }, + { + args: args{ + data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), + dst: &s, + }, + want: string(byteArray(16384)), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 5791436b47..41ac1fd855 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -327,13 +327,6 @@ func Test_encodeState_encodeBigInteger(t *testing.T) { } func Test_encodeState_encodeBytes(t *testing.T) { - var byteArray = func(length int) []byte { - b := make([]byte, length) - for i := 0; i < length; i++ { - b[i] = 0xff - } - return b - } testString1 := "We love you! We believe in open source as wonderful form of giving." // n = 67 testString2 := strings.Repeat("We need a longer string to test with. Let's multiple this several times.", 230) // n = 72 * 230 = 16560 testString3 := "Let's test some special ASCII characters: ~ € · © ÿ" // n = 55 (UTF-8 encoding versus n = 51 with ASCII encoding) @@ -886,3 +879,11 @@ func Test_encodeState_encodeArray(t *testing.T) { }) } } + +var byteArray = func(length int) []byte { + b := make([]byte, length) + for i := 0; i < length; i++ { + b[i] = 0xff + } + return b +} From 806b87146678c2e10217a47c7c390945185803f5 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 14 May 2021 13:32:09 -0400 Subject: [PATCH 008/245] encodeBool and tests --- pkg/scale/decode.go | 29 +++++++++++++++++++--- pkg/scale/decode_test.go | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 2e69dfa0a9..a8868fb9fd 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -26,13 +26,14 @@ type decodeState struct { } func (ds *decodeState) unmarshal(dst interface{}) (err error) { - rv := reflect.ValueOf(dst) - if rv.Kind() != reflect.Ptr || rv.IsNil() { + dstv := reflect.ValueOf(dst) + if dstv.Kind() != reflect.Ptr || dstv.IsNil() { err = fmt.Errorf("unsupported dst: %T", dst) return } - in := rv.Elem().Interface() + // this needs to be used to handle zero value + in := dstv.Elem().Interface() switch in.(type) { case *big.Int: in, err = ds.decodeBigInt() @@ -46,12 +47,32 @@ func (ds *decodeState) unmarshal(dst interface{}) (err error) { var b []byte b, err = ds.decodeBytes() in = string(b) + case bool: + in, err = ds.decodeBool() } if err != nil { return } - rv.Elem().Set(reflect.ValueOf(in)) + dstv.Elem().Set(reflect.ValueOf(in)) + return +} + +// decodeBool accepts a byte array representing a SCALE encoded bool and performs SCALE decoding +// of the bool then returns it. if invalid, return false and an error +func (ds *decodeState) decodeBool() (b bool, err error) { + rb, err := ds.ReadByte() + if err != nil { + return + } + + switch rb { + case 0x00: + case 0x01: + b = true + default: + err = fmt.Errorf("could not decode invalid bool") + } return } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index aa2ea12f4e..f71ef9ee63 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -798,3 +798,56 @@ func Test_decodeState_decodeBytes(t *testing.T) { }) } } + +func Test_decodeState_decodeBool(t *testing.T) { + var b bool + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + args: args{ + data: []byte{0x01}, + dst: &b, + }, + want: true, + }, + { + args: args{ + data: []byte{0x00}, + dst: &b, + }, + want: false, + }, + { + name: "error case", + args: args{ + data: []byte{0x03}, + dst: &b, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From f13b8a10e54ea543086c383e420322ad10ba4792 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 19 May 2021 22:24:58 -0400 Subject: [PATCH 009/245] refactor tests, include optionality tests --- pkg/scale/decode.go | 30 ++ pkg/scale/encode_test.go | 858 ++++++++++++++++----------------------- 2 files changed, 385 insertions(+), 503 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index a8868fb9fd..27a4acf0e2 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -49,6 +49,36 @@ func (ds *decodeState) unmarshal(dst interface{}) (err error) { in = string(b) case bool: in, err = ds.decodeBool() + // case VaryingDataType: + // err = es.encodeVaryingDataType(in) + // // TODO: case common.Hash: + // // n, err = es.Writer.Write(v.ToBytes()) + default: + switch reflect.TypeOf(in).Kind() { + case reflect.Ptr: + var rb byte + rb, err = ds.ReadByte() + if err != nil { + break + } + switch rb { + case 0x00: + in = nil + case 0x01: + elem := reflect.ValueOf(in).Elem() + err = ds.unmarshal(elem) + default: + err = fmt.Errorf("unsupported Option value: %v", rb) + } + // case reflect.Struct: + // err = es.encodeStruct(in) + // case reflect.Array: + // err = es.encodeArray(in) + // case reflect.Slice: + // err = es.encodeSlice(in) + default: + err = fmt.Errorf("unsupported type: %T", in) + } } if err != nil { diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 41ac1fd855..1775389f39 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -1,600 +1,424 @@ package scale import ( - "bytes" "math/big" "reflect" "strings" "testing" ) -func Test_encodeState_encodeFixedWidthInteger(t *testing.T) { - type args struct { - i interface{} +type test struct { + name string + in interface{} + wantErr bool + want []byte +} +type tests []test + +func newTests(ts ...tests) (appended tests) { + for _, t := range ts { + appended = append(appended, t...) } - tests := []struct { - name string - args args - wantErr bool - want []byte - }{ + return +} + +var ( + intTests = tests{ { name: "int(1)", - args: args{ - i: int(1), - }, + in: int(1), want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0}, }, { name: "int(16383)", - args: args{ - i: int(16383), - }, + in: int(16383), want: []byte{0xff, 0x3f, 0, 0, 0, 0, 0, 0}, }, { name: "int(1073741823)", - args: args{ - i: int(1073741823), - }, + in: int(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0}, }, { name: "int(9223372036854775807)", - args: args{ - i: int(9223372036854775807), - }, + in: int(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + uintTests = tests{ { name: "uint(1)", - args: args{ - i: int(1), - }, + in: int(1), want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0}, }, { name: "uint(16383)", - args: args{ - i: uint(16383), - }, + in: uint(16383), want: []byte{0xff, 0x3f, 0, 0, 0, 0, 0, 0}, }, { name: "uint(1073741823)", - args: args{ - i: uint(1073741823), - }, + in: uint(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0}, }, { name: "uint(9223372036854775807)", - args: args{ - i: uint(9223372036854775807), - }, + in: uint(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + int64Tests = tests{ { name: "int64(1)", - args: args{ - i: int64(1), - }, + in: int64(1), want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0}, }, { name: "int64(16383)", - args: args{ - i: int64(16383), - }, + in: int64(16383), want: []byte{0xff, 0x3f, 0, 0, 0, 0, 0, 0}, }, { name: "int64(1073741823)", - args: args{ - i: int64(1073741823), - }, + in: int64(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0}, }, { name: "int64(9223372036854775807)", - args: args{ - i: int64(9223372036854775807), - }, + in: int64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + uint64Tests = tests{ { name: "uint64(1)", - args: args{ - i: uint64(1), - }, + in: uint64(1), want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0}, }, { name: "uint64(16383)", - args: args{ - i: uint64(16383), - }, + in: uint64(16383), want: []byte{0xff, 0x3f, 0, 0, 0, 0, 0, 0}, }, { name: "uint64(1073741823)", - args: args{ - i: uint64(1073741823), - }, + in: uint64(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0}, }, { name: "uint64(9223372036854775807)", - args: args{ - i: uint64(9223372036854775807), - }, + in: uint64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + int32Tests = tests{ { name: "int32(1)", - args: args{ - i: int32(1), - }, + in: int32(1), want: []byte{0x01, 0, 0, 0}, }, { name: "int32(16383)", - args: args{ - i: int32(16383), - }, + in: int32(16383), want: []byte{0xff, 0x3f, 0, 0}, }, { name: "int32(1073741823)", - args: args{ - i: int32(1073741823), - }, + in: int32(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f}, }, + } + uint32Tests = tests{ { name: "uint32(1)", - args: args{ - i: uint32(1), - }, + in: uint32(1), want: []byte{0x01, 0, 0, 0}, }, { name: "uint32(16383)", - args: args{ - i: uint32(16383), - }, + in: uint32(16383), want: []byte{0xff, 0x3f, 0, 0}, }, { name: "uint32(1073741823)", - args: args{ - i: uint32(1073741823), - }, + in: uint32(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f}, }, + } + int8Tests = tests{ { name: "int8(1)", - args: args{ - i: int8(1), - }, + in: int8(1), want: []byte{0x01}, }, + } + uint8Tests = tests{ { name: "uint8(1)", - args: args{ - i: uint8(1), - }, + in: uint8(1), want: []byte{0x01}, }, + } + int16Tests = tests{ { name: "int16(1)", - args: args{ - i: int16(1), - }, + in: int16(1), want: []byte{0x01, 0}, }, { name: "int16(16383)", - args: args{ - i: int16(16383), - }, + in: int16(16383), want: []byte{0xff, 0x3f}, }, + } + uint16Tests = tests{ { name: "uint16(1)", - args: args{ - i: uint16(1), - }, + in: uint16(1), want: []byte{0x01, 0}, }, { name: "uint16(16383)", - args: args{ - i: uint16(16383), - }, + in: uint16(16383), want: []byte{0xff, 0x3f}, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - es := &encodeState{} - if err := es.marshal(tt.args.i); (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeFixedWidthInt() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { - t.Errorf("encodeState.encodeFixedWidthInt() = %v, want %v", es.Buffer.Bytes(), tt.want) - } - }) - } -} + fixedWidthIntegerTests = newTests( + int8Tests, int16Tests, int32Tests, int64Tests, intTests, uint8Tests, uint16Tests, uint32Tests, uint64Tests, uintTests, + ) -func Test_encodeState_encodeBigInteger(t *testing.T) { - type args struct { - i *big.Int - } - tests := []struct { - name string - args args - wantErr bool - want []byte - }{ + zeroValBigInt *big.Int + bigIntTests = tests{ { name: "error nil pointer", + in: zeroValBigInt, wantErr: true, }, { name: "big.NewInt(0)", - args: args{ - i: big.NewInt(0), - }, + in: big.NewInt(0), want: []byte{0x00}, }, { name: "big.NewInt(1)", - args: args{ - i: big.NewInt(1), - }, + in: big.NewInt(1), want: []byte{0x04}, }, { name: "big.NewInt(42)", - args: args{ - i: big.NewInt(42), - }, + in: big.NewInt(42), want: []byte{0xa8}, }, { name: "big.NewInt(69)", - args: args{ - i: big.NewInt(69), - }, + in: big.NewInt(69), want: []byte{0x15, 0x01}, }, { name: "big.NewInt(1000)", - args: args{ - i: big.NewInt(1000), - }, + in: big.NewInt(1000), want: []byte{0xa1, 0x0f}, }, { name: "big.NewInt(16383)", - args: args{ - i: big.NewInt(16383), - }, + in: big.NewInt(16383), want: []byte{0xfd, 0xff}, }, { name: "big.NewInt(16384)", - args: args{ - i: big.NewInt(16384), - }, + in: big.NewInt(16384), want: []byte{0x02, 0x00, 0x01, 0x00}, }, { name: "big.NewInt(1073741823)", - args: args{ - i: big.NewInt(1073741823), - }, + in: big.NewInt(1073741823), want: []byte{0xfe, 0xff, 0xff, 0xff}, }, { name: "big.NewInt(1073741824)", - args: args{ - i: big.NewInt(1073741824), - }, + in: big.NewInt(1073741824), want: []byte{3, 0, 0, 0, 64}, }, { name: "big.NewInt(1<<32 - 1)", - args: args{ - i: big.NewInt(1<<32 - 1), - }, + in: big.NewInt(1<<32 - 1), want: []byte{0x03, 0xff, 0xff, 0xff, 0xff}, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - es := &encodeState{} - if err := es.marshal(tt.args.i); (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeBigInt() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { - t.Errorf("encodeState.encodeBigInt() = %v, want %v", es.Buffer.Bytes(), tt.want) - } - }) - } -} - -func Test_encodeState_encodeBytes(t *testing.T) { - testString1 := "We love you! We believe in open source as wonderful form of giving." // n = 67 - testString2 := strings.Repeat("We need a longer string to test with. Let's multiple this several times.", 230) // n = 72 * 230 = 16560 - testString3 := "Let's test some special ASCII characters: ~ € · © ÿ" // n = 55 (UTF-8 encoding versus n = 51 with ASCII encoding) - type args struct { - b interface{} + testStrings = []string{ + "We love you! We believe in open source as wonderful form of giving.", // n = 67 + strings.Repeat("We need a longer string to test with. Let's multiple this several times.", 230), // n = 72 * 230 = 16560 + "Let's test some special ASCII characters: ~ € · © ÿ", // n = 55 (UTF-8 encoding versus n = 51 with ASCII encoding) } - tests := []struct { - name string - args args - wantErr bool - want []byte - }{ + stringTests = tests{ { name: "[]byte{0x01}", - args: args{ - b: []byte{0x01}, - }, + in: []byte{0x01}, + want: []byte{0x04, 0x01}, }, { name: "[]byte{0xff}", - args: args{ - b: []byte{0xff}, - }, + in: []byte{0xff}, + want: []byte{0x04, 0xff}, }, { name: "[]byte{0x01, 0x01}", - args: args{ - b: []byte{0x01, 0x01}, - }, + in: []byte{0x01, 0x01}, + want: []byte{0x08, 0x01, 0x01}, }, { name: "byteArray(32)", - args: args{ - b: byteArray(32), - }, + in: byteArray(32), + want: append([]byte{0x80}, byteArray(32)...), }, { name: "byteArray(64)", - args: args{ - b: byteArray(64), - }, + in: byteArray(64), + want: append([]byte{0x01, 0x01}, byteArray(64)...), }, { name: "byteArray(16384)", - args: args{ - b: byteArray(16384), - }, + in: byteArray(16384), + want: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), }, { name: "\"a\"", - args: args{ - b: []byte("a"), - }, + in: []byte("a"), + want: []byte{0x04, 'a'}, }, { name: "\"go-pre\"", - args: args{ - b: []byte("go-pre"), - }, + in: []byte("go-pre"), + want: append([]byte{0x18}, string("go-pre")...), }, { - name: "testString1", - args: args{ - b: testString1, - }, - want: append([]byte{0x0D, 0x01}, testString1...), + name: "testStrings[0]", + in: testStrings[0], + + want: append([]byte{0x0D, 0x01}, testStrings[0]...), }, { - name: "testString2, long string", - args: args{ - b: testString2, - }, - want: append([]byte{0xC2, 0x02, 0x01, 0x00}, testString2...), + name: "testString[1], long string", + in: testStrings[1], + + want: append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), }, { - name: "testString3, special chars", - args: args{ - b: testString3, - }, - want: append([]byte{0xDC}, testString3...), + name: "testString[2], special chars", + in: testStrings[2], + + want: append([]byte{0xDC}, testStrings[2]...), }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - es := &encodeState{} - if err := es.marshal(tt.args.b); (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeBytes() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { - t.Errorf("encodeState.encodeBytes() = %v, want %v", es.Buffer.Bytes(), tt.want) - } - }) - } -} -func Test_encodeState_encodeBool(t *testing.T) { - type fields struct { - Buffer bytes.Buffer - } - type args struct { - b bool - } - tests := []struct { - name string - fields fields - args args - wantErr bool - want []byte - }{ + boolTests = tests{ { name: "false", - args: args{ - b: false, - }, + in: false, want: []byte{0x00}, }, { name: "true", - args: args{ - b: true, - }, + in: true, want: []byte{0x01}, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - es := &encodeState{ - Buffer: tt.fields.Buffer, - } - if err := es.marshal(tt.args.b); (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeBool() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { - t.Errorf("encodeState.encodeBool() = %v, want %v", es.Buffer.Bytes(), tt.want) - } - }) - } -} -func Test_encodeState_encodeStruct(t *testing.T) { - type myStruct struct { - Foo []byte - Bar int32 - Baz bool - } - var nilPtrMyStruct *myStruct - // pointer to - var ptrMystruct *myStruct = &myStruct{ + nilPtrMyStruct *myStruct + ptrMystruct *myStruct = &myStruct{ Foo: []byte{0x01}, Bar: 2, Baz: true, } - var nilPtrMyStruct2 *myStruct = &myStruct{ - Foo: []byte{0x01}, - Bar: 2, - Baz: true, - } - nilPtrMyStruct2 = nil - - type args struct { - t interface{} - } - tests := []struct { - name string - args args - wantErr bool - want []byte - }{ + nilPtrMyStruct2 *myStruct = nil + // nilPtrMyStruct2 = nil + structTests = tests{ { name: "nilPtrMyStruct", - args: args{ - nilPtrMyStruct, - }, + in: nilPtrMyStruct, want: []byte{0}, }, { name: "ptrMystruct", - args: args{ - ptrMystruct, - }, + in: ptrMystruct, want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, }, { name: "ptrMystruct cache hit", - args: args{ - ptrMystruct, - }, + in: ptrMystruct, + want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, }, { name: "nilPtrMyStruct2", - args: args{ - nilPtrMyStruct2, - }, + in: nilPtrMyStruct2, + want: []byte{0}, }, { name: "&struct {[]byte, int32}", - args: args{ - t: &struct { - Foo []byte - Bar int32 - Baz bool - }{ - Foo: []byte{0x01}, - Bar: 2, - Baz: true, - }, + in: &struct { + Foo []byte + Bar int32 + Baz bool + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, }, want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, }, { name: "struct {[]byte, int32}", - args: args{ - t: struct { - Foo []byte - Bar int32 - Baz bool - }{ - Foo: []byte{0x01}, - Bar: 2, - Baz: true, - }, + in: struct { + Foo []byte + Bar int32 + Baz bool + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, }, { name: "struct {[]byte, int32, bool}", - args: args{ - t: struct { - Baz bool `scale:"3,enum"` - Bar int32 `scale:"2"` - Foo []byte `scale:"1"` - }{ - Foo: []byte{0x01}, - Bar: 2, - Baz: true, - }, + in: struct { + Baz bool `scale:"3,enum"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, }, { name: "struct {[]byte, int32, bool} with untagged attributes", - args: args{ - t: struct { - Baz bool `scale:"3"` - Bar int32 `scale:"2"` - Foo []byte `scale:"1"` - End1 bool - End2 []byte - End3 []byte - }{ - Foo: []byte{0x01}, - Bar: 2, - Baz: true, - End1: false, - End2: []byte{0xff}, - End3: []byte{0x06}, - }, + in: struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + End1 bool + End2 []byte + End3 []byte + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + End1: false, + End2: []byte{0xff}, + End3: []byte{0x06}, }, want: []byte{ 0x04, 0x01, 0x02, 0, 0, 0, 0x01, @@ -608,24 +432,23 @@ func Test_encodeState_encodeStruct(t *testing.T) { }, { name: "struct {[]byte, int32, bool} with untagged attributes", - args: args{ - t: struct { - End1 bool - Baz bool `scale:"3"` - End2 []byte - Bar int32 `scale:"2"` - End3 []byte - Foo []byte `scale:"1"` - }{ - Foo: []byte{0x01}, - Bar: 2, - Baz: true, - End1: false, - End2: []byte{0xff}, - // End3: 0xff - End3: []byte{0x06}, - }, + in: struct { + End1 bool + Baz bool `scale:"3"` + End2 []byte + Bar int32 `scale:"2"` + End3 []byte + Foo []byte `scale:"1"` + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + End1: false, + End2: []byte{0xff}, + // End3: 0xff + End3: []byte{0x06}, }, + want: []byte{ 0x04, 0x01, 0x02, 0, 0, 0, 0x01, // End1: false @@ -638,240 +461,210 @@ func Test_encodeState_encodeStruct(t *testing.T) { }, { name: "struct {[]byte, int32, bool} with private attributes", - args: args{ - t: struct { - priv0 string - Baz bool `scale:"3"` - Bar int32 `scale:"2"` - Foo []byte `scale:"1"` - priv1 []byte - }{ - priv0: "stuff", - Foo: []byte{0x01}, - Bar: 2, - Baz: true, - }, + in: struct { + priv0 string + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + priv1 []byte + }{ + priv0: "stuff", + Foo: []byte{0x01}, + Bar: 2, + Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, }, { name: "struct {[]byte, int32, bool} with ignored attributes", - args: args{ - t: struct { - Baz bool `scale:"3"` - Bar int32 `scale:"2"` - Foo []byte `scale:"1"` - Ignore string `scale:"-"` - somethingElse *struct { - fields int - } - }{ - Foo: []byte{0x01}, - Bar: 2, - Baz: true, - Ignore: "me", - }, + in: struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + Ignore string `scale:"-"` + somethingElse *struct { + fields int + } + }{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + Ignore: "me", }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - es := &encodeState{fieldScaleIndicesCache: cache} - err := es.marshal(tt.args.t) - if (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeStruct() = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { - t.Errorf("encodeState.encodeStruct() = %v, want %v", es.Buffer.Bytes(), tt.want) - } - }) - } -} -func Test_encodeState_encodeSlice(t *testing.T) { - type fields struct { - Buffer bytes.Buffer - } - type args struct { - t interface{} - } - tests := []struct { - name string - fields fields - args args - wantErr bool - want []byte - }{ + sliceTests = tests{ { name: "[]int{1, 2, 3, 4}", - args: args{ - []int{1, 2, 3, 4}, - }, + in: []int{1, 2, 3, 4}, want: []byte{0x10, 0x04, 0x08, 0x0c, 0x10}, }, { name: "[]int{16384, 2, 3, 4}", - args: args{ - []int{16384, 2, 3, 4}, - }, + in: []int{16384, 2, 3, 4}, want: []byte{0x10, 0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, }, { name: "[]int{1073741824, 2, 3, 4}", - args: args{ - []int{1073741824, 2, 3, 4}, - }, + in: []int{1073741824, 2, 3, 4}, want: []byte{0x10, 0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, }, { name: "[]int{1 << 32, 2, 3, 1 << 32}", - args: args{ - []int{1 << 32, 2, 3, 1 << 32}, - }, + in: []int{1 << 32, 2, 3, 1 << 32}, want: []byte{0x10, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, }, { name: "[]bool{true, false, true}", - args: args{ - []bool{true, false, true}, - }, + in: []bool{true, false, true}, want: []byte{0x0c, 0x01, 0x00, 0x01}, }, { name: "[][]int{{0, 1}, {1, 0}}", - args: args{ - [][]int{{0, 1}, {1, 0}}, - }, + in: [][]int{{0, 1}, {1, 0}}, want: []byte{0x08, 0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, }, { name: "[]*big.Int{big.NewInt(0), big.NewInt(1)}", - args: args{ - []*big.Int{big.NewInt(0), big.NewInt(1)}, - }, + in: []*big.Int{big.NewInt(0), big.NewInt(1)}, want: []byte{0x08, 0x00, 0x04}, }, { name: "[][]byte{{0x00, 0x01}, {0x01, 0x00}}", - args: args{ - [][]byte{{0x00, 0x01}, {0x01, 0x00}}, - }, + in: [][]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x08, 0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - es := &encodeState{ - Buffer: tt.fields.Buffer, - } - err := es.marshal(tt.args.t) - if (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeStruct() = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { - t.Errorf("encodeState.encodeStruct() = %v, want %v", es.Buffer.Bytes(), tt.want) - } - }) - } -} -func Test_encodeState_encodeArray(t *testing.T) { - type fields struct { - Buffer bytes.Buffer - } - type args struct { - t interface{} - } - tests := []struct { - name string - fields fields - args args - wantErr bool - want []byte - }{ + arrayTests = tests{ { name: "[4]int{1, 2, 3, 4}", - args: args{ - [4]int{1, 2, 3, 4}, - }, + in: [4]int{1, 2, 3, 4}, want: []byte{0x04, 0x08, 0x0c, 0x10}, }, { name: "[4]int{16384, 2, 3, 4}", - args: args{ - [4]int{16384, 2, 3, 4}, - }, + in: [4]int{16384, 2, 3, 4}, want: []byte{0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, }, { name: "[4]int{1073741824, 2, 3, 4}", - args: args{ - [4]int{1073741824, 2, 3, 4}, - }, + in: [4]int{1073741824, 2, 3, 4}, want: []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, }, { name: "[4]int{1 << 32, 2, 3, 1 << 32}", - args: args{ - [4]int{1 << 32, 2, 3, 1 << 32}, - }, + in: [4]int{1 << 32, 2, 3, 1 << 32}, want: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, }, { name: "[3]bool{true, false, true}", - args: args{ - [3]bool{true, false, true}, - }, + in: [3]bool{true, false, true}, want: []byte{0x01, 0x00, 0x01}, }, { name: "[2][]int{{0, 1}, {1, 0}}", - args: args{ - [2][]int{{0, 1}, {1, 0}}, - }, + in: [2][]int{{0, 1}, {1, 0}}, want: []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, }, { name: "[2][2]int{{0, 1}, {1, 0}}", - args: args{ - [2][2]int{{0, 1}, {1, 0}}, - }, + in: [2][2]int{{0, 1}, {1, 0}}, want: []byte{0x00, 0x04, 0x04, 0x00}, }, { name: "[2]*big.Int{big.NewInt(0), big.NewInt(1)}", - args: args{ - [2]*big.Int{big.NewInt(0), big.NewInt(1)}, - }, + in: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, want: []byte{0x00, 0x04}, }, { name: "[2][]byte{{0x00, 0x01}, {0x01, 0x00}}", - args: args{ - [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, - }, + in: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, }, { name: "[2][2]byte{{0x00, 0x01}, {0x01, 0x00}}", - args: args{ - [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, - }, + in: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x00, 0x01, 0x01, 0x00}, }, } - for _, tt := range tests { + + allTests = newTests( + fixedWidthIntegerTests, bigIntTests, stringTests, + boolTests, structTests, sliceTests, arrayTests, + ) +) + +type myStruct struct { + Foo []byte + Bar int32 + Baz bool +} + +func Test_encodeState_encodeFixedWidthInteger(t *testing.T) { + for _, tt := range fixedWidthIntegerTests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeFixedWidthInt() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeFixedWidthInt() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_encodeState_encodeBigInt(t *testing.T) { + for _, tt := range bigIntTests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeBigInt() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeBigInt() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_encodeState_encodeBytes(t *testing.T) { + for _, tt := range stringTests { t.Run(tt.name, func(t *testing.T) { - es := &encodeState{ - Buffer: tt.fields.Buffer, + es := &encodeState{} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeBytes() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeBytes() = %v, want %v", es.Buffer.Bytes(), tt.want) } - err := es.marshal(tt.args.t) - if (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeStruct() = %v, wantErr %v", err, tt.wantErr) - return + }) + } +} + +func Test_encodeState_encodeBool(t *testing.T) { + for _, tt := range boolTests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeBool() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeBool() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_encodeState_encodeStruct(t *testing.T) { + for _, tt := range structTests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{fieldScaleIndicesCache: cache} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) } if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { t.Errorf("encodeState.encodeStruct() = %v, want %v", es.Buffer.Bytes(), tt.want) @@ -880,6 +673,65 @@ func Test_encodeState_encodeArray(t *testing.T) { } } +func Test_encodeState_encodeSlice(t *testing.T) { + for _, tt := range sliceTests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{fieldScaleIndicesCache: cache} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeSlice() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeSlice() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_encodeState_encodeArray(t *testing.T) { + for _, tt := range arrayTests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{fieldScaleIndicesCache: cache} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeArray() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeArray() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_marshal_optionality(t *testing.T) { + var ptrTests tests + for i := range allTests { + t := allTests[i] + ptrTest := test{ + name: t.name, + in: &t.in, + wantErr: t.wantErr, + want: t.want, + } + switch t.in { + case nil: + ptrTest.want = []byte{0x00} + default: + ptrTest.want = append([]byte{0x01}, t.want...) + } + ptrTests = append(ptrTests, ptrTest) + } + for _, tt := range ptrTests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{fieldScaleIndicesCache: cache} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeFixedWidthInt() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeFixedWidthInt() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + var byteArray = func(length int) []byte { b := make([]byte, length) for i := 0; i < length; i++ { From 094b90bc55bcfb074840971051401a930d9b19e7 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 21 May 2021 17:17:04 -0400 Subject: [PATCH 010/245] use shared tests in decode --- pkg/scale/decode_test.go | 884 ++++----------------------------------- 1 file changed, 73 insertions(+), 811 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index f71ef9ee63..99a04a306d 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -1,853 +1,115 @@ package scale import ( - "math/big" "reflect" "testing" ) func Test_decodeState_decodeFixedWidthInt(t *testing.T) { - var ( - i int - ui uint - i8 int8 - ui8 uint8 - i16 int16 - ui16 uint16 - i32 int32 - ui32 uint32 - i64 int64 - ui64 uint64 - ) - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - // int8 - { - args: args{ - data: []byte{0x00}, - dst: &i8, - }, - want: int8(0), - }, - { - args: args{ - data: []byte{0x01}, - dst: &i8, - }, - want: int8(1), - }, - { - args: args{ - data: []byte{0x2a}, - dst: &i8, - }, - want: int8(42), - }, - { - args: args{ - data: []byte{0x40}, - dst: &i8, - }, - want: int8(64), - }, - // uint8 - { - args: args{ - data: []byte{0x00}, - dst: &ui8, - }, - want: uint8(0), - }, - { - args: args{ - data: []byte{0x01}, - dst: &ui8, - }, - want: uint8(1), - }, - { - args: args{ - data: []byte{0x2a}, - dst: &ui8, - }, - want: uint8(42), - }, - { - args: args{ - data: []byte{0x40}, - dst: &ui8, - }, - want: uint8(64), - }, - { - args: args{ - data: []byte{0x45}, - dst: &ui8, - }, - want: uint8(69), - }, - // int - { - args: args{ - data: []byte{0x00}, - dst: &i, - }, - want: int(0), - }, - { - args: args{ - data: []byte{0x01}, - dst: &i, - }, - want: int(1), - }, - { - args: args{ - data: []byte{0x2a}, - dst: &i, - }, - want: int(42), - }, - { - args: args{ - data: []byte{0x40}, - dst: &i, - }, - want: int(64), - }, - { - args: args{ - data: []byte{0x45}, - dst: &i, - }, - want: int(69), - }, - // uint - { - args: args{ - data: []byte{0x00}, - dst: &ui, - }, - want: uint(0), - }, - { - args: args{ - data: []byte{0x01}, - dst: &ui, - }, - want: uint(1), - }, - { - args: args{ - data: []byte{0x2a}, - dst: &ui, - }, - want: uint(42), - }, - { - args: args{ - data: []byte{0x40}, - dst: &ui, - }, - want: uint(64), - }, - { - args: args{ - data: []byte{0x45}, - dst: &ui, - }, - want: uint(69), - }, - // int16 - { - args: args{ - data: []byte{0x00}, - dst: &i16, - }, - want: int16(0), - }, - { - args: args{ - data: []byte{0x01}, - dst: &i16, - }, - want: int16(1), - }, - { - args: args{ - data: []byte{0x2a}, - dst: &i16, - }, - want: int16(42), - }, - { - args: args{ - data: []byte{0x40}, - dst: &i16, - }, - want: int16(64), - }, - { - args: args{ - data: []byte{0x45}, - dst: &i16, - }, - want: int16(69), - }, - { - args: args{ - data: []byte{0xff, 0x3f}, - dst: &i16, - }, - want: int16(16383), - }, - { - args: args{ - data: []byte{0x00, 0x40}, - dst: &i16, - }, - want: int16(16384), - }, - // uint16 - { - args: args{ - data: []byte{0x00}, - dst: &ui16, - }, - want: uint16(0), - }, - { - args: args{ - data: []byte{0x01}, - dst: &ui16, - }, - want: uint16(1), - }, - { - args: args{ - data: []byte{0x2a}, - dst: &ui16, - }, - want: uint16(42), - }, - { - args: args{ - data: []byte{0x40}, - dst: &ui16, - }, - want: uint16(64), - }, - { - args: args{ - data: []byte{0x45}, - dst: &ui16, - }, - want: uint16(69), - }, - { - args: args{ - data: []byte{0xff, 0x3f}, - dst: &ui16, - }, - want: uint16(16383), - }, - { - args: args{ - data: []byte{0x00, 0x40}, - dst: &ui16, - }, - want: uint16(16384), - }, - // int32 - { - args: args{ - data: []byte{0x00}, - dst: &i32, - }, - want: int32(0), - }, - { - args: args{ - data: []byte{0x01}, - dst: &i32, - }, - want: int32(1), - }, - { - args: args{ - data: []byte{0x2a}, - dst: &i32, - }, - want: int32(42), - }, - { - args: args{ - data: []byte{0x40}, - dst: &i32, - }, - want: int32(64), - }, - { - args: args{ - data: []byte{0x45}, - dst: &i32, - }, - want: int32(69), - }, - { - args: args{ - data: []byte{0xff, 0x3f}, - dst: &i32, - }, - want: int32(16383), - }, - { - args: args{ - data: []byte{0x00, 0x40}, - dst: &i32, - }, - want: int32(16384), - }, - { - args: args{ - data: []byte{0xff, 0xff, 0xff, 0x3f}, - dst: &i32, - }, - want: int32(1073741823), - }, - { - args: args{ - data: []byte{0x00, 0x00, 0x00, 0x40}, - dst: &i32, - }, - want: int32(1073741824), - }, - // uint32 - { - args: args{ - data: []byte{0x00}, - dst: &ui32, - }, - want: uint32(0), - }, - { - args: args{ - data: []byte{0x01}, - dst: &ui32, - }, - want: uint32(1), - }, - { - args: args{ - data: []byte{0x2a}, - dst: &ui32, - }, - want: uint32(42), - }, - { - args: args{ - data: []byte{0x40}, - dst: &ui32, - }, - want: uint32(64), - }, - { - args: args{ - data: []byte{0x45}, - dst: &ui32, - }, - want: uint32(69), - }, - { - args: args{ - data: []byte{0xff, 0x3f}, - dst: &ui32, - }, - want: uint32(16383), - }, - { - args: args{ - data: []byte{0x00, 0x40}, - dst: &ui32, - }, - want: uint32(16384), - }, - { - args: args{ - data: []byte{0xff, 0xff, 0xff, 0x3f}, - dst: &ui32, - }, - want: uint32(1073741823), - }, - { - args: args{ - data: []byte{0x00, 0x00, 0x00, 0x40}, - dst: &ui32, - }, - want: uint32(1073741824), - }, - // int64 - { - args: args{ - data: []byte{0x00}, - dst: &i64, - }, - want: int64(0), - }, - { - args: args{ - data: []byte{0x01}, - dst: &i64, - }, - want: int64(1), - }, - { - args: args{ - data: []byte{0x2a}, - dst: &i64, - }, - want: int64(42), - }, - { - args: args{ - data: []byte{0x40}, - dst: &i64, - }, - want: int64(64), - }, - { - args: args{ - data: []byte{0x45}, - dst: &i64, - }, - want: int64(69), - }, - { - args: args{ - data: []byte{0xff, 0x3f}, - dst: &i64, - }, - want: int64(16383), - }, - { - args: args{ - data: []byte{0x00, 0x40}, - dst: &i64, - }, - want: int64(16384), - }, - { - args: args{ - data: []byte{0xff, 0xff, 0xff, 0x3f}, - dst: &i64, - }, - want: int64(1073741823), - }, - { - args: args{ - data: []byte{0x00, 0x00, 0x00, 0x40}, - dst: &i64, - }, - want: int64(1073741824), - }, - { - args: args{ - data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, - dst: &i64, - }, - want: int64(274877906947), - }, - { - args: args{ - data: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, - dst: &i64, - }, - want: int64(-1), - }, - // uint64 - { - args: args{ - data: []byte{0x00}, - dst: &ui64, - }, - want: uint64(0), - }, - { - args: args{ - data: []byte{0x01}, - dst: &ui64, - }, - want: uint64(1), - }, - { - args: args{ - data: []byte{0x2a}, - dst: &ui64, - }, - want: uint64(42), - }, - { - args: args{ - data: []byte{0x40}, - dst: &ui64, - }, - want: uint64(64), - }, - { - args: args{ - data: []byte{0x45}, - dst: &ui64, - }, - want: uint64(69), - }, - { - args: args{ - data: []byte{0xff, 0x3f}, - dst: &ui64, - }, - want: uint64(16383), - }, - { - args: args{ - data: []byte{0x00, 0x40}, - dst: &ui64, - }, - want: uint64(16384), - }, - { - args: args{ - data: []byte{0xff, 0xff, 0xff, 0x3f}, - dst: &ui64, - }, - want: uint64(1073741823), - }, - { - args: args{ - data: []byte{0x00, 0x00, 0x00, 0x40}, - dst: &ui64, - }, - want: uint64(1073741824), - }, - { - args: args{ - data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, - dst: &ui64, - }, - want: uint64(274877906947), - }, - { - args: args{ - data: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, - dst: &ui64, - }, - want: uint64(1<<64 - 1), - }, - } - for _, tt := range tests { + for _, tt := range fixedWidthIntegerTests { t.Run(tt.name, func(t *testing.T) { - if err := Unmarshal(tt.args.data, tt.args.dst); (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } func Test_decodeState_decodeBigInt(t *testing.T) { - var ( - bi *big.Int - ) - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - name: "error case, ensure **big.Int", - args: args{ - data: []byte{0x00}, - dst: bi, - }, - wantErr: true, - }, - { - args: args{ - data: []byte{0x00}, - dst: &bi, - }, - want: big.NewInt(0), - }, - { - args: args{ - data: []byte{0x04}, - dst: &bi, - }, - want: big.NewInt(1), - }, - { - args: args{ - data: []byte{0xa8}, - dst: &bi, - }, - want: big.NewInt(42), - }, - { - args: args{ - data: []byte{0x01, 0x01}, - dst: &bi, - }, - want: big.NewInt(64), - }, - { - args: args{ - data: []byte{0x15, 0x01}, - dst: &bi, - }, - want: big.NewInt(69), - }, - { - args: args{ - data: []byte{0xfd, 0xff}, - dst: &bi, - }, - want: big.NewInt(16383), - }, - { - args: args{ - data: []byte{0x02, 0x00, 0x01, 0x00}, - dst: &bi, - }, - want: big.NewInt(16384), - }, - { - args: args{ - data: []byte{0xfe, 0xff, 0xff, 0xff}, - dst: &bi, - }, - want: big.NewInt(1073741823), - }, - { - args: args{ - data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, - dst: &bi, - }, - want: big.NewInt(1073741824), - }, - { - args: args{ - data: []byte{0x03, 0xff, 0xff, 0xff, 0xff}, - dst: &bi, - }, - want: big.NewInt(1<<32 - 1), - }, - { - args: args{ - data: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, - dst: &bi, - }, - want: big.NewInt(1 << 32), - }, - } - for _, tt := range tests { + for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } func Test_decodeState_decodeBytes(t *testing.T) { - var b []byte - var s string - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - args: args{ - data: []byte{0x04, 0x01}, - dst: &b, - }, - want: []byte{0x01}, - }, - { - args: args{ - data: []byte{0x04, 0xff}, - dst: &b, - }, - want: []byte{0xff}, - }, - { - args: args{ - data: []byte{0x08, 0x01, 0x01}, - dst: &b, - }, - want: []byte{0x01, 0x01}, - }, - { - args: args{ - data: append([]byte{0x01, 0x01}, byteArray(64)...), - dst: &b, - }, - want: byteArray(64), - }, - { - args: args{ - data: append([]byte{0xfd, 0xff}, byteArray(16383)...), - dst: &b, - }, - want: byteArray(16383), - }, - { - args: args{ - data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), - dst: &b, - }, - want: byteArray(16384), - }, - // string - { - args: args{ - data: []byte{0x04, 0x01}, - dst: &s, - }, - want: string([]byte{0x01}), - }, - { - args: args{ - data: []byte{0x04, 0xff}, - dst: &s, - }, - want: string([]byte{0xff}), - }, - { - args: args{ - data: []byte{0x08, 0x01, 0x01}, - dst: &s, - }, - want: string([]byte{0x01, 0x01}), - }, - { - args: args{ - data: append([]byte{0x01, 0x01}, byteArray(64)...), - dst: &s, - }, - want: string(byteArray(64)), - }, - { - args: args{ - data: append([]byte{0xfd, 0xff}, byteArray(16383)...), - dst: &s, - }, - want: string(byteArray(16383)), - }, - { - args: args{ - data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), - dst: &s, - }, - want: string(byteArray(16384)), - }, - } - for _, tt := range tests { + for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } func Test_decodeState_decodeBool(t *testing.T) { - var b bool - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - args: args{ - data: []byte{0x01}, - dst: &b, - }, - want: true, - }, - { - args: args{ - data: []byte{0x00}, - dst: &b, - }, - want: false, - }, - { - name: "error case", - args: args{ - data: []byte{0x03}, - dst: &b, - }, - wantErr: true, - }, - } - for _, tt := range tests { + for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } + +// func Test_decodeState_decodeBool(t *testing.T) { +// var b bool +// type args struct { +// data []byte +// dst interface{} +// } +// tests := []struct { +// name string +// args args +// wantErr bool +// want interface{} +// }{ +// { +// args: args{ +// data: []byte{0x01}, +// dst: &b, +// }, +// want: true, +// }, +// { +// args: args{ +// data: []byte{0x00}, +// dst: &b, +// }, +// want: false, +// }, +// { +// name: "error case", +// args: args{ +// data: []byte{0x03}, +// dst: &b, +// }, +// wantErr: true, +// }, +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// err := Unmarshal(tt.args.data, tt.args.dst) +// if (err != nil) != tt.wantErr { +// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// return +// } +// if err != nil { +// return +// } +// got := reflect.ValueOf(tt.args.dst).Elem().Interface() +// if !reflect.DeepEqual(got, tt.want) { +// t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) +// } +// }) +// } +// } From c510b646a0d43f2d27c2b9ec3d7f963d17bed2c9 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 26 May 2021 11:25:29 -0400 Subject: [PATCH 011/245] struct decode tests, and unmarshal refactor --- pkg/scale/decode.go | 69 ++++++++++++++++++++++++++-------- pkg/scale/decode_test.go | 81 ++++++++++++++-------------------------- pkg/scale/encode_test.go | 8 ++-- 3 files changed, 86 insertions(+), 72 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 27a4acf0e2..89ff6e975a 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -10,6 +10,12 @@ import ( ) func Unmarshal(data []byte, dst interface{}) (err error) { + dstv := reflect.ValueOf(dst) + if dstv.Kind() != reflect.Ptr || dstv.IsNil() { + err = fmt.Errorf("unsupported dst: %T", dst) + return + } + buf := &bytes.Buffer{} ds := decodeState{} _, err = buf.Write(data) @@ -17,7 +23,13 @@ func Unmarshal(data []byte, dst interface{}) (err error) { return } ds.Buffer = *buf - err = ds.unmarshal(dst) + out, err := ds.unmarshal(dstv.Elem()) + if err != nil { + return + } + if out != nil { + dstv.Elem().Set(reflect.ValueOf(out)) + } return } @@ -25,15 +37,8 @@ type decodeState struct { bytes.Buffer } -func (ds *decodeState) unmarshal(dst interface{}) (err error) { - dstv := reflect.ValueOf(dst) - if dstv.Kind() != reflect.Ptr || dstv.IsNil() { - err = fmt.Errorf("unsupported dst: %T", dst) - return - } - - // this needs to be used to handle zero value - in := dstv.Elem().Interface() +func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error) { + in := dstv.Interface() switch in.(type) { case *big.Int: in, err = ds.decodeBigInt() @@ -66,12 +71,17 @@ func (ds *decodeState) unmarshal(dst interface{}) (err error) { in = nil case 0x01: elem := reflect.ValueOf(in).Elem() - err = ds.unmarshal(elem) + out := elem.Interface() + out, err = ds.unmarshal(elem) + if err != nil { + break + } + in = &out default: - err = fmt.Errorf("unsupported Option value: %v", rb) + err = fmt.Errorf("unsupported Option value: %v, bytes: %v", rb, ds.Bytes()) } - // case reflect.Struct: - // err = es.encodeStruct(in) + case reflect.Struct: + in, err = ds.decodeStruct(in) // case reflect.Array: // err = es.encodeArray(in) // case reflect.Slice: @@ -84,7 +94,36 @@ func (ds *decodeState) unmarshal(dst interface{}) (err error) { if err != nil { return } - dstv.Elem().Set(reflect.ValueOf(in)) + out = in + return +} + +// decodeStruct comment about this +func (ds *decodeState) decodeStruct(dst interface{}) (s interface{}, err error) { + v, indices, err := cache.fieldScaleIndices(dst) + if err != nil { + return + } + + s = v.Interface() + sv := reflect.ValueOf(s) + for _, i := range indices { + field := sv.Field(i.fieldIndex) + if !field.CanInterface() { + continue + } + + var out interface{} + out, err = ds.unmarshal(field) + if err != nil { + err = fmt.Errorf("%s, field: %+v", err, field) + return + } + if out != nil { + continue + } + field.Set(reflect.ValueOf(out)) + } return } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 99a04a306d..34447a5a65 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -34,7 +34,7 @@ func Test_decodeState_decodeBigInt(t *testing.T) { } func Test_decodeState_decodeBytes(t *testing.T) { - for _, tt := range boolTests { + for _, tt := range stringTests { t.Run(tt.name, func(t *testing.T) { dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { @@ -61,55 +61,30 @@ func Test_decodeState_decodeBool(t *testing.T) { } } -// func Test_decodeState_decodeBool(t *testing.T) { -// var b bool -// type args struct { -// data []byte -// dst interface{} -// } -// tests := []struct { -// name string -// args args -// wantErr bool -// want interface{} -// }{ -// { -// args: args{ -// data: []byte{0x01}, -// dst: &b, -// }, -// want: true, -// }, -// { -// args: args{ -// data: []byte{0x00}, -// dst: &b, -// }, -// want: false, -// }, -// { -// name: "error case", -// args: args{ -// data: []byte{0x03}, -// dst: &b, -// }, -// wantErr: true, -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// err := Unmarshal(tt.args.data, tt.args.dst) -// if (err != nil) != tt.wantErr { -// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// if err != nil { -// return -// } -// got := reflect.ValueOf(tt.args.dst).Elem().Interface() -// if !reflect.DeepEqual(got, tt.want) { -// t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) -// } -// }) -// } -// } +func Test_decodeState_decodeStruct(t *testing.T) { + for _, tt := range structTests { + t.Run(tt.name, func(t *testing.T) { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + switch reflect.ValueOf(tt.in).Kind() { + case reflect.Ptr: + if reflect.ValueOf(dst).IsZero() { + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } else { + // have to do this since reflect.DeepEqual won't come back true for different addresses + if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } + default: + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } + }) + } +} diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 1775389f39..eb926ea914 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -333,13 +333,13 @@ var ( }, } - nilPtrMyStruct *myStruct - ptrMystruct *myStruct = &myStruct{ + nilPtrMyStruct *MyStruct + ptrMystruct *MyStruct = &MyStruct{ Foo: []byte{0x01}, Bar: 2, Baz: true, } - nilPtrMyStruct2 *myStruct = nil + nilPtrMyStruct2 *MyStruct = nil // nilPtrMyStruct2 = nil structTests = tests{ { @@ -597,7 +597,7 @@ var ( ) ) -type myStruct struct { +type MyStruct struct { Foo []byte Bar int32 Baz bool From 10905ea4635de2841a0bae8c4a532b63282d1135 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 27 May 2021 16:00:27 -0400 Subject: [PATCH 012/245] wip --- pkg/scale/decode.go | 135 ++++++++++++++++++++++++++++++++++++--- pkg/scale/decode_test.go | 132 ++++++++++++++++++++++++++++++++------ pkg/scale/encode_test.go | 91 ++++++++++++++------------ 3 files changed, 286 insertions(+), 72 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 89ff6e975a..ce5953a66f 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -27,8 +27,12 @@ func Unmarshal(data []byte, dst interface{}) (err error) { if err != nil { return } + + elem := dstv.Elem() if out != nil { - dstv.Elem().Set(reflect.ValueOf(out)) + elem.Set(reflect.ValueOf(out)) + } else { + elem.Set(reflect.Zero(elem.Type())) } return } @@ -68,6 +72,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error } switch rb { case 0x00: + fmt.Println("in here!") in = nil case 0x01: elem := reflect.ValueOf(in).Elem() @@ -82,10 +87,10 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error } case reflect.Struct: in, err = ds.decodeStruct(in) - // case reflect.Array: - // err = es.encodeArray(in) - // case reflect.Slice: - // err = es.encodeSlice(in) + case reflect.Array: + in, err = ds.decodeArray(in) + case reflect.Slice: + in, err = ds.decodeSlice(in) default: err = fmt.Errorf("unsupported type: %T", in) } @@ -98,17 +103,120 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error return } +// decodeArray comment about this +func (ds *decodeState) decodeSlice(dst interface{}) (arr interface{}, err error) { + dstv := reflect.ValueOf(dst) + // seems redundant since this shouldn't be called unless it is a slice, + // but I'll leave this error check in for completeness + if dstv.Kind() != reflect.Slice { + err = fmt.Errorf("can not decodeSlice dst type: %T", dstv) + return + } + + l, err := ds.decodeLength() + if err != nil { + return nil, err + } + + switch dst.(type) { + case []int: + temp := make([]int, l) + for i := 0; i < l; i++ { + var ui uint64 + ui, err = ds.decodeUint() + if err != nil { + return + } + temp[i] = int(ui) + } + arr = temp + default: + temp := reflect.New(reflect.TypeOf(dst)) + for i := 0; i < l; i++ { + tempElemType := reflect.TypeOf(dst).Elem() + tempElem := reflect.New(tempElemType).Elem() + + var out interface{} + out, err = ds.unmarshal(tempElem) + if err != nil { + return + } + + if reflect.ValueOf(out).IsValid() { + tempElem.Set(reflect.ValueOf(out)) + } else { + tempElem.Set(reflect.Zero(tempElem.Type())) + } + temp.Elem().Set(reflect.Append(temp.Elem(), tempElem)) + } + arr = temp.Elem().Interface() + } + + return +} + +// decodeArray comment about this +func (ds *decodeState) decodeArray(dst interface{}) (arr interface{}, err error) { + dstv := reflect.ValueOf(dst) + // seems redundant since this shouldn't be called unless it is an array, + // but I'll leave this error check in for completeness + if dstv.Kind() != reflect.Array { + err = fmt.Errorf("can not decodeArray dst type: %T", dstv) + return + } + + // temp := reflect.New(reflect.Indirect(reflect.ValueOf(dst)).Type()) + temp := reflect.New(reflect.TypeOf(dst)) + for i := 0; i < dstv.Len(); i++ { + elem := dstv.Index(i) + elemIn := elem.Interface() + + var out interface{} + switch elemIn.(type) { + case int: + fmt.Println("calling uint", elemIn) + var ui uint64 + ui, err = ds.decodeUint() + out = int(ui) + default: + fmt.Println("calling unmarshal", elemIn) + out, err = ds.unmarshal(elem) + } + if err != nil { + return + } + + tempElem := temp.Elem().Index(i) + if reflect.ValueOf(out).IsValid() { + tempElem.Set(reflect.ValueOf(out)) + } else { + tempElem.Set(reflect.Zero(tempElem.Type())) + } + } + + arr = temp.Elem().Interface() + return +} + // decodeStruct comment about this func (ds *decodeState) decodeStruct(dst interface{}) (s interface{}, err error) { + dstv := reflect.ValueOf(dst) + // seems redundant since this shouldn't be called unless it is a struct, + // but I'll leave this error check in for completeness + if dstv.Kind() != reflect.Struct { + err = fmt.Errorf("can not decodeStruct dst type: %T", dstv) + return + } + v, indices, err := cache.fieldScaleIndices(dst) if err != nil { return } - s = v.Interface() - sv := reflect.ValueOf(s) + temp := reflect.New(reflect.TypeOf(dst)) + // temp := reflect.New(reflect.Indirect(reflect.ValueOf(dst)).Type()) for _, i := range indices { - field := sv.Field(i.fieldIndex) + field := v.Field(i.fieldIndex) if !field.CanInterface() { continue } @@ -119,11 +227,18 @@ func (ds *decodeState) decodeStruct(dst interface{}) (s interface{}, err error) err = fmt.Errorf("%s, field: %+v", err, field) return } - if out != nil { + if out == nil { continue } - field.Set(reflect.ValueOf(out)) + + tempElem := temp.Elem().Field(i.fieldIndex) + if reflect.ValueOf(out).IsValid() { + tempElem.Set(reflect.ValueOf(out)) + } else { + tempElem.Set(reflect.Zero(tempElem.Type())) + } } + s = temp.Elem().Interface() return } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 34447a5a65..c5d2b0b5ac 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -1,6 +1,7 @@ package scale import ( + "math/big" "reflect" "testing" ) @@ -22,7 +23,8 @@ func Test_decodeState_decodeFixedWidthInt(t *testing.T) { func Test_decodeState_decodeBigInt(t *testing.T) { for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + var dst *big.Int + // dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -50,7 +52,8 @@ func Test_decodeState_decodeBytes(t *testing.T) { func Test_decodeState_decodeBool(t *testing.T) { for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + // dst := reflect.ValueOf(tt.in).Interface() + var dst bool if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -61,29 +64,118 @@ func Test_decodeState_decodeBool(t *testing.T) { } } +func Test_decodeState_decodeStructManual(t *testing.T) { + // nil case + var dst *MyStruct = nil + var b = []byte{0} + var want *MyStruct = nil + + // dst = structTests[0].dst + + err := Unmarshal(b, &dst) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + + // zero case MyStruct + var dst1 *MyStruct = &MyStruct{} + err = Unmarshal(b, &dst1) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst1, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + + // zero case MyStruct + var dst2 *MyStruct = &MyStruct{Baz: true} + err = Unmarshal(b, &dst2) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst2, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + +} + func Test_decodeState_decodeStruct(t *testing.T) { - for _, tt := range structTests { + for _, tt := range append([]test{}, structTests[0]) { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + // dst := tt.dst if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - switch reflect.ValueOf(tt.in).Kind() { - case reflect.Ptr: - if reflect.ValueOf(dst).IsZero() { - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - } else { - // have to do this since reflect.DeepEqual won't come back true for different addresses - if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - } - default: - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } + // dstv := reflect.ValueOf(dst) + // inv := reflect.ValueOf(tt.in) + + // if dstv.Kind() != inv.Kind() { + // t.Errorf("decodeState.unmarshal() differing kind = %T, want %T", dst, tt.in) + // return + // } + + // switch inv.Kind() { + // case reflect.Ptr: + // fmt.Println(dst, dstv.Interface(), tt.in, inv.Interface()) + // if inv.IsZero() { + // fmt.Println(dst, tt.in, dstv.Interface(), inv.Interface()) + // if !reflect.DeepEqual(dstv.Interface(), tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } else if inv.IsNil() { + // if !reflect.DeepEqual(dst, tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } else { + // // // have to do this since reflect.DeepEqual won't come back true for different addresses + // // if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { + // // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // // } + // if !reflect.DeepEqual(dst, inv) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } + // default: + // if !reflect.DeepEqual(dst, tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} +func Test_decodeState_decodeArray(t *testing.T) { + for _, tt := range arrayTests { + t.Run(tt.name, func(t *testing.T) { + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} + +func Test_decodeState_decodeSlice(t *testing.T) { + for _, tt := range sliceTests { + t.Run(tt.name, func(t *testing.T) { + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index eb926ea914..1b49418541 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -10,6 +10,7 @@ import ( type test struct { name string in interface{} + dst interface{} wantErr bool want []byte } @@ -340,55 +341,50 @@ var ( Baz: true, } nilPtrMyStruct2 *MyStruct = nil - // nilPtrMyStruct2 = nil - structTests = tests{ - { - name: "nilPtrMyStruct", - in: nilPtrMyStruct, - want: []byte{0}, - }, - { - name: "ptrMystruct", - in: ptrMystruct, - want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - }, - { - name: "ptrMystruct cache hit", - in: ptrMystruct, - - want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - }, - { - name: "nilPtrMyStruct2", - in: nilPtrMyStruct2, - - want: []byte{0}, - }, - { - name: "&struct {[]byte, int32}", - in: &struct { - Foo []byte - Bar int32 - Baz bool - }{ - Foo: []byte{0x01}, - Bar: 2, - Baz: true, - }, - want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - }, + structTests = tests{ + // { + // name: "nilPtrMyStruct", + // in: nilPtrMyStruct2, + // want: []byte{0}, + // dst: &MyStruct{Baz: true}, + // }, + // { + // name: "ptrMystruct", + // in: ptrMystruct, + // want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + // dst: &MyStruct{}, + // }, + // { + // name: "ptrMystruct cache hit", + // in: ptrMystruct, + // want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + // dst: &MyStruct{}, + // }, + // { + // name: "nilPtrMyStruct2", + // in: nilPtrMyStruct2, + // want: []byte{0}, + // dst: new(MyStruct), + // }, + // { + // name: "&struct {[]byte, int32}", + // in: &MyStruct{ + // Foo: []byte{0x01}, + // Bar: 2, + // Baz: true, + // }, + // want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + // dst: &MyStruct{}, + // }, { name: "struct {[]byte, int32}", - in: struct { - Foo []byte - Bar int32 - Baz bool - }{ + in: MyStruct{ Foo: []byte{0x01}, Bar: 2, Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool}", @@ -402,6 +398,7 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool} with untagged attributes", @@ -543,51 +540,61 @@ var ( name: "[4]int{1, 2, 3, 4}", in: [4]int{1, 2, 3, 4}, want: []byte{0x04, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{16384, 2, 3, 4}", in: [4]int{16384, 2, 3, 4}, want: []byte{0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{1073741824, 2, 3, 4}", in: [4]int{1073741824, 2, 3, 4}, want: []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{1 << 32, 2, 3, 1 << 32}", in: [4]int{1 << 32, 2, 3, 1 << 32}, want: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, + dst: [4]int{}, }, { name: "[3]bool{true, false, true}", in: [3]bool{true, false, true}, want: []byte{0x01, 0x00, 0x01}, + dst: [3]bool{}, }, { name: "[2][]int{{0, 1}, {1, 0}}", in: [2][]int{{0, 1}, {1, 0}}, want: []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, + dst: [2]int{}, }, { name: "[2][2]int{{0, 1}, {1, 0}}", in: [2][2]int{{0, 1}, {1, 0}}, want: []byte{0x00, 0x04, 0x04, 0x00}, + dst: [2][2]int{}, }, { name: "[2]*big.Int{big.NewInt(0), big.NewInt(1)}", in: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, want: []byte{0x00, 0x04}, + dst: [2]*big.Int{}, }, { name: "[2][]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, + // dst: [2][]byte{{0x00, 0x01}, {0x01, 0x00}} }, { name: "[2][2]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x00, 0x01, 0x01, 0x00}, + dst: [2][2]byte{}, }, } From 868b0e77ce60c4effac77db87239831d51a52332 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 28 May 2021 10:37:33 -0400 Subject: [PATCH 013/245] decode of VariantDataType, wip tests --- pkg/scale/comparison_test.go | 65 +++++++++----------------------- pkg/scale/decode.go | 72 +++++++++++++++++++++++++++++++----- pkg/scale/encode.go | 21 ++++++++--- pkg/scale/scale.go | 23 ++++++++++++ 4 files changed, 117 insertions(+), 64 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 56a02df047..8945bd9425 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -1,57 +1,14 @@ -package scale +package scale_test import ( - "encoding/binary" "reflect" "testing" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/lib/crypto/sr25519" + "github.com/ChainSafe/gossamer/pkg/scale" ) -// BabePrimaryPreDigest as defined in Polkadot RE Spec, definition 5.10 in section 5.1.4 -type BabePrimaryPreDigest struct { - AuthorityIndex uint32 - SlotNumber uint64 - VrfOutput [sr25519.VrfOutputLength]byte - VrfProof [sr25519.VrfProofLength]byte -} - -// Encode performs SCALE encoding of a BABEPrimaryPreDigest -func (d *BabePrimaryPreDigest) Encode() []byte { - enc := []byte{byte(1)} - - buf := make([]byte, 4) - binary.LittleEndian.PutUint32(buf, d.AuthorityIndex) - enc = append(enc, buf...) - - buf = make([]byte, 8) - binary.LittleEndian.PutUint64(buf, d.SlotNumber) - enc = append(enc, buf...) - enc = append(enc, d.VrfOutput[:]...) - enc = append(enc, d.VrfProof[:]...) - return enc -} - -func TestOldVsNewEncoding(t *testing.T) { - bh := &BabePrimaryPreDigest{ - VrfOutput: [sr25519.VrfOutputLength]byte{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, - VrfProof: [sr25519.VrfProofLength]byte{120, 23, 235, 159, 115, 122, 207, 206, 123, 232, 75, 243, 115, 255, 131, 181, 219, 241, 200, 206, 21, 22, 238, 16, 68, 49, 86, 99, 76, 139, 39, 0, 102, 106, 181, 136, 97, 141, 187, 1, 234, 183, 241, 28, 27, 229, 133, 8, 32, 246, 245, 206, 199, 142, 134, 124, 226, 217, 95, 30, 176, 246, 5, 3}, - AuthorityIndex: 17, - SlotNumber: 420, - } - oldEncode := bh.Encode() - newEncode, err := Marshal(bh) - if err != nil { - t.Errorf("unexpected err: %v", err) - return - } - if !reflect.DeepEqual(oldEncode, newEncode) { - t.Errorf("encodeState.encodeStruct() = %v, want %v", oldEncode, newEncode) - } -} - // ChangesTrieRootDigest contains the root of the changes trie at a given block, if the runtime supports it. type ChangesTrieRootDigest struct { Hash common.Hash @@ -91,7 +48,7 @@ func (prd SealDigest) Index() uint { return 5 } -func TestOldVsNewEncoding2(t *testing.T) { +func TestOldVsNewEncoding(t *testing.T) { oldDigest := types.Digest{ &types.ChangesTrieRootDigest{ Hash: common.Hash{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, @@ -115,7 +72,10 @@ func TestOldVsNewEncoding2(t *testing.T) { return } - newDigest := VaryingDataType{ + type Digests scale.VaryingDataType + scale.RegisterVaryingDataType(Digests{}, ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}) + + newDigest := Digests{ ChangesTrieRootDigest{ Hash: common.Hash{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, }, @@ -133,7 +93,7 @@ func TestOldVsNewEncoding2(t *testing.T) { }, } - newEncode, err := Marshal(newDigest) + newEncode, err := scale.Marshal(newDigest) if err != nil { t.Errorf("unexpected err: %v", err) return @@ -141,4 +101,13 @@ func TestOldVsNewEncoding2(t *testing.T) { if !reflect.DeepEqual(oldEncode, newEncode) { t.Errorf("encodeState.encodeStruct() = %v, want %v", oldEncode, newEncode) } + + var decoded Digests + err = scale.Unmarshal(newEncode, &decoded) + if err != nil { + t.Errorf("unexpected err: %v", err) + } + if !reflect.DeepEqual(decoded, newDigest) { + t.Errorf("Unmarshal() = %v, want %v", decoded, newDigest) + } } diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index ce5953a66f..454d2f88fd 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -59,9 +59,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error case bool: in, err = ds.decodeBool() // case VaryingDataType: - // err = es.encodeVaryingDataType(in) - // // TODO: case common.Hash: - // // n, err = es.Writer.Write(v.ToBytes()) + // in, err = ds.decodeVaryingDataType() default: switch reflect.TypeOf(in).Kind() { case reflect.Ptr: @@ -72,7 +70,6 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error } switch rb { case 0x00: - fmt.Println("in here!") in = nil case 0x01: elem := reflect.ValueOf(in).Elem() @@ -90,7 +87,15 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error case reflect.Array: in, err = ds.decodeArray(in) case reflect.Slice: - in, err = ds.decodeSlice(in) + t := reflect.TypeOf(in) + // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType + switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { + case true: + in, err = ds.decodeVaryingDataType(in) + case false: + in, err = ds.decodeSlice(in) + } + default: err = fmt.Errorf("unsupported type: %T", in) } @@ -103,6 +108,52 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error return } +func (ds *decodeState) decodeVaryingDataType(dst interface{}) (vdt interface{}, err error) { + l, err := ds.decodeLength() + if err != nil { + return nil, err + } + + dstt := reflect.TypeOf(dst) + key := fmt.Sprintf("%s.%s", dstt.PkgPath(), dstt.Name()) + mappedValues, ok := vdtCache[key] + if !ok { + err = fmt.Errorf("unable to find registered custom VaryingDataType: %T", dst) + return + } + + temp := reflect.New(dstt) + for i := 0; i < l; i++ { + var b byte + b, err = ds.ReadByte() + if err != nil { + return + } + + val, ok := mappedValues[uint(b)] + if !ok { + err = fmt.Errorf("unable to find registered VaryingDataTypeValue for type: %T", dst) + return + } + + tempVal := reflect.New(reflect.TypeOf(val)).Elem() + var out interface{} + out, err = ds.unmarshal(tempVal) + if err != nil { + return + } + + if reflect.ValueOf(out).IsValid() { + tempVal.Set(reflect.ValueOf(out)) + } else { + tempVal.Set(reflect.Zero(tempVal.Type())) + } + temp.Elem().Set(reflect.Append(temp.Elem(), tempVal)) + } + vdt = temp.Elem().Interface() + return +} + // decodeArray comment about this func (ds *decodeState) decodeSlice(dst interface{}) (arr interface{}, err error) { dstv := reflect.ValueOf(dst) @@ -174,16 +225,17 @@ func (ds *decodeState) decodeArray(dst interface{}) (arr interface{}, err error) var out interface{} switch elemIn.(type) { case int: - fmt.Println("calling uint", elemIn) var ui uint64 ui, err = ds.decodeUint() + if err != nil { + return + } out = int(ui) default: - fmt.Println("calling unmarshal", elemIn) out, err = ds.unmarshal(elem) - } - if err != nil { - return + if err != nil { + return + } } tempElem := temp.Elem().Index(i) diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 90477ea71f..7a56a215f2 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/binary" "fmt" + "log" "math/big" "reflect" ) @@ -39,10 +40,6 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeBytes([]byte(in)) case bool: err = es.encodeBool(in) - case VaryingDataType: - err = es.encodeVaryingDataType(in) - // TODO: case common.Hash: - // n, err = es.Writer.Write(v.ToBytes()) default: switch reflect.TypeOf(in).Kind() { case reflect.Ptr: @@ -63,7 +60,20 @@ func (es *encodeState) marshal(in interface{}) (err error) { case reflect.Array: err = es.encodeArray(in) case reflect.Slice: - err = es.encodeSlice(in) + t := reflect.TypeOf(in) + // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType + switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { + case true: + invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) + switch in := invdt.Interface().(type) { + case VaryingDataType: + err = es.encodeVaryingDataType(in) + default: + log.Panicf("this should never happen") + } + case false: + err = es.encodeSlice(in) + } default: err = fmt.Errorf("unsupported type: %T", in) } @@ -72,7 +82,6 @@ func (es *encodeState) marshal(in interface{}) (err error) { } func (es *encodeState) encodeVaryingDataType(values VaryingDataType) (err error) { - // err = es.encodeLength(len(types)) err = es.encodeLength(len(values)) if err != nil { return diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 035f156a9a..5b339409f6 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -8,8 +8,31 @@ import ( "sync" ) +type varyingDataTypeCache map[string]map[uint]VaryingDataTypeValue + +var vdtCache varyingDataTypeCache = make(varyingDataTypeCache) + type VaryingDataType []VaryingDataTypeValue +func RegisterVaryingDataType(in interface{}, values ...VaryingDataTypeValue) (err error) { + _, ok := in.(VaryingDataType) + if !ok { + err = fmt.Errorf("%T is not a VaryingDataType", in) + } + + t := reflect.TypeOf(in) + key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) + + _, ok = vdtCache[key] + if !ok { + vdtCache[key] = make(map[uint]VaryingDataTypeValue) + } + for _, val := range values { + vdtCache[key][val.Index()] = val + } + return +} + // VaryingDataType is used to represent scale encodable types type VaryingDataTypeValue interface { Index() uint From 91e030b1d35bda9b0d5d90706425a55e13122126 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 28 May 2021 16:07:37 -0400 Subject: [PATCH 014/245] add optionality testing --- pkg/scale/decode.go | 46 ++++++++++++++------------- pkg/scale/decode_test.go | 68 +++++++++++++++++++--------------------- pkg/scale/encode_test.go | 13 -------- 3 files changed, 57 insertions(+), 70 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 454d2f88fd..a62972d4a1 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -58,30 +58,10 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error in = string(b) case bool: in, err = ds.decodeBool() - // case VaryingDataType: - // in, err = ds.decodeVaryingDataType() default: switch reflect.TypeOf(in).Kind() { case reflect.Ptr: - var rb byte - rb, err = ds.ReadByte() - if err != nil { - break - } - switch rb { - case 0x00: - in = nil - case 0x01: - elem := reflect.ValueOf(in).Elem() - out := elem.Interface() - out, err = ds.unmarshal(elem) - if err != nil { - break - } - in = &out - default: - err = fmt.Errorf("unsupported Option value: %v, bytes: %v", rb, ds.Bytes()) - } + in, err = ds.decodePointer(in) case reflect.Struct: in, err = ds.decodeStruct(in) case reflect.Array: @@ -95,7 +75,6 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error case false: in, err = ds.decodeSlice(in) } - default: err = fmt.Errorf("unsupported type: %T", in) } @@ -108,6 +87,29 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error return } +func (ds *decodeState) decodePointer(in interface{}) (out interface{}, err error) { + var rb byte + rb, err = ds.ReadByte() + if err != nil { + return + } + switch rb { + case 0x00: + out = nil + case 0x01: + elem := reflect.ValueOf(in).Elem() + temp := elem.Interface() + temp, err = ds.unmarshal(elem) + if err != nil { + break + } + out = &temp + default: + err = fmt.Errorf("unsupported Option value: %v, bytes: %v", rb, ds.Bytes()) + } + return +} + func (ds *decodeState) decodeVaryingDataType(dst interface{}) (vdt interface{}, err error) { l, err := ds.decodeLength() if err != nil { diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index c5d2b0b5ac..7fc99b8dc6 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -99,7 +99,6 @@ func Test_decodeState_decodeStructManual(t *testing.T) { if !reflect.DeepEqual(dst2, want) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) } - } func Test_decodeState_decodeStruct(t *testing.T) { @@ -111,40 +110,6 @@ func Test_decodeState_decodeStruct(t *testing.T) { if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - // dstv := reflect.ValueOf(dst) - // inv := reflect.ValueOf(tt.in) - - // if dstv.Kind() != inv.Kind() { - // t.Errorf("decodeState.unmarshal() differing kind = %T, want %T", dst, tt.in) - // return - // } - - // switch inv.Kind() { - // case reflect.Ptr: - // fmt.Println(dst, dstv.Interface(), tt.in, inv.Interface()) - // if inv.IsZero() { - // fmt.Println(dst, tt.in, dstv.Interface(), inv.Interface()) - // if !reflect.DeepEqual(dstv.Interface(), tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } else if inv.IsNil() { - // if !reflect.DeepEqual(dst, tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } else { - // // // have to do this since reflect.DeepEqual won't come back true for different addresses - // // if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { - // // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // // } - // if !reflect.DeepEqual(dst, inv) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } - // default: - // if !reflect.DeepEqual(dst, tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } if !reflect.DeepEqual(dst, tt.in) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } @@ -180,3 +145,36 @@ func Test_decodeState_decodeSlice(t *testing.T) { }) } } + +func Test_unmarshal_optionality(t *testing.T) { + var ptrTests tests + for _, t := range allTests { + ptrTest := test{ + name: t.name, + in: t.in, + wantErr: t.wantErr, + want: t.want, + } + switch t.in { + case nil: + // this doesn't actually happen since none of the tests have nil value for tt.in + ptrTest.want = []byte{0x00} + default: + ptrTest.want = append([]byte{0x01}, t.want...) + } + ptrTests = append(ptrTests, ptrTest) + } + for _, tt := range ptrTests { + t.Run(tt.name, func(t *testing.T) { + // this becomes a pointer to a zero value of the underlying value + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + // es := &encodeState{fieldScaleIndicesCache: cache} + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, &tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 1b49418541..6468f3efe5 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -10,7 +10,6 @@ import ( type test struct { name string in interface{} - dst interface{} wantErr bool want []byte } @@ -384,7 +383,6 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool}", @@ -398,7 +396,6 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool} with untagged attributes", @@ -540,61 +537,51 @@ var ( name: "[4]int{1, 2, 3, 4}", in: [4]int{1, 2, 3, 4}, want: []byte{0x04, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{16384, 2, 3, 4}", in: [4]int{16384, 2, 3, 4}, want: []byte{0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{1073741824, 2, 3, 4}", in: [4]int{1073741824, 2, 3, 4}, want: []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{1 << 32, 2, 3, 1 << 32}", in: [4]int{1 << 32, 2, 3, 1 << 32}, want: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, - dst: [4]int{}, }, { name: "[3]bool{true, false, true}", in: [3]bool{true, false, true}, want: []byte{0x01, 0x00, 0x01}, - dst: [3]bool{}, }, { name: "[2][]int{{0, 1}, {1, 0}}", in: [2][]int{{0, 1}, {1, 0}}, want: []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, - dst: [2]int{}, }, { name: "[2][2]int{{0, 1}, {1, 0}}", in: [2][2]int{{0, 1}, {1, 0}}, want: []byte{0x00, 0x04, 0x04, 0x00}, - dst: [2][2]int{}, }, { name: "[2]*big.Int{big.NewInt(0), big.NewInt(1)}", in: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, want: []byte{0x00, 0x04}, - dst: [2]*big.Int{}, }, { name: "[2][]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, - // dst: [2][]byte{{0x00, 0x01}, {0x01, 0x00}} }, { name: "[2][2]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x00, 0x01, 0x01, 0x00}, - dst: [2][2]byte{}, }, } From 82927944722f58812210ef80a57cfe05dc954c79 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 31 May 2021 13:42:36 -0400 Subject: [PATCH 015/245] fix struct tests and optionality tests --- go.mod | 1 + go.sum | 2 ++ pkg/scale/decode_test.go | 53 +++++++++++++++++++++++++++------------- pkg/scale/encode_test.go | 27 +++++++++++++------- 4 files changed, 57 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 620a0385ed..519258043d 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 // indirect github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect + github.com/google/go-cmp v0.5.6 // indirect github.com/google/uuid v1.1.5 // indirect github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 diff --git a/go.sum b/go.sum index 37101993d9..7a54e962d3 100644 --- a/go.sum +++ b/go.sum @@ -148,6 +148,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= github.com/google/gopacket v1.1.18 h1:lum7VRA9kdlvBi7/v2p7/zcbkduHaCH/SVVyurs7OpY= github.com/google/gopacket v1.1.18/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 7fc99b8dc6..8a67b7b8fc 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -4,6 +4,9 @@ import ( "math/big" "reflect" "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" ) func Test_decodeState_decodeFixedWidthInt(t *testing.T) { @@ -24,7 +27,6 @@ func Test_decodeState_decodeBigInt(t *testing.T) { for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { var dst *big.Int - // dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -52,7 +54,6 @@ func Test_decodeState_decodeBytes(t *testing.T) { func Test_decodeState_decodeBool(t *testing.T) { for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() var dst bool if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) @@ -70,8 +71,6 @@ func Test_decodeState_decodeStructManual(t *testing.T) { var b = []byte{0} var want *MyStruct = nil - // dst = structTests[0].dst - err := Unmarshal(b, &dst) if err != nil { t.Errorf("unexpected error: %v", err) @@ -102,16 +101,20 @@ func Test_decodeState_decodeStructManual(t *testing.T) { } func Test_decodeState_decodeStruct(t *testing.T) { - for _, tt := range append([]test{}, structTests[0]) { + for _, tt := range structTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() - // dst := tt.dst + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + var diff string + if tt.out != nil { + diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(tt.in)) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) } }) } @@ -119,8 +122,7 @@ func Test_decodeState_decodeStruct(t *testing.T) { func Test_decodeState_decodeArray(t *testing.T) { for _, tt := range arrayTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -134,8 +136,7 @@ func Test_decodeState_decodeArray(t *testing.T) { func Test_decodeState_decodeSlice(t *testing.T) { for _, tt := range sliceTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -154,6 +155,7 @@ func Test_unmarshal_optionality(t *testing.T) { in: t.in, wantErr: t.wantErr, want: t.want, + out: t.out, } switch t.in { case nil: @@ -168,12 +170,29 @@ func Test_unmarshal_optionality(t *testing.T) { t.Run(tt.name, func(t *testing.T) { // this becomes a pointer to a zero value of the underlying value dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - // es := &encodeState{fieldScaleIndicesCache: cache} if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - if !reflect.DeepEqual(dst, &tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + + if tt.wantErr == true { + return + } + + switch reflect.TypeOf(tt.in).Kind() { + case reflect.Struct: + var diff string + if tt.out != nil { + diff = cmp.Diff(dst, &tt.out, cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(dst, &tt.in, cmpopts.IgnoreUnexported(tt.in)) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + default: + if !reflect.DeepEqual(dst, &tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } } }) } diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 6468f3efe5..b2dfeea724 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -12,6 +12,7 @@ type test struct { in interface{} wantErr bool want []byte + out interface{} } type tests []test @@ -471,21 +472,20 @@ var ( }, { name: "struct {[]byte, int32, bool} with ignored attributes", - in: struct { - Baz bool `scale:"3"` - Bar int32 `scale:"2"` - Foo []byte `scale:"1"` - Ignore string `scale:"-"` - somethingElse *struct { - fields int - } - }{ + in: MyStructWithIgnore{ Foo: []byte{0x01}, Bar: 2, Baz: true, Ignore: "me", }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + out: MyStructWithIgnore{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + // zero value of string, since this field is ignored + Ignore: "", + }, }, } @@ -596,6 +596,15 @@ type MyStruct struct { Bar int32 Baz bool } +type MyStructWithIgnore struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + Ignore string `scale:"-"` + somethingElse *struct { + fields int + } +} func Test_encodeState_encodeFixedWidthInteger(t *testing.T) { for _, tt := range fixedWidthIntegerTests { From 2fc8515ee575aaab0c9874790b229566597b3171 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 31 May 2021 22:54:57 -0400 Subject: [PATCH 016/245] test VaryingDataType --- pkg/scale/comparison_test.go | 7 +- pkg/scale/decode.go | 39 +++- pkg/scale/decode_test.go | 30 +-- pkg/scale/encode.go | 17 +- pkg/scale/encode_test.go | 246 ++++++++++++++++++++---- pkg/scale/scale.go | 30 --- pkg/scale/varying_data_type.go | 35 ++++ pkg/scale/varying_data_type_test.go | 288 ++++++++++++++++++++++++++++ 8 files changed, 589 insertions(+), 103 deletions(-) create mode 100644 pkg/scale/varying_data_type.go create mode 100644 pkg/scale/varying_data_type_test.go diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 8945bd9425..93f6c2f0e5 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -73,8 +73,11 @@ func TestOldVsNewEncoding(t *testing.T) { } type Digests scale.VaryingDataType - scale.RegisterVaryingDataType(Digests{}, ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}) - + err = scale.RegisterVaryingDataType(Digests{}, ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}) + if err != nil { + t.Errorf("unexpected err: %v", err) + return + } newDigest := Digests{ ChangesTrieRootDigest{ Hash: common.Hash{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index a62972d4a1..0142dc1058 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -12,7 +12,7 @@ import ( func Unmarshal(data []byte, dst interface{}) (err error) { dstv := reflect.ValueOf(dst) if dstv.Kind() != reflect.Ptr || dstv.IsNil() { - err = fmt.Errorf("unsupported dst: %T", dst) + err = fmt.Errorf("unsupported dst: %T, must be a pointer to a destination", dst) return } @@ -76,7 +76,21 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error in, err = ds.decodeSlice(in) } default: - err = fmt.Errorf("unsupported type: %T", in) + _, ok := in.(VaryingDataTypeValue) + switch ok { + case true: + t := reflect.TypeOf(in) + switch t.Kind() { + // TODO: support more primitive types. Do we need to support arrays and slices as well? + case reflect.Int: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + case reflect.Int16: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + } + in, err = ds.unmarshal(reflect.ValueOf(in)) + default: + err = fmt.Errorf("unsupported type: %T", in) + } } } @@ -97,13 +111,20 @@ func (ds *decodeState) decodePointer(in interface{}) (out interface{}, err error case 0x00: out = nil case 0x01: - elem := reflect.ValueOf(in).Elem() - temp := elem.Interface() - temp, err = ds.unmarshal(elem) + inType := reflect.TypeOf(in) + elem := inType.Elem() + inCopy := reflect.New(inType) + tempElem := reflect.New(elem) + + var temp interface{} + temp, err = ds.unmarshal(tempElem.Elem()) if err != nil { break } - out = &temp + + tempElem.Elem().Set(reflect.ValueOf(temp).Convert(tempElem.Elem().Type())) + inCopy.Elem().Set(tempElem) + out = inCopy.Elem().Interface() default: err = fmt.Errorf("unsupported Option value: %v, bytes: %v", rb, ds.Bytes()) } @@ -146,7 +167,7 @@ func (ds *decodeState) decodeVaryingDataType(dst interface{}) (vdt interface{}, } if reflect.ValueOf(out).IsValid() { - tempVal.Set(reflect.ValueOf(out)) + tempVal.Set(reflect.ValueOf(out).Convert(tempVal.Type())) } else { tempVal.Set(reflect.Zero(tempVal.Type())) } @@ -196,7 +217,7 @@ func (ds *decodeState) decodeSlice(dst interface{}) (arr interface{}, err error) } if reflect.ValueOf(out).IsValid() { - tempElem.Set(reflect.ValueOf(out)) + tempElem.Set(reflect.ValueOf(out).Convert(tempElem.Type())) } else { tempElem.Set(reflect.Zero(tempElem.Type())) } @@ -242,7 +263,7 @@ func (ds *decodeState) decodeArray(dst interface{}) (arr interface{}, err error) tempElem := temp.Elem().Index(i) if reflect.ValueOf(out).IsValid() { - tempElem.Set(reflect.ValueOf(out)) + tempElem.Set(reflect.ValueOf(out).Convert(tempElem.Type())) } else { tempElem.Set(reflect.Zero(tempElem.Type())) } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 8a67b7b8fc..f1adbab293 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -111,7 +111,7 @@ func Test_decodeState_decodeStruct(t *testing.T) { if tt.out != nil { diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in)) } else { - diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(tt.in)) + diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(big.Int{}, tt.in, VDTValue2{}, MyStructWithIgnore{})) } if diff != "" { t.Errorf("decodeState.unmarshal() = %s", diff) @@ -172,28 +172,18 @@ func Test_unmarshal_optionality(t *testing.T) { dst := reflect.New(reflect.TypeOf(tt.in)).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - - if tt.wantErr == true { return } - - switch reflect.TypeOf(tt.in).Kind() { - case reflect.Struct: - var diff string - if tt.out != nil { - diff = cmp.Diff(dst, &tt.out, cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(dst, &tt.in, cmpopts.IgnoreUnexported(tt.in)) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - default: - if !reflect.DeepEqual(dst, &tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) } } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 7a56a215f2..6dd45cfa52 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -75,7 +75,22 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - err = fmt.Errorf("unsupported type: %T", in) + _, ok := in.(VaryingDataTypeValue) + switch ok { + case true: + t := reflect.TypeOf(in) + switch t.Kind() { + // TODO: support more primitive types. Do we need to support arrays and slices as well? + case reflect.Int: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + case reflect.Int16: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + } + err = es.marshal(in) + default: + err = fmt.Errorf("unsupported type: %T", in) + } + } } return diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index b2dfeea724..58774e902d 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -23,6 +23,73 @@ func newTests(ts ...tests) (appended tests) { return } +func newWant(wanted ...[]byte) (want []byte) { + for _, w := range wanted { + want = append(want, w...) + } + return +} + +func newBigIntPtr(in *big.Int) (ptr **big.Int) { + ptr = &in + return +} + +func newIntPtr(in int) (ptr *int) { + ptr = &in + return +} + +func newUintPtr(in uint) (ptr *uint) { + ptr = &in + return +} + +func newInt8Ptr(in int8) (ptr *int8) { + ptr = &in + return +} +func newUint8Ptr(in uint8) (ptr *uint8) { + ptr = &in + return +} +func newInt16Ptr(in int16) (ptr *int16) { + ptr = &in + return +} +func newUint16Ptr(in uint16) (ptr *uint16) { + ptr = &in + return +} +func newInt32Ptr(in int32) (ptr *int32) { + ptr = &in + return +} +func newUint32Ptr(in uint32) (ptr *uint32) { + ptr = &in + return +} +func newInt64Ptr(in int64) (ptr *int64) { + ptr = &in + return +} +func newUint64Ptr(in uint64) (ptr *uint64) { + ptr = &in + return +} +func newBytesPtr(in []byte) (ptr *[]byte) { + ptr = &in + return +} +func newStringPtr(in string) (ptr *string) { + ptr = &in + return +} +func newBoolPtr(in bool) (ptr *bool) { + ptr = &in + return +} + var ( intTests = tests{ { @@ -342,40 +409,6 @@ var ( } nilPtrMyStruct2 *MyStruct = nil structTests = tests{ - // { - // name: "nilPtrMyStruct", - // in: nilPtrMyStruct2, - // want: []byte{0}, - // dst: &MyStruct{Baz: true}, - // }, - // { - // name: "ptrMystruct", - // in: ptrMystruct, - // want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - // dst: &MyStruct{}, - // }, - // { - // name: "ptrMystruct cache hit", - // in: ptrMystruct, - // want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - // dst: &MyStruct{}, - // }, - // { - // name: "nilPtrMyStruct2", - // in: nilPtrMyStruct2, - // want: []byte{0}, - // dst: new(MyStruct), - // }, - // { - // name: "&struct {[]byte, int32}", - // in: &MyStruct{ - // Foo: []byte{0x01}, - // Bar: 2, - // Baz: true, - // }, - // want: []byte{1, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - // dst: &MyStruct{}, - // }, { name: "struct {[]byte, int32}", in: MyStruct{ @@ -456,13 +489,7 @@ var ( }, { name: "struct {[]byte, int32, bool} with private attributes", - in: struct { - priv0 string - Baz bool `scale:"3"` - Bar int32 `scale:"2"` - Foo []byte `scale:"1"` - priv1 []byte - }{ + in: MyStructWithPrivate{ priv0: "stuff", Foo: []byte{0x01}, Bar: 2, @@ -487,6 +514,136 @@ var ( Ignore: "", }, }, + { + in: VDTValue{ + A: big.NewInt(1073741823), + B: int(1073741823), + C: uint(1073741823), + D: int8(1), + E: uint8(1), + F: int16(16383), + G: uint16(16383), + H: int32(1073741823), + I: uint32(1073741823), + J: int64(9223372036854775807), + K: uint64(9223372036854775807), + L: byteArray(64), + M: testStrings[1], + N: true, + }, + want: newWant( + []byte{ + 0xfe, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0x01, + 0x01, + 0xff, 0x3f, + 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + }, + append([]byte{0x01, 0x01}, byteArray(64)...), + append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), + []byte{0x01}, + ), + }, + { + in: VDTValue1{ + O: newBigIntPtr(big.NewInt(1073741823)), + P: newIntPtr(int(1073741823)), + Q: newUintPtr(uint(1073741823)), + R: newInt8Ptr(int8(1)), + S: newUint8Ptr(uint8(1)), + T: newInt16Ptr(16383), + U: newUint16Ptr(16383), + V: newInt32Ptr(1073741823), + W: newUint32Ptr(1073741823), + X: newInt64Ptr(9223372036854775807), + Y: newUint64Ptr(9223372036854775807), + Z: newBytesPtr(byteArray(64)), + AA: newStringPtr(testStrings[1]), + AB: newBoolPtr(true), + }, + want: newWant( + []byte{ + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x01, 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0x01, 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0x01, 0x01, + 0x01, 0x01, + 0x01, 0xff, 0x3f, + 0x01, 0xff, 0x3f, + 0x01, 0xff, 0xff, 0xff, 0x3f, + 0x01, 0xff, 0xff, 0xff, 0x3f, + 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + }, + append([]byte{0x01, 0x01, 0x01}, byteArray(64)...), + append([]byte{0x01, 0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), + []byte{0x01, 0x01}, + ), + }, + { + in: VDTValue2{ + A: MyStruct{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + B: MyStructWithIgnore{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + C: &MyStruct{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + D: &MyStructWithIgnore{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + + E: []int{1073741824, 2, 3, 4}, + F: []bool{true, false, true}, + G: []*big.Int{big.NewInt(0), big.NewInt(1)}, + H: [][]int{{0, 1}, {1, 0}}, + I: [][]byte{{0x00, 0x01}, {0x01, 0x00}}, + + J: [4]int{1073741824, 2, 3, 4}, + K: [3]bool{true, false, true}, + L: [2][]int{{0, 1}, {1, 0}}, + M: [2][2]int{{0, 1}, {1, 0}}, + N: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, + O: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, + P: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, + }, + want: newWant( + []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + []byte{0x01, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + []byte{0x01, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + + []byte{0x10, 0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, + []byte{0x0c, 0x01, 0x00, 0x01}, + []byte{0x08, 0x00, 0x04}, + []byte{0x08, 0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, + []byte{0x08, 0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, + + []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, + []byte{0x01, 0x00, 0x01}, + []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, + []byte{0x00, 0x04, 0x04, 0x00}, + []byte{0x00, 0x04}, + []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, + []byte{0x00, 0x01, 0x01, 0x00}, + ), + }, } sliceTests = tests{ @@ -605,6 +762,13 @@ type MyStructWithIgnore struct { fields int } } +type MyStructWithPrivate struct { + priv0 string + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + priv1 []byte +} func Test_encodeState_encodeFixedWidthInteger(t *testing.T) { for _, tt := range fixedWidthIntegerTests { diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 5b339409f6..f96b8bd8c9 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -8,36 +8,6 @@ import ( "sync" ) -type varyingDataTypeCache map[string]map[uint]VaryingDataTypeValue - -var vdtCache varyingDataTypeCache = make(varyingDataTypeCache) - -type VaryingDataType []VaryingDataTypeValue - -func RegisterVaryingDataType(in interface{}, values ...VaryingDataTypeValue) (err error) { - _, ok := in.(VaryingDataType) - if !ok { - err = fmt.Errorf("%T is not a VaryingDataType", in) - } - - t := reflect.TypeOf(in) - key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) - - _, ok = vdtCache[key] - if !ok { - vdtCache[key] = make(map[uint]VaryingDataTypeValue) - } - for _, val := range values { - vdtCache[key][val.Index()] = val - } - return -} - -// VaryingDataType is used to represent scale encodable types -type VaryingDataTypeValue interface { - Index() uint -} - // package level cache for fieldScaleIndicies var cache = &fieldScaleIndicesCache{ cache: make(map[string]fieldScaleIndices), diff --git a/pkg/scale/varying_data_type.go b/pkg/scale/varying_data_type.go new file mode 100644 index 0000000000..05f82d36be --- /dev/null +++ b/pkg/scale/varying_data_type.go @@ -0,0 +1,35 @@ +package scale + +import ( + "fmt" + "reflect" +) + +type varyingDataTypeCache map[string]map[uint]VaryingDataTypeValue + +var vdtCache varyingDataTypeCache = make(varyingDataTypeCache) + +type VaryingDataType []VaryingDataTypeValue + +func RegisterVaryingDataType(in interface{}, values ...VaryingDataTypeValue) (err error) { + t := reflect.TypeOf(in) + if !t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { + err = fmt.Errorf("%T is not a VaryingDataType", in) + return + } + + key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) + _, ok := vdtCache[key] + if !ok { + vdtCache[key] = make(map[uint]VaryingDataTypeValue) + } + for _, val := range values { + vdtCache[key][val.Index()] = val + } + return +} + +// VaryingDataType is used to represent scale encodable types +type VaryingDataTypeValue interface { + Index() uint +} diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go new file mode 100644 index 0000000000..11208c0189 --- /dev/null +++ b/pkg/scale/varying_data_type_test.go @@ -0,0 +1,288 @@ +package scale + +import ( + "math/big" + "reflect" + "testing" +) + +type VDTValue struct { + A *big.Int + B int + C uint + D int8 + E uint8 + F int16 + G uint16 + H int32 + I uint32 + J int64 + K uint64 + L []byte + M string + N bool +} + +func (ctrd VDTValue) Index() uint { + return 1 +} + +type VDTValue1 struct { + O **big.Int + P *int + Q *uint + R *int8 + S *uint8 + T *int16 + U *uint16 + V *int32 + W *uint32 + X *int64 + Y *uint64 + Z *[]byte + AA *string + AB *bool +} + +func (ctrd VDTValue1) Index() uint { + return 2 +} + +type VDTValue2 struct { + A MyStruct + B MyStructWithIgnore + C *MyStruct + D *MyStructWithIgnore + + E []int + F []bool + G []*big.Int + H [][]int + I [][]byte + + J [4]int + K [3]bool + L [2][]int + M [2][2]int + N [2]*big.Int + O [2][]byte + P [2][2]byte +} + +func (ctrd VDTValue2) Index() uint { + return 3 +} + +type VDTValue3 int16 + +func (ctrd VDTValue3) Index() uint { + return 4 +} + +type testVDT VaryingDataType + +func init() { + err := RegisterVaryingDataType(testVDT{}, VDTValue{}, VDTValue2{}, VDTValue1{}, VDTValue3(0)) + if err != nil { + panic(err) + } +} + +var VariableDataTypeTests = tests{ + { + in: testVDT{ + VDTValue1{ + O: newBigIntPtr(big.NewInt(1073741823)), + }, + }, + want: []byte{ + 4, 2, + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + }, + { + in: testVDT{ + VDTValue{ + A: big.NewInt(1073741823), + B: int(1073741823), + C: uint(1073741823), + D: int8(1), + E: uint8(1), + F: int16(16383), + G: uint16(16383), + H: int32(1073741823), + I: uint32(1073741823), + J: int64(9223372036854775807), + K: uint64(9223372036854775807), + L: byteArray(64), + M: testStrings[1], + N: true, + }, + VDTValue1{ + O: newBigIntPtr(big.NewInt(1073741823)), + P: newIntPtr(int(1073741823)), + Q: newUintPtr(uint(1073741823)), + R: newInt8Ptr(int8(1)), + S: newUint8Ptr(uint8(1)), + T: newInt16Ptr(16383), + U: newUint16Ptr(16383), + V: newInt32Ptr(1073741823), + W: newUint32Ptr(1073741823), + X: newInt64Ptr(9223372036854775807), + Y: newUint64Ptr(9223372036854775807), + Z: newBytesPtr(byteArray(64)), + AA: newStringPtr(testStrings[1]), + AB: newBoolPtr(true), + }, + VDTValue2{ + A: MyStruct{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + B: MyStructWithIgnore{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + C: &MyStruct{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + D: &MyStructWithIgnore{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + }, + + E: []int{1073741824, 2, 3, 4}, + F: []bool{true, false, true}, + G: []*big.Int{big.NewInt(0), big.NewInt(1)}, + H: [][]int{{0, 1}, {1, 0}}, + I: [][]byte{{0x00, 0x01}, {0x01, 0x00}}, + + J: [4]int{1073741824, 2, 3, 4}, + K: [3]bool{true, false, true}, + L: [2][]int{{0, 1}, {1, 0}}, + M: [2][2]int{{0, 1}, {1, 0}}, + N: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, + O: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, + P: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, + }, + VDTValue3(16383), + }, + want: newWant( + // length encoding of 3 + []byte{16}, + // index of VDTValue + []byte{1}, + // encoding of struct + []byte{ + 0xfe, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0x01, + 0x01, + 0xff, 0x3f, + 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + }, + append([]byte{0x01, 0x01}, byteArray(64)...), + append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), + []byte{0x01}, + + // index of VDTValue1 + []byte{2}, + // encoding of struct + []byte{ + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x01, 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0x01, 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0x01, 0x01, + 0x01, 0x01, + 0x01, 0xff, 0x3f, + 0x01, 0xff, 0x3f, + 0x01, 0xff, 0xff, 0xff, 0x3f, + 0x01, 0xff, 0xff, 0xff, 0x3f, + 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + }, + append([]byte{0x01, 0x01, 0x01}, byteArray(64)...), + append([]byte{0x01, 0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), + []byte{0x01, 0x01}, + + // index of VDTValue2 + []byte{3}, + // encoding of struct + []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + []byte{0x01, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + []byte{0x01, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + + []byte{0x10, 0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, + []byte{0x0c, 0x01, 0x00, 0x01}, + []byte{0x08, 0x00, 0x04}, + []byte{0x08, 0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, + []byte{0x08, 0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, + + []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, + []byte{0x01, 0x00, 0x01}, + []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, + []byte{0x00, 0x04, 0x04, 0x00}, + []byte{0x00, 0x04}, + []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, + []byte{0x00, 0x01, 0x01, 0x00}, + + // index of VDTValue2 + []byte{4}, + // encoding of int16 + []byte{0xff, 0x3f}, + ), + }, +} + +func Test_encodeState_encodeVaryingDataType(t *testing.T) { + for _, tt := range VariableDataTypeTests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{fieldScaleIndicesCache: cache} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeStruct() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_decodeState_decodeVaryingDataType(t *testing.T) { + for _, tt := range append(tests{}, VariableDataTypeTests...) { + t.Run(tt.name, func(t *testing.T) { + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} From 51772295c42e3dfad6c877cf390dd543f5461413 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 10:52:45 -0400 Subject: [PATCH 017/245] wip decode refactor, use reflect.Value as passed param --- pkg/scale/decode.go | 241 ++++++++++++++++++--------------------- pkg/scale/decode_test.go | 5 +- 2 files changed, 116 insertions(+), 130 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 0142dc1058..10f22aab0d 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -23,17 +23,10 @@ func Unmarshal(data []byte, dst interface{}) (err error) { return } ds.Buffer = *buf - out, err := ds.unmarshal(dstv.Elem()) + err = ds.unmarshal(dstv.Elem()) if err != nil { return } - - elem := dstv.Elem() - if out != nil { - elem.Set(reflect.ValueOf(out)) - } else { - elem.Set(reflect.Zero(elem.Type())) - } return } @@ -41,31 +34,30 @@ type decodeState struct { bytes.Buffer } -func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error) { +func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { in := dstv.Interface() switch in.(type) { case *big.Int: - in, err = ds.decodeBigInt() + err = ds.decodeBigInt(dstv) case *Uint128: - in, err = ds.decodeUint128() + err = ds.decodeUint128(dstv) case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: - in, err = ds.decodeFixedWidthInt(in) + err = ds.decodeFixedWidthInt(dstv) case []byte: - in, err = ds.decodeBytes() + err = ds.decodeBytes(dstv) case string: - var b []byte - b, err = ds.decodeBytes() - in = string(b) + err = ds.decodeString(dstv) + // in = string(b) case bool: - in, err = ds.decodeBool() + err = ds.decodeBool(dstv) default: switch reflect.TypeOf(in).Kind() { case reflect.Ptr: - in, err = ds.decodePointer(in) + err = ds.decodePointer(dstv) case reflect.Struct: - in, err = ds.decodeStruct(in) + err = ds.decodeStruct(dstv) case reflect.Array: - in, err = ds.decodeArray(in) + err = ds.decodeArray(dstv) case reflect.Slice: t := reflect.TypeOf(in) // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType @@ -73,7 +65,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error case true: in, err = ds.decodeVaryingDataType(in) case false: - in, err = ds.decodeSlice(in) + err = ds.decodeSlice(dstv) } default: _, ok := in.(VaryingDataTypeValue) @@ -87,7 +79,6 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error case reflect.Int16: in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() } - in, err = ds.unmarshal(reflect.ValueOf(in)) default: err = fmt.Errorf("unsupported type: %T", in) } @@ -97,11 +88,11 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (out interface{}, err error if err != nil { return } - out = in + // out = in return } -func (ds *decodeState) decodePointer(in interface{}) (out interface{}, err error) { +func (ds *decodeState) decodePointer(dstv reflect.Value) (err error) { var rb byte rb, err = ds.ReadByte() if err != nil { @@ -109,22 +100,15 @@ func (ds *decodeState) decodePointer(in interface{}) (out interface{}, err error } switch rb { case 0x00: - out = nil + // just return dstv untouched case 0x01: - inType := reflect.TypeOf(in) - elem := inType.Elem() - inCopy := reflect.New(inType) - tempElem := reflect.New(elem) - - var temp interface{} - temp, err = ds.unmarshal(tempElem.Elem()) + elemType := reflect.TypeOf(dstv.Interface()).Elem() + tempElem := reflect.New(elemType) + err = ds.unmarshal(tempElem.Elem()) if err != nil { break } - - tempElem.Elem().Set(reflect.ValueOf(temp).Convert(tempElem.Elem().Type())) - inCopy.Elem().Set(tempElem) - out = inCopy.Elem().Interface() + dstv.Set(tempElem) default: err = fmt.Errorf("unsupported Option value: %v, bytes: %v", rb, ds.Bytes()) } @@ -160,17 +144,11 @@ func (ds *decodeState) decodeVaryingDataType(dst interface{}) (vdt interface{}, } tempVal := reflect.New(reflect.TypeOf(val)).Elem() - var out interface{} - out, err = ds.unmarshal(tempVal) + err = ds.unmarshal(tempVal) if err != nil { return } - if reflect.ValueOf(out).IsValid() { - tempVal.Set(reflect.ValueOf(out).Convert(tempVal.Type())) - } else { - tempVal.Set(reflect.Zero(tempVal.Type())) - } temp.Elem().Set(reflect.Append(temp.Elem(), tempVal)) } vdt = temp.Elem().Interface() @@ -178,21 +156,20 @@ func (ds *decodeState) decodeVaryingDataType(dst interface{}) (vdt interface{}, } // decodeArray comment about this -func (ds *decodeState) decodeSlice(dst interface{}) (arr interface{}, err error) { - dstv := reflect.ValueOf(dst) +func (ds *decodeState) decodeSlice(dstv reflect.Value) (err error) { // seems redundant since this shouldn't be called unless it is a slice, // but I'll leave this error check in for completeness - if dstv.Kind() != reflect.Slice { - err = fmt.Errorf("can not decodeSlice dst type: %T", dstv) - return - } + // if dstv.Kind() != reflect.Slice { + // err = fmt.Errorf("can not decodeSlice dst type: %T %v", dstv.Interface(), dstv.Kind()) + // return + // } l, err := ds.decodeLength() if err != nil { - return nil, err + return } - switch dst.(type) { + switch dstv.Interface().(type) { case []int: temp := make([]int, l) for i := 0; i < l; i++ { @@ -203,49 +180,44 @@ func (ds *decodeState) decodeSlice(dst interface{}) (arr interface{}, err error) } temp[i] = int(ui) } - arr = temp + dstv.Set(reflect.ValueOf(temp)) default: - temp := reflect.New(reflect.TypeOf(dst)) + in := dstv.Interface() + temp := reflect.New(reflect.ValueOf(in).Type()) for i := 0; i < l; i++ { - tempElemType := reflect.TypeOf(dst).Elem() + tempElemType := reflect.TypeOf(in).Elem() tempElem := reflect.New(tempElemType).Elem() - var out interface{} - out, err = ds.unmarshal(tempElem) + err = ds.unmarshal(tempElem) if err != nil { return } - - if reflect.ValueOf(out).IsValid() { - tempElem.Set(reflect.ValueOf(out).Convert(tempElem.Type())) - } else { - tempElem.Set(reflect.Zero(tempElem.Type())) - } temp.Elem().Set(reflect.Append(temp.Elem(), tempElem)) } - arr = temp.Elem().Interface() + dstv.Set(temp.Elem()) } return } // decodeArray comment about this -func (ds *decodeState) decodeArray(dst interface{}) (arr interface{}, err error) { - dstv := reflect.ValueOf(dst) - // seems redundant since this shouldn't be called unless it is an array, - // but I'll leave this error check in for completeness - if dstv.Kind() != reflect.Array { - err = fmt.Errorf("can not decodeArray dst type: %T", dstv) - return - } +func (ds *decodeState) decodeArray(dstv reflect.Value) (err error) { + // dstv := reflect.ValueOf(dst) + // // seems redundant since this shouldn't be called unless it is an array, + // // but I'll leave this error check in for completeness + // if dstv.Kind() != reflect.Array { + // err = fmt.Errorf("can not decodeArray dst type: %T", dstv) + // return + // } // temp := reflect.New(reflect.Indirect(reflect.ValueOf(dst)).Type()) - temp := reflect.New(reflect.TypeOf(dst)) - for i := 0; i < dstv.Len(); i++ { - elem := dstv.Index(i) + in := dstv.Interface() + temp := reflect.New(reflect.ValueOf(in).Type()) + for i := 0; i < temp.Elem().Len(); i++ { + elem := temp.Elem().Index(i) elemIn := elem.Interface() - var out interface{} + // var out interface{} switch elemIn.(type) { case int: var ui uint64 @@ -253,78 +225,58 @@ func (ds *decodeState) decodeArray(dst interface{}) (arr interface{}, err error) if err != nil { return } - out = int(ui) + elem.Set(reflect.ValueOf(int(ui))) default: - out, err = ds.unmarshal(elem) + err = ds.unmarshal(elem) if err != nil { return } } - tempElem := temp.Elem().Index(i) - if reflect.ValueOf(out).IsValid() { - tempElem.Set(reflect.ValueOf(out).Convert(tempElem.Type())) - } else { - tempElem.Set(reflect.Zero(tempElem.Type())) - } + // tempElem := temp.Elem().Index(i) + // if reflect.ValueOf(out).IsValid() { + // tempElem.Set(reflect.ValueOf(out).Convert(tempElem.Type())) + // } else { + // tempElem.Set(reflect.Zero(tempElem.Type())) + // } } - - arr = temp.Elem().Interface() + dstv.Set(temp.Elem()) + // arr = temp.Elem().Interface() return } // decodeStruct comment about this -func (ds *decodeState) decodeStruct(dst interface{}) (s interface{}, err error) { - dstv := reflect.ValueOf(dst) - // seems redundant since this shouldn't be called unless it is a struct, - // but I'll leave this error check in for completeness - if dstv.Kind() != reflect.Struct { - err = fmt.Errorf("can not decodeStruct dst type: %T", dstv) - return - } - - v, indices, err := cache.fieldScaleIndices(dst) +func (ds *decodeState) decodeStruct(dstv reflect.Value) (err error) { + in := dstv.Interface() + _, indices, err := cache.fieldScaleIndices(in) if err != nil { return } - - temp := reflect.New(reflect.TypeOf(dst)) - // temp := reflect.New(reflect.Indirect(reflect.ValueOf(dst)).Type()) + temp := reflect.New(reflect.ValueOf(in).Type()) for _, i := range indices { - field := v.Field(i.fieldIndex) + field := temp.Elem().Field(i.fieldIndex) if !field.CanInterface() { continue } - - var out interface{} - out, err = ds.unmarshal(field) + err = ds.unmarshal(field) if err != nil { err = fmt.Errorf("%s, field: %+v", err, field) return } - if out == nil { - continue - } - - tempElem := temp.Elem().Field(i.fieldIndex) - if reflect.ValueOf(out).IsValid() { - tempElem.Set(reflect.ValueOf(out)) - } else { - tempElem.Set(reflect.Zero(tempElem.Type())) - } } - s = temp.Elem().Interface() + dstv.Set(temp.Elem()) return } // decodeBool accepts a byte array representing a SCALE encoded bool and performs SCALE decoding // of the bool then returns it. if invalid, return false and an error -func (ds *decodeState) decodeBool() (b bool, err error) { +func (ds *decodeState) decodeBool(dstv reflect.Value) (err error) { rb, err := ds.ReadByte() if err != nil { return } + var b bool switch rb { case 0x00: case 0x01: @@ -332,6 +284,7 @@ func (ds *decodeState) decodeBool() (b bool, err error) { default: err = fmt.Errorf("could not decode invalid bool") } + dstv.Set(reflect.ValueOf(b)) return } @@ -386,17 +339,39 @@ func (ds *decodeState) decodeLength() (l int, err error) { // of the byte array // if the encoding is valid, it then returns the decoded byte array, the total number of input bytes decoded, and nil // otherwise, it returns nil, 0, and error -func (ds *decodeState) decodeBytes() (b []byte, err error) { +func (ds *decodeState) decodeBytes(dstv reflect.Value) (err error) { length, err := ds.decodeLength() if err != nil { - return nil, err + return + } + + b := make([]byte, length) + _, err = ds.Read(b) + if err != nil { + return + } + + dstv.Set(reflect.ValueOf(b).Convert(dstv.Type())) + return +} + +// decodeString accepts a byte array representing a SCALE encoded byte array and performs SCALE decoding +// of the byte array +// if the encoding is valid, it then returns the decoded byte array, the total number of input bytes decoded, and nil +// otherwise, it returns nil, 0, and error +func (ds *decodeState) decodeString(dstv reflect.Value) (err error) { + length, err := ds.decodeLength() + if err != nil { + return } - b = make([]byte, length) + b := make([]byte, length) _, err = ds.Read(b) if err != nil { - return nil, errors.New("could not decode invalid byte array: reached early EOF") + err = fmt.Errorf("could not decode invalid string: %v", err) } + + dstv.Set(reflect.ValueOf(string(b))) return } @@ -426,12 +401,13 @@ func (ds *decodeState) decodeSmallInt(firstByte, mode byte) (out int64, err erro // decodeBigInt decodes a SCALE encoded byte array into a *big.Int // Works for all integers, including ints > 2**64 -func (ds *decodeState) decodeBigInt() (output *big.Int, err error) { +func (ds *decodeState) decodeBigInt(dstv reflect.Value) (err error) { b, err := ds.ReadByte() if err != nil { return } + var output *big.Int // check mode of encoding, stored at 2 least significant bits mode := b & 0x03 switch { @@ -450,18 +426,21 @@ func (ds *decodeState) decodeBigInt() (output *big.Int, err error) { buf := make([]byte, byteLen) _, err = ds.Read(buf) - if err == nil { - o := reverseBytes(buf) - output = big.NewInt(0).SetBytes(o) - } else { - err = errors.New("could not decode invalid big.Int: reached early EOF") + if err != nil { + err = fmt.Errorf("could not decode invalid big.Int: %v", err) + break } + o := reverseBytes(buf) + output = big.NewInt(0).SetBytes(o) } + dstv.Set(reflect.ValueOf(output)) return } // decodeFixedWidthInt decodes integers < 2**32 by reading the bytes in little endian -func (ds *decodeState) decodeFixedWidthInt(in interface{}) (out interface{}, err error) { +func (ds *decodeState) decodeFixedWidthInt(dstv reflect.Value) (err error) { + in := dstv.Interface() + var out interface{} switch in.(type) { case int8: var b byte @@ -534,17 +513,23 @@ func (ds *decodeState) decodeFixedWidthInt(in interface{}) (out interface{}, err } out = uint(binary.LittleEndian.Uint64(buf)) } + dstv.Set(reflect.ValueOf(out)) return } // decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 // if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise // it returns nil and error -func (ds *decodeState) decodeUint128() (ui *Uint128, err error) { +func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { buf := make([]byte, 16) err = binary.Read(ds, binary.LittleEndian, buf) if err != nil { - return nil, err + return } - return NewUint128(buf) + ui128, err := NewUint128(buf) + if err != nil { + return + } + dstv.Set(reflect.ValueOf(ui128)) + return } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index f1adbab293..3a62b34f0e 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -12,7 +12,7 @@ import ( func Test_decodeState_decodeFixedWidthInt(t *testing.T) { for _, tt := range fixedWidthIntegerTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -40,9 +40,10 @@ func Test_decodeState_decodeBigInt(t *testing.T) { func Test_decodeState_decodeBytes(t *testing.T) { for _, tt := range stringTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return } if !reflect.DeepEqual(dst, tt.in) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) From c854487bf9450517887f05af5e3711bc0927eaeb Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 17:41:07 -0400 Subject: [PATCH 018/245] repurpose int and uint as compact length encoded integers, remove unnecessary handling of []int --- pkg/scale/comparison_test.go | 21 +++ pkg/scale/decode.go | 230 ++++++++++------------------ pkg/scale/decode_test.go | 49 ++---- pkg/scale/encode.go | 7 +- pkg/scale/encode_test.go | 69 +++++++-- pkg/scale/uint128.go | 1 + pkg/scale/varying_data_type_test.go | 16 +- 7 files changed, 192 insertions(+), 201 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 93f6c2f0e5..29d9ec5e39 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -1,11 +1,14 @@ package scale_test import ( + "fmt" + "math/big" "reflect" "testing" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" + oldScale "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -114,3 +117,21 @@ func TestOldVsNewEncoding(t *testing.T) { t.Errorf("Unmarshal() = %v, want %v", decoded, newDigest) } } + +func TestSomething(t *testing.T) { + i := big.NewInt(0) + expectedVal := *common.Uint128FromBigInt(i) + + encByts, err := oldScale.Encode(expectedVal) + if err != nil { + t.Errorf("%v", err) + return + } + encBytes2, err := oldScale.Encode(i) + if err != nil { + t.Errorf("%v", err) + return + } + + fmt.Printf("%+v, %+v", encByts, encBytes2) +} diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 10f22aab0d..378f1cb0a3 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -41,13 +41,14 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { err = ds.decodeBigInt(dstv) case *Uint128: err = ds.decodeUint128(dstv) - case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: + case int, uint: + err = ds.decodeUint(dstv) + case int8, uint8, int16, uint16, int32, uint32, int64, uint64: err = ds.decodeFixedWidthInt(dstv) case []byte: err = ds.decodeBytes(dstv) case string: - err = ds.decodeString(dstv) - // in = string(b) + err = ds.decodeBytes(dstv) case bool: err = ds.decodeBool(dstv) default: @@ -63,7 +64,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { case true: - in, err = ds.decodeVaryingDataType(in) + err = ds.decodeVaryingDataType(dstv) case false: err = ds.decodeSlice(dstv) } @@ -71,24 +72,29 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { _, ok := in.(VaryingDataTypeValue) switch ok { case true: + var temp reflect.Value t := reflect.TypeOf(in) switch t.Kind() { // TODO: support more primitive types. Do we need to support arrays and slices as well? case reflect.Int: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + temp = reflect.New(reflect.TypeOf(int(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } case reflect.Int16: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + temp = reflect.New(reflect.TypeOf(int16(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } } + dstv.Set(temp.Elem().Convert(t)) default: err = fmt.Errorf("unsupported type: %T", in) } } } - - if err != nil { - return - } - // out = in return } @@ -100,7 +106,7 @@ func (ds *decodeState) decodePointer(dstv reflect.Value) (err error) { } switch rb { case 0x00: - // just return dstv untouched + // nil case case 0x01: elemType := reflect.TypeOf(dstv.Interface()).Elem() tempElem := reflect.New(elemType) @@ -115,17 +121,17 @@ func (ds *decodeState) decodePointer(dstv reflect.Value) (err error) { return } -func (ds *decodeState) decodeVaryingDataType(dst interface{}) (vdt interface{}, err error) { +func (ds *decodeState) decodeVaryingDataType(dstv reflect.Value) (err error) { l, err := ds.decodeLength() if err != nil { - return nil, err + return } - dstt := reflect.TypeOf(dst) + dstt := reflect.TypeOf(dstv.Interface()) key := fmt.Sprintf("%s.%s", dstt.PkgPath(), dstt.Name()) mappedValues, ok := vdtCache[key] if !ok { - err = fmt.Errorf("unable to find registered custom VaryingDataType: %T", dst) + err = fmt.Errorf("unable to find registered custom VaryingDataType: %T", dstv.Interface()) return } @@ -139,7 +145,7 @@ func (ds *decodeState) decodeVaryingDataType(dst interface{}) (vdt interface{}, val, ok := mappedValues[uint(b)] if !ok { - err = fmt.Errorf("unable to find registered VaryingDataTypeValue for type: %T", dst) + err = fmt.Errorf("unable to find registered VaryingDataTypeValue for type: %T", dstv.Interface()) return } @@ -151,97 +157,43 @@ func (ds *decodeState) decodeVaryingDataType(dst interface{}) (vdt interface{}, temp.Elem().Set(reflect.Append(temp.Elem(), tempVal)) } - vdt = temp.Elem().Interface() + dstv.Set(temp.Elem()) return } -// decodeArray comment about this func (ds *decodeState) decodeSlice(dstv reflect.Value) (err error) { - // seems redundant since this shouldn't be called unless it is a slice, - // but I'll leave this error check in for completeness - // if dstv.Kind() != reflect.Slice { - // err = fmt.Errorf("can not decodeSlice dst type: %T %v", dstv.Interface(), dstv.Kind()) - // return - // } - l, err := ds.decodeLength() if err != nil { return } + in := dstv.Interface() + temp := reflect.New(reflect.ValueOf(in).Type()) + for i := 0; i < l; i++ { + tempElemType := reflect.TypeOf(in).Elem() + tempElem := reflect.New(tempElemType).Elem() - switch dstv.Interface().(type) { - case []int: - temp := make([]int, l) - for i := 0; i < l; i++ { - var ui uint64 - ui, err = ds.decodeUint() - if err != nil { - return - } - temp[i] = int(ui) - } - dstv.Set(reflect.ValueOf(temp)) - default: - in := dstv.Interface() - temp := reflect.New(reflect.ValueOf(in).Type()) - for i := 0; i < l; i++ { - tempElemType := reflect.TypeOf(in).Elem() - tempElem := reflect.New(tempElemType).Elem() - - err = ds.unmarshal(tempElem) - if err != nil { - return - } - temp.Elem().Set(reflect.Append(temp.Elem(), tempElem)) + err = ds.unmarshal(tempElem) + if err != nil { + return } - dstv.Set(temp.Elem()) + temp.Elem().Set(reflect.Append(temp.Elem(), tempElem)) } + dstv.Set(temp.Elem()) return } -// decodeArray comment about this func (ds *decodeState) decodeArray(dstv reflect.Value) (err error) { - // dstv := reflect.ValueOf(dst) - // // seems redundant since this shouldn't be called unless it is an array, - // // but I'll leave this error check in for completeness - // if dstv.Kind() != reflect.Array { - // err = fmt.Errorf("can not decodeArray dst type: %T", dstv) - // return - // } - - // temp := reflect.New(reflect.Indirect(reflect.ValueOf(dst)).Type()) in := dstv.Interface() temp := reflect.New(reflect.ValueOf(in).Type()) for i := 0; i < temp.Elem().Len(); i++ { elem := temp.Elem().Index(i) - elemIn := elem.Interface() - - // var out interface{} - switch elemIn.(type) { - case int: - var ui uint64 - ui, err = ds.decodeUint() - if err != nil { - return - } - elem.Set(reflect.ValueOf(int(ui))) - default: - err = ds.unmarshal(elem) - if err != nil { - return - } + err = ds.unmarshal(elem) + if err != nil { + return } - - // tempElem := temp.Elem().Index(i) - // if reflect.ValueOf(out).IsValid() { - // tempElem.Set(reflect.ValueOf(out).Convert(tempElem.Type())) - // } else { - // tempElem.Set(reflect.Zero(tempElem.Type())) - // } } dstv.Set(temp.Elem()) - // arr = temp.Elem().Interface() return } @@ -269,7 +221,7 @@ func (ds *decodeState) decodeStruct(dstv reflect.Value) (err error) { } // decodeBool accepts a byte array representing a SCALE encoded bool and performs SCALE decoding -// of the bool then returns it. if invalid, return false and an error +// of the bool then returns it. if invalid returns an error func (ds *decodeState) decodeBool(dstv reflect.Value) (err error) { rb, err := ds.ReadByte() if err != nil { @@ -288,78 +240,66 @@ func (ds *decodeState) decodeBool(dstv reflect.Value) (err error) { return } -// DecodeUnsignedInteger will decode unsigned integer -func (ds *decodeState) decodeUint() (o uint64, err error) { +// decodeUint will decode unsigned integer +func (ds *decodeState) decodeUint(dstv reflect.Value) (err error) { b, err := ds.ReadByte() if err != nil { - return 0, err + return } + in := dstv.Interface() + temp := reflect.New(reflect.TypeOf(in)) // check mode of encoding, stored at 2 least significant bits mode := b & 3 - if mode <= 2 { - val, e := ds.decodeSmallInt(b, mode) - return uint64(val), e - } - - // >4 byte mode - topSixBits := b >> 2 - byteLen := uint(topSixBits) + 4 + switch { + case mode <= 2: + var val int64 + val, err = ds.decodeSmallInt(b, mode) + if err != nil { + return + } + temp.Elem().Set(reflect.ValueOf(val).Convert(reflect.TypeOf(in))) + dstv.Set(temp.Elem()) + default: + // >4 byte mode + topSixBits := b >> 2 + byteLen := uint(topSixBits) + 4 - buf := make([]byte, byteLen) - _, err = ds.Read(buf) - if err != nil { - return 0, err - } + buf := make([]byte, byteLen) + _, err = ds.Read(buf) + if err != nil { + return + } - if byteLen == 4 { - o = uint64(binary.LittleEndian.Uint32(buf)) - } else if byteLen > 4 && byteLen < 8 { - tmp := make([]byte, 8) - copy(tmp, buf) - o = binary.LittleEndian.Uint64(tmp) - } else { - err = errors.New("could not decode invalid integer") + var o uint64 + if byteLen == 4 { + o = uint64(binary.LittleEndian.Uint32(buf)) + } else if byteLen > 4 && byteLen <= 8 { + tmp := make([]byte, 8) + copy(tmp, buf) + o = binary.LittleEndian.Uint64(tmp) + } else { + err = errors.New("could not decode invalid integer") + return + } + dstv.Set(reflect.ValueOf(o).Convert(reflect.TypeOf(in))) } - - return o, err -} - -// decodeLength accepts a byte array representing a SCALE encoded integer and performs SCALE decoding of the int -// if the encoding is valid, it then returns (o, bytesDecoded, err) where o is the decoded integer, bytesDecoded is the -// number of input bytes decoded, and err is nil -// otherwise, it returns 0, 0, and error -func (ds *decodeState) decodeLength() (l int, err error) { - ui, err := ds.decodeUint() - l = int(ui) return } -// DecodeByteArray accepts a byte array representing a SCALE encoded byte array and performs SCALE decoding -// of the byte array -// if the encoding is valid, it then returns the decoded byte array, the total number of input bytes decoded, and nil -// otherwise, it returns nil, 0, and error -func (ds *decodeState) decodeBytes(dstv reflect.Value) (err error) { - length, err := ds.decodeLength() - if err != nil { - return - } - - b := make([]byte, length) - _, err = ds.Read(b) +// decodeLength is helper method which calls decodeUint and casts to int +func (ds *decodeState) decodeLength() (l int, err error) { + dstv := reflect.New(reflect.TypeOf(l)) + err = ds.decodeUint(dstv.Elem()) if err != nil { return } - - dstv.Set(reflect.ValueOf(b).Convert(dstv.Type())) + l = dstv.Elem().Interface().(int) return } -// decodeString accepts a byte array representing a SCALE encoded byte array and performs SCALE decoding -// of the byte array -// if the encoding is valid, it then returns the decoded byte array, the total number of input bytes decoded, and nil -// otherwise, it returns nil, 0, and error -func (ds *decodeState) decodeString(dstv reflect.Value) (err error) { +// decodeBytes is used to decode with a destination of []byte or string type +func (ds *decodeState) decodeBytes(dstv reflect.Value) (err error) { length, err := ds.decodeLength() if err != nil { return @@ -368,14 +308,16 @@ func (ds *decodeState) decodeString(dstv reflect.Value) (err error) { b := make([]byte, length) _, err = ds.Read(b) if err != nil { - err = fmt.Errorf("could not decode invalid string: %v", err) + return } - dstv.Set(reflect.ValueOf(string(b))) + in := dstv.Interface() + inType := reflect.TypeOf(in) + dstv.Set(reflect.ValueOf(b).Convert(inType)) return } -// decodeSmallInt is used in the DecodeInteger and decodeBigInt functions when the mode is <= 2 +// decodeSmallInt is used in the decodeUint and decodeBigInt functions when the mode is <= 2 // need to pass in the first byte, since we assume it's already been read func (ds *decodeState) decodeSmallInt(firstByte, mode byte) (out int64, err error) { switch mode { @@ -518,8 +460,6 @@ func (ds *decodeState) decodeFixedWidthInt(dstv reflect.Value) (err error) { } // decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 -// if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise -// it returns nil and error func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { buf := make([]byte, 16) err = binary.Read(ds, binary.LittleEndian, buf) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 3a62b34f0e..4929c15e18 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -23,6 +23,20 @@ func Test_decodeState_decodeFixedWidthInt(t *testing.T) { } } +func Test_decodeState_decodeVariableWidthInt(t *testing.T) { + for _, tt := range variableWidthIntegerTests { + t.Run(tt.name, func(t *testing.T) { + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} + func Test_decodeState_decodeBigInt(t *testing.T) { for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { @@ -66,41 +80,6 @@ func Test_decodeState_decodeBool(t *testing.T) { } } -func Test_decodeState_decodeStructManual(t *testing.T) { - // nil case - var dst *MyStruct = nil - var b = []byte{0} - var want *MyStruct = nil - - err := Unmarshal(b, &dst) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } - - // zero case MyStruct - var dst1 *MyStruct = &MyStruct{} - err = Unmarshal(b, &dst1) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst1, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } - - // zero case MyStruct - var dst2 *MyStruct = &MyStruct{Baz: true} - err = Unmarshal(b, &dst2) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst2, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } -} - func Test_decodeState_decodeStruct(t *testing.T) { for _, tt := range structTests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 6dd45cfa52..e5bfb23c5e 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -28,7 +28,11 @@ type encodeState struct { func (es *encodeState) marshal(in interface{}) (err error) { switch in := in.(type) { - case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: + case int: + err = es.encodeUint(uint(in)) + case uint: + err = es.encodeUint(in) + case int8, uint8, int16, uint16, int32, uint32, int64, uint64: err = es.encodeFixedWidthInt(in) case *big.Int: err = es.encodeBigInt(in) @@ -377,5 +381,6 @@ func (es *encodeState) encodeUint(i uint) (err error) { // encodeUint128 encodes a Uint128 func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) + fmt.Println("bytes", es.Bytes(), i.Bytes()) return } diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 58774e902d..e23de7b9e3 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -95,46 +95,48 @@ var ( { name: "int(1)", in: int(1), - want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0}, + want: []byte{0x04}, }, { name: "int(16383)", in: int(16383), - want: []byte{0xff, 0x3f, 0, 0, 0, 0, 0, 0}, + want: []byte{0xfd, 0xff}, }, { name: "int(1073741823)", in: int(1073741823), - want: []byte{0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0}, + want: []byte{0xfe, 0xff, 0xff, 0xff}, }, { name: "int(9223372036854775807)", in: int(9223372036854775807), - want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + want: []byte{19, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, } uintTests = tests{ { name: "uint(1)", in: int(1), - want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0}, + want: []byte{0x04}, }, { name: "uint(16383)", in: uint(16383), - want: []byte{0xff, 0x3f, 0, 0, 0, 0, 0, 0}, + want: []byte{0xfd, 0xff}, }, { name: "uint(1073741823)", in: uint(1073741823), - want: []byte{0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0}, + want: []byte{0xfe, 0xff, 0xff, 0xff}, }, { name: "uint(9223372036854775807)", in: uint(9223372036854775807), - want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, } + variableWidthIntegerTests = newTests(intTests, uintTests) + int64Tests = tests{ { name: "int64(1)", @@ -252,7 +254,7 @@ var ( }, } fixedWidthIntegerTests = newTests( - int8Tests, int16Tests, int32Tests, int64Tests, intTests, uint8Tests, uint16Tests, uint32Tests, uint64Tests, uintTests, + int8Tests, int16Tests, int32Tests, int64Tests, uint8Tests, uint16Tests, uint32Tests, uint64Tests, ) zeroValBigInt *big.Int @@ -314,6 +316,17 @@ var ( }, } + uint128Tests = tests{ + { + in: MustNewUint128(big.NewInt(0)), + want: nil, + }, + { + in: MustNewUint128(big.NewInt(1)), + want: []byte{0x01}, + }, + } + testStrings = []string{ "We love you! We believe in open source as wonderful form of giving.", // n = 67 strings.Repeat("We need a longer string to test with. Let's multiple this several times.", 230), // n = 72 * 230 = 16560 @@ -534,8 +547,8 @@ var ( want: newWant( []byte{ 0xfe, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, - 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, 0x01, 0x01, 0xff, 0x3f, @@ -570,8 +583,8 @@ var ( want: newWant( []byte{ 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x01, 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, - 0x01, 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x01, 0xfe, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0xff, 0x3f, @@ -743,7 +756,7 @@ var ( } allTests = newTests( - fixedWidthIntegerTests, bigIntTests, stringTests, + fixedWidthIntegerTests, variableWidthIntegerTests, bigIntTests, stringTests, boolTests, structTests, sliceTests, arrayTests, ) ) @@ -784,6 +797,20 @@ func Test_encodeState_encodeFixedWidthInteger(t *testing.T) { } } +func Test_encodeState_encodeVariableWidthIntegers(t *testing.T) { + for _, tt := range variableWidthIntegerTests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeFixedWidthInt() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeFixedWidthInt() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + func Test_encodeState_encodeBigInt(t *testing.T) { for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { @@ -798,6 +825,20 @@ func Test_encodeState_encodeBigInt(t *testing.T) { } } +func Test_encodeState_encodeUint128(t *testing.T) { + for _, tt := range uint128Tests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeUin128() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeUin128() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + func Test_encodeState_encodeBytes(t *testing.T) { for _, tt := range stringTests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index fd9f441964..5708e3a9a0 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -89,6 +89,7 @@ func NewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128, err erro // Bytes returns the Uint128 in little endian format by default. A variadic paramter // order can be used to specify the binary.ByteOrder used func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { + fmt.Println("called bytes!") var o binary.ByteOrder = binary.LittleEndian if len(order) > 0 { o = order[0] diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index 11208c0189..516e2f37bd 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -4,6 +4,9 @@ import ( "math/big" "reflect" "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" ) type VDTValue struct { @@ -193,8 +196,8 @@ var VariableDataTypeTests = tests{ // encoding of struct []byte{ 0xfe, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, - 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, 0x01, 0x01, 0xff, 0x3f, @@ -213,8 +216,8 @@ var VariableDataTypeTests = tests{ // encoding of struct []byte{ 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x01, 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, - 0x01, 0xff, 0xff, 0xff, 0x3f, 0, 0, 0, 0, + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x01, 0xfe, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0xff, 0x3f, @@ -280,8 +283,9 @@ func Test_decodeState_decodeVaryingDataType(t *testing.T) { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + diff := cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) } }) } From bbc787a22ad6bfb6136d27228b26ffdf3eaded07 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 21:42:36 -0400 Subject: [PATCH 019/245] cleanup, and all tests benchmark --- pkg/scale/comparison_test.go | 52 ++++++++++++++++------------- pkg/scale/decode.go | 3 ++ pkg/scale/encode.go | 1 - pkg/scale/encode_test.go | 4 +-- pkg/scale/uint128.go | 1 - pkg/scale/varying_data_type_test.go | 6 ++-- 6 files changed, 36 insertions(+), 31 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 29d9ec5e39..fda57a4dea 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -1,15 +1,11 @@ -package scale_test +package scale import ( - "fmt" - "math/big" "reflect" "testing" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" - oldScale "github.com/ChainSafe/gossamer/lib/scale" - "github.com/ChainSafe/gossamer/pkg/scale" ) // ChangesTrieRootDigest contains the root of the changes trie at a given block, if the runtime supports it. @@ -75,8 +71,8 @@ func TestOldVsNewEncoding(t *testing.T) { return } - type Digests scale.VaryingDataType - err = scale.RegisterVaryingDataType(Digests{}, ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}) + type Digests VaryingDataType + err = RegisterVaryingDataType(Digests{}, ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}) if err != nil { t.Errorf("unexpected err: %v", err) return @@ -99,7 +95,7 @@ func TestOldVsNewEncoding(t *testing.T) { }, } - newEncode, err := scale.Marshal(newDigest) + newEncode, err := Marshal(newDigest) if err != nil { t.Errorf("unexpected err: %v", err) return @@ -109,7 +105,7 @@ func TestOldVsNewEncoding(t *testing.T) { } var decoded Digests - err = scale.Unmarshal(newEncode, &decoded) + err = Unmarshal(newEncode, &decoded) if err != nil { t.Errorf("unexpected err: %v", err) } @@ -118,20 +114,28 @@ func TestOldVsNewEncoding(t *testing.T) { } } -func TestSomething(t *testing.T) { - i := big.NewInt(0) - expectedVal := *common.Uint128FromBigInt(i) - - encByts, err := oldScale.Encode(expectedVal) - if err != nil { - t.Errorf("%v", err) - return - } - encBytes2, err := oldScale.Encode(i) - if err != nil { - t.Errorf("%v", err) - return +func BenchmarkUnmarshal(b *testing.B) { + for _, tt := range allTests { + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } } - - fmt.Printf("%+v, %+v", encByts, encBytes2) } + +// func BenchmarkDecode(b *testing.B) { +// for _, tt := range variableWidthIntegerTests { +// dst := reflect.New(reflect.TypeOf(tt.in)).Interface() +// fmt.Printf("%v %T\n", dst, dst) +// // if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { +// // b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// // return +// // } +// _, err := oldScale.Decode(tt.want, dst) +// if err != nil { +// b.Errorf("%v", err) +// return +// } +// } +// } diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 378f1cb0a3..bd2f5382ae 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -454,6 +454,9 @@ func (ds *decodeState) decodeFixedWidthInt(dstv reflect.Value) (err error) { break } out = uint(binary.LittleEndian.Uint64(buf)) + default: + err = fmt.Errorf("invalid type: %T", in) + return } dstv.Set(reflect.ValueOf(out)) return diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index e5bfb23c5e..b640f5d29b 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -381,6 +381,5 @@ func (es *encodeState) encodeUint(i uint) (err error) { // encodeUint128 encodes a Uint128 func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) - fmt.Println("bytes", es.Bytes(), i.Bytes()) return } diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index e23de7b9e3..9682c45da3 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -756,8 +756,8 @@ var ( } allTests = newTests( - fixedWidthIntegerTests, variableWidthIntegerTests, bigIntTests, stringTests, - boolTests, structTests, sliceTests, arrayTests, + fixedWidthIntegerTests, variableWidthIntegerTests, stringTests, + boolTests, structTests, sliceTests, arrayTests, varyingDataTypeTests, ) ) diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index 5708e3a9a0..fd9f441964 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -89,7 +89,6 @@ func NewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128, err erro // Bytes returns the Uint128 in little endian format by default. A variadic paramter // order can be used to specify the binary.ByteOrder used func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { - fmt.Println("called bytes!") var o binary.ByteOrder = binary.LittleEndian if len(order) > 0 { o = order[0] diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index 516e2f37bd..e7caf5c6c8 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -91,7 +91,7 @@ func init() { } } -var VariableDataTypeTests = tests{ +var varyingDataTypeTests = tests{ { in: testVDT{ VDTValue1{ @@ -262,7 +262,7 @@ var VariableDataTypeTests = tests{ } func Test_encodeState_encodeVaryingDataType(t *testing.T) { - for _, tt := range VariableDataTypeTests { + for _, tt := range varyingDataTypeTests { t.Run(tt.name, func(t *testing.T) { es := &encodeState{fieldScaleIndicesCache: cache} if err := es.marshal(tt.in); (err != nil) != tt.wantErr { @@ -276,7 +276,7 @@ func Test_encodeState_encodeVaryingDataType(t *testing.T) { } func Test_decodeState_decodeVaryingDataType(t *testing.T) { - for _, tt := range append(tests{}, VariableDataTypeTests...) { + for _, tt := range varyingDataTypeTests { t.Run(tt.name, func(t *testing.T) { dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { From 942ea32064de5ededefbd1fc1220b2050cd60432 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Jun 2021 15:13:11 -0400 Subject: [PATCH 020/245] add README --- pkg/scale/README.md | 230 ++++++++++++++++++++++++++++++++++++++++++++ pkg/scale/decode.go | 3 + pkg/scale/encode.go | 3 + 3 files changed, 236 insertions(+) create mode 100644 pkg/scale/README.md diff --git a/pkg/scale/README.md b/pkg/scale/README.md new file mode 100644 index 0000000000..a29145fd92 --- /dev/null +++ b/pkg/scale/README.md @@ -0,0 +1,230 @@ +# go-scale Codec + +Go implementation of the SCALE (Simple Concatenated Aggregate Little-Endian) data format for types used in the Parity Substrate framework. + +SCALE is a light-weight format which allows encoding (and decoding) which makes it highly suitable for resource-constrained execution environments like blockchain runtimes and low-power, low-memory devices. + +It is important to note that the encoding context (knowledge of how the types and data structures look) needs to be known separately at both encoding and decoding ends. The encoded data does not include this contextual information. + +This codec attempts to translate the primitive Go types to the associated types in SCALE. It also introduces a few custom types to implement SCALE primitives that have no direct translation to a Go primmitive type. + +## Translating From SCALE to Go + +Goavro does not use Go's structure tags to translate data between +native Go types and Avro encoded data. + +When translating from SCALE to native Go data, +go-scale returns primitive Go data values for corresponding SCALE data +values. The table below shows how go-scale translates SCALE types to Go. + +### Primitives + +| SCALE/Rust | Go | +| ------------------ | ------------------------ | +| `i8` | `int8` | +| `u8` | `uint8` | +| `i16` | `int16` | +| `u16` | `uint16` | +| `i32` | `int32` | +| `u32` | `uint32` | +| `i64` | `int64` | +| `u64` | `uint64` | +| `i128` | `*big.Int` | +| `u128` | `*scale.Uint128` | +| `bytes` | `[]byte` | +| `string` | `string` | +| `enum` | `scale.VaryingDataType` | +| `struct` | `struct` | + +### Structs + +When decoding SCALE data knowledge of the structure of the destination data type is required to decode. Structs are encoded as a SCALE Tuple, where each struct field is encoded in the sequence of the fields. + +#### Struct Tags + +go-scale uses `scale` struct tag do modify the order of the field values during encoding. This is also used when decoding attributes back to the original type. This essentially allows you to modify struct field ordering but preserve the encoding/decoding functionality. + +See the [usage example](#Struct-Tag-Example). + +### Option + +For all `Option` a pointer to the underlying type is used in go-scale. In the `None` case the pointer value is `nil`. + +| SCALE/Rust | Go | +| ------------------ | ------------------------ | +| `Option` | `*int8` | +| `Option` | `*uint8` | +| `Option` | `*int16` | +| `Option` | `*uint16` | +| `Option` | `*int32` | +| `Option` | `*uint32` | +| `Option` | `*int64` | +| `Option` | `*uint64` | +| `Option` | `**big.Int` | +| `Option` | `**scale.Uint128` | +| `Option` | `*[]byte` | +| `Option` | `*string` | +| `Option` | `*scale.VaryingDataType` | +| `Option` | `*struct` | +| `None` | `nil` | + +### Compact Encoding + +SCALE uses a compact encoding for variable width unsigned integers. + +| SCALE/Rust | Go | +| ------------------ | ------------------------ | +| `Compact` | `uint` | +| `Compact` | `*uint16` | +| `Compact` | `*uint32` | +| `Compact` | `*uint64` | +| `Compact` | `*big.Int` | + +### Result + +To be implemented and documented. + +## Usage + +### Basic Example + +Basic example which encodes and decodes a `uint`. +``` +import ( + "fmt" + "github.com/ChainSafe/gossamer/pkg/scale" +) + +func basicExample() { + // compact length encoded uint + var ui uint = 999 + bytes, err := scale.Marshal(ui) + if err != nil { + panic(err) + } + + var unmarshaled uint + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // 999 + fmt.Printf("%d", unmarshaled) +} +``` + +### Struct Tag Example + +Use the `scale` struct tag for struct fields to conform to specific encoding sequence of struct field values. A struct tag of `"-"` will be omitted from encoding and decoding. + +``` +import ( + "fmt" + "github.com/ChainSafe/gossamer/pkg/scale" +) + +func structExample() { + type MyStruct struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + Ignored int64 `scale:"-"` + } + var ms = MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + Ignored: 999 + } + bytes, err := scale.Marshal(ms) + if err != nil { + panic(err) + } + + var unmarshaled MyStruct + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // {Baz:true Bar:999 Foo:[1 2] Ignored:0} + fmt.Printf("%+v", unmarshaled) +} +``` + +### Varying Data Type + +A VaryingDataType is analogous to a Rust enum. A VaryingDataType needs to be registered using the `RegisterVaryingDataType` function with it's associated `VaryingDataTypeValue` types. `VaryingDataTypeValue` is an +interface with one `Index() uint` method that needs to be implemented. The returned `uint` index should be unique per type and needs to be the same index as defined in the Rust enum. + +``` +import ( + "fmt" + "testing" + + "github.com/ChainSafe/gossamer/pkg/scale" +) + +type MyStruct struct { + Baz bool + Bar uint32 + Foo []byte +} + +func (ms MyStruct) Index() uint { + return 1 +} + +type MyOtherStruct struct { + Foo string + Bar uint64 + Baz uint +} + +func (mos MyOtherStruct) Index() uint { + return 2 +} + +type MyInt16 int16 + +func (mi16 MyInt16) Index() uint { + return 3 +} + +type MyVaryingDataType scale.VaryingDataType + +func varyingDataTypeExample() { + err := scale.RegisterVaryingDataType(MyVaryingDataType{}, MyStruct{}, MyOtherStruct{}, MyInt16(0)) + if err != nil { + panic(err) + } + + mvdt := MyVaryingDataType{ + MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + }, + MyOtherStruct{ + Foo: "hello", + Bar: 999, + Baz: 888, + }, + MyInt16(111), + } + bytes, err := scale.Marshal(mvdt) + if err != nil { + panic(err) + } + + var unmarshaled MyVaryingDataType + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // [{Baz:true Bar:999 Foo:[1 2]} {Foo:hello Bar:999 Baz:888} 111] + fmt.Printf("%+v", unmarshaled) +} +``` \ No newline at end of file diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index bd2f5382ae..493c80a431 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -88,6 +88,9 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { if err != nil { break } + default: + err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + return } dstv.Set(temp.Elem().Convert(t)) default: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index b640f5d29b..9e88b1c08d 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -89,6 +89,9 @@ func (es *encodeState) marshal(in interface{}) (err error) { in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() case reflect.Int16: in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + default: + err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + return } err = es.marshal(in) default: From 62eb7296237807de95d5c59948810cf4be7595ac Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Jun 2021 15:15:10 -0400 Subject: [PATCH 021/245] update README --- pkg/scale/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/scale/README.md b/pkg/scale/README.md index a29145fd92..7e15e83627 100644 --- a/pkg/scale/README.md +++ b/pkg/scale/README.md @@ -75,9 +75,9 @@ SCALE uses a compact encoding for variable width unsigned integers. | SCALE/Rust | Go | | ------------------ | ------------------------ | | `Compact` | `uint` | -| `Compact` | `*uint16` | -| `Compact` | `*uint32` | -| `Compact` | `*uint64` | +| `Compact` | `uint` | +| `Compact` | `uint` | +| `Compact` | `uint` | | `Compact` | `*big.Int` | ### Result From 4c75bef3edbf74a35cec884d319a6a82accaa844 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Jun 2021 16:57:00 -0400 Subject: [PATCH 022/245] update readme --- pkg/scale/README.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pkg/scale/README.md b/pkg/scale/README.md index 7e15e83627..9e3e63ceda 100644 --- a/pkg/scale/README.md +++ b/pkg/scale/README.md @@ -6,13 +6,10 @@ SCALE is a light-weight format which allows encoding (and decoding) which makes It is important to note that the encoding context (knowledge of how the types and data structures look) needs to be known separately at both encoding and decoding ends. The encoded data does not include this contextual information. -This codec attempts to translate the primitive Go types to the associated types in SCALE. It also introduces a few custom types to implement SCALE primitives that have no direct translation to a Go primmitive type. +This codec attempts to translate the primitive Go types to the associated types in SCALE. It also introduces a few custom types to implement SCALE primitives that have no direct translation to a Go primitive type. ## Translating From SCALE to Go -Goavro does not use Go's structure tags to translate data between -native Go types and Avro encoded data. - When translating from SCALE to native Go data, go-scale returns primitive Go data values for corresponding SCALE data values. The table below shows how go-scale translates SCALE types to Go. @@ -38,11 +35,11 @@ values. The table below shows how go-scale translates SCALE types to Go. ### Structs -When decoding SCALE data knowledge of the structure of the destination data type is required to decode. Structs are encoded as a SCALE Tuple, where each struct field is encoded in the sequence of the fields. +When decoding SCALE data, knowledge of the structure of the destination data type is required to decode. Structs are encoded as a SCALE Tuple, where each struct field is encoded in the sequence of the fields. #### Struct Tags -go-scale uses `scale` struct tag do modify the order of the field values during encoding. This is also used when decoding attributes back to the original type. This essentially allows you to modify struct field ordering but preserve the encoding/decoding functionality. +go-scale uses a `scale` struct tag to modify the order of the field values during encoding. This is also used when decoding attributes back to the original type. This essentially allows you to modify struct field ordering but preserve the encoding/decoding ordering. See the [usage example](#Struct-Tag-Example). @@ -156,7 +153,7 @@ func structExample() { ### Varying Data Type A VaryingDataType is analogous to a Rust enum. A VaryingDataType needs to be registered using the `RegisterVaryingDataType` function with it's associated `VaryingDataTypeValue` types. `VaryingDataTypeValue` is an -interface with one `Index() uint` method that needs to be implemented. The returned `uint` index should be unique per type and needs to be the same index as defined in the Rust enum. +interface with one `Index() uint` method that needs to be implemented. The returned `uint` index should be unique per type and needs to be the same index as defined in the Rust enum to ensure interopability. ``` import ( From 60a74be1444c1ae1182ac4b241affec86ca955f7 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Jun 2021 16:59:31 -0400 Subject: [PATCH 023/245] update readme --- pkg/scale/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/scale/README.md b/pkg/scale/README.md index 9e3e63ceda..c868c9f063 100644 --- a/pkg/scale/README.md +++ b/pkg/scale/README.md @@ -106,7 +106,7 @@ func basicExample() { panic(err) } - // 999 + // 999 fmt.Printf("%d", unmarshaled) } ``` @@ -126,13 +126,13 @@ func structExample() { Baz bool `scale:"3"` Bar int32 `scale:"2"` Foo []byte `scale:"1"` - Ignored int64 `scale:"-"` + Ignored int64 `scale:"-"` } var ms = MyStruct{ Baz: true, Bar: 999, Foo: []byte{1, 2}, - Ignored: 999 + Ignored: 999 } bytes, err := scale.Marshal(ms) if err != nil { From 5801f197f19d5b65ef04311e4b64ad393c8bf41b Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 3 Jun 2021 10:30:08 -0400 Subject: [PATCH 024/245] update README --- pkg/scale/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/scale/README.md b/pkg/scale/README.md index c868c9f063..787d765c52 100644 --- a/pkg/scale/README.md +++ b/pkg/scale/README.md @@ -158,8 +158,6 @@ interface with one `Index() uint` method that needs to be implemented. The retu ``` import ( "fmt" - "testing" - "github.com/ChainSafe/gossamer/pkg/scale" ) From 215cc10eb6f9108067a7233a4730ac5cf76e255e Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 3 Jun 2021 10:35:11 -0400 Subject: [PATCH 025/245] update README --- pkg/scale/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/scale/README.md b/pkg/scale/README.md index 787d765c52..b8b60ac003 100644 --- a/pkg/scale/README.md +++ b/pkg/scale/README.md @@ -79,7 +79,7 @@ SCALE uses a compact encoding for variable width unsigned integers. ### Result -To be implemented and documented. +> TODO: To be implemented and documented. ## Usage @@ -152,8 +152,10 @@ func structExample() { ### Varying Data Type -A VaryingDataType is analogous to a Rust enum. A VaryingDataType needs to be registered using the `RegisterVaryingDataType` function with it's associated `VaryingDataTypeValue` types. `VaryingDataTypeValue` is an -interface with one `Index() uint` method that needs to be implemented. The returned `uint` index should be unique per type and needs to be the same index as defined in the Rust enum to ensure interopability. +A `VaryingDataType` is analogous to a Rust enum. A `VaryingDataType` needs to be registered using the `RegisterVaryingDataType` function with its associated `VaryingDataTypeValue` types. `VaryingDataTypeValue` is an +interface with one `Index() uint` method that needs to be implemented. The returned `uint` index should be unique per type and needs to be the same index as defined in the Rust enum to ensure interopability. + +> TODO: The only custom `VaryingDataTypeValue` types supported are currently `struct`, `int`, and `int16`. Need to add other supported primitives. ``` import ( From 2aae887eb4f086366ff8dbf0d4c068a485120c63 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 3 Jun 2021 15:34:36 -0400 Subject: [PATCH 026/245] rResult encode/decode and RegisterResult --- pkg/scale/decode.go | 60 +++++++++++++++++++++++++++++++++++++++- pkg/scale/encode.go | 54 ++++++++++++++++++++++++++++-------- pkg/scale/result.go | 41 +++++++++++++++++++++++++++ pkg/scale/result_test.go | 57 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 200 insertions(+), 12 deletions(-) create mode 100644 pkg/scale/result.go create mode 100644 pkg/scale/result_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 493c80a431..826d445819 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -56,7 +56,14 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Ptr: err = ds.decodePointer(dstv) case reflect.Struct: - err = ds.decodeStruct(dstv) + t := reflect.TypeOf(in) + // check if this is a convertible to Result, if so encode using decodeResult + switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + case true: + err = ds.decodeResult(dstv) + case false: + err = ds.decodeStruct(dstv) + } case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: @@ -101,6 +108,57 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { return } +func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { + dstt := reflect.TypeOf(dstv.Interface()) + key := fmt.Sprintf("%s.%s", dstt.PkgPath(), dstt.Name()) + resultCases, ok := resCache[key] + if !ok { + err = fmt.Errorf("unable to find registered custom Result: %T", dstv.Interface()) + return + } + + var rb byte + rb, err = ds.ReadByte() + if err != nil { + return + } + switch rb { + case 0x00: + // Ok case + okIn, ok := resultCases[true] + if !ok { + err = fmt.Errorf("unable to find registered custom Result.Ok for: %T", dstv.Interface()) + return + } + newOk := reflect.New(reflect.TypeOf(okIn)) + err = ds.unmarshal(newOk.Elem()) + if err != nil { + break + } + res := reflect.New(dstt) + res.Elem().FieldByName("Ok").Set(newOk.Elem()) + dstv.Set(res.Elem()) + case 0x01: + // Error case + errIn, ok := resultCases[false] + if !ok { + err = fmt.Errorf("unable to find registered custom Result.Err for: %T", dstv.Interface()) + return + } + newErr := reflect.New(reflect.TypeOf(errIn)) + err = ds.unmarshal(newErr.Elem()) + if err != nil { + break + } + res := reflect.New(dstt) + res.Elem().FieldByName("Err").Set(newErr.Elem()) + dstv.Set(res.Elem()) + default: + err = fmt.Errorf("unsupported Result value: %v, bytes: %v", rb, ds.Bytes()) + } + return +} + func (ds *decodeState) decodePointer(dstv reflect.Value) (err error) { var rb byte rb, err = ds.ReadByte() diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 9e88b1c08d..1076a008ce 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/binary" "fmt" - "log" "math/big" "reflect" ) @@ -60,7 +59,16 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: - err = es.encodeStruct(in) + t := reflect.TypeOf(in) + // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType + switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + case true: + resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) + err = es.encodeResult(resv.Interface().(Result)) + case false: + err = es.encodeStruct(in) + } + case reflect.Array: err = es.encodeArray(in) case reflect.Slice: @@ -69,12 +77,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { case true: invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) - switch in := invdt.Interface().(type) { - case VaryingDataType: - err = es.encodeVaryingDataType(in) - default: - log.Panicf("this should never happen") - } + err = es.encodeVaryingDataType(invdt.Interface().(VaryingDataType)) case false: err = es.encodeSlice(in) } @@ -103,18 +106,47 @@ func (es *encodeState) marshal(in interface{}) (err error) { return } +func (es *encodeState) encodeResult(res Result) (err error) { + err = res.Validate() + if err != nil { + return + } + + switch res.Ok { + case nil: + // error case + err = es.WriteByte(1) + if err != nil { + break + } + // TODO: type checking of res.Err against resultCache to ensure Err is same as + // registered type + err = es.marshal(res.Err) + default: + err = es.WriteByte(0) + if err != nil { + break + } + // TODO: type checking of res.Ok against resultCache to ensure Err is same as + // registered type + err = es.marshal(res.Ok) + } + return +} + func (es *encodeState) encodeVaryingDataType(values VaryingDataType) (err error) { err = es.encodeLength(len(values)) if err != nil { return } - for _, t := range values { + for _, val := range values { + // TODO: type checking of val against vdtCache to ensure it is a registered type // encode type.Index (idx) for varying data type - err = es.WriteByte(byte(t.Index())) + err = es.WriteByte(byte(val.Index())) if err != nil { return } - err = es.marshal(t) + err = es.marshal(val) } return } diff --git a/pkg/scale/result.go b/pkg/scale/result.go new file mode 100644 index 0000000000..f6c78b7e79 --- /dev/null +++ b/pkg/scale/result.go @@ -0,0 +1,41 @@ +package scale + +import ( + "fmt" + "reflect" +) + +type resultCache map[string]map[bool]interface{} +type Result struct { + Ok interface{} + Err interface{} +} + +// Validate ensures Result is valid. Only one of the Ok and Err attributes should be nil +func (r Result) Validate() (err error) { + switch { + case r.Ok == nil && r.Err != nil, r.Ok != nil && r.Err == nil: + default: + err = fmt.Errorf("Result is invalid: %+v", r) + } + return +} + +var resCache resultCache = make(resultCache) + +func RegisterResult(in interface{}, inOK interface{}, inErr interface{}) (err error) { + t := reflect.TypeOf(in) + if !t.ConvertibleTo(reflect.TypeOf(Result{})) { + err = fmt.Errorf("%T is not a Result", in) + return + } + + key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) + _, ok := resCache[key] + if !ok { + resCache[key] = make(map[bool]interface{}) + } + resCache[key][true] = inOK + resCache[key][false] = inErr + return +} diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go new file mode 100644 index 0000000000..81cf3ed9b0 --- /dev/null +++ b/pkg/scale/result_test.go @@ -0,0 +1,57 @@ +package scale + +import ( + "reflect" + "testing" +) + +type MyResult Result + +func TestEncodeDecodeResult(t *testing.T) { + err := RegisterResult(MyResult{}, MyStruct{}, false) + if err != nil { + t.Errorf("%v", err) + } + + ms := MyStruct{ + Foo: []byte{0x01}, + Bar: 2, + Baz: true, + } + mr := MyResult{Ok: ms} + bytes, err := Marshal(mr) + if err != nil { + t.Errorf("%v", err) + } + + if !reflect.DeepEqual([]byte{0x00, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, bytes) { + t.Errorf("unexpected bytes: %v", bytes) + } + + mr1 := MyResult{Err: true} + bytes, err = Marshal(mr1) + if err != nil { + t.Errorf("%v", err) + } + if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { + t.Errorf("unexpected bytes: %v", bytes) + } + + mr2 := MyResult{} + err = Unmarshal([]byte{0x00, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, &mr2) + if err != nil { + t.Errorf("%v", err) + } + if !reflect.DeepEqual(MyResult{Ok: ms}, mr2) { + t.Errorf("unexpected MyResult") + } + + mr3 := MyResult{} + err = Unmarshal([]byte{0x01, 0x01}, &mr3) + if err != nil { + t.Errorf("%v", err) + } + if !reflect.DeepEqual(MyResult{Err: true}, mr3) { + t.Errorf("unexpected MyResult yo") + } +} From 40c2728c34216457e6c3721903fe14ad24dbd422 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 4 Jun 2021 15:26:42 -0400 Subject: [PATCH 027/245] wip cr feedback --- pkg/scale/README.md | 2 +- pkg/scale/decode.go | 5 +-- pkg/scale/scale.go | 10 +++--- pkg/scale/scale_test.go | 73 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 pkg/scale/scale_test.go diff --git a/pkg/scale/README.md b/pkg/scale/README.md index b8b60ac003..6e1e3107d0 100644 --- a/pkg/scale/README.md +++ b/pkg/scale/README.md @@ -6,7 +6,7 @@ SCALE is a light-weight format which allows encoding (and decoding) which makes It is important to note that the encoding context (knowledge of how the types and data structures look) needs to be known separately at both encoding and decoding ends. The encoded data does not include this contextual information. -This codec attempts to translate the primitive Go types to the associated types in SCALE. It also introduces a few custom types to implement SCALE primitives that have no direct translation to a Go primitive type. +This codec attempts to translate the primitive Go types to the associated types in SCALE. It also introduces a few custom types to implement Rust primitives that have no direct translation to a Go primitive type. ## Translating From SCALE to Go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 826d445819..e82567ad60 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -68,7 +68,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { err = ds.decodeArray(dstv) case reflect.Slice: t := reflect.TypeOf(in) - // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType + // check if this is a convertible to VaryingDataType, if so decode using encodeVaryingDataType switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { case true: err = ds.decodeVaryingDataType(dstv) @@ -258,7 +258,8 @@ func (ds *decodeState) decodeArray(dstv reflect.Value) (err error) { return } -// decodeStruct comment about this +// decodeStruct decodes a byte array representing a SCALE tuple. The order of data is +// determined by the source tuple in rust, or the struct field order in a go struct func (ds *decodeState) decodeStruct(dstv reflect.Value) (err error) { in := dstv.Interface() _, indices, err := cache.fieldScaleIndices(in) diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index f96b8bd8c9..d7f29156e8 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -20,7 +20,7 @@ type fieldScaleIndex struct { } type fieldScaleIndices []fieldScaleIndex -// fieldScaleIndic +// fieldScaleIndicesCache stores the order of the fields per struct type fieldScaleIndicesCache struct { cache map[string]fieldScaleIndices sync.RWMutex @@ -78,9 +78,11 @@ func (fsic *fieldScaleIndicesCache) fieldScaleIndices(in interface{}) (v reflect return false }) - fsic.Lock() - fsic.cache[key] = indices - fsic.Unlock() + if key != "." { + fsic.Lock() + fsic.cache[key] = indices + fsic.Unlock() + } return } diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go new file mode 100644 index 0000000000..a5bdac2364 --- /dev/null +++ b/pkg/scale/scale_test.go @@ -0,0 +1,73 @@ +package scale + +import ( + "reflect" + "testing" +) + +func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { + tests := []struct { + name string + in interface{} + wantIndices fieldScaleIndices + wantErr bool + }{ + // TODO: Add test cases. + { + in: struct{ Foo int }{}, + wantIndices: fieldScaleIndices{ + { + fieldIndex: 0, + }, + }, + }, + { + in: struct { + End1 bool + Baz bool `scale:"3"` + End2 []byte + Bar int32 `scale:"2"` + End3 []byte + Foo []byte `scale:"1"` + }{}, + wantIndices: fieldScaleIndices{ + { + fieldIndex: 5, + scaleIndex: newStringPtr("1"), + }, + { + fieldIndex: 3, + scaleIndex: newStringPtr("2"), + }, + { + fieldIndex: 1, + scaleIndex: newStringPtr("3"), + }, + { + fieldIndex: 0, + }, + { + fieldIndex: 2, + }, + { + fieldIndex: 4, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fsic := &fieldScaleIndicesCache{ + cache: make(map[string]fieldScaleIndices), + } + _, gotIndices, err := fsic.fieldScaleIndices(tt.in) + if (err != nil) != tt.wantErr { + t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotIndices, tt.wantIndices) { + t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() gotIndices = %v, want %v", gotIndices, tt.wantIndices) + } + }) + } +} From 3c836b63e659ce94a2fa3e717e1f241343253769 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 7 Jun 2021 14:57:50 -0400 Subject: [PATCH 028/245] add licenses --- pkg/scale/comparison_test.go | 32 ++++++++++++++--------------- pkg/scale/decode.go | 16 +++++++++++++++ pkg/scale/decode_test.go | 16 +++++++++++++++ pkg/scale/encode.go | 16 +++++++++++++++ pkg/scale/encode_test.go | 16 +++++++++++++++ pkg/scale/result.go | 16 +++++++++++++++ pkg/scale/result_test.go | 16 +++++++++++++++ pkg/scale/scale.go | 16 +++++++++++++++ pkg/scale/scale_test.go | 16 +++++++++++++++ pkg/scale/uint128.go | 1 + pkg/scale/uint128_test.go | 1 + pkg/scale/varying_data_type.go | 16 +++++++++++++++ pkg/scale/varying_data_type_test.go | 16 +++++++++++++++ 13 files changed, 178 insertions(+), 16 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index fda57a4dea..a413cdd8a4 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( @@ -123,19 +139,3 @@ func BenchmarkUnmarshal(b *testing.B) { } } } - -// func BenchmarkDecode(b *testing.B) { -// for _, tt := range variableWidthIntegerTests { -// dst := reflect.New(reflect.TypeOf(tt.in)).Interface() -// fmt.Printf("%v %T\n", dst, dst) -// // if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { -// // b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// // return -// // } -// _, err := oldScale.Decode(tt.want, dst) -// if err != nil { -// b.Errorf("%v", err) -// return -// } -// } -// } diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index e82567ad60..8636fd4dc4 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 4929c15e18..b8375374b9 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 1076a008ce..e5bab37578 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 9682c45da3..cff5820ba5 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/result.go b/pkg/scale/result.go index f6c78b7e79..2d3b31465a 100644 --- a/pkg/scale/result.go +++ b/pkg/scale/result.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 81cf3ed9b0..6750206d05 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index d7f29156e8..855016b8ca 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index a5bdac2364..632ab804d4 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index fd9f441964..861d9872b0 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -13,6 +13,7 @@ // // You should have received a copy of the GNU Lesser General Public License // along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/uint128_test.go b/pkg/scale/uint128_test.go index 9936e74de6..af3c51a8df 100644 --- a/pkg/scale/uint128_test.go +++ b/pkg/scale/uint128_test.go @@ -13,6 +13,7 @@ // // You should have received a copy of the GNU Lesser General Public License // along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/varying_data_type.go b/pkg/scale/varying_data_type.go index 05f82d36be..8c2bf36f67 100644 --- a/pkg/scale/varying_data_type.go +++ b/pkg/scale/varying_data_type.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index e7caf5c6c8..78200cf3f5 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( From 416321e4377f20e0f2aef1bbaee09e8da9b8da8a Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 7 Jun 2021 17:04:20 -0400 Subject: [PATCH 029/245] add custom primitive encode/decode --- pkg/scale/decode.go | 120 +++++++++++++++++++++++++++++++++----------- pkg/scale/encode.go | 73 ++++++++++++++++++++------- 2 files changed, 147 insertions(+), 46 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 8636fd4dc4..0bf828a260 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -68,7 +68,12 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case bool: err = ds.decodeBool(dstv) default: - switch reflect.TypeOf(in).Kind() { + t := reflect.TypeOf(in) + switch t.Kind() { + case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, + reflect.Int32, reflect.Int64, reflect.String, reflect.Uint, + reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + err = ds.decodeCustomPrimitive(dstv) case reflect.Ptr: err = ds.decodePointer(dstv) case reflect.Struct: @@ -92,35 +97,94 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { err = ds.decodeSlice(dstv) } default: - _, ok := in.(VaryingDataTypeValue) - switch ok { - case true: - var temp reflect.Value - t := reflect.TypeOf(in) - switch t.Kind() { - // TODO: support more primitive types. Do we need to support arrays and slices as well? - case reflect.Int: - temp = reflect.New(reflect.TypeOf(int(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int16: - temp = reflect.New(reflect.TypeOf(int16(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - default: - err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - return - } - dstv.Set(temp.Elem().Convert(t)) - default: - err = fmt.Errorf("unsupported type: %T", in) - } + err = fmt.Errorf("unsupported type: %T", in) + } + } + return +} + +func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { + in := dstv.Interface() + inType := reflect.TypeOf(in) + var temp reflect.Value + switch inType.Kind() { + case reflect.Bool: + temp = reflect.New(reflect.TypeOf(false)) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int: + temp = reflect.New(reflect.TypeOf(int(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int8: + temp = reflect.New(reflect.TypeOf(int8(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int16: + temp = reflect.New(reflect.TypeOf(int16(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int32: + temp = reflect.New(reflect.TypeOf(int32(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break } + case reflect.Int64: + temp = reflect.New(reflect.TypeOf(int64(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.String: + temp = reflect.New(reflect.TypeOf("")) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint: + temp = reflect.New(reflect.TypeOf(uint(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint8: + temp = reflect.New(reflect.TypeOf(uint8(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint16: + temp = reflect.New(reflect.TypeOf(uint16(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint32: + temp = reflect.New(reflect.TypeOf(uint32(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint64: + temp = reflect.New(reflect.TypeOf(uint64(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + default: + err = fmt.Errorf("unsupported type for custom primitive: %T", in) + return } + dstv.Set(temp.Elem().Convert(inType)) return } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index e5bab37578..fafc2eb76e 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -61,6 +61,10 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeBool(in) default: switch reflect.TypeOf(in).Kind() { + case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, + reflect.Int32, reflect.Int64, reflect.String, reflect.Uint, + reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + err = es.encodeCustomPrimitive(in) case reflect.Ptr: // Assuming that anything that is a pointer is an Option to capture {nil, T} elem := reflect.ValueOf(in).Elem() @@ -98,30 +102,63 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - _, ok := in.(VaryingDataTypeValue) - switch ok { - case true: - t := reflect.TypeOf(in) - switch t.Kind() { - // TODO: support more primitive types. Do we need to support arrays and slices as well? - case reflect.Int: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() - case reflect.Int16: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() - default: - err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - return - } - err = es.marshal(in) - default: - err = fmt.Errorf("unsupported type: %T", in) - } + // _, ok := in.(VaryingDataTypeValue) + // switch ok { + // case true: + // t := reflect.TypeOf(in) + // switch t.Kind() { + // // TODO: support more primitive types. Do we need to support arrays and slices as well? + // case reflect.Int: + // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + // case reflect.Int16: + // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + // default: + // err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + // return + // } + // err = es.marshal(in) + // default: + err = fmt.Errorf("unsupported type: %T", in) + // } } } return } +func (es *encodeState) encodeCustomPrimitive(in interface{}) (err error) { + switch reflect.TypeOf(in).Kind() { + case reflect.Bool: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(false)).Interface() + case reflect.Int: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(0))).Interface() + case reflect.Int8: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int8(0))).Interface() + case reflect.Int16: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(0))).Interface() + case reflect.Int32: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int32(0))).Interface() + case reflect.Int64: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int64(0))).Interface() + case reflect.String: + in = reflect.ValueOf(in).Convert(reflect.TypeOf("")).Interface() + case reflect.Uint: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(uint(0))).Interface() + case reflect.Uint8: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(uint8(0))).Interface() + case reflect.Uint16: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(uint16(0))).Interface() + case reflect.Uint32: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(uint32(0))).Interface() + case reflect.Uint64: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(uint64(0))).Interface() + default: + err = fmt.Errorf("unsupported type for custom primitive: %T", in) + return + } + err = es.marshal(in) + return +} func (es *encodeState) encodeResult(res Result) (err error) { err = res.Validate() if err != nil { From d6ba1fc5c89175f214bf410227036d8e53b28ceb Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 8 Jun 2021 13:57:33 -0400 Subject: [PATCH 030/245] more cr feedback --- pkg/scale/comparison_test.go | 24 +++++-- pkg/scale/decode.go | 14 ---- pkg/scale/decode_test.go | 55 ++++++++++++--- pkg/scale/encode.go | 130 ++++------------------------------- pkg/scale/encode_test.go | 122 ++++++++++++++++++++++++++++++-- pkg/scale/scale_test.go | 1 - pkg/scale/uint128.go | 14 ++-- 7 files changed, 205 insertions(+), 155 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index a413cdd8a4..a1da3697c2 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -131,11 +131,25 @@ func TestOldVsNewEncoding(t *testing.T) { } func BenchmarkUnmarshal(b *testing.B) { - for _, tt := range allTests { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return + for i := 0; i < b.N; i++ { + for _, tt := range allTests { + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + } + } +} + +func BenchmarkMarshal(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, tt := range allTests { + // dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if _, err := Marshal(tt.in); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } } } } diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 0bf828a260..7d7f48cf9f 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -582,20 +582,6 @@ func (ds *decodeState) decodeFixedWidthInt(dstv reflect.Value) (err error) { break } out = binary.LittleEndian.Uint64(buf) - case int: - buf := make([]byte, 8) - _, err = ds.Read(buf) - if err != nil { - break - } - out = int(binary.LittleEndian.Uint64(buf)) - case uint: - buf := make([]byte, 8) - _, err = ds.Read(buf) - if err != nil { - break - } - out = uint(binary.LittleEndian.Uint64(buf)) default: err = fmt.Errorf("invalid type: %T", in) return diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index b8375374b9..f8051b56da 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -85,7 +85,7 @@ func Test_decodeState_decodeBytes(t *testing.T) { func Test_decodeState_decodeBool(t *testing.T) { for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - var dst bool + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -153,13 +153,8 @@ func Test_unmarshal_optionality(t *testing.T) { want: t.want, out: t.out, } - switch t.in { - case nil: - // this doesn't actually happen since none of the tests have nil value for tt.in - ptrTest.want = []byte{0x00} - default: - ptrTest.want = append([]byte{0x01}, t.want...) - } + + ptrTest.want = append([]byte{0x01}, t.want...) ptrTests = append(ptrTests, ptrTest) } for _, tt := range ptrTests { @@ -183,3 +178,47 @@ func Test_unmarshal_optionality(t *testing.T) { }) } } + +func Test_unmarshal_optionality_nil_case(t *testing.T) { + var ptrTests tests + for _, t := range allTests { + ptrTest := test{ + name: t.name, + in: t.in, + wantErr: t.wantErr, + want: t.want, + // ignore out, since we are testing nil case + // out: t.out, + } + ptrTest.want = []byte{0x00} + + temp := reflect.New(reflect.TypeOf(t.in)) + // create a new pointer to type of temp + tempv := reflect.New(reflect.PtrTo(temp.Type()).Elem()) + // set zero value to elem of **temp so that is nil + tempv.Elem().Set(reflect.Zero(tempv.Elem().Type())) + // set test.in to *temp + ptrTest.in = tempv.Elem().Interface() + + ptrTests = append(ptrTests, ptrTest) + } + for _, tt := range ptrTests { + t.Run(tt.name, func(t *testing.T) { + // this becomes a pointer to a zero value of the underlying value + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface()) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) + } +} diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index fafc2eb76e..e7917d8d8c 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -102,25 +102,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - // _, ok := in.(VaryingDataTypeValue) - // switch ok { - // case true: - // t := reflect.TypeOf(in) - // switch t.Kind() { - // // TODO: support more primitive types. Do we need to support arrays and slices as well? - // case reflect.Int: - // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() - // case reflect.Int16: - // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() - // default: - // err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - // return - // } - // err = es.marshal(in) - // default: err = fmt.Errorf("unsupported type: %T", in) - // } - } } return @@ -204,91 +186,17 @@ func (es *encodeState) encodeVaryingDataType(values VaryingDataType) (err error) return } -func (es *encodeState) encodeSlice(t interface{}) (err error) { - switch arr := t.(type) { - // int is the only case that handles encoding differently. - // all other cases can recursively call es.marshal() - case []int: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.encodeUint(uint(elem)) - if err != nil { - return - } - } - // the cases below are to avoid using the reflect library - // for commonly used slices in gossamer - case []*big.Int: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.marshal(elem) - if err != nil { - return - } - } - case []bool: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.marshal(elem) - if err != nil { - return - } - } - case [][]byte: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.marshal(elem) - if err != nil { - return - } - } - case [][]int: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.marshal(elem) - if err != nil { - return - } - } - case []string: - err = es.encodeLength(len(arr)) - if err != nil { - return - } - for _, elem := range arr { - err = es.marshal(elem) - if err != nil { - return - } - } - default: - // use reflect package for any other cases - s := reflect.ValueOf(t) - err = es.encodeUint(uint(s.Len())) +func (es *encodeState) encodeSlice(in interface{}) (err error) { + v := reflect.ValueOf(in) + err = es.encodeLength(v.Len()) + if err != nil { + return + } + for i := 0; i < v.Len(); i++ { + err = es.marshal(v.Index(i).Interface()) if err != nil { return } - for i := 0; i < s.Len(); i++ { - err = es.marshal(s.Index(i).Interface()) - if err != nil { - return - } - } } return } @@ -298,19 +206,9 @@ func (es *encodeState) encodeSlice(t interface{}) (err error) { func (es *encodeState) encodeArray(in interface{}) (err error) { v := reflect.ValueOf(in) for i := 0; i < v.Len(); i++ { - elem := v.Index(i).Interface() - switch elem := elem.(type) { - case int: - // an array of unsized integers needs to be encoded using scale length encoding - err = es.encodeUint(uint(elem)) - if err != nil { - return - } - default: - err = es.marshal(v.Index(i).Interface()) - if err != nil { - return - } + err = es.marshal(v.Index(i).Interface()) + if err != nil { + return } } return @@ -362,7 +260,7 @@ func (es *encodeState) encodeBool(l bool) (err error) { // it writes to the buffer a byte array where the first byte is the length of b encoded with SCALE, followed by the // byte array b itself func (es *encodeState) encodeBytes(b []byte) (err error) { - err = es.encodeUint(uint(len(b))) + err = es.encodeLength(len(b)) if err != nil { return } @@ -390,10 +288,6 @@ func (es *encodeState) encodeFixedWidthInt(i interface{}) (err error) { err = binary.Write(es, binary.LittleEndian, uint64(i)) case uint64: err = binary.Write(es, binary.LittleEndian, i) - case int: - err = binary.Write(es, binary.LittleEndian, int64(i)) - case uint: - err = binary.Write(es, binary.LittleEndian, uint64(i)) default: err = fmt.Errorf("could not encode fixed width integer, invalid type: %T", i) } diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index cff5820ba5..b0d93d2f94 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -106,6 +106,20 @@ func newBoolPtr(in bool) (ptr *bool) { return } +type myCustomInt int +type myCustomInt8 int8 +type myCustomInt16 int16 +type myCustomInt32 int32 +type myCustomInt64 int64 +type myCustomUint uint +type myCustomUint8 uint8 +type myCustomUint16 uint16 +type myCustomUint32 uint32 +type myCustomUint64 uint64 +type myCustomBytes []byte +type myCustomString string +type myCustomBool bool + var ( intTests = tests{ { @@ -128,6 +142,11 @@ var ( in: int(9223372036854775807), want: []byte{19, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomInt(9223372036854775807)", + in: myCustomInt(9223372036854775807), + want: []byte{19, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } uintTests = tests{ { @@ -150,6 +169,11 @@ var ( in: uint(9223372036854775807), want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomUint(9223372036854775807)", + in: myCustomUint(9223372036854775807), + want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } variableWidthIntegerTests = newTests(intTests, uintTests) @@ -174,6 +198,11 @@ var ( in: int64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomInt64(9223372036854775807)", + in: myCustomInt64(9223372036854775807), + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } uint64Tests = tests{ { @@ -196,6 +225,11 @@ var ( in: uint64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomUint64(9223372036854775807)", + in: myCustomUint64(9223372036854775807), + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } int32Tests = tests{ { @@ -213,6 +247,11 @@ var ( in: int32(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f}, }, + { + name: "myCustomInt32(1073741823)", + in: myCustomInt32(1073741823), + want: []byte{0xff, 0xff, 0xff, 0x3f}, + }, } uint32Tests = tests{ { @@ -230,6 +269,11 @@ var ( in: uint32(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f}, }, + { + name: "uint32(1073741823)", + in: myCustomUint32(1073741823), + want: []byte{0xff, 0xff, 0xff, 0x3f}, + }, } int8Tests = tests{ { @@ -237,6 +281,11 @@ var ( in: int8(1), want: []byte{0x01}, }, + { + name: "myCustomInt8(1)", + in: myCustomInt8(1), + want: []byte{0x01}, + }, } uint8Tests = tests{ { @@ -244,6 +293,11 @@ var ( in: uint8(1), want: []byte{0x01}, }, + { + name: "myCustomInt8(1)", + in: myCustomUint8(1), + want: []byte{0x01}, + }, } int16Tests = tests{ { @@ -256,6 +310,11 @@ var ( in: int16(16383), want: []byte{0xff, 0x3f}, }, + { + name: "myCustomInt16(16383)", + in: myCustomInt16(16383), + want: []byte{0xff, 0x3f}, + }, } uint16Tests = tests{ { @@ -268,6 +327,11 @@ var ( in: uint16(16383), want: []byte{0xff, 0x3f}, }, + { + name: "myCustomUint16(16383)", + in: myCustomUint16(16383), + want: []byte{0xff, 0x3f}, + }, } fixedWidthIntegerTests = newTests( int8Tests, int16Tests, int32Tests, int64Tests, uint8Tests, uint16Tests, uint32Tests, uint64Tests, @@ -415,6 +479,18 @@ var ( want: append([]byte{0xDC}, testStrings[2]...), }, + { + name: "myCustomString(testStrings[0])", + in: myCustomString(testStrings[0]), + + want: append([]byte{0x0D, 0x01}, testStrings[0]...), + }, + { + name: "myCustomBytes(testStrings[0])", + in: myCustomBytes(testStrings[0]), + + want: append([]byte{0x0D, 0x01}, testStrings[0]...), + }, } boolTests = tests{ @@ -428,6 +504,11 @@ var ( in: true, want: []byte{0x01}, }, + { + name: "myCustomBool(true)", + in: myCustomBool(true), + want: []byte{0x01}, + }, } nilPtrMyStruct *MyStruct @@ -935,12 +1016,43 @@ func Test_marshal_optionality(t *testing.T) { wantErr: t.wantErr, want: t.want, } - switch t.in { - case nil: - ptrTest.want = []byte{0x00} - default: - ptrTest.want = append([]byte{0x01}, t.want...) + ptrTest.want = append([]byte{0x01}, t.want...) + ptrTests = append(ptrTests, ptrTest) + } + for _, tt := range ptrTests { + t.Run(tt.name, func(t *testing.T) { + es := &encodeState{fieldScaleIndicesCache: cache} + if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeFixedWidthInt() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { + t.Errorf("encodeState.encodeFixedWidthInt() = %v, want %v", es.Buffer.Bytes(), tt.want) + } + }) + } +} + +func Test_marshal_optionality_nil_cases(t *testing.T) { + var ptrTests tests + for i := range allTests { + t := allTests[i] + ptrTest := test{ + name: t.name, + // in: t.in, + wantErr: t.wantErr, + want: t.want, } + // create a new pointer to new zero value of t.in + temp := reflect.New(reflect.TypeOf(t.in)) + // create a new pointer to type of temp + tempv := reflect.New(reflect.PtrTo(temp.Type()).Elem()) + // set zero value to elem of **temp so that is nil + tempv.Elem().Set(reflect.Zero(tempv.Elem().Type())) + // set test.in to *temp + ptrTest.in = tempv.Elem().Interface() + // want encoded nil + ptrTest.want = []byte{0x00} + // append to test ptrTests = append(ptrTests, ptrTest) } for _, tt := range ptrTests { diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index 632ab804d4..a96ab2b539 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -28,7 +28,6 @@ func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { wantIndices fieldScaleIndices wantErr bool }{ - // TODO: Add test cases. { in: struct{ Foo int }{}, wantIndices: fieldScaleIndices{ diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index 861d9872b0..53dcf05282 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -111,13 +111,19 @@ func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { // Cmp returns 1 if the receiver is greater than other, 0 if they are equal, and -1 otherwise. func (u *Uint128) Compare(other *Uint128) int { switch { - case u.Upper > other.Upper, u.Lower > other.Lower: + case u.Upper > other.Upper: return 1 - case u.Upper < other.Upper, u.Lower < other.Lower: + case u.Upper < other.Upper: return -1 - default: - return 0 + case u.Upper == other.Upper: + switch { + case u.Lower > other.Lower: + return 1 + case u.Lower < other.Lower: + return -1 + } } + return 0 } func (u *Uint128) trimBytes(b []byte, order binary.ByteOrder) []byte { From 8a663d47ed57c14c3e8336397bc37f07dd1dc904 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 9 Jun 2021 11:02:06 -0400 Subject: [PATCH 031/245] cr feedback --- pkg/scale/encode.go | 5 +++-- pkg/scale/result.go | 14 ++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index e7917d8d8c..33b434c98c 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -142,8 +142,9 @@ func (es *encodeState) encodeCustomPrimitive(in interface{}) (err error) { return } func (es *encodeState) encodeResult(res Result) (err error) { - err = res.Validate() - if err != nil { + switch res.IsValid() { + case false: + err = fmt.Errorf("Result is invalid: %+v", res) return } diff --git a/pkg/scale/result.go b/pkg/scale/result.go index 2d3b31465a..c3e07d1551 100644 --- a/pkg/scale/result.go +++ b/pkg/scale/result.go @@ -22,19 +22,17 @@ import ( ) type resultCache map[string]map[bool]interface{} + +// Result encapsulates an Ok or an Err case. It's not a valid result unless one of the +// attributes != nil type Result struct { Ok interface{} Err interface{} } -// Validate ensures Result is valid. Only one of the Ok and Err attributes should be nil -func (r Result) Validate() (err error) { - switch { - case r.Ok == nil && r.Err != nil, r.Ok != nil && r.Err == nil: - default: - err = fmt.Errorf("Result is invalid: %+v", r) - } - return +// Valid returns whether the Result is valid. Only one of the Ok and Err attributes should be nil +func (r Result) IsValid() bool { + return r.Ok == nil && r.Err != nil || r.Ok != nil && r.Err == nil } var resCache resultCache = make(resultCache) From 159cc026ad54adad0c90004ccb4138110b269a7b Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 14 Jun 2021 12:04:15 -0400 Subject: [PATCH 032/245] wip --- pkg/scale/decode.go | 73 +++++++++++++++++------ pkg/scale/encode.go | 37 +++++++++--- pkg/scale/result.go | 57 ++++++++++++++++-- pkg/scale/result_test.go | 107 ++++++++++++++++++++++++++++++--- pkg/scale/something_test.go | 114 ++++++++++++++++++++++++++++++++++++ 5 files changed, 350 insertions(+), 38 deletions(-) create mode 100644 pkg/scale/something_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 7d7f48cf9f..fca3d55979 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -78,13 +78,27 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { err = ds.decodePointer(dstv) case reflect.Struct: t := reflect.TypeOf(in) - // check if this is a convertible to Result, if so encode using decodeResult - switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + field, ok := t.FieldByName("Result") + switch ok { case true: + if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { + err = fmt.Errorf("%T is not a Result", in) + return + } + // res := reflect.ValueOf(in).FieldByName("Result") + // fmt.Println("yao!", res) err = ds.decodeResult(dstv) - case false: + default: err = ds.decodeStruct(dstv) } + + // // check if this is a convertible to Result, if so encode using decodeResult + // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + // case true: + // err = ds.decodeResult(dstv) + // case false: + // err = ds.decodeStruct(dstv) + // } case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: @@ -191,6 +205,7 @@ func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { dstt := reflect.TypeOf(dstv.Interface()) key := fmt.Sprintf("%s.%s", dstt.PkgPath(), dstt.Name()) + fmt.Printf("key: %s", key) resultCases, ok := resCache[key] if !ok { err = fmt.Errorf("unable to find registered custom Result: %T", dstv.Interface()) @@ -210,14 +225,27 @@ func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { err = fmt.Errorf("unable to find registered custom Result.Ok for: %T", dstv.Interface()) return } - newOk := reflect.New(reflect.TypeOf(okIn)) - err = ds.unmarshal(newOk.Elem()) - if err != nil { - break + + switch okIn { + case nil: + var empty interface{} + res := Result{ + ok: &empty, + } + dstv.FieldByName("Result").Set(reflect.ValueOf(res)) + default: + newOk := reflect.New(reflect.TypeOf(okIn)) + err = ds.unmarshal(newOk.Elem()) + if err != nil { + break + } + newOkIn := newOk.Elem().Interface() + res := Result{ + ok: &newOkIn, + } + dstv.FieldByName("Result").Set(reflect.ValueOf(res)) } - res := reflect.New(dstt) - res.Elem().FieldByName("Ok").Set(newOk.Elem()) - dstv.Set(res.Elem()) + case 0x01: // Error case errIn, ok := resultCases[false] @@ -225,14 +253,25 @@ func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { err = fmt.Errorf("unable to find registered custom Result.Err for: %T", dstv.Interface()) return } - newErr := reflect.New(reflect.TypeOf(errIn)) - err = ds.unmarshal(newErr.Elem()) - if err != nil { - break + + if errIn != nil { + newErr := reflect.New(reflect.TypeOf(errIn)) + err = ds.unmarshal(newErr.Elem()) + if err != nil { + break + } + newErrIn := newErr.Elem().Interface() + res := Result{ + err: &newErrIn, + } + dstv.FieldByName("Result").Set(reflect.ValueOf(res)) + } else { + var empty interface{} + res := Result{ + err: &empty, + } + dstv.FieldByName("Result").Set(reflect.ValueOf(res)) } - res := reflect.New(dstt) - res.Elem().FieldByName("Err").Set(newErr.Elem()) - dstv.Set(res.Elem()) default: err = fmt.Errorf("unsupported Result value: %v, bytes: %v", rb, ds.Bytes()) } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 33b434c98c..7721bbc5f7 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -79,16 +79,31 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: + // fmt.Println("in here!") t := reflect.TypeOf(in) - // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType - switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + field, ok := t.FieldByName("Result") + switch ok { case true: - resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) - err = es.encodeResult(resv.Interface().(Result)) - case false: + if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { + err = fmt.Errorf("%T is not a Result", in) + return + } + res := reflect.ValueOf(in).FieldByName("Result").Interface().(Result) + // fmt.Println("yao!", res) + err = es.encodeResult(res) + default: err = es.encodeStruct(in) } + // check if this is a type with an embedded Result, aka a registered result + // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + // case true: + // resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) + // err = es.encodeResult(resv.Interface().(Result)) + // case false: + // err = es.encodeStruct(in) + // } + case reflect.Array: err = es.encodeArray(in) case reflect.Slice: @@ -148,7 +163,7 @@ func (es *encodeState) encodeResult(res Result) (err error) { return } - switch res.Ok { + switch res.ok { case nil: // error case err = es.WriteByte(1) @@ -157,15 +172,19 @@ func (es *encodeState) encodeResult(res Result) (err error) { } // TODO: type checking of res.Err against resultCache to ensure Err is same as // registered type - err = es.marshal(res.Err) + if *res.err != nil { + err = es.marshal(*res.err) + } default: err = es.WriteByte(0) if err != nil { break } - // TODO: type checking of res.Ok against resultCache to ensure Err is same as + // TODO: type checking of res.Ok against resultCache to ensure ok is same as // registered type - err = es.marshal(res.Ok) + if *res.ok != nil { + err = es.marshal(*res.ok) + } } return } diff --git a/pkg/scale/result.go b/pkg/scale/result.go index c3e07d1551..d0bfa8cf41 100644 --- a/pkg/scale/result.go +++ b/pkg/scale/result.go @@ -26,26 +26,73 @@ type resultCache map[string]map[bool]interface{} // Result encapsulates an Ok or an Err case. It's not a valid result unless one of the // attributes != nil type Result struct { - Ok interface{} + ok *interface{} + err *interface{} +} + +func (r *Result) SetOk(in interface{}) (err error) { + r.ok = &in + return +} + +func (r *Result) SetErr(in interface{}) (err error) { + r.err = &in + return +} + +// func (r *Result) Ok() (okIn interface{}, err error) { +// return +// } + +// func (r *Result) Err() (errIn interface{}, err error) { +// return +// } + +type ResultErr struct { Err interface{} } +func (r ResultErr) Error() string { + return fmt.Sprintf("ResultErr %+v", r.Err) +} + +// Result returns the result in go standard wrapping the Err case in a ResultErr +func (r *Result) Result() (in interface{}, err error) { + if !r.IsValid() { + err = fmt.Errorf("result is not valid") + return + } + if r.ok != nil { + in = *r.ok + } else { + in = *r.err + err = ResultErr{*r.err} + } + return +} + // Valid returns whether the Result is valid. Only one of the Ok and Err attributes should be nil -func (r Result) IsValid() bool { - return r.Ok == nil && r.Err != nil || r.Ok != nil && r.Err == nil +func (r *Result) IsValid() bool { + return (r.ok == nil && r.err != nil) || (r.ok != nil && r.err == nil) } var resCache resultCache = make(resultCache) func RegisterResult(in interface{}, inOK interface{}, inErr interface{}) (err error) { t := reflect.TypeOf(in) - if !t.ConvertibleTo(reflect.TypeOf(Result{})) { + field, ok := t.FieldByName("Result") + if !ok { + err = fmt.Errorf("yao") + return + } + + if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { err = fmt.Errorf("%T is not a Result", in) return } key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) - _, ok := resCache[key] + _, ok = resCache[key] if !ok { resCache[key] = make(map[bool]interface{}) } diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 6750206d05..50a0a3f612 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -21,9 +21,11 @@ import ( "testing" ) -type MyResult Result - func TestEncodeDecodeResult(t *testing.T) { + type MyResult struct { + Result + } + err := RegisterResult(MyResult{}, MyStruct{}, false) if err != nil { t.Errorf("%v", err) @@ -34,7 +36,8 @@ func TestEncodeDecodeResult(t *testing.T) { Bar: 2, Baz: true, } - mr := MyResult{Ok: ms} + mr := MyResult{} + mr.SetOk(ms) bytes, err := Marshal(mr) if err != nil { t.Errorf("%v", err) @@ -44,7 +47,8 @@ func TestEncodeDecodeResult(t *testing.T) { t.Errorf("unexpected bytes: %v", bytes) } - mr1 := MyResult{Err: true} + mr1 := MyResult{} + mr1.SetErr(true) bytes, err = Marshal(mr1) if err != nil { t.Errorf("%v", err) @@ -58,7 +62,9 @@ func TestEncodeDecodeResult(t *testing.T) { if err != nil { t.Errorf("%v", err) } - if !reflect.DeepEqual(MyResult{Ok: ms}, mr2) { + expected := MyResult{} + expected.SetOk(ms) + if !reflect.DeepEqual(expected, mr2) { t.Errorf("unexpected MyResult") } @@ -67,7 +73,94 @@ func TestEncodeDecodeResult(t *testing.T) { if err != nil { t.Errorf("%v", err) } - if !reflect.DeepEqual(MyResult{Err: true}, mr3) { - t.Errorf("unexpected MyResult yo") + + expected = MyResult{} + expected.SetErr(true) + if !reflect.DeepEqual(expected, mr3) { + t.Errorf("unexpected MyResult") + } +} + +func TestNilErr(t *testing.T) { + type MyResult struct { + Result + } + + err := RegisterResult(MyResult{}, true, nil) + if err != nil { + t.Errorf("%v", err) + } + + mr := MyResult{} + mr.SetErr(nil) + bytes, err := Marshal(mr) + if err != nil { + t.Errorf("%v", err) + return + } + + if !reflect.DeepEqual([]byte{0x01}, bytes) { + t.Errorf("unexpected bytes: %v", bytes) + } + + mr1 := MyResult{} + err = Unmarshal([]byte{0x01}, &mr1) + if err != nil { + t.Errorf("%v", err) + } + expected := mr + if !reflect.DeepEqual(expected, mr1) { + t.Errorf("unexpected MyResult, %+v, %+v", expected, mr1) } } + +// func TestNilOk(t *testing.T) { +// mr := MyResult{} +// mr.SetOk(nil) +// bytes, err := Marshal(mr) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x00}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } + +// mr1 := MyResult{} +// mr1.SetErr(true) +// bytes, err = Marshal(mr1) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } +// } + +// func TestBothNil(t *testing.T) { +// mr := MyResult{} +// mr.SetOk(nil) +// bytes, err := Marshal(mr) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x00}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } + +// mr1 := MyResult{} +// mr1.SetErr(nil) +// bytes, err = Marshal(mr1) +// if err != nil { +// t.Errorf("%v", err) +// return +// } +// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } +// } diff --git a/pkg/scale/something_test.go b/pkg/scale/something_test.go new file mode 100644 index 0000000000..8194c32890 --- /dev/null +++ b/pkg/scale/something_test.go @@ -0,0 +1,114 @@ +package scale_test + +import ( + "fmt" + "testing" + + "github.com/ChainSafe/gossamer/pkg/scale" +) + +type MyStruct struct { + Baz bool + Bar uint32 + Foo []byte +} + +func (ms MyStruct) Index() uint { + return 1 +} + +type MyOtherStruct struct { + Foo string + Bar uint64 + Baz uint +} + +func (mos MyOtherStruct) Index() uint { + return 2 +} + +type MyInt16 int16 + +func (mi16 MyInt16) Index() uint { + return 3 +} + +type MyVaryingDataType scale.VaryingDataType + +func varyingDataTypeExample() { + err := scale.RegisterVaryingDataType(MyVaryingDataType{}, MyStruct{}, MyOtherStruct{}, MyInt16(0)) + if err != nil { + panic(err) + } + + mvdt := MyVaryingDataType{ + MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + }, + MyOtherStruct{ + Foo: "hello", + Bar: 999, + Baz: 888, + }, + MyInt16(111), + } + bytes, err := scale.Marshal(mvdt) + if err != nil { + panic(err) + } + + var unmarshaled MyVaryingDataType + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // [{Baz:true Bar:999 Foo:[1 2]} {Foo:hello Bar:999 Baz:888} 111] + fmt.Printf("%+v", unmarshaled) +} +func structExample() { + type MyStruct struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + } + var ms = MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + } + bytes, err := scale.Marshal(ms) + if err != nil { + panic(err) + } + + var unmarshaled MyStruct + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // Baz:true Bar:999 Foo:[1 2]} + fmt.Printf("%+v", unmarshaled) +} +func TestSomething(t *testing.T) { + // // compact length encoded uint + // var ui uint = 999 + // bytes, err := scale.Marshal(ui) + // if err != nil { + // panic(err) + // } + + // var unmarshaled uint + // err = scale.Unmarshal(bytes, &unmarshaled) + // if err != nil { + // panic(err) + // } + + // fmt.Printf("%d", unmarshaled) + + // structExample() + varyingDataTypeExample() +} From 1d109d553692128c2f8d8d15b4f987e42642c909 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 16 Jun 2021 15:37:38 -0400 Subject: [PATCH 033/245] revise Result --- pkg/scale/decode.go | 95 +++-------- pkg/scale/encode.go | 66 +++----- pkg/scale/result.go | 136 +++++++++------- pkg/scale/result_test.go | 312 +++++++++++++++++++++++++----------- pkg/scale/something_test.go | 114 ------------- 5 files changed, 332 insertions(+), 391 deletions(-) delete mode 100644 pkg/scale/something_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index fca3d55979..0f3d31fb85 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -67,6 +67,8 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { err = ds.decodeBytes(dstv) case bool: err = ds.decodeBool(dstv) + case Result: + err = ds.decodeResult(dstv) default: t := reflect.TypeOf(in) switch t.Kind() { @@ -77,28 +79,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Ptr: err = ds.decodePointer(dstv) case reflect.Struct: - t := reflect.TypeOf(in) - field, ok := t.FieldByName("Result") - switch ok { - case true: - if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { - err = fmt.Errorf("%T is not a Result", in) - return - } - // res := reflect.ValueOf(in).FieldByName("Result") - // fmt.Println("yao!", res) - err = ds.decodeResult(dstv) - default: - err = ds.decodeStruct(dstv) - } - - // // check if this is a convertible to Result, if so encode using decodeResult - // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { - // case true: - // err = ds.decodeResult(dstv) - // case false: - // err = ds.decodeStruct(dstv) - // } + err = ds.decodeStruct(dstv) case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: @@ -203,15 +184,7 @@ func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { } func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { - dstt := reflect.TypeOf(dstv.Interface()) - key := fmt.Sprintf("%s.%s", dstt.PkgPath(), dstt.Name()) - fmt.Printf("key: %s", key) - resultCases, ok := resCache[key] - if !ok { - err = fmt.Errorf("unable to find registered custom Result: %T", dstv.Interface()) - return - } - + res := dstv.Interface().(Result) var rb byte rb, err = ds.ReadByte() if err != nil { @@ -219,59 +192,27 @@ func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { } switch rb { case 0x00: - // Ok case - okIn, ok := resultCases[true] - if !ok { - err = fmt.Errorf("unable to find registered custom Result.Ok for: %T", dstv.Interface()) + tempElem := reflect.New(reflect.TypeOf(res.ok)) + err = ds.unmarshal(tempElem.Elem()) + if err != nil { return } - - switch okIn { - case nil: - var empty interface{} - res := Result{ - ok: &empty, - } - dstv.FieldByName("Result").Set(reflect.ValueOf(res)) - default: - newOk := reflect.New(reflect.TypeOf(okIn)) - err = ds.unmarshal(newOk.Elem()) - if err != nil { - break - } - newOkIn := newOk.Elem().Interface() - res := Result{ - ok: &newOkIn, - } - dstv.FieldByName("Result").Set(reflect.ValueOf(res)) + err = res.Set(OK, tempElem.Elem().Interface()) + if err != nil { + return } - + dstv.Set(reflect.ValueOf(res)) case 0x01: - // Error case - errIn, ok := resultCases[false] - if !ok { - err = fmt.Errorf("unable to find registered custom Result.Err for: %T", dstv.Interface()) + tempElem := reflect.New(reflect.TypeOf(res.err)) + err = ds.unmarshal(tempElem.Elem()) + if err != nil { return } - - if errIn != nil { - newErr := reflect.New(reflect.TypeOf(errIn)) - err = ds.unmarshal(newErr.Elem()) - if err != nil { - break - } - newErrIn := newErr.Elem().Interface() - res := Result{ - err: &newErrIn, - } - dstv.FieldByName("Result").Set(reflect.ValueOf(res)) - } else { - var empty interface{} - res := Result{ - err: &empty, - } - dstv.FieldByName("Result").Set(reflect.ValueOf(res)) + err = res.Set(Err, tempElem.Elem().Interface()) + if err != nil { + return } + dstv.Set(reflect.ValueOf(res)) default: err = fmt.Errorf("unsupported Result value: %v, bytes: %v", rb, ds.Bytes()) } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 7721bbc5f7..458d36695c 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -59,6 +59,8 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeBytes([]byte(in)) case bool: err = es.encodeBool(in) + case Result: + err = es.encodeResult(in) default: switch reflect.TypeOf(in).Kind() { case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, @@ -79,31 +81,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: - // fmt.Println("in here!") - t := reflect.TypeOf(in) - field, ok := t.FieldByName("Result") - switch ok { - case true: - if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { - err = fmt.Errorf("%T is not a Result", in) - return - } - res := reflect.ValueOf(in).FieldByName("Result").Interface().(Result) - // fmt.Println("yao!", res) - err = es.encodeResult(res) - default: - err = es.encodeStruct(in) - } - - // check if this is a type with an embedded Result, aka a registered result - // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { - // case true: - // resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) - // err = es.encodeResult(resv.Interface().(Result)) - // case false: - // err = es.encodeStruct(in) - // } - + err = es.encodeStruct(in) case reflect.Array: err = es.encodeArray(in) case reflect.Slice: @@ -157,34 +135,30 @@ func (es *encodeState) encodeCustomPrimitive(in interface{}) (err error) { return } func (es *encodeState) encodeResult(res Result) (err error) { - switch res.IsValid() { - case false: - err = fmt.Errorf("Result is invalid: %+v", res) + if !res.IsSet() { + err = fmt.Errorf("Result is not set: %+v", res) return } - switch res.ok { - case nil: - // error case - err = es.WriteByte(1) - if err != nil { - break - } - // TODO: type checking of res.Err against resultCache to ensure Err is same as - // registered type - if *res.err != nil { - err = es.marshal(*res.err) - } - default: + var in interface{} + switch res.mode { + case OK: err = es.WriteByte(0) if err != nil { - break + return } - // TODO: type checking of res.Ok against resultCache to ensure ok is same as - // registered type - if *res.ok != nil { - err = es.marshal(*res.ok) + in = res.ok + case Err: + err = es.WriteByte(1) + if err != nil { + return } + in = res.err + } + switch in := in.(type) { + case empty: + default: + err = es.marshal(in) } return } diff --git a/pkg/scale/result.go b/pkg/scale/result.go index d0bfa8cf41..237d9609a6 100644 --- a/pkg/scale/result.go +++ b/pkg/scale/result.go @@ -21,82 +21,106 @@ import ( "reflect" ) -type resultCache map[string]map[bool]interface{} +type ResultMode int -// Result encapsulates an Ok or an Err case. It's not a valid result unless one of the -// attributes != nil +const ( + Unset ResultMode = iota + OK + Err +) + +// Result encapsulates an Ok or an Err case type Result struct { - ok *interface{} - err *interface{} + ok interface{} + err interface{} + mode ResultMode } -func (r *Result) SetOk(in interface{}) (err error) { - r.ok = &in +// NewResult is constructor for Result. Use nil to represent empty tuple () in Rust. +func NewResult(okIn interface{}, errIn interface{}) (res Result) { + switch okIn { + case nil: + res.ok = empty{} + default: + res.ok = okIn + } + switch errIn { + case nil: + res.err = empty{} + default: + res.err = errIn + } return } -func (r *Result) SetErr(in interface{}) (err error) { - r.err = &in +func (r *Result) Set(mode ResultMode, in interface{}) (err error) { + switch mode { + case OK: + if reflect.TypeOf(r.ok) != reflect.TypeOf(in) { + err = fmt.Errorf("type mistmatch for result.ok: %T, and inputted: %T", r.ok, in) + return + } + r.ok = in + r.mode = mode + case Err: + if reflect.TypeOf(r.err) != reflect.TypeOf(in) { + err = fmt.Errorf("type mistmatch for result.ok: %T, and inputted: %T", r.ok, in) + return + } + r.err = in + r.mode = mode + default: + err = fmt.Errorf("invalid ResultMode %v", mode) + } return } -// func (r *Result) Ok() (okIn interface{}, err error) { -// return -// } - -// func (r *Result) Err() (errIn interface{}, err error) { -// return -// } - -type ResultErr struct { - Err interface{} -} - -func (r ResultErr) Error() string { - return fmt.Sprintf("ResultErr %+v", r.Err) -} +type UnsetResult error // Result returns the result in go standard wrapping the Err case in a ResultErr -func (r *Result) Result() (in interface{}, err error) { - if !r.IsValid() { - err = fmt.Errorf("result is not valid") +func (r *Result) Unwrap() (ok interface{}, err error) { + if !r.IsSet() { + err = UnsetResult(fmt.Errorf("result is not set")) return } - if r.ok != nil { - in = *r.ok - } else { - in = *r.err - err = ResultErr{*r.err} + switch r.mode { + case OK: + switch r.ok.(type) { + case empty: + ok = nil + default: + ok = r.ok + } + case Err: + switch r.err.(type) { + case empty: + err = WrappedErr{nil} + default: + err = WrappedErr{r.err} + } } return } -// Valid returns whether the Result is valid. Only one of the Ok and Err attributes should be nil -func (r *Result) IsValid() bool { - return (r.ok == nil && r.err != nil) || (r.ok != nil && r.err == nil) +// IsSet returns whether the Result is set with an Ok or Err value. +func (r *Result) IsSet() bool { + if r.ok == nil || r.err == nil { + return false + } + switch r.mode { + case OK, Err: + default: + return false + } + return true } -var resCache resultCache = make(resultCache) - -func RegisterResult(in interface{}, inOK interface{}, inErr interface{}) (err error) { - t := reflect.TypeOf(in) - field, ok := t.FieldByName("Result") - if !ok { - err = fmt.Errorf("yao") - return - } +type empty struct{} - if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { - err = fmt.Errorf("%T is not a Result", in) - return - } +type WrappedErr struct { + Err interface{} +} - key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) - _, ok = resCache[key] - if !ok { - resCache[key] = make(map[bool]interface{}) - } - resCache[key][true] = inOK - resCache[key][false] = inErr - return +func (r WrappedErr) Error() string { + return fmt.Sprintf("ResultErr %+v", r.Err) } diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 50a0a3f612..4140a4254b 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -22,23 +22,15 @@ import ( ) func TestEncodeDecodeResult(t *testing.T) { - type MyResult struct { - Result - } - - err := RegisterResult(MyResult{}, MyStruct{}, false) - if err != nil { - t.Errorf("%v", err) - } - ms := MyStruct{ Foo: []byte{0x01}, Bar: 2, Baz: true, } - mr := MyResult{} - mr.SetOk(ms) - bytes, err := Marshal(mr) + res := NewResult(ms, nil) + res.Set(OK, ms) + + bytes, err := Marshal(res) if err != nil { t.Errorf("%v", err) } @@ -47,9 +39,9 @@ func TestEncodeDecodeResult(t *testing.T) { t.Errorf("unexpected bytes: %v", bytes) } - mr1 := MyResult{} - mr1.SetErr(true) - bytes, err = Marshal(mr1) + res = NewResult(nil, true) + res.Set(Err, true) + bytes, err = Marshal(res) if err != nil { t.Errorf("%v", err) } @@ -57,110 +49,234 @@ func TestEncodeDecodeResult(t *testing.T) { t.Errorf("unexpected bytes: %v", bytes) } - mr2 := MyResult{} + res = NewResult(nil, true) + res.Set(Err, false) + bytes, err = Marshal(res) + if err != nil { + t.Errorf("%v", err) + } + if !reflect.DeepEqual([]byte{0x01, 0x00}, bytes) { + t.Errorf("unexpected bytes: %v", bytes) + } + + mr2 := NewResult(ms, nil) err = Unmarshal([]byte{0x00, 0x04, 0x01, 0x02, 0, 0, 0, 0x01}, &mr2) if err != nil { t.Errorf("%v", err) } - expected := MyResult{} - expected.SetOk(ms) + expected := NewResult(ms, nil) + expected.Set(OK, ms) if !reflect.DeepEqual(expected, mr2) { - t.Errorf("unexpected MyResult") + t.Errorf("unexpected MyResult %+v %+v", expected, mr2) } - mr3 := MyResult{} + mr3 := NewResult(nil, true) err = Unmarshal([]byte{0x01, 0x01}, &mr3) if err != nil { t.Errorf("%v", err) } - - expected = MyResult{} - expected.SetErr(true) + expected = NewResult(nil, true) + expected.Set(Err, true) if !reflect.DeepEqual(expected, mr3) { - t.Errorf("unexpected MyResult") + t.Errorf("unexpected MyResult %+v %+v", expected, mr3) } } -func TestNilErr(t *testing.T) { - type MyResult struct { - Result +func TestResult_IsSet(t *testing.T) { + type fields struct { + ok interface{} + err interface{} + mode ResultMode } - - err := RegisterResult(MyResult{}, true, nil) - if err != nil { - t.Errorf("%v", err) + tests := []struct { + name string + fields fields + want bool + }{ + { + want: false, + }, + { + fields: fields{ + ok: empty{}, + }, + want: false, + }, + { + fields: fields{ + ok: empty{}, + err: empty{}, + }, + want: false, + }, + { + fields: fields{ + ok: empty{}, + err: empty{}, + mode: OK, + }, + want: true, + }, + { + fields: fields{ + ok: empty{}, + err: empty{}, + mode: Err, + }, + want: true, + }, } - - mr := MyResult{} - mr.SetErr(nil) - bytes, err := Marshal(mr) - if err != nil { - t.Errorf("%v", err) - return + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &Result{ + ok: tt.fields.ok, + err: tt.fields.err, + mode: tt.fields.mode, + } + if got := r.IsSet(); got != tt.want { + t.Errorf("Result.IsSet() = %v, want %v", got, tt.want) + } + }) } +} - if !reflect.DeepEqual([]byte{0x01}, bytes) { - t.Errorf("unexpected bytes: %v", bytes) +func TestResult_Unwrap(t *testing.T) { + type fields struct { + ok interface{} + err interface{} + mode ResultMode } - - mr1 := MyResult{} - err = Unmarshal([]byte{0x01}, &mr1) - if err != nil { - t.Errorf("%v", err) + tests := []struct { + name string + fields fields + wantOk interface{} + wantErr bool + }{ + { + fields: fields{ + ok: empty{}, + err: empty{}, + }, + wantErr: true, + }, + { + fields: fields{ + ok: empty{}, + err: empty{}, + mode: OK, + }, + }, + { + fields: fields{ + ok: empty{}, + err: empty{}, + mode: Err, + }, + wantErr: true, + }, + { + fields: fields{ + ok: true, + err: empty{}, + mode: OK, + }, + wantOk: true, + }, + { + fields: fields{ + ok: empty{}, + err: true, + mode: Err, + }, + wantErr: true, + }, } - expected := mr - if !reflect.DeepEqual(expected, mr1) { - t.Errorf("unexpected MyResult, %+v, %+v", expected, mr1) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &Result{ + ok: tt.fields.ok, + err: tt.fields.err, + mode: tt.fields.mode, + } + gotOk, err := r.Unwrap() + if (err != nil) != tt.wantErr { + t.Errorf("Result.Unwrap() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotOk, tt.wantOk) { + t.Errorf("Result.Unwrap() = %v, want %v", gotOk, tt.wantOk) + } + }) } } -// func TestNilOk(t *testing.T) { -// mr := MyResult{} -// mr.SetOk(nil) -// bytes, err := Marshal(mr) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x00}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } - -// mr1 := MyResult{} -// mr1.SetErr(true) -// bytes, err = Marshal(mr1) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } -// } - -// func TestBothNil(t *testing.T) { -// mr := MyResult{} -// mr.SetOk(nil) -// bytes, err := Marshal(mr) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x00}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } - -// mr1 := MyResult{} -// mr1.SetErr(nil) -// bytes, err = Marshal(mr1) -// if err != nil { -// t.Errorf("%v", err) -// return -// } -// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } -// } +func TestResult_Set(t *testing.T) { + type args struct { + mode ResultMode + in interface{} + } + tests := []struct { + name string + res Result + args args + wantErr bool + }{ + // TODO: Add test cases. + { + args: args{ + mode: Unset, + }, + wantErr: true, + }, + { + args: args{ + mode: OK, + in: nil, + }, + }, + { + args: args{ + mode: Err, + in: nil, + }, + }, + { + args: args{ + mode: OK, + in: true, + }, + res: NewResult(true, nil), + }, + { + args: args{ + mode: Err, + in: true, + }, + res: NewResult(nil, true), + }, + { + args: args{ + mode: OK, + in: true, + }, + res: NewResult("ok", "err"), + wantErr: true, + }, + { + args: args{ + mode: Err, + in: nil, + }, + res: NewResult(nil, true), + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := tt.res + if err := r.Set(tt.args.mode, tt.args.in); (err != nil) != tt.wantErr { + t.Errorf("Result.Set() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/scale/something_test.go b/pkg/scale/something_test.go deleted file mode 100644 index 8194c32890..0000000000 --- a/pkg/scale/something_test.go +++ /dev/null @@ -1,114 +0,0 @@ -package scale_test - -import ( - "fmt" - "testing" - - "github.com/ChainSafe/gossamer/pkg/scale" -) - -type MyStruct struct { - Baz bool - Bar uint32 - Foo []byte -} - -func (ms MyStruct) Index() uint { - return 1 -} - -type MyOtherStruct struct { - Foo string - Bar uint64 - Baz uint -} - -func (mos MyOtherStruct) Index() uint { - return 2 -} - -type MyInt16 int16 - -func (mi16 MyInt16) Index() uint { - return 3 -} - -type MyVaryingDataType scale.VaryingDataType - -func varyingDataTypeExample() { - err := scale.RegisterVaryingDataType(MyVaryingDataType{}, MyStruct{}, MyOtherStruct{}, MyInt16(0)) - if err != nil { - panic(err) - } - - mvdt := MyVaryingDataType{ - MyStruct{ - Baz: true, - Bar: 999, - Foo: []byte{1, 2}, - }, - MyOtherStruct{ - Foo: "hello", - Bar: 999, - Baz: 888, - }, - MyInt16(111), - } - bytes, err := scale.Marshal(mvdt) - if err != nil { - panic(err) - } - - var unmarshaled MyVaryingDataType - err = scale.Unmarshal(bytes, &unmarshaled) - if err != nil { - panic(err) - } - - // [{Baz:true Bar:999 Foo:[1 2]} {Foo:hello Bar:999 Baz:888} 111] - fmt.Printf("%+v", unmarshaled) -} -func structExample() { - type MyStruct struct { - Baz bool `scale:"3"` - Bar int32 `scale:"2"` - Foo []byte `scale:"1"` - } - var ms = MyStruct{ - Baz: true, - Bar: 999, - Foo: []byte{1, 2}, - } - bytes, err := scale.Marshal(ms) - if err != nil { - panic(err) - } - - var unmarshaled MyStruct - err = scale.Unmarshal(bytes, &unmarshaled) - if err != nil { - panic(err) - } - - // Baz:true Bar:999 Foo:[1 2]} - fmt.Printf("%+v", unmarshaled) -} -func TestSomething(t *testing.T) { - // // compact length encoded uint - // var ui uint = 999 - // bytes, err := scale.Marshal(ui) - // if err != nil { - // panic(err) - // } - - // var unmarshaled uint - // err = scale.Unmarshal(bytes, &unmarshaled) - // if err != nil { - // panic(err) - // } - - // fmt.Printf("%d", unmarshaled) - - // structExample() - varyingDataTypeExample() -} From 22d69ede2b2f7665b235faabd99214927887fc24 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 16 Jun 2021 21:52:23 -0400 Subject: [PATCH 034/245] add readme --- pkg/scale/README.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/scale/README.md b/pkg/scale/README.md index 6e1e3107d0..55f382fe00 100644 --- a/pkg/scale/README.md +++ b/pkg/scale/README.md @@ -77,10 +77,6 @@ SCALE uses a compact encoding for variable width unsigned integers. | `Compact` | `uint` | | `Compact` | `*big.Int` | -### Result - -> TODO: To be implemented and documented. - ## Usage ### Basic Example @@ -150,6 +146,19 @@ func structExample() { } ``` +### Result + +A `Result` is custom type analogous to a rust result. A `Result` needs to be constructed using the `NewResult` constructor. The two parameters accepted are the expected types that are associated to the `Ok`, and `Err` cases. + +``` +// Rust +Result = Ok(10) + +// go-scale +result := scale.NewResult(int32(0), int32(0) +result.Set(scale.Ok, 10) +``` + ### Varying Data Type A `VaryingDataType` is analogous to a Rust enum. A `VaryingDataType` needs to be registered using the `RegisterVaryingDataType` function with its associated `VaryingDataTypeValue` types. `VaryingDataTypeValue` is an From 7be94798f90e180777493d0bb62b2e1d236bc9dc Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 17 Jun 2021 10:43:10 -0400 Subject: [PATCH 035/245] add usage example for Result --- pkg/scale/README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/pkg/scale/README.md b/pkg/scale/README.md index 55f382fe00..029c022abc 100644 --- a/pkg/scale/README.md +++ b/pkg/scale/README.md @@ -159,6 +159,55 @@ result := scale.NewResult(int32(0), int32(0) result.Set(scale.Ok, 10) ``` +``` +import ( + "fmt" + "github.com/ChainSafe/gossamer/pkg/scale" +) + +func resultExample() { + // pass in zero or non-zero values of the types for Ok and Err cases + res := scale.NewResult(bool(false), string("")) + + // set the OK case with a value of true, any values for OK that are not bool will return an error + err := res.Set(scale.OK, true) + if err != nil { + panic(err) + } + + bytes, err := scale.Marshal(res) + if err != nil { + panic(err) + } + + // [0x00, 0x01] + fmt.Printf("%v\n", bytes) + + res1 := scale.NewResult(bool(false), string("")) + + err = scale.Unmarshal(bytes, &res1) + if err != nil { + panic(err) + } + + // res1 should be Set with OK mode and value of true + ok, err := res1.Unwrap() + if err != nil { + panic(err) + } + + switch ok := ok.(type) { + case bool: + if !ok { + panic(fmt.Errorf("unexpected ok value: %v", ok)) + } + default: + panic(fmt.Errorf("unexpected type: %T", ok)) + } +} + +``` + ### Varying Data Type A `VaryingDataType` is analogous to a Rust enum. A `VaryingDataType` needs to be registered using the `RegisterVaryingDataType` function with its associated `VaryingDataTypeValue` types. `VaryingDataTypeValue` is an From 594898c61058bd8657171f77c70abbd0baf3eed7 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 18 Jun 2021 22:13:39 -0400 Subject: [PATCH 036/245] wip VaryingDataType and VaryingDataTypeSlice --- pkg/scale/comparison_test.go | 75 ++++-- pkg/scale/decode.go | 140 ++++++---- pkg/scale/decode_test.go | 48 ++-- pkg/scale/encode.go | 34 +-- pkg/scale/encode_test.go | 3 +- pkg/scale/result.go | 8 + pkg/scale/result_example_test.go | 53 ++++ pkg/scale/varying_data_type.go | 93 +++++-- pkg/scale/varying_data_type_test.go | 389 +++++++++++++++++++++++----- 9 files changed, 647 insertions(+), 196 deletions(-) create mode 100644 pkg/scale/result_example_test.go diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index a1da3697c2..23d539b5d8 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -64,7 +64,7 @@ func (prd SealDigest) Index() uint { } func TestOldVsNewEncoding(t *testing.T) { - oldDigest := types.Digest{ + oldDigests := types.Digest{ &types.ChangesTrieRootDigest{ Hash: common.Hash{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, }, @@ -81,34 +81,53 @@ func TestOldVsNewEncoding(t *testing.T) { Data: []byte{1, 3, 5, 7}, }, } - oldEncode, err := oldDigest.Encode() + oldEncode, err := oldDigests.Encode() if err != nil { t.Errorf("unexpected err: %v", err) return } - type Digests VaryingDataType - err = RegisterVaryingDataType(Digests{}, ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}) + vdt, err := NewVaryingDataType(ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}) if err != nil { t.Errorf("unexpected err: %v", err) return } - newDigest := Digests{ - ChangesTrieRootDigest{ - Hash: common.Hash{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, - }, - PreRuntimeDigest{ - ConsensusEngineID: types.BabeEngineID, - Data: []byte{1, 3, 5, 7}, - }, - ConsensusDigest{ - ConsensusEngineID: types.BabeEngineID, - Data: []byte{1, 3, 5, 7}, - }, - SealDigest{ - ConsensusEngineID: types.BabeEngineID, - Data: []byte{1, 3, 5, 7}, - }, + err = vdt.Set(ChangesTrieRootDigest{ + Hash: common.Hash{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, + }) + if err != nil { + t.Errorf("unexpected err: %v", err) + return + } + + newDigest := []VaryingDataType{ + mustNewVaryingDataTypeAndSet( + ChangesTrieRootDigest{ + Hash: common.Hash{0, 91, 50, 25, 214, 94, 119, 36, 71, 216, 33, 152, 85, 184, 34, 120, 61, 161, 164, 223, 76, 53, 40, 246, 76, 38, 235, 204, 43, 31, 179, 28}, + }, + ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}, + ), + mustNewVaryingDataTypeAndSet( + PreRuntimeDigest{ + ConsensusEngineID: types.BabeEngineID, + Data: []byte{1, 3, 5, 7}, + }, + ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}, + ), + mustNewVaryingDataTypeAndSet( + ConsensusDigest{ + ConsensusEngineID: types.BabeEngineID, + Data: []byte{1, 3, 5, 7}, + }, + ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}, + ), + mustNewVaryingDataTypeAndSet( + SealDigest{ + ConsensusEngineID: types.BabeEngineID, + Data: []byte{1, 3, 5, 7}, + }, + ChangesTrieRootDigest{}, PreRuntimeDigest{}, ConsensusDigest{}, SealDigest{}, + ), } newEncode, err := Marshal(newDigest) @@ -120,14 +139,14 @@ func TestOldVsNewEncoding(t *testing.T) { t.Errorf("encodeState.encodeStruct() = %v, want %v", oldEncode, newEncode) } - var decoded Digests - err = Unmarshal(newEncode, &decoded) - if err != nil { - t.Errorf("unexpected err: %v", err) - } - if !reflect.DeepEqual(decoded, newDigest) { - t.Errorf("Unmarshal() = %v, want %v", decoded, newDigest) - } + // decoded := []VaryingDataType{} + // err = Unmarshal(newEncode, &decoded) + // if err != nil { + // t.Errorf("unexpected err: %v", err) + // } + // if !reflect.DeepEqual(decoded, newDigest) { + // t.Errorf("Unmarshal() = %v, want %v", decoded, newDigest) + // } } func BenchmarkUnmarshal(b *testing.B) { diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 0f3d31fb85..6ab8f14460 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -25,6 +25,49 @@ import ( "reflect" ) +// indirect walks down v allocating pointers as needed, +// until it gets to a non-pointer. +func indirect(dstv reflect.Value) (elem reflect.Value, err error) { + dstv0 := dstv + haveAddr := false + for { + // Load value from interface, but only if the result will be + // usefully addressable. + if dstv.Kind() == reflect.Interface && !dstv.IsNil() { + e := dstv.Elem() + if e.Kind() == reflect.Ptr && !e.IsNil() && e.Elem().Kind() == reflect.Ptr { + haveAddr = false + dstv = e + continue + } + } + if dstv.Kind() != reflect.Ptr { + break + } + if dstv.CanSet() { + break + } + // Prevent infinite loop if v is an interface pointing to its own address: + // var v interface{} + // v = &v + if dstv.Elem().Kind() == reflect.Interface && dstv.Elem().Elem() == dstv { + dstv = dstv.Elem() + break + } + if dstv.IsNil() { + dstv.Set(reflect.New(dstv.Type().Elem())) + } + if haveAddr { + dstv = dstv0 // restore original value after round-trip Value.Addr().Elem() + haveAddr = false + } else { + dstv = dstv.Elem() + } + } + elem = dstv + return +} + func Unmarshal(data []byte, dst interface{}) (err error) { dstv := reflect.ValueOf(dst) if dstv.Kind() != reflect.Ptr || dstv.IsNil() { @@ -32,6 +75,11 @@ func Unmarshal(data []byte, dst interface{}) (err error) { return } + elem, err := indirect(dstv) + if err != nil { + return + } + buf := &bytes.Buffer{} ds := decodeState{} _, err = buf.Write(data) @@ -39,7 +87,8 @@ func Unmarshal(data []byte, dst interface{}) (err error) { return } ds.Buffer = *buf - err = ds.unmarshal(dstv.Elem()) + + err = ds.unmarshal(elem) if err != nil { return } @@ -69,6 +118,8 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { err = ds.decodeBool(dstv) case Result: err = ds.decodeResult(dstv) + case VaryingDataType: + err = ds.decodeVaryingDataType(dstv) default: t := reflect.TypeOf(in) switch t.Kind() { @@ -83,14 +134,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: - t := reflect.TypeOf(in) - // check if this is a convertible to VaryingDataType, if so decode using encodeVaryingDataType - switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { - case true: - err = ds.decodeVaryingDataType(dstv) - case false: - err = ds.decodeSlice(dstv) - } + err = ds.decodeSlice(dstv) default: err = fmt.Errorf("unsupported type: %T", in) } @@ -229,13 +273,22 @@ func (ds *decodeState) decodePointer(dstv reflect.Value) (err error) { case 0x00: // nil case case 0x01: - elemType := reflect.TypeOf(dstv.Interface()).Elem() - tempElem := reflect.New(elemType) - err = ds.unmarshal(tempElem.Elem()) - if err != nil { - break + switch dstv.IsZero() { + case false: + if dstv.Elem().Kind() == reflect.Ptr { + err = ds.unmarshal(dstv.Elem().Elem()) + } else { + err = ds.unmarshal(dstv.Elem()) + } + case true: + elemType := reflect.TypeOf(dstv.Interface()).Elem() + tempElem := reflect.New(elemType) + err = ds.unmarshal(tempElem.Elem()) + if err != nil { + return + } + dstv.Set(tempElem) } - dstv.Set(tempElem) default: err = fmt.Errorf("unsupported Option value: %v, bytes: %v", rb, ds.Bytes()) } @@ -243,42 +296,29 @@ func (ds *decodeState) decodePointer(dstv reflect.Value) (err error) { } func (ds *decodeState) decodeVaryingDataType(dstv reflect.Value) (err error) { - l, err := ds.decodeLength() + var b byte + b, err = ds.ReadByte() if err != nil { return } - dstt := reflect.TypeOf(dstv.Interface()) - key := fmt.Sprintf("%s.%s", dstt.PkgPath(), dstt.Name()) - mappedValues, ok := vdtCache[key] + vdt := dstv.Interface().(VaryingDataType) + val, ok := vdt.cache[uint(b)] if !ok { - err = fmt.Errorf("unable to find registered custom VaryingDataType: %T", dstv.Interface()) + err = fmt.Errorf("unable to find VaryingDataTypeValue with index: %d", uint(b)) return } - temp := reflect.New(dstt) - for i := 0; i < l; i++ { - var b byte - b, err = ds.ReadByte() - if err != nil { - return - } - - val, ok := mappedValues[uint(b)] - if !ok { - err = fmt.Errorf("unable to find registered VaryingDataTypeValue for type: %T", dstv.Interface()) - return - } - - tempVal := reflect.New(reflect.TypeOf(val)).Elem() - err = ds.unmarshal(tempVal) - if err != nil { - return - } - - temp.Elem().Set(reflect.Append(temp.Elem(), tempVal)) + tempVal := reflect.New(reflect.TypeOf(val)).Elem() + err = ds.unmarshal(tempVal) + if err != nil { + return } - dstv.Set(temp.Elem()) + err = vdt.Set(tempVal.Interface().(VaryingDataTypeValue)) + if err != nil { + return + } + dstv.Set(reflect.ValueOf(vdt)) return } @@ -510,56 +550,56 @@ func (ds *decodeState) decodeFixedWidthInt(dstv reflect.Value) (err error) { var b byte b, err = ds.ReadByte() if err != nil { - break + return } out = int8(b) case uint8: var b byte b, err = ds.ReadByte() if err != nil { - break + return } out = uint8(b) case int16: buf := make([]byte, 2) _, err = ds.Read(buf) if err != nil { - break + return } out = int16(binary.LittleEndian.Uint16(buf)) case uint16: buf := make([]byte, 2) _, err = ds.Read(buf) if err != nil { - break + return } out = binary.LittleEndian.Uint16(buf) case int32: buf := make([]byte, 4) _, err = ds.Read(buf) if err != nil { - break + return } out = int32(binary.LittleEndian.Uint32(buf)) case uint32: buf := make([]byte, 4) _, err = ds.Read(buf) if err != nil { - break + return } out = binary.LittleEndian.Uint32(buf) case int64: buf := make([]byte, 8) _, err = ds.Read(buf) if err != nil { - break + return } out = int64(binary.LittleEndian.Uint64(buf)) case uint64: buf := make([]byte, 8) _, err = ds.Read(buf) if err != nil { - break + return } out = binary.LittleEndian.Uint64(buf) default: diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index f8051b56da..22412b7d8e 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -145,7 +145,7 @@ func Test_decodeState_decodeSlice(t *testing.T) { func Test_unmarshal_optionality(t *testing.T) { var ptrTests tests - for _, t := range allTests { + for _, t := range append(tests{}, allTests...) { ptrTest := test{ name: t.name, in: t.in, @@ -159,22 +159,38 @@ func Test_unmarshal_optionality(t *testing.T) { } for _, tt := range ptrTests { t.Run(tt.name, func(t *testing.T) { - // this becomes a pointer to a zero value of the underlying value - dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - var diff string - if tt.out != nil { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } + switch in := tt.in.(type) { + case VaryingDataType: + // copy the inputted vdt cause we need the cached values + copy := in + vdt := copy + vdt.value = nil + var dst interface{} = &vdt + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + diff := cmp.Diff(vdt.value, tt.in.(VaryingDataType).value, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + default: + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + } }) } } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 458d36695c..d9026c5ca4 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -61,6 +61,10 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeBool(in) case Result: err = es.encodeResult(in) + case VaryingDataType: + err = es.encodeVaryingDataType(in) + case VaryingDataTypeSlice: + err = es.encodeVaryingDataTypeSlice(in) default: switch reflect.TypeOf(in).Kind() { case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, @@ -85,15 +89,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { case reflect.Array: err = es.encodeArray(in) case reflect.Slice: - t := reflect.TypeOf(in) - // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType - switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { - case true: - invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) - err = es.encodeVaryingDataType(invdt.Interface().(VaryingDataType)) - case false: - err = es.encodeSlice(in) - } + err = es.encodeSlice(in) default: err = fmt.Errorf("unsupported type: %T", in) } @@ -134,6 +130,7 @@ func (es *encodeState) encodeCustomPrimitive(in interface{}) (err error) { err = es.marshal(in) return } + func (es *encodeState) encodeResult(res Result) (err error) { if !res.IsSet() { err = fmt.Errorf("Result is not set: %+v", res) @@ -163,20 +160,17 @@ func (es *encodeState) encodeResult(res Result) (err error) { return } -func (es *encodeState) encodeVaryingDataType(values VaryingDataType) (err error) { - err = es.encodeLength(len(values)) +func (es *encodeState) encodeVaryingDataType(vdt VaryingDataType) (err error) { + err = es.WriteByte(byte(vdt.value.Index())) if err != nil { return } - for _, val := range values { - // TODO: type checking of val against vdtCache to ensure it is a registered type - // encode type.Index (idx) for varying data type - err = es.WriteByte(byte(val.Index())) - if err != nil { - return - } - err = es.marshal(val) - } + err = es.marshal(vdt.value) + return +} + +func (es *encodeState) encodeVaryingDataTypeSlice(vdts VaryingDataTypeSlice) (err error) { + err = es.marshal(vdts.Values) return } diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index b0d93d2f94..8be29486ab 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -854,7 +854,8 @@ var ( allTests = newTests( fixedWidthIntegerTests, variableWidthIntegerTests, stringTests, - boolTests, structTests, sliceTests, arrayTests, varyingDataTypeTests, + boolTests, structTests, sliceTests, arrayTests, + varyingDataTypeTests, ) ) diff --git a/pkg/scale/result.go b/pkg/scale/result.go index 237d9609a6..88e18c0aa8 100644 --- a/pkg/scale/result.go +++ b/pkg/scale/result.go @@ -21,11 +21,15 @@ import ( "reflect" ) +// ResultMode is the mode the Result is set to type ResultMode int const ( + // Unset ResultMode is zero value mode Unset ResultMode = iota + // Ok case OK + // Err case Err ) @@ -53,6 +57,7 @@ func NewResult(okIn interface{}, errIn interface{}) (res Result) { return } +// Set takes in a mode (OK/Err) and the associated interface and sets the Result value func (r *Result) Set(mode ResultMode, in interface{}) (err error) { switch mode { case OK: @@ -75,6 +80,7 @@ func (r *Result) Set(mode ResultMode, in interface{}) (err error) { return } +// UnsetResult is error when Result is unset with a value. type UnsetResult error // Result returns the result in go standard wrapping the Err case in a ResultErr @@ -117,10 +123,12 @@ func (r *Result) IsSet() bool { type empty struct{} +// WrappedErr is returned by Result.Unwrap(). The underlying Err value is wrapped and stored in Err attribute type WrappedErr struct { Err interface{} } +// Error fulfills the error interface func (r WrappedErr) Error() string { return fmt.Sprintf("ResultErr %+v", r.Err) } diff --git a/pkg/scale/result_example_test.go b/pkg/scale/result_example_test.go new file mode 100644 index 0000000000..d32c26df0b --- /dev/null +++ b/pkg/scale/result_example_test.go @@ -0,0 +1,53 @@ +package scale_test + +import ( + "fmt" + "testing" + + "github.com/ChainSafe/gossamer/pkg/scale" +) + +func resultExample() { + // pass in zero or non-zero values of the types for Ok and Err cases + res := scale.NewResult(bool(false), string("")) + + // set the OK case with a value of true, any values for OK that are not bool will return an error + err := res.Set(scale.OK, true) + if err != nil { + panic(err) + } + + bytes, err := scale.Marshal(res) + if err != nil { + panic(err) + } + + // [0x00, 0x01] + fmt.Printf("%v\n", bytes) + + res1 := scale.NewResult(bool(false), string("")) + + err = scale.Unmarshal(bytes, &res1) + if err != nil { + panic(err) + } + + // res1 should be Set with OK mode and value of true + ok, err := res1.Unwrap() + if err != nil { + panic(err) + } + + switch ok := ok.(type) { + case bool: + if !ok { + panic(fmt.Errorf("unexpected ok value: %v", ok)) + } + default: + panic(fmt.Errorf("unexpected type: %T", ok)) + } +} + +func TestSomething2(t *testing.T) { + resultExample() +} diff --git a/pkg/scale/varying_data_type.go b/pkg/scale/varying_data_type.go index 8c2bf36f67..b5f3e19d76 100644 --- a/pkg/scale/varying_data_type.go +++ b/pkg/scale/varying_data_type.go @@ -18,34 +18,93 @@ package scale import ( "fmt" - "reflect" ) -type varyingDataTypeCache map[string]map[uint]VaryingDataTypeValue +// VaryingDataType is used to represent scale encodable types +type VaryingDataTypeValue interface { + Index() uint +} -var vdtCache varyingDataTypeCache = make(varyingDataTypeCache) +// VaryingDataTypeSlice is used to represent []VaryingDataType. SCALE requires knowledge +// of the underlying data, so it is required to have the VaryingDataType required for decoding +type VaryingDataTypeSlice struct { + VaryingDataType + Values []VaryingDataType +} + +// Add takes variadic parameter values to add VaryingDataTypeValue(s) +func (vdts *VaryingDataTypeSlice) Add(values ...VaryingDataTypeValue) (err error) { + for _, val := range values { + copied := vdts.VaryingDataType + err = copied.Set(val) + if err != nil { + return + } + vdts.Values = append(vdts.Values, copied) + } + return +} -type VaryingDataType []VaryingDataTypeValue +// NewVaryingDataTypeSlice is constructor for VaryingDataTypeSlice +func NewVaryingDataTypeSlice(vdt VaryingDataType) (vdts VaryingDataTypeSlice) { + vdts.VaryingDataType = vdt + vdts.Values = make([]VaryingDataType, 0) + return +} -func RegisterVaryingDataType(in interface{}, values ...VaryingDataTypeValue) (err error) { - t := reflect.TypeOf(in) - if !t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { - err = fmt.Errorf("%T is not a VaryingDataType", in) - return +func mustNewVaryingDataTypeSliceAndSet(vdt VaryingDataType, values ...VaryingDataTypeValue) (vdts VaryingDataTypeSlice) { + vdts = NewVaryingDataTypeSlice(vdt) + err := vdts.Add(values...) + if err != nil { + panic(err) } + return +} - key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) - _, ok := vdtCache[key] +// VaryingDataType is analogous to a rust enum. Name is taken from polkadot spec. +type VaryingDataType struct { + value VaryingDataTypeValue + cache map[uint]VaryingDataTypeValue +} + +// Set will set the VaryingDataType value +func (vdt *VaryingDataType) Set(value VaryingDataTypeValue) (err error) { + _, ok := vdt.cache[value.Index()] if !ok { - vdtCache[key] = make(map[uint]VaryingDataTypeValue) + err = fmt.Errorf("unable to append VaryingDataTypeValue: %T, not in cache", value) + return } - for _, val := range values { - vdtCache[key][val.Index()] = val + vdt.value = value + return +} + +// Value returns value stored in vdt +func (vdt *VaryingDataType) Value() VaryingDataTypeValue { + return vdt.value +} + +// NewVaryingDataType is constructor for VaryingDataType +func NewVaryingDataType(values ...VaryingDataTypeValue) (vdt VaryingDataType, err error) { + if len(values) == 0 { + err = fmt.Errorf("must provide atleast one VaryingDataTypeValue") + return + } + vdt.cache = make(map[uint]VaryingDataTypeValue) + for _, value := range values { + _, ok := vdt.cache[value.Index()] + if ok { + err = fmt.Errorf("duplicate index with VaryingDataType: %T with index: %d", value, value.Index()) + return + } + vdt.cache[value.Index()] = value } return } -// VaryingDataType is used to represent scale encodable types -type VaryingDataTypeValue interface { - Index() uint +func MustNewVaryingDataType(values ...VaryingDataTypeValue) (vdt VaryingDataType) { + vdt, err := NewVaryingDataType(values...) + if err != nil { + panic(err) + } + return } diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index 78200cf3f5..3bdba09340 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -25,6 +25,23 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" ) +func mustNewVaryingDataType(values ...VaryingDataTypeValue) (vdt VaryingDataType) { + vdt, err := NewVaryingDataType(values...) + if err != nil { + panic(err) + } + return +} + +func mustNewVaryingDataTypeAndSet(value VaryingDataTypeValue, values ...VaryingDataTypeValue) (vdt VaryingDataType) { + vdt = mustNewVaryingDataType(values...) + err := vdt.Set(value) + if err != nil { + panic(err) + } + return +} + type VDTValue struct { A *big.Int B int @@ -98,24 +115,14 @@ func (ctrd VDTValue3) Index() uint { return 4 } -type testVDT VaryingDataType - -func init() { - err := RegisterVaryingDataType(testVDT{}, VDTValue{}, VDTValue2{}, VDTValue1{}, VDTValue3(0)) - if err != nil { - panic(err) - } -} - var varyingDataTypeTests = tests{ { - in: testVDT{ - VDTValue1{ - O: newBigIntPtr(big.NewInt(1073741823)), - }, - }, + in: mustNewVaryingDataTypeAndSet( + VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), want: []byte{ - 4, 2, + 2, 0x01, 0xfe, 0xff, 0xff, 0xff, 0x00, 0x00, @@ -133,7 +140,7 @@ var varyingDataTypeTests = tests{ }, }, { - in: testVDT{ + in: mustNewVaryingDataTypeAndSet( VDTValue{ A: big.NewInt(1073741823), B: int(1073741823), @@ -150,6 +157,32 @@ var varyingDataTypeTests = tests{ M: testStrings[1], N: true, }, + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + want: newWant( + // index of VDTValue + []byte{1}, + // encoding of struct + []byte{ + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, + 0x01, + 0x01, + 0xff, 0x3f, + 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + }, + append([]byte{0x01, 0x01}, byteArray(64)...), + append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), + []byte{0x01}, + ), + }, + { + in: mustNewVaryingDataTypeAndSet( VDTValue1{ O: newBigIntPtr(big.NewInt(1073741823)), P: newIntPtr(int(1073741823)), @@ -166,6 +199,32 @@ var varyingDataTypeTests = tests{ AA: newStringPtr(testStrings[1]), AB: newBoolPtr(true), }, + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + want: newWant( + // index of VDTValue1 + []byte{2}, + // encoding of struct + []byte{ + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x01, 0x01, + 0x01, 0x01, + 0x01, 0xff, 0x3f, + 0x01, 0xff, 0x3f, + 0x01, 0xff, 0xff, 0xff, 0x3f, + 0x01, 0xff, 0xff, 0xff, 0x3f, + 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + }, + append([]byte{0x01, 0x01, 0x01}, byteArray(64)...), + append([]byte{0x01, 0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), + []byte{0x01, 0x01}, + ), + }, + { + in: mustNewVaryingDataTypeAndSet( VDTValue2{ A: MyStruct{ Foo: []byte{0x01}, @@ -202,51 +261,9 @@ var varyingDataTypeTests = tests{ O: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, P: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, }, - VDTValue3(16383), - }, + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), want: newWant( - // length encoding of 3 - []byte{16}, - // index of VDTValue - []byte{1}, - // encoding of struct - []byte{ - 0xfe, 0xff, 0xff, 0xff, - 0xfe, 0xff, 0xff, 0xff, - 0xfe, 0xff, 0xff, 0xff, - 0x01, - 0x01, - 0xff, 0x3f, - 0xff, 0x3f, - 0xff, 0xff, 0xff, 0x3f, - 0xff, 0xff, 0xff, 0x3f, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, - }, - append([]byte{0x01, 0x01}, byteArray(64)...), - append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), - []byte{0x01}, - - // index of VDTValue1 - []byte{2}, - // encoding of struct - []byte{ - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x01, 0x01, - 0x01, 0x01, - 0x01, 0xff, 0x3f, - 0x01, 0xff, 0x3f, - 0x01, 0xff, 0xff, 0xff, 0x3f, - 0x01, 0xff, 0xff, 0xff, 0x3f, - 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, - 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, - }, - append([]byte{0x01, 0x01, 0x01}, byteArray(64)...), - append([]byte{0x01, 0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), - []byte{0x01, 0x01}, - // index of VDTValue2 []byte{3}, // encoding of struct @@ -268,7 +285,14 @@ var varyingDataTypeTests = tests{ []byte{0x00, 0x04}, []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, []byte{0x00, 0x01, 0x01, 0x00}, - + ), + }, + { + in: mustNewVaryingDataTypeAndSet( + VDTValue3(16383), + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + want: newWant( // index of VDTValue2 []byte{4}, // encoding of int16 @@ -281,7 +305,8 @@ func Test_encodeState_encodeVaryingDataType(t *testing.T) { for _, tt := range varyingDataTypeTests { t.Run(tt.name, func(t *testing.T) { es := &encodeState{fieldScaleIndicesCache: cache} - if err := es.marshal(tt.in); (err != nil) != tt.wantErr { + vdt := tt.in.(VaryingDataType) + if err := es.marshal(vdt); (err != nil) != tt.wantErr { t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) } if !reflect.DeepEqual(es.Buffer.Bytes(), tt.want) { @@ -294,15 +319,251 @@ func Test_encodeState_encodeVaryingDataType(t *testing.T) { func Test_decodeState_decodeVaryingDataType(t *testing.T) { for _, tt := range varyingDataTypeTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + dst, err := NewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0)) + if err != nil { + t.Errorf("%v", err) + return + } if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) return } - diff := cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) + vdt := tt.in.(VaryingDataType) + diff := cmp.Diff(dst.Value(), vdt.Value(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) if diff != "" { t.Errorf("decodeState.unmarshal() = %s", diff) } }) } } + +func TestNewVaryingDataType(t *testing.T) { + type args struct { + values []VaryingDataTypeValue + } + tests := []struct { + name string + args args + wantVdt VaryingDataType + wantErr bool + }{ + { + args: args{ + values: []VaryingDataTypeValue{}, + }, + wantErr: true, + }, + { + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + }, + }, + wantVdt: VaryingDataType{ + cache: map[uint]VaryingDataTypeValue{ + VDTValue{}.Index(): VDTValue{}, + VDTValue1{}.Index(): VDTValue1{}, + VDTValue2{}.Index(): VDTValue2{}, + VDTValue3(0).Index(): VDTValue3(0), + }, + }, + }, + { + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), VDTValue{}, + }, + }, + wantVdt: VaryingDataType{ + cache: map[uint]VaryingDataTypeValue{ + VDTValue{}.Index(): VDTValue{}, + VDTValue1{}.Index(): VDTValue1{}, + VDTValue2{}.Index(): VDTValue2{}, + VDTValue3(0).Index(): VDTValue3(0), + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotVdt, err := NewVaryingDataType(tt.args.values...) + if (err != nil) != tt.wantErr { + t.Errorf("NewVaryingDataType() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotVdt, tt.wantVdt) { + t.Errorf("NewVaryingDataType() = %v, want %v", gotVdt, tt.wantVdt) + } + }) + } +} + +func TestVaryingDataType_Set(t *testing.T) { + type args struct { + value VaryingDataTypeValue + } + tests := []struct { + name string + vdt VaryingDataType + args args + wantErr bool + }{ + { + vdt: mustNewVaryingDataType(VDTValue1{}), + args: args{ + value: VDTValue1{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue1{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue2{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue3(0), + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + vdt := tt.vdt + if err := vdt.Set(tt.args.value); (err != nil) != tt.wantErr { + t.Errorf("VaryingDataType.SetValue() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestVaryingDataTypeSlice_Add(t *testing.T) { + type args struct { + values []VaryingDataTypeValue + } + tests := []struct { + name string + vdts VaryingDataTypeSlice + args args + wantErr bool + wantValues []VaryingDataType + }{ + { + name: "happy path", + vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0))), + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{ + B: 1, + }, + }, + }, + wantValues: []VaryingDataType{ + mustNewVaryingDataTypeAndSet( + VDTValue{ + B: 1, + }, + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + }, + }, + { + name: "invalid value error case", + vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{})), + args: args{ + values: []VaryingDataTypeValue{ + VDTValue3(0), + }, + }, + wantValues: []VaryingDataType{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + vdts := &tt.vdts + if err := vdts.Add(tt.args.values...); (err != nil) != tt.wantErr { + t.Errorf("VaryingDataTypeSlice.Add() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(vdts.Values, tt.wantValues) { + t.Errorf("NewVaryingDataType() = %v, want %v", vdts.Values, tt.wantValues) + } + }) + } +} + +var varyingDataTypeSliceTests = tests{ + { + in: mustNewVaryingDataTypeSliceAndSet( + mustNewVaryingDataType( + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, + ), + want: []byte{ + // length + 4, + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + }, +} + +func Test_encodeState_encodeVaryingDataTypeSlice(t *testing.T) { + for _, tt := range varyingDataTypeSliceTests { + t.Run(tt.name, func(t *testing.T) { + vdt := tt.in.(VaryingDataTypeSlice) + b, err := Marshal(vdt) + if (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(b, tt.want) { + t.Errorf("encodeState.encodeStruct() = %v, want %v", b, tt.want) + } + }) + } +} + +// func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { +// for _, tt := range varyingDataTypeTests { +// t.Run(tt.name, func(t *testing.T) { +// dst, err := NewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0)) +// if err != nil { +// t.Errorf("%v", err) +// return +// } +// if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { +// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// return +// } +// vdt := tt.in.(VaryingDataType) +// diff := cmp.Diff(dst.Value(), vdt.Value(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) +// if diff != "" { +// t.Errorf("decodeState.unmarshal() = %s", diff) +// } +// }) +// } +// } From 135066800daa29b8191f3761d03b2d3e7c8bd7bf Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 21 Jun 2021 11:00:07 -0400 Subject: [PATCH 037/245] refactor VaryingDataType and add VaryingDataTypeSlice --- pkg/scale/decode.go | 22 +++ pkg/scale/encode.go | 2 +- pkg/scale/varying_data_type.go | 6 +- pkg/scale/varying_data_type_example_test.go | 115 ++++++++++++++ pkg/scale/varying_data_type_test.go | 161 ++++++++++++++------ 5 files changed, 257 insertions(+), 49 deletions(-) create mode 100644 pkg/scale/varying_data_type_example_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 6ab8f14460..a26dde3156 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -120,6 +120,8 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { err = ds.decodeResult(dstv) case VaryingDataType: err = ds.decodeVaryingDataType(dstv) + case VaryingDataTypeSlice: + err = ds.decodeVaryingDataTypeSlice(dstv) default: t := reflect.TypeOf(in) switch t.Kind() { @@ -295,6 +297,26 @@ func (ds *decodeState) decodePointer(dstv reflect.Value) (err error) { return } +func (ds *decodeState) decodeVaryingDataTypeSlice(dstv reflect.Value) (err error) { + vdts := dstv.Interface().(VaryingDataTypeSlice) + l, err := ds.decodeLength() + if err != nil { + return + } + for i := 0; i < l; i++ { + vdt := vdts.VaryingDataType + vdtv := reflect.New(reflect.TypeOf(vdt)) + vdtv.Elem().Set(reflect.ValueOf(vdt)) + err = ds.unmarshal(vdtv.Elem()) + if err != nil { + return + } + vdts.Types = append(vdts.Types, vdtv.Elem().Interface().(VaryingDataType)) + } + dstv.Set(reflect.ValueOf(vdts)) + return +} + func (ds *decodeState) decodeVaryingDataType(dstv reflect.Value) (err error) { var b byte b, err = ds.ReadByte() diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index d9026c5ca4..9e15b2e30e 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -170,7 +170,7 @@ func (es *encodeState) encodeVaryingDataType(vdt VaryingDataType) (err error) { } func (es *encodeState) encodeVaryingDataTypeSlice(vdts VaryingDataTypeSlice) (err error) { - err = es.marshal(vdts.Values) + err = es.marshal(vdts.Types) return } diff --git a/pkg/scale/varying_data_type.go b/pkg/scale/varying_data_type.go index b5f3e19d76..f37a01a885 100644 --- a/pkg/scale/varying_data_type.go +++ b/pkg/scale/varying_data_type.go @@ -29,7 +29,7 @@ type VaryingDataTypeValue interface { // of the underlying data, so it is required to have the VaryingDataType required for decoding type VaryingDataTypeSlice struct { VaryingDataType - Values []VaryingDataType + Types []VaryingDataType } // Add takes variadic parameter values to add VaryingDataTypeValue(s) @@ -40,7 +40,7 @@ func (vdts *VaryingDataTypeSlice) Add(values ...VaryingDataTypeValue) (err error if err != nil { return } - vdts.Values = append(vdts.Values, copied) + vdts.Types = append(vdts.Types, copied) } return } @@ -48,7 +48,7 @@ func (vdts *VaryingDataTypeSlice) Add(values ...VaryingDataTypeValue) (err error // NewVaryingDataTypeSlice is constructor for VaryingDataTypeSlice func NewVaryingDataTypeSlice(vdt VaryingDataType) (vdts VaryingDataTypeSlice) { vdts.VaryingDataType = vdt - vdts.Values = make([]VaryingDataType, 0) + vdts.Types = make([]VaryingDataType, 0) return } diff --git a/pkg/scale/varying_data_type_example_test.go b/pkg/scale/varying_data_type_example_test.go new file mode 100644 index 0000000000..b14d5c605c --- /dev/null +++ b/pkg/scale/varying_data_type_example_test.go @@ -0,0 +1,115 @@ +package scale_test + +import ( + "fmt" + "reflect" + "testing" + + "github.com/ChainSafe/gossamer/pkg/scale" +) + +type MyStruct struct { + Baz bool + Bar uint32 + Foo []byte +} + +func (ms MyStruct) Index() uint { + return 1 +} + +type MyOtherStruct struct { + Foo string + Bar uint64 + Baz uint +} + +func (mos MyOtherStruct) Index() uint { + return 2 +} + +type MyInt16 int16 + +func (mi16 MyInt16) Index() uint { + return 3 +} + +func ExampleVaryingDataType() { + vdt, err := scale.NewVaryingDataType(MyStruct{}, MyOtherStruct{}, MyInt16(0)) + if err != nil { + panic(err) + } + + err = vdt.Set(MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + }) + if err != nil { + panic(err) + } + + bytes, err := scale.Marshal(vdt) + if err != nil { + panic(err) + } + + vdt1, err := scale.NewVaryingDataType(MyStruct{}, MyOtherStruct{}, MyInt16(0)) + if err != nil { + panic(err) + } + + err = scale.Unmarshal(bytes, &vdt1) + if err != nil { + panic(err) + } + + if !reflect.DeepEqual(vdt, vdt1) { + panic(fmt.Errorf("uh oh: %+v %+v", vdt, vdt1)) + } +} + +func ExampleVaryingDataTypeSlice() { + vdt, err := scale.NewVaryingDataType(MyStruct{}, MyOtherStruct{}, MyInt16(0)) + if err != nil { + panic(err) + } + + vdts := scale.NewVaryingDataTypeSlice(vdt) + + err = vdts.Add( + MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + }, + MyInt16(1), + ) + if err != nil { + panic(err) + } + + bytes, err := scale.Marshal(vdts) + if err != nil { + panic(err) + } + + vdts1 := scale.NewVaryingDataTypeSlice(vdt) + if err != nil { + panic(err) + } + + err = scale.Unmarshal(bytes, &vdts1) + if err != nil { + panic(err) + } + + if !reflect.DeepEqual(vdts, vdts1) { + panic(fmt.Errorf("uh oh: %+v %+v", vdts, vdts1)) + } +} + +func TestExamples(t *testing.T) { + ExampleVaryingDataType() + ExampleVaryingDataTypeSlice() +} diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index 3bdba09340..d92dd7c723 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -493,8 +493,8 @@ func TestVaryingDataTypeSlice_Add(t *testing.T) { if err := vdts.Add(tt.args.values...); (err != nil) != tt.wantErr { t.Errorf("VaryingDataTypeSlice.Add() error = %v, wantErr %v", err, tt.wantErr) } - if !reflect.DeepEqual(vdts.Values, tt.wantValues) { - t.Errorf("NewVaryingDataType() = %v, want %v", vdts.Values, tt.wantValues) + if !reflect.DeepEqual(vdts.Types, tt.wantValues) { + t.Errorf("NewVaryingDataType() = %v, want %v", vdts.Types, tt.wantValues) } }) } @@ -508,27 +508,97 @@ var varyingDataTypeSliceTests = tests{ ), VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, ), - want: []byte{ - // length - 4, - // index - 2, - // value - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - }, + want: newWant( + []byte{ + // length + 4, + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + ), + }, + { + in: mustNewVaryingDataTypeSliceAndSet( + mustNewVaryingDataType( + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, + VDTValue{ + A: big.NewInt(1073741823), + B: int(1073741823), + C: uint(1073741823), + D: int8(1), + E: uint8(1), + F: int16(16383), + G: uint16(16383), + H: int32(1073741823), + I: uint32(1073741823), + J: int64(9223372036854775807), + K: uint64(9223372036854775807), + L: byteArray(64), + M: testStrings[1], + N: true, + }, + ), + want: newWant( + []byte{ + // length + 8, + }, + []byte{ + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + []byte{ + // index + 1, + // value + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, + 0x01, + 0x01, + 0xff, 0x3f, + 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + }, + append([]byte{0x01, 0x01}, byteArray(64)...), + append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), + []byte{0x01}, + ), }, } @@ -538,32 +608,33 @@ func Test_encodeState_encodeVaryingDataTypeSlice(t *testing.T) { vdt := tt.in.(VaryingDataTypeSlice) b, err := Marshal(vdt) if (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr) } if !reflect.DeepEqual(b, tt.want) { - t.Errorf("encodeState.encodeStruct() = %v, want %v", b, tt.want) + t.Errorf("Marshal() = %v, want %v", b, tt.want) } }) } } -// func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { -// for _, tt := range varyingDataTypeTests { -// t.Run(tt.name, func(t *testing.T) { -// dst, err := NewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0)) -// if err != nil { -// t.Errorf("%v", err) -// return -// } -// if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { -// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// vdt := tt.in.(VaryingDataType) -// diff := cmp.Diff(dst.Value(), vdt.Value(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) -// if diff != "" { -// t.Errorf("decodeState.unmarshal() = %s", diff) -// } -// }) -// } -// } +func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { + opt := cmp.Comparer(func(x, y VaryingDataType) bool { + return reflect.DeepEqual(x.value, y.value) && reflect.DeepEqual(x.cache, y.cache) + }) + + for _, tt := range varyingDataTypeSliceTests { + t.Run(tt.name, func(t *testing.T) { + dst := tt.in.(VaryingDataTypeSlice) + dst.Types = make([]VaryingDataType, 0) + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + vdts := tt.in.(VaryingDataTypeSlice) + diff := cmp.Diff(dst, vdts, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}), opt) + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) + } +} From 6c5c755fce48ad885b16bd86f2ea39e143aab1c4 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 21 Jun 2021 11:10:48 -0400 Subject: [PATCH 038/245] update README --- pkg/scale/README.md | 79 ++++++++++++++++++++++++-------- pkg/scale/result_example_test.go | 6 +-- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/pkg/scale/README.md b/pkg/scale/README.md index 029c022abc..47d53dbc85 100644 --- a/pkg/scale/README.md +++ b/pkg/scale/README.md @@ -210,10 +210,8 @@ func resultExample() { ### Varying Data Type -A `VaryingDataType` is analogous to a Rust enum. A `VaryingDataType` needs to be registered using the `RegisterVaryingDataType` function with its associated `VaryingDataTypeValue` types. `VaryingDataTypeValue` is an -interface with one `Index() uint` method that needs to be implemented. The returned `uint` index should be unique per type and needs to be the same index as defined in the Rust enum to ensure interopability. - -> TODO: The only custom `VaryingDataTypeValue` types supported are currently `struct`, `int`, and `int16`. Need to add other supported primitives. +A `VaryingDataType` is analogous to a Rust enum. A `VaryingDataType` needs to be constructed using the `NewVaryingDataType` constructor. `VaryingDataTypeValue` is an +interface with one `Index() uint` method that needs to be implemented. The returned `uint` index should be unique per type and needs to be the same index as defined in the Rust enum to ensure interopability. To set the value of the `VaryingDataType`, the `VaryingDataType.Set()` function should be called with an associated `VaryingDataTypeValue`. ``` import ( @@ -247,39 +245,82 @@ func (mi16 MyInt16) Index() uint { return 3 } -type MyVaryingDataType scale.VaryingDataType +func ExampleVaryingDataType() { + vdt, err := scale.NewVaryingDataType(MyStruct{}, MyOtherStruct{}, MyInt16(0)) + if err != nil { + panic(err) + } + + err = vdt.Set(MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + }) + if err != nil { + panic(err) + } + + bytes, err := scale.Marshal(vdt) + if err != nil { + panic(err) + } + + vdt1, err := scale.NewVaryingDataType(MyStruct{}, MyOtherStruct{}, MyInt16(0)) + if err != nil { + panic(err) + } + + err = scale.Unmarshal(bytes, &vdt1) + if err != nil { + panic(err) + } + + if !reflect.DeepEqual(vdt, vdt1) { + panic(fmt.Errorf("uh oh: %+v %+v", vdt, vdt1)) + } +} +``` + +A `VaryingDataTypeSlice` is a slice containing multiple `VaryingDataType` elements. Each `VaryingDataTypeValue` must be of a supported type of the `VaryingDataType` passed into the `NewVaryingDataTypeSlice` constructor. The method to call to add `VaryingDataTypeValue` instances is `VaryingDataTypeSlice.Add()`. -func varyingDataTypeExample() { - err := scale.RegisterVaryingDataType(MyVaryingDataType{}, MyStruct{}, MyOtherStruct{}, MyInt16(0)) +``` +func ExampleVaryingDataTypeSlice() { + vdt, err := scale.NewVaryingDataType(MyStruct{}, MyOtherStruct{}, MyInt16(0)) if err != nil { panic(err) } - mvdt := MyVaryingDataType{ + vdts := scale.NewVaryingDataTypeSlice(vdt) + + err = vdts.Add( MyStruct{ Baz: true, Bar: 999, Foo: []byte{1, 2}, }, - MyOtherStruct{ - Foo: "hello", - Bar: 999, - Baz: 888, - }, - MyInt16(111), + MyInt16(1), + ) + if err != nil { + panic(err) } - bytes, err := scale.Marshal(mvdt) + + bytes, err := scale.Marshal(vdts) if err != nil { panic(err) } - var unmarshaled MyVaryingDataType - err = scale.Unmarshal(bytes, &unmarshaled) + vdts1 := scale.NewVaryingDataTypeSlice(vdt) if err != nil { panic(err) } - // [{Baz:true Bar:999 Foo:[1 2]} {Foo:hello Bar:999 Baz:888} 111] - fmt.Printf("%+v", unmarshaled) + err = scale.Unmarshal(bytes, &vdts1) + if err != nil { + panic(err) + } + + if !reflect.DeepEqual(vdts, vdts1) { + panic(fmt.Errorf("uh oh: %+v %+v", vdts, vdts1)) + } } ``` \ No newline at end of file diff --git a/pkg/scale/result_example_test.go b/pkg/scale/result_example_test.go index d32c26df0b..43d7084133 100644 --- a/pkg/scale/result_example_test.go +++ b/pkg/scale/result_example_test.go @@ -7,7 +7,7 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) -func resultExample() { +func ExampleResult() { // pass in zero or non-zero values of the types for Ok and Err cases res := scale.NewResult(bool(false), string("")) @@ -48,6 +48,6 @@ func resultExample() { } } -func TestSomething2(t *testing.T) { - resultExample() +func TestExampleResult(t *testing.T) { + ExampleResult() } From c356f7ffa47f96f43fe323c56cba546129cb401a Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 21 Jun 2021 12:46:24 -0400 Subject: [PATCH 039/245] fix lint --- pkg/scale/decode.go | 7 ++++--- pkg/scale/encode.go | 1 + pkg/scale/encode_test.go | 13 +++++-------- pkg/scale/result.go | 6 +++--- pkg/scale/uint128.go | 4 ++-- pkg/scale/varying_data_type.go | 3 ++- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index a26dde3156..f08029485e 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -27,7 +27,7 @@ import ( // indirect walks down v allocating pointers as needed, // until it gets to a non-pointer. -func indirect(dstv reflect.Value) (elem reflect.Value, err error) { +func indirect(dstv reflect.Value) (elem reflect.Value) { dstv0 := dstv haveAddr := false for { @@ -68,6 +68,7 @@ func indirect(dstv reflect.Value) (elem reflect.Value, err error) { return } +// Unmarshal takes data and a destination pointer to unmarshal the data to. func Unmarshal(data []byte, dst interface{}) (err error) { dstv := reflect.ValueOf(dst) if dstv.Kind() != reflect.Ptr || dstv.IsNil() { @@ -75,7 +76,7 @@ func Unmarshal(data []byte, dst interface{}) (err error) { return } - elem, err := indirect(dstv) + elem := indirect(dstv) if err != nil { return } @@ -581,7 +582,7 @@ func (ds *decodeState) decodeFixedWidthInt(dstv reflect.Value) (err error) { if err != nil { return } - out = uint8(b) + out = uint8(b) // nolint case int16: buf := make([]byte, 2) _, err = ds.Read(buf) diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 9e15b2e30e..7c62a77bd5 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -24,6 +24,7 @@ import ( "reflect" ) +// Marshal takes in an interface{} and attempts to marshal into []byte func Marshal(v interface{}) (b []byte, err error) { es := encodeState{ fieldScaleIndicesCache: cache, diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 8be29486ab..4f9deedbd4 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -511,14 +511,7 @@ var ( }, } - nilPtrMyStruct *MyStruct - ptrMystruct *MyStruct = &MyStruct{ - Foo: []byte{0x01}, - Bar: 2, - Baz: true, - } - nilPtrMyStruct2 *MyStruct = nil - structTests = tests{ + structTests = tests{ { name: "struct {[]byte, int32}", in: MyStruct{ @@ -604,6 +597,7 @@ var ( Foo: []byte{0x01}, Bar: 2, Baz: true, + priv1: []byte{0x00}, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, }, @@ -614,6 +608,9 @@ var ( Bar: 2, Baz: true, Ignore: "me", + somethingElse: &struct { + fields int + }{1}, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, out: MyStructWithIgnore{ diff --git a/pkg/scale/result.go b/pkg/scale/result.go index 88e18c0aa8..147d314e9c 100644 --- a/pkg/scale/result.go +++ b/pkg/scale/result.go @@ -27,7 +27,7 @@ type ResultMode int const ( // Unset ResultMode is zero value mode Unset ResultMode = iota - // Ok case + // OK case OK // Err case Err @@ -83,7 +83,7 @@ func (r *Result) Set(mode ResultMode, in interface{}) (err error) { // UnsetResult is error when Result is unset with a value. type UnsetResult error -// Result returns the result in go standard wrapping the Err case in a ResultErr +// Unwrap returns the result in go standard wrapping the Err case in a ResultErr func (r *Result) Unwrap() (ok interface{}, err error) { if !r.IsSet() { err = UnsetResult(fmt.Errorf("result is not set")) @@ -128,7 +128,7 @@ type WrappedErr struct { Err interface{} } -// Error fulfills the error interface +// Error fulfils the error interface func (r WrappedErr) Error() string { return fmt.Sprintf("ResultErr %+v", r.Err) } diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index 53dcf05282..12e00c42b9 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -87,7 +87,7 @@ func NewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128, err erro return } -// Bytes returns the Uint128 in little endian format by default. A variadic paramter +// Bytes returns the Uint128 in little endian format by default. A variadic parameter // order can be used to specify the binary.ByteOrder used func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { var o binary.ByteOrder = binary.LittleEndian @@ -108,7 +108,7 @@ func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { return } -// Cmp returns 1 if the receiver is greater than other, 0 if they are equal, and -1 otherwise. +// Compare returns 1 if the receiver is greater than other, 0 if they are equal, and -1 otherwise. func (u *Uint128) Compare(other *Uint128) int { switch { case u.Upper > other.Upper: diff --git a/pkg/scale/varying_data_type.go b/pkg/scale/varying_data_type.go index f37a01a885..aa01c8ae56 100644 --- a/pkg/scale/varying_data_type.go +++ b/pkg/scale/varying_data_type.go @@ -20,7 +20,7 @@ import ( "fmt" ) -// VaryingDataType is used to represent scale encodable types +// VaryingDataTypeValue is used to represent scale encodable types of an associated VaryingDataType type VaryingDataTypeValue interface { Index() uint } @@ -101,6 +101,7 @@ func NewVaryingDataType(values ...VaryingDataTypeValue) (vdt VaryingDataType, er return } +// MustNewVaryingDataType is constructor for VaryingDataType func MustNewVaryingDataType(values ...VaryingDataTypeValue) (vdt VaryingDataType) { vdt, err := NewVaryingDataType(values...) if err != nil { From 8ba49eb8e998bff2a2ed8e1ba580a65bd09ff6c3 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 21 Jun 2021 11:03:11 -0600 Subject: [PATCH 040/245] integrate scale pkg into lib/transaction --- go.mod | 2 +- lib/transaction/types.go | 35 ----------------------------------- lib/transaction/types_test.go | 3 ++- pkg/scale/decode.go | 2 +- 4 files changed, 4 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index 519258043d..1410d97cf1 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 // indirect github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect - github.com/google/go-cmp v0.5.6 // indirect + github.com/google/go-cmp v0.5.6 github.com/google/uuid v1.1.5 // indirect github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 diff --git a/lib/transaction/types.go b/lib/transaction/types.go index 9c743c84a3..6d538ab37f 100644 --- a/lib/transaction/types.go +++ b/lib/transaction/types.go @@ -17,10 +17,7 @@ package transaction import ( - "encoding/binary" - "github.com/ChainSafe/gossamer/dot/types" - "github.com/ChainSafe/gossamer/lib/scale" ) // Validity struct see: https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/sr-primitives/src/transaction_validity.rs#L178 @@ -56,35 +53,3 @@ func NewValidTransaction(extrinsic types.Extrinsic, validity *Validity) *ValidTr Validity: validity, } } - -// Encode SCALE encodes the transaction -func (vt *ValidTransaction) Encode() ([]byte, error) { - enc := []byte(vt.Extrinsic) - - buf := make([]byte, 8) - binary.LittleEndian.PutUint64(buf, vt.Validity.Priority) - enc = append(enc, buf...) - - d, err := scale.Encode(vt.Validity.Requires) - if err != nil { - return nil, err - } - enc = append(enc, d...) - - d, err = scale.Encode(vt.Validity.Provides) - if err != nil { - return nil, err - } - enc = append(enc, d...) - - binary.LittleEndian.PutUint64(buf, vt.Validity.Longevity) - enc = append(enc, buf...) - - if vt.Validity.Propagate { - enc = append(enc, 1) - } else { - enc = append(enc, 0) - } - - return enc, nil -} diff --git a/lib/transaction/types_test.go b/lib/transaction/types_test.go index b1e9062a27..d59545dfc8 100644 --- a/lib/transaction/types_test.go +++ b/lib/transaction/types_test.go @@ -3,6 +3,7 @@ package transaction import ( "testing" + "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) @@ -18,7 +19,7 @@ func TestValidTransaction_Encode(t *testing.T) { extrinsic := []byte("nootwashere") vt := NewValidTransaction(extrinsic, validity) - enc, err := vt.Encode() + enc, err := scale.Marshal(vt) require.NoError(t, err) if len(enc) == 0 { diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 0f3d31fb85..773848fdf7 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -583,4 +583,4 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { } dstv.Set(reflect.ValueOf(ui128)) return -} +} \ No newline at end of file From e7906bf78745a4f55c4070a87323a552a09d72e3 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 21 Jun 2021 13:07:56 -0400 Subject: [PATCH 041/245] update examples --- pkg/scale/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/scale/README.md b/pkg/scale/README.md index 47d53dbc85..6e97e6b1eb 100644 --- a/pkg/scale/README.md +++ b/pkg/scale/README.md @@ -88,7 +88,7 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) -func basicExample() { +func ExampleBasic() { // compact length encoded uint var ui uint = 999 bytes, err := scale.Marshal(ui) @@ -117,7 +117,7 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) -func structExample() { +func ExampleStruct() { type MyStruct struct { Baz bool `scale:"3"` Bar int32 `scale:"2"` @@ -165,7 +165,7 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) -func resultExample() { +func ExampleResult() { // pass in zero or non-zero values of the types for Ok and Err cases res := scale.NewResult(bool(false), string("")) From 20ffc9899084d60a987cb46e7e9d68c2e9cd786e Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 21 Jun 2021 13:10:59 -0400 Subject: [PATCH 042/245] fix deepsource --- pkg/scale/result.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/scale/result.go b/pkg/scale/result.go index 147d314e9c..21f93f4c75 100644 --- a/pkg/scale/result.go +++ b/pkg/scale/result.go @@ -41,7 +41,7 @@ type Result struct { } // NewResult is constructor for Result. Use nil to represent empty tuple () in Rust. -func NewResult(okIn interface{}, errIn interface{}) (res Result) { +func NewResult(okIn, errIn interface{}) (res Result) { switch okIn { case nil: res.ok = empty{} From 15e8f5b8054aaf3efc5ec2e043460d225505ae2b Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 11:01:44 -0600 Subject: [PATCH 043/245] WIP/Integrate scale pkg into lib/babe --- lib/babe/errors.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 16f513d839..7543d028e4 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -16,9 +16,8 @@ package babe import ( "errors" "fmt" - "github.com/ChainSafe/gossamer/lib/common/optional" - "github.com/ChainSafe/gossamer/lib/scale" + "github.com/ChainSafe/gossamer/pkg/scale" ) var ( @@ -94,8 +93,15 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { switch res[0] { case 0: - unKnownError, _ := scale.Decode(res[1:], []byte{}) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", string(unKnownError.([]byte)))} + //unKnownError, _ := scale.Decode(res[1:], []byte{}) + //fmt.Sprintf("Output: %s", string(unKnownError.([]byte))) + //fmt.Println("Output:") + // This has not been working + var v []byte + fmt.Println(res) + unKnownError := scale.Unmarshal(res[1:], &v) + fmt.Println("Output: " + unKnownError.Error()) + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", unKnownError.Error())} case 1: return &DispatchOutcomeError{"failed lookup"} case 2: From 28ea2d13579ca0935b126e1b41350eadd13e011a Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 11:26:59 -0600 Subject: [PATCH 044/245] fixed error unmarshaling in errors.go --- lib/babe/errors.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 7543d028e4..5ec53e6fb3 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -93,15 +93,14 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { switch res[0] { case 0: - //unKnownError, _ := scale.Decode(res[1:], []byte{}) - //fmt.Sprintf("Output: %s", string(unKnownError.([]byte))) - //fmt.Println("Output:") - // This has not been working var v []byte - fmt.Println(res) - unKnownError := scale.Unmarshal(res[1:], &v) - fmt.Println("Output: " + unKnownError.Error()) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", unKnownError.Error())} + err := scale.Unmarshal(res[1:], &v) + if err != nil { + // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure + // TODO Create stucts for errors and integrate into Varying data type + return errInvalidResult + } + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} case 1: return &DispatchOutcomeError{"failed lookup"} case 2: From 4354f42bcdaf10179a029a57b8167c1b88b3a691 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 21 Jun 2021 11:03:11 -0600 Subject: [PATCH 045/245] integrate scale pkg into lib/transaction --- go.mod | 2 +- lib/transaction/types.go | 35 ----------------------------------- lib/transaction/types_test.go | 3 ++- pkg/scale/decode.go | 2 +- 4 files changed, 4 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index 519258043d..1410d97cf1 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 // indirect github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect - github.com/google/go-cmp v0.5.6 // indirect + github.com/google/go-cmp v0.5.6 github.com/google/uuid v1.1.5 // indirect github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 diff --git a/lib/transaction/types.go b/lib/transaction/types.go index 9c743c84a3..6d538ab37f 100644 --- a/lib/transaction/types.go +++ b/lib/transaction/types.go @@ -17,10 +17,7 @@ package transaction import ( - "encoding/binary" - "github.com/ChainSafe/gossamer/dot/types" - "github.com/ChainSafe/gossamer/lib/scale" ) // Validity struct see: https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/sr-primitives/src/transaction_validity.rs#L178 @@ -56,35 +53,3 @@ func NewValidTransaction(extrinsic types.Extrinsic, validity *Validity) *ValidTr Validity: validity, } } - -// Encode SCALE encodes the transaction -func (vt *ValidTransaction) Encode() ([]byte, error) { - enc := []byte(vt.Extrinsic) - - buf := make([]byte, 8) - binary.LittleEndian.PutUint64(buf, vt.Validity.Priority) - enc = append(enc, buf...) - - d, err := scale.Encode(vt.Validity.Requires) - if err != nil { - return nil, err - } - enc = append(enc, d...) - - d, err = scale.Encode(vt.Validity.Provides) - if err != nil { - return nil, err - } - enc = append(enc, d...) - - binary.LittleEndian.PutUint64(buf, vt.Validity.Longevity) - enc = append(enc, buf...) - - if vt.Validity.Propagate { - enc = append(enc, 1) - } else { - enc = append(enc, 0) - } - - return enc, nil -} diff --git a/lib/transaction/types_test.go b/lib/transaction/types_test.go index b1e9062a27..d59545dfc8 100644 --- a/lib/transaction/types_test.go +++ b/lib/transaction/types_test.go @@ -3,6 +3,7 @@ package transaction import ( "testing" + "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) @@ -18,7 +19,7 @@ func TestValidTransaction_Encode(t *testing.T) { extrinsic := []byte("nootwashere") vt := NewValidTransaction(extrinsic, validity) - enc, err := vt.Encode() + enc, err := scale.Marshal(vt) require.NoError(t, err) if len(enc) == 0 { diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index f08029485e..15e96a9e88 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -646,4 +646,4 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { } dstv.Set(reflect.ValueOf(ui128)) return -} +} \ No newline at end of file From 1da714dc639aee3fa9140337163d9bc8ae5cc20b Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 11:01:44 -0600 Subject: [PATCH 046/245] WIP/Integrate scale pkg into lib/babe --- lib/babe/errors.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 16f513d839..7543d028e4 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -16,9 +16,8 @@ package babe import ( "errors" "fmt" - "github.com/ChainSafe/gossamer/lib/common/optional" - "github.com/ChainSafe/gossamer/lib/scale" + "github.com/ChainSafe/gossamer/pkg/scale" ) var ( @@ -94,8 +93,15 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { switch res[0] { case 0: - unKnownError, _ := scale.Decode(res[1:], []byte{}) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", string(unKnownError.([]byte)))} + //unKnownError, _ := scale.Decode(res[1:], []byte{}) + //fmt.Sprintf("Output: %s", string(unKnownError.([]byte))) + //fmt.Println("Output:") + // This has not been working + var v []byte + fmt.Println(res) + unKnownError := scale.Unmarshal(res[1:], &v) + fmt.Println("Output: " + unKnownError.Error()) + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", unKnownError.Error())} case 1: return &DispatchOutcomeError{"failed lookup"} case 2: From e83eda26b30b004c9576fcb769f1a77662d78dbf Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 11:26:59 -0600 Subject: [PATCH 047/245] fixed error unmarshaling in errors.go --- lib/babe/errors.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 7543d028e4..5ec53e6fb3 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -93,15 +93,14 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { switch res[0] { case 0: - //unKnownError, _ := scale.Decode(res[1:], []byte{}) - //fmt.Sprintf("Output: %s", string(unKnownError.([]byte))) - //fmt.Println("Output:") - // This has not been working var v []byte - fmt.Println(res) - unKnownError := scale.Unmarshal(res[1:], &v) - fmt.Println("Output: " + unKnownError.Error()) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", unKnownError.Error())} + err := scale.Unmarshal(res[1:], &v) + if err != nil { + // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure + // TODO Create stucts for errors and integrate into Varying data type + return errInvalidResult + } + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} case 1: return &DispatchOutcomeError{"failed lookup"} case 2: From 310054c70fba3dca92366ea6bacbbc08ff24bba7 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 22 Jun 2021 13:34:31 -0400 Subject: [PATCH 048/245] check decoded in comparison_test --- pkg/scale/comparison_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 23d539b5d8..54f4225da3 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -139,14 +139,15 @@ func TestOldVsNewEncoding(t *testing.T) { t.Errorf("encodeState.encodeStruct() = %v, want %v", oldEncode, newEncode) } - // decoded := []VaryingDataType{} - // err = Unmarshal(newEncode, &decoded) - // if err != nil { - // t.Errorf("unexpected err: %v", err) - // } - // if !reflect.DeepEqual(decoded, newDigest) { - // t.Errorf("Unmarshal() = %v, want %v", decoded, newDigest) - // } + decoded := NewVaryingDataTypeSlice(vdt) + err = Unmarshal(newEncode, &decoded) + if err != nil { + t.Errorf("unexpected err: %v", err) + } + // decoded.Types + if !reflect.DeepEqual(decoded.Types, newDigest) { + t.Errorf("Unmarshal() = %v, want %v", decoded.Types, newDigest) + } } func BenchmarkUnmarshal(b *testing.B) { From deb8a1ea2755c2b184350654b4dd3bfb99d9b558 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 22 Jun 2021 13:36:10 -0400 Subject: [PATCH 049/245] cr feedback --- pkg/scale/varying_data_type.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/scale/varying_data_type.go b/pkg/scale/varying_data_type.go index aa01c8ae56..de524014e2 100644 --- a/pkg/scale/varying_data_type.go +++ b/pkg/scale/varying_data_type.go @@ -54,8 +54,7 @@ func NewVaryingDataTypeSlice(vdt VaryingDataType) (vdts VaryingDataTypeSlice) { func mustNewVaryingDataTypeSliceAndSet(vdt VaryingDataType, values ...VaryingDataTypeValue) (vdts VaryingDataTypeSlice) { vdts = NewVaryingDataTypeSlice(vdt) - err := vdts.Add(values...) - if err != nil { + if err := vdts.Add(values...); err != nil { panic(err) } return From 4a814744c4a018cdafee531218770392e5ec9df8 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 12:03:02 -0600 Subject: [PATCH 050/245] WIP/Integrate scale into lib/babe --- lib/babe/build.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index 8b756625d7..46cee736e3 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -30,6 +30,7 @@ import ( "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" + scale2 "github.com/ChainSafe/gossamer/pkg/scale" ) // construct a block for this slot with the given parent @@ -326,16 +327,17 @@ func hasSlotEnded(slot Slot) bool { return time.Since(slotEnd) >= 0 } -// ExtrinsicsToBody returns scale encoded block body which contains inherent and extrinsic. +// ExtrinsicsToBody returns scale2 encoded block body which contains inherent and extrinsic. func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (*types.Body, error) { extrinsics := types.BytesArrayToExtrinsics(inherents) for _, tx := range txs { - decExt, err := scale.Decode(tx.Extrinsic, []byte{}) + var decExt []byte + err := scale2.Unmarshal(tx.Extrinsic, &decExt) if err != nil { return nil, err } - extrinsics = append(extrinsics, decExt.([]byte)) + extrinsics = append(extrinsics, decExt) } return types.NewBodyFromExtrinsics(extrinsics) From c1f86d63ef2fd9cfdea1ea08678b479d6da836e3 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 13:31:50 -0600 Subject: [PATCH 051/245] integrate scale package into babe library --- lib/babe/build.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index 46cee736e3..cc8cbaf986 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -28,9 +28,8 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/runtime" - "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" - scale2 "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/ChainSafe/gossamer/pkg/scale" ) // construct a block for this slot with the given parent @@ -285,14 +284,15 @@ func (b *BlockBuilder) buildBlockInherents(slot Slot) ([][]byte, error) { } // decode inherent extrinsics - exts, err := scale.Decode(inherentExts, [][]byte{}) + var exts [][]byte + err = scale.Unmarshal(inherentExts, &exts) if err != nil { return nil, err } // apply each inherent extrinsic - for _, ext := range exts.([][]byte) { - in, err := scale.Encode(ext) + for _, ext := range exts { + in, err := scale.Marshal(ext) if err != nil { return nil, err } @@ -308,7 +308,7 @@ func (b *BlockBuilder) buildBlockInherents(slot Slot) ([][]byte, error) { } } - return exts.([][]byte), nil + return exts, nil } func (b *BlockBuilder) addToQueue(txs []*transaction.ValidTransaction) { @@ -333,7 +333,7 @@ func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) ( for _, tx := range txs { var decExt []byte - err := scale2.Unmarshal(tx.Extrinsic, &decExt) + err := scale.Unmarshal(tx.Extrinsic, &decExt) if err != nil { return nil, err } From a44dbe9ec0d8106e2999acbf2d745b4daf730cb6 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 22 Jun 2021 17:01:03 -0400 Subject: [PATCH 052/245] fix result.set with nil value --- pkg/scale/result.go | 12 +++++++--- pkg/scale/result_test.go | 47 +++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/pkg/scale/result.go b/pkg/scale/result.go index 21f93f4c75..5fd30879a4 100644 --- a/pkg/scale/result.go +++ b/pkg/scale/result.go @@ -61,15 +61,21 @@ func NewResult(okIn, errIn interface{}) (res Result) { func (r *Result) Set(mode ResultMode, in interface{}) (err error) { switch mode { case OK: - if reflect.TypeOf(r.ok) != reflect.TypeOf(in) { + if reflect.TypeOf(r.ok) == reflect.TypeOf(empty{}) && in == nil { + r.mode = mode + return + } else if reflect.TypeOf(r.ok) != reflect.TypeOf(in) { err = fmt.Errorf("type mistmatch for result.ok: %T, and inputted: %T", r.ok, in) return } r.ok = in r.mode = mode case Err: - if reflect.TypeOf(r.err) != reflect.TypeOf(in) { - err = fmt.Errorf("type mistmatch for result.ok: %T, and inputted: %T", r.ok, in) + if reflect.TypeOf(r.err) == reflect.TypeOf(empty{}) && in == nil { + r.mode = mode + return + } else if reflect.TypeOf(r.err) != reflect.TypeOf(in) { + err = fmt.Errorf("type mistmatch for result.err: %T, and inputted: %T", r.ok, in) return } r.err = in diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 4140a4254b..d5b5320974 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -216,29 +216,45 @@ func TestResult_Set(t *testing.T) { in interface{} } tests := []struct { - name string - res Result - args args - wantErr bool + name string + res Result + args args + wantErr bool + wantResult Result }{ - // TODO: Add test cases. { args: args{ mode: Unset, }, + res: NewResult(nil, nil), wantErr: true, + wantResult: Result{ + ok: empty{}, err: empty{}, + }, }, { args: args{ mode: OK, in: nil, }, + res: NewResult(nil, nil), + wantResult: Result{ + ok: empty{}, + err: empty{}, + mode: OK, + }, }, { args: args{ mode: Err, in: nil, }, + res: NewResult(nil, nil), + wantResult: Result{ + ok: empty{}, + err: empty{}, + mode: Err, + }, }, { args: args{ @@ -246,6 +262,11 @@ func TestResult_Set(t *testing.T) { in: true, }, res: NewResult(true, nil), + wantResult: Result{ + ok: true, + err: empty{}, + mode: OK, + }, }, { args: args{ @@ -253,6 +274,11 @@ func TestResult_Set(t *testing.T) { in: true, }, res: NewResult(nil, true), + wantResult: Result{ + ok: empty{}, + err: true, + mode: Err, + }, }, { args: args{ @@ -261,6 +287,10 @@ func TestResult_Set(t *testing.T) { }, res: NewResult("ok", "err"), wantErr: true, + wantResult: Result{ + ok: "ok", + err: "err", + }, }, { args: args{ @@ -269,6 +299,10 @@ func TestResult_Set(t *testing.T) { }, res: NewResult(nil, true), wantErr: true, + wantResult: Result{ + ok: empty{}, + err: true, + }, }, } for _, tt := range tests { @@ -277,6 +311,9 @@ func TestResult_Set(t *testing.T) { if err := r.Set(tt.args.mode, tt.args.in); (err != nil) != tt.wantErr { t.Errorf("Result.Set() error = %v, wantErr %v", err, tt.wantErr) } + if !reflect.DeepEqual(tt.wantResult, r) { + t.Errorf("Result.Unwrap() = %v, want %v", tt.wantResult, r) + } }) } } From 9a29cf9d3ae14215b22a0afa9784ffc71c73f4d3 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 21 Jun 2021 11:03:11 -0600 Subject: [PATCH 053/245] integrate scale pkg into lib/transaction --- go.mod | 2 +- lib/transaction/types.go | 35 ----------------------------------- lib/transaction/types_test.go | 3 ++- pkg/scale/decode.go | 2 +- 4 files changed, 4 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index 519258043d..1410d97cf1 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 // indirect github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect - github.com/google/go-cmp v0.5.6 // indirect + github.com/google/go-cmp v0.5.6 github.com/google/uuid v1.1.5 // indirect github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 diff --git a/lib/transaction/types.go b/lib/transaction/types.go index 9c743c84a3..6d538ab37f 100644 --- a/lib/transaction/types.go +++ b/lib/transaction/types.go @@ -17,10 +17,7 @@ package transaction import ( - "encoding/binary" - "github.com/ChainSafe/gossamer/dot/types" - "github.com/ChainSafe/gossamer/lib/scale" ) // Validity struct see: https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/sr-primitives/src/transaction_validity.rs#L178 @@ -56,35 +53,3 @@ func NewValidTransaction(extrinsic types.Extrinsic, validity *Validity) *ValidTr Validity: validity, } } - -// Encode SCALE encodes the transaction -func (vt *ValidTransaction) Encode() ([]byte, error) { - enc := []byte(vt.Extrinsic) - - buf := make([]byte, 8) - binary.LittleEndian.PutUint64(buf, vt.Validity.Priority) - enc = append(enc, buf...) - - d, err := scale.Encode(vt.Validity.Requires) - if err != nil { - return nil, err - } - enc = append(enc, d...) - - d, err = scale.Encode(vt.Validity.Provides) - if err != nil { - return nil, err - } - enc = append(enc, d...) - - binary.LittleEndian.PutUint64(buf, vt.Validity.Longevity) - enc = append(enc, buf...) - - if vt.Validity.Propagate { - enc = append(enc, 1) - } else { - enc = append(enc, 0) - } - - return enc, nil -} diff --git a/lib/transaction/types_test.go b/lib/transaction/types_test.go index b1e9062a27..d59545dfc8 100644 --- a/lib/transaction/types_test.go +++ b/lib/transaction/types_test.go @@ -3,6 +3,7 @@ package transaction import ( "testing" + "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) @@ -18,7 +19,7 @@ func TestValidTransaction_Encode(t *testing.T) { extrinsic := []byte("nootwashere") vt := NewValidTransaction(extrinsic, validity) - enc, err := vt.Encode() + enc, err := scale.Marshal(vt) require.NoError(t, err) if len(enc) == 0 { diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index f08029485e..15e96a9e88 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -646,4 +646,4 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { } dstv.Set(reflect.ValueOf(ui128)) return -} +} \ No newline at end of file From c188027251910205554308166555136048ecaa95 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 11:01:44 -0600 Subject: [PATCH 054/245] WIP/Integrate scale pkg into lib/babe --- lib/babe/errors.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 16f513d839..7543d028e4 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -16,9 +16,8 @@ package babe import ( "errors" "fmt" - "github.com/ChainSafe/gossamer/lib/common/optional" - "github.com/ChainSafe/gossamer/lib/scale" + "github.com/ChainSafe/gossamer/pkg/scale" ) var ( @@ -94,8 +93,15 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { switch res[0] { case 0: - unKnownError, _ := scale.Decode(res[1:], []byte{}) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", string(unKnownError.([]byte)))} + //unKnownError, _ := scale.Decode(res[1:], []byte{}) + //fmt.Sprintf("Output: %s", string(unKnownError.([]byte))) + //fmt.Println("Output:") + // This has not been working + var v []byte + fmt.Println(res) + unKnownError := scale.Unmarshal(res[1:], &v) + fmt.Println("Output: " + unKnownError.Error()) + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", unKnownError.Error())} case 1: return &DispatchOutcomeError{"failed lookup"} case 2: From b5544199ec739b2e3e2812eec94a12ebf55c21dc Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 11:26:59 -0600 Subject: [PATCH 055/245] fixed error unmarshaling in errors.go --- lib/babe/errors.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 7543d028e4..5ec53e6fb3 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -93,15 +93,14 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { switch res[0] { case 0: - //unKnownError, _ := scale.Decode(res[1:], []byte{}) - //fmt.Sprintf("Output: %s", string(unKnownError.([]byte))) - //fmt.Println("Output:") - // This has not been working var v []byte - fmt.Println(res) - unKnownError := scale.Unmarshal(res[1:], &v) - fmt.Println("Output: " + unKnownError.Error()) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", unKnownError.Error())} + err := scale.Unmarshal(res[1:], &v) + if err != nil { + // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure + // TODO Create stucts for errors and integrate into Varying data type + return errInvalidResult + } + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} case 1: return &DispatchOutcomeError{"failed lookup"} case 2: From 0692d431498f108c4672a84d9061ccfc1bab0f47 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 12:03:02 -0600 Subject: [PATCH 056/245] WIP/Integrate scale into lib/babe --- lib/babe/build.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index 8b756625d7..46cee736e3 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -30,6 +30,7 @@ import ( "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" + scale2 "github.com/ChainSafe/gossamer/pkg/scale" ) // construct a block for this slot with the given parent @@ -326,16 +327,17 @@ func hasSlotEnded(slot Slot) bool { return time.Since(slotEnd) >= 0 } -// ExtrinsicsToBody returns scale encoded block body which contains inherent and extrinsic. +// ExtrinsicsToBody returns scale2 encoded block body which contains inherent and extrinsic. func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (*types.Body, error) { extrinsics := types.BytesArrayToExtrinsics(inherents) for _, tx := range txs { - decExt, err := scale.Decode(tx.Extrinsic, []byte{}) + var decExt []byte + err := scale2.Unmarshal(tx.Extrinsic, &decExt) if err != nil { return nil, err } - extrinsics = append(extrinsics, decExt.([]byte)) + extrinsics = append(extrinsics, decExt) } return types.NewBodyFromExtrinsics(extrinsics) From 37d82409701a53585fd846a593467b56a10c76f0 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 13:31:50 -0600 Subject: [PATCH 057/245] integrate scale package into babe library --- lib/babe/build.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index 46cee736e3..cc8cbaf986 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -28,9 +28,8 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/runtime" - "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" - scale2 "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/ChainSafe/gossamer/pkg/scale" ) // construct a block for this slot with the given parent @@ -285,14 +284,15 @@ func (b *BlockBuilder) buildBlockInherents(slot Slot) ([][]byte, error) { } // decode inherent extrinsics - exts, err := scale.Decode(inherentExts, [][]byte{}) + var exts [][]byte + err = scale.Unmarshal(inherentExts, &exts) if err != nil { return nil, err } // apply each inherent extrinsic - for _, ext := range exts.([][]byte) { - in, err := scale.Encode(ext) + for _, ext := range exts { + in, err := scale.Marshal(ext) if err != nil { return nil, err } @@ -308,7 +308,7 @@ func (b *BlockBuilder) buildBlockInherents(slot Slot) ([][]byte, error) { } } - return exts.([][]byte), nil + return exts, nil } func (b *BlockBuilder) addToQueue(txs []*transaction.ValidTransaction) { @@ -333,7 +333,7 @@ func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) ( for _, tx := range txs { var decExt []byte - err := scale2.Unmarshal(tx.Extrinsic, &decExt) + err := scale.Unmarshal(tx.Extrinsic, &decExt) if err != nil { return nil, err } From faabf3656a0d718b692925c6bd64d411d5c4768f Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 15:06:02 -0600 Subject: [PATCH 058/245] WIP/refactor babe errors to utilize new scale pkg --- lib/babe/errors.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 5ec53e6fb3..6c934ff7a0 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -149,9 +149,36 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } +type UnknownError struct { + Err string +} + +func (err UnknownError) Index() uint { + return 1 +} + +type FailedLookup struct { + Err string +} + +func (err FailedLookup) Index() uint { + return 2 +} + +type BadOrigin struct { + Err string +} + +func (err BadOrigin) Index() uint { + return 3 +} + func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome + result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) + fmt.Println("Result err") + fmt.Println(result) switch res[1] { case 0: return nil From 88a4e300d3959904559e0af7dca352014c2b5396 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 15:56:28 -0600 Subject: [PATCH 059/245] Integrate scale pkg into babe tests --- lib/babe/build_test.go | 7 ++++--- lib/babe/errors.go | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/babe/build_test.go b/lib/babe/build_test.go index e84dc8943c..0bfb448403 100644 --- a/lib/babe/build_test.go +++ b/lib/babe/build_test.go @@ -26,8 +26,8 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/sr25519" - "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" + "github.com/ChainSafe/gossamer/pkg/scale" log "github.com/ChainSafe/log15" cscale "github.com/centrifuge/go-substrate-rpc-client/v2/scale" "github.com/centrifuge/go-substrate-rpc-client/v2/signature" @@ -222,11 +222,12 @@ func TestBuildAndApplyExtrinsic(t *testing.T) { // build extrinsic rawMeta, err := babeService.rt.Metadata() require.NoError(t, err) - decoded, err := scale.Decode(rawMeta, []byte{}) + var decoded []byte + err = scale.Unmarshal(rawMeta, []byte{}) require.NoError(t, err) meta := &ctypes.Metadata{} - err = ctypes.DecodeFromBytes(decoded.([]byte), meta) + err = ctypes.DecodeFromBytes(decoded, meta) require.NoError(t, err) rv, err := babeService.rt.Version() diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 6c934ff7a0..3d3430dcfe 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -177,7 +177,11 @@ func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) - fmt.Println("Result err") + //err := result.Set(scale.OK, nil) + //if err != nil { + // panic(err) + //} + fmt.Println("Result") fmt.Println(result) switch res[1] { case 0: From 730a4e70b0b13b06c487783897f5d4ef862b97c4 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 16:22:12 -0600 Subject: [PATCH 060/245] create init structs for dispatch outcome errors --- lib/babe/errors.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 3d3430dcfe..727c3a9ea4 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -173,10 +173,18 @@ func (err BadOrigin) Index() uint { return 3 } +type CustomModuleError struct { + Err string +} + +func (err CustomModuleError) Index() uint { + return 4 +} + func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) + result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) //err := result.Set(scale.OK, nil) //if err != nil { // panic(err) From 144ffa8cead9b168ab513423d4fad338e7d467ff Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 16:49:47 -0600 Subject: [PATCH 061/245] WIP/refactor babe error handling to utiliize new scale pkg features --- lib/babe/errors.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 727c3a9ea4..10e58a6711 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -150,7 +150,7 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - Err string + Err DispatchOutcomeError } func (err UnknownError) Index() uint { @@ -158,7 +158,7 @@ func (err UnknownError) Index() uint { } type FailedLookup struct { - Err string + err DispatchOutcomeError } func (err FailedLookup) Index() uint { @@ -166,7 +166,7 @@ func (err FailedLookup) Index() uint { } type BadOrigin struct { - Err string + Err DispatchOutcomeError } func (err BadOrigin) Index() uint { @@ -174,7 +174,7 @@ func (err BadOrigin) Index() uint { } type CustomModuleError struct { - Err string + Err DispatchOutcomeError } func (err CustomModuleError) Index() uint { @@ -189,6 +189,11 @@ func determineErr(res []byte) error { //if err != nil { // panic(err) //} + + // This code chunk works + v := FailedLookup{err: DispatchOutcomeError{"failed lookup"}} + fmt.Println(v.err) + fmt.Println("Result") fmt.Println(result) switch res[1] { From 59b01e4f50ad932531d8060a4586d08f5c316386 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 15:44:21 -0600 Subject: [PATCH 062/245] WIP scale/babe integration. Closer but still errors --- lib/babe/errors.go | 116 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 91 insertions(+), 25 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 10e58a6711..5dacd7275d 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -62,10 +62,13 @@ var ( ) // A DispatchOutcomeError is outcome of dispatching the extrinsic +// Can I make this not a struct? type DispatchOutcomeError struct { msg string // description of error } +//type DispatchOutcomeError string + func (e DispatchOutcomeError) Error() string { return fmt.Sprintf("dispatch outcome error: %s", e.msg) } @@ -90,24 +93,81 @@ func determineCustomModuleErr(res []byte) error { return fmt.Errorf("index: %d code: %d message: %s", res[0], res[1], errMsg.String()) } +//func determineDispatchErr(res []byte) error { +// var v []byte +// err := scale.Unmarshal(res[1:], &v) +// if err != nil { +// // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure +// // TODO Create stucts for errors and integrate into Varying data type +// return errInvalidResult +// } +// +// switch res[0] { +// case 0: +// var v []byte +// err := scale.Unmarshal(res[1:], &v) +// if err != nil { +// // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure +// // TODO Create stucts for errors and integrate into Varying data type +// return errInvalidResult +// } +// return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} +// case 1: +// return &DispatchOutcomeError{"failed lookup"} +// case 2: +// return &DispatchOutcomeError{"bad origin"} +// case 3: +// return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))} +// } +// +// return errInvalidResult +//} + func determineDispatchErr(res []byte) error { - switch res[0] { - case 0: - var v []byte - err := scale.Unmarshal(res[1:], &v) - if err != nil { - // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure - // TODO Create stucts for errors and integrate into Varying data type - return errInvalidResult - } - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} - case 1: + //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) + vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) + //fmt.Println("Result") + //fmt.Println(result) + + //var v []byte + err := scale.Unmarshal(res[1:], &vdt) + if err != nil { + // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure + // TODO Create stucts for errors and integrate into Varying data type + return errInvalidResult + } + + + //fmt.Println("Unmarsalled dispatchErr Type:") + //fmt.Println(vdt.Value()) + + switch val := vdt.Value().(type){ + case UnknownError: + // For some reason its not entering here, going to customModuleError instead + // Maybe cuz struct is wrong + fmt.Println("Val:") + fmt.Println(val) + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val.Err)} + case FailedLookup: return &DispatchOutcomeError{"failed lookup"} - case 2: + case BadOrigin: return &DispatchOutcomeError{"bad origin"} - case 3: - return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))} + case CustomModuleError: + // Printing nice, not getting the correct values + return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} } + //// if custon error module + //if vdt.Value().Index() == 4 { + // err = vdt.Set(CustomModuleError{ + // Err: DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))}, + // }) + // if err != nil { + // panic(err) + // } + //} + //fmt.Println("Unmarsalled dispatchErr Value2:") + //fmt.Println(vdt.Value()) + return errInvalidResult } @@ -150,7 +210,7 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - Err DispatchOutcomeError + Err string } func (err UnknownError) Index() uint { @@ -158,7 +218,7 @@ func (err UnknownError) Index() uint { } type FailedLookup struct { - err DispatchOutcomeError + Err string } func (err FailedLookup) Index() uint { @@ -166,7 +226,7 @@ func (err FailedLookup) Index() uint { } type BadOrigin struct { - Err DispatchOutcomeError + Err string } func (err BadOrigin) Index() uint { @@ -174,28 +234,34 @@ func (err BadOrigin) Index() uint { } type CustomModuleError struct { - Err DispatchOutcomeError + index uint8 `scale:"3"` + err uint8 `scale:"2"` + message string `scale:"1"` // might need to be *string } func (err CustomModuleError) Index() uint { return 4 } +func (err CustomModuleError) String() string { + return fmt.Sprintf("index: %d code: %d message: %s", err.index, err.err, err.message) +} + func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) + //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) //err := result.Set(scale.OK, nil) //if err != nil { // panic(err) //} - // This code chunk works - v := FailedLookup{err: DispatchOutcomeError{"failed lookup"}} - fmt.Println(v.err) - - fmt.Println("Result") - fmt.Println(result) + //// This code chunk works + //v := FailedLookup{Err: DispatchOutcomeError{"failed lookup"}} + //fmt.Println(v.Err) + // + //fmt.Println("Result") + //fmt.Println(result) switch res[1] { case 0: return nil From cc6a0f15627d0f555b04a84764754cb70abc137c Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 16:29:40 -0600 Subject: [PATCH 063/245] WIP/Fix type issues in babe refactor --- lib/babe/errors.go | 55 ++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 39 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 5dacd7275d..fe31bd49bc 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -62,7 +62,6 @@ var ( ) // A DispatchOutcomeError is outcome of dispatching the extrinsic -// Can I make this not a struct? type DispatchOutcomeError struct { msg string // description of error } @@ -124,49 +123,39 @@ func determineCustomModuleErr(res []byte) error { //} func determineDispatchErr(res []byte) error { - //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) + // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - //fmt.Println("Result") - //fmt.Println(result) - //var v []byte + // Am I unmarshalling the right thing here? Make sure should be res[1] err := scale.Unmarshal(res[1:], &vdt) if err != nil { - // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure - // TODO Create stucts for errors and integrate into Varying data type + // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } - - //fmt.Println("Unmarsalled dispatchErr Type:") - //fmt.Println(vdt.Value()) - + // Might have to change testing to adjust to new types + // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ case UnknownError: // For some reason its not entering here, going to customModuleError instead - // Maybe cuz struct is wrong + // Maybe cuz struct is wrong? fmt.Println("Val:") fmt.Println(val) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val.Err)} + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case FailedLookup: + // Add testing for this case, make sure struct is correct + fmt.Println("failed lookup") return &DispatchOutcomeError{"failed lookup"} case BadOrigin: + // Add testing for this case, make sure struct is correct + fmt.Println("bad origin") return &DispatchOutcomeError{"bad origin"} case CustomModuleError: // Printing nice, not getting the correct values + fmt.Println("Val2:") + fmt.Println(val) return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} } - //// if custon error module - //if vdt.Value().Index() == 4 { - // err = vdt.Set(CustomModuleError{ - // Err: DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))}, - // }) - // if err != nil { - // panic(err) - // } - //} - //fmt.Println("Unmarsalled dispatchErr Value2:") - //fmt.Println(vdt.Value()) return errInvalidResult } @@ -210,7 +199,7 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - Err string + err string } func (err UnknownError) Index() uint { @@ -218,7 +207,7 @@ func (err UnknownError) Index() uint { } type FailedLookup struct { - Err string + err string } func (err FailedLookup) Index() uint { @@ -226,7 +215,7 @@ func (err FailedLookup) Index() uint { } type BadOrigin struct { - Err string + err string } func (err BadOrigin) Index() uint { @@ -250,18 +239,6 @@ func (err CustomModuleError) String() string { func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) - //err := result.Set(scale.OK, nil) - //if err != nil { - // panic(err) - //} - - //// This code chunk works - //v := FailedLookup{Err: DispatchOutcomeError{"failed lookup"}} - //fmt.Println(v.Err) - // - //fmt.Println("Result") - //fmt.Println(result) switch res[1] { case 0: return nil From 40d1964d8c38180dcb9bb9f227c6bac52e85a2d6 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 16:35:55 -0600 Subject: [PATCH 064/245] Comments on issues to fix in for babe error handling --- lib/babe/errors.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index fe31bd49bc..86b1dac823 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -122,7 +122,13 @@ func determineCustomModuleErr(res []byte) error { // return errInvalidResult //} +/* + Two main issues I need to fix: + 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling + 2) CustomModuleError data isnt being decoded. The type is but the struct is empty + */ func determineDispatchErr(res []byte) error { + // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) From 4cc402124357b222a6113f631d55329a8d324bac Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 09:22:10 -0600 Subject: [PATCH 065/245] WIP/refactor babe error handling with new scale pkg --- lib/babe/errors.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 86b1dac823..92a883c88b 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -130,7 +130,8 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError - vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) + //var e := &UnknownError + vdt := scale.MustNewVaryingDataType(&UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) // Am I unmarshalling the right thing here? Make sure should be res[1] err := scale.Unmarshal(res[1:], &vdt) @@ -142,12 +143,12 @@ func determineDispatchErr(res []byte) error { // Might have to change testing to adjust to new types // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case UnknownError: + case *UnknownError: // For some reason its not entering here, going to customModuleError instead // Maybe cuz struct is wrong? fmt.Println("Val:") fmt.Println(val) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %p", val)} case FailedLookup: // Add testing for this case, make sure struct is correct fmt.Println("failed lookup") @@ -205,9 +206,11 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - err string + err *string } +//type UnknownError *string + func (err UnknownError) Index() uint { return 1 } @@ -231,7 +234,7 @@ func (err BadOrigin) Index() uint { type CustomModuleError struct { index uint8 `scale:"3"` err uint8 `scale:"2"` - message string `scale:"1"` // might need to be *string + message string `scale:"1"` // might need to be *string or an option } func (err CustomModuleError) Index() uint { From ae05c36f303677a78ac17346667bfc82e3fdabd4 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:25:18 -0600 Subject: [PATCH 066/245] resolve type issue --- lib/babe/errors.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 92a883c88b..e7c60087bf 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -128,27 +128,31 @@ func determineCustomModuleErr(res []byte) error { 2) CustomModuleError data isnt being decoded. The type is but the struct is empty */ func determineDispatchErr(res []byte) error { + fmt.Println("In the method") // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError //var e := &UnknownError - vdt := scale.MustNewVaryingDataType(&UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - + vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) + fmt.Println("Past vdt") // Am I unmarshalling the right thing here? Make sure should be res[1] - err := scale.Unmarshal(res[1:], &vdt) + err := scale.Unmarshal(res, &vdt) if err != nil { // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } + fmt.Println("Vdt") + fmt.Println(vdt.Value()) + // Might have to change testing to adjust to new types // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case *UnknownError: + case UnknownError: // For some reason its not entering here, going to customModuleError instead // Maybe cuz struct is wrong? fmt.Println("Val:") fmt.Println(val) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %p", val)} + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case FailedLookup: // Add testing for this case, make sure struct is correct fmt.Println("failed lookup") @@ -162,6 +166,8 @@ func determineDispatchErr(res []byte) error { fmt.Println("Val2:") fmt.Println(val) return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} + default: // Remove this before PR lol + fmt.Println("Something is most definitly afoot at the circle K") } return errInvalidResult @@ -206,13 +212,13 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - err *string + err string } //type UnknownError *string func (err UnknownError) Index() uint { - return 1 + return 0 } type FailedLookup struct { @@ -220,7 +226,7 @@ type FailedLookup struct { } func (err FailedLookup) Index() uint { - return 2 + return 1 } type BadOrigin struct { @@ -228,7 +234,7 @@ type BadOrigin struct { } func (err BadOrigin) Index() uint { - return 3 + return 2 } type CustomModuleError struct { @@ -238,7 +244,7 @@ type CustomModuleError struct { } func (err CustomModuleError) Index() uint { - return 4 + return 3 } func (err CustomModuleError) String() string { From 6919833ffd6ba5aa7e588dab47e3c8806f2759e1 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:36:34 -0600 Subject: [PATCH 067/245] fixed unknown error data populating issue --- lib/babe/errors.go | 22 ++++++++-------------- lib/babe/errors_test.go | 1 + 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index e7c60087bf..9d94be3a1b 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -124,16 +124,14 @@ func determineCustomModuleErr(res []byte) error { /* Two main issues I need to fix: - 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling + 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling - fixed 2) CustomModuleError data isnt being decoded. The type is but the struct is empty */ func determineDispatchErr(res []byte) error { - fmt.Println("In the method") // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError - //var e := &UnknownError - vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - fmt.Println("Past vdt") + var e UnknownError + vdt := scale.MustNewVaryingDataType(e, FailedLookup{}, BadOrigin{}, CustomModuleError{}) // Am I unmarshalling the right thing here? Make sure should be res[1] err := scale.Unmarshal(res, &vdt) if err != nil { @@ -147,11 +145,7 @@ func determineDispatchErr(res []byte) error { // Might have to change testing to adjust to new types // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case UnknownError: - // For some reason its not entering here, going to customModuleError instead - // Maybe cuz struct is wrong? - fmt.Println("Val:") - fmt.Println(val) + case UnknownError: // Got it! return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case FailedLookup: // Add testing for this case, make sure struct is correct @@ -211,11 +205,11 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } -type UnknownError struct { - err string -} +//type UnknownError struct { +// err string +//} -//type UnknownError *string +type UnknownError string func (err UnknownError) Index() uint { return 0 diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 8ec2923c6d..a318f19032 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -61,6 +61,7 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } + t.Log(err.Error()) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) From f195c168390e2d1fa469bb208630c9004f8e9127 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:56:37 -0600 Subject: [PATCH 068/245] WIP/Get module data to be populated when unmarshalling --- lib/babe/errors.go | 12 ++++-------- lib/babe/errors_test.go | 1 - 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 9d94be3a1b..f0b267003c 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -205,10 +205,6 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } -//type UnknownError struct { -// err string -//} - type UnknownError string func (err UnknownError) Index() uint { @@ -232,9 +228,9 @@ func (err BadOrigin) Index() uint { } type CustomModuleError struct { - index uint8 `scale:"3"` - err uint8 `scale:"2"` - message string `scale:"1"` // might need to be *string or an option + index uint8 `scale:"3"` + err uint8 `scale:"2"` + message *string `scale:"1"` // Does scale need to consider that rust has multiple string types? } func (err CustomModuleError) Index() uint { @@ -242,7 +238,7 @@ func (err CustomModuleError) Index() uint { } func (err CustomModuleError) String() string { - return fmt.Sprintf("index: %d code: %d message: %s", err.index, err.err, err.message) + return fmt.Sprintf("index: %d code: %d message: %p", err.index, err.err, err.message) } func determineErr(res []byte) error { diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index a318f19032..8ec2923c6d 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -61,7 +61,6 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } - t.Log(err.Error()) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) From a0d51c70c1072b37d7d0ff6139f7232c8403811d Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 16:31:08 -0600 Subject: [PATCH 069/245] resolved data population issue --- lib/babe/errors.go | 60 +++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index f0b267003c..052e4d3f4b 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -123,45 +123,34 @@ func determineCustomModuleErr(res []byte) error { //} /* - Two main issues I need to fix: - 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling - fixed - 2) CustomModuleError data isnt being decoded. The type is but the struct is empty + TODO: + 1) Add test cases for 2 middle cases + 2) Expand on this to include other error types + 3) Rebase + 4) Clean up code + 5) Make sure everything I do is okay (errors returned and printing as hex instead of string). This could be included in a pr + 6) PR??? */ -func determineDispatchErr(res []byte) error { - // Maybe I need to do something with this first status byte? unsure of what tho - // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError - var e UnknownError - vdt := scale.MustNewVaryingDataType(e, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - // Am I unmarshalling the right thing here? Make sure should be res[1] +func determineDispatchErr(res []byte) error { // This works yay! + var e Other + vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) if err != nil { // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } - fmt.Println("Vdt") - fmt.Println(vdt.Value()) - - // Might have to change testing to adjust to new types - // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case UnknownError: // Got it! + case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} - case FailedLookup: + case CannotLookup: // Add testing for this case, make sure struct is correct - fmt.Println("failed lookup") return &DispatchOutcomeError{"failed lookup"} case BadOrigin: // Add testing for this case, make sure struct is correct - fmt.Println("bad origin") return &DispatchOutcomeError{"bad origin"} - case CustomModuleError: - // Printing nice, not getting the correct values - fmt.Println("Val2:") - fmt.Println(val) + case Module: return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} - default: // Remove this before PR lol - fmt.Println("Something is most definitly afoot at the circle K") } return errInvalidResult @@ -205,17 +194,17 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } -type UnknownError string +type Other string -func (err UnknownError) Index() uint { +func (err Other) Index() uint { return 0 } -type FailedLookup struct { +type CannotLookup struct { err string } -func (err FailedLookup) Index() uint { +func (err CannotLookup) Index() uint { return 1 } @@ -227,18 +216,19 @@ func (err BadOrigin) Index() uint { return 2 } -type CustomModuleError struct { - index uint8 `scale:"3"` - err uint8 `scale:"2"` - message *string `scale:"1"` // Does scale need to consider that rust has multiple string types? +type Module struct { // add in `scale:"1"` after + Idx uint8 + Err uint8 + Message *string } -func (err CustomModuleError) Index() uint { +func (err Module) Index() uint { return 3 } -func (err CustomModuleError) String() string { - return fmt.Sprintf("index: %d code: %d message: %p", err.index, err.err, err.message) +func (err Module) String() string { + // Make sure this is okay as opposed to being a string. Should be fine since its wrapped in the error + return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } func determineErr(res []byte) error { From fc101002abad1bf4dda46b353b6e233d1b8e58af Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 30 Apr 2021 12:11:04 -0400 Subject: [PATCH 070/245] add mtx --- pkg/scale/scale.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 855016b8ca..0b1dd49ffb 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -17,7 +17,10 @@ package scale import ( + "bytes" + "encoding/binary" "fmt" + "math/big" "reflect" "sort" "strings" From 5111bde114e0f4d9ca20d3506fd088db9889bb32 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 4 May 2021 10:57:34 -0400 Subject: [PATCH 071/245] add variable data type interface, test to compare old vs new encoding --- pkg/scale/scale.go | 10 +++-- pkg/scale/scale_test.go | 88 ----------------------------------------- 2 files changed, 7 insertions(+), 91 deletions(-) delete mode 100644 pkg/scale/scale_test.go diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 0b1dd49ffb..ac6530bae3 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -17,16 +17,20 @@ package scale import ( - "bytes" - "encoding/binary" "fmt" - "math/big" "reflect" "sort" "strings" "sync" ) +type VaryingDataType []VaryingDataTypeValue + +// VaryingDataType is used to represent scale encodable types +type VaryingDataTypeValue interface { + Index() uint +} + // package level cache for fieldScaleIndicies var cache = &fieldScaleIndicesCache{ cache: make(map[string]fieldScaleIndices), diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go deleted file mode 100644 index a96ab2b539..0000000000 --- a/pkg/scale/scale_test.go +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package scale - -import ( - "reflect" - "testing" -) - -func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { - tests := []struct { - name string - in interface{} - wantIndices fieldScaleIndices - wantErr bool - }{ - { - in: struct{ Foo int }{}, - wantIndices: fieldScaleIndices{ - { - fieldIndex: 0, - }, - }, - }, - { - in: struct { - End1 bool - Baz bool `scale:"3"` - End2 []byte - Bar int32 `scale:"2"` - End3 []byte - Foo []byte `scale:"1"` - }{}, - wantIndices: fieldScaleIndices{ - { - fieldIndex: 5, - scaleIndex: newStringPtr("1"), - }, - { - fieldIndex: 3, - scaleIndex: newStringPtr("2"), - }, - { - fieldIndex: 1, - scaleIndex: newStringPtr("3"), - }, - { - fieldIndex: 0, - }, - { - fieldIndex: 2, - }, - { - fieldIndex: 4, - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fsic := &fieldScaleIndicesCache{ - cache: make(map[string]fieldScaleIndices), - } - _, gotIndices, err := fsic.fieldScaleIndices(tt.in) - if (err != nil) != tt.wantErr { - t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(gotIndices, tt.wantIndices) { - t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() gotIndices = %v, want %v", gotIndices, tt.wantIndices) - } - }) - } -} From 33788df3ec6e21311a286303bedab8f5b4391a7c Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 6 May 2021 11:29:34 -0400 Subject: [PATCH 072/245] wip --- pkg/scale/decode.go | 12 ++++ pkg/scale/decode_test.go | 118 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index f08029485e..9c90e3e93f 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -647,3 +647,15 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { dstv.Set(reflect.ValueOf(ui128)) return } + +// decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 +// if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise +// it returns nil and error +func (ds *decodeState) decodeUint128() (out *Uint128, err error) { + buf := make([]byte, 16) + err = binary.Read(ds, binary.LittleEndian, buf) + if err != nil { + return nil, err + } + return NewUint128(buf) +} diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 22412b7d8e..24481cdb85 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -238,3 +238,121 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { }) } } + +func Test_decodeState_decodeBigInt(t *testing.T) { + var ( + bi *big.Int + ) + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + name: "error case, ensure **big.Int", + args: args{ + data: []byte{0x00}, + dst: bi, + }, + wantErr: true, + }, + { + args: args{ + data: []byte{0x00}, + dst: &bi, + }, + want: big.NewInt(0), + }, + { + args: args{ + data: []byte{0x04}, + dst: &bi, + }, + want: big.NewInt(1), + }, + { + args: args{ + data: []byte{0xa8}, + dst: &bi, + }, + want: big.NewInt(42), + }, + { + args: args{ + data: []byte{0x01, 0x01}, + dst: &bi, + }, + want: big.NewInt(64), + }, + { + args: args{ + data: []byte{0x15, 0x01}, + dst: &bi, + }, + want: big.NewInt(69), + }, + { + args: args{ + data: []byte{0xfd, 0xff}, + dst: &bi, + }, + want: big.NewInt(16383), + }, + { + args: args{ + data: []byte{0x02, 0x00, 0x01, 0x00}, + dst: &bi, + }, + want: big.NewInt(16384), + }, + { + args: args{ + data: []byte{0xfe, 0xff, 0xff, 0xff}, + dst: &bi, + }, + want: big.NewInt(1073741823), + }, + { + args: args{ + data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, + dst: &bi, + }, + want: big.NewInt(1073741824), + }, + { + args: args{ + data: []byte{0x03, 0xff, 0xff, 0xff, 0xff}, + dst: &bi, + }, + want: big.NewInt(1<<32 - 1), + }, + { + args: args{ + data: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, + dst: &bi, + }, + want: big.NewInt(1 << 32), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From 60a680a393094a6ddc51c92ab34bd399d83417ef Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 14 May 2021 12:14:16 -0400 Subject: [PATCH 073/245] []byte, string decoding --- pkg/scale/decode.go | 4 +- pkg/scale/decode_test.go | 117 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 9c90e3e93f..b8ccff16a9 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -490,7 +490,7 @@ func (ds *decodeState) decodeBytes(dstv reflect.Value) (err error) { return } - b := make([]byte, length) + b = make([]byte, length) _, err = ds.Read(b) if err != nil { return @@ -651,7 +651,7 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { // decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 // if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise // it returns nil and error -func (ds *decodeState) decodeUint128() (out *Uint128, err error) { +func (ds *decodeState) decodeUint128() (ui *Uint128, err error) { buf := make([]byte, 16) err = binary.Read(ds, binary.LittleEndian, buf) if err != nil { diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 24481cdb85..fced26e432 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -356,3 +356,120 @@ func Test_decodeState_decodeBigInt(t *testing.T) { }) } } + +func Test_decodeState_decodeBytes(t *testing.T) { + var b []byte + var s string + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + args: args{ + data: []byte{0x04, 0x01}, + dst: &b, + }, + want: []byte{0x01}, + }, + { + args: args{ + data: []byte{0x04, 0xff}, + dst: &b, + }, + want: []byte{0xff}, + }, + { + args: args{ + data: []byte{0x08, 0x01, 0x01}, + dst: &b, + }, + want: []byte{0x01, 0x01}, + }, + { + args: args{ + data: append([]byte{0x01, 0x01}, byteArray(64)...), + dst: &b, + }, + want: byteArray(64), + }, + { + args: args{ + data: append([]byte{0xfd, 0xff}, byteArray(16383)...), + dst: &b, + }, + want: byteArray(16383), + }, + { + args: args{ + data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), + dst: &b, + }, + want: byteArray(16384), + }, + // string + { + args: args{ + data: []byte{0x04, 0x01}, + dst: &s, + }, + want: string([]byte{0x01}), + }, + { + args: args{ + data: []byte{0x04, 0xff}, + dst: &s, + }, + want: string([]byte{0xff}), + }, + { + args: args{ + data: []byte{0x08, 0x01, 0x01}, + dst: &s, + }, + want: string([]byte{0x01, 0x01}), + }, + { + args: args{ + data: append([]byte{0x01, 0x01}, byteArray(64)...), + dst: &s, + }, + want: string(byteArray(64)), + }, + { + args: args{ + data: append([]byte{0xfd, 0xff}, byteArray(16383)...), + dst: &s, + }, + want: string(byteArray(16383)), + }, + { + args: args{ + data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), + dst: &s, + }, + want: string(byteArray(16384)), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From ef092bec25cef1eba67c7fb8d8d81be917bc1cd2 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 14 May 2021 13:32:09 -0400 Subject: [PATCH 074/245] encodeBool and tests --- pkg/scale/decode_test.go | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index fced26e432..964958dc3b 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -473,3 +473,56 @@ func Test_decodeState_decodeBytes(t *testing.T) { }) } } + +func Test_decodeState_decodeBool(t *testing.T) { + var b bool + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + args: args{ + data: []byte{0x01}, + dst: &b, + }, + want: true, + }, + { + args: args{ + data: []byte{0x00}, + dst: &b, + }, + want: false, + }, + { + name: "error case", + args: args{ + data: []byte{0x03}, + dst: &b, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From 02cafa7fcbbd3e9a07115e7cc12b6f6ed221cd49 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 19 May 2021 22:24:58 -0400 Subject: [PATCH 075/245] refactor tests, include optionality tests --- pkg/scale/encode_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 4f9deedbd4..146f07ed74 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -169,6 +169,8 @@ var ( in: uint(9223372036854775807), want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + int64Tests = tests{ { name: "myCustomUint(9223372036854775807)", in: myCustomUint(9223372036854775807), @@ -198,6 +200,8 @@ var ( in: int64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + uint64Tests = tests{ { name: "myCustomInt64(9223372036854775807)", in: myCustomInt64(9223372036854775807), @@ -225,6 +229,8 @@ var ( in: uint64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + int32Tests = tests{ { name: "myCustomUint64(9223372036854775807)", in: myCustomUint64(9223372036854775807), @@ -269,6 +275,8 @@ var ( in: uint32(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f}, }, + } + int8Tests = tests{ { name: "uint32(1073741823)", in: myCustomUint32(1073741823), @@ -293,6 +301,8 @@ var ( in: uint8(1), want: []byte{0x01}, }, + } + int16Tests = tests{ { name: "myCustomInt8(1)", in: myCustomUint8(1), @@ -310,6 +320,8 @@ var ( in: int16(16383), want: []byte{0xff, 0x3f}, }, + } + uint16Tests = tests{ { name: "myCustomInt16(16383)", in: myCustomInt16(16383), From bb8acb97616818ec02a2d7abab4290bb7341b918 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 21 May 2021 17:17:04 -0400 Subject: [PATCH 076/245] use shared tests in decode --- pkg/scale/decode_test.go | 330 ++++++++------------------------------- 1 file changed, 68 insertions(+), 262 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 964958dc3b..a9e459af22 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -17,7 +17,6 @@ package scale import ( - "math/big" "reflect" "testing" @@ -240,289 +239,96 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { } func Test_decodeState_decodeBigInt(t *testing.T) { - var ( - bi *big.Int - ) - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - name: "error case, ensure **big.Int", - args: args{ - data: []byte{0x00}, - dst: bi, - }, - wantErr: true, - }, - { - args: args{ - data: []byte{0x00}, - dst: &bi, - }, - want: big.NewInt(0), - }, - { - args: args{ - data: []byte{0x04}, - dst: &bi, - }, - want: big.NewInt(1), - }, - { - args: args{ - data: []byte{0xa8}, - dst: &bi, - }, - want: big.NewInt(42), - }, - { - args: args{ - data: []byte{0x01, 0x01}, - dst: &bi, - }, - want: big.NewInt(64), - }, - { - args: args{ - data: []byte{0x15, 0x01}, - dst: &bi, - }, - want: big.NewInt(69), - }, - { - args: args{ - data: []byte{0xfd, 0xff}, - dst: &bi, - }, - want: big.NewInt(16383), - }, - { - args: args{ - data: []byte{0x02, 0x00, 0x01, 0x00}, - dst: &bi, - }, - want: big.NewInt(16384), - }, - { - args: args{ - data: []byte{0xfe, 0xff, 0xff, 0xff}, - dst: &bi, - }, - want: big.NewInt(1073741823), - }, - { - args: args{ - data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, - dst: &bi, - }, - want: big.NewInt(1073741824), - }, - { - args: args{ - data: []byte{0x03, 0xff, 0xff, 0xff, 0xff}, - dst: &bi, - }, - want: big.NewInt(1<<32 - 1), - }, - { - args: args{ - data: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, - dst: &bi, - }, - want: big.NewInt(1 << 32), - }, - } - for _, tt := range tests { + for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } func Test_decodeState_decodeBytes(t *testing.T) { - var b []byte - var s string - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - args: args{ - data: []byte{0x04, 0x01}, - dst: &b, - }, - want: []byte{0x01}, - }, - { - args: args{ - data: []byte{0x04, 0xff}, - dst: &b, - }, - want: []byte{0xff}, - }, - { - args: args{ - data: []byte{0x08, 0x01, 0x01}, - dst: &b, - }, - want: []byte{0x01, 0x01}, - }, - { - args: args{ - data: append([]byte{0x01, 0x01}, byteArray(64)...), - dst: &b, - }, - want: byteArray(64), - }, - { - args: args{ - data: append([]byte{0xfd, 0xff}, byteArray(16383)...), - dst: &b, - }, - want: byteArray(16383), - }, - { - args: args{ - data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), - dst: &b, - }, - want: byteArray(16384), - }, - // string - { - args: args{ - data: []byte{0x04, 0x01}, - dst: &s, - }, - want: string([]byte{0x01}), - }, - { - args: args{ - data: []byte{0x04, 0xff}, - dst: &s, - }, - want: string([]byte{0xff}), - }, - { - args: args{ - data: []byte{0x08, 0x01, 0x01}, - dst: &s, - }, - want: string([]byte{0x01, 0x01}), - }, - { - args: args{ - data: append([]byte{0x01, 0x01}, byteArray(64)...), - dst: &s, - }, - want: string(byteArray(64)), - }, - { - args: args{ - data: append([]byte{0xfd, 0xff}, byteArray(16383)...), - dst: &s, - }, - want: string(byteArray(16383)), - }, - { - args: args{ - data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), - dst: &s, - }, - want: string(byteArray(16384)), - }, - } - for _, tt := range tests { + for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return } - if err != nil { - return - } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } func Test_decodeState_decodeBool(t *testing.T) { - var b bool - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - args: args{ - data: []byte{0x01}, - dst: &b, - }, - want: true, - }, - { - args: args{ - data: []byte{0x00}, - dst: &b, - }, - want: false, - }, - { - name: "error case", - args: args{ - data: []byte{0x03}, - dst: &b, - }, - wantErr: true, - }, - } - for _, tt := range tests { + for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } + +// func Test_decodeState_decodeBool(t *testing.T) { +// var b bool +// type args struct { +// data []byte +// dst interface{} +// } +// tests := []struct { +// name string +// args args +// wantErr bool +// want interface{} +// }{ +// { +// args: args{ +// data: []byte{0x01}, +// dst: &b, +// }, +// want: true, +// }, +// { +// args: args{ +// data: []byte{0x00}, +// dst: &b, +// }, +// want: false, +// }, +// { +// name: "error case", +// args: args{ +// data: []byte{0x03}, +// dst: &b, +// }, +// wantErr: true, +// }, +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// err := Unmarshal(tt.args.data, tt.args.dst) +// if (err != nil) != tt.wantErr { +// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// return +// } +// if err != nil { +// return +// } +// got := reflect.ValueOf(tt.args.dst).Elem().Interface() +// if !reflect.DeepEqual(got, tt.want) { +// t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) +// } +// }) +// } +// } From 62f686a5c7799cf08a6abe96daf70a8d06958122 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 26 May 2021 11:25:29 -0400 Subject: [PATCH 077/245] struct decode tests, and unmarshal refactor --- pkg/scale/decode_test.go | 81 ++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 53 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index a9e459af22..c8073b8ffd 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -253,7 +253,7 @@ func Test_decodeState_decodeBigInt(t *testing.T) { } func Test_decodeState_decodeBytes(t *testing.T) { - for _, tt := range boolTests { + for _, tt := range stringTests { t.Run(tt.name, func(t *testing.T) { dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { @@ -280,55 +280,30 @@ func Test_decodeState_decodeBool(t *testing.T) { } } -// func Test_decodeState_decodeBool(t *testing.T) { -// var b bool -// type args struct { -// data []byte -// dst interface{} -// } -// tests := []struct { -// name string -// args args -// wantErr bool -// want interface{} -// }{ -// { -// args: args{ -// data: []byte{0x01}, -// dst: &b, -// }, -// want: true, -// }, -// { -// args: args{ -// data: []byte{0x00}, -// dst: &b, -// }, -// want: false, -// }, -// { -// name: "error case", -// args: args{ -// data: []byte{0x03}, -// dst: &b, -// }, -// wantErr: true, -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// err := Unmarshal(tt.args.data, tt.args.dst) -// if (err != nil) != tt.wantErr { -// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// if err != nil { -// return -// } -// got := reflect.ValueOf(tt.args.dst).Elem().Interface() -// if !reflect.DeepEqual(got, tt.want) { -// t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) -// } -// }) -// } -// } +func Test_decodeState_decodeStruct(t *testing.T) { + for _, tt := range structTests { + t.Run(tt.name, func(t *testing.T) { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + switch reflect.ValueOf(tt.in).Kind() { + case reflect.Ptr: + if reflect.ValueOf(dst).IsZero() { + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } else { + // have to do this since reflect.DeepEqual won't come back true for different addresses + if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } + default: + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } + }) + } +} From bba659d8b0253ad5f3bc7b5489dd0dd3993164f9 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 27 May 2021 16:00:27 -0400 Subject: [PATCH 078/245] wip --- pkg/scale/decode_test.go | 132 +++++++++++++++++++++++++++++++++------ pkg/scale/encode_test.go | 13 ++++ 2 files changed, 125 insertions(+), 20 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index c8073b8ffd..dc8e43ecae 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -17,6 +17,7 @@ package scale import ( + "math/big" "reflect" "testing" @@ -241,7 +242,8 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { func Test_decodeState_decodeBigInt(t *testing.T) { for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + var dst *big.Int + // dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -269,7 +271,8 @@ func Test_decodeState_decodeBytes(t *testing.T) { func Test_decodeState_decodeBool(t *testing.T) { for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + // dst := reflect.ValueOf(tt.in).Interface() + var dst bool if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -280,29 +283,118 @@ func Test_decodeState_decodeBool(t *testing.T) { } } +func Test_decodeState_decodeStructManual(t *testing.T) { + // nil case + var dst *MyStruct = nil + var b = []byte{0} + var want *MyStruct = nil + + // dst = structTests[0].dst + + err := Unmarshal(b, &dst) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + + // zero case MyStruct + var dst1 *MyStruct = &MyStruct{} + err = Unmarshal(b, &dst1) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst1, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + + // zero case MyStruct + var dst2 *MyStruct = &MyStruct{Baz: true} + err = Unmarshal(b, &dst2) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst2, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + +} + func Test_decodeState_decodeStruct(t *testing.T) { - for _, tt := range structTests { + for _, tt := range append([]test{}, structTests[0]) { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + // dst := tt.dst if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - switch reflect.ValueOf(tt.in).Kind() { - case reflect.Ptr: - if reflect.ValueOf(dst).IsZero() { - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - } else { - // have to do this since reflect.DeepEqual won't come back true for different addresses - if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - } - default: - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } + // dstv := reflect.ValueOf(dst) + // inv := reflect.ValueOf(tt.in) + + // if dstv.Kind() != inv.Kind() { + // t.Errorf("decodeState.unmarshal() differing kind = %T, want %T", dst, tt.in) + // return + // } + + // switch inv.Kind() { + // case reflect.Ptr: + // fmt.Println(dst, dstv.Interface(), tt.in, inv.Interface()) + // if inv.IsZero() { + // fmt.Println(dst, tt.in, dstv.Interface(), inv.Interface()) + // if !reflect.DeepEqual(dstv.Interface(), tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } else if inv.IsNil() { + // if !reflect.DeepEqual(dst, tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } else { + // // // have to do this since reflect.DeepEqual won't come back true for different addresses + // // if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { + // // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // // } + // if !reflect.DeepEqual(dst, inv) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } + // default: + // if !reflect.DeepEqual(dst, tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} +func Test_decodeState_decodeArray(t *testing.T) { + for _, tt := range arrayTests { + t.Run(tt.name, func(t *testing.T) { + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} + +func Test_decodeState_decodeSlice(t *testing.T) { + for _, tt := range sliceTests { + t.Run(tt.name, func(t *testing.T) { + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 146f07ed74..f7ea5e9309 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -26,6 +26,7 @@ import ( type test struct { name string in interface{} + dst interface{} wantErr bool want []byte out interface{} @@ -532,6 +533,7 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool}", @@ -545,6 +547,7 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool} with untagged attributes", @@ -813,51 +816,61 @@ var ( name: "[4]int{1, 2, 3, 4}", in: [4]int{1, 2, 3, 4}, want: []byte{0x04, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{16384, 2, 3, 4}", in: [4]int{16384, 2, 3, 4}, want: []byte{0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{1073741824, 2, 3, 4}", in: [4]int{1073741824, 2, 3, 4}, want: []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{1 << 32, 2, 3, 1 << 32}", in: [4]int{1 << 32, 2, 3, 1 << 32}, want: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, + dst: [4]int{}, }, { name: "[3]bool{true, false, true}", in: [3]bool{true, false, true}, want: []byte{0x01, 0x00, 0x01}, + dst: [3]bool{}, }, { name: "[2][]int{{0, 1}, {1, 0}}", in: [2][]int{{0, 1}, {1, 0}}, want: []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, + dst: [2]int{}, }, { name: "[2][2]int{{0, 1}, {1, 0}}", in: [2][2]int{{0, 1}, {1, 0}}, want: []byte{0x00, 0x04, 0x04, 0x00}, + dst: [2][2]int{}, }, { name: "[2]*big.Int{big.NewInt(0), big.NewInt(1)}", in: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, want: []byte{0x00, 0x04}, + dst: [2]*big.Int{}, }, { name: "[2][]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, + // dst: [2][]byte{{0x00, 0x01}, {0x01, 0x00}} }, { name: "[2][2]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x00, 0x01, 0x01, 0x00}, + dst: [2][2]byte{}, }, } From 9520ee0735b5a78fda9b9022287a1e187384854a Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 28 May 2021 10:37:33 -0400 Subject: [PATCH 079/245] decode of VariantDataType, wip tests --- pkg/scale/comparison_test.go | 2 +- pkg/scale/encode.go | 16 +++++++++++++++- pkg/scale/scale.go | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 54f4225da3..56050ec075 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -130,7 +130,7 @@ func TestOldVsNewEncoding(t *testing.T) { ), } - newEncode, err := Marshal(newDigest) + newEncode, err := scale.Marshal(newDigest) if err != nil { t.Errorf("unexpected err: %v", err) return diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 7c62a77bd5..cd6ff6890f 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/binary" "fmt" + "log" "math/big" "reflect" ) @@ -90,7 +91,20 @@ func (es *encodeState) marshal(in interface{}) (err error) { case reflect.Array: err = es.encodeArray(in) case reflect.Slice: - err = es.encodeSlice(in) + t := reflect.TypeOf(in) + // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType + switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { + case true: + invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) + switch in := invdt.Interface().(type) { + case VaryingDataType: + err = es.encodeVaryingDataType(in) + default: + log.Panicf("this should never happen") + } + case false: + err = es.encodeSlice(in) + } default: err = fmt.Errorf("unsupported type: %T", in) } diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index ac6530bae3..698ad6983f 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -24,8 +24,31 @@ import ( "sync" ) +type varyingDataTypeCache map[string]map[uint]VaryingDataTypeValue + +var vdtCache varyingDataTypeCache = make(varyingDataTypeCache) + type VaryingDataType []VaryingDataTypeValue +func RegisterVaryingDataType(in interface{}, values ...VaryingDataTypeValue) (err error) { + _, ok := in.(VaryingDataType) + if !ok { + err = fmt.Errorf("%T is not a VaryingDataType", in) + } + + t := reflect.TypeOf(in) + key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) + + _, ok = vdtCache[key] + if !ok { + vdtCache[key] = make(map[uint]VaryingDataTypeValue) + } + for _, val := range values { + vdtCache[key][val.Index()] = val + } + return +} + // VaryingDataType is used to represent scale encodable types type VaryingDataTypeValue interface { Index() uint From 144e1036a025bd993c35c7b7d3c13cc72c90fae2 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 28 May 2021 16:07:37 -0400 Subject: [PATCH 080/245] add optionality testing --- pkg/scale/decode_test.go | 68 +++++++++++++++++++--------------------- pkg/scale/encode_test.go | 13 -------- 2 files changed, 33 insertions(+), 48 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index dc8e43ecae..b27d6f1f25 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -318,7 +318,6 @@ func Test_decodeState_decodeStructManual(t *testing.T) { if !reflect.DeepEqual(dst2, want) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) } - } func Test_decodeState_decodeStruct(t *testing.T) { @@ -330,40 +329,6 @@ func Test_decodeState_decodeStruct(t *testing.T) { if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - // dstv := reflect.ValueOf(dst) - // inv := reflect.ValueOf(tt.in) - - // if dstv.Kind() != inv.Kind() { - // t.Errorf("decodeState.unmarshal() differing kind = %T, want %T", dst, tt.in) - // return - // } - - // switch inv.Kind() { - // case reflect.Ptr: - // fmt.Println(dst, dstv.Interface(), tt.in, inv.Interface()) - // if inv.IsZero() { - // fmt.Println(dst, tt.in, dstv.Interface(), inv.Interface()) - // if !reflect.DeepEqual(dstv.Interface(), tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } else if inv.IsNil() { - // if !reflect.DeepEqual(dst, tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } else { - // // // have to do this since reflect.DeepEqual won't come back true for different addresses - // // if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { - // // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // // } - // if !reflect.DeepEqual(dst, inv) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } - // default: - // if !reflect.DeepEqual(dst, tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } if !reflect.DeepEqual(dst, tt.in) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } @@ -399,3 +364,36 @@ func Test_decodeState_decodeSlice(t *testing.T) { }) } } + +func Test_unmarshal_optionality(t *testing.T) { + var ptrTests tests + for _, t := range allTests { + ptrTest := test{ + name: t.name, + in: t.in, + wantErr: t.wantErr, + want: t.want, + } + switch t.in { + case nil: + // this doesn't actually happen since none of the tests have nil value for tt.in + ptrTest.want = []byte{0x00} + default: + ptrTest.want = append([]byte{0x01}, t.want...) + } + ptrTests = append(ptrTests, ptrTest) + } + for _, tt := range ptrTests { + t.Run(tt.name, func(t *testing.T) { + // this becomes a pointer to a zero value of the underlying value + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + // es := &encodeState{fieldScaleIndicesCache: cache} + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, &tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index f7ea5e9309..146f07ed74 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -26,7 +26,6 @@ import ( type test struct { name string in interface{} - dst interface{} wantErr bool want []byte out interface{} @@ -533,7 +532,6 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool}", @@ -547,7 +545,6 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool} with untagged attributes", @@ -816,61 +813,51 @@ var ( name: "[4]int{1, 2, 3, 4}", in: [4]int{1, 2, 3, 4}, want: []byte{0x04, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{16384, 2, 3, 4}", in: [4]int{16384, 2, 3, 4}, want: []byte{0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{1073741824, 2, 3, 4}", in: [4]int{1073741824, 2, 3, 4}, want: []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{1 << 32, 2, 3, 1 << 32}", in: [4]int{1 << 32, 2, 3, 1 << 32}, want: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, - dst: [4]int{}, }, { name: "[3]bool{true, false, true}", in: [3]bool{true, false, true}, want: []byte{0x01, 0x00, 0x01}, - dst: [3]bool{}, }, { name: "[2][]int{{0, 1}, {1, 0}}", in: [2][]int{{0, 1}, {1, 0}}, want: []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, - dst: [2]int{}, }, { name: "[2][2]int{{0, 1}, {1, 0}}", in: [2][2]int{{0, 1}, {1, 0}}, want: []byte{0x00, 0x04, 0x04, 0x00}, - dst: [2][2]int{}, }, { name: "[2]*big.Int{big.NewInt(0), big.NewInt(1)}", in: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, want: []byte{0x00, 0x04}, - dst: [2]*big.Int{}, }, { name: "[2][]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, - // dst: [2][]byte{{0x00, 0x01}, {0x01, 0x00}} }, { name: "[2][2]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x00, 0x01, 0x01, 0x00}, - dst: [2][2]byte{}, }, } From d721a9b24c4c7a1a888b1b55f7054551a3b9cfe3 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 31 May 2021 13:42:36 -0400 Subject: [PATCH 081/245] fix struct tests and optionality tests --- go.mod | 4 ++++ pkg/scale/decode_test.go | 50 ++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 01a0602622..dd50017e94 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,11 @@ require ( github.com/go-playground/validator/v10 v10.4.1 github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect +<<<<<<< HEAD github.com/google/go-cmp v0.5.6 +======= + github.com/google/go-cmp v0.5.6 // indirect +>>>>>>> fix struct tests and optionality tests github.com/google/uuid v1.1.5 // indirect github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index b27d6f1f25..f8eccb1f41 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -243,7 +243,6 @@ func Test_decodeState_decodeBigInt(t *testing.T) { for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { var dst *big.Int - // dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -271,7 +270,6 @@ func Test_decodeState_decodeBytes(t *testing.T) { func Test_decodeState_decodeBool(t *testing.T) { for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() var dst bool if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) @@ -289,8 +287,6 @@ func Test_decodeState_decodeStructManual(t *testing.T) { var b = []byte{0} var want *MyStruct = nil - // dst = structTests[0].dst - err := Unmarshal(b, &dst) if err != nil { t.Errorf("unexpected error: %v", err) @@ -321,16 +317,20 @@ func Test_decodeState_decodeStructManual(t *testing.T) { } func Test_decodeState_decodeStruct(t *testing.T) { - for _, tt := range append([]test{}, structTests[0]) { + for _, tt := range structTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() - // dst := tt.dst + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + var diff string + if tt.out != nil { + diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(tt.in)) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) } }) } @@ -338,8 +338,7 @@ func Test_decodeState_decodeStruct(t *testing.T) { func Test_decodeState_decodeArray(t *testing.T) { for _, tt := range arrayTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -353,8 +352,7 @@ func Test_decodeState_decodeArray(t *testing.T) { func Test_decodeState_decodeSlice(t *testing.T) { for _, tt := range sliceTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -373,6 +371,7 @@ func Test_unmarshal_optionality(t *testing.T) { in: t.in, wantErr: t.wantErr, want: t.want, + out: t.out, } switch t.in { case nil: @@ -387,12 +386,29 @@ func Test_unmarshal_optionality(t *testing.T) { t.Run(tt.name, func(t *testing.T) { // this becomes a pointer to a zero value of the underlying value dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - // es := &encodeState{fieldScaleIndicesCache: cache} if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - if !reflect.DeepEqual(dst, &tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + + if tt.wantErr == true { + return + } + + switch reflect.TypeOf(tt.in).Kind() { + case reflect.Struct: + var diff string + if tt.out != nil { + diff = cmp.Diff(dst, &tt.out, cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(dst, &tt.in, cmpopts.IgnoreUnexported(tt.in)) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + default: + if !reflect.DeepEqual(dst, &tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } } }) } From 5cc4be12e5f482ec56fa360b5038a3c2c96aff6a Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 31 May 2021 22:54:57 -0400 Subject: [PATCH 082/245] test VaryingDataType --- pkg/scale/decode.go | 16 +++++++++++++++- pkg/scale/decode_test.go | 30 ++++++++++-------------------- pkg/scale/encode.go | 17 ++++++++++++++++- pkg/scale/scale.go | 30 ------------------------------ 4 files changed, 41 insertions(+), 52 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index b8ccff16a9..096ff0cf9a 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -139,7 +139,21 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Slice: err = ds.decodeSlice(dstv) default: - err = fmt.Errorf("unsupported type: %T", in) + _, ok := in.(VaryingDataTypeValue) + switch ok { + case true: + t := reflect.TypeOf(in) + switch t.Kind() { + // TODO: support more primitive types. Do we need to support arrays and slices as well? + case reflect.Int: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + case reflect.Int16: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + } + in, err = ds.unmarshal(reflect.ValueOf(in)) + default: + err = fmt.Errorf("unsupported type: %T", in) + } } } return diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index f8eccb1f41..5ebf4fa514 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -327,7 +327,7 @@ func Test_decodeState_decodeStruct(t *testing.T) { if tt.out != nil { diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in)) } else { - diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(tt.in)) + diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(big.Int{}, tt.in, VDTValue2{}, MyStructWithIgnore{})) } if diff != "" { t.Errorf("decodeState.unmarshal() = %s", diff) @@ -388,28 +388,18 @@ func Test_unmarshal_optionality(t *testing.T) { dst := reflect.New(reflect.TypeOf(tt.in)).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - - if tt.wantErr == true { return } - - switch reflect.TypeOf(tt.in).Kind() { - case reflect.Struct: - var diff string - if tt.out != nil { - diff = cmp.Diff(dst, &tt.out, cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(dst, &tt.in, cmpopts.IgnoreUnexported(tt.in)) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - default: - if !reflect.DeepEqual(dst, &tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) } } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index cd6ff6890f..1c50802f61 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -106,7 +106,22 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - err = fmt.Errorf("unsupported type: %T", in) + _, ok := in.(VaryingDataTypeValue) + switch ok { + case true: + t := reflect.TypeOf(in) + switch t.Kind() { + // TODO: support more primitive types. Do we need to support arrays and slices as well? + case reflect.Int: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + case reflect.Int16: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + } + err = es.marshal(in) + default: + err = fmt.Errorf("unsupported type: %T", in) + } + } } return diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 698ad6983f..855016b8ca 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -24,36 +24,6 @@ import ( "sync" ) -type varyingDataTypeCache map[string]map[uint]VaryingDataTypeValue - -var vdtCache varyingDataTypeCache = make(varyingDataTypeCache) - -type VaryingDataType []VaryingDataTypeValue - -func RegisterVaryingDataType(in interface{}, values ...VaryingDataTypeValue) (err error) { - _, ok := in.(VaryingDataType) - if !ok { - err = fmt.Errorf("%T is not a VaryingDataType", in) - } - - t := reflect.TypeOf(in) - key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) - - _, ok = vdtCache[key] - if !ok { - vdtCache[key] = make(map[uint]VaryingDataTypeValue) - } - for _, val := range values { - vdtCache[key][val.Index()] = val - } - return -} - -// VaryingDataType is used to represent scale encodable types -type VaryingDataTypeValue interface { - Index() uint -} - // package level cache for fieldScaleIndicies var cache = &fieldScaleIndicesCache{ cache: make(map[string]fieldScaleIndices), From 1e390c029c6d6ab74bd9c7a63f5a1d99333b2b0e Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 10:52:45 -0400 Subject: [PATCH 083/245] wip decode refactor, use reflect.Value as passed param --- pkg/scale/decode.go | 14 +++++++++----- pkg/scale/decode_test.go | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 096ff0cf9a..d5f85a8ce2 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -150,7 +150,6 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Int16: in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() } - in, err = ds.unmarshal(reflect.ValueOf(in)) default: err = fmt.Errorf("unsupported type: %T", in) } @@ -504,7 +503,7 @@ func (ds *decodeState) decodeBytes(dstv reflect.Value) (err error) { return } - b = make([]byte, length) + b := make([]byte, length) _, err = ds.Read(b) if err != nil { return @@ -665,11 +664,16 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { // decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 // if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise // it returns nil and error -func (ds *decodeState) decodeUint128() (ui *Uint128, err error) { +func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { buf := make([]byte, 16) err = binary.Read(ds, binary.LittleEndian, buf) if err != nil { - return nil, err + return + } + ui128, err := NewUint128(buf) + if err != nil { + return } - return NewUint128(buf) + dstv.Set(reflect.ValueOf(ui128)) + return } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 5ebf4fa514..1dadd954f2 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -256,9 +256,10 @@ func Test_decodeState_decodeBigInt(t *testing.T) { func Test_decodeState_decodeBytes(t *testing.T) { for _, tt := range stringTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return } if !reflect.DeepEqual(dst, tt.in) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) From 5a6d63da4ece0134b4aa1ee248c06e08ee235825 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 17:41:07 -0400 Subject: [PATCH 084/245] repurpose int and uint as compact length encoded integers, remove unnecessary handling of []int --- pkg/scale/comparison_test.go | 20 ++++++++++++++++++++ pkg/scale/decode.go | 14 ++++++++++++-- pkg/scale/decode_test.go | 35 ----------------------------------- pkg/scale/encode.go | 1 + pkg/scale/uint128.go | 1 + 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 56050ec075..b7c2a3af79 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -17,6 +17,8 @@ package scale import ( + "fmt" + "math/big" "reflect" "testing" @@ -173,3 +175,21 @@ func BenchmarkMarshal(b *testing.B) { } } } + +func TestSomething(t *testing.T) { + i := big.NewInt(0) + expectedVal := *common.Uint128FromBigInt(i) + + encByts, err := oldScale.Encode(expectedVal) + if err != nil { + t.Errorf("%v", err) + return + } + encBytes2, err := oldScale.Encode(i) + if err != nil { + t.Errorf("%v", err) + return + } + + fmt.Printf("%+v, %+v", encByts, encBytes2) +} diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index d5f85a8ce2..da1317a041 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -142,14 +142,24 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { _, ok := in.(VaryingDataTypeValue) switch ok { case true: + var temp reflect.Value t := reflect.TypeOf(in) switch t.Kind() { // TODO: support more primitive types. Do we need to support arrays and slices as well? case reflect.Int: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + temp = reflect.New(reflect.TypeOf(int(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } case reflect.Int16: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + temp = reflect.New(reflect.TypeOf(int16(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } } + dstv.Set(temp.Elem().Convert(t)) default: err = fmt.Errorf("unsupported type: %T", in) } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 1dadd954f2..2cc76640bf 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -282,41 +282,6 @@ func Test_decodeState_decodeBool(t *testing.T) { } } -func Test_decodeState_decodeStructManual(t *testing.T) { - // nil case - var dst *MyStruct = nil - var b = []byte{0} - var want *MyStruct = nil - - err := Unmarshal(b, &dst) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } - - // zero case MyStruct - var dst1 *MyStruct = &MyStruct{} - err = Unmarshal(b, &dst1) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst1, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } - - // zero case MyStruct - var dst2 *MyStruct = &MyStruct{Baz: true} - err = Unmarshal(b, &dst2) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst2, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } -} - func Test_decodeState_decodeStruct(t *testing.T) { for _, tt := range structTests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 1c50802f61..6efb0ed448 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -381,5 +381,6 @@ func (es *encodeState) encodeUint(i uint) (err error) { // encodeUint128 encodes a Uint128 func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) + fmt.Println("bytes", es.Bytes(), i.Bytes()) return } diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index 12e00c42b9..17fc09952c 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -90,6 +90,7 @@ func NewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128, err erro // Bytes returns the Uint128 in little endian format by default. A variadic parameter // order can be used to specify the binary.ByteOrder used func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { + fmt.Println("called bytes!") var o binary.ByteOrder = binary.LittleEndian if len(order) > 0 { o = order[0] From 4c5ab052771edea03b091fba7afe01a333374ccc Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 21:42:36 -0400 Subject: [PATCH 085/245] cleanup, and all tests benchmark --- pkg/scale/comparison_test.go | 42 ++++++++++++++++++++---------------- pkg/scale/encode.go | 1 - pkg/scale/uint128.go | 1 - 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index b7c2a3af79..a50098cecc 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -17,8 +17,6 @@ package scale import ( - "fmt" - "math/big" "reflect" "testing" @@ -132,7 +130,7 @@ func TestOldVsNewEncoding(t *testing.T) { ), } - newEncode, err := scale.Marshal(newDigest) + newEncode, err := Marshal(newDigest) if err != nil { t.Errorf("unexpected err: %v", err) return @@ -176,20 +174,28 @@ func BenchmarkMarshal(b *testing.B) { } } -func TestSomething(t *testing.T) { - i := big.NewInt(0) - expectedVal := *common.Uint128FromBigInt(i) - - encByts, err := oldScale.Encode(expectedVal) - if err != nil { - t.Errorf("%v", err) - return - } - encBytes2, err := oldScale.Encode(i) - if err != nil { - t.Errorf("%v", err) - return +func BenchmarkUnmarshal(b *testing.B) { + for _, tt := range allTests { + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } } - - fmt.Printf("%+v, %+v", encByts, encBytes2) } + +// func BenchmarkDecode(b *testing.B) { +// for _, tt := range variableWidthIntegerTests { +// dst := reflect.New(reflect.TypeOf(tt.in)).Interface() +// fmt.Printf("%v %T\n", dst, dst) +// // if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { +// // b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// // return +// // } +// _, err := oldScale.Decode(tt.want, dst) +// if err != nil { +// b.Errorf("%v", err) +// return +// } +// } +// } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 6efb0ed448..1c50802f61 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -381,6 +381,5 @@ func (es *encodeState) encodeUint(i uint) (err error) { // encodeUint128 encodes a Uint128 func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) - fmt.Println("bytes", es.Bytes(), i.Bytes()) return } diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index 17fc09952c..12e00c42b9 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -90,7 +90,6 @@ func NewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128, err erro // Bytes returns the Uint128 in little endian format by default. A variadic parameter // order can be used to specify the binary.ByteOrder used func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { - fmt.Println("called bytes!") var o binary.ByteOrder = binary.LittleEndian if len(order) > 0 { o = order[0] From 615ee9275fb2578a0b9a5bee3a570158913db382 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Jun 2021 15:13:11 -0400 Subject: [PATCH 086/245] add README --- pkg/scale/decode.go | 3 +++ pkg/scale/encode.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index da1317a041..5c8280067b 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -158,6 +158,9 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { if err != nil { break } + default: + err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + return } dstv.Set(temp.Elem().Convert(t)) default: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 1c50802f61..30b41e333a 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -116,6 +116,9 @@ func (es *encodeState) marshal(in interface{}) (err error) { in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() case reflect.Int16: in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + default: + err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + return } err = es.marshal(in) default: From 5e0e55cc483293388905ca6396793a2c5f592cd5 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 3 Jun 2021 15:34:36 -0400 Subject: [PATCH 087/245] rResult encode/decode and RegisterResult --- pkg/scale/decode.go | 9 ++++++++- pkg/scale/encode.go | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 5c8280067b..17db227433 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -133,7 +133,14 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Ptr: err = ds.decodePointer(dstv) case reflect.Struct: - err = ds.decodeStruct(dstv) + t := reflect.TypeOf(in) + // check if this is a convertible to Result, if so encode using decodeResult + switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + case true: + err = ds.decodeResult(dstv) + case false: + err = ds.decodeStruct(dstv) + } case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 30b41e333a..4c33db8405 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/binary" "fmt" - "log" "math/big" "reflect" ) @@ -87,7 +86,16 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: - err = es.encodeStruct(in) + t := reflect.TypeOf(in) + // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType + switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + case true: + resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) + err = es.encodeResult(resv.Interface().(Result)) + case false: + err = es.encodeStruct(in) + } + case reflect.Array: err = es.encodeArray(in) case reflect.Slice: @@ -96,12 +104,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { case true: invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) - switch in := invdt.Interface().(type) { - case VaryingDataType: - err = es.encodeVaryingDataType(in) - default: - log.Panicf("this should never happen") - } + err = es.encodeVaryingDataType(invdt.Interface().(VaryingDataType)) case false: err = es.encodeSlice(in) } From 5edd13210a0af8eb83d56523977268e7d3dfc370 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 4 Jun 2021 15:26:42 -0400 Subject: [PATCH 088/245] wip cr feedback --- pkg/scale/scale_test.go | 73 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 pkg/scale/scale_test.go diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go new file mode 100644 index 0000000000..a5bdac2364 --- /dev/null +++ b/pkg/scale/scale_test.go @@ -0,0 +1,73 @@ +package scale + +import ( + "reflect" + "testing" +) + +func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { + tests := []struct { + name string + in interface{} + wantIndices fieldScaleIndices + wantErr bool + }{ + // TODO: Add test cases. + { + in: struct{ Foo int }{}, + wantIndices: fieldScaleIndices{ + { + fieldIndex: 0, + }, + }, + }, + { + in: struct { + End1 bool + Baz bool `scale:"3"` + End2 []byte + Bar int32 `scale:"2"` + End3 []byte + Foo []byte `scale:"1"` + }{}, + wantIndices: fieldScaleIndices{ + { + fieldIndex: 5, + scaleIndex: newStringPtr("1"), + }, + { + fieldIndex: 3, + scaleIndex: newStringPtr("2"), + }, + { + fieldIndex: 1, + scaleIndex: newStringPtr("3"), + }, + { + fieldIndex: 0, + }, + { + fieldIndex: 2, + }, + { + fieldIndex: 4, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fsic := &fieldScaleIndicesCache{ + cache: make(map[string]fieldScaleIndices), + } + _, gotIndices, err := fsic.fieldScaleIndices(tt.in) + if (err != nil) != tt.wantErr { + t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotIndices, tt.wantIndices) { + t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() gotIndices = %v, want %v", gotIndices, tt.wantIndices) + } + }) + } +} From 40feb5ea2ca8f31eefbb2852d1a55119ab692517 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 7 Jun 2021 14:57:50 -0400 Subject: [PATCH 089/245] add licenses --- pkg/scale/comparison_test.go | 16 ---------------- pkg/scale/scale_test.go | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index a50098cecc..6b2fa07f57 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -183,19 +183,3 @@ func BenchmarkUnmarshal(b *testing.B) { } } } - -// func BenchmarkDecode(b *testing.B) { -// for _, tt := range variableWidthIntegerTests { -// dst := reflect.New(reflect.TypeOf(tt.in)).Interface() -// fmt.Printf("%v %T\n", dst, dst) -// // if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { -// // b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// // return -// // } -// _, err := oldScale.Decode(tt.want, dst) -// if err != nil { -// b.Errorf("%v", err) -// return -// } -// } -// } diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index a5bdac2364..632ab804d4 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( From 7120ebe2a4812954c7acd2181c80faf5b366a3ba Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 7 Jun 2021 17:04:20 -0400 Subject: [PATCH 090/245] add custom primitive encode/decode --- pkg/scale/decode.go | 113 +++++++++++++++++++++++++++++++++----------- pkg/scale/encode.go | 36 +++++++------- 2 files changed, 104 insertions(+), 45 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 17db227433..8b758ed60b 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -146,33 +146,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Slice: err = ds.decodeSlice(dstv) default: - _, ok := in.(VaryingDataTypeValue) - switch ok { - case true: - var temp reflect.Value - t := reflect.TypeOf(in) - switch t.Kind() { - // TODO: support more primitive types. Do we need to support arrays and slices as well? - case reflect.Int: - temp = reflect.New(reflect.TypeOf(int(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int16: - temp = reflect.New(reflect.TypeOf(int16(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - default: - err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - return - } - dstv.Set(temp.Elem().Convert(t)) - default: - err = fmt.Errorf("unsupported type: %T", in) - } + err = fmt.Errorf("unsupported type: %T", in) } } return @@ -263,6 +237,91 @@ func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { return } +func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { + in := dstv.Interface() + inType := reflect.TypeOf(in) + var temp reflect.Value + switch inType.Kind() { + case reflect.Bool: + temp = reflect.New(reflect.TypeOf(false)) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int: + temp = reflect.New(reflect.TypeOf(int(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int8: + temp = reflect.New(reflect.TypeOf(int8(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int16: + temp = reflect.New(reflect.TypeOf(int16(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int32: + temp = reflect.New(reflect.TypeOf(int32(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int64: + temp = reflect.New(reflect.TypeOf(int64(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.String: + temp = reflect.New(reflect.TypeOf("")) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint: + temp = reflect.New(reflect.TypeOf(uint(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint8: + temp = reflect.New(reflect.TypeOf(uint8(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint16: + temp = reflect.New(reflect.TypeOf(uint16(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint32: + temp = reflect.New(reflect.TypeOf(uint32(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint64: + temp = reflect.New(reflect.TypeOf(uint64(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + default: + err = fmt.Errorf("unsupported type for custom primitive: %T", in) + return + } + dstv.Set(temp.Elem().Convert(inType)) + return +} + func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { res := dstv.Interface().(Result) var rb byte diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 4c33db8405..610df7cd6c 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -109,24 +109,24 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - _, ok := in.(VaryingDataTypeValue) - switch ok { - case true: - t := reflect.TypeOf(in) - switch t.Kind() { - // TODO: support more primitive types. Do we need to support arrays and slices as well? - case reflect.Int: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() - case reflect.Int16: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() - default: - err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - return - } - err = es.marshal(in) - default: - err = fmt.Errorf("unsupported type: %T", in) - } + // _, ok := in.(VaryingDataTypeValue) + // switch ok { + // case true: + // t := reflect.TypeOf(in) + // switch t.Kind() { + // // TODO: support more primitive types. Do we need to support arrays and slices as well? + // case reflect.Int: + // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + // case reflect.Int16: + // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + // default: + // err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + // return + // } + // err = es.marshal(in) + // default: + err = fmt.Errorf("unsupported type: %T", in) + // } } } From 3d35375817b0c6247fe9c44c4d37a3a135626628 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 8 Jun 2021 13:57:33 -0400 Subject: [PATCH 091/245] more cr feedback --- pkg/scale/comparison_test.go | 24 ++++++++++++---- pkg/scale/decode_test.go | 53 +++++++++++++++++++++++++++++++----- pkg/scale/encode.go | 18 ------------ pkg/scale/encode_test.go | 30 ++++++++++++++++++++ pkg/scale/scale_test.go | 1 - 5 files changed, 95 insertions(+), 31 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 6b2fa07f57..b2367c0239 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -175,11 +175,25 @@ func BenchmarkMarshal(b *testing.B) { } func BenchmarkUnmarshal(b *testing.B) { - for _, tt := range allTests { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return + for i := 0; i < b.N; i++ { + for _, tt := range allTests { + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + } + } +} + +func BenchmarkMarshal(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, tt := range allTests { + // dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if _, err := Marshal(tt.in); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } } } } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 2cc76640bf..0fc1d35a5d 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -339,13 +339,8 @@ func Test_unmarshal_optionality(t *testing.T) { want: t.want, out: t.out, } - switch t.in { - case nil: - // this doesn't actually happen since none of the tests have nil value for tt.in - ptrTest.want = []byte{0x00} - default: - ptrTest.want = append([]byte{0x01}, t.want...) - } + + ptrTest.want = append([]byte{0x01}, t.want...) ptrTests = append(ptrTests, ptrTest) } for _, tt := range ptrTests { @@ -369,3 +364,47 @@ func Test_unmarshal_optionality(t *testing.T) { }) } } + +func Test_unmarshal_optionality_nil_case(t *testing.T) { + var ptrTests tests + for _, t := range allTests { + ptrTest := test{ + name: t.name, + in: t.in, + wantErr: t.wantErr, + want: t.want, + // ignore out, since we are testing nil case + // out: t.out, + } + ptrTest.want = []byte{0x00} + + temp := reflect.New(reflect.TypeOf(t.in)) + // create a new pointer to type of temp + tempv := reflect.New(reflect.PtrTo(temp.Type()).Elem()) + // set zero value to elem of **temp so that is nil + tempv.Elem().Set(reflect.Zero(tempv.Elem().Type())) + // set test.in to *temp + ptrTest.in = tempv.Elem().Interface() + + ptrTests = append(ptrTests, ptrTest) + } + for _, tt := range ptrTests { + t.Run(tt.name, func(t *testing.T) { + // this becomes a pointer to a zero value of the underlying value + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface()) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) + } +} diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 610df7cd6c..ecc1b4234e 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -109,25 +109,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - // _, ok := in.(VaryingDataTypeValue) - // switch ok { - // case true: - // t := reflect.TypeOf(in) - // switch t.Kind() { - // // TODO: support more primitive types. Do we need to support arrays and slices as well? - // case reflect.Int: - // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() - // case reflect.Int16: - // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() - // default: - // err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - // return - // } - // err = es.marshal(in) - // default: err = fmt.Errorf("unsupported type: %T", in) - // } - } } return diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 146f07ed74..6681662f65 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -169,6 +169,11 @@ var ( in: uint(9223372036854775807), want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomUint(9223372036854775807)", + in: myCustomUint(9223372036854775807), + want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } int64Tests = tests{ { @@ -200,6 +205,11 @@ var ( in: int64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomInt64(9223372036854775807)", + in: myCustomInt64(9223372036854775807), + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } uint64Tests = tests{ { @@ -229,6 +239,11 @@ var ( in: uint64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomUint64(9223372036854775807)", + in: myCustomUint64(9223372036854775807), + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } int32Tests = tests{ { @@ -275,6 +290,11 @@ var ( in: uint32(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f}, }, + { + name: "uint32(1073741823)", + in: myCustomUint32(1073741823), + want: []byte{0xff, 0xff, 0xff, 0x3f}, + }, } int8Tests = tests{ { @@ -301,6 +321,11 @@ var ( in: uint8(1), want: []byte{0x01}, }, + { + name: "myCustomInt8(1)", + in: myCustomUint8(1), + want: []byte{0x01}, + }, } int16Tests = tests{ { @@ -320,6 +345,11 @@ var ( in: int16(16383), want: []byte{0xff, 0x3f}, }, + { + name: "myCustomInt16(16383)", + in: myCustomInt16(16383), + want: []byte{0xff, 0x3f}, + }, } uint16Tests = tests{ { diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index 632ab804d4..a96ab2b539 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -28,7 +28,6 @@ func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { wantIndices fieldScaleIndices wantErr bool }{ - // TODO: Add test cases. { in: struct{ Foo int }{}, wantIndices: fieldScaleIndices{ From 3b4fd36e2d97cd70772499c9c98686da9567cb61 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 14 Jun 2021 12:04:15 -0400 Subject: [PATCH 092/245] wip --- pkg/scale/decode.go | 20 ++++++- pkg/scale/encode.go | 25 ++++++-- pkg/scale/result_test.go | 51 ++++++++++++++++ pkg/scale/something_test.go | 114 ++++++++++++++++++++++++++++++++++++ 4 files changed, 202 insertions(+), 8 deletions(-) create mode 100644 pkg/scale/something_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 8b758ed60b..0bc90c5663 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -134,13 +134,27 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { err = ds.decodePointer(dstv) case reflect.Struct: t := reflect.TypeOf(in) - // check if this is a convertible to Result, if so encode using decodeResult - switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + field, ok := t.FieldByName("Result") + switch ok { case true: + if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { + err = fmt.Errorf("%T is not a Result", in) + return + } + // res := reflect.ValueOf(in).FieldByName("Result") + // fmt.Println("yao!", res) err = ds.decodeResult(dstv) - case false: + default: err = ds.decodeStruct(dstv) } + + // // check if this is a convertible to Result, if so encode using decodeResult + // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + // case true: + // err = ds.decodeResult(dstv) + // case false: + // err = ds.decodeStruct(dstv) + // } case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index ecc1b4234e..a9e9ab789d 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -86,16 +86,31 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: + // fmt.Println("in here!") t := reflect.TypeOf(in) - // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType - switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + field, ok := t.FieldByName("Result") + switch ok { case true: - resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) - err = es.encodeResult(resv.Interface().(Result)) - case false: + if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { + err = fmt.Errorf("%T is not a Result", in) + return + } + res := reflect.ValueOf(in).FieldByName("Result").Interface().(Result) + // fmt.Println("yao!", res) + err = es.encodeResult(res) + default: err = es.encodeStruct(in) } + // check if this is a type with an embedded Result, aka a registered result + // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + // case true: + // resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) + // err = es.encodeResult(resv.Interface().(Result)) + // case false: + // err = es.encodeStruct(in) + // } + case reflect.Array: err = es.encodeArray(in) case reflect.Slice: diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index d5b5320974..52f4c19edf 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -317,3 +317,54 @@ func TestResult_Set(t *testing.T) { }) } } + +// func TestNilOk(t *testing.T) { +// mr := MyResult{} +// mr.SetOk(nil) +// bytes, err := Marshal(mr) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x00}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } + +// mr1 := MyResult{} +// mr1.SetErr(true) +// bytes, err = Marshal(mr1) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } +// } + +// func TestBothNil(t *testing.T) { +// mr := MyResult{} +// mr.SetOk(nil) +// bytes, err := Marshal(mr) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x00}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } + +// mr1 := MyResult{} +// mr1.SetErr(nil) +// bytes, err = Marshal(mr1) +// if err != nil { +// t.Errorf("%v", err) +// return +// } +// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } +// } diff --git a/pkg/scale/something_test.go b/pkg/scale/something_test.go new file mode 100644 index 0000000000..8194c32890 --- /dev/null +++ b/pkg/scale/something_test.go @@ -0,0 +1,114 @@ +package scale_test + +import ( + "fmt" + "testing" + + "github.com/ChainSafe/gossamer/pkg/scale" +) + +type MyStruct struct { + Baz bool + Bar uint32 + Foo []byte +} + +func (ms MyStruct) Index() uint { + return 1 +} + +type MyOtherStruct struct { + Foo string + Bar uint64 + Baz uint +} + +func (mos MyOtherStruct) Index() uint { + return 2 +} + +type MyInt16 int16 + +func (mi16 MyInt16) Index() uint { + return 3 +} + +type MyVaryingDataType scale.VaryingDataType + +func varyingDataTypeExample() { + err := scale.RegisterVaryingDataType(MyVaryingDataType{}, MyStruct{}, MyOtherStruct{}, MyInt16(0)) + if err != nil { + panic(err) + } + + mvdt := MyVaryingDataType{ + MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + }, + MyOtherStruct{ + Foo: "hello", + Bar: 999, + Baz: 888, + }, + MyInt16(111), + } + bytes, err := scale.Marshal(mvdt) + if err != nil { + panic(err) + } + + var unmarshaled MyVaryingDataType + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // [{Baz:true Bar:999 Foo:[1 2]} {Foo:hello Bar:999 Baz:888} 111] + fmt.Printf("%+v", unmarshaled) +} +func structExample() { + type MyStruct struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + } + var ms = MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + } + bytes, err := scale.Marshal(ms) + if err != nil { + panic(err) + } + + var unmarshaled MyStruct + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // Baz:true Bar:999 Foo:[1 2]} + fmt.Printf("%+v", unmarshaled) +} +func TestSomething(t *testing.T) { + // // compact length encoded uint + // var ui uint = 999 + // bytes, err := scale.Marshal(ui) + // if err != nil { + // panic(err) + // } + + // var unmarshaled uint + // err = scale.Unmarshal(bytes, &unmarshaled) + // if err != nil { + // panic(err) + // } + + // fmt.Printf("%d", unmarshaled) + + // structExample() + varyingDataTypeExample() +} From a87135e339451786d4d50ae509b31ab984ee136e Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 16 Jun 2021 15:37:38 -0400 Subject: [PATCH 093/245] revise Result --- pkg/scale/decode.go | 23 +------ pkg/scale/encode.go | 26 +------- pkg/scale/result_test.go | 120 +++++++++++++++++++++--------------- pkg/scale/something_test.go | 114 ---------------------------------- 4 files changed, 72 insertions(+), 211 deletions(-) delete mode 100644 pkg/scale/something_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 0bc90c5663..e551c6a9d1 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -133,28 +133,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Ptr: err = ds.decodePointer(dstv) case reflect.Struct: - t := reflect.TypeOf(in) - field, ok := t.FieldByName("Result") - switch ok { - case true: - if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { - err = fmt.Errorf("%T is not a Result", in) - return - } - // res := reflect.ValueOf(in).FieldByName("Result") - // fmt.Println("yao!", res) - err = ds.decodeResult(dstv) - default: - err = ds.decodeStruct(dstv) - } - - // // check if this is a convertible to Result, if so encode using decodeResult - // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { - // case true: - // err = ds.decodeResult(dstv) - // case false: - // err = ds.decodeStruct(dstv) - // } + err = ds.decodeStruct(dstv) case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index a9e9ab789d..7c5e80afca 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -86,31 +86,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: - // fmt.Println("in here!") - t := reflect.TypeOf(in) - field, ok := t.FieldByName("Result") - switch ok { - case true: - if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { - err = fmt.Errorf("%T is not a Result", in) - return - } - res := reflect.ValueOf(in).FieldByName("Result").Interface().(Result) - // fmt.Println("yao!", res) - err = es.encodeResult(res) - default: - err = es.encodeStruct(in) - } - - // check if this is a type with an embedded Result, aka a registered result - // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { - // case true: - // resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) - // err = es.encodeResult(resv.Interface().(Result)) - // case false: - // err = es.encodeStruct(in) - // } - + err = es.encodeStruct(in) case reflect.Array: err = es.encodeArray(in) case reflect.Slice: diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 52f4c19edf..62efd25068 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -318,53 +318,73 @@ func TestResult_Set(t *testing.T) { } } -// func TestNilOk(t *testing.T) { -// mr := MyResult{} -// mr.SetOk(nil) -// bytes, err := Marshal(mr) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x00}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } - -// mr1 := MyResult{} -// mr1.SetErr(true) -// bytes, err = Marshal(mr1) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } -// } - -// func TestBothNil(t *testing.T) { -// mr := MyResult{} -// mr.SetOk(nil) -// bytes, err := Marshal(mr) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x00}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } - -// mr1 := MyResult{} -// mr1.SetErr(nil) -// bytes, err = Marshal(mr1) -// if err != nil { -// t.Errorf("%v", err) -// return -// } -// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } -// } +func TestResult_Set(t *testing.T) { + type args struct { + mode ResultMode + in interface{} + } + tests := []struct { + name string + res Result + args args + wantErr bool + }{ + // TODO: Add test cases. + { + args: args{ + mode: Unset, + }, + wantErr: true, + }, + { + args: args{ + mode: OK, + in: nil, + }, + }, + { + args: args{ + mode: Err, + in: nil, + }, + }, + { + args: args{ + mode: OK, + in: true, + }, + res: NewResult(true, nil), + }, + { + args: args{ + mode: Err, + in: true, + }, + res: NewResult(nil, true), + }, + { + args: args{ + mode: OK, + in: true, + }, + res: NewResult("ok", "err"), + wantErr: true, + }, + { + args: args{ + mode: Err, + in: nil, + }, + res: NewResult(nil, true), + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := tt.res + if err := r.Set(tt.args.mode, tt.args.in); (err != nil) != tt.wantErr { + t.Errorf("Result.Set() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/scale/something_test.go b/pkg/scale/something_test.go deleted file mode 100644 index 8194c32890..0000000000 --- a/pkg/scale/something_test.go +++ /dev/null @@ -1,114 +0,0 @@ -package scale_test - -import ( - "fmt" - "testing" - - "github.com/ChainSafe/gossamer/pkg/scale" -) - -type MyStruct struct { - Baz bool - Bar uint32 - Foo []byte -} - -func (ms MyStruct) Index() uint { - return 1 -} - -type MyOtherStruct struct { - Foo string - Bar uint64 - Baz uint -} - -func (mos MyOtherStruct) Index() uint { - return 2 -} - -type MyInt16 int16 - -func (mi16 MyInt16) Index() uint { - return 3 -} - -type MyVaryingDataType scale.VaryingDataType - -func varyingDataTypeExample() { - err := scale.RegisterVaryingDataType(MyVaryingDataType{}, MyStruct{}, MyOtherStruct{}, MyInt16(0)) - if err != nil { - panic(err) - } - - mvdt := MyVaryingDataType{ - MyStruct{ - Baz: true, - Bar: 999, - Foo: []byte{1, 2}, - }, - MyOtherStruct{ - Foo: "hello", - Bar: 999, - Baz: 888, - }, - MyInt16(111), - } - bytes, err := scale.Marshal(mvdt) - if err != nil { - panic(err) - } - - var unmarshaled MyVaryingDataType - err = scale.Unmarshal(bytes, &unmarshaled) - if err != nil { - panic(err) - } - - // [{Baz:true Bar:999 Foo:[1 2]} {Foo:hello Bar:999 Baz:888} 111] - fmt.Printf("%+v", unmarshaled) -} -func structExample() { - type MyStruct struct { - Baz bool `scale:"3"` - Bar int32 `scale:"2"` - Foo []byte `scale:"1"` - } - var ms = MyStruct{ - Baz: true, - Bar: 999, - Foo: []byte{1, 2}, - } - bytes, err := scale.Marshal(ms) - if err != nil { - panic(err) - } - - var unmarshaled MyStruct - err = scale.Unmarshal(bytes, &unmarshaled) - if err != nil { - panic(err) - } - - // Baz:true Bar:999 Foo:[1 2]} - fmt.Printf("%+v", unmarshaled) -} -func TestSomething(t *testing.T) { - // // compact length encoded uint - // var ui uint = 999 - // bytes, err := scale.Marshal(ui) - // if err != nil { - // panic(err) - // } - - // var unmarshaled uint - // err = scale.Unmarshal(bytes, &unmarshaled) - // if err != nil { - // panic(err) - // } - - // fmt.Printf("%d", unmarshaled) - - // structExample() - varyingDataTypeExample() -} From f8174cd3550258ac7d067f1e5f53dc2d7504c2a2 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 18 Jun 2021 22:13:39 -0400 Subject: [PATCH 094/245] wip VaryingDataType and VaryingDataTypeSlice --- pkg/scale/decode_test.go | 46 ++++-- pkg/scale/encode.go | 10 +- pkg/scale/varying_data_type_test.go | 231 ++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+), 24 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 0fc1d35a5d..2a3400d907 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -345,22 +345,38 @@ func Test_unmarshal_optionality(t *testing.T) { } for _, tt := range ptrTests { t.Run(tt.name, func(t *testing.T) { - // this becomes a pointer to a zero value of the underlying value - dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - var diff string - if tt.out != nil { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } + switch in := tt.in.(type) { + case VaryingDataType: + // copy the inputted vdt cause we need the cached values + copy := in + vdt := copy + vdt.value = nil + var dst interface{} = &vdt + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + diff := cmp.Diff(vdt.value, tt.in.(VaryingDataType).value, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + default: + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + } }) } } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 7c5e80afca..7c62a77bd5 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -90,15 +90,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { case reflect.Array: err = es.encodeArray(in) case reflect.Slice: - t := reflect.TypeOf(in) - // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType - switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { - case true: - invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) - err = es.encodeVaryingDataType(invdt.Interface().(VaryingDataType)) - case false: - err = es.encodeSlice(in) - } + err = es.encodeSlice(in) default: err = fmt.Errorf("unsupported type: %T", in) } diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index d92dd7c723..0abe0569cb 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -638,3 +638,234 @@ func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { }) } } + +func TestNewVaryingDataType(t *testing.T) { + type args struct { + values []VaryingDataTypeValue + } + tests := []struct { + name string + args args + wantVdt VaryingDataType + wantErr bool + }{ + { + args: args{ + values: []VaryingDataTypeValue{}, + }, + wantErr: true, + }, + { + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + }, + }, + wantVdt: VaryingDataType{ + cache: map[uint]VaryingDataTypeValue{ + VDTValue{}.Index(): VDTValue{}, + VDTValue1{}.Index(): VDTValue1{}, + VDTValue2{}.Index(): VDTValue2{}, + VDTValue3(0).Index(): VDTValue3(0), + }, + }, + }, + { + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), VDTValue{}, + }, + }, + wantVdt: VaryingDataType{ + cache: map[uint]VaryingDataTypeValue{ + VDTValue{}.Index(): VDTValue{}, + VDTValue1{}.Index(): VDTValue1{}, + VDTValue2{}.Index(): VDTValue2{}, + VDTValue3(0).Index(): VDTValue3(0), + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotVdt, err := NewVaryingDataType(tt.args.values...) + if (err != nil) != tt.wantErr { + t.Errorf("NewVaryingDataType() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotVdt, tt.wantVdt) { + t.Errorf("NewVaryingDataType() = %v, want %v", gotVdt, tt.wantVdt) + } + }) + } +} + +func TestVaryingDataType_Set(t *testing.T) { + type args struct { + value VaryingDataTypeValue + } + tests := []struct { + name string + vdt VaryingDataType + args args + wantErr bool + }{ + { + vdt: mustNewVaryingDataType(VDTValue1{}), + args: args{ + value: VDTValue1{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue1{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue2{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue3(0), + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + vdt := tt.vdt + if err := vdt.Set(tt.args.value); (err != nil) != tt.wantErr { + t.Errorf("VaryingDataType.SetValue() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestVaryingDataTypeSlice_Add(t *testing.T) { + type args struct { + values []VaryingDataTypeValue + } + tests := []struct { + name string + vdts VaryingDataTypeSlice + args args + wantErr bool + wantValues []VaryingDataType + }{ + { + name: "happy path", + vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0))), + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{ + B: 1, + }, + }, + }, + wantValues: []VaryingDataType{ + mustNewVaryingDataTypeAndSet( + VDTValue{ + B: 1, + }, + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + }, + }, + { + name: "invalid value error case", + vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{})), + args: args{ + values: []VaryingDataTypeValue{ + VDTValue3(0), + }, + }, + wantValues: []VaryingDataType{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + vdts := &tt.vdts + if err := vdts.Add(tt.args.values...); (err != nil) != tt.wantErr { + t.Errorf("VaryingDataTypeSlice.Add() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(vdts.Values, tt.wantValues) { + t.Errorf("NewVaryingDataType() = %v, want %v", vdts.Values, tt.wantValues) + } + }) + } +} + +var varyingDataTypeSliceTests = tests{ + { + in: mustNewVaryingDataTypeSliceAndSet( + mustNewVaryingDataType( + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, + ), + want: []byte{ + // length + 4, + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + }, +} + +func Test_encodeState_encodeVaryingDataTypeSlice(t *testing.T) { + for _, tt := range varyingDataTypeSliceTests { + t.Run(tt.name, func(t *testing.T) { + vdt := tt.in.(VaryingDataTypeSlice) + b, err := Marshal(vdt) + if (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(b, tt.want) { + t.Errorf("encodeState.encodeStruct() = %v, want %v", b, tt.want) + } + }) + } +} + +// func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { +// for _, tt := range varyingDataTypeTests { +// t.Run(tt.name, func(t *testing.T) { +// dst, err := NewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0)) +// if err != nil { +// t.Errorf("%v", err) +// return +// } +// if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { +// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// return +// } +// vdt := tt.in.(VaryingDataType) +// diff := cmp.Diff(dst.Value(), vdt.Value(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) +// if diff != "" { +// t.Errorf("decodeState.unmarshal() = %s", diff) +// } +// }) +// } +// } From a94b2975051b70b707441e7fd8ed70052f193dab Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 21 Jun 2021 11:00:07 -0400 Subject: [PATCH 095/245] refactor VaryingDataType and add VaryingDataTypeSlice --- pkg/scale/varying_data_type_test.go | 157 ++++++++++++++++++++-------- 1 file changed, 114 insertions(+), 43 deletions(-) diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index 0abe0569cb..d63063acfd 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -810,27 +810,97 @@ var varyingDataTypeSliceTests = tests{ ), VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, ), - want: []byte{ - // length - 4, - // index - 2, - // value - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - }, + want: newWant( + []byte{ + // length + 4, + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + ), + }, + { + in: mustNewVaryingDataTypeSliceAndSet( + mustNewVaryingDataType( + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, + VDTValue{ + A: big.NewInt(1073741823), + B: int(1073741823), + C: uint(1073741823), + D: int8(1), + E: uint8(1), + F: int16(16383), + G: uint16(16383), + H: int32(1073741823), + I: uint32(1073741823), + J: int64(9223372036854775807), + K: uint64(9223372036854775807), + L: byteArray(64), + M: testStrings[1], + N: true, + }, + ), + want: newWant( + []byte{ + // length + 8, + }, + []byte{ + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + []byte{ + // index + 1, + // value + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, + 0x01, + 0x01, + 0xff, 0x3f, + 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + }, + append([]byte{0x01, 0x01}, byteArray(64)...), + append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), + []byte{0x01}, + ), }, } @@ -840,32 +910,33 @@ func Test_encodeState_encodeVaryingDataTypeSlice(t *testing.T) { vdt := tt.in.(VaryingDataTypeSlice) b, err := Marshal(vdt) if (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr) } if !reflect.DeepEqual(b, tt.want) { - t.Errorf("encodeState.encodeStruct() = %v, want %v", b, tt.want) + t.Errorf("Marshal() = %v, want %v", b, tt.want) } }) } } -// func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { -// for _, tt := range varyingDataTypeTests { -// t.Run(tt.name, func(t *testing.T) { -// dst, err := NewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0)) -// if err != nil { -// t.Errorf("%v", err) -// return -// } -// if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { -// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// vdt := tt.in.(VaryingDataType) -// diff := cmp.Diff(dst.Value(), vdt.Value(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) -// if diff != "" { -// t.Errorf("decodeState.unmarshal() = %s", diff) -// } -// }) -// } -// } +func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { + opt := cmp.Comparer(func(x, y VaryingDataType) bool { + return reflect.DeepEqual(x.value, y.value) && reflect.DeepEqual(x.cache, y.cache) + }) + + for _, tt := range varyingDataTypeSliceTests { + t.Run(tt.name, func(t *testing.T) { + dst := tt.in.(VaryingDataTypeSlice) + dst.Types = make([]VaryingDataType, 0) + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + vdts := tt.in.(VaryingDataTypeSlice) + diff := cmp.Diff(dst, vdts, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}), opt) + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) + } +} From e8b76526de72f7c63ce1adb67d155b67664a998d Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 21 Jun 2021 11:03:11 -0600 Subject: [PATCH 096/245] integrate scale pkg into lib/transaction --- go.mod | 4 ---- lib/transaction/types.go | 35 ----------------------------------- lib/transaction/types_test.go | 3 ++- 3 files changed, 2 insertions(+), 40 deletions(-) diff --git a/go.mod b/go.mod index dd50017e94..01a0602622 100644 --- a/go.mod +++ b/go.mod @@ -19,11 +19,7 @@ require ( github.com/go-playground/validator/v10 v10.4.1 github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect -<<<<<<< HEAD github.com/google/go-cmp v0.5.6 -======= - github.com/google/go-cmp v0.5.6 // indirect ->>>>>>> fix struct tests and optionality tests github.com/google/uuid v1.1.5 // indirect github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 diff --git a/lib/transaction/types.go b/lib/transaction/types.go index 9c743c84a3..6d538ab37f 100644 --- a/lib/transaction/types.go +++ b/lib/transaction/types.go @@ -17,10 +17,7 @@ package transaction import ( - "encoding/binary" - "github.com/ChainSafe/gossamer/dot/types" - "github.com/ChainSafe/gossamer/lib/scale" ) // Validity struct see: https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/sr-primitives/src/transaction_validity.rs#L178 @@ -56,35 +53,3 @@ func NewValidTransaction(extrinsic types.Extrinsic, validity *Validity) *ValidTr Validity: validity, } } - -// Encode SCALE encodes the transaction -func (vt *ValidTransaction) Encode() ([]byte, error) { - enc := []byte(vt.Extrinsic) - - buf := make([]byte, 8) - binary.LittleEndian.PutUint64(buf, vt.Validity.Priority) - enc = append(enc, buf...) - - d, err := scale.Encode(vt.Validity.Requires) - if err != nil { - return nil, err - } - enc = append(enc, d...) - - d, err = scale.Encode(vt.Validity.Provides) - if err != nil { - return nil, err - } - enc = append(enc, d...) - - binary.LittleEndian.PutUint64(buf, vt.Validity.Longevity) - enc = append(enc, buf...) - - if vt.Validity.Propagate { - enc = append(enc, 1) - } else { - enc = append(enc, 0) - } - - return enc, nil -} diff --git a/lib/transaction/types_test.go b/lib/transaction/types_test.go index b1e9062a27..d59545dfc8 100644 --- a/lib/transaction/types_test.go +++ b/lib/transaction/types_test.go @@ -3,6 +3,7 @@ package transaction import ( "testing" + "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) @@ -18,7 +19,7 @@ func TestValidTransaction_Encode(t *testing.T) { extrinsic := []byte("nootwashere") vt := NewValidTransaction(extrinsic, validity) - enc, err := vt.Encode() + enc, err := scale.Marshal(vt) require.NoError(t, err) if len(enc) == 0 { From 4a65e58eea1882c53a1bb019538ee72fabef9a83 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 11:01:44 -0600 Subject: [PATCH 097/245] WIP/Integrate scale pkg into lib/babe --- lib/babe/errors.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index d8f4b41d94..d69cea7a65 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -16,9 +16,8 @@ package babe import ( "errors" "fmt" - "github.com/ChainSafe/gossamer/lib/common/optional" - "github.com/ChainSafe/gossamer/lib/scale" + "github.com/ChainSafe/gossamer/pkg/scale" ) var ( @@ -95,8 +94,15 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { switch res[0] { case 0: - unKnownError, _ := scale.Decode(res[1:], []byte{}) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", string(unKnownError.([]byte)))} + //unKnownError, _ := scale.Decode(res[1:], []byte{}) + //fmt.Sprintf("Output: %s", string(unKnownError.([]byte))) + //fmt.Println("Output:") + // This has not been working + var v []byte + fmt.Println(res) + unKnownError := scale.Unmarshal(res[1:], &v) + fmt.Println("Output: " + unKnownError.Error()) + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", unKnownError.Error())} case 1: return &DispatchOutcomeError{"failed lookup"} case 2: From fd885c38eed602f5eb6b912c37911e2982200711 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 11:26:59 -0600 Subject: [PATCH 098/245] fixed error unmarshaling in errors.go --- lib/babe/errors.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index d69cea7a65..084dfa87ad 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -94,15 +94,14 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { switch res[0] { case 0: - //unKnownError, _ := scale.Decode(res[1:], []byte{}) - //fmt.Sprintf("Output: %s", string(unKnownError.([]byte))) - //fmt.Println("Output:") - // This has not been working var v []byte - fmt.Println(res) - unKnownError := scale.Unmarshal(res[1:], &v) - fmt.Println("Output: " + unKnownError.Error()) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", unKnownError.Error())} + err := scale.Unmarshal(res[1:], &v) + if err != nil { + // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure + // TODO Create stucts for errors and integrate into Varying data type + return errInvalidResult + } + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} case 1: return &DispatchOutcomeError{"failed lookup"} case 2: From 847084eb9633b72cf7408ec2cee1b00c403926c9 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 12:03:02 -0600 Subject: [PATCH 099/245] WIP/Integrate scale into lib/babe --- lib/babe/build.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index 66b181caf8..b821d73e60 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -31,6 +31,7 @@ import ( "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" + scale2 "github.com/ChainSafe/gossamer/pkg/scale" ) const ( @@ -347,16 +348,17 @@ func hasSlotEnded(slot Slot) bool { return time.Since(slotEnd) >= 0 } -// ExtrinsicsToBody returns scale encoded block body which contains inherent and extrinsic. +// ExtrinsicsToBody returns scale2 encoded block body which contains inherent and extrinsic. func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (*types.Body, error) { extrinsics := types.BytesArrayToExtrinsics(inherents) for _, tx := range txs { - decExt, err := scale.Decode(tx.Extrinsic, []byte{}) + var decExt []byte + err := scale2.Unmarshal(tx.Extrinsic, &decExt) if err != nil { return nil, err } - extrinsics = append(extrinsics, decExt.([]byte)) + extrinsics = append(extrinsics, decExt) } return types.NewBodyFromExtrinsics(extrinsics) From 0f8006b4a1e572f7f204774f1080a318f8fa08a4 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 13:31:50 -0600 Subject: [PATCH 100/245] integrate scale package into babe library --- lib/babe/build.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index b821d73e60..1972e265b0 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -29,9 +29,8 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/runtime" - "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" - scale2 "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/ChainSafe/gossamer/pkg/scale" ) const ( @@ -306,14 +305,15 @@ func (b *BlockBuilder) buildBlockInherents(slot Slot) ([][]byte, error) { } // decode inherent extrinsics - exts, err := scale.Decode(inherentExts, [][]byte{}) + var exts [][]byte + err = scale.Unmarshal(inherentExts, &exts) if err != nil { return nil, err } // apply each inherent extrinsic - for _, ext := range exts.([][]byte) { - in, err := scale.Encode(ext) + for _, ext := range exts { + in, err := scale.Marshal(ext) if err != nil { return nil, err } @@ -329,7 +329,7 @@ func (b *BlockBuilder) buildBlockInherents(slot Slot) ([][]byte, error) { } } - return exts.([][]byte), nil + return exts, nil } func (b *BlockBuilder) addToQueue(txs []*transaction.ValidTransaction) { @@ -354,7 +354,7 @@ func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) ( for _, tx := range txs { var decExt []byte - err := scale2.Unmarshal(tx.Extrinsic, &decExt) + err := scale.Unmarshal(tx.Extrinsic, &decExt) if err != nil { return nil, err } From d4bed00c2a2e76931d7662af974e908b5a597937 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 22 Jun 2021 17:01:03 -0400 Subject: [PATCH 101/245] fix result.set with nil value --- pkg/scale/result_test.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 62efd25068..c63c196ae0 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -329,24 +329,39 @@ func TestResult_Set(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. { args: args{ mode: Unset, }, + res: NewResult(nil, nil), wantErr: true, + wantResult: Result{ + ok: empty{}, err: empty{}, + }, }, { args: args{ mode: OK, in: nil, }, + res: NewResult(nil, nil), + wantResult: Result{ + ok: empty{}, + err: empty{}, + mode: OK, + }, }, { args: args{ mode: Err, in: nil, }, + res: NewResult(nil, nil), + wantResult: Result{ + ok: empty{}, + err: empty{}, + mode: Err, + }, }, { args: args{ @@ -354,6 +369,11 @@ func TestResult_Set(t *testing.T) { in: true, }, res: NewResult(true, nil), + wantResult: Result{ + ok: true, + err: empty{}, + mode: OK, + }, }, { args: args{ @@ -361,6 +381,11 @@ func TestResult_Set(t *testing.T) { in: true, }, res: NewResult(nil, true), + wantResult: Result{ + ok: empty{}, + err: true, + mode: Err, + }, }, { args: args{ @@ -369,6 +394,10 @@ func TestResult_Set(t *testing.T) { }, res: NewResult("ok", "err"), wantErr: true, + wantResult: Result{ + ok: "ok", + err: "err", + }, }, { args: args{ @@ -377,6 +406,10 @@ func TestResult_Set(t *testing.T) { }, res: NewResult(nil, true), wantErr: true, + wantResult: Result{ + ok: empty{}, + err: true, + }, }, } for _, tt := range tests { @@ -385,6 +418,9 @@ func TestResult_Set(t *testing.T) { if err := r.Set(tt.args.mode, tt.args.in); (err != nil) != tt.wantErr { t.Errorf("Result.Set() error = %v, wantErr %v", err, tt.wantErr) } + if !reflect.DeepEqual(tt.wantResult, r) { + t.Errorf("Result.Unwrap() = %v, want %v", tt.wantResult, r) + } }) } } From 7f81d13db32ec5fe828c40d4203f88c2f623419e Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 15:06:02 -0600 Subject: [PATCH 102/245] WIP/refactor babe errors to utilize new scale pkg --- lib/babe/errors.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 084dfa87ad..cd43eae711 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -150,9 +150,36 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } +type UnknownError struct { + Err string +} + +func (err UnknownError) Index() uint { + return 1 +} + +type FailedLookup struct { + Err string +} + +func (err FailedLookup) Index() uint { + return 2 +} + +type BadOrigin struct { + Err string +} + +func (err BadOrigin) Index() uint { + return 3 +} + func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome + result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) + fmt.Println("Result err") + fmt.Println(result) switch res[1] { case 0: return nil From 6322ef61a90b1b7210f26322a8977e34f7eb549d Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 15:56:28 -0600 Subject: [PATCH 103/245] Integrate scale pkg into babe tests --- lib/babe/build_test.go | 7 ++++--- lib/babe/errors.go | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/babe/build_test.go b/lib/babe/build_test.go index 73bb4b3165..d18247c833 100644 --- a/lib/babe/build_test.go +++ b/lib/babe/build_test.go @@ -26,8 +26,8 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/sr25519" - "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" + "github.com/ChainSafe/gossamer/pkg/scale" log "github.com/ChainSafe/log15" cscale "github.com/centrifuge/go-substrate-rpc-client/v2/scale" "github.com/centrifuge/go-substrate-rpc-client/v2/signature" @@ -223,11 +223,12 @@ func TestBuildAndApplyExtrinsic(t *testing.T) { // build extrinsic rawMeta, err := babeService.rt.Metadata() require.NoError(t, err) - decoded, err := scale.Decode(rawMeta, []byte{}) + var decoded []byte + err = scale.Unmarshal(rawMeta, []byte{}) require.NoError(t, err) meta := &ctypes.Metadata{} - err = ctypes.DecodeFromBytes(decoded.([]byte), meta) + err = ctypes.DecodeFromBytes(decoded, meta) require.NoError(t, err) rv, err := babeService.rt.Version() diff --git a/lib/babe/errors.go b/lib/babe/errors.go index cd43eae711..60133ac699 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -178,7 +178,11 @@ func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) - fmt.Println("Result err") + //err := result.Set(scale.OK, nil) + //if err != nil { + // panic(err) + //} + fmt.Println("Result") fmt.Println(result) switch res[1] { case 0: From 3b679a2f7547e06d3cb7316195532774affdc307 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 16:22:12 -0600 Subject: [PATCH 104/245] create init structs for dispatch outcome errors --- lib/babe/errors.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 60133ac699..eb48825b43 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -174,10 +174,18 @@ func (err BadOrigin) Index() uint { return 3 } +type CustomModuleError struct { + Err string +} + +func (err CustomModuleError) Index() uint { + return 4 +} + func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) + result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) //err := result.Set(scale.OK, nil) //if err != nil { // panic(err) From 6308a04609febbe413a914f2d32286e6cc67d287 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 16:49:47 -0600 Subject: [PATCH 105/245] WIP/refactor babe error handling to utiliize new scale pkg features --- lib/babe/errors.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index eb48825b43..522e4f636d 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -151,7 +151,7 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - Err string + Err DispatchOutcomeError } func (err UnknownError) Index() uint { @@ -159,7 +159,7 @@ func (err UnknownError) Index() uint { } type FailedLookup struct { - Err string + err DispatchOutcomeError } func (err FailedLookup) Index() uint { @@ -167,7 +167,7 @@ func (err FailedLookup) Index() uint { } type BadOrigin struct { - Err string + Err DispatchOutcomeError } func (err BadOrigin) Index() uint { @@ -175,7 +175,7 @@ func (err BadOrigin) Index() uint { } type CustomModuleError struct { - Err string + Err DispatchOutcomeError } func (err CustomModuleError) Index() uint { @@ -190,6 +190,11 @@ func determineErr(res []byte) error { //if err != nil { // panic(err) //} + + // This code chunk works + v := FailedLookup{err: DispatchOutcomeError{"failed lookup"}} + fmt.Println(v.err) + fmt.Println("Result") fmt.Println(result) switch res[1] { From ce14097a99f021eafe5d344cfed488d5357a1ffe Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 15:44:21 -0600 Subject: [PATCH 106/245] WIP scale/babe integration. Closer but still errors --- lib/babe/errors.go | 116 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 91 insertions(+), 25 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 522e4f636d..e03a5c0525 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -63,10 +63,13 @@ var ( ) // A DispatchOutcomeError is outcome of dispatching the extrinsic +// Can I make this not a struct? type DispatchOutcomeError struct { msg string // description of error } +//type DispatchOutcomeError string + func (e DispatchOutcomeError) Error() string { return fmt.Sprintf("dispatch outcome error: %s", e.msg) } @@ -91,24 +94,81 @@ func determineCustomModuleErr(res []byte) error { return fmt.Errorf("index: %d code: %d message: %s", res[0], res[1], errMsg.String()) } +//func determineDispatchErr(res []byte) error { +// var v []byte +// err := scale.Unmarshal(res[1:], &v) +// if err != nil { +// // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure +// // TODO Create stucts for errors and integrate into Varying data type +// return errInvalidResult +// } +// +// switch res[0] { +// case 0: +// var v []byte +// err := scale.Unmarshal(res[1:], &v) +// if err != nil { +// // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure +// // TODO Create stucts for errors and integrate into Varying data type +// return errInvalidResult +// } +// return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} +// case 1: +// return &DispatchOutcomeError{"failed lookup"} +// case 2: +// return &DispatchOutcomeError{"bad origin"} +// case 3: +// return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))} +// } +// +// return errInvalidResult +//} + func determineDispatchErr(res []byte) error { - switch res[0] { - case 0: - var v []byte - err := scale.Unmarshal(res[1:], &v) - if err != nil { - // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure - // TODO Create stucts for errors and integrate into Varying data type - return errInvalidResult - } - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} - case 1: + //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) + vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) + //fmt.Println("Result") + //fmt.Println(result) + + //var v []byte + err := scale.Unmarshal(res[1:], &vdt) + if err != nil { + // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure + // TODO Create stucts for errors and integrate into Varying data type + return errInvalidResult + } + + + //fmt.Println("Unmarsalled dispatchErr Type:") + //fmt.Println(vdt.Value()) + + switch val := vdt.Value().(type){ + case UnknownError: + // For some reason its not entering here, going to customModuleError instead + // Maybe cuz struct is wrong + fmt.Println("Val:") + fmt.Println(val) + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val.Err)} + case FailedLookup: return &DispatchOutcomeError{"failed lookup"} - case 2: + case BadOrigin: return &DispatchOutcomeError{"bad origin"} - case 3: - return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))} + case CustomModuleError: + // Printing nice, not getting the correct values + return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} } + //// if custon error module + //if vdt.Value().Index() == 4 { + // err = vdt.Set(CustomModuleError{ + // Err: DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))}, + // }) + // if err != nil { + // panic(err) + // } + //} + //fmt.Println("Unmarsalled dispatchErr Value2:") + //fmt.Println(vdt.Value()) + return errInvalidResult } @@ -151,7 +211,7 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - Err DispatchOutcomeError + Err string } func (err UnknownError) Index() uint { @@ -159,7 +219,7 @@ func (err UnknownError) Index() uint { } type FailedLookup struct { - err DispatchOutcomeError + Err string } func (err FailedLookup) Index() uint { @@ -167,7 +227,7 @@ func (err FailedLookup) Index() uint { } type BadOrigin struct { - Err DispatchOutcomeError + Err string } func (err BadOrigin) Index() uint { @@ -175,28 +235,34 @@ func (err BadOrigin) Index() uint { } type CustomModuleError struct { - Err DispatchOutcomeError + index uint8 `scale:"3"` + err uint8 `scale:"2"` + message string `scale:"1"` // might need to be *string } func (err CustomModuleError) Index() uint { return 4 } +func (err CustomModuleError) String() string { + return fmt.Sprintf("index: %d code: %d message: %s", err.index, err.err, err.message) +} + func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) + //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) //err := result.Set(scale.OK, nil) //if err != nil { // panic(err) //} - // This code chunk works - v := FailedLookup{err: DispatchOutcomeError{"failed lookup"}} - fmt.Println(v.err) - - fmt.Println("Result") - fmt.Println(result) + //// This code chunk works + //v := FailedLookup{Err: DispatchOutcomeError{"failed lookup"}} + //fmt.Println(v.Err) + // + //fmt.Println("Result") + //fmt.Println(result) switch res[1] { case 0: return nil From 090bf451f1cbbb8f6d29d407362350ddedbde4ee Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 16:29:40 -0600 Subject: [PATCH 107/245] WIP/Fix type issues in babe refactor --- lib/babe/errors.go | 55 ++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 39 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index e03a5c0525..92515d0bb2 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -63,7 +63,6 @@ var ( ) // A DispatchOutcomeError is outcome of dispatching the extrinsic -// Can I make this not a struct? type DispatchOutcomeError struct { msg string // description of error } @@ -125,49 +124,39 @@ func determineCustomModuleErr(res []byte) error { //} func determineDispatchErr(res []byte) error { - //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) + // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - //fmt.Println("Result") - //fmt.Println(result) - //var v []byte + // Am I unmarshalling the right thing here? Make sure should be res[1] err := scale.Unmarshal(res[1:], &vdt) if err != nil { - // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure - // TODO Create stucts for errors and integrate into Varying data type + // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } - - //fmt.Println("Unmarsalled dispatchErr Type:") - //fmt.Println(vdt.Value()) - + // Might have to change testing to adjust to new types + // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ case UnknownError: // For some reason its not entering here, going to customModuleError instead - // Maybe cuz struct is wrong + // Maybe cuz struct is wrong? fmt.Println("Val:") fmt.Println(val) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val.Err)} + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case FailedLookup: + // Add testing for this case, make sure struct is correct + fmt.Println("failed lookup") return &DispatchOutcomeError{"failed lookup"} case BadOrigin: + // Add testing for this case, make sure struct is correct + fmt.Println("bad origin") return &DispatchOutcomeError{"bad origin"} case CustomModuleError: // Printing nice, not getting the correct values + fmt.Println("Val2:") + fmt.Println(val) return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} } - //// if custon error module - //if vdt.Value().Index() == 4 { - // err = vdt.Set(CustomModuleError{ - // Err: DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))}, - // }) - // if err != nil { - // panic(err) - // } - //} - //fmt.Println("Unmarsalled dispatchErr Value2:") - //fmt.Println(vdt.Value()) return errInvalidResult } @@ -211,7 +200,7 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - Err string + err string } func (err UnknownError) Index() uint { @@ -219,7 +208,7 @@ func (err UnknownError) Index() uint { } type FailedLookup struct { - Err string + err string } func (err FailedLookup) Index() uint { @@ -227,7 +216,7 @@ func (err FailedLookup) Index() uint { } type BadOrigin struct { - Err string + err string } func (err BadOrigin) Index() uint { @@ -251,18 +240,6 @@ func (err CustomModuleError) String() string { func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) - //err := result.Set(scale.OK, nil) - //if err != nil { - // panic(err) - //} - - //// This code chunk works - //v := FailedLookup{Err: DispatchOutcomeError{"failed lookup"}} - //fmt.Println(v.Err) - // - //fmt.Println("Result") - //fmt.Println(result) switch res[1] { case 0: return nil From 71d2b5e90a6e9dae28c87ce9d6cd982dc24e6282 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 16:35:55 -0600 Subject: [PATCH 108/245] Comments on issues to fix in for babe error handling --- lib/babe/errors.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 92515d0bb2..feb7c8e039 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -123,7 +123,13 @@ func determineCustomModuleErr(res []byte) error { // return errInvalidResult //} +/* + Two main issues I need to fix: + 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling + 2) CustomModuleError data isnt being decoded. The type is but the struct is empty + */ func determineDispatchErr(res []byte) error { + // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) From 1c74606c53cb375e84329907726e79ac607980ac Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 09:22:10 -0600 Subject: [PATCH 109/245] WIP/refactor babe error handling with new scale pkg --- lib/babe/errors.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index feb7c8e039..26b9851b79 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -131,7 +131,8 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError - vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) + //var e := &UnknownError + vdt := scale.MustNewVaryingDataType(&UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) // Am I unmarshalling the right thing here? Make sure should be res[1] err := scale.Unmarshal(res[1:], &vdt) @@ -143,12 +144,12 @@ func determineDispatchErr(res []byte) error { // Might have to change testing to adjust to new types // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case UnknownError: + case *UnknownError: // For some reason its not entering here, going to customModuleError instead // Maybe cuz struct is wrong? fmt.Println("Val:") fmt.Println(val) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %p", val)} case FailedLookup: // Add testing for this case, make sure struct is correct fmt.Println("failed lookup") @@ -206,9 +207,11 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - err string + err *string } +//type UnknownError *string + func (err UnknownError) Index() uint { return 1 } @@ -232,7 +235,7 @@ func (err BadOrigin) Index() uint { type CustomModuleError struct { index uint8 `scale:"3"` err uint8 `scale:"2"` - message string `scale:"1"` // might need to be *string + message string `scale:"1"` // might need to be *string or an option } func (err CustomModuleError) Index() uint { From 694a8f44e4a8c9a8108e135e2e008cb50f592fdc Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:25:18 -0600 Subject: [PATCH 110/245] resolve type issue --- lib/babe/errors.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 26b9851b79..572b4d523c 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -129,27 +129,31 @@ func determineCustomModuleErr(res []byte) error { 2) CustomModuleError data isnt being decoded. The type is but the struct is empty */ func determineDispatchErr(res []byte) error { + fmt.Println("In the method") // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError //var e := &UnknownError - vdt := scale.MustNewVaryingDataType(&UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - + vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) + fmt.Println("Past vdt") // Am I unmarshalling the right thing here? Make sure should be res[1] - err := scale.Unmarshal(res[1:], &vdt) + err := scale.Unmarshal(res, &vdt) if err != nil { // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } + fmt.Println("Vdt") + fmt.Println(vdt.Value()) + // Might have to change testing to adjust to new types // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case *UnknownError: + case UnknownError: // For some reason its not entering here, going to customModuleError instead // Maybe cuz struct is wrong? fmt.Println("Val:") fmt.Println(val) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %p", val)} + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case FailedLookup: // Add testing for this case, make sure struct is correct fmt.Println("failed lookup") @@ -163,6 +167,8 @@ func determineDispatchErr(res []byte) error { fmt.Println("Val2:") fmt.Println(val) return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} + default: // Remove this before PR lol + fmt.Println("Something is most definitly afoot at the circle K") } return errInvalidResult @@ -207,13 +213,13 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - err *string + err string } //type UnknownError *string func (err UnknownError) Index() uint { - return 1 + return 0 } type FailedLookup struct { @@ -221,7 +227,7 @@ type FailedLookup struct { } func (err FailedLookup) Index() uint { - return 2 + return 1 } type BadOrigin struct { @@ -229,7 +235,7 @@ type BadOrigin struct { } func (err BadOrigin) Index() uint { - return 3 + return 2 } type CustomModuleError struct { @@ -239,7 +245,7 @@ type CustomModuleError struct { } func (err CustomModuleError) Index() uint { - return 4 + return 3 } func (err CustomModuleError) String() string { From f015dbf73fbfd472ce19abd5c371e769b542143b Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:36:34 -0600 Subject: [PATCH 111/245] fixed unknown error data populating issue --- lib/babe/errors.go | 22 ++++++++-------------- lib/babe/errors_test.go | 1 + 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 572b4d523c..17a52a0dea 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -125,16 +125,14 @@ func determineCustomModuleErr(res []byte) error { /* Two main issues I need to fix: - 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling + 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling - fixed 2) CustomModuleError data isnt being decoded. The type is but the struct is empty */ func determineDispatchErr(res []byte) error { - fmt.Println("In the method") // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError - //var e := &UnknownError - vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - fmt.Println("Past vdt") + var e UnknownError + vdt := scale.MustNewVaryingDataType(e, FailedLookup{}, BadOrigin{}, CustomModuleError{}) // Am I unmarshalling the right thing here? Make sure should be res[1] err := scale.Unmarshal(res, &vdt) if err != nil { @@ -148,11 +146,7 @@ func determineDispatchErr(res []byte) error { // Might have to change testing to adjust to new types // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case UnknownError: - // For some reason its not entering here, going to customModuleError instead - // Maybe cuz struct is wrong? - fmt.Println("Val:") - fmt.Println(val) + case UnknownError: // Got it! return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case FailedLookup: // Add testing for this case, make sure struct is correct @@ -212,11 +206,11 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } -type UnknownError struct { - err string -} +//type UnknownError struct { +// err string +//} -//type UnknownError *string +type UnknownError string func (err UnknownError) Index() uint { return 0 diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 8ec2923c6d..a318f19032 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -61,6 +61,7 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } + t.Log(err.Error()) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) From 245c5b1a522baf12bc5052f8acc9243f7819511c Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:56:37 -0600 Subject: [PATCH 112/245] WIP/Get module data to be populated when unmarshalling --- lib/babe/errors.go | 12 ++++-------- lib/babe/errors_test.go | 1 - 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 17a52a0dea..93d4f30c1c 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -206,10 +206,6 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } -//type UnknownError struct { -// err string -//} - type UnknownError string func (err UnknownError) Index() uint { @@ -233,9 +229,9 @@ func (err BadOrigin) Index() uint { } type CustomModuleError struct { - index uint8 `scale:"3"` - err uint8 `scale:"2"` - message string `scale:"1"` // might need to be *string or an option + index uint8 `scale:"3"` + err uint8 `scale:"2"` + message *string `scale:"1"` // Does scale need to consider that rust has multiple string types? } func (err CustomModuleError) Index() uint { @@ -243,7 +239,7 @@ func (err CustomModuleError) Index() uint { } func (err CustomModuleError) String() string { - return fmt.Sprintf("index: %d code: %d message: %s", err.index, err.err, err.message) + return fmt.Sprintf("index: %d code: %d message: %p", err.index, err.err, err.message) } func determineErr(res []byte) error { diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index a318f19032..8ec2923c6d 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -61,7 +61,6 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } - t.Log(err.Error()) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) From 8426b31ab500bf5e6b2327c017373db04eaefcdd Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 16:31:08 -0600 Subject: [PATCH 113/245] resolved data population issue --- lib/babe/errors.go | 60 +++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 93d4f30c1c..3076232b2c 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -124,45 +124,34 @@ func determineCustomModuleErr(res []byte) error { //} /* - Two main issues I need to fix: - 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling - fixed - 2) CustomModuleError data isnt being decoded. The type is but the struct is empty + TODO: + 1) Add test cases for 2 middle cases + 2) Expand on this to include other error types + 3) Rebase + 4) Clean up code + 5) Make sure everything I do is okay (errors returned and printing as hex instead of string). This could be included in a pr + 6) PR??? */ -func determineDispatchErr(res []byte) error { - // Maybe I need to do something with this first status byte? unsure of what tho - // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError - var e UnknownError - vdt := scale.MustNewVaryingDataType(e, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - // Am I unmarshalling the right thing here? Make sure should be res[1] +func determineDispatchErr(res []byte) error { // This works yay! + var e Other + vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) if err != nil { // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } - fmt.Println("Vdt") - fmt.Println(vdt.Value()) - - // Might have to change testing to adjust to new types - // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case UnknownError: // Got it! + case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} - case FailedLookup: + case CannotLookup: // Add testing for this case, make sure struct is correct - fmt.Println("failed lookup") return &DispatchOutcomeError{"failed lookup"} case BadOrigin: // Add testing for this case, make sure struct is correct - fmt.Println("bad origin") return &DispatchOutcomeError{"bad origin"} - case CustomModuleError: - // Printing nice, not getting the correct values - fmt.Println("Val2:") - fmt.Println(val) + case Module: return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} - default: // Remove this before PR lol - fmt.Println("Something is most definitly afoot at the circle K") } return errInvalidResult @@ -206,17 +195,17 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } -type UnknownError string +type Other string -func (err UnknownError) Index() uint { +func (err Other) Index() uint { return 0 } -type FailedLookup struct { +type CannotLookup struct { err string } -func (err FailedLookup) Index() uint { +func (err CannotLookup) Index() uint { return 1 } @@ -228,18 +217,19 @@ func (err BadOrigin) Index() uint { return 2 } -type CustomModuleError struct { - index uint8 `scale:"3"` - err uint8 `scale:"2"` - message *string `scale:"1"` // Does scale need to consider that rust has multiple string types? +type Module struct { // add in `scale:"1"` after + Idx uint8 + Err uint8 + Message *string } -func (err CustomModuleError) Index() uint { +func (err Module) Index() uint { return 3 } -func (err CustomModuleError) String() string { - return fmt.Sprintf("index: %d code: %d message: %p", err.index, err.err, err.message) +func (err Module) String() string { + // Make sure this is okay as opposed to being a string. Should be fine since its wrapped in the error + return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } func determineErr(res []byte) error { From 8240997509adf157b56cffdcbbe090fa3fd45155 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 10:58:50 -0600 Subject: [PATCH 114/245] WIP/Finish babe error handling refactor --- lib/babe/errors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 3076232b2c..3f91705d24 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -141,7 +141,7 @@ func determineDispatchErr(res []byte) error { // This works yay! return errInvalidResult } - switch val := vdt.Value().(type){ + switch val := vdt.Value().(type) { case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case CannotLookup: From 43039088d676eb5f147c36a78f175626c368029d Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 12:48:57 -0600 Subject: [PATCH 115/245] rebase with development --- pkg/scale/decode.go | 104 +------------------------------------------- 1 file changed, 1 insertion(+), 103 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index e551c6a9d1..15e96a9e88 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -230,91 +230,6 @@ func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { return } -func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { - in := dstv.Interface() - inType := reflect.TypeOf(in) - var temp reflect.Value - switch inType.Kind() { - case reflect.Bool: - temp = reflect.New(reflect.TypeOf(false)) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int: - temp = reflect.New(reflect.TypeOf(int(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int8: - temp = reflect.New(reflect.TypeOf(int8(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int16: - temp = reflect.New(reflect.TypeOf(int16(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int32: - temp = reflect.New(reflect.TypeOf(int32(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int64: - temp = reflect.New(reflect.TypeOf(int64(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.String: - temp = reflect.New(reflect.TypeOf("")) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint: - temp = reflect.New(reflect.TypeOf(uint(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint8: - temp = reflect.New(reflect.TypeOf(uint8(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint16: - temp = reflect.New(reflect.TypeOf(uint16(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint32: - temp = reflect.New(reflect.TypeOf(uint32(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint64: - temp = reflect.New(reflect.TypeOf(uint64(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - default: - err = fmt.Errorf("unsupported type for custom primitive: %T", in) - return - } - dstv.Set(temp.Elem().Convert(inType)) - return -} - func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { res := dstv.Interface().(Result) var rb byte @@ -731,21 +646,4 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { } dstv.Set(reflect.ValueOf(ui128)) return -} - -// decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 -// if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise -// it returns nil and error -func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { - buf := make([]byte, 16) - err = binary.Read(ds, binary.LittleEndian, buf) - if err != nil { - return - } - ui128, err := NewUint128(buf) - if err != nil { - return - } - dstv.Set(reflect.ValueOf(ui128)) - return -} +} \ No newline at end of file From 54067f9872d152ba82d40da3b45ee8cb6e23becf Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 12:58:43 -0600 Subject: [PATCH 116/245] final merge changes --- go.sum | 150 --------------------------------------------- lib/babe/build.go | 3 - lib/babe/errors.go | 4 -- 3 files changed, 157 deletions(-) diff --git a/go.sum b/go.sum index b803d10adc..2165343fb3 100644 --- a/go.sum +++ b/go.sum @@ -182,62 +182,44 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -<<<<<<< HEAD github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= -======= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= -github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= ->>>>>>> a0d51c70c1072b37d7d0ff6139f7232c8403811d github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= github.com/google/gopacket v1.1.18/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= -github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/uuid v1.1.5 h1:kxhtnfFVi+rYdOALN0B3k9UT86zVJKfBimRaciULW4I= github.com/google/uuid v1.1.5/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY= github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/gorilla/rpc v1.2.0 h1:WvvdC2lNeT1SP32zrIce5l0ECBfbAlmrmSBsuc57wfk= github.com/gorilla/rpc v1.2.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw= github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= -github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is= github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= -github.com/gtank/ristretto255 v0.1.2 h1:JEqUCPA1NvLq5DwYtuzigd7ss8fwbYay9fi4/5uMzcc= github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIvY4OmlYW69o= github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU= github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48= -github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI= github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= -github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs= github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao= github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc= -github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031 h1:HarGZ5h9HD9LgEg1yRVMXyfiw4wlXiLiYM2oMjeA/SE= github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031/go.mod h1:nNs7wvRfN1eKaMknBydLNQU6146XQim8t4h+q90biWo= github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= @@ -247,7 +229,6 @@ github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUP github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj6+M= github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-cid v0.0.6/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= -github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY= github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.1.0/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= @@ -255,62 +236,46 @@ github.com/ipfs/go-datastore v0.1.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRV github.com/ipfs/go-datastore v0.4.0/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= github.com/ipfs/go-datastore v0.4.1/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= github.com/ipfs/go-datastore v0.4.4/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= -github.com/ipfs/go-datastore v0.4.5 h1:cwOUcGMLdLPWgu3SlrCckCMznaGADbPqE0r8h768/Dg= github.com/ipfs/go-datastore v0.4.5/go.mod h1:eXTcaaiN6uOlVCLS9GjJUJtlvJfM3xk23w3fyfrmmJs= -github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk= github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-ds-badger v0.0.2/go.mod h1:Y3QpeSFWQf6MopLTiZD+VT6IC1yZqaGmjvRcKeSGij8= github.com/ipfs/go-ds-badger v0.0.5/go.mod h1:g5AuuCGmr7efyzQhLL8MzwqcauPojGPUaHzfGTzuE3s= github.com/ipfs/go-ds-badger v0.0.7/go.mod h1:qt0/fWzZDoPW6jpQeqUjR5kBfhDNB65jd9YlmAvpQBk= github.com/ipfs/go-ds-badger v0.2.1/go.mod h1:Tx7l3aTph3FMFrRS838dcSJh+jjA7cX9DrGVwx/NOwE= -github.com/ipfs/go-ds-badger v0.2.3 h1:J27YvAcpuA5IvZUbeBxOcQgqnYHUPxoygc6QxxkodZ4= github.com/ipfs/go-ds-badger v0.2.3/go.mod h1:pEYw0rgg3FIrywKKnL+Snr+w/LjJZVMTBRn4FS6UHUk= -github.com/ipfs/go-ds-badger2 v0.1.0 h1:784py6lXkwlVF+K6XSuqmdMgy5l8GI6k60ngBokb9Fg= github.com/ipfs/go-ds-badger2 v0.1.0/go.mod h1:pbR1p817OZbdId9EvLOhKBgUVTM3BMCSTan78lDDVaw= github.com/ipfs/go-ds-leveldb v0.0.1/go.mod h1:feO8V3kubwsEF22n0YRQCffeb79OOYIykR4L04tMOYc= github.com/ipfs/go-ds-leveldb v0.1.0/go.mod h1:hqAW8y4bwX5LWcCtku2rFNX3vjDZCy5LZCg+cSZvYb8= github.com/ipfs/go-ds-leveldb v0.4.1/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s= -github.com/ipfs/go-ds-leveldb v0.4.2 h1:QmQoAJ9WkPMUfBLnu1sBVy0xWWlJPg0m4kRAiJL9iaw= github.com/ipfs/go-ds-leveldb v0.4.2/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s= github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw= github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc= -github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8= github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ= -github.com/ipfs/go-ipns v0.0.2 h1:oq4ErrV4hNQ2Eim257RTYRgfOSV/s8BDaf9iIl4NwFs= github.com/ipfs/go-ipns v0.0.2/go.mod h1:WChil4e0/m9cIINWLxZe1Jtf77oz5L05rO2ei/uKJ5U= github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM= github.com/ipfs/go-log v1.0.2/go.mod h1:1MNjMxe0u6xvJZgeqbJ8vdo2TKaGwZ1a0Bpza+sr2Sk= github.com/ipfs/go-log v1.0.3/go.mod h1:OsLySYkwIbiSUR/yBTdv1qPtcE4FW3WPWk/ewz9Ru+A= -github.com/ipfs/go-log v1.0.4 h1:6nLQdX4W8P9yZZFH7mO+X/PzjN8Laozm/lMJ6esdgzY= github.com/ipfs/go-log v1.0.4/go.mod h1:oDCg2FkjogeFOhqqb+N39l2RpTNPL6F/StPkB3kPgcs= github.com/ipfs/go-log/v2 v2.0.2/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= github.com/ipfs/go-log/v2 v2.0.3/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= github.com/ipfs/go-log/v2 v2.0.5/go.mod h1:eZs4Xt4ZUJQFM3DlanGhy7TkwwawCZcSByscwkWG+dw= github.com/ipfs/go-log/v2 v2.1.1/go.mod h1:2v2nsGfZsvvAJz13SyFzf9ObaqwHiHxsPLEHntrv9KM= -github.com/ipfs/go-log/v2 v2.1.3 h1:1iS3IU7aXRlbgUpN8yTTpJ53NXYjAe37vcI5+5nYrzk= github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g= github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA= github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= -github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jbenet/go-cienv v0.0.0-20150120210510-1bb1476777ec/go.mod h1:rGaEvXB4uRSZMmzKNLoXvTu1sfx+1kv/DojUlPrSZGs= -github.com/jbenet/go-cienv v0.1.0 h1:Vc/s0QbQtoxX8MwwSLWWh+xNNZvM3Lw7NsTcHrvvhMc= github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA= github.com/jbenet/go-temp-err-catcher v0.0.0-20150120210811-aac704a3f4f2/go.mod h1:8GXXJV31xl8whumTzdZsTt3RnUIiPqzkyf7mxToRCMs= -github.com/jbenet/go-temp-err-catcher v0.1.0 h1:zpb3ZH6wIE8Shj2sKS+khgRvf7T7RABoLk/+KKHggpk= github.com/jbenet/go-temp-err-catcher v0.1.0/go.mod h1:0kJRvmDZXNMIiJirNPEYfhpPwbGVtZVWC34vc5WLsDk= github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY= github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= -github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o= github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= -github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 h1:EFT6MH3igZK/dIVqgGbTqWVvkZ7wJ5iGN03SVtvvdd8= github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25/go.mod h1:sWkGw/wsaHtRsT9zGQ/WyJCotGWG/Anow/9hsAcBWRw= github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= -github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= -github.com/jpillora/ipfilter v1.2.2 h1:lfENG7V1/T+ZutAtSbt6gssvzj3Ql0JmcFlqS/BES2E= github.com/jpillora/ipfilter v1.2.2/go.mod h1:xvAYjA+48eM9E5+sg9yI55N5lE9sefckjsnDvSiEA+g= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= @@ -322,62 +287,46 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d h1:68u9r4wEvL3gYg2jvAOgROwZ3H+Y3hIDk4tbbmIjcYQ= github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= -github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/libp2p/go-addr-util v0.0.1/go.mod h1:4ac6O7n9rIAKB1dnd+s8IbbMXkt+oBpzX4/+RACcnlQ= -github.com/libp2p/go-addr-util v0.0.2 h1:7cWK5cdA5x72jX0g8iLrQWm5TRJZ6CzGdPEhWj7plWU= github.com/libp2p/go-addr-util v0.0.2/go.mod h1:Ecd6Fb3yIuLzq4bD7VcywcVSBtefcAwnUISBM3WG15E= github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ= -github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs= github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM= -github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38yPW7c= github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-conn-security-multistream v0.1.0/go.mod h1:aw6eD7LOsHEX7+2hJkDxw1MteijaVcI+/eP2/x3J1xc= github.com/libp2p/go-conn-security-multistream v0.2.0/go.mod h1:hZN4MjlNetKD3Rq5Jb/P5ohUnFLNzEAR4DLSzpn2QLU= -github.com/libp2p/go-conn-security-multistream v0.2.1 h1:ft6/POSK7F+vl/2qzegnHDaXFU0iWB4yVTYrioC6Zy0= github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70= github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UGQf8A2eEmx4= -github.com/libp2p/go-eventbus v0.2.1 h1:VanAdErQnpTioN2TowqNcOijf6YwhuODe4pPKSDpxGc= github.com/libp2p/go-eventbus v0.2.1/go.mod h1:jc2S4SoEVPP48H9Wpzm5aiGwUCBMfGhVhhBjyhhCJs8= github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8= github.com/libp2p/go-flow-metrics v0.0.2/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= -github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM= github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= github.com/libp2p/go-libp2p v0.6.1/go.mod h1:CTFnWXogryAHjXAKEbOf1OWY+VeAP3lDMZkfEI5sT54= github.com/libp2p/go-libp2p v0.7.0/go.mod h1:hZJf8txWeCduQRDC/WSqBGMxaTHCOYHt2xSU1ivxn0k= github.com/libp2p/go-libp2p v0.7.4/go.mod h1:oXsBlTLF1q7pxr+9w6lqzS1ILpyHsaBPniVO7zIHGMw= github.com/libp2p/go-libp2p v0.8.1/go.mod h1:QRNH9pwdbEBpx5DTJYg+qxcVaDMAz3Ee/qDKwXujH5o= github.com/libp2p/go-libp2p v0.12.0/go.mod h1:FpHZrfC1q7nA8jitvdjKBDF31hguaC676g/nT9PgQM0= -github.com/libp2p/go-libp2p v0.14.2 h1:qs0ABtjjNjS+RIXT1uM7sMJEvIc0pq2nKR0VQxFXhHI= github.com/libp2p/go-libp2p v0.14.2/go.mod h1:0PQMADQEjCM2l8cSMYDpTgsb8gr6Zq7i4LUgq1mlW2E= -github.com/libp2p/go-libp2p-asn-util v0.0.0-20200825225859-85005c6cf052 h1:BM7aaOF7RpmNn9+9g6uTjGJ0cTzWr5j9i9IKeun2M8U= github.com/libp2p/go-libp2p-asn-util v0.0.0-20200825225859-85005c6cf052/go.mod h1:nRMRTab+kZuk0LnKZpxhOVH/ndsdr2Nr//Zltc/vwgo= github.com/libp2p/go-libp2p-autonat v0.1.1/go.mod h1:OXqkeGOY2xJVWKAGV2inNF5aKN/djNA3fdpCWloIudE= github.com/libp2p/go-libp2p-autonat v0.2.0/go.mod h1:DX+9teU4pEEoZUqR1PiMlqliONQdNbfzE1C718tcViI= github.com/libp2p/go-libp2p-autonat v0.2.1/go.mod h1:MWtAhV5Ko1l6QBsHQNSuM6b1sRkXrpk0/LqCr+vCVxI= github.com/libp2p/go-libp2p-autonat v0.2.2/go.mod h1:HsM62HkqZmHR2k1xgX34WuWDzk/nBwNHoeyyT4IWV6A= github.com/libp2p/go-libp2p-autonat v0.4.0/go.mod h1:YxaJlpr81FhdOv3W3BTconZPfhaYivRdf53g+S2wobk= -github.com/libp2p/go-libp2p-autonat v0.4.2 h1:YMp7StMi2dof+baaxkbxaizXjY1RPvU71CXfxExzcUU= github.com/libp2p/go-libp2p-autonat v0.4.2/go.mod h1:YxaJlpr81FhdOv3W3BTconZPfhaYivRdf53g+S2wobk= github.com/libp2p/go-libp2p-blankhost v0.1.1/go.mod h1:pf2fvdLJPsC1FsVrNP3DUUvMzUts2dsLLBEpo1vW1ro= github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU= -github.com/libp2p/go-libp2p-blankhost v0.2.0 h1:3EsGAi0CBGcZ33GwRuXEYJLLPoVWyXJ1bcJzAJjINkk= github.com/libp2p/go-libp2p-blankhost v0.2.0/go.mod h1:eduNKXGTioTuQAUcZ5epXi9vMl+t4d8ugUBRQ4SqaNQ= github.com/libp2p/go-libp2p-circuit v0.1.4/go.mod h1:CY67BrEjKNDhdTk8UgBX1Y/H5c3xkAcs3gnksxY7osU= github.com/libp2p/go-libp2p-circuit v0.2.1/go.mod h1:BXPwYDN5A8z4OEY9sOfr2DUQMLQvKt/6oku45YUmjIo= -github.com/libp2p/go-libp2p-circuit v0.4.0 h1:eqQ3sEYkGTtybWgr6JLqJY6QLtPWRErvFjFDfAOO1wc= github.com/libp2p/go-libp2p-circuit v0.4.0/go.mod h1:t/ktoFIUzM6uLQ+o1G6NuBl2ANhBKN9Bc8jRIk31MoA= github.com/libp2p/go-libp2p-core v0.0.1/go.mod h1:g/VxnTZ/1ygHxH3dKok7Vno1VfpvGcGip57wjTU4fco= github.com/libp2p/go-libp2p-core v0.0.4/go.mod h1:jyuCQP356gzfCFtRKyvAbNkyeuxb7OlyhWZ3nls5d2I= @@ -401,16 +350,12 @@ github.com/libp2p/go-libp2p-core v0.7.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB github.com/libp2p/go-libp2p-core v0.8.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-core v0.8.1/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-core v0.8.2/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= -github.com/libp2p/go-libp2p-core v0.8.5 h1:aEgbIcPGsKy6zYcC+5AJivYFedhYa4sW7mIpWpUaLKw= github.com/libp2p/go-libp2p-core v0.8.5/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= github.com/libp2p/go-libp2p-discovery v0.3.0/go.mod h1:o03drFnz9BVAZdzC/QUQ+NeQOu38Fu7LJGEOK2gQltw= -github.com/libp2p/go-libp2p-discovery v0.5.0 h1:Qfl+e5+lfDgwdrXdu4YNCWyEo3fWuP+WgN9mN0iWviQ= github.com/libp2p/go-libp2p-discovery v0.5.0/go.mod h1:+srtPIU9gDaBNu//UHvcdliKBIcr4SfDcm0/PfPJLug= -github.com/libp2p/go-libp2p-kad-dht v0.11.1 h1:FsriVQhOUZpCotWIjyFSjEDNJmUzuMma/RyyTDZanwc= github.com/libp2p/go-libp2p-kad-dht v0.11.1/go.mod h1:5ojtR2acDPqh/jXf5orWy8YGb8bHQDS+qeDcoscL/PI= -github.com/libp2p/go-libp2p-kbucket v0.4.7 h1:spZAcgxifvFZHBD8tErvppbnNiKA5uokDu3CV7axu70= github.com/libp2p/go-libp2p-kbucket v0.4.7/go.mod h1:XyVo99AfQH0foSf176k4jY1xUJ2+jUJIZCSDm7r2YKk= github.com/libp2p/go-libp2p-loggables v0.1.0/go.mod h1:EyumB2Y6PrYjr55Q3/tiJ/o3xoDasoRYM7nOzEpoa90= github.com/libp2p/go-libp2p-mplex v0.2.0/go.mod h1:Ejl9IyjvXJ0T9iqUTE1jpYATQ9NM3g+OtR+EMMODbKo= @@ -419,15 +364,11 @@ github.com/libp2p/go-libp2p-mplex v0.2.2/go.mod h1:74S9eum0tVQdAfFiKxAyKzNdSuLqw github.com/libp2p/go-libp2p-mplex v0.2.3/go.mod h1:CK3p2+9qH9x+7ER/gWWDYJ3QW5ZxWDkm+dVvjfuG3ek= github.com/libp2p/go-libp2p-mplex v0.3.0/go.mod h1:l9QWxRbbb5/hQMECEb908GbS9Sm2UAR2KFZKUJEynEs= github.com/libp2p/go-libp2p-mplex v0.4.0/go.mod h1:yCyWJE2sc6TBTnFpjvLuEJgTSw/u+MamvzILKdX7asw= -github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc= github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g= github.com/libp2p/go-libp2p-nat v0.0.5/go.mod h1:1qubaE5bTZMJE+E/uu2URroMbzdubFz1ChgiN79yKPE= -github.com/libp2p/go-libp2p-nat v0.0.6 h1:wMWis3kYynCbHoyKLPBEMu4YRLltbm8Mk08HGSfvTkU= github.com/libp2p/go-libp2p-nat v0.0.6/go.mod h1:iV59LVhB3IkFvS6S6sauVTSOrNEANnINbI/fkaLimiw= -github.com/libp2p/go-libp2p-netutil v0.1.0 h1:zscYDNVEcGxyUpMd0JReUZTrpMfia8PmLKcKF72EAMQ= github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCThNdbQD54k3TqjpbFU= github.com/libp2p/go-libp2p-noise v0.1.1/go.mod h1:QDFLdKX7nluB7DEnlVPbz7xlLHdwHFA9HiohJRr3vwM= -github.com/libp2p/go-libp2p-noise v0.2.0 h1:wmk5nhB9a2w2RxMOyvsoKjizgJOEaJdfAakr0jN8gds= github.com/libp2p/go-libp2p-noise v0.2.0/go.mod h1:IEbYhBBzGyvdLBoxxULL/SGbJARhUeqlO8lVSREYu2Q= github.com/libp2p/go-libp2p-peer v0.2.0/go.mod h1:RCffaCvUyW2CJmG2gAWVqwePwW7JMgxjsHm7+J5kjWY= github.com/libp2p/go-libp2p-peerstore v0.1.0/go.mod h1:2CeHkQsr8svp4fZ+Oi9ykN1HBb6u0MOvdJ7YIsmcwtY= @@ -437,21 +378,15 @@ github.com/libp2p/go-libp2p-peerstore v0.2.0/go.mod h1:N2l3eVIeAitSg3Pi2ipSrJYnq github.com/libp2p/go-libp2p-peerstore v0.2.1/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRjwRLBr4TYKfNgrUkOPA= github.com/libp2p/go-libp2p-peerstore v0.2.2/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRjwRLBr4TYKfNgrUkOPA= github.com/libp2p/go-libp2p-peerstore v0.2.6/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= -github.com/libp2p/go-libp2p-peerstore v0.2.7 h1:83JoLxyR9OYTnNfB5vvFqvMUv/xDNa6NoPHnENhBsGw= github.com/libp2p/go-libp2p-peerstore v0.2.7/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= -github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= -github.com/libp2p/go-libp2p-quic-transport v0.10.0 h1:koDCbWD9CCHwcHZL3/WEvP2A+e/o5/W5L3QS/2SPMA0= github.com/libp2p/go-libp2p-quic-transport v0.10.0/go.mod h1:RfJbZ8IqXIhxBRm5hqUEJqjiiY8xmEuq3HUDS993MkA= github.com/libp2p/go-libp2p-record v0.1.2/go.mod h1:pal0eNcT5nqZaTV7UGhqeGqxFgGdsU/9W//C8dqjQDk= -github.com/libp2p/go-libp2p-record v0.1.3 h1:R27hoScIhQf/A8XJZ8lYpnqh9LatJ5YbHs28kCIfql0= github.com/libp2p/go-libp2p-record v0.1.3/go.mod h1:yNUff/adKIfPnYQXgp6FQmNu3gLJ6EMg7+/vv2+9pY4= -github.com/libp2p/go-libp2p-routing-helpers v0.2.3 h1:xY61alxJ6PurSi+MXbywZpelvuU4U4p/gPTxjqCqTzY= github.com/libp2p/go-libp2p-routing-helpers v0.2.3/go.mod h1:795bh+9YeoFl99rMASoiVgHdi5bjack0N1+AFAdbvBw= github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8= github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g= github.com/libp2p/go-libp2p-secio v0.2.1/go.mod h1:cWtZpILJqkqrSkiYcDBh5lA3wbT2Q+hz3rJQq3iftD8= -github.com/libp2p/go-libp2p-secio v0.2.2 h1:rLLPvShPQAcY6eNurKNZq3eZjPWfU9kXF2eI9jIYdrg= github.com/libp2p/go-libp2p-secio v0.2.2/go.mod h1:wP3bS+m5AUnFA+OFO7Er03uO1mncHG0uVwGrwvjYlNY= github.com/libp2p/go-libp2p-swarm v0.1.0/go.mod h1:wQVsCdjsuZoc730CgOvh5ox6K8evllckjebkdiY5ta4= github.com/libp2p/go-libp2p-swarm v0.2.2/go.mod h1:fvmtQ0T1nErXym1/aa1uJEyN7JzaTNyBcHImCxRpPKU= @@ -459,7 +394,6 @@ github.com/libp2p/go-libp2p-swarm v0.2.3/go.mod h1:P2VO/EpxRyDxtChXz/VPVXyTnszHv github.com/libp2p/go-libp2p-swarm v0.2.8/go.mod h1:JQKMGSth4SMqonruY0a8yjlPVIkb0mdNSwckW7OYziM= github.com/libp2p/go-libp2p-swarm v0.3.0/go.mod h1:hdv95GWCTmzkgeJpP+GK/9D9puJegb7H57B5hWQR5Kk= github.com/libp2p/go-libp2p-swarm v0.3.1/go.mod h1:hdv95GWCTmzkgeJpP+GK/9D9puJegb7H57B5hWQR5Kk= -github.com/libp2p/go-libp2p-swarm v0.5.0 h1:HIK0z3Eqoo8ugmN8YqWAhD2RORgR+3iNXYG4U2PFd1E= github.com/libp2p/go-libp2p-swarm v0.5.0/go.mod h1:sU9i6BoHE0Ve5SKz3y9WfKrh8dUat6JknzUehFx8xW4= github.com/libp2p/go-libp2p-testing v0.0.2/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= github.com/libp2p/go-libp2p-testing v0.0.3/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= @@ -468,14 +402,11 @@ github.com/libp2p/go-libp2p-testing v0.1.0/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eq github.com/libp2p/go-libp2p-testing v0.1.1/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0= github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod h1:Qy8sAncLKpwXtS2dSnDOP8ktexIAHKu+J+pnZOFZLTc= github.com/libp2p/go-libp2p-testing v0.3.0/go.mod h1:efZkql4UZ7OVsEfaxNHZPzIehtsBXMrXnCfJIgDti5g= -github.com/libp2p/go-libp2p-testing v0.4.0 h1:PrwHRi0IGqOwVQWR3xzgigSlhlLfxgfXgkHxr77EghQ= github.com/libp2p/go-libp2p-testing v0.4.0/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0= -github.com/libp2p/go-libp2p-tls v0.1.3 h1:twKMhMu44jQO+HgQK9X8NHO5HkeJu2QbhLzLJpa8oNM= github.com/libp2p/go-libp2p-tls v0.1.3/go.mod h1:wZfuewxOndz5RTnCAxFliGjvYSDA40sKitV4c50uI1M= github.com/libp2p/go-libp2p-transport-upgrader v0.1.1/go.mod h1:IEtA6or8JUbsV07qPW4r01GnTenLW4oi3lOPbUMGJJA= github.com/libp2p/go-libp2p-transport-upgrader v0.2.0/go.mod h1:mQcrHj4asu6ArfSoMuyojOdjx73Q47cYD7s5+gZOlns= github.com/libp2p/go-libp2p-transport-upgrader v0.3.0/go.mod h1:i+SKzbRnvXdVbU3D1dwydnTmKRPXiAR/fyvi1dXuL4o= -github.com/libp2p/go-libp2p-transport-upgrader v0.4.2 h1:4JsnbfJzgZeRS9AWN7B9dPqn/LY/HoQTlO9gtdJTIYM= github.com/libp2p/go-libp2p-transport-upgrader v0.4.2/go.mod h1:NR8ne1VwfreD5VIWIU62Agt/J18ekORFU/j1i2y8zvk= github.com/libp2p/go-libp2p-yamux v0.2.0/go.mod h1:Db2gU+XfLpm6E4rG5uGCFX6uXA8MEXOxFcRoXUODaK8= github.com/libp2p/go-libp2p-yamux v0.2.2/go.mod h1:lIohaR0pT6mOt0AZ0L2dFze9hds9Req3OfS+B+dv4qw= @@ -484,59 +415,46 @@ github.com/libp2p/go-libp2p-yamux v0.2.7/go.mod h1:X28ENrBMU/nm4I3Nx4sZ4dgjZ6VhL github.com/libp2p/go-libp2p-yamux v0.2.8/go.mod h1:/t6tDqeuZf0INZMTgd0WxIRbtK2EzI2h7HbFm9eAKI4= github.com/libp2p/go-libp2p-yamux v0.4.0/go.mod h1:+DWDjtFMzoAwYLVkNZftoucn7PelNoy5nm3tZ3/Zw30= github.com/libp2p/go-libp2p-yamux v0.5.0/go.mod h1:AyR8k5EzyM2QN9Bbdg6X1SkVVuqLwTGf0L4DFq9g6po= -github.com/libp2p/go-libp2p-yamux v0.5.4 h1:/UOPtT/6DHPtr3TtKXBHa6g0Le0szYuI33Xc/Xpd7fQ= github.com/libp2p/go-libp2p-yamux v0.5.4/go.mod h1:tfrXbyaTqqSU654GTvK3ocnSZL3BuHoeTSqhcel1wsE= github.com/libp2p/go-maddr-filter v0.0.4/go.mod h1:6eT12kSQMA9x2pvFQa+xesMKUBlj9VImZbj3B9FBH/Q= github.com/libp2p/go-maddr-filter v0.0.5/go.mod h1:Jk+36PMfIqCJhAnaASRH83bdAvfDRp/w6ENFaC9bG+M= -github.com/libp2p/go-maddr-filter v0.1.0 h1:4ACqZKw8AqiuJfwFGq1CYDFugfXTOos+qQ3DETkhtCE= github.com/libp2p/go-maddr-filter v0.1.0/go.mod h1:VzZhTXkMucEGGEOSKddrwGiOv0tUhgnKqNEmIAz/bPU= github.com/libp2p/go-mplex v0.0.3/go.mod h1:pK5yMLmOoBR1pNCqDlA2GQrdAVTMkqFalaTWe7l4Yd0= github.com/libp2p/go-mplex v0.1.0/go.mod h1:SXgmdki2kwCUlCCbfGLEgHjC4pFqhTp0ZoV6aiKgxDU= github.com/libp2p/go-mplex v0.1.1/go.mod h1:Xgz2RDCi3co0LeZfgjm4OgUF15+sVR8SRcu3SFXI1lk= github.com/libp2p/go-mplex v0.1.2/go.mod h1:Xgz2RDCi3co0LeZfgjm4OgUF15+sVR8SRcu3SFXI1lk= github.com/libp2p/go-mplex v0.2.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= -github.com/libp2p/go-mplex v0.3.0 h1:U1T+vmCYJaEoDJPV1aq31N56hS+lJgb397GsylNSgrU= github.com/libp2p/go-mplex v0.3.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= github.com/libp2p/go-msgio v0.0.2/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= -github.com/libp2p/go-msgio v0.0.6 h1:lQ7Uc0kS1wb1EfRxO2Eir/RJoHkHn7t6o+EiwsYIKJA= github.com/libp2p/go-msgio v0.0.6/go.mod h1:4ecVB6d9f4BDSL5fqvPiC4A3KivjWn+Venn/1ALLMWA= github.com/libp2p/go-nat v0.0.4/go.mod h1:Nmw50VAvKuk38jUBcmNh6p9lUJLoODbJRvYAa/+KSDo= -github.com/libp2p/go-nat v0.0.5 h1:qxnwkco8RLKqVh1NmjQ+tJ8p8khNLFxuElYG/TwqW4Q= github.com/libp2p/go-nat v0.0.5/go.mod h1:B7NxsVNPZmRLvMOwiEO1scOSyjA56zxYAGv1yQgRkEU= github.com/libp2p/go-netroute v0.1.2/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= github.com/libp2p/go-netroute v0.1.3/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= -github.com/libp2p/go-netroute v0.1.6 h1:ruPJStbYyXVYGQ81uzEDzuvbYRLKRrLvTYd33yomC38= github.com/libp2p/go-netroute v0.1.6/go.mod h1:AqhkMh0VuWmfgtxKPp3Oc1LdU5QSWS7wl0QLhSZqXxQ= github.com/libp2p/go-openssl v0.0.2/go.mod h1:v8Zw2ijCSWBQi8Pq5GAixw6DbFfa9u6VIYDXnvOXkc0= github.com/libp2p/go-openssl v0.0.3/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-openssl v0.0.4/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-openssl v0.0.5/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= -github.com/libp2p/go-openssl v0.0.7 h1:eCAzdLejcNVBzP/iZM9vqHnQm+XyCEbSSIheIPRGNsw= github.com/libp2p/go-openssl v0.0.7/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-reuseport v0.0.1/go.mod h1:jn6RmB1ufnQwl0Q1f+YxAj8isJgDCQzaaxIFYDhcYEA= -github.com/libp2p/go-reuseport v0.0.2 h1:XSG94b1FJfGA01BUrT82imejHQyTxO4jEWqheyCXYvU= github.com/libp2p/go-reuseport v0.0.2/go.mod h1:SPD+5RwGC7rcnzngoYC86GjPzjSywuQyMVAheVBD9nQ= github.com/libp2p/go-reuseport-transport v0.0.2/go.mod h1:YkbSDrvjUVDL6b8XqriyA20obEtsW9BLkuOUyQAOCbs= github.com/libp2p/go-reuseport-transport v0.0.3/go.mod h1:Spv+MPft1exxARzP2Sruj2Wb5JSyHNncjf1Oi2dEbzM= -github.com/libp2p/go-reuseport-transport v0.0.4 h1:OZGz0RB620QDGpv300n1zaOcKGGAoGVf8h9txtt/1uM= github.com/libp2p/go-reuseport-transport v0.0.4/go.mod h1:trPa7r/7TJK/d+0hdBLOCGvpQQVOU74OXbNCIMkufGw= github.com/libp2p/go-sockaddr v0.0.2/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= -github.com/libp2p/go-sockaddr v0.1.1 h1:yD80l2ZOdGksnOyHrhxDdTDFrf7Oy+v3FMVArIRgZxQ= github.com/libp2p/go-sockaddr v0.1.1/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= github.com/libp2p/go-stream-muxer v0.0.1/go.mod h1:bAo8x7YkSpadMTbtTaxGVHWUQsR/l5MEaHbKaliuT14= github.com/libp2p/go-stream-muxer-multistream v0.2.0/go.mod h1:j9eyPol/LLRqT+GPLSxvimPhNph4sfYfMoDPd7HkzIc= -github.com/libp2p/go-stream-muxer-multistream v0.3.0 h1:TqnSHPJEIqDEO7h1wZZ0p3DXdvDSiLHQidKKUGZtiOY= github.com/libp2p/go-stream-muxer-multistream v0.3.0/go.mod h1:yDh8abSIzmZtqtOt64gFJUXEryejzNb0lisTt+fAMJA= github.com/libp2p/go-tcp-transport v0.1.0/go.mod h1:oJ8I5VXryj493DEJ7OsBieu8fcg2nHGctwtInJVpipc= github.com/libp2p/go-tcp-transport v0.1.1/go.mod h1:3HzGvLbx6etZjnFlERyakbaYPdfjg2pWP97dFZworkY= github.com/libp2p/go-tcp-transport v0.2.0/go.mod h1:vX2U0CnWimU4h0SGSEsg++AzvBcroCGYw28kh94oLe0= -github.com/libp2p/go-tcp-transport v0.2.1 h1:ExZiVQV+h+qL16fzCWtd1HSzPsqWottJ8KXwWaVi8Ns= github.com/libp2p/go-tcp-transport v0.2.1/go.mod h1:zskiJ70MEfWz2MKxvFB/Pv+tPIB1PpPUrHIWQ8aFw7M= github.com/libp2p/go-ws-transport v0.2.0/go.mod h1:9BHJz/4Q5A9ludYWKoGCFC5gUElzlHoKzu0yY9p/klM= github.com/libp2p/go-ws-transport v0.3.0/go.mod h1:bpgTJmRZAvVHrgHybCVyqoBmyLQ1fiZuEaBYusP5zsk= github.com/libp2p/go-ws-transport v0.3.1/go.mod h1:bpgTJmRZAvVHrgHybCVyqoBmyLQ1fiZuEaBYusP5zsk= -github.com/libp2p/go-ws-transport v0.4.0 h1:9tvtQ9xbws6cA5LvqdE6Ne3vcmGB4f1z9SByggk4s0k= github.com/libp2p/go-ws-transport v0.4.0/go.mod h1:EcIEKqf/7GDjth6ksuS/6p7R49V4CBY6/E7R/iyhYUA= github.com/libp2p/go-yamux v1.2.2/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/libp2p/go-yamux v1.3.0/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= @@ -544,30 +462,23 @@ github.com/libp2p/go-yamux v1.3.3/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZ github.com/libp2p/go-yamux v1.3.5/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/libp2p/go-yamux v1.3.7/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux v1.4.0/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= -github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI= github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= -github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= -github.com/lucas-clemente/quic-go v0.19.3 h1:eCDQqvGBB+kCTkA0XrAFtNe81FMa0/fn4QSoeAbmiF4= github.com/lucas-clemente/quic-go v0.19.3/go.mod h1:ADXpNbTQjq1hIzCpB+y/k5iz4n4z4IwqoLb94Kh5Hu8= github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/marten-seemann/qpack v0.2.1/go.mod h1:F7Gl5L1jIgN1D11ucXefiuJS9UMVP2opoCp2jDKb7wc= -github.com/marten-seemann/qtls v0.10.0 h1:ECsuYUKalRL240rRD4Ri33ISb7kAQ3qGDlrrl55b2pc= github.com/marten-seemann/qtls v0.10.0/go.mod h1:UvMd1oaYDACI99/oZUYLzMCkBXQVT0aGm99sJhbT8hs= -github.com/marten-seemann/qtls-go1-15 v0.1.1 h1:LIH6K34bPVttyXnUWixk0bzH6/N07VxbSabxn5A5gZQ= github.com/marten-seemann/qtls-go1-15 v0.1.1/go.mod h1:GyFwywLKkRt+6mfU99csTEY1joMZz5vmB1WNZH3P81I= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= @@ -575,17 +486,13 @@ github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00v github.com/miekg/dns v1.1.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.28/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= -github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY= github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= -github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 h1:hLDRPB66XQT/8+wG9WsDpiCvZf1yKO7sz7scAjSlBa0= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= -github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= -github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= @@ -595,11 +502,8 @@ github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVq github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= -github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= -github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI= github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= -github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4= github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM= github.com/multiformats/go-multiaddr v0.0.1/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= github.com/multiformats/go-multiaddr v0.0.2/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= @@ -610,15 +514,12 @@ github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y9 github.com/multiformats/go-multiaddr v0.2.1/go.mod h1:s/Apk6IyxfvMjDafnhJgJ3/46z7tZ04iMk5wP4QMGGE= github.com/multiformats/go-multiaddr v0.2.2/go.mod h1:NtfXiOtHvghW9KojvtySjH5y0u0xW5UouOmQQrn6a3Y= github.com/multiformats/go-multiaddr v0.3.0/go.mod h1:dF9kph9wfJ+3VLAaeBqo9Of8x4fJxp6ggJGteB8HQTI= -github.com/multiformats/go-multiaddr v0.3.1 h1:1bxa+W7j9wZKTZREySx1vPMs2TqrYWjVZ7zE6/XLG1I= github.com/multiformats/go-multiaddr v0.3.1/go.mod h1:uPbspcUPd5AfaP6ql3ujFY+QWzmBD8uLLL4bXW0XfGc= github.com/multiformats/go-multiaddr-dns v0.0.1/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.0.2/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.2.0/go.mod h1:TJ5pr5bBO7Y1B18djPuRsVkduhQH2YqYSbxWJzYGdK0= -github.com/multiformats/go-multiaddr-dns v0.3.1 h1:QgQgR+LQVt3NPTjbrLLpsaT2ufAA2y0Mkk+QRVJbW3A= github.com/multiformats/go-multiaddr-dns v0.3.1/go.mod h1:G/245BRQ6FJGmryJCrOuTdB37AMA5AMOVuO6NY3JwTk= github.com/multiformats/go-multiaddr-fmt v0.0.1/go.mod h1:aBYjqL4T/7j4Qx+R73XSv/8JsgnRFlf0w2KGLCmXl3Q= -github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E= github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo= github.com/multiformats/go-multiaddr-net v0.0.1/go.mod h1:nw6HSxNmCIQH27XPGBuX+d1tnvM7ihcFwHMSstNAVUU= github.com/multiformats/go-multiaddr-net v0.1.0/go.mod h1:5JNbcfBOP4dnhoZOv10JJVkJO0pCCEf8mTnipAo2UZQ= @@ -627,10 +528,8 @@ github.com/multiformats/go-multiaddr-net v0.1.2/go.mod h1:QsWt3XK/3hwvNxZJp92iMQ github.com/multiformats/go-multiaddr-net v0.1.3/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= github.com/multiformats/go-multiaddr-net v0.1.4/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= github.com/multiformats/go-multiaddr-net v0.1.5/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= -github.com/multiformats/go-multiaddr-net v0.2.0 h1:MSXRGN0mFymt6B1yo/6BPnIRpLPEnKgQNvVfCX5VDJk= github.com/multiformats/go-multiaddr-net v0.2.0/go.mod h1:gGdH3UXny6U3cKKYCvpXI5rnK7YaOIEOPVDI9tsJbEA= github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs= -github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk= github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc= github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po= @@ -638,60 +537,45 @@ github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa github.com/multiformats/go-multihash v0.0.9/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= github.com/multiformats/go-multihash v0.0.10/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= -github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I= github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= github.com/multiformats/go-multistream v0.2.0/go.mod h1:5GZPQZbkWOLOn3J2y4Y99vVW7vOfsAflxARk3x14o6k= github.com/multiformats/go-multistream v0.2.1/go.mod h1:5GZPQZbkWOLOn3J2y4Y99vVW7vOfsAflxARk3x14o6k= -github.com/multiformats/go-multistream v0.2.2 h1:TCYu1BHTDr1F/Qm75qwYISQdzGcRdC21nFgQW7l7GBo= github.com/multiformats/go-multistream v0.2.2/go.mod h1:UIcnm7Zuo8HKG+HkWgfQsGL+/MIEhyTqbODbIUwSXKs= github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= -github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY= github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= -github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975 h1:zm/Rb2OsnLWCY88Njoqgo4X6yt/lx3oBNWhepX0AOMU= github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975/go.mod h1:4Mct/lWCFf1jzQTTAaWtOI7sXqmG+wBeiBfT4CxoaJk= -github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks= github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= -github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 h1:shk/vn9oCoOTmwcouEdwIeOtOGA/ELRUw/GwvxwfT+0= github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= -github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA= -github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea h1:okKoivlkNRRLqXraEtatHfEhW+D71QTwkaj+4n4M2Xc= github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea/go.mod h1:3KEU5Dm8MAYWZqity880wOFJ9PhQjyKVZGwAEfc5Q4E= -github.com/phuslu/iploc v1.0.20200807 h1:LIBm2Y9l5zmUvnJhQgMcLZ0iVwuG+5/L6AgbMwSOpE4= github.com/phuslu/iploc v1.0.20200807/go.mod h1:Q/0VX0txvbxekt4NhWIi3Q3eyZ139lHhnlzvDxyXhuc= -github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo= github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= @@ -700,7 +584,6 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7q github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= -github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -733,10 +616,8 @@ github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qt github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE= github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA= github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0= -github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU= github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= -github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= @@ -746,41 +627,30 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/src-d/envconfig v1.0.0/go.mod h1:Q9YQZ7BKITldTBnoxsE5gOeB5y66RyPXeue/R4aaNBc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= -github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca h1:Ld/zXl5t4+D69SiV4JoN7kkfvJdOWlPpfxrzxpLMoUk= github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= -github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc= github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= -github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI= github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= -github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= -github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= -github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec h1:VElCeVyfCWNmCv6UisKQrr+P2/JRG0uf4/FIdCB4pL0= github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec/go.mod h1:VGyarTzasuS7k5KhSIGpM3tciSZlkP31Mp9VJTHMMeI= -github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= github.com/whyrusleeping/go-logging v0.0.1/go.mod h1:lDPYj54zutzG1XYfHAhcc7oNXEburHQBn+Iqd4yS4vE= github.com/whyrusleeping/mafmt v1.2.8/go.mod h1:faQJFPbLSxzD9xpA02ttW/tS9vZykNvXwGvqIpk20FA= -github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 h1:Y1/FEOpaCpD21WxrmfeIYCFPuVPRCY2XZTWzTNHGw30= github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4= -github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI= github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= @@ -792,23 +662,18 @@ go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M= go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= -go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/goleak v1.0.0 h1:qsup4IcBdlmsnGfqyLl4Ntn3C2XCCuKAE7DwHpScyUo= go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= -go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.14.1/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= -go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM= go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE= golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw= @@ -843,12 +708,10 @@ golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTk golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k= golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -888,7 +751,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180810173357-98c5dad5d1a0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -926,14 +788,12 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 h1:JWgyZ1qgdTaF3N3oxC+MdTV7qvEEgHo3otj+HB5CM7Q= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -956,12 +816,10 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20210106214847-113979e3529a h1:CB3a9Nez8M13wwlr/E2YtwoU+qYHKfC+JrDa45RXXoQ= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= @@ -971,7 +829,6 @@ google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -992,7 +849,6 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.28.1/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= -google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= @@ -1009,30 +865,24 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= -gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU= gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c= gopkg.in/src-d/go-cli.v0 v0.0.0-20181105080154-d492247bbc0d/go.mod h1:z+K8VcOYVYcSwSjGebuDL6176A1XskgbtNl64NSg+n8= gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/lib/babe/build.go b/lib/babe/build.go index 4d8c71724d..1972e265b0 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -31,14 +31,11 @@ import ( "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/transaction" "github.com/ChainSafe/gossamer/pkg/scale" -<<<<<<< HEAD ) const ( buildBlockTimer = "gossamer/proposer/block/constructed" buildBlockErrors = "gossamer/proposer/block/constructed/errors" -======= ->>>>>>> a0d51c70c1072b37d7d0ff6139f7232c8403811d ) // construct a block for this slot with the given parent diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 74cebc5b88..3f91705d24 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -141,11 +141,7 @@ func determineDispatchErr(res []byte) error { // This works yay! return errInvalidResult } -<<<<<<< HEAD switch val := vdt.Value().(type) { -======= - switch val := vdt.Value().(type){ ->>>>>>> a0d51c70c1072b37d7d0ff6139f7232c8403811d case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case CannotLookup: From a38f458f3952dec39d0eddb8a66751ed50ca17a4 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 13:10:17 -0600 Subject: [PATCH 117/245] added further testing for dispatch errors --- go.sum | 40 ++++++++++++++++++++++++++++++++++++++++ lib/babe/errors.go | 5 ++--- lib/babe/errors_test.go | 10 ++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/go.sum b/go.sum index 2165343fb3..1a7f5c763d 100644 --- a/go.sum +++ b/go.sum @@ -184,11 +184,13 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= github.com/google/gopacket v1.1.18/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= +github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= @@ -260,6 +262,7 @@ github.com/ipfs/go-log/v2 v2.0.2/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBW github.com/ipfs/go-log/v2 v2.0.3/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= github.com/ipfs/go-log/v2 v2.0.5/go.mod h1:eZs4Xt4ZUJQFM3DlanGhy7TkwwawCZcSByscwkWG+dw= github.com/ipfs/go-log/v2 v2.1.1/go.mod h1:2v2nsGfZsvvAJz13SyFzf9ObaqwHiHxsPLEHntrv9KM= +github.com/ipfs/go-log/v2 v2.1.3 h1:1iS3IU7aXRlbgUpN8yTTpJ53NXYjAe37vcI5+5nYrzk= github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g= github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA= github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= @@ -290,6 +293,7 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -303,6 +307,7 @@ github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoR github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-conn-security-multistream v0.1.0/go.mod h1:aw6eD7LOsHEX7+2hJkDxw1MteijaVcI+/eP2/x3J1xc= github.com/libp2p/go-conn-security-multistream v0.2.0/go.mod h1:hZN4MjlNetKD3Rq5Jb/P5ohUnFLNzEAR4DLSzpn2QLU= +github.com/libp2p/go-conn-security-multistream v0.2.1 h1:ft6/POSK7F+vl/2qzegnHDaXFU0iWB4yVTYrioC6Zy0= github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70= github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UGQf8A2eEmx4= github.com/libp2p/go-eventbus v0.2.1/go.mod h1:jc2S4SoEVPP48H9Wpzm5aiGwUCBMfGhVhhBjyhhCJs8= @@ -314,6 +319,7 @@ github.com/libp2p/go-libp2p v0.7.0/go.mod h1:hZJf8txWeCduQRDC/WSqBGMxaTHCOYHt2xS github.com/libp2p/go-libp2p v0.7.4/go.mod h1:oXsBlTLF1q7pxr+9w6lqzS1ILpyHsaBPniVO7zIHGMw= github.com/libp2p/go-libp2p v0.8.1/go.mod h1:QRNH9pwdbEBpx5DTJYg+qxcVaDMAz3Ee/qDKwXujH5o= github.com/libp2p/go-libp2p v0.12.0/go.mod h1:FpHZrfC1q7nA8jitvdjKBDF31hguaC676g/nT9PgQM0= +github.com/libp2p/go-libp2p v0.14.2 h1:qs0ABtjjNjS+RIXT1uM7sMJEvIc0pq2nKR0VQxFXhHI= github.com/libp2p/go-libp2p v0.14.2/go.mod h1:0PQMADQEjCM2l8cSMYDpTgsb8gr6Zq7i4LUgq1mlW2E= github.com/libp2p/go-libp2p-asn-util v0.0.0-20200825225859-85005c6cf052/go.mod h1:nRMRTab+kZuk0LnKZpxhOVH/ndsdr2Nr//Zltc/vwgo= github.com/libp2p/go-libp2p-autonat v0.1.1/go.mod h1:OXqkeGOY2xJVWKAGV2inNF5aKN/djNA3fdpCWloIudE= @@ -321,6 +327,7 @@ github.com/libp2p/go-libp2p-autonat v0.2.0/go.mod h1:DX+9teU4pEEoZUqR1PiMlqliONQ github.com/libp2p/go-libp2p-autonat v0.2.1/go.mod h1:MWtAhV5Ko1l6QBsHQNSuM6b1sRkXrpk0/LqCr+vCVxI= github.com/libp2p/go-libp2p-autonat v0.2.2/go.mod h1:HsM62HkqZmHR2k1xgX34WuWDzk/nBwNHoeyyT4IWV6A= github.com/libp2p/go-libp2p-autonat v0.4.0/go.mod h1:YxaJlpr81FhdOv3W3BTconZPfhaYivRdf53g+S2wobk= +github.com/libp2p/go-libp2p-autonat v0.4.2 h1:YMp7StMi2dof+baaxkbxaizXjY1RPvU71CXfxExzcUU= github.com/libp2p/go-libp2p-autonat v0.4.2/go.mod h1:YxaJlpr81FhdOv3W3BTconZPfhaYivRdf53g+S2wobk= github.com/libp2p/go-libp2p-blankhost v0.1.1/go.mod h1:pf2fvdLJPsC1FsVrNP3DUUvMzUts2dsLLBEpo1vW1ro= github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU= @@ -350,6 +357,7 @@ github.com/libp2p/go-libp2p-core v0.7.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB github.com/libp2p/go-libp2p-core v0.8.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-core v0.8.1/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-core v0.8.2/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= +github.com/libp2p/go-libp2p-core v0.8.5 h1:aEgbIcPGsKy6zYcC+5AJivYFedhYa4sW7mIpWpUaLKw= github.com/libp2p/go-libp2p-core v0.8.5/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -364,11 +372,13 @@ github.com/libp2p/go-libp2p-mplex v0.2.2/go.mod h1:74S9eum0tVQdAfFiKxAyKzNdSuLqw github.com/libp2p/go-libp2p-mplex v0.2.3/go.mod h1:CK3p2+9qH9x+7ER/gWWDYJ3QW5ZxWDkm+dVvjfuG3ek= github.com/libp2p/go-libp2p-mplex v0.3.0/go.mod h1:l9QWxRbbb5/hQMECEb908GbS9Sm2UAR2KFZKUJEynEs= github.com/libp2p/go-libp2p-mplex v0.4.0/go.mod h1:yCyWJE2sc6TBTnFpjvLuEJgTSw/u+MamvzILKdX7asw= +github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc= github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g= github.com/libp2p/go-libp2p-nat v0.0.5/go.mod h1:1qubaE5bTZMJE+E/uu2URroMbzdubFz1ChgiN79yKPE= github.com/libp2p/go-libp2p-nat v0.0.6/go.mod h1:iV59LVhB3IkFvS6S6sauVTSOrNEANnINbI/fkaLimiw= github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCThNdbQD54k3TqjpbFU= github.com/libp2p/go-libp2p-noise v0.1.1/go.mod h1:QDFLdKX7nluB7DEnlVPbz7xlLHdwHFA9HiohJRr3vwM= +github.com/libp2p/go-libp2p-noise v0.2.0 h1:wmk5nhB9a2w2RxMOyvsoKjizgJOEaJdfAakr0jN8gds= github.com/libp2p/go-libp2p-noise v0.2.0/go.mod h1:IEbYhBBzGyvdLBoxxULL/SGbJARhUeqlO8lVSREYu2Q= github.com/libp2p/go-libp2p-peer v0.2.0/go.mod h1:RCffaCvUyW2CJmG2gAWVqwePwW7JMgxjsHm7+J5kjWY= github.com/libp2p/go-libp2p-peerstore v0.1.0/go.mod h1:2CeHkQsr8svp4fZ+Oi9ykN1HBb6u0MOvdJ7YIsmcwtY= @@ -378,8 +388,10 @@ github.com/libp2p/go-libp2p-peerstore v0.2.0/go.mod h1:N2l3eVIeAitSg3Pi2ipSrJYnq github.com/libp2p/go-libp2p-peerstore v0.2.1/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRjwRLBr4TYKfNgrUkOPA= github.com/libp2p/go-libp2p-peerstore v0.2.2/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRjwRLBr4TYKfNgrUkOPA= github.com/libp2p/go-libp2p-peerstore v0.2.6/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= +github.com/libp2p/go-libp2p-peerstore v0.2.7 h1:83JoLxyR9OYTnNfB5vvFqvMUv/xDNa6NoPHnENhBsGw= github.com/libp2p/go-libp2p-peerstore v0.2.7/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= +github.com/libp2p/go-libp2p-quic-transport v0.10.0 h1:koDCbWD9CCHwcHZL3/WEvP2A+e/o5/W5L3QS/2SPMA0= github.com/libp2p/go-libp2p-quic-transport v0.10.0/go.mod h1:RfJbZ8IqXIhxBRm5hqUEJqjiiY8xmEuq3HUDS993MkA= github.com/libp2p/go-libp2p-record v0.1.2/go.mod h1:pal0eNcT5nqZaTV7UGhqeGqxFgGdsU/9W//C8dqjQDk= github.com/libp2p/go-libp2p-record v0.1.3/go.mod h1:yNUff/adKIfPnYQXgp6FQmNu3gLJ6EMg7+/vv2+9pY4= @@ -394,6 +406,7 @@ github.com/libp2p/go-libp2p-swarm v0.2.3/go.mod h1:P2VO/EpxRyDxtChXz/VPVXyTnszHv github.com/libp2p/go-libp2p-swarm v0.2.8/go.mod h1:JQKMGSth4SMqonruY0a8yjlPVIkb0mdNSwckW7OYziM= github.com/libp2p/go-libp2p-swarm v0.3.0/go.mod h1:hdv95GWCTmzkgeJpP+GK/9D9puJegb7H57B5hWQR5Kk= github.com/libp2p/go-libp2p-swarm v0.3.1/go.mod h1:hdv95GWCTmzkgeJpP+GK/9D9puJegb7H57B5hWQR5Kk= +github.com/libp2p/go-libp2p-swarm v0.5.0 h1:HIK0z3Eqoo8ugmN8YqWAhD2RORgR+3iNXYG4U2PFd1E= github.com/libp2p/go-libp2p-swarm v0.5.0/go.mod h1:sU9i6BoHE0Ve5SKz3y9WfKrh8dUat6JknzUehFx8xW4= github.com/libp2p/go-libp2p-testing v0.0.2/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= github.com/libp2p/go-libp2p-testing v0.0.3/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= @@ -402,11 +415,13 @@ github.com/libp2p/go-libp2p-testing v0.1.0/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eq github.com/libp2p/go-libp2p-testing v0.1.1/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0= github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod h1:Qy8sAncLKpwXtS2dSnDOP8ktexIAHKu+J+pnZOFZLTc= github.com/libp2p/go-libp2p-testing v0.3.0/go.mod h1:efZkql4UZ7OVsEfaxNHZPzIehtsBXMrXnCfJIgDti5g= +github.com/libp2p/go-libp2p-testing v0.4.0 h1:PrwHRi0IGqOwVQWR3xzgigSlhlLfxgfXgkHxr77EghQ= github.com/libp2p/go-libp2p-testing v0.4.0/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0= github.com/libp2p/go-libp2p-tls v0.1.3/go.mod h1:wZfuewxOndz5RTnCAxFliGjvYSDA40sKitV4c50uI1M= github.com/libp2p/go-libp2p-transport-upgrader v0.1.1/go.mod h1:IEtA6or8JUbsV07qPW4r01GnTenLW4oi3lOPbUMGJJA= github.com/libp2p/go-libp2p-transport-upgrader v0.2.0/go.mod h1:mQcrHj4asu6ArfSoMuyojOdjx73Q47cYD7s5+gZOlns= github.com/libp2p/go-libp2p-transport-upgrader v0.3.0/go.mod h1:i+SKzbRnvXdVbU3D1dwydnTmKRPXiAR/fyvi1dXuL4o= +github.com/libp2p/go-libp2p-transport-upgrader v0.4.2 h1:4JsnbfJzgZeRS9AWN7B9dPqn/LY/HoQTlO9gtdJTIYM= github.com/libp2p/go-libp2p-transport-upgrader v0.4.2/go.mod h1:NR8ne1VwfreD5VIWIU62Agt/J18ekORFU/j1i2y8zvk= github.com/libp2p/go-libp2p-yamux v0.2.0/go.mod h1:Db2gU+XfLpm6E4rG5uGCFX6uXA8MEXOxFcRoXUODaK8= github.com/libp2p/go-libp2p-yamux v0.2.2/go.mod h1:lIohaR0pT6mOt0AZ0L2dFze9hds9Req3OfS+B+dv4qw= @@ -415,15 +430,18 @@ github.com/libp2p/go-libp2p-yamux v0.2.7/go.mod h1:X28ENrBMU/nm4I3Nx4sZ4dgjZ6VhL github.com/libp2p/go-libp2p-yamux v0.2.8/go.mod h1:/t6tDqeuZf0INZMTgd0WxIRbtK2EzI2h7HbFm9eAKI4= github.com/libp2p/go-libp2p-yamux v0.4.0/go.mod h1:+DWDjtFMzoAwYLVkNZftoucn7PelNoy5nm3tZ3/Zw30= github.com/libp2p/go-libp2p-yamux v0.5.0/go.mod h1:AyR8k5EzyM2QN9Bbdg6X1SkVVuqLwTGf0L4DFq9g6po= +github.com/libp2p/go-libp2p-yamux v0.5.4 h1:/UOPtT/6DHPtr3TtKXBHa6g0Le0szYuI33Xc/Xpd7fQ= github.com/libp2p/go-libp2p-yamux v0.5.4/go.mod h1:tfrXbyaTqqSU654GTvK3ocnSZL3BuHoeTSqhcel1wsE= github.com/libp2p/go-maddr-filter v0.0.4/go.mod h1:6eT12kSQMA9x2pvFQa+xesMKUBlj9VImZbj3B9FBH/Q= github.com/libp2p/go-maddr-filter v0.0.5/go.mod h1:Jk+36PMfIqCJhAnaASRH83bdAvfDRp/w6ENFaC9bG+M= +github.com/libp2p/go-maddr-filter v0.1.0 h1:4ACqZKw8AqiuJfwFGq1CYDFugfXTOos+qQ3DETkhtCE= github.com/libp2p/go-maddr-filter v0.1.0/go.mod h1:VzZhTXkMucEGGEOSKddrwGiOv0tUhgnKqNEmIAz/bPU= github.com/libp2p/go-mplex v0.0.3/go.mod h1:pK5yMLmOoBR1pNCqDlA2GQrdAVTMkqFalaTWe7l4Yd0= github.com/libp2p/go-mplex v0.1.0/go.mod h1:SXgmdki2kwCUlCCbfGLEgHjC4pFqhTp0ZoV6aiKgxDU= github.com/libp2p/go-mplex v0.1.1/go.mod h1:Xgz2RDCi3co0LeZfgjm4OgUF15+sVR8SRcu3SFXI1lk= github.com/libp2p/go-mplex v0.1.2/go.mod h1:Xgz2RDCi3co0LeZfgjm4OgUF15+sVR8SRcu3SFXI1lk= github.com/libp2p/go-mplex v0.2.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= +github.com/libp2p/go-mplex v0.3.0 h1:U1T+vmCYJaEoDJPV1aq31N56hS+lJgb397GsylNSgrU= github.com/libp2p/go-mplex v0.3.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= github.com/libp2p/go-msgio v0.0.2/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= @@ -432,6 +450,7 @@ github.com/libp2p/go-nat v0.0.4/go.mod h1:Nmw50VAvKuk38jUBcmNh6p9lUJLoODbJRvYAa/ github.com/libp2p/go-nat v0.0.5/go.mod h1:B7NxsVNPZmRLvMOwiEO1scOSyjA56zxYAGv1yQgRkEU= github.com/libp2p/go-netroute v0.1.2/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= github.com/libp2p/go-netroute v0.1.3/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= +github.com/libp2p/go-netroute v0.1.6 h1:ruPJStbYyXVYGQ81uzEDzuvbYRLKRrLvTYd33yomC38= github.com/libp2p/go-netroute v0.1.6/go.mod h1:AqhkMh0VuWmfgtxKPp3Oc1LdU5QSWS7wl0QLhSZqXxQ= github.com/libp2p/go-openssl v0.0.2/go.mod h1:v8Zw2ijCSWBQi8Pq5GAixw6DbFfa9u6VIYDXnvOXkc0= github.com/libp2p/go-openssl v0.0.3/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= @@ -444,6 +463,7 @@ github.com/libp2p/go-reuseport-transport v0.0.2/go.mod h1:YkbSDrvjUVDL6b8XqriyA2 github.com/libp2p/go-reuseport-transport v0.0.3/go.mod h1:Spv+MPft1exxARzP2Sruj2Wb5JSyHNncjf1Oi2dEbzM= github.com/libp2p/go-reuseport-transport v0.0.4/go.mod h1:trPa7r/7TJK/d+0hdBLOCGvpQQVOU74OXbNCIMkufGw= github.com/libp2p/go-sockaddr v0.0.2/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= +github.com/libp2p/go-sockaddr v0.1.1 h1:yD80l2ZOdGksnOyHrhxDdTDFrf7Oy+v3FMVArIRgZxQ= github.com/libp2p/go-sockaddr v0.1.1/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= github.com/libp2p/go-stream-muxer v0.0.1/go.mod h1:bAo8x7YkSpadMTbtTaxGVHWUQsR/l5MEaHbKaliuT14= github.com/libp2p/go-stream-muxer-multistream v0.2.0/go.mod h1:j9eyPol/LLRqT+GPLSxvimPhNph4sfYfMoDPd7HkzIc= @@ -455,6 +475,7 @@ github.com/libp2p/go-tcp-transport v0.2.1/go.mod h1:zskiJ70MEfWz2MKxvFB/Pv+tPIB1 github.com/libp2p/go-ws-transport v0.2.0/go.mod h1:9BHJz/4Q5A9ludYWKoGCFC5gUElzlHoKzu0yY9p/klM= github.com/libp2p/go-ws-transport v0.3.0/go.mod h1:bpgTJmRZAvVHrgHybCVyqoBmyLQ1fiZuEaBYusP5zsk= github.com/libp2p/go-ws-transport v0.3.1/go.mod h1:bpgTJmRZAvVHrgHybCVyqoBmyLQ1fiZuEaBYusP5zsk= +github.com/libp2p/go-ws-transport v0.4.0 h1:9tvtQ9xbws6cA5LvqdE6Ne3vcmGB4f1z9SByggk4s0k= github.com/libp2p/go-ws-transport v0.4.0/go.mod h1:EcIEKqf/7GDjth6ksuS/6p7R49V4CBY6/E7R/iyhYUA= github.com/libp2p/go-yamux v1.2.2/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/libp2p/go-yamux v1.3.0/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= @@ -462,15 +483,20 @@ github.com/libp2p/go-yamux v1.3.3/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZ github.com/libp2p/go-yamux v1.3.5/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/libp2p/go-yamux v1.3.7/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux v1.4.0/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= +github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI= github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= +github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= +github.com/lucas-clemente/quic-go v0.19.3 h1:eCDQqvGBB+kCTkA0XrAFtNe81FMa0/fn4QSoeAbmiF4= github.com/lucas-clemente/quic-go v0.19.3/go.mod h1:ADXpNbTQjq1hIzCpB+y/k5iz4n4z4IwqoLb94Kh5Hu8= github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/marten-seemann/qpack v0.2.1/go.mod h1:F7Gl5L1jIgN1D11ucXefiuJS9UMVP2opoCp2jDKb7wc= +github.com/marten-seemann/qtls v0.10.0 h1:ECsuYUKalRL240rRD4Ri33ISb7kAQ3qGDlrrl55b2pc= github.com/marten-seemann/qtls v0.10.0/go.mod h1:UvMd1oaYDACI99/oZUYLzMCkBXQVT0aGm99sJhbT8hs= +github.com/marten-seemann/qtls-go1-15 v0.1.1 h1:LIH6K34bPVttyXnUWixk0bzH6/N07VxbSabxn5A5gZQ= github.com/marten-seemann/qtls-go1-15 v0.1.1/go.mod h1:GyFwywLKkRt+6mfU99csTEY1joMZz5vmB1WNZH3P81I= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= @@ -486,6 +512,7 @@ github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00v github.com/miekg/dns v1.1.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.28/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= +github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY= github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= @@ -518,6 +545,7 @@ github.com/multiformats/go-multiaddr v0.3.1/go.mod h1:uPbspcUPd5AfaP6ql3ujFY+QWz github.com/multiformats/go-multiaddr-dns v0.0.1/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.0.2/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.2.0/go.mod h1:TJ5pr5bBO7Y1B18djPuRsVkduhQH2YqYSbxWJzYGdK0= +github.com/multiformats/go-multiaddr-dns v0.3.1 h1:QgQgR+LQVt3NPTjbrLLpsaT2ufAA2y0Mkk+QRVJbW3A= github.com/multiformats/go-multiaddr-dns v0.3.1/go.mod h1:G/245BRQ6FJGmryJCrOuTdB37AMA5AMOVuO6NY3JwTk= github.com/multiformats/go-multiaddr-fmt v0.0.1/go.mod h1:aBYjqL4T/7j4Qx+R73XSv/8JsgnRFlf0w2KGLCmXl3Q= github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo= @@ -542,6 +570,7 @@ github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wS github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= github.com/multiformats/go-multistream v0.2.0/go.mod h1:5GZPQZbkWOLOn3J2y4Y99vVW7vOfsAflxARk3x14o6k= github.com/multiformats/go-multistream v0.2.1/go.mod h1:5GZPQZbkWOLOn3J2y4Y99vVW7vOfsAflxARk3x14o6k= +github.com/multiformats/go-multistream v0.2.2 h1:TCYu1BHTDr1F/Qm75qwYISQdzGcRdC21nFgQW7l7GBo= github.com/multiformats/go-multistream v0.2.2/go.mod h1:UIcnm7Zuo8HKG+HkWgfQsGL+/MIEhyTqbODbIUwSXKs= github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= @@ -638,6 +667,7 @@ github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpP github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= +github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI= github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= @@ -662,13 +692,16 @@ go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M= go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= +go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= @@ -708,6 +741,7 @@ golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTk golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k= golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= @@ -751,6 +785,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180810173357-98c5dad5d1a0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -788,12 +823,14 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 h1:JWgyZ1qgdTaF3N3oxC+MdTV7qvEEgHo3otj+HB5CM7Q= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -816,6 +853,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a h1:CB3a9Nez8M13wwlr/E2YtwoU+qYHKfC+JrDa45RXXoQ= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -849,6 +887,7 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.28.1/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= @@ -865,6 +904,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 3f91705d24..1194884c93 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -228,13 +228,12 @@ func (err Module) Index() uint { } func (err Module) String() string { - // Make sure this is okay as opposed to being a string. Should be fine since its wrapped in the error return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } func determineErr(res []byte) error { switch res[0] { - case 0: // DispatchOutcome + case 0: switch res[1] { case 0: return nil @@ -243,7 +242,7 @@ func determineErr(res []byte) error { default: return errInvalidResult } - case 1: // TransactionValidityError + case 1: switch res[1] { case 0: return determineInvalidTxnErr(res[2:]) diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 8ec2923c6d..c495cde251 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -32,6 +32,16 @@ func TestApplyExtrinsicErrors(t *testing.T) { test: []byte{0, 1, 0, 0x04, 65}, expected: "dispatch outcome error: unknown error: A", }, + { + name: "Dispatch failed lookup", + test: []byte{0, 1, 1}, + expected: "dispatch outcome error: failed lookup", + }, + { + name: "Dispatch bad origin", + test: []byte{0, 1, 2}, + expected: "dispatch outcome error: bad origin", + }, { name: "Invalid txn payment error", test: []byte{1, 0, 1}, From d7de542f0b2c0a4d9f5b3315c6c29255ac826842 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 13:28:00 -0600 Subject: [PATCH 118/245] refactor unknown transaction errors --- lib/babe/errors.go | 182 +++++++++++++++++++++++---------------------- 1 file changed, 92 insertions(+), 90 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 1194884c93..c704a4fabf 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -16,7 +16,6 @@ package babe import ( "errors" "fmt" - "github.com/ChainSafe/gossamer/lib/common/optional" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -67,8 +66,6 @@ type DispatchOutcomeError struct { msg string // description of error } -//type DispatchOutcomeError string - func (e DispatchOutcomeError) Error() string { return fmt.Sprintf("dispatch outcome error: %s", e.msg) } @@ -82,62 +79,84 @@ func (e TransactionValidityError) Error() string { return fmt.Sprintf("transaction validity error: %s", e.msg) } -func determineCustomModuleErr(res []byte) error { - if len(res) < 3 { - return errInvalidResult - } - errMsg, err := optional.NewBytes(false, nil).DecodeBytes(res[2:]) - if err != nil { - return err - } - return fmt.Errorf("index: %d code: %d message: %s", res[0], res[1], errMsg.String()) +// Dispatch Errors +type Other string + +type CannotLookup struct { + err string } -//func determineDispatchErr(res []byte) error { -// var v []byte -// err := scale.Unmarshal(res[1:], &v) -// if err != nil { -// // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure -// // TODO Create stucts for errors and integrate into Varying data type -// return errInvalidResult -// } -// -// switch res[0] { -// case 0: -// var v []byte -// err := scale.Unmarshal(res[1:], &v) -// if err != nil { -// // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure -// // TODO Create stucts for errors and integrate into Varying data type -// return errInvalidResult -// } -// return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} -// case 1: -// return &DispatchOutcomeError{"failed lookup"} -// case 2: -// return &DispatchOutcomeError{"bad origin"} -// case 3: -// return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))} -// } -// -// return errInvalidResult -//} +type BadOrigin struct { + err string +} + +type Module struct { // add in `scale:"1"` after + Idx uint8 + Err uint8 + Message *string +} + +// Dispatch Receivers +func (err Other) Index() uint { + return 0 +} + +func (err CannotLookup) Index() uint { + return 1 +} + +func (err BadOrigin) Index() uint { + return 2 +} + +func (err Module) Index() uint { + return 3 +} + +func (err Module) String() string { + return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) +} + +// Unknown Transaction Errors +type ValidityCannotLookup struct { + err string +} + +type NoUnsignedValidator struct { + err string +} + +type Custom uint8 + +// Unknown Transaction Receivers +func (err ValidityCannotLookup) Index() uint { + return 0 +} + +func (err NoUnsignedValidator) Index() uint { + return 1 +} + +func (err Custom) Index() uint { + return 2 +} + +// Invalid Transaction Errors + +// Invalid Transaction Receivers /* TODO: - 1) Add test cases for 2 middle cases - 2) Expand on this to include other error types - 3) Rebase - 4) Clean up code - 5) Make sure everything I do is okay (errors returned and printing as hex instead of string). This could be included in a pr - 6) PR??? + 1) Expand on this to include other error types + 2) Clean up code + 3) Make sure everything I do is okay (errors returned and printing as hex instead of string). This could be included in a pr + 4) PR??? */ func determineDispatchErr(res []byte) error { // This works yay! var e Other vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) if err != nil { - // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } @@ -145,10 +164,8 @@ func determineDispatchErr(res []byte) error { // This works yay! case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case CannotLookup: - // Add testing for this case, make sure struct is correct return &DispatchOutcomeError{"failed lookup"} case BadOrigin: - // Add testing for this case, make sure struct is correct return &DispatchOutcomeError{"bad origin"} case Module: return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} @@ -183,53 +200,38 @@ func determineInvalidTxnErr(res []byte) error { return errInvalidResult } +//func determineUnknownTxnErr(res []byte) error { +// switch res[0] { +// case 0: +// return &TransactionValidityError{"lookup failed"} +// case 1: +// return &TransactionValidityError{"validator not found"} +// case 2: +// return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} +// } +// return errInvalidResult +//} + func determineUnknownTxnErr(res []byte) error { - switch res[0] { - case 0: + var c Custom + vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) + err := scale.Unmarshal(res, &vdt) + if err != nil { + return errInvalidResult + } + + switch val := vdt.Value().(type) { + case ValidityCannotLookup: return &TransactionValidityError{"lookup failed"} - case 1: + case NoUnsignedValidator: return &TransactionValidityError{"validator not found"} - case 2: - return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} + case Custom: + return &TransactionValidityError{fmt.Sprintf("unknown error: %d", val)} } - return errInvalidResult -} - -type Other string - -func (err Other) Index() uint { - return 0 -} - -type CannotLookup struct { - err string -} - -func (err CannotLookup) Index() uint { - return 1 -} - -type BadOrigin struct { - err string -} - -func (err BadOrigin) Index() uint { - return 2 -} -type Module struct { // add in `scale:"1"` after - Idx uint8 - Err uint8 - Message *string -} - -func (err Module) Index() uint { - return 3 + return errInvalidResult } -func (err Module) String() string { - return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) -} func determineErr(res []byte) error { switch res[0] { From a09cf7201f60d78ae6100485ac391cdde624c1fd Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 13:45:38 -0600 Subject: [PATCH 119/245] refactor error handling --- lib/babe/errors.go | 171 +++++++++++++++++++++++++++++++++------------ 1 file changed, 125 insertions(+), 46 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index c704a4fabf..2854c87d17 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -97,53 +97,109 @@ type Module struct { // add in `scale:"1"` after } // Dispatch Receivers -func (err Other) Index() uint { - return 0 +func (err Other) Index() uint {return 0} +func (err CannotLookup) Index() uint {return 1} +func (err BadOrigin) Index() uint {return 2} +func (err Module) Index() uint {return 3} +func (err Module) String() string { + return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } -func (err CannotLookup) Index() uint { - return 1 +// Unknown Transaction Errors +type ValidityCannotLookup struct { + err string } -func (err BadOrigin) Index() uint { - return 2 +type NoUnsignedValidator struct { + err string } -func (err Module) Index() uint { - return 3 +type UnknownCustom uint8 + +// Unknown Transaction Receivers +func (err ValidityCannotLookup) Index() uint {return 0} +func (err NoUnsignedValidator) Index() uint {return 1} +func (err UnknownCustom) Index() uint {return 2} + +// Invalid Transaction Errors +type Call struct { + err string } -func (err Module) String() string { - return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) +type Payment struct { + err string } -// Unknown Transaction Errors -type ValidityCannotLookup struct { +type Future struct { err string } -type NoUnsignedValidator struct { +type Stale struct { err string } -type Custom uint8 +type BadProof struct { + err string +} -// Unknown Transaction Receivers -func (err ValidityCannotLookup) Index() uint { +type AncientBirthBlock struct { + err string +} + +type ExhaustsResources struct { + err string +} + +type InvalidCustom uint8 + +type BadMandatory struct { + err string +} + +type MandatoryDispatch struct { + err string +} + +// Invalid Transaction Receivers +func (err Call) Index() uint { return 0 } -func (err NoUnsignedValidator) Index() uint { +func (err Payment) Index() uint { return 1 } -func (err Custom) Index() uint { +func (err Future) Index() uint { return 2 } -// Invalid Transaction Errors +func (err Stale) Index() uint { + return 3 +} -// Invalid Transaction Receivers +func (err BadProof) Index() uint { + return 4 +} + +func (err AncientBirthBlock) Index() uint { + return 5 +} + +func (err ExhaustsResources) Index() uint { + return 6 +} + +func (err InvalidCustom) Index() uint { + return 7 +} + +func (err BadMandatory) Index() uint { + return 8 +} + +func (err MandatoryDispatch) Index() uint { + return 9 +} /* TODO: @@ -174,46 +230,69 @@ func determineDispatchErr(res []byte) error { // This works yay! return errInvalidResult } +//func determineInvalidTxnErr(res []byte) error { +// switch res[0] { +// case 0: +// return &TransactionValidityError{"call of the transaction is not expected"} +// case 1: +// return &TransactionValidityError{"invalid payment"} +// case 2: +// return &TransactionValidityError{"invalid transaction"} +// case 3: +// return &TransactionValidityError{"outdated transaction"} +// case 4: +// return &TransactionValidityError{"bad proof"} +// case 5: +// return &TransactionValidityError{"ancient birth block"} +// case 6: +// return &TransactionValidityError{"exhausts resources"} +// case 7: +// return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} +// case 8: +// return &TransactionValidityError{"mandatory dispatch error"} +// case 9: +// return &TransactionValidityError{"invalid mandatory dispatch"} +// } +// return errInvalidResult +//} + func determineInvalidTxnErr(res []byte) error { - switch res[0] { - case 0: + var c InvalidCustom + vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, + ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) + err := scale.Unmarshal(res, &vdt) + if err != nil { + return errInvalidResult + } + + switch val := vdt.Value().(type) { + case Call: return &TransactionValidityError{"call of the transaction is not expected"} - case 1: + case Payment: return &TransactionValidityError{"invalid payment"} - case 2: + case Future: return &TransactionValidityError{"invalid transaction"} - case 3: + case Stale: return &TransactionValidityError{"outdated transaction"} - case 4: + case BadProof: return &TransactionValidityError{"bad proof"} - case 5: + case AncientBirthBlock: return &TransactionValidityError{"ancient birth block"} - case 6: + case ExhaustsResources: return &TransactionValidityError{"exhausts resources"} - case 7: - return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} - case 8: + case InvalidCustom: + return &TransactionValidityError{fmt.Sprintf("unknown error: %d", val)} + case BadMandatory: return &TransactionValidityError{"mandatory dispatch error"} - case 9: + case MandatoryDispatch: return &TransactionValidityError{"invalid mandatory dispatch"} } + return errInvalidResult } -//func determineUnknownTxnErr(res []byte) error { -// switch res[0] { -// case 0: -// return &TransactionValidityError{"lookup failed"} -// case 1: -// return &TransactionValidityError{"validator not found"} -// case 2: -// return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} -// } -// return errInvalidResult -//} - func determineUnknownTxnErr(res []byte) error { - var c Custom + var c UnknownCustom vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) err := scale.Unmarshal(res, &vdt) if err != nil { @@ -225,7 +304,7 @@ func determineUnknownTxnErr(res []byte) error { return &TransactionValidityError{"lookup failed"} case NoUnsignedValidator: return &TransactionValidityError{"validator not found"} - case Custom: + case UnknownCustom: return &TransactionValidityError{fmt.Sprintf("unknown error: %d", val)} } From e57dc02c0262b8b93a42191533aa5b7ae12f666d Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 13:52:12 -0600 Subject: [PATCH 120/245] inital integration of scale pkg into babe --- lib/babe/errors.go | 82 ++++++---------------------------------------- 1 file changed, 10 insertions(+), 72 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 2854c87d17..d067438b2a 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -161,53 +161,17 @@ type MandatoryDispatch struct { } // Invalid Transaction Receivers -func (err Call) Index() uint { - return 0 -} - -func (err Payment) Index() uint { - return 1 -} - -func (err Future) Index() uint { - return 2 -} - -func (err Stale) Index() uint { - return 3 -} - -func (err BadProof) Index() uint { - return 4 -} - -func (err AncientBirthBlock) Index() uint { - return 5 -} - -func (err ExhaustsResources) Index() uint { - return 6 -} - -func (err InvalidCustom) Index() uint { - return 7 -} +func (err Call) Index() uint {return 0} +func (err Payment) Index() uint {return 1} +func (err Future) Index() uint {return 2} +func (err Stale) Index() uint {return 3} +func (err BadProof) Index() uint {return 4} +func (err AncientBirthBlock) Index() uint {return 5} +func (err ExhaustsResources) Index() uint {return 6} +func (err InvalidCustom) Index() uint {return 7} +func (err BadMandatory) Index() uint {return 8} +func (err MandatoryDispatch) Index() uint {return 9} -func (err BadMandatory) Index() uint { - return 8 -} - -func (err MandatoryDispatch) Index() uint { - return 9 -} - -/* - TODO: - 1) Expand on this to include other error types - 2) Clean up code - 3) Make sure everything I do is okay (errors returned and printing as hex instead of string). This could be included in a pr - 4) PR??? - */ func determineDispatchErr(res []byte) error { // This works yay! var e Other vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) @@ -230,32 +194,6 @@ func determineDispatchErr(res []byte) error { // This works yay! return errInvalidResult } -//func determineInvalidTxnErr(res []byte) error { -// switch res[0] { -// case 0: -// return &TransactionValidityError{"call of the transaction is not expected"} -// case 1: -// return &TransactionValidityError{"invalid payment"} -// case 2: -// return &TransactionValidityError{"invalid transaction"} -// case 3: -// return &TransactionValidityError{"outdated transaction"} -// case 4: -// return &TransactionValidityError{"bad proof"} -// case 5: -// return &TransactionValidityError{"ancient birth block"} -// case 6: -// return &TransactionValidityError{"exhausts resources"} -// case 7: -// return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} -// case 8: -// return &TransactionValidityError{"mandatory dispatch error"} -// case 9: -// return &TransactionValidityError{"invalid mandatory dispatch"} -// } -// return errInvalidResult -//} - func determineInvalidTxnErr(res []byte) error { var c InvalidCustom vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, From ad794ab059ad3083883eb5c605dee7db09523aa6 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 13:56:08 -0600 Subject: [PATCH 121/245] remove changes in scale pkg --- pkg/scale/comparison_test.go | 2 +- pkg/scale/decode.go | 2 +- pkg/scale/result_test.go | 2 +- pkg/scale/varying_data_type_test.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 1084ac12a2..54f4225da3 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -172,4 +172,4 @@ func BenchmarkMarshal(b *testing.B) { } } } -} \ No newline at end of file +} diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 15e96a9e88..f08029485e 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -646,4 +646,4 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { } dstv.Set(reflect.ValueOf(ui128)) return -} \ No newline at end of file +} diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 2de0a67285..d5b5320974 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -316,4 +316,4 @@ func TestResult_Set(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index 1c5a1756ec..d92dd7c723 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -637,4 +637,4 @@ func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { } }) } -} \ No newline at end of file +} From bc721607d7c54bb2493a5b9e25b1dc4d3638ffe4 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 28 Jun 2021 08:20:49 -0600 Subject: [PATCH 122/245] fix leftover comments --- lib/babe/build.go | 2 +- lib/babe/errors.go | 45 ++++++++++++++++++++++----------------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index 1972e265b0..03b6b44a57 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -348,7 +348,7 @@ func hasSlotEnded(slot Slot) bool { return time.Since(slotEnd) >= 0 } -// ExtrinsicsToBody returns scale2 encoded block body which contains inherent and extrinsic. +// ExtrinsicsToBody returns scale encoded block body which contains inherent and extrinsic. func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (*types.Body, error) { extrinsics := types.BytesArrayToExtrinsics(inherents) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index d067438b2a..1f60d94690 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -91,16 +91,16 @@ type BadOrigin struct { } type Module struct { // add in `scale:"1"` after - Idx uint8 - Err uint8 + Idx uint8 + Err uint8 Message *string } // Dispatch Receivers -func (err Other) Index() uint {return 0} -func (err CannotLookup) Index() uint {return 1} -func (err BadOrigin) Index() uint {return 2} -func (err Module) Index() uint {return 3} +func (err Other) Index() uint { return 0 } +func (err CannotLookup) Index() uint { return 1 } +func (err BadOrigin) Index() uint { return 2 } +func (err Module) Index() uint { return 3 } func (err Module) String() string { return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } @@ -117,9 +117,9 @@ type NoUnsignedValidator struct { type UnknownCustom uint8 // Unknown Transaction Receivers -func (err ValidityCannotLookup) Index() uint {return 0} -func (err NoUnsignedValidator) Index() uint {return 1} -func (err UnknownCustom) Index() uint {return 2} +func (err ValidityCannotLookup) Index() uint { return 0 } +func (err NoUnsignedValidator) Index() uint { return 1 } +func (err UnknownCustom) Index() uint { return 2 } // Invalid Transaction Errors type Call struct { @@ -161,18 +161,18 @@ type MandatoryDispatch struct { } // Invalid Transaction Receivers -func (err Call) Index() uint {return 0} -func (err Payment) Index() uint {return 1} -func (err Future) Index() uint {return 2} -func (err Stale) Index() uint {return 3} -func (err BadProof) Index() uint {return 4} -func (err AncientBirthBlock) Index() uint {return 5} -func (err ExhaustsResources) Index() uint {return 6} -func (err InvalidCustom) Index() uint {return 7} -func (err BadMandatory) Index() uint {return 8} -func (err MandatoryDispatch) Index() uint {return 9} - -func determineDispatchErr(res []byte) error { // This works yay! +func (err Call) Index() uint { return 0 } +func (err Payment) Index() uint { return 1 } +func (err Future) Index() uint { return 2 } +func (err Stale) Index() uint { return 3 } +func (err BadProof) Index() uint { return 4 } +func (err AncientBirthBlock) Index() uint { return 5 } +func (err ExhaustsResources) Index() uint { return 6 } +func (err InvalidCustom) Index() uint { return 7 } +func (err BadMandatory) Index() uint { return 8 } +func (err MandatoryDispatch) Index() uint { return 9 } + +func determineDispatchErr(res []byte) error { var e Other vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) @@ -197,7 +197,7 @@ func determineDispatchErr(res []byte) error { // This works yay! func determineInvalidTxnErr(res []byte) error { var c InvalidCustom vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, - ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) + ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) err := scale.Unmarshal(res, &vdt) if err != nil { return errInvalidResult @@ -249,7 +249,6 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } - func determineErr(res []byte) error { switch res[0] { case 0: From 6b133737293a0d83b9a90f9c15b39d2578897ae6 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 28 Jun 2021 09:14:16 -0600 Subject: [PATCH 123/245] reorganize receivers --- lib/babe/errors.go | 53 +++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 1f60d94690..75ac2f01c6 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -79,97 +79,106 @@ func (e TransactionValidityError) Error() string { return fmt.Sprintf("transaction validity error: %s", e.msg) } -// Dispatch Errors type Other string +func (err Other) Index() uint { return 0 } + type CannotLookup struct { err string } +func (err CannotLookup) Index() uint { return 1 } + type BadOrigin struct { err string } +func (err BadOrigin) Index() uint { return 2 } + type Module struct { // add in `scale:"1"` after Idx uint8 Err uint8 Message *string } -// Dispatch Receivers -func (err Other) Index() uint { return 0 } -func (err CannotLookup) Index() uint { return 1 } -func (err BadOrigin) Index() uint { return 2 } -func (err Module) Index() uint { return 3 } +func (err Module) Index() uint { return 3 } + func (err Module) String() string { return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } -// Unknown Transaction Errors type ValidityCannotLookup struct { err string } +func (err ValidityCannotLookup) Index() uint { return 0 } + type NoUnsignedValidator struct { err string } +func (err NoUnsignedValidator) Index() uint { return 1 } + type UnknownCustom uint8 -// Unknown Transaction Receivers -func (err ValidityCannotLookup) Index() uint { return 0 } -func (err NoUnsignedValidator) Index() uint { return 1 } -func (err UnknownCustom) Index() uint { return 2 } +func (err UnknownCustom) Index() uint { return 2 } -// Invalid Transaction Errors type Call struct { err string } +func (err Call) Index() uint { return 0 } + type Payment struct { err string } +func (err Payment) Index() uint { return 1 } + type Future struct { err string } +func (err Future) Index() uint { return 2 } + type Stale struct { err string } +func (err Stale) Index() uint { return 3 } + type BadProof struct { err string } +func (err BadProof) Index() uint { return 4 } + type AncientBirthBlock struct { err string } +func (err AncientBirthBlock) Index() uint { return 5 } + type ExhaustsResources struct { err string } +func (err ExhaustsResources) Index() uint { return 6 } + type InvalidCustom uint8 +func (err InvalidCustom) Index() uint { return 7 } + type BadMandatory struct { err string } +func (err BadMandatory) Index() uint { return 8 } + type MandatoryDispatch struct { err string } -// Invalid Transaction Receivers -func (err Call) Index() uint { return 0 } -func (err Payment) Index() uint { return 1 } -func (err Future) Index() uint { return 2 } -func (err Stale) Index() uint { return 3 } -func (err BadProof) Index() uint { return 4 } -func (err AncientBirthBlock) Index() uint { return 5 } -func (err ExhaustsResources) Index() uint { return 6 } -func (err InvalidCustom) Index() uint { return 7 } -func (err BadMandatory) Index() uint { return 8 } func (err MandatoryDispatch) Index() uint { return 9 } func determineDispatchErr(res []byte) error { From 52257ff2df0d4732900a609a878c423c42b7f0c7 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 28 Jun 2021 11:05:10 -0600 Subject: [PATCH 124/245] formatting --- go.sum | 105 +++++++++++++++++++++++++++++++++++++++++++++ lib/babe/errors.go | 20 ++++++++- 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/go.sum b/go.sum index 1a7f5c763d..cbd5a83445 100644 --- a/go.sum +++ b/go.sum @@ -197,31 +197,42 @@ github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OI github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.1.5 h1:kxhtnfFVi+rYdOALN0B3k9UT86zVJKfBimRaciULW4I= github.com/google/uuid v1.1.5/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY= github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/rpc v1.2.0 h1:WvvdC2lNeT1SP32zrIce5l0ECBfbAlmrmSBsuc57wfk= github.com/gorilla/rpc v1.2.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw= github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= +github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is= github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= +github.com/gtank/ristretto255 v0.1.2 h1:JEqUCPA1NvLq5DwYtuzigd7ss8fwbYay9fi4/5uMzcc= github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIvY4OmlYW69o= github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU= github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI= github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs= github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao= github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc= +github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031 h1:HarGZ5h9HD9LgEg1yRVMXyfiw4wlXiLiYM2oMjeA/SE= github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031/go.mod h1:nNs7wvRfN1eKaMknBydLNQU6146XQim8t4h+q90biWo= github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= @@ -231,6 +242,7 @@ github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUP github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj6+M= github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-cid v0.0.6/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= +github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY= github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.1.0/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= @@ -238,25 +250,33 @@ github.com/ipfs/go-datastore v0.1.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRV github.com/ipfs/go-datastore v0.4.0/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= github.com/ipfs/go-datastore v0.4.1/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= github.com/ipfs/go-datastore v0.4.4/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= +github.com/ipfs/go-datastore v0.4.5 h1:cwOUcGMLdLPWgu3SlrCckCMznaGADbPqE0r8h768/Dg= github.com/ipfs/go-datastore v0.4.5/go.mod h1:eXTcaaiN6uOlVCLS9GjJUJtlvJfM3xk23w3fyfrmmJs= +github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk= github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-ds-badger v0.0.2/go.mod h1:Y3QpeSFWQf6MopLTiZD+VT6IC1yZqaGmjvRcKeSGij8= github.com/ipfs/go-ds-badger v0.0.5/go.mod h1:g5AuuCGmr7efyzQhLL8MzwqcauPojGPUaHzfGTzuE3s= github.com/ipfs/go-ds-badger v0.0.7/go.mod h1:qt0/fWzZDoPW6jpQeqUjR5kBfhDNB65jd9YlmAvpQBk= github.com/ipfs/go-ds-badger v0.2.1/go.mod h1:Tx7l3aTph3FMFrRS838dcSJh+jjA7cX9DrGVwx/NOwE= +github.com/ipfs/go-ds-badger v0.2.3 h1:J27YvAcpuA5IvZUbeBxOcQgqnYHUPxoygc6QxxkodZ4= github.com/ipfs/go-ds-badger v0.2.3/go.mod h1:pEYw0rgg3FIrywKKnL+Snr+w/LjJZVMTBRn4FS6UHUk= +github.com/ipfs/go-ds-badger2 v0.1.0 h1:784py6lXkwlVF+K6XSuqmdMgy5l8GI6k60ngBokb9Fg= github.com/ipfs/go-ds-badger2 v0.1.0/go.mod h1:pbR1p817OZbdId9EvLOhKBgUVTM3BMCSTan78lDDVaw= github.com/ipfs/go-ds-leveldb v0.0.1/go.mod h1:feO8V3kubwsEF22n0YRQCffeb79OOYIykR4L04tMOYc= github.com/ipfs/go-ds-leveldb v0.1.0/go.mod h1:hqAW8y4bwX5LWcCtku2rFNX3vjDZCy5LZCg+cSZvYb8= github.com/ipfs/go-ds-leveldb v0.4.1/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s= +github.com/ipfs/go-ds-leveldb v0.4.2 h1:QmQoAJ9WkPMUfBLnu1sBVy0xWWlJPg0m4kRAiJL9iaw= github.com/ipfs/go-ds-leveldb v0.4.2/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s= github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw= github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc= +github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8= github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ= +github.com/ipfs/go-ipns v0.0.2 h1:oq4ErrV4hNQ2Eim257RTYRgfOSV/s8BDaf9iIl4NwFs= github.com/ipfs/go-ipns v0.0.2/go.mod h1:WChil4e0/m9cIINWLxZe1Jtf77oz5L05rO2ei/uKJ5U= github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM= github.com/ipfs/go-log v1.0.2/go.mod h1:1MNjMxe0u6xvJZgeqbJ8vdo2TKaGwZ1a0Bpza+sr2Sk= github.com/ipfs/go-log v1.0.3/go.mod h1:OsLySYkwIbiSUR/yBTdv1qPtcE4FW3WPWk/ewz9Ru+A= +github.com/ipfs/go-log v1.0.4 h1:6nLQdX4W8P9yZZFH7mO+X/PzjN8Laozm/lMJ6esdgzY= github.com/ipfs/go-log v1.0.4/go.mod h1:oDCg2FkjogeFOhqqb+N39l2RpTNPL6F/StPkB3kPgcs= github.com/ipfs/go-log/v2 v2.0.2/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= github.com/ipfs/go-log/v2 v2.0.3/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= @@ -266,19 +286,26 @@ github.com/ipfs/go-log/v2 v2.1.3 h1:1iS3IU7aXRlbgUpN8yTTpJ53NXYjAe37vcI5+5nYrzk= github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g= github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA= github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= +github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jbenet/go-cienv v0.0.0-20150120210510-1bb1476777ec/go.mod h1:rGaEvXB4uRSZMmzKNLoXvTu1sfx+1kv/DojUlPrSZGs= +github.com/jbenet/go-cienv v0.1.0 h1:Vc/s0QbQtoxX8MwwSLWWh+xNNZvM3Lw7NsTcHrvvhMc= github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA= github.com/jbenet/go-temp-err-catcher v0.0.0-20150120210811-aac704a3f4f2/go.mod h1:8GXXJV31xl8whumTzdZsTt3RnUIiPqzkyf7mxToRCMs= +github.com/jbenet/go-temp-err-catcher v0.1.0 h1:zpb3ZH6wIE8Shj2sKS+khgRvf7T7RABoLk/+KKHggpk= github.com/jbenet/go-temp-err-catcher v0.1.0/go.mod h1:0kJRvmDZXNMIiJirNPEYfhpPwbGVtZVWC34vc5WLsDk= github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY= github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= +github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o= github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= +github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 h1:EFT6MH3igZK/dIVqgGbTqWVvkZ7wJ5iGN03SVtvvdd8= github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25/go.mod h1:sWkGw/wsaHtRsT9zGQ/WyJCotGWG/Anow/9hsAcBWRw= github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= +github.com/jpillora/ipfilter v1.2.2 h1:lfENG7V1/T+ZutAtSbt6gssvzj3Ql0JmcFlqS/BES2E= github.com/jpillora/ipfilter v1.2.2/go.mod h1:xvAYjA+48eM9E5+sg9yI55N5lE9sefckjsnDvSiEA+g= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= @@ -290,6 +317,7 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d h1:68u9r4wEvL3gYg2jvAOgROwZ3H+Y3hIDk4tbbmIjcYQ= github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= @@ -297,22 +325,30 @@ github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/libp2p/go-addr-util v0.0.1/go.mod h1:4ac6O7n9rIAKB1dnd+s8IbbMXkt+oBpzX4/+RACcnlQ= +github.com/libp2p/go-addr-util v0.0.2 h1:7cWK5cdA5x72jX0g8iLrQWm5TRJZ6CzGdPEhWj7plWU= github.com/libp2p/go-addr-util v0.0.2/go.mod h1:Ecd6Fb3yIuLzq4bD7VcywcVSBtefcAwnUISBM3WG15E= github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ= +github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs= github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM= +github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38yPW7c= github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-conn-security-multistream v0.1.0/go.mod h1:aw6eD7LOsHEX7+2hJkDxw1MteijaVcI+/eP2/x3J1xc= github.com/libp2p/go-conn-security-multistream v0.2.0/go.mod h1:hZN4MjlNetKD3Rq5Jb/P5ohUnFLNzEAR4DLSzpn2QLU= github.com/libp2p/go-conn-security-multistream v0.2.1 h1:ft6/POSK7F+vl/2qzegnHDaXFU0iWB4yVTYrioC6Zy0= github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70= github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UGQf8A2eEmx4= +github.com/libp2p/go-eventbus v0.2.1 h1:VanAdErQnpTioN2TowqNcOijf6YwhuODe4pPKSDpxGc= github.com/libp2p/go-eventbus v0.2.1/go.mod h1:jc2S4SoEVPP48H9Wpzm5aiGwUCBMfGhVhhBjyhhCJs8= github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8= github.com/libp2p/go-flow-metrics v0.0.2/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= +github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM= github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= github.com/libp2p/go-libp2p v0.6.1/go.mod h1:CTFnWXogryAHjXAKEbOf1OWY+VeAP3lDMZkfEI5sT54= github.com/libp2p/go-libp2p v0.7.0/go.mod h1:hZJf8txWeCduQRDC/WSqBGMxaTHCOYHt2xSU1ivxn0k= @@ -321,6 +357,7 @@ github.com/libp2p/go-libp2p v0.8.1/go.mod h1:QRNH9pwdbEBpx5DTJYg+qxcVaDMAz3Ee/qD github.com/libp2p/go-libp2p v0.12.0/go.mod h1:FpHZrfC1q7nA8jitvdjKBDF31hguaC676g/nT9PgQM0= github.com/libp2p/go-libp2p v0.14.2 h1:qs0ABtjjNjS+RIXT1uM7sMJEvIc0pq2nKR0VQxFXhHI= github.com/libp2p/go-libp2p v0.14.2/go.mod h1:0PQMADQEjCM2l8cSMYDpTgsb8gr6Zq7i4LUgq1mlW2E= +github.com/libp2p/go-libp2p-asn-util v0.0.0-20200825225859-85005c6cf052 h1:BM7aaOF7RpmNn9+9g6uTjGJ0cTzWr5j9i9IKeun2M8U= github.com/libp2p/go-libp2p-asn-util v0.0.0-20200825225859-85005c6cf052/go.mod h1:nRMRTab+kZuk0LnKZpxhOVH/ndsdr2Nr//Zltc/vwgo= github.com/libp2p/go-libp2p-autonat v0.1.1/go.mod h1:OXqkeGOY2xJVWKAGV2inNF5aKN/djNA3fdpCWloIudE= github.com/libp2p/go-libp2p-autonat v0.2.0/go.mod h1:DX+9teU4pEEoZUqR1PiMlqliONQdNbfzE1C718tcViI= @@ -331,9 +368,11 @@ github.com/libp2p/go-libp2p-autonat v0.4.2 h1:YMp7StMi2dof+baaxkbxaizXjY1RPvU71C github.com/libp2p/go-libp2p-autonat v0.4.2/go.mod h1:YxaJlpr81FhdOv3W3BTconZPfhaYivRdf53g+S2wobk= github.com/libp2p/go-libp2p-blankhost v0.1.1/go.mod h1:pf2fvdLJPsC1FsVrNP3DUUvMzUts2dsLLBEpo1vW1ro= github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU= +github.com/libp2p/go-libp2p-blankhost v0.2.0 h1:3EsGAi0CBGcZ33GwRuXEYJLLPoVWyXJ1bcJzAJjINkk= github.com/libp2p/go-libp2p-blankhost v0.2.0/go.mod h1:eduNKXGTioTuQAUcZ5epXi9vMl+t4d8ugUBRQ4SqaNQ= github.com/libp2p/go-libp2p-circuit v0.1.4/go.mod h1:CY67BrEjKNDhdTk8UgBX1Y/H5c3xkAcs3gnksxY7osU= github.com/libp2p/go-libp2p-circuit v0.2.1/go.mod h1:BXPwYDN5A8z4OEY9sOfr2DUQMLQvKt/6oku45YUmjIo= +github.com/libp2p/go-libp2p-circuit v0.4.0 h1:eqQ3sEYkGTtybWgr6JLqJY6QLtPWRErvFjFDfAOO1wc= github.com/libp2p/go-libp2p-circuit v0.4.0/go.mod h1:t/ktoFIUzM6uLQ+o1G6NuBl2ANhBKN9Bc8jRIk31MoA= github.com/libp2p/go-libp2p-core v0.0.1/go.mod h1:g/VxnTZ/1ygHxH3dKok7Vno1VfpvGcGip57wjTU4fco= github.com/libp2p/go-libp2p-core v0.0.4/go.mod h1:jyuCQP356gzfCFtRKyvAbNkyeuxb7OlyhWZ3nls5d2I= @@ -362,8 +401,11 @@ github.com/libp2p/go-libp2p-core v0.8.5/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= github.com/libp2p/go-libp2p-discovery v0.3.0/go.mod h1:o03drFnz9BVAZdzC/QUQ+NeQOu38Fu7LJGEOK2gQltw= +github.com/libp2p/go-libp2p-discovery v0.5.0 h1:Qfl+e5+lfDgwdrXdu4YNCWyEo3fWuP+WgN9mN0iWviQ= github.com/libp2p/go-libp2p-discovery v0.5.0/go.mod h1:+srtPIU9gDaBNu//UHvcdliKBIcr4SfDcm0/PfPJLug= +github.com/libp2p/go-libp2p-kad-dht v0.11.1 h1:FsriVQhOUZpCotWIjyFSjEDNJmUzuMma/RyyTDZanwc= github.com/libp2p/go-libp2p-kad-dht v0.11.1/go.mod h1:5ojtR2acDPqh/jXf5orWy8YGb8bHQDS+qeDcoscL/PI= +github.com/libp2p/go-libp2p-kbucket v0.4.7 h1:spZAcgxifvFZHBD8tErvppbnNiKA5uokDu3CV7axu70= github.com/libp2p/go-libp2p-kbucket v0.4.7/go.mod h1:XyVo99AfQH0foSf176k4jY1xUJ2+jUJIZCSDm7r2YKk= github.com/libp2p/go-libp2p-loggables v0.1.0/go.mod h1:EyumB2Y6PrYjr55Q3/tiJ/o3xoDasoRYM7nOzEpoa90= github.com/libp2p/go-libp2p-mplex v0.2.0/go.mod h1:Ejl9IyjvXJ0T9iqUTE1jpYATQ9NM3g+OtR+EMMODbKo= @@ -375,7 +417,9 @@ github.com/libp2p/go-libp2p-mplex v0.4.0/go.mod h1:yCyWJE2sc6TBTnFpjvLuEJgTSw/u+ github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc= github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g= github.com/libp2p/go-libp2p-nat v0.0.5/go.mod h1:1qubaE5bTZMJE+E/uu2URroMbzdubFz1ChgiN79yKPE= +github.com/libp2p/go-libp2p-nat v0.0.6 h1:wMWis3kYynCbHoyKLPBEMu4YRLltbm8Mk08HGSfvTkU= github.com/libp2p/go-libp2p-nat v0.0.6/go.mod h1:iV59LVhB3IkFvS6S6sauVTSOrNEANnINbI/fkaLimiw= +github.com/libp2p/go-libp2p-netutil v0.1.0 h1:zscYDNVEcGxyUpMd0JReUZTrpMfia8PmLKcKF72EAMQ= github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCThNdbQD54k3TqjpbFU= github.com/libp2p/go-libp2p-noise v0.1.1/go.mod h1:QDFLdKX7nluB7DEnlVPbz7xlLHdwHFA9HiohJRr3vwM= github.com/libp2p/go-libp2p-noise v0.2.0 h1:wmk5nhB9a2w2RxMOyvsoKjizgJOEaJdfAakr0jN8gds= @@ -390,15 +434,19 @@ github.com/libp2p/go-libp2p-peerstore v0.2.2/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRj github.com/libp2p/go-libp2p-peerstore v0.2.6/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= github.com/libp2p/go-libp2p-peerstore v0.2.7 h1:83JoLxyR9OYTnNfB5vvFqvMUv/xDNa6NoPHnENhBsGw= github.com/libp2p/go-libp2p-peerstore v0.2.7/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= +github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= github.com/libp2p/go-libp2p-quic-transport v0.10.0 h1:koDCbWD9CCHwcHZL3/WEvP2A+e/o5/W5L3QS/2SPMA0= github.com/libp2p/go-libp2p-quic-transport v0.10.0/go.mod h1:RfJbZ8IqXIhxBRm5hqUEJqjiiY8xmEuq3HUDS993MkA= github.com/libp2p/go-libp2p-record v0.1.2/go.mod h1:pal0eNcT5nqZaTV7UGhqeGqxFgGdsU/9W//C8dqjQDk= +github.com/libp2p/go-libp2p-record v0.1.3 h1:R27hoScIhQf/A8XJZ8lYpnqh9LatJ5YbHs28kCIfql0= github.com/libp2p/go-libp2p-record v0.1.3/go.mod h1:yNUff/adKIfPnYQXgp6FQmNu3gLJ6EMg7+/vv2+9pY4= +github.com/libp2p/go-libp2p-routing-helpers v0.2.3 h1:xY61alxJ6PurSi+MXbywZpelvuU4U4p/gPTxjqCqTzY= github.com/libp2p/go-libp2p-routing-helpers v0.2.3/go.mod h1:795bh+9YeoFl99rMASoiVgHdi5bjack0N1+AFAdbvBw= github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8= github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g= github.com/libp2p/go-libp2p-secio v0.2.1/go.mod h1:cWtZpILJqkqrSkiYcDBh5lA3wbT2Q+hz3rJQq3iftD8= +github.com/libp2p/go-libp2p-secio v0.2.2 h1:rLLPvShPQAcY6eNurKNZq3eZjPWfU9kXF2eI9jIYdrg= github.com/libp2p/go-libp2p-secio v0.2.2/go.mod h1:wP3bS+m5AUnFA+OFO7Er03uO1mncHG0uVwGrwvjYlNY= github.com/libp2p/go-libp2p-swarm v0.1.0/go.mod h1:wQVsCdjsuZoc730CgOvh5ox6K8evllckjebkdiY5ta4= github.com/libp2p/go-libp2p-swarm v0.2.2/go.mod h1:fvmtQ0T1nErXym1/aa1uJEyN7JzaTNyBcHImCxRpPKU= @@ -417,6 +465,7 @@ github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod github.com/libp2p/go-libp2p-testing v0.3.0/go.mod h1:efZkql4UZ7OVsEfaxNHZPzIehtsBXMrXnCfJIgDti5g= github.com/libp2p/go-libp2p-testing v0.4.0 h1:PrwHRi0IGqOwVQWR3xzgigSlhlLfxgfXgkHxr77EghQ= github.com/libp2p/go-libp2p-testing v0.4.0/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0= +github.com/libp2p/go-libp2p-tls v0.1.3 h1:twKMhMu44jQO+HgQK9X8NHO5HkeJu2QbhLzLJpa8oNM= github.com/libp2p/go-libp2p-tls v0.1.3/go.mod h1:wZfuewxOndz5RTnCAxFliGjvYSDA40sKitV4c50uI1M= github.com/libp2p/go-libp2p-transport-upgrader v0.1.1/go.mod h1:IEtA6or8JUbsV07qPW4r01GnTenLW4oi3lOPbUMGJJA= github.com/libp2p/go-libp2p-transport-upgrader v0.2.0/go.mod h1:mQcrHj4asu6ArfSoMuyojOdjx73Q47cYD7s5+gZOlns= @@ -445,8 +494,10 @@ github.com/libp2p/go-mplex v0.3.0 h1:U1T+vmCYJaEoDJPV1aq31N56hS+lJgb397GsylNSgrU github.com/libp2p/go-mplex v0.3.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= github.com/libp2p/go-msgio v0.0.2/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= +github.com/libp2p/go-msgio v0.0.6 h1:lQ7Uc0kS1wb1EfRxO2Eir/RJoHkHn7t6o+EiwsYIKJA= github.com/libp2p/go-msgio v0.0.6/go.mod h1:4ecVB6d9f4BDSL5fqvPiC4A3KivjWn+Venn/1ALLMWA= github.com/libp2p/go-nat v0.0.4/go.mod h1:Nmw50VAvKuk38jUBcmNh6p9lUJLoODbJRvYAa/+KSDo= +github.com/libp2p/go-nat v0.0.5 h1:qxnwkco8RLKqVh1NmjQ+tJ8p8khNLFxuElYG/TwqW4Q= github.com/libp2p/go-nat v0.0.5/go.mod h1:B7NxsVNPZmRLvMOwiEO1scOSyjA56zxYAGv1yQgRkEU= github.com/libp2p/go-netroute v0.1.2/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= github.com/libp2p/go-netroute v0.1.3/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= @@ -456,21 +507,26 @@ github.com/libp2p/go-openssl v0.0.2/go.mod h1:v8Zw2ijCSWBQi8Pq5GAixw6DbFfa9u6VIY github.com/libp2p/go-openssl v0.0.3/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-openssl v0.0.4/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-openssl v0.0.5/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= +github.com/libp2p/go-openssl v0.0.7 h1:eCAzdLejcNVBzP/iZM9vqHnQm+XyCEbSSIheIPRGNsw= github.com/libp2p/go-openssl v0.0.7/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-reuseport v0.0.1/go.mod h1:jn6RmB1ufnQwl0Q1f+YxAj8isJgDCQzaaxIFYDhcYEA= +github.com/libp2p/go-reuseport v0.0.2 h1:XSG94b1FJfGA01BUrT82imejHQyTxO4jEWqheyCXYvU= github.com/libp2p/go-reuseport v0.0.2/go.mod h1:SPD+5RwGC7rcnzngoYC86GjPzjSywuQyMVAheVBD9nQ= github.com/libp2p/go-reuseport-transport v0.0.2/go.mod h1:YkbSDrvjUVDL6b8XqriyA20obEtsW9BLkuOUyQAOCbs= github.com/libp2p/go-reuseport-transport v0.0.3/go.mod h1:Spv+MPft1exxARzP2Sruj2Wb5JSyHNncjf1Oi2dEbzM= +github.com/libp2p/go-reuseport-transport v0.0.4 h1:OZGz0RB620QDGpv300n1zaOcKGGAoGVf8h9txtt/1uM= github.com/libp2p/go-reuseport-transport v0.0.4/go.mod h1:trPa7r/7TJK/d+0hdBLOCGvpQQVOU74OXbNCIMkufGw= github.com/libp2p/go-sockaddr v0.0.2/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= github.com/libp2p/go-sockaddr v0.1.1 h1:yD80l2ZOdGksnOyHrhxDdTDFrf7Oy+v3FMVArIRgZxQ= github.com/libp2p/go-sockaddr v0.1.1/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= github.com/libp2p/go-stream-muxer v0.0.1/go.mod h1:bAo8x7YkSpadMTbtTaxGVHWUQsR/l5MEaHbKaliuT14= github.com/libp2p/go-stream-muxer-multistream v0.2.0/go.mod h1:j9eyPol/LLRqT+GPLSxvimPhNph4sfYfMoDPd7HkzIc= +github.com/libp2p/go-stream-muxer-multistream v0.3.0 h1:TqnSHPJEIqDEO7h1wZZ0p3DXdvDSiLHQidKKUGZtiOY= github.com/libp2p/go-stream-muxer-multistream v0.3.0/go.mod h1:yDh8abSIzmZtqtOt64gFJUXEryejzNb0lisTt+fAMJA= github.com/libp2p/go-tcp-transport v0.1.0/go.mod h1:oJ8I5VXryj493DEJ7OsBieu8fcg2nHGctwtInJVpipc= github.com/libp2p/go-tcp-transport v0.1.1/go.mod h1:3HzGvLbx6etZjnFlERyakbaYPdfjg2pWP97dFZworkY= github.com/libp2p/go-tcp-transport v0.2.0/go.mod h1:vX2U0CnWimU4h0SGSEsg++AzvBcroCGYw28kh94oLe0= +github.com/libp2p/go-tcp-transport v0.2.1 h1:ExZiVQV+h+qL16fzCWtd1HSzPsqWottJ8KXwWaVi8Ns= github.com/libp2p/go-tcp-transport v0.2.1/go.mod h1:zskiJ70MEfWz2MKxvFB/Pv+tPIB1PpPUrHIWQ8aFw7M= github.com/libp2p/go-ws-transport v0.2.0/go.mod h1:9BHJz/4Q5A9ludYWKoGCFC5gUElzlHoKzu0yY9p/klM= github.com/libp2p/go-ws-transport v0.3.0/go.mod h1:bpgTJmRZAvVHrgHybCVyqoBmyLQ1fiZuEaBYusP5zsk= @@ -501,10 +557,12 @@ github.com/marten-seemann/qtls-go1-15 v0.1.1/go.mod h1:GyFwywLKkRt+6mfU99csTEY1j github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= @@ -514,12 +572,15 @@ github.com/miekg/dns v1.1.28/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7 github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY= github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= +github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 h1:hLDRPB66XQT/8+wG9WsDpiCvZf1yKO7sz7scAjSlBa0= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= +github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= +github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= @@ -529,8 +590,11 @@ github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVq github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= +github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= +github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI= github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= +github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4= github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM= github.com/multiformats/go-multiaddr v0.0.1/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= github.com/multiformats/go-multiaddr v0.0.2/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= @@ -541,6 +605,7 @@ github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y9 github.com/multiformats/go-multiaddr v0.2.1/go.mod h1:s/Apk6IyxfvMjDafnhJgJ3/46z7tZ04iMk5wP4QMGGE= github.com/multiformats/go-multiaddr v0.2.2/go.mod h1:NtfXiOtHvghW9KojvtySjH5y0u0xW5UouOmQQrn6a3Y= github.com/multiformats/go-multiaddr v0.3.0/go.mod h1:dF9kph9wfJ+3VLAaeBqo9Of8x4fJxp6ggJGteB8HQTI= +github.com/multiformats/go-multiaddr v0.3.1 h1:1bxa+W7j9wZKTZREySx1vPMs2TqrYWjVZ7zE6/XLG1I= github.com/multiformats/go-multiaddr v0.3.1/go.mod h1:uPbspcUPd5AfaP6ql3ujFY+QWzmBD8uLLL4bXW0XfGc= github.com/multiformats/go-multiaddr-dns v0.0.1/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.0.2/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= @@ -548,6 +613,7 @@ github.com/multiformats/go-multiaddr-dns v0.2.0/go.mod h1:TJ5pr5bBO7Y1B18djPuRsV github.com/multiformats/go-multiaddr-dns v0.3.1 h1:QgQgR+LQVt3NPTjbrLLpsaT2ufAA2y0Mkk+QRVJbW3A= github.com/multiformats/go-multiaddr-dns v0.3.1/go.mod h1:G/245BRQ6FJGmryJCrOuTdB37AMA5AMOVuO6NY3JwTk= github.com/multiformats/go-multiaddr-fmt v0.0.1/go.mod h1:aBYjqL4T/7j4Qx+R73XSv/8JsgnRFlf0w2KGLCmXl3Q= +github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E= github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo= github.com/multiformats/go-multiaddr-net v0.0.1/go.mod h1:nw6HSxNmCIQH27XPGBuX+d1tnvM7ihcFwHMSstNAVUU= github.com/multiformats/go-multiaddr-net v0.1.0/go.mod h1:5JNbcfBOP4dnhoZOv10JJVkJO0pCCEf8mTnipAo2UZQ= @@ -556,8 +622,10 @@ github.com/multiformats/go-multiaddr-net v0.1.2/go.mod h1:QsWt3XK/3hwvNxZJp92iMQ github.com/multiformats/go-multiaddr-net v0.1.3/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= github.com/multiformats/go-multiaddr-net v0.1.4/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= github.com/multiformats/go-multiaddr-net v0.1.5/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= +github.com/multiformats/go-multiaddr-net v0.2.0 h1:MSXRGN0mFymt6B1yo/6BPnIRpLPEnKgQNvVfCX5VDJk= github.com/multiformats/go-multiaddr-net v0.2.0/go.mod h1:gGdH3UXny6U3cKKYCvpXI5rnK7YaOIEOPVDI9tsJbEA= github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs= +github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk= github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc= github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po= @@ -565,6 +633,7 @@ github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa github.com/multiformats/go-multihash v0.0.9/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= github.com/multiformats/go-multihash v0.0.10/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= +github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I= github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= @@ -575,36 +644,49 @@ github.com/multiformats/go-multistream v0.2.2/go.mod h1:UIcnm7Zuo8HKG+HkWgfQsGL+ github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= +github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY= github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= +github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975 h1:zm/Rb2OsnLWCY88Njoqgo4X6yt/lx3oBNWhepX0AOMU= github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975/go.mod h1:4Mct/lWCFf1jzQTTAaWtOI7sXqmG+wBeiBfT4CxoaJk= +github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks= github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= +github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 h1:shk/vn9oCoOTmwcouEdwIeOtOGA/ELRUw/GwvxwfT+0= github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA= +github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea h1:okKoivlkNRRLqXraEtatHfEhW+D71QTwkaj+4n4M2Xc= github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea/go.mod h1:3KEU5Dm8MAYWZqity880wOFJ9PhQjyKVZGwAEfc5Q4E= +github.com/phuslu/iploc v1.0.20200807 h1:LIBm2Y9l5zmUvnJhQgMcLZ0iVwuG+5/L6AgbMwSOpE4= github.com/phuslu/iploc v1.0.20200807/go.mod h1:Q/0VX0txvbxekt4NhWIi3Q3eyZ139lHhnlzvDxyXhuc= +github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo= github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= @@ -613,6 +695,7 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7q github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= +github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -645,8 +728,10 @@ github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qt github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE= github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA= github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0= +github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU= github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= @@ -656,31 +741,41 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/src-d/envconfig v1.0.0/go.mod h1:Q9YQZ7BKITldTBnoxsE5gOeB5y66RyPXeue/R4aaNBc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= +github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca h1:Ld/zXl5t4+D69SiV4JoN7kkfvJdOWlPpfxrzxpLMoUk= github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= +github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc= github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI= github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= +github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= +github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec h1:VElCeVyfCWNmCv6UisKQrr+P2/JRG0uf4/FIdCB4pL0= github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec/go.mod h1:VGyarTzasuS7k5KhSIGpM3tciSZlkP31Mp9VJTHMMeI= +github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= github.com/whyrusleeping/go-logging v0.0.1/go.mod h1:lDPYj54zutzG1XYfHAhcc7oNXEburHQBn+Iqd4yS4vE= github.com/whyrusleeping/mafmt v1.2.8/go.mod h1:faQJFPbLSxzD9xpA02ttW/tS9vZykNvXwGvqIpk20FA= +github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 h1:Y1/FEOpaCpD21WxrmfeIYCFPuVPRCY2XZTWzTNHGw30= github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4= +github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI= github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= @@ -698,6 +793,7 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.0.0 h1:qsup4IcBdlmsnGfqyLl4Ntn3C2XCCuKAE7DwHpScyUo= go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= @@ -707,6 +803,7 @@ go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9E go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.14.1/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= +go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM= go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE= golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw= @@ -746,6 +843,7 @@ golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPI golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -858,6 +956,7 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= @@ -867,6 +966,7 @@ google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -909,20 +1009,25 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU= gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c= gopkg.in/src-d/go-cli.v0 v0.0.0-20181105080154-d492247bbc0d/go.mod h1:z+K8VcOYVYcSwSjGebuDL6176A1XskgbtNl64NSg+n8= gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 75ac2f01c6..bbe27b2f4b 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -16,6 +16,7 @@ package babe import ( "errors" "fmt" + "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -79,23 +80,27 @@ func (e TransactionValidityError) Error() string { return fmt.Sprintf("transaction validity error: %s", e.msg) } +// Other Some error occurred type Other string func (err Other) Index() uint { return 0 } +// CannotLookup Failed to lookup some data type CannotLookup struct { err string } func (err CannotLookup) Index() uint { return 1 } +// BadOrigin A bad origin type BadOrigin struct { err string } func (err BadOrigin) Index() uint { return 2 } -type Module struct { // add in `scale:"1"` after +// Module A custom error in a module +type Module struct { Idx uint8 Err uint8 Message *string @@ -107,74 +112,87 @@ func (err Module) String() string { return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } +// ValidityCannotLookup Could not lookup some information that is required to validate the transaction type ValidityCannotLookup struct { err string } func (err ValidityCannotLookup) Index() uint { return 0 } +// NoUnsignedValidator No validator found for the given unsigned transaction type NoUnsignedValidator struct { err string } func (err NoUnsignedValidator) Index() uint { return 1 } +// UnknownCustom Any other custom unknown validity that is not covered type UnknownCustom uint8 func (err UnknownCustom) Index() uint { return 2 } +// Call The call of the transaction is not expected type Call struct { err string } func (err Call) Index() uint { return 0 } +// Payment General error to do with the inability to pay some fees (e.g. account balance too low) type Payment struct { err string } func (err Payment) Index() uint { return 1 } +// Future General error to do with the transaction not yet being valid (e.g. nonce too high) type Future struct { err string } func (err Future) Index() uint { return 2 } +// Stale General error to do with the transaction being outdated (e.g. nonce too low) type Stale struct { err string } func (err Stale) Index() uint { return 3 } +// BadProof General error to do with the transaction’s proofs (e.g. signature) type BadProof struct { err string } func (err BadProof) Index() uint { return 4 } +// AncientBirthBlock The transaction birth block is ancient type AncientBirthBlock struct { err string } func (err AncientBirthBlock) Index() uint { return 5 } +// ExhaustsResources The transaction would exhaust the resources of current block type ExhaustsResources struct { err string } func (err ExhaustsResources) Index() uint { return 6 } +// InvalidCustom Any other custom invalid validity that is not covered type InvalidCustom uint8 func (err InvalidCustom) Index() uint { return 7 } +// BadMandatory An extrinsic with a Mandatory dispatch resulted in Error type BadMandatory struct { err string } func (err BadMandatory) Index() uint { return 8 } +// MandatoryDispatch A transaction with a mandatory dispatch type MandatoryDispatch struct { err string } From ef429413de5ea15a29fca901bebbcdebb1ebb0fa Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 28 Jun 2021 11:49:08 -0600 Subject: [PATCH 125/245] lint and tidy --- lib/babe/errors.go | 88 +++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index bbe27b2f4b..bc9b56e1b4 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -80,23 +80,31 @@ func (e TransactionValidityError) Error() string { return fmt.Sprintf("transaction validity error: %s", e.msg) } +// A UnmarshallingError is when unmarshalling fails +type UnmarshallingError struct { + msg string +} + +func (e UnmarshallingError) Error() string { + return fmt.Sprintf("unmarshalling error: %s", e.msg) +} + // Other Some error occurred type Other string +// Index Returns VDT index func (err Other) Index() uint { return 0 } // CannotLookup Failed to lookup some data -type CannotLookup struct { - err string -} +type CannotLookup struct{} +// Index Returns VDT index func (err CannotLookup) Index() uint { return 1 } // BadOrigin A bad origin -type BadOrigin struct { - err string -} +type BadOrigin struct{} +// Index Returns VDT index func (err BadOrigin) Index() uint { return 2 } // Module A custom error in a module @@ -106,97 +114,89 @@ type Module struct { Message *string } +// Index Returns VDT index func (err Module) Index() uint { return 3 } -func (err Module) String() string { +func (err Module) string() string { return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } // ValidityCannotLookup Could not lookup some information that is required to validate the transaction -type ValidityCannotLookup struct { - err string -} +type ValidityCannotLookup struct{} +// Index Returns VDT index func (err ValidityCannotLookup) Index() uint { return 0 } // NoUnsignedValidator No validator found for the given unsigned transaction -type NoUnsignedValidator struct { - err string -} +type NoUnsignedValidator struct{} +// Index Returns VDT index func (err NoUnsignedValidator) Index() uint { return 1 } // UnknownCustom Any other custom unknown validity that is not covered type UnknownCustom uint8 +// Index Returns VDT index func (err UnknownCustom) Index() uint { return 2 } // Call The call of the transaction is not expected -type Call struct { - err string -} +type Call struct{} +// Index Returns VDT index func (err Call) Index() uint { return 0 } // Payment General error to do with the inability to pay some fees (e.g. account balance too low) -type Payment struct { - err string -} +type Payment struct{} +// Index Returns VDT index func (err Payment) Index() uint { return 1 } // Future General error to do with the transaction not yet being valid (e.g. nonce too high) -type Future struct { - err string -} +type Future struct{} +// Index Returns VDT index func (err Future) Index() uint { return 2 } // Stale General error to do with the transaction being outdated (e.g. nonce too low) -type Stale struct { - err string -} +type Stale struct{} +// Index Returns VDT index func (err Stale) Index() uint { return 3 } // BadProof General error to do with the transaction’s proofs (e.g. signature) -type BadProof struct { - err string -} +type BadProof struct{} +// Index Returns VDT index func (err BadProof) Index() uint { return 4 } // AncientBirthBlock The transaction birth block is ancient -type AncientBirthBlock struct { - err string -} +type AncientBirthBlock struct{} +// Index Returns VDT index func (err AncientBirthBlock) Index() uint { return 5 } // ExhaustsResources The transaction would exhaust the resources of current block -type ExhaustsResources struct { - err string -} +type ExhaustsResources struct{} +// Index Returns VDT index func (err ExhaustsResources) Index() uint { return 6 } // InvalidCustom Any other custom invalid validity that is not covered type InvalidCustom uint8 +// Index Returns VDT index func (err InvalidCustom) Index() uint { return 7 } // BadMandatory An extrinsic with a Mandatory dispatch resulted in Error -type BadMandatory struct { - err string -} +type BadMandatory struct{} +// Index Returns VDT index func (err BadMandatory) Index() uint { return 8 } // MandatoryDispatch A transaction with a mandatory dispatch -type MandatoryDispatch struct { - err string -} +type MandatoryDispatch struct{} +// Index Returns VDT index func (err MandatoryDispatch) Index() uint { return 9 } func determineDispatchErr(res []byte) error { @@ -204,7 +204,7 @@ func determineDispatchErr(res []byte) error { vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) if err != nil { - return errInvalidResult + return &UnmarshallingError{err.Error()} } switch val := vdt.Value().(type) { @@ -215,7 +215,7 @@ func determineDispatchErr(res []byte) error { case BadOrigin: return &DispatchOutcomeError{"bad origin"} case Module: - return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} + return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.string())} } return errInvalidResult @@ -227,7 +227,7 @@ func determineInvalidTxnErr(res []byte) error { ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) err := scale.Unmarshal(res, &vdt) if err != nil { - return errInvalidResult + return &UnmarshallingError{err.Error()} } switch val := vdt.Value().(type) { @@ -261,7 +261,7 @@ func determineUnknownTxnErr(res []byte) error { vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) err := scale.Unmarshal(res, &vdt) if err != nil { - return errInvalidResult + return &UnmarshallingError{err.Error()} } switch val := vdt.Value().(type) { From d1b9934d97127a4fed0d0429b0865c795e7ae3e3 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 28 Jun 2021 13:22:56 -0600 Subject: [PATCH 126/245] use marshal to encode block body --- lib/babe/build.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index 03b6b44a57..3c2d3ecbd2 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -361,5 +361,10 @@ func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) ( extrinsics = append(extrinsics, decExt) } - return types.NewBodyFromExtrinsics(extrinsics) + enc, err := scale.Marshal(extrinsics) + if err != nil { + return nil, err + } + body := types.Body(enc) + return &body, nil } From bb281d4b9d01efc67c48396a240cd3e989575974 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Mon, 28 Jun 2021 18:00:30 -0400 Subject: [PATCH 127/245] chore: replace time.After with time.NewTicker (#1650) * replace time.After with time.NewTimer * replace time.Affer with time.NewTicker * lint * replace time.After is discovery so ttl var is used * replace time.After in if statement * add configuration variables for time duration functions --- dot/network/discovery.go | 8 +++++-- dot/network/sync.go | 48 ++++++++++++++++++++++++-------------- dot/telemetry/telemetry.go | 18 +++++++++----- lib/grandpa/network.go | 4 +++- 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/dot/network/discovery.go b/dot/network/discovery.go index 35f42ad712..6c3c6b1a54 100644 --- a/dot/network/discovery.go +++ b/dot/network/discovery.go @@ -75,13 +75,15 @@ func (d *discovery) start() error { // get all currently connected peers and use them to bootstrap the DHT peers := d.h.Network().Peers() + t := time.NewTicker(startDHTTimeout) + defer t.Stop() for { if len(peers) > 0 { break } select { - case <-time.After(startDHTTimeout): + case <-t.C: logger.Debug("no peers yet, waiting to start DHT...") // wait for peers to connect before starting DHT, otherwise DHT bootstrap nodes // will be empty and we will fail to fill the routing table @@ -169,11 +171,13 @@ func (d *discovery) advertise() { } func (d *discovery) checkPeerCount() { + t := time.NewTicker(connectToPeersTimeout) + defer t.Stop() for { select { case <-d.ctx.Done(): return - case <-time.After(connectToPeersTimeout): + case <-t.C: if len(d.h.Network().Peers()) > d.minPeers { continue } diff --git a/dot/network/sync.go b/dot/network/sync.go index 855e4d6a25..36de6e405d 100644 --- a/dot/network/sync.go +++ b/dot/network/sync.go @@ -88,7 +88,9 @@ const ( badPeerThreshold int = -2 protectedPeerThreshold int = 7 - defaultSlotDuration = time.Second * 6 + defaultSlotDuration = time.Second * 6 + defaultHandleResponseQueueDuration = time.Second + defaultPrunePeersDuration = time.Second * 30 ) var ( @@ -132,26 +134,30 @@ type syncQueue struct { goal int64 // goal block number we are trying to sync to currStart, currEnd int64 // the start and end of the BlockResponse we are currently handling; 0 and 0 if we are not currently handling any - benchmarker *syncBenchmarker + benchmarker *syncBenchmarker + handleResponseQueueDuration time.Duration + prunePeersDuration time.Duration } func newSyncQueue(s *Service) *syncQueue { ctx, cancel := context.WithCancel(s.ctx) return &syncQueue{ - s: s, - slotDuration: defaultSlotDuration, - ctx: ctx, - cancel: cancel, - peerScore: new(sync.Map), - requestData: new(sync.Map), - requestDataByHash: new(sync.Map), - justificationRequestData: new(sync.Map), - requestCh: make(chan *syncRequest, blockRequestBufferSize), - responses: []*types.BlockData{}, - responseCh: make(chan []*types.BlockData, blockResponseBufferSize), - benchmarker: newSyncBenchmarker(), - buf: make([]byte, maxBlockResponseSize), + s: s, + slotDuration: defaultSlotDuration, + ctx: ctx, + cancel: cancel, + peerScore: new(sync.Map), + requestData: new(sync.Map), + requestDataByHash: new(sync.Map), + justificationRequestData: new(sync.Map), + requestCh: make(chan *syncRequest, blockRequestBufferSize), + responses: []*types.BlockData{}, + responseCh: make(chan []*types.BlockData, blockResponseBufferSize), + benchmarker: newSyncBenchmarker(), + buf: make([]byte, maxBlockResponseSize), + handleResponseQueueDuration: defaultHandleResponseQueueDuration, + prunePeersDuration: defaultPrunePeersDuration, } } @@ -176,10 +182,12 @@ func (q *syncQueue) syncAtHead() { q.s.syncer.SetSyncing(true) q.s.noGossip = true // don't gossip messages until we're at the head + t := time.NewTicker(q.slotDuration * 2) + defer t.Stop() for { select { // sleep for average block time TODO: make this configurable from slot duration - case <-time.After(q.slotDuration * 2): + case <-t.C: case <-q.ctx.Done(): return } @@ -214,9 +222,11 @@ func (q *syncQueue) syncAtHead() { } func (q *syncQueue) handleResponseQueue() { + t := time.NewTicker(q.handleResponseQueueDuration) + defer t.Stop() for { select { - case <-time.After(time.Second): + case <-t.C: case <-q.ctx.Done(): return } @@ -260,9 +270,11 @@ func (q *syncQueue) handleResponseQueue() { // prune peers with low score and connect to new peers func (q *syncQueue) prunePeers() { + t := time.NewTicker(q.prunePeersDuration) + defer t.Stop() for { select { - case <-time.After(time.Second * 30): + case <-t.C: case <-q.ctx.Done(): return } diff --git a/dot/telemetry/telemetry.go b/dot/telemetry/telemetry.go index cd11d65207..22ba7286ef 100644 --- a/dot/telemetry/telemetry.go +++ b/dot/telemetry/telemetry.go @@ -40,9 +40,10 @@ type Message struct { // Handler struct for holding telemetry related things type Handler struct { - msg chan Message - connections []*telemetryConnection - log log.Logger + msg chan Message + connections []*telemetryConnection + log log.Logger + sendMessageTimeout time.Duration } // KeyValue object to hold key value pairs used in telemetry messages @@ -56,14 +57,17 @@ var ( handlerInstance *Handler ) +const defaultMessageTimeout = time.Second + // GetInstance singleton pattern to for accessing TelemetryHandler func GetInstance() *Handler { //nolint if handlerInstance == nil { once.Do( func() { handlerInstance = &Handler{ - msg: make(chan Message, 256), - log: log.New("pkg", "telemetry"), + msg: make(chan Message, 256), + log: log.New("pkg", "telemetry"), + sendMessageTimeout: defaultMessageTimeout, } go handlerInstance.startListening() }) @@ -109,10 +113,12 @@ func (h *Handler) AddConnections(conns []*genesis.TelemetryEndpoint) { // SendMessage sends Message to connected telemetry listeners func (h *Handler) SendMessage(msg *Message) error { + t := time.NewTicker(h.sendMessageTimeout) + defer t.Stop() select { case h.msg <- *msg: - case <-time.After(time.Second * 1): + case <-t.C: return errors.New("timeout sending message") } return nil diff --git a/lib/grandpa/network.go b/lib/grandpa/network.go index a5c6c90556..e9f8d441e4 100644 --- a/lib/grandpa/network.go +++ b/lib/grandpa/network.go @@ -177,11 +177,13 @@ func (s *Service) handleNetworkMessage(from peer.ID, msg NotificationsMessage) ( } func (s *Service) sendNeighbourMessage() { + t := time.NewTicker(neighbourMessageInterval) + defer t.Stop() for { select { case <-s.ctx.Done(): return - case <-time.After(neighbourMessageInterval): + case <-t.C: if s.neighbourMessage == nil { continue } From f7429ff15c7f4056165200089c88db51497e9019 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 29 Jun 2021 08:24:58 -0600 Subject: [PATCH 128/245] cr revisions --- lib/babe/errors.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index bc9b56e1b4..b6c3717f4e 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -80,13 +80,13 @@ func (e TransactionValidityError) Error() string { return fmt.Sprintf("transaction validity error: %s", e.msg) } -// A UnmarshallingError is when unmarshalling fails -type UnmarshallingError struct { +// A UnmarshalError is when unmarshalling fails +type UnmarshalError struct { msg string } -func (e UnmarshallingError) Error() string { - return fmt.Sprintf("unmarshalling error: %s", e.msg) +func (e UnmarshalError) Error() string { + return fmt.Sprintf("unmarshal error: %s", e.msg) } // Other Some error occurred @@ -204,7 +204,7 @@ func determineDispatchErr(res []byte) error { vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) if err != nil { - return &UnmarshallingError{err.Error()} + return &UnmarshalError{err.Error()} } switch val := vdt.Value().(type) { @@ -227,7 +227,7 @@ func determineInvalidTxnErr(res []byte) error { ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) err := scale.Unmarshal(res, &vdt) if err != nil { - return &UnmarshallingError{err.Error()} + return &UnmarshalError{err.Error()} } switch val := vdt.Value().(type) { @@ -261,7 +261,7 @@ func determineUnknownTxnErr(res []byte) error { vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) err := scale.Unmarshal(res, &vdt) if err != nil { - return &UnmarshallingError{err.Error()} + return &UnmarshalError{err.Error()} } switch val := vdt.Value().(type) { From a81844ea679198324879e7842d7c873ad7b0fb98 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Wed, 30 Jun 2021 11:48:10 -0400 Subject: [PATCH 129/245] feat(dot/telemetry): implement telemetry message network_state (#1618) * refactor telemetry messages to map format * add basic network state telemetry message * refactor message sender to handle interface{} types * refactor telemetry messages to be structs * lint * go fmt * lint * move msg building logic outside msg sending loop * make telemetry messages an interface * Lookup transactions count from TransactionsState * address comments * fix mocks for tests * lint * refactor TelemetryMessage to Message * update mock handler to return result * add TransactionsCount to mockhandler * move logic to build new network state message * lint * fix interface * update mockhandler * lint --- chain/dev/genesis.json | 9 +- chain/gssmr/genesis.json | 8 +- dot/core/messages.go | 5 + dot/core/messages_test.go | 6 +- dot/network/mock_transaction_handler.go | 14 ++ dot/network/service.go | 33 ++-- dot/network/service_test.go | 1 + dot/network/state.go | 1 + dot/network/test_helpers.go | 1 + dot/network/transaction_test.go | 1 + dot/node.go | 21 ++- dot/sync/syncer.go | 12 +- dot/telemetry/telemetry.go | 226 +++++++++++++++++++----- dot/telemetry/telemetry_test.go | 71 ++++---- 14 files changed, 289 insertions(+), 120 deletions(-) diff --git a/chain/dev/genesis.json b/chain/dev/genesis.json index a5a8ca24e5..6b76f85b45 100644 --- a/chain/dev/genesis.json +++ b/chain/dev/genesis.json @@ -3,7 +3,12 @@ "id": "dev", "chainType": "Local", "bootNodes": [], - "telemetryEndpoints": null, + "telemetryEndpoints": [ + [ + "wss://telemetry.polkadot.io/submit/", + 0 + ] + ], "protocolId": "/gossamer/dev/0", "genesis": { "raw": { @@ -32,4 +37,4 @@ "forkBlocks": null, "badBlocks": null, "consensusEngine": "" -} \ No newline at end of file +} diff --git a/chain/gssmr/genesis.json b/chain/gssmr/genesis.json index 2e6f157d90..080619e282 100644 --- a/chain/gssmr/genesis.json +++ b/chain/gssmr/genesis.json @@ -3,6 +3,12 @@ "id": "gssmr", "chainType": "Local", "bootNodes": [], + "telemetryEndpoints": [ + [ + "wss://telemetry.polkadot.io/submit/", + 0 + ] + ], "protocolId": "/gossamer/gssmr/0", "genesis": { "raw": { @@ -40,4 +46,4 @@ "forkBlocks": null, "badBlocks": null, "consensusEngine": "" -} \ No newline at end of file +} diff --git a/dot/core/messages.go b/dot/core/messages.go index 9fa5e97b51..318414eab8 100644 --- a/dot/core/messages.go +++ b/dot/core/messages.go @@ -57,3 +57,8 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (boo return len(msg.Extrinsics) > 0, nil } + +// TransactionsCount returns number for pending transactions in pool +func (s *Service) TransactionsCount() int { + return len(s.transactionState.PendingInPool()) +} diff --git a/dot/core/messages_test.go b/dot/core/messages_test.go index 2717d3704e..6b718b0d5c 100644 --- a/dot/core/messages_test.go +++ b/dot/core/messages_test.go @@ -21,7 +21,7 @@ import ( "testing" "time" - . "github.com/ChainSafe/gossamer/dot/core/mocks" + . "github.com/ChainSafe/gossamer/dot/core/mocks" // nolint "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/types" @@ -38,7 +38,7 @@ import ( func TestService_ProcessBlockAnnounceMessage(t *testing.T) { // TODO: move to sync package - net := new(MockNetwork) + net := new(MockNetwork) // nolint cfg := &Config{ Network: net, @@ -136,7 +136,7 @@ func TestService_HandleTransactionMessage(t *testing.T) { ks := keystore.NewGlobalKeystore() ks.Acco.Insert(kp) - bp := new(MockBlockProducer) + bp := new(MockBlockProducer) // nolint blockC := make(chan types.Block) bp.On("GetBlockChannel", nil).Return(blockC) diff --git a/dot/network/mock_transaction_handler.go b/dot/network/mock_transaction_handler.go index c3eaa07972..5fe1d7e597 100644 --- a/dot/network/mock_transaction_handler.go +++ b/dot/network/mock_transaction_handler.go @@ -29,3 +29,17 @@ func (_m *MockTransactionHandler) HandleTransactionMessage(_a0 *TransactionMessa return r0, r1 } + +// TransactionsCount provides a mock function with given fields: +func (_m *MockTransactionHandler) TransactionsCount() int { + ret := _m.Called() + + var r0 int + if rf, ok := ret.Get(0).(func() int); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(int) + } + + return r0 +} diff --git a/dot/network/service.go b/dot/network/service.go index 738c1e0fb3..429ec79c52 100644 --- a/dot/network/service.go +++ b/dot/network/service.go @@ -20,6 +20,7 @@ import ( "context" "errors" "io" + "math/big" "os" "sync" "time" @@ -315,11 +316,12 @@ main: case <-ticker.C: o := s.host.bwc.GetBandwidthTotals() - err := telemetry.GetInstance().SendMessage(telemetry.NewTelemetryMessage( - telemetry.NewKeyValue("bandwidth_download", o.RateIn), - telemetry.NewKeyValue("bandwidth_upload", o.RateOut), - telemetry.NewKeyValue("msg", "system.interval"), - telemetry.NewKeyValue("peers", s.host.peerCount()))) + err := telemetry.GetInstance().SendMessage(telemetry.NewBandwidthTM(o.RateIn, o.RateOut, s.host.peerCount())) + if err != nil { + logger.Debug("problem sending system.interval telemetry message", "error", err) + } + + err = telemetry.GetInstance().SendMessage(telemetry.NewNetworkStateTM(s.host.h, s.Peers())) if err != nil { logger.Debug("problem sending system.interval telemetry message", "error", err) } @@ -333,19 +335,22 @@ func (s *Service) sentBlockIntervalTelemetry() { if err != nil { continue } + bestHash := best.Hash() + finalized, err := s.blockState.GetFinalizedHeader(0, 0) //nolint if err != nil { continue } - - err = telemetry.GetInstance().SendMessage(telemetry.NewTelemetryMessage( - telemetry.NewKeyValue("best", best.Hash().String()), - telemetry.NewKeyValue("finalized_hash", finalized.Hash().String()), //nolint - telemetry.NewKeyValue("finalized_height", finalized.Number), //nolint - telemetry.NewKeyValue("height", best.Number), - telemetry.NewKeyValue("msg", "system.interval"), - telemetry.NewKeyValue("txcount", 0), // todo (ed) determine where to get tx count - telemetry.NewKeyValue("used_state_cache_size", 0))) // todo (ed) determine where to get used_state_cache_size + finalizedHash := finalized.Hash() + + err = telemetry.GetInstance().SendMessage(telemetry.NewBlockIntervalTM( + &bestHash, + best.Number, + &finalizedHash, + finalized.Number, + big.NewInt(int64(s.transactionHandler.TransactionsCount())), + big.NewInt(0), // todo (ed) determine where to get used_state_cache_size + )) if err != nil { logger.Debug("problem sending system.interval telemetry message", "error", err) } diff --git a/dot/network/service_test.go b/dot/network/service_test.go index db20565b1d..cbc44f64fd 100644 --- a/dot/network/service_test.go +++ b/dot/network/service_test.go @@ -84,6 +84,7 @@ func createTestService(t *testing.T, cfg *Config) (srvc *Service) { if cfg.TransactionHandler == nil { mocktxhandler := &MockTransactionHandler{} mocktxhandler.On("HandleTransactionMessage", mock.AnythingOfType("*TransactionMessage")).Return(nil) + mocktxhandler.On("TransactionsCount").Return(0) cfg.TransactionHandler = mocktxhandler } diff --git a/dot/network/state.go b/dot/network/state.go index 61c777526b..323fdf2f02 100644 --- a/dot/network/state.go +++ b/dot/network/state.go @@ -56,4 +56,5 @@ type Syncer interface { // TransactionHandler is the interface used by the transactions sub-protocol type TransactionHandler interface { HandleTransactionMessage(*TransactionMessage) (bool, error) + TransactionsCount() int } diff --git a/dot/network/test_helpers.go b/dot/network/test_helpers.go index d5ff38f2ff..cf513983ec 100644 --- a/dot/network/test_helpers.go +++ b/dot/network/test_helpers.go @@ -59,6 +59,7 @@ func NewMockSyncer() *MockSyncer { func NewMockTransactionHandler() *MockTransactionHandler { mocktxhandler := new(MockTransactionHandler) mocktxhandler.On("HandleTransactionMessage", mock.AnythingOfType("*network.TransactionMessage")).Return(nil) + mocktxhandler.On("TransactionsCount").Return(0) return mocktxhandler } diff --git a/dot/network/transaction_test.go b/dot/network/transaction_test.go index cb78794c31..41b2f38d63 100644 --- a/dot/network/transaction_test.go +++ b/dot/network/transaction_test.go @@ -56,6 +56,7 @@ func TestHandleTransactionMessage(t *testing.T) { basePath := utils.NewTestBasePath(t, "nodeA") mockhandler := &MockTransactionHandler{} mockhandler.On("HandleTransactionMessage", mock.AnythingOfType("*network.TransactionMessage")).Return(true, nil) + mockhandler.On("TransactionsCount").Return(0) config := &Config{ BasePath: basePath, diff --git a/dot/node.go b/dot/node.go index 768999bb40..9af3efdb8a 100644 --- a/dot/node.go +++ b/dot/node.go @@ -345,17 +345,16 @@ func NewNode(cfg *Config, ks *keystore.GlobalKeystore, stopFunc func()) (*Node, } telemetry.GetInstance().AddConnections(gd.TelemetryEndpoints) - - err = telemetry.GetInstance().SendMessage(telemetry.NewTelemetryMessage( - telemetry.NewKeyValue("authority", cfg.Core.GrandpaAuthority), - telemetry.NewKeyValue("chain", sysSrvc.ChainName()), - telemetry.NewKeyValue("genesis_hash", stateSrvc.Block.GenesisHash().String()), - telemetry.NewKeyValue("implementation", sysSrvc.SystemName()), - telemetry.NewKeyValue("msg", "system.connected"), - telemetry.NewKeyValue("name", cfg.Global.Name), - telemetry.NewKeyValue("network_id", networkSrvc.NetworkState().PeerID), - telemetry.NewKeyValue("startup_time", strconv.FormatInt(time.Now().UnixNano(), 10)), - telemetry.NewKeyValue("version", sysSrvc.SystemVersion()))) + genesisHash := stateSrvc.Block.GenesisHash() + err = telemetry.GetInstance().SendMessage(telemetry.NewSystemConnectedTM( + cfg.Core.GrandpaAuthority, + sysSrvc.ChainName(), + &genesisHash, + sysSrvc.SystemName(), + cfg.Global.Name, + networkSrvc.NetworkState().PeerID, + strconv.FormatInt(time.Now().UnixNano(), 10), + sysSrvc.SystemVersion())) if err != nil { logger.Debug("problem sending system.connected telemetry message", "err", err) } diff --git a/dot/sync/syncer.go b/dot/sync/syncer.go index c6e3b5cf5b..9ac360ed3d 100644 --- a/dot/sync/syncer.go +++ b/dot/sync/syncer.go @@ -346,13 +346,13 @@ func (s *Service) handleBlock(block *types.Block) error { logger.Debug("🔗 imported block", "number", block.Header.Number, "hash", block.Header.Hash()) - err = telemetry.GetInstance().SendMessage(telemetry.NewTelemetryMessage( // nolint - telemetry.NewKeyValue("best", block.Header.Hash().String()), - telemetry.NewKeyValue("height", block.Header.Number.Uint64()), - telemetry.NewKeyValue("msg", "block.import"), - telemetry.NewKeyValue("origin", "NetworkInitialSync"))) + blockHash := block.Header.Hash() + err = telemetry.GetInstance().SendMessage(telemetry.NewBlockImportTM( + &blockHash, + block.Header.Number, + "NetworkInitialSync")) if err != nil { - logger.Trace("problem sending block.import telemetry message", "error", err) + logger.Debug("problem sending block.import telemetry message", "error", err) } return nil diff --git a/dot/telemetry/telemetry.go b/dot/telemetry/telemetry.go index 22ba7286ef..2df26725b7 100644 --- a/dot/telemetry/telemetry.go +++ b/dot/telemetry/telemetry.go @@ -19,12 +19,16 @@ package telemetry import ( "encoding/json" "errors" + "fmt" + "math/big" "sync" "time" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" log "github.com/ChainSafe/log15" "github.com/gorilla/websocket" + libp2phost "github.com/libp2p/go-libp2p-core/host" ) type telemetryConnection struct { @@ -33,11 +37,6 @@ type telemetryConnection struct { sync.Mutex } -// Message struct to hold telemetry message data -type Message struct { - values map[string]interface{} -} - // Handler struct for holding telemetry related things type Handler struct { msg chan Message @@ -46,12 +45,6 @@ type Handler struct { sendMessageTimeout time.Duration } -// KeyValue object to hold key value pairs used in telemetry messages -type KeyValue struct { - key string - value interface{} -} - var ( once sync.Once handlerInstance *Handler @@ -75,25 +68,6 @@ func GetInstance() *Handler { //nolint return handlerInstance } -// NewTelemetryMessage builds a telemetry message -func NewTelemetryMessage(values ...*KeyValue) *Message { //nolint - mvals := make(map[string]interface{}) - for _, v := range values { - mvals[v.key] = v.value - } - return &Message{ - values: mvals, - } -} - -// NewKeyValue builds a key value pair for telemetry messages -func NewKeyValue(key string, value interface{}) *KeyValue { //nolint - return &KeyValue{ - key: key, - value: value, - } -} - // AddConnections adds the given telemetry endpoint as listeners that will receive telemetry data func (h *Handler) AddConnections(conns []*genesis.TelemetryEndpoint) { for _, v := range conns { @@ -112,11 +86,11 @@ func (h *Handler) AddConnections(conns []*genesis.TelemetryEndpoint) { } // SendMessage sends Message to connected telemetry listeners -func (h *Handler) SendMessage(msg *Message) error { +func (h *Handler) SendMessage(msg Message) error { t := time.NewTicker(h.sendMessageTimeout) defer t.Stop() select { - case h.msg <- *msg: + case h.msg <- msg: case <-t.C: return errors.New("timeout sending message") @@ -128,33 +102,191 @@ func (h *Handler) startListening() { for { msg := <-h.msg go func() { + msgBytes, err := h.msgToJSON(msg) + if err != nil { + h.log.Debug("issue decoding telemetry message", "error", err) + return + } for _, conn := range h.connections { conn.Lock() - err := conn.wsconn.WriteMessage(websocket.TextMessage, msgToBytes(msg)) + defer conn.Unlock() + + err = conn.wsconn.WriteMessage(websocket.TextMessage, msgBytes) if err != nil { h.log.Warn("issue while sending telemetry message", "error", err) } - conn.Unlock() } }() } } -type response struct { - ID int `json:"id"` - Payload map[string]interface{} `json:"payload"` - Timestamp time.Time `json:"ts"` -} +func (h *Handler) msgToJSON(message Message) ([]byte, error) { + messageBytes, err := json.Marshal(message) + if err != nil { + return nil, err + } -func msgToBytes(message Message) []byte { - res := response{ - ID: 1, // todo (ed) determine how this is used - Payload: message.values, - Timestamp: time.Now(), + messageMap := make(map[string]interface{}) + err = json.Unmarshal(messageBytes, &messageMap) + if err != nil { + return nil, err } - resB, err := json.Marshal(res) + + messageMap["ts"] = time.Now() + + messageMap["msg"] = message.messageType() + + fullRes, err := json.Marshal(messageMap) if err != nil { - return nil + return nil, err } - return resB + return fullRes, nil +} + +// Message interface for Message functions +type Message interface { + messageType() string +} + +// SystemConnectedTM struct to hold system connected telemetry messages +type SystemConnectedTM struct { + Authority bool `json:"authority"` + Chain string `json:"chain"` + GenesisHash *common.Hash `json:"genesis_hash"` + Implementation string `json:"implementation"` + Msg string `json:"msg"` + Name string `json:"name"` + NetworkID string `json:"network_id"` + StartupTime string `json:"startup_time"` + Version string `json:"version"` +} + +// NewSystemConnectedTM function to create new System Connected Telemetry Message +func NewSystemConnectedTM(authority bool, chain string, genesisHash *common.Hash, + implementation, name, networkID, startupTime, version string) *SystemConnectedTM { + return &SystemConnectedTM{ + Authority: authority, + Chain: chain, + GenesisHash: genesisHash, + Implementation: implementation, + Msg: "system.connected", + Name: name, + NetworkID: networkID, + StartupTime: startupTime, + Version: version, + } +} +func (tm *SystemConnectedTM) messageType() string { + return tm.Msg +} + +// BlockImportTM struct to hold block import telemetry messages +type BlockImportTM struct { + BestHash *common.Hash `json:"best"` + Height *big.Int `json:"height"` + Msg string `json:"msg"` + Origin string `json:"origin"` +} + +// NewBlockImportTM function to create new Block Import Telemetry Message +func NewBlockImportTM(bestHash *common.Hash, height *big.Int, origin string) *BlockImportTM { + return &BlockImportTM{ + BestHash: bestHash, + Height: height, + Msg: "block.import", + Origin: origin, + } +} + +func (tm *BlockImportTM) messageType() string { + return tm.Msg +} + +// SystemIntervalTM struct to hold system interval telemetry messages +type SystemIntervalTM struct { + BandwidthDownload float64 `json:"bandwidth_download,omitempty"` + BandwidthUpload float64 `json:"bandwidth_upload,omitempty"` + Msg string `json:"msg"` + Peers int `json:"peers,omitempty"` + BestHash *common.Hash `json:"best,omitempty"` + BestHeight *big.Int `json:"height,omitempty"` + FinalisedHash *common.Hash `json:"finalized_hash,omitempty"` // nolint + FinalisedHeight *big.Int `json:"finalized_height,omitempty"` // nolint + TxCount *big.Int `json:"txcount,omitempty"` + UsedStateCacheSize *big.Int `json:"used_state_cache_size,omitempty"` +} + +// NewBandwidthTM function to create new Bandwidth Telemetry Message +func NewBandwidthTM(bandwidthDownload, bandwidthUpload float64, peers int) *SystemIntervalTM { + return &SystemIntervalTM{ + BandwidthDownload: bandwidthDownload, + BandwidthUpload: bandwidthUpload, + Msg: "system.interval", + Peers: peers, + } +} + +// NewBlockIntervalTM function to create new Block Interval Telemetry Message +func NewBlockIntervalTM(beshHash *common.Hash, bestHeight *big.Int, finalisedHash *common.Hash, + finalisedHeight, txCount, usedStateCacheSize *big.Int) *SystemIntervalTM { + return &SystemIntervalTM{ + Msg: "system.interval", + BestHash: beshHash, + BestHeight: bestHeight, + FinalisedHash: finalisedHash, + FinalisedHeight: finalisedHeight, + TxCount: txCount, + UsedStateCacheSize: usedStateCacheSize, + } +} + +func (tm *SystemIntervalTM) messageType() string { + return tm.Msg +} + +type peerInfo struct { + Roles byte `json:"roles"` + BestHash string `json:"bestHash"` + BestNumber uint64 `json:"bestNumber"` +} + +// NetworkStateTM struct to hold network state telemetry messages +type NetworkStateTM struct { + Msg string `json:"msg"` + State map[string]interface{} `json:"state"` +} + +// NewNetworkStateTM function to create new Network State Telemetry Message +func NewNetworkStateTM(host libp2phost.Host, peerInfos []common.PeerInfo) *NetworkStateTM { + netState := make(map[string]interface{}) + netState["peerId"] = host.ID() + hostAddrs := []string{} + for _, v := range host.Addrs() { + hostAddrs = append(hostAddrs, v.String()) + } + netState["externalAddressess"] = hostAddrs + listAddrs := []string{} + for _, v := range host.Network().ListenAddresses() { + listAddrs = append(listAddrs, fmt.Sprintf("%s/p2p/%s", v, host.ID())) + } + netState["listenedAddressess"] = listAddrs + + peers := make(map[string]interface{}) + for _, v := range peerInfos { + p := &peerInfo{ + Roles: v.Roles, + BestHash: v.BestHash.String(), + BestNumber: v.BestNumber, + } + peers[v.PeerID] = *p + } + netState["connectedPeers"] = peers + + return &NetworkStateTM{ + Msg: "system.network_state", + State: netState, + } +} +func (tm *NetworkStateTM) messageType() string { + return tm.Msg } diff --git a/dot/telemetry/telemetry_test.go b/dot/telemetry/telemetry_test.go index e2b7b9c05b..f9e606b9f8 100644 --- a/dot/telemetry/telemetry_test.go +++ b/dot/telemetry/telemetry_test.go @@ -2,6 +2,7 @@ package telemetry import ( "bytes" + "fmt" "log" "math/big" "net/http" @@ -11,6 +12,7 @@ import ( "testing" "time" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" "github.com/gorilla/websocket" "github.com/stretchr/testify/require" @@ -46,55 +48,44 @@ func TestHandler_SendMulti(t *testing.T) { resultCh = make(chan []byte) go func() { - GetInstance().SendMessage(NewTelemetryMessage( - NewKeyValue("authority", false), - NewKeyValue("chain", "chain"), - NewKeyValue("genesis_hash", "hash"), - NewKeyValue("implementation", "systemName"), - NewKeyValue("msg", "system.connected"), - NewKeyValue("name", "nodeName"), - NewKeyValue("network_id", "netID"), - NewKeyValue("startup_time", "startTime"), - NewKeyValue("version", "version"))) + genesisHash := common.MustHexToHash("0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3") + + GetInstance().SendMessage(NewSystemConnectedTM(false, "chain", &genesisHash, + "systemName", "nodeName", "netID", "startTime", "0.1")) + wg.Done() }() go func() { - GetInstance().SendMessage(NewTelemetryMessage( - NewKeyValue("best", "hash"), - NewKeyValue("height", big.NewInt(2)), - NewKeyValue("msg", "block.import"), - NewKeyValue("origin", "NetworkInitialSync"))) + bh := common.MustHexToHash("0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6") + GetInstance().SendMessage(NewBlockImportTM(&bh, big.NewInt(2), "NetworkInitialSync")) + wg.Done() }() go func() { - GetInstance().SendMessage(NewTelemetryMessage( - NewKeyValue("bandwidth_download", 2), - NewKeyValue("bandwidth_upload", 3), - NewKeyValue("msg", "system.interval"), - NewKeyValue("peers", 1))) + GetInstance().SendMessage(NewBandwidthTM(2, 3, 1)) + wg.Done() }() go func() { - GetInstance().SendMessage(NewTelemetryMessage( - NewKeyValue("best", "0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6"), - NewKeyValue("finalized_hash", "0x687197c11b4cf95374159843e7f46fbcd63558db981aaef01a8bac2a44a1d6b2"), // nolint - NewKeyValue("finalized_height", 32256), NewKeyValue("height", 32375), // nolint - NewKeyValue("msg", "system.interval"), NewKeyValue("txcount", 2), - NewKeyValue("used_state_cache_size", 1886357))) + bestHash := common.MustHexToHash("0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6") + finalisedHash := common.MustHexToHash("0x687197c11b4cf95374159843e7f46fbcd63558db981aaef01a8bac2a44a1d6b2") + GetInstance().SendMessage(NewBlockIntervalTM(&bestHash, big.NewInt(32375), &finalisedHash, + big.NewInt(32256), big.NewInt(0), big.NewInt(1234))) + wg.Done() }() wg.Wait() - expected1 := []byte(`{"id":1,"payload":{"bandwidth_download":2,"bandwidth_upload":3,"msg":"system.interval","peers":1},"ts":`) - expected2 := []byte(`{"id":1,"payload":{"best":"hash","height":2,"msg":"block.import","origin":"NetworkInitialSync"},"ts":`) - expected3 := []byte(`{"id":1,"payload":{"authority":false,"chain":"chain","genesis_hash":"hash","implementation":"systemName","msg":"system.connected","name":"nodeName","network_id":"netID","startup_time":"startTime","version":"version"},"ts":`) - expected4 := []byte(`{"id":1,"payload":{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","finalized_hash":"0x687197c11b4cf95374159843e7f46fbcd63558db981aaef01a8bac2a44a1d6b2","finalized_height":32256,"height":32375,"msg":"system.interval","txcount":2,"used_state_cache_size":1886357},"ts":`) // nolint + expected1 := []byte(`{"authority":false,"chain":"chain","genesis_hash":"0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3","implementation":"systemName","msg":"system.connected","name":"nodeName","network_id":"netID","startup_time":"startTime","ts":`) + expected2 := []byte(`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","height":2,"msg":"block.import","origin":"NetworkInitialSync","ts":`) + expected3 := []byte(`{"bandwidth_download":2,"bandwidth_upload":3,"msg":"system.interval","peers":1,"ts":`) + expected4 := []byte(`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","finalized_hash":"0x687197c11b4cf95374159843e7f46fbcd63558db981aaef01a8bac2a44a1d6b2","finalized_height":32256,"height":32375,"msg":"system.interval","ts":`) // nolint - expected := [][]byte{expected3, expected1, expected4, expected2} + expected := [][]byte{expected1, expected3, expected4, expected2} var actual [][]byte for data := range resultCh { @@ -121,11 +112,9 @@ func TestListenerConcurrency(t *testing.T) { resultCh = make(chan []byte) for i := 0; i < qty; i++ { go func() { - GetInstance().SendMessage(NewTelemetryMessage( - NewKeyValue("best", "hash"), - NewKeyValue("height", big.NewInt(2)), - NewKeyValue("msg", "block.import"), - NewKeyValue("origin", "NetworkInitialSync"))) + bestHash := common.Hash{} + GetInstance().SendMessage(NewBlockImportTM(&bestHash, big.NewInt(2), "NetworkInitialSync")) + wg.Done() }() } @@ -139,6 +128,16 @@ func TestListenerConcurrency(t *testing.T) { } } +// TestInfiniteListener starts loop that print out data received on websocket ws://localhost:8001/ +// this can be useful to see what data is sent to telemetry server +func TestInfiniteListener(t *testing.T) { + t.Skip() + resultCh = make(chan []byte) + for data := range resultCh { + fmt.Printf("Data %s\n", data) + } +} + func listen(w http.ResponseWriter, r *http.Request) { c, err := upgrader.Upgrade(w, r, nil) if err != nil { From 6797e77e269c70204ab03ee69f0fe48f9e402342 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 30 Jun 2021 13:15:14 -0600 Subject: [PATCH 130/245] WIP/use results for unmarshalling --- lib/babe/errors.go | 68 +++++++++++++++++++++++++++------- lib/babe/errors_test.go | 2 + pkg/scale/decode.go | 4 ++ pkg/scale/varying_data_type.go | 4 ++ 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index b6c3717f4e..0cc84924ef 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -199,15 +199,30 @@ type MandatoryDispatch struct{} // Index Returns VDT index func (err MandatoryDispatch) Index() uint { return 9 } -func determineDispatchErr(res []byte) error { - var e Other - vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) - err := scale.Unmarshal(res, &vdt) - if err != nil { - return &UnmarshalError{err.Error()} - } +//func determineDispatchErr(res []byte) error { +// var e Other +// vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) +// err := scale.Unmarshal(res, &vdt) +// if err != nil { +// return &UnmarshalError{err.Error()} +// } +// +// switch val := vdt.Value().(type) { +// case Other: +// return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} +// case CannotLookup: +// return &DispatchOutcomeError{"failed lookup"} +// case BadOrigin: +// return &DispatchOutcomeError{"bad origin"} +// case Module: +// return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.string())} +// } +// +// return errInvalidResult +//} - switch val := vdt.Value().(type) { +func determineDispatchErr(i interface{}) error { + switch val := i.(type) { case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case CannotLookup: @@ -276,17 +291,42 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } +//Result{ +//OK: Result{ OK: nil, Err: VaryingDataType(string, FailedLookup, BadOrigin, Other?) }. +//Err: VaryingDataType( VaryingDataType{all the invalid tx errors}, VaryingDataType{TxValidityErros}, +//} func determineErr(res []byte) error { switch res[0] { case 0: - switch res[1] { - case 0: - return nil - case 1: - return determineDispatchErr(res[2:]) - default: + var e Other + vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) + r := scale.NewResult(nil, vdt) + err := scale.Unmarshal(res[1:], &r) + if err != nil { + //fmt.Println("error1") + return err + } + + ok, err := r.Unwrap() + //fmt.Println(err) + if err != nil { + er := determineDispatchErr(err) + //fmt.Println(er) + return er + } + //fmt.Println(ok) + if ok != nil { return errInvalidResult } + return nil + //switch res[1] { + //case 0: + // return nil + //case 1: + // return determineDispatchErr(res[2:]) + //default: + // return errInvalidResult + //} case 1: switch res[1] { case 0: diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index c495cde251..921f30168f 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -1,6 +1,7 @@ package babe import ( + "fmt" "testing" "github.com/stretchr/testify/require" @@ -71,6 +72,7 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } + fmt.Println(err) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index f08029485e..16bb1e0dcd 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -321,12 +321,16 @@ func (ds *decodeState) decodeVaryingDataTypeSlice(dstv reflect.Value) (err error func (ds *decodeState) decodeVaryingDataType(dstv reflect.Value) (err error) { var b byte b, err = ds.ReadByte() + fmt.Println("expected index") + fmt.Println(b) if err != nil { return } vdt := dstv.Interface().(VaryingDataType) val, ok := vdt.cache[uint(b)] + fmt.Println("cache at index") + fmt.Println(vdt.cache[uint(b)]) if !ok { err = fmt.Errorf("unable to find VaryingDataTypeValue with index: %d", uint(b)) return diff --git a/pkg/scale/varying_data_type.go b/pkg/scale/varying_data_type.go index de524014e2..4f6438f11e 100644 --- a/pkg/scale/varying_data_type.go +++ b/pkg/scale/varying_data_type.go @@ -90,12 +90,16 @@ func NewVaryingDataType(values ...VaryingDataTypeValue) (vdt VaryingDataType, er } vdt.cache = make(map[uint]VaryingDataTypeValue) for _, value := range values { + //fmt.Println("index") + //fmt.Println(value.Index()) _, ok := vdt.cache[value.Index()] if ok { err = fmt.Errorf("duplicate index with VaryingDataType: %T with index: %d", value, value.Index()) return } vdt.cache[value.Index()] = value + //fmt.Println("value at cache") + //fmt.Println(vdt.cache[value.Index()]) } return } From 902d83d20ad1e292ba3e10e984adc40233ba054d Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 30 Jun 2021 15:36:37 -0400 Subject: [PATCH 131/245] fixed it --- lib/babe/errors.go | 51 +++++++++++++++-------- lib/babe/errors_test.go | 90 ++++++++++++++++++++--------------------- pkg/scale/decode.go | 2 + 3 files changed, 81 insertions(+), 62 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 0cc84924ef..4742e459df 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -300,6 +300,7 @@ func determineErr(res []byte) error { case 0: var e Other vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) + r := scale.NewResult(nil, vdt) err := scale.Unmarshal(res[1:], &r) if err != nil { @@ -308,25 +309,41 @@ func determineErr(res []byte) error { } ok, err := r.Unwrap() - //fmt.Println(err) if err != nil { - er := determineDispatchErr(err) - //fmt.Println(er) - return er - } - //fmt.Println(ok) - if ok != nil { - return errInvalidResult + // error case + fmt.Printf("err %v\n", err) + switch err := err.(type) { + case scale.WrappedErr: + vdt := err.Err.(scale.VaryingDataType) + switch val := vdt.Value().(type) { + case Module, BadOrigin, CannotLookup, Other: + fmt.Printf("in here! %T %v\n", val, val) + } + + } + } else { + // ok case + fmt.Printf("ok %v\n", ok) } - return nil - //switch res[1] { - //case 0: - // return nil - //case 1: - // return determineDispatchErr(res[2:]) - //default: - // return errInvalidResult - //} + // //fmt.Println(err) + // if err != nil { + // er := determineDispatchErr(err) + // //fmt.Println(er) + // return er + // } + // //fmt.Println(ok) + // if ok != nil { + // return errInvalidResult + // } + // return nil + // //switch res[1] { + // //case 0: + // // return nil + // //case 1: + // // return determineDispatchErr(res[2:]) + // //default: + // // return errInvalidResult + // //} case 1: switch res[1] { case 0: diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 921f30168f..07cb6d3d98 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -13,56 +13,56 @@ func TestApplyExtrinsicErrors(t *testing.T) { test []byte expected string }{ - { - name: "Valid extrinsic", - test: []byte{0, 0}, - expected: "", - }, + // { + // name: "Valid extrinsic", + // test: []byte{0, 0}, + // expected: "", + // }, { name: "Dispatch custom module error empty", test: []byte{0, 1, 3, 4, 5, 1, 0}, expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: ", }, - { - name: "Dispatch custom module error", - test: []byte{0, 1, 3, 4, 5, 1, 0x04, 0x65}, - expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: 65", - }, - { - name: "Dispatch unknown error", - test: []byte{0, 1, 0, 0x04, 65}, - expected: "dispatch outcome error: unknown error: A", - }, - { - name: "Dispatch failed lookup", - test: []byte{0, 1, 1}, - expected: "dispatch outcome error: failed lookup", - }, - { - name: "Dispatch bad origin", - test: []byte{0, 1, 2}, - expected: "dispatch outcome error: bad origin", - }, - { - name: "Invalid txn payment error", - test: []byte{1, 0, 1}, - expected: "transaction validity error: invalid payment", - }, - { - name: "Invalid txn payment error", - test: []byte{1, 0, 7, 65}, - expected: "transaction validity error: unknown error: 65", - }, - { - name: "Unknown txn lookup failed error", - test: []byte{1, 1, 0}, - expected: "transaction validity error: lookup failed", - }, - { - name: "Invalid txn unknown error", - test: []byte{1, 1, 2, 75}, - expected: "transaction validity error: unknown error: 75", - }, + // { + // name: "Dispatch custom module error", + // test: []byte{0, 1, 3, 4, 5, 1, 0x04, 0x65}, + // expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: 65", + // }, + // { + // name: "Dispatch unknown error", + // test: []byte{0, 1, 0, 0x04, 65}, + // expected: "dispatch outcome error: unknown error: A", + // }, + // { + // name: "Dispatch failed lookup", + // test: []byte{0, 1, 1}, + // expected: "dispatch outcome error: failed lookup", + // }, + // { + // name: "Dispatch bad origin", + // test: []byte{0, 1, 2}, + // expected: "dispatch outcome error: bad origin", + // }, + // { + // name: "Invalid txn payment error", + // test: []byte{1, 0, 1}, + // expected: "transaction validity error: invalid payment", + // }, + // { + // name: "Invalid txn payment error", + // test: []byte{1, 0, 7, 65}, + // expected: "transaction validity error: unknown error: 65", + // }, + // { + // name: "Unknown txn lookup failed error", + // test: []byte{1, 1, 0}, + // expected: "transaction validity error: lookup failed", + // }, + // { + // name: "Invalid txn unknown error", + // test: []byte{1, 1, 2, 75}, + // expected: "transaction validity error: unknown error: 75", + // }, } for _, c := range testCases { diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 16bb1e0dcd..7b80ace43e 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -240,6 +240,7 @@ func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { switch rb { case 0x00: tempElem := reflect.New(reflect.TypeOf(res.ok)) + tempElem.Elem().Set(reflect.ValueOf(res.ok)) err = ds.unmarshal(tempElem.Elem()) if err != nil { return @@ -251,6 +252,7 @@ func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { dstv.Set(reflect.ValueOf(res)) case 0x01: tempElem := reflect.New(reflect.TypeOf(res.err)) + tempElem.Elem().Set(reflect.ValueOf(res.err)) err = ds.unmarshal(tempElem.Elem()) if err != nil { return From 94f3b2f9a58cff17efb1bbfa238c105a67536bf9 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 30 Jun 2021 14:09:52 -0600 Subject: [PATCH 132/245] use result type for dispatch error --- lib/babe/errors.go | 38 ++------------ lib/babe/errors_test.go | 90 +++++++++++++++++----------------- pkg/scale/decode.go | 4 -- pkg/scale/varying_data_type.go | 4 -- 4 files changed, 50 insertions(+), 86 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 4742e459df..98aac2bd47 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -221,8 +221,8 @@ func (err MandatoryDispatch) Index() uint { return 9 } // return errInvalidResult //} -func determineDispatchErr(i interface{}) error { - switch val := i.(type) { +func determineDispatchErr(vdt scale.VaryingDataType) error { + switch val := vdt.Value().(type) { case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case CannotLookup: @@ -300,50 +300,22 @@ func determineErr(res []byte) error { case 0: var e Other vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) - r := scale.NewResult(nil, vdt) err := scale.Unmarshal(res[1:], &r) if err != nil { - //fmt.Println("error1") return err } - ok, err := r.Unwrap() + _, err = r.Unwrap() if err != nil { - // error case - fmt.Printf("err %v\n", err) switch err := err.(type) { case scale.WrappedErr: vdt := err.Err.(scale.VaryingDataType) - switch val := vdt.Value().(type) { - case Module, BadOrigin, CannotLookup, Other: - fmt.Printf("in here! %T %v\n", val, val) - } - + return determineDispatchErr(vdt) } } else { - // ok case - fmt.Printf("ok %v\n", ok) + return nil } - // //fmt.Println(err) - // if err != nil { - // er := determineDispatchErr(err) - // //fmt.Println(er) - // return er - // } - // //fmt.Println(ok) - // if ok != nil { - // return errInvalidResult - // } - // return nil - // //switch res[1] { - // //case 0: - // // return nil - // //case 1: - // // return determineDispatchErr(res[2:]) - // //default: - // // return errInvalidResult - // //} case 1: switch res[1] { case 0: diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 07cb6d3d98..921f30168f 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -13,56 +13,56 @@ func TestApplyExtrinsicErrors(t *testing.T) { test []byte expected string }{ - // { - // name: "Valid extrinsic", - // test: []byte{0, 0}, - // expected: "", - // }, + { + name: "Valid extrinsic", + test: []byte{0, 0}, + expected: "", + }, { name: "Dispatch custom module error empty", test: []byte{0, 1, 3, 4, 5, 1, 0}, expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: ", }, - // { - // name: "Dispatch custom module error", - // test: []byte{0, 1, 3, 4, 5, 1, 0x04, 0x65}, - // expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: 65", - // }, - // { - // name: "Dispatch unknown error", - // test: []byte{0, 1, 0, 0x04, 65}, - // expected: "dispatch outcome error: unknown error: A", - // }, - // { - // name: "Dispatch failed lookup", - // test: []byte{0, 1, 1}, - // expected: "dispatch outcome error: failed lookup", - // }, - // { - // name: "Dispatch bad origin", - // test: []byte{0, 1, 2}, - // expected: "dispatch outcome error: bad origin", - // }, - // { - // name: "Invalid txn payment error", - // test: []byte{1, 0, 1}, - // expected: "transaction validity error: invalid payment", - // }, - // { - // name: "Invalid txn payment error", - // test: []byte{1, 0, 7, 65}, - // expected: "transaction validity error: unknown error: 65", - // }, - // { - // name: "Unknown txn lookup failed error", - // test: []byte{1, 1, 0}, - // expected: "transaction validity error: lookup failed", - // }, - // { - // name: "Invalid txn unknown error", - // test: []byte{1, 1, 2, 75}, - // expected: "transaction validity error: unknown error: 75", - // }, + { + name: "Dispatch custom module error", + test: []byte{0, 1, 3, 4, 5, 1, 0x04, 0x65}, + expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: 65", + }, + { + name: "Dispatch unknown error", + test: []byte{0, 1, 0, 0x04, 65}, + expected: "dispatch outcome error: unknown error: A", + }, + { + name: "Dispatch failed lookup", + test: []byte{0, 1, 1}, + expected: "dispatch outcome error: failed lookup", + }, + { + name: "Dispatch bad origin", + test: []byte{0, 1, 2}, + expected: "dispatch outcome error: bad origin", + }, + { + name: "Invalid txn payment error", + test: []byte{1, 0, 1}, + expected: "transaction validity error: invalid payment", + }, + { + name: "Invalid txn payment error", + test: []byte{1, 0, 7, 65}, + expected: "transaction validity error: unknown error: 65", + }, + { + name: "Unknown txn lookup failed error", + test: []byte{1, 1, 0}, + expected: "transaction validity error: lookup failed", + }, + { + name: "Invalid txn unknown error", + test: []byte{1, 1, 2, 75}, + expected: "transaction validity error: unknown error: 75", + }, } for _, c := range testCases { diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 7b80ace43e..68f43405b6 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -323,16 +323,12 @@ func (ds *decodeState) decodeVaryingDataTypeSlice(dstv reflect.Value) (err error func (ds *decodeState) decodeVaryingDataType(dstv reflect.Value) (err error) { var b byte b, err = ds.ReadByte() - fmt.Println("expected index") - fmt.Println(b) if err != nil { return } vdt := dstv.Interface().(VaryingDataType) val, ok := vdt.cache[uint(b)] - fmt.Println("cache at index") - fmt.Println(vdt.cache[uint(b)]) if !ok { err = fmt.Errorf("unable to find VaryingDataTypeValue with index: %d", uint(b)) return diff --git a/pkg/scale/varying_data_type.go b/pkg/scale/varying_data_type.go index 4f6438f11e..de524014e2 100644 --- a/pkg/scale/varying_data_type.go +++ b/pkg/scale/varying_data_type.go @@ -90,16 +90,12 @@ func NewVaryingDataType(values ...VaryingDataTypeValue) (vdt VaryingDataType, er } vdt.cache = make(map[uint]VaryingDataTypeValue) for _, value := range values { - //fmt.Println("index") - //fmt.Println(value.Index()) _, ok := vdt.cache[value.Index()] if ok { err = fmt.Errorf("duplicate index with VaryingDataType: %T with index: %d", value, value.Index()) return } vdt.cache[value.Index()] = value - //fmt.Println("value at cache") - //fmt.Println(vdt.cache[value.Index()]) } return } From 7f377487c8ddf1631939f935a830de4c1fd8a8f1 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 30 Jun 2021 17:16:30 -0600 Subject: [PATCH 133/245] WIP/Finish result implementation --- lib/babe/errors.go | 160 ++++++++++++++++++++++++++++++--------------- 1 file changed, 106 insertions(+), 54 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 98aac2bd47..90934ec119 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -199,28 +199,7 @@ type MandatoryDispatch struct{} // Index Returns VDT index func (err MandatoryDispatch) Index() uint { return 9 } -//func determineDispatchErr(res []byte) error { -// var e Other -// vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) -// err := scale.Unmarshal(res, &vdt) -// if err != nil { -// return &UnmarshalError{err.Error()} -// } -// -// switch val := vdt.Value().(type) { -// case Other: -// return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} -// case CannotLookup: -// return &DispatchOutcomeError{"failed lookup"} -// case BadOrigin: -// return &DispatchOutcomeError{"bad origin"} -// case Module: -// return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.string())} -// } -// -// return errInvalidResult -//} - +// Consolidate these into one function func determineDispatchErr(vdt scale.VaryingDataType) error { switch val := vdt.Value().(type) { case Other: @@ -236,15 +215,7 @@ func determineDispatchErr(vdt scale.VaryingDataType) error { return errInvalidResult } -func determineInvalidTxnErr(res []byte) error { - var c InvalidCustom - vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, - ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) - err := scale.Unmarshal(res, &vdt) - if err != nil { - return &UnmarshalError{err.Error()} - } - +func determineInvalidTxnErr(vdt scale.VaryingDataType) error { switch val := vdt.Value().(type) { case Call: return &TransactionValidityError{"call of the transaction is not expected"} @@ -291,38 +262,119 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } + +type ValidityVdt struct{ + vdt scale.VaryingDataType +} +func (err ValidityVdt) Index() uint { return 0 } + +type UnknownVdt struct{ + vdt scale.VaryingDataType +} +func (err UnknownVdt) Index() uint { return 1 } + //Result{ //OK: Result{ OK: nil, Err: VaryingDataType(string, FailedLookup, BadOrigin, Other?) }. //Err: VaryingDataType( VaryingDataType{all the invalid tx errors}, VaryingDataType{TxValidityErros}, //} func determineErr(res []byte) error { - switch res[0] { - case 0: - var e Other - vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) - r := scale.NewResult(nil, vdt) - err := scale.Unmarshal(res[1:], &r) - if err != nil { - return err - } + // Dispatch + var other Other + dispatchVdt := scale.MustNewVaryingDataType(other, CannotLookup{}, BadOrigin{}, Module{}) + // Invalid + var invalidCustom InvalidCustom + var validityVdt ValidityVdt + validityVdt.vdt = scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, + ExhaustsResources{}, invalidCustom, BadMandatory{}, MandatoryDispatch{}) + // Unknown + var unknownCustom UnknownCustom + var unknownVdt UnknownVdt + unknownVdt.vdt = scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, unknownCustom) + // Ok result + okRes := scale.NewResult(nil, dispatchVdt) + + // Big boy result + result := scale.NewResult(okRes, scale.MustNewVaryingDataType(validityVdt, unknownCustom)) + err := scale.Unmarshal(res, &result) + if err != nil { + return err + } - _, err = r.Unwrap() - if err != nil { - switch err := err.(type) { - case scale.WrappedErr: - vdt := err.Err.(scale.VaryingDataType) - return determineDispatchErr(vdt) + ok, err := result.Unwrap() + if err != nil { // Error case is a vdt of 2 vdts + fmt.Printf("err %v\n", err) + switch err := err.(type) { + case scale.WrappedErr: + fmt.Printf("ERROR wrapped error \n") + vdt := err.Err.(scale.VaryingDataType) + fmt.Printf("Error type: %T\n", vdt.Value()) + switch val := vdt.Value().(type) { + case ValidityVdt: + // This part returns invalid error so gotta figure that out + fmt.Printf("ValidityVdt \n") + return determineInvalidTxnErr(val.vdt) + case UnknownVdt: + // Not finding the index here + fmt.Printf("UnknownVdt \n") + default: + fmt.Printf("in here! %T %v\n", val, val) } - } else { - return nil + default: + fmt.Printf("Shouldnt reach here: %v\n", err) } - case 1: - switch res[1] { - case 0: - return determineInvalidTxnErr(res[2:]) - case 1: - return determineUnknownTxnErr(res[2:]) + + } else { // okay case is a result + fmt.Printf("ok type %T\n", ok) + switch o := ok.(type) { + case scale.Result: + fmt.Printf("OK result \n") + ok, err = o.Unwrap() + if err != nil { + fmt.Printf("This should be dispatch error \n") + switch err := err.(type) { + case scale.WrappedErr: + vdt := err.Err.(scale.VaryingDataType) + return determineDispatchErr(vdt) + } + } else { // No error + fmt.Printf("No error \n") + return nil + } + default: + fmt.Printf("Shouldnt reach here: %v\n", o) } } + + //switch res[0] { + //case 0: + // r := scale.NewResult(nil, dispatchVdt) + // err := scale.Unmarshal(res[1:], &r) + // if err != nil { + // return err + // } + // + // _, err = r.Unwrap() + // if err != nil { + // switch err := err.(type) { + // case scale.WrappedErr: + // vdt := err.Err.(scale.VaryingDataType) + // return determineDispatchErr(vdt) + // } + // } else { + // return nil + // } + //case 1: + // switch res[1] { + // case 0: + // //var c InvalidCustom + // //vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, + // // ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) + // return determineInvalidTxnErr(res[2:]) + // case 1: + // //var c UnknownCustom + // //vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) + // return determineUnknownTxnErr(res[2:]) + // } + //} return errInvalidResult } From baebb9cb4190e13ddfd14f827e1f4d6a877ab026 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 30 Jun 2021 17:52:10 -0600 Subject: [PATCH 134/245] WIP/Fix decoding custom VDT types --- lib/babe/errors.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 90934ec119..ef258de4d7 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -237,6 +237,8 @@ func determineInvalidTxnErr(vdt scale.VaryingDataType) error { return &TransactionValidityError{"mandatory dispatch error"} case MandatoryDispatch: return &TransactionValidityError{"invalid mandatory dispatch"} + default: + fmt.Printf("Default %T\n", val) } return errInvalidResult @@ -308,11 +310,13 @@ func determineErr(res []byte) error { fmt.Printf("ERROR wrapped error \n") vdt := err.Err.(scale.VaryingDataType) fmt.Printf("Error type: %T\n", vdt.Value()) + // This doesnt seem to be being decoding correctly with custom VDTs switch val := vdt.Value().(type) { case ValidityVdt: // This part returns invalid error so gotta figure that out - fmt.Printf("ValidityVdt \n") - return determineInvalidTxnErr(val.vdt) + fmt.Printf("ValidityVdt %T\n", val.vdt) + fmt.Printf("ValidityVdt %v\n", val.vdt) + //return determineInvalidTxnErr(val.vdt) case UnknownVdt: // Not finding the index here fmt.Printf("UnknownVdt \n") From 67ef09d1494d62d7475c1075ac2db1949e464d66 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 1 Jul 2021 09:42:39 -0600 Subject: [PATCH 135/245] use results for unmarshalling dispatchErrors --- lib/babe/errors.go | 149 ++++++++++----------------------------------- 1 file changed, 32 insertions(+), 117 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index ef258de4d7..fc7696fe63 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -199,7 +199,6 @@ type MandatoryDispatch struct{} // Index Returns VDT index func (err MandatoryDispatch) Index() uint { return 9 } -// Consolidate these into one function func determineDispatchErr(vdt scale.VaryingDataType) error { switch val := vdt.Value().(type) { case Other: @@ -215,7 +214,14 @@ func determineDispatchErr(vdt scale.VaryingDataType) error { return errInvalidResult } -func determineInvalidTxnErr(vdt scale.VaryingDataType) error { +func determineInvalidTxnErr(res []byte) error { + var c InvalidCustom + vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, + ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) + err := scale.Unmarshal(res, &vdt) + if err != nil { + return &UnmarshalError{err.Error()} + } switch val := vdt.Value().(type) { case Call: return &TransactionValidityError{"call of the transaction is not expected"} @@ -237,10 +243,7 @@ func determineInvalidTxnErr(vdt scale.VaryingDataType) error { return &TransactionValidityError{"mandatory dispatch error"} case MandatoryDispatch: return &TransactionValidityError{"invalid mandatory dispatch"} - default: - fmt.Printf("Default %T\n", val) } - return errInvalidResult } @@ -251,7 +254,6 @@ func determineUnknownTxnErr(res []byte) error { if err != nil { return &UnmarshalError{err.Error()} } - switch val := vdt.Value().(type) { case ValidityCannotLookup: return &TransactionValidityError{"lookup failed"} @@ -260,125 +262,38 @@ func determineUnknownTxnErr(res []byte) error { case UnknownCustom: return &TransactionValidityError{fmt.Sprintf("unknown error: %d", val)} } - return errInvalidResult } - -type ValidityVdt struct{ - vdt scale.VaryingDataType -} -func (err ValidityVdt) Index() uint { return 0 } - -type UnknownVdt struct{ - vdt scale.VaryingDataType -} -func (err UnknownVdt) Index() uint { return 1 } - -//Result{ -//OK: Result{ OK: nil, Err: VaryingDataType(string, FailedLookup, BadOrigin, Other?) }. -//Err: VaryingDataType( VaryingDataType{all the invalid tx errors}, VaryingDataType{TxValidityErros}, -//} +// TODO wrap all errors in a Result to unmarshall func determineErr(res []byte) error { - // Dispatch - var other Other - dispatchVdt := scale.MustNewVaryingDataType(other, CannotLookup{}, BadOrigin{}, Module{}) - // Invalid - var invalidCustom InvalidCustom - var validityVdt ValidityVdt - validityVdt.vdt = scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, - ExhaustsResources{}, invalidCustom, BadMandatory{}, MandatoryDispatch{}) - // Unknown - var unknownCustom UnknownCustom - var unknownVdt UnknownVdt - unknownVdt.vdt = scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, unknownCustom) - // Ok result - okRes := scale.NewResult(nil, dispatchVdt) - - // Big boy result - result := scale.NewResult(okRes, scale.MustNewVaryingDataType(validityVdt, unknownCustom)) - err := scale.Unmarshal(res, &result) - if err != nil { - return err - } - - ok, err := result.Unwrap() - if err != nil { // Error case is a vdt of 2 vdts - fmt.Printf("err %v\n", err) - switch err := err.(type) { - case scale.WrappedErr: - fmt.Printf("ERROR wrapped error \n") - vdt := err.Err.(scale.VaryingDataType) - fmt.Printf("Error type: %T\n", vdt.Value()) - // This doesnt seem to be being decoding correctly with custom VDTs - switch val := vdt.Value().(type) { - case ValidityVdt: - // This part returns invalid error so gotta figure that out - fmt.Printf("ValidityVdt %T\n", val.vdt) - fmt.Printf("ValidityVdt %v\n", val.vdt) - //return determineInvalidTxnErr(val.vdt) - case UnknownVdt: - // Not finding the index here - fmt.Printf("UnknownVdt \n") - default: - fmt.Printf("in here! %T %v\n", val, val) - } - default: - fmt.Printf("Shouldnt reach here: %v\n", err) + switch res[0] { + case 0: + var e Other + vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) + r := scale.NewResult(nil, vdt) + err := scale.Unmarshal(res[1:], &r) + if err != nil { + return err } - } else { // okay case is a result - fmt.Printf("ok type %T\n", ok) - switch o := ok.(type) { - case scale.Result: - fmt.Printf("OK result \n") - ok, err = o.Unwrap() - if err != nil { - fmt.Printf("This should be dispatch error \n") - switch err := err.(type) { - case scale.WrappedErr: - vdt := err.Err.(scale.VaryingDataType) - return determineDispatchErr(vdt) - } - } else { // No error - fmt.Printf("No error \n") - return nil + _, err = r.Unwrap() + if err != nil { + switch err := err.(type) { + case scale.WrappedErr: + vdt := err.Err.(scale.VaryingDataType) + return determineDispatchErr(vdt) } - default: - fmt.Printf("Shouldnt reach here: %v\n", o) + } else { + return nil + } + case 1: + switch res[1] { + case 0: + return determineInvalidTxnErr(res[2:]) + case 1: + return determineUnknownTxnErr(res[2:]) } } - - //switch res[0] { - //case 0: - // r := scale.NewResult(nil, dispatchVdt) - // err := scale.Unmarshal(res[1:], &r) - // if err != nil { - // return err - // } - // - // _, err = r.Unwrap() - // if err != nil { - // switch err := err.(type) { - // case scale.WrappedErr: - // vdt := err.Err.(scale.VaryingDataType) - // return determineDispatchErr(vdt) - // } - // } else { - // return nil - // } - //case 1: - // switch res[1] { - // case 0: - // //var c InvalidCustom - // //vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, - // // ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) - // return determineInvalidTxnErr(res[2:]) - // case 1: - // //var c UnknownCustom - // //vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) - // return determineUnknownTxnErr(res[2:]) - // } - //} return errInvalidResult } From ce86b82741a35ebbf473c5bf9639276da41952d3 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 1 Jul 2021 10:51:22 -0600 Subject: [PATCH 136/245] cleanup --- lib/babe/errors.go | 2 ++ lib/babe/errors_test.go | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index fc7696fe63..b7a44cb96f 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -283,6 +283,8 @@ func determineErr(res []byte) error { case scale.WrappedErr: vdt := err.Err.(scale.VaryingDataType) return determineDispatchErr(vdt) + default: + return &UnmarshalError{"Unexpected result error type"} } } else { return nil diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 921f30168f..c495cde251 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -1,7 +1,6 @@ package babe import ( - "fmt" "testing" "github.com/stretchr/testify/require" @@ -72,7 +71,6 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } - fmt.Println(err) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) From cbbec0afed9709da9891f0b388b6b374e610609a Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 30 Apr 2021 12:11:04 -0400 Subject: [PATCH 137/245] add mtx --- pkg/scale/scale_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index a96ab2b539..8f25da5400 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -17,6 +17,8 @@ package scale import ( + "bytes" + "math/big" "reflect" "testing" ) From e08ebc9239e18c9113f3778ca810f0d353a47696 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 4 May 2021 10:57:34 -0400 Subject: [PATCH 138/245] add variable data type interface, test to compare old vs new encoding --- pkg/scale/scale.go | 7 ++++ pkg/scale/scale_test.go | 90 ----------------------------------------- 2 files changed, 7 insertions(+), 90 deletions(-) delete mode 100644 pkg/scale/scale_test.go diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 855016b8ca..ac6530bae3 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -24,6 +24,13 @@ import ( "sync" ) +type VaryingDataType []VaryingDataTypeValue + +// VaryingDataType is used to represent scale encodable types +type VaryingDataTypeValue interface { + Index() uint +} + // package level cache for fieldScaleIndicies var cache = &fieldScaleIndicesCache{ cache: make(map[string]fieldScaleIndices), diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go deleted file mode 100644 index 8f25da5400..0000000000 --- a/pkg/scale/scale_test.go +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package scale - -import ( - "bytes" - "math/big" - "reflect" - "testing" -) - -func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { - tests := []struct { - name string - in interface{} - wantIndices fieldScaleIndices - wantErr bool - }{ - { - in: struct{ Foo int }{}, - wantIndices: fieldScaleIndices{ - { - fieldIndex: 0, - }, - }, - }, - { - in: struct { - End1 bool - Baz bool `scale:"3"` - End2 []byte - Bar int32 `scale:"2"` - End3 []byte - Foo []byte `scale:"1"` - }{}, - wantIndices: fieldScaleIndices{ - { - fieldIndex: 5, - scaleIndex: newStringPtr("1"), - }, - { - fieldIndex: 3, - scaleIndex: newStringPtr("2"), - }, - { - fieldIndex: 1, - scaleIndex: newStringPtr("3"), - }, - { - fieldIndex: 0, - }, - { - fieldIndex: 2, - }, - { - fieldIndex: 4, - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fsic := &fieldScaleIndicesCache{ - cache: make(map[string]fieldScaleIndices), - } - _, gotIndices, err := fsic.fieldScaleIndices(tt.in) - if (err != nil) != tt.wantErr { - t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(gotIndices, tt.wantIndices) { - t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() gotIndices = %v, want %v", gotIndices, tt.wantIndices) - } - }) - } -} From be12db4126dd17e9c71e123d2789a8c3b2bef9d4 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 6 May 2021 11:29:34 -0400 Subject: [PATCH 139/245] wip --- pkg/scale/decode.go | 12 ++++ pkg/scale/decode_test.go | 118 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index f08029485e..9c90e3e93f 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -647,3 +647,15 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { dstv.Set(reflect.ValueOf(ui128)) return } + +// decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 +// if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise +// it returns nil and error +func (ds *decodeState) decodeUint128() (out *Uint128, err error) { + buf := make([]byte, 16) + err = binary.Read(ds, binary.LittleEndian, buf) + if err != nil { + return nil, err + } + return NewUint128(buf) +} diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 22412b7d8e..24481cdb85 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -238,3 +238,121 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { }) } } + +func Test_decodeState_decodeBigInt(t *testing.T) { + var ( + bi *big.Int + ) + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + name: "error case, ensure **big.Int", + args: args{ + data: []byte{0x00}, + dst: bi, + }, + wantErr: true, + }, + { + args: args{ + data: []byte{0x00}, + dst: &bi, + }, + want: big.NewInt(0), + }, + { + args: args{ + data: []byte{0x04}, + dst: &bi, + }, + want: big.NewInt(1), + }, + { + args: args{ + data: []byte{0xa8}, + dst: &bi, + }, + want: big.NewInt(42), + }, + { + args: args{ + data: []byte{0x01, 0x01}, + dst: &bi, + }, + want: big.NewInt(64), + }, + { + args: args{ + data: []byte{0x15, 0x01}, + dst: &bi, + }, + want: big.NewInt(69), + }, + { + args: args{ + data: []byte{0xfd, 0xff}, + dst: &bi, + }, + want: big.NewInt(16383), + }, + { + args: args{ + data: []byte{0x02, 0x00, 0x01, 0x00}, + dst: &bi, + }, + want: big.NewInt(16384), + }, + { + args: args{ + data: []byte{0xfe, 0xff, 0xff, 0xff}, + dst: &bi, + }, + want: big.NewInt(1073741823), + }, + { + args: args{ + data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, + dst: &bi, + }, + want: big.NewInt(1073741824), + }, + { + args: args{ + data: []byte{0x03, 0xff, 0xff, 0xff, 0xff}, + dst: &bi, + }, + want: big.NewInt(1<<32 - 1), + }, + { + args: args{ + data: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, + dst: &bi, + }, + want: big.NewInt(1 << 32), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From e2cabfb5bb7268dd34785bb42225bea00b5d5476 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 14 May 2021 12:14:16 -0400 Subject: [PATCH 140/245] []byte, string decoding --- pkg/scale/decode.go | 4 +- pkg/scale/decode_test.go | 117 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 9c90e3e93f..b8ccff16a9 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -490,7 +490,7 @@ func (ds *decodeState) decodeBytes(dstv reflect.Value) (err error) { return } - b := make([]byte, length) + b = make([]byte, length) _, err = ds.Read(b) if err != nil { return @@ -651,7 +651,7 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { // decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 // if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise // it returns nil and error -func (ds *decodeState) decodeUint128() (out *Uint128, err error) { +func (ds *decodeState) decodeUint128() (ui *Uint128, err error) { buf := make([]byte, 16) err = binary.Read(ds, binary.LittleEndian, buf) if err != nil { diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 24481cdb85..fced26e432 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -356,3 +356,120 @@ func Test_decodeState_decodeBigInt(t *testing.T) { }) } } + +func Test_decodeState_decodeBytes(t *testing.T) { + var b []byte + var s string + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + args: args{ + data: []byte{0x04, 0x01}, + dst: &b, + }, + want: []byte{0x01}, + }, + { + args: args{ + data: []byte{0x04, 0xff}, + dst: &b, + }, + want: []byte{0xff}, + }, + { + args: args{ + data: []byte{0x08, 0x01, 0x01}, + dst: &b, + }, + want: []byte{0x01, 0x01}, + }, + { + args: args{ + data: append([]byte{0x01, 0x01}, byteArray(64)...), + dst: &b, + }, + want: byteArray(64), + }, + { + args: args{ + data: append([]byte{0xfd, 0xff}, byteArray(16383)...), + dst: &b, + }, + want: byteArray(16383), + }, + { + args: args{ + data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), + dst: &b, + }, + want: byteArray(16384), + }, + // string + { + args: args{ + data: []byte{0x04, 0x01}, + dst: &s, + }, + want: string([]byte{0x01}), + }, + { + args: args{ + data: []byte{0x04, 0xff}, + dst: &s, + }, + want: string([]byte{0xff}), + }, + { + args: args{ + data: []byte{0x08, 0x01, 0x01}, + dst: &s, + }, + want: string([]byte{0x01, 0x01}), + }, + { + args: args{ + data: append([]byte{0x01, 0x01}, byteArray(64)...), + dst: &s, + }, + want: string(byteArray(64)), + }, + { + args: args{ + data: append([]byte{0xfd, 0xff}, byteArray(16383)...), + dst: &s, + }, + want: string(byteArray(16383)), + }, + { + args: args{ + data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), + dst: &s, + }, + want: string(byteArray(16384)), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From d22fe73168970b8962c2edeab3a58ca3514f20ad Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 14 May 2021 13:32:09 -0400 Subject: [PATCH 141/245] encodeBool and tests --- pkg/scale/decode_test.go | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index fced26e432..964958dc3b 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -473,3 +473,56 @@ func Test_decodeState_decodeBytes(t *testing.T) { }) } } + +func Test_decodeState_decodeBool(t *testing.T) { + var b bool + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + args: args{ + data: []byte{0x01}, + dst: &b, + }, + want: true, + }, + { + args: args{ + data: []byte{0x00}, + dst: &b, + }, + want: false, + }, + { + name: "error case", + args: args{ + data: []byte{0x03}, + dst: &b, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From 907ad962bb3ac64f9b14092ebda923ffda17abe2 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 19 May 2021 22:24:58 -0400 Subject: [PATCH 142/245] refactor tests, include optionality tests --- pkg/scale/encode_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 4f9deedbd4..146f07ed74 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -169,6 +169,8 @@ var ( in: uint(9223372036854775807), want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + int64Tests = tests{ { name: "myCustomUint(9223372036854775807)", in: myCustomUint(9223372036854775807), @@ -198,6 +200,8 @@ var ( in: int64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + uint64Tests = tests{ { name: "myCustomInt64(9223372036854775807)", in: myCustomInt64(9223372036854775807), @@ -225,6 +229,8 @@ var ( in: uint64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + int32Tests = tests{ { name: "myCustomUint64(9223372036854775807)", in: myCustomUint64(9223372036854775807), @@ -269,6 +275,8 @@ var ( in: uint32(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f}, }, + } + int8Tests = tests{ { name: "uint32(1073741823)", in: myCustomUint32(1073741823), @@ -293,6 +301,8 @@ var ( in: uint8(1), want: []byte{0x01}, }, + } + int16Tests = tests{ { name: "myCustomInt8(1)", in: myCustomUint8(1), @@ -310,6 +320,8 @@ var ( in: int16(16383), want: []byte{0xff, 0x3f}, }, + } + uint16Tests = tests{ { name: "myCustomInt16(16383)", in: myCustomInt16(16383), From b975029cabc7939f811e8bdb24dc1a967d9c0dfb Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 21 May 2021 17:17:04 -0400 Subject: [PATCH 143/245] use shared tests in decode --- pkg/scale/decode_test.go | 330 ++++++++------------------------------- 1 file changed, 68 insertions(+), 262 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 964958dc3b..a9e459af22 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -17,7 +17,6 @@ package scale import ( - "math/big" "reflect" "testing" @@ -240,289 +239,96 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { } func Test_decodeState_decodeBigInt(t *testing.T) { - var ( - bi *big.Int - ) - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - name: "error case, ensure **big.Int", - args: args{ - data: []byte{0x00}, - dst: bi, - }, - wantErr: true, - }, - { - args: args{ - data: []byte{0x00}, - dst: &bi, - }, - want: big.NewInt(0), - }, - { - args: args{ - data: []byte{0x04}, - dst: &bi, - }, - want: big.NewInt(1), - }, - { - args: args{ - data: []byte{0xa8}, - dst: &bi, - }, - want: big.NewInt(42), - }, - { - args: args{ - data: []byte{0x01, 0x01}, - dst: &bi, - }, - want: big.NewInt(64), - }, - { - args: args{ - data: []byte{0x15, 0x01}, - dst: &bi, - }, - want: big.NewInt(69), - }, - { - args: args{ - data: []byte{0xfd, 0xff}, - dst: &bi, - }, - want: big.NewInt(16383), - }, - { - args: args{ - data: []byte{0x02, 0x00, 0x01, 0x00}, - dst: &bi, - }, - want: big.NewInt(16384), - }, - { - args: args{ - data: []byte{0xfe, 0xff, 0xff, 0xff}, - dst: &bi, - }, - want: big.NewInt(1073741823), - }, - { - args: args{ - data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, - dst: &bi, - }, - want: big.NewInt(1073741824), - }, - { - args: args{ - data: []byte{0x03, 0xff, 0xff, 0xff, 0xff}, - dst: &bi, - }, - want: big.NewInt(1<<32 - 1), - }, - { - args: args{ - data: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, - dst: &bi, - }, - want: big.NewInt(1 << 32), - }, - } - for _, tt := range tests { + for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } func Test_decodeState_decodeBytes(t *testing.T) { - var b []byte - var s string - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - args: args{ - data: []byte{0x04, 0x01}, - dst: &b, - }, - want: []byte{0x01}, - }, - { - args: args{ - data: []byte{0x04, 0xff}, - dst: &b, - }, - want: []byte{0xff}, - }, - { - args: args{ - data: []byte{0x08, 0x01, 0x01}, - dst: &b, - }, - want: []byte{0x01, 0x01}, - }, - { - args: args{ - data: append([]byte{0x01, 0x01}, byteArray(64)...), - dst: &b, - }, - want: byteArray(64), - }, - { - args: args{ - data: append([]byte{0xfd, 0xff}, byteArray(16383)...), - dst: &b, - }, - want: byteArray(16383), - }, - { - args: args{ - data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), - dst: &b, - }, - want: byteArray(16384), - }, - // string - { - args: args{ - data: []byte{0x04, 0x01}, - dst: &s, - }, - want: string([]byte{0x01}), - }, - { - args: args{ - data: []byte{0x04, 0xff}, - dst: &s, - }, - want: string([]byte{0xff}), - }, - { - args: args{ - data: []byte{0x08, 0x01, 0x01}, - dst: &s, - }, - want: string([]byte{0x01, 0x01}), - }, - { - args: args{ - data: append([]byte{0x01, 0x01}, byteArray(64)...), - dst: &s, - }, - want: string(byteArray(64)), - }, - { - args: args{ - data: append([]byte{0xfd, 0xff}, byteArray(16383)...), - dst: &s, - }, - want: string(byteArray(16383)), - }, - { - args: args{ - data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), - dst: &s, - }, - want: string(byteArray(16384)), - }, - } - for _, tt := range tests { + for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return } - if err != nil { - return - } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } func Test_decodeState_decodeBool(t *testing.T) { - var b bool - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - args: args{ - data: []byte{0x01}, - dst: &b, - }, - want: true, - }, - { - args: args{ - data: []byte{0x00}, - dst: &b, - }, - want: false, - }, - { - name: "error case", - args: args{ - data: []byte{0x03}, - dst: &b, - }, - wantErr: true, - }, - } - for _, tt := range tests { + for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } + +// func Test_decodeState_decodeBool(t *testing.T) { +// var b bool +// type args struct { +// data []byte +// dst interface{} +// } +// tests := []struct { +// name string +// args args +// wantErr bool +// want interface{} +// }{ +// { +// args: args{ +// data: []byte{0x01}, +// dst: &b, +// }, +// want: true, +// }, +// { +// args: args{ +// data: []byte{0x00}, +// dst: &b, +// }, +// want: false, +// }, +// { +// name: "error case", +// args: args{ +// data: []byte{0x03}, +// dst: &b, +// }, +// wantErr: true, +// }, +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// err := Unmarshal(tt.args.data, tt.args.dst) +// if (err != nil) != tt.wantErr { +// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// return +// } +// if err != nil { +// return +// } +// got := reflect.ValueOf(tt.args.dst).Elem().Interface() +// if !reflect.DeepEqual(got, tt.want) { +// t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) +// } +// }) +// } +// } From 396bbfa215e09b04b33a3c932b6c70d4477ee567 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 26 May 2021 11:25:29 -0400 Subject: [PATCH 144/245] struct decode tests, and unmarshal refactor --- pkg/scale/decode_test.go | 82 ++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 53 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index a9e459af22..3594b35fc8 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -17,6 +17,7 @@ package scale import ( + "math/big" "reflect" "testing" @@ -253,7 +254,7 @@ func Test_decodeState_decodeBigInt(t *testing.T) { } func Test_decodeState_decodeBytes(t *testing.T) { - for _, tt := range boolTests { + for _, tt := range stringTests { t.Run(tt.name, func(t *testing.T) { dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { @@ -280,55 +281,30 @@ func Test_decodeState_decodeBool(t *testing.T) { } } -// func Test_decodeState_decodeBool(t *testing.T) { -// var b bool -// type args struct { -// data []byte -// dst interface{} -// } -// tests := []struct { -// name string -// args args -// wantErr bool -// want interface{} -// }{ -// { -// args: args{ -// data: []byte{0x01}, -// dst: &b, -// }, -// want: true, -// }, -// { -// args: args{ -// data: []byte{0x00}, -// dst: &b, -// }, -// want: false, -// }, -// { -// name: "error case", -// args: args{ -// data: []byte{0x03}, -// dst: &b, -// }, -// wantErr: true, -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// err := Unmarshal(tt.args.data, tt.args.dst) -// if (err != nil) != tt.wantErr { -// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// if err != nil { -// return -// } -// got := reflect.ValueOf(tt.args.dst).Elem().Interface() -// if !reflect.DeepEqual(got, tt.want) { -// t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) -// } -// }) -// } -// } +func Test_decodeState_decodeStruct(t *testing.T) { + for _, tt := range structTests { + t.Run(tt.name, func(t *testing.T) { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + switch reflect.ValueOf(tt.in).Kind() { + case reflect.Ptr: + if reflect.ValueOf(dst).IsZero() { + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } else { + // have to do this since reflect.DeepEqual won't come back true for different addresses + if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } + default: + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } + }) + } +} From 6909b268e73db121bd4e500b0a2cad95da68ce9d Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 27 May 2021 16:00:27 -0400 Subject: [PATCH 145/245] wip --- pkg/scale/decode_test.go | 131 +++++++++++++++++++++++++++++++++------ pkg/scale/encode_test.go | 13 ++++ 2 files changed, 124 insertions(+), 20 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 3594b35fc8..dc8e43ecae 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -242,7 +242,8 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { func Test_decodeState_decodeBigInt(t *testing.T) { for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + var dst *big.Int + // dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -270,7 +271,8 @@ func Test_decodeState_decodeBytes(t *testing.T) { func Test_decodeState_decodeBool(t *testing.T) { for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + // dst := reflect.ValueOf(tt.in).Interface() + var dst bool if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -281,29 +283,118 @@ func Test_decodeState_decodeBool(t *testing.T) { } } +func Test_decodeState_decodeStructManual(t *testing.T) { + // nil case + var dst *MyStruct = nil + var b = []byte{0} + var want *MyStruct = nil + + // dst = structTests[0].dst + + err := Unmarshal(b, &dst) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + + // zero case MyStruct + var dst1 *MyStruct = &MyStruct{} + err = Unmarshal(b, &dst1) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst1, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + + // zero case MyStruct + var dst2 *MyStruct = &MyStruct{Baz: true} + err = Unmarshal(b, &dst2) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst2, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + +} + func Test_decodeState_decodeStruct(t *testing.T) { - for _, tt := range structTests { + for _, tt := range append([]test{}, structTests[0]) { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + // dst := tt.dst if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - switch reflect.ValueOf(tt.in).Kind() { - case reflect.Ptr: - if reflect.ValueOf(dst).IsZero() { - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - } else { - // have to do this since reflect.DeepEqual won't come back true for different addresses - if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - } - default: - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } + // dstv := reflect.ValueOf(dst) + // inv := reflect.ValueOf(tt.in) + + // if dstv.Kind() != inv.Kind() { + // t.Errorf("decodeState.unmarshal() differing kind = %T, want %T", dst, tt.in) + // return + // } + + // switch inv.Kind() { + // case reflect.Ptr: + // fmt.Println(dst, dstv.Interface(), tt.in, inv.Interface()) + // if inv.IsZero() { + // fmt.Println(dst, tt.in, dstv.Interface(), inv.Interface()) + // if !reflect.DeepEqual(dstv.Interface(), tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } else if inv.IsNil() { + // if !reflect.DeepEqual(dst, tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } else { + // // // have to do this since reflect.DeepEqual won't come back true for different addresses + // // if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { + // // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // // } + // if !reflect.DeepEqual(dst, inv) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } + // default: + // if !reflect.DeepEqual(dst, tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} +func Test_decodeState_decodeArray(t *testing.T) { + for _, tt := range arrayTests { + t.Run(tt.name, func(t *testing.T) { + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} + +func Test_decodeState_decodeSlice(t *testing.T) { + for _, tt := range sliceTests { + t.Run(tt.name, func(t *testing.T) { + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 146f07ed74..f7ea5e9309 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -26,6 +26,7 @@ import ( type test struct { name string in interface{} + dst interface{} wantErr bool want []byte out interface{} @@ -532,6 +533,7 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool}", @@ -545,6 +547,7 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool} with untagged attributes", @@ -813,51 +816,61 @@ var ( name: "[4]int{1, 2, 3, 4}", in: [4]int{1, 2, 3, 4}, want: []byte{0x04, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{16384, 2, 3, 4}", in: [4]int{16384, 2, 3, 4}, want: []byte{0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{1073741824, 2, 3, 4}", in: [4]int{1073741824, 2, 3, 4}, want: []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{1 << 32, 2, 3, 1 << 32}", in: [4]int{1 << 32, 2, 3, 1 << 32}, want: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, + dst: [4]int{}, }, { name: "[3]bool{true, false, true}", in: [3]bool{true, false, true}, want: []byte{0x01, 0x00, 0x01}, + dst: [3]bool{}, }, { name: "[2][]int{{0, 1}, {1, 0}}", in: [2][]int{{0, 1}, {1, 0}}, want: []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, + dst: [2]int{}, }, { name: "[2][2]int{{0, 1}, {1, 0}}", in: [2][2]int{{0, 1}, {1, 0}}, want: []byte{0x00, 0x04, 0x04, 0x00}, + dst: [2][2]int{}, }, { name: "[2]*big.Int{big.NewInt(0), big.NewInt(1)}", in: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, want: []byte{0x00, 0x04}, + dst: [2]*big.Int{}, }, { name: "[2][]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, + // dst: [2][]byte{{0x00, 0x01}, {0x01, 0x00}} }, { name: "[2][2]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x00, 0x01, 0x01, 0x00}, + dst: [2][2]byte{}, }, } From a593a26f34059cad6e8dc7a9deab3d37edd5e21a Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 28 May 2021 10:37:33 -0400 Subject: [PATCH 146/245] decode of VariantDataType, wip tests --- pkg/scale/comparison_test.go | 2 +- pkg/scale/encode.go | 16 +++++++++++++++- pkg/scale/scale.go | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 54f4225da3..56050ec075 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -130,7 +130,7 @@ func TestOldVsNewEncoding(t *testing.T) { ), } - newEncode, err := Marshal(newDigest) + newEncode, err := scale.Marshal(newDigest) if err != nil { t.Errorf("unexpected err: %v", err) return diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 7c62a77bd5..cd6ff6890f 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/binary" "fmt" + "log" "math/big" "reflect" ) @@ -90,7 +91,20 @@ func (es *encodeState) marshal(in interface{}) (err error) { case reflect.Array: err = es.encodeArray(in) case reflect.Slice: - err = es.encodeSlice(in) + t := reflect.TypeOf(in) + // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType + switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { + case true: + invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) + switch in := invdt.Interface().(type) { + case VaryingDataType: + err = es.encodeVaryingDataType(in) + default: + log.Panicf("this should never happen") + } + case false: + err = es.encodeSlice(in) + } default: err = fmt.Errorf("unsupported type: %T", in) } diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index ac6530bae3..698ad6983f 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -24,8 +24,31 @@ import ( "sync" ) +type varyingDataTypeCache map[string]map[uint]VaryingDataTypeValue + +var vdtCache varyingDataTypeCache = make(varyingDataTypeCache) + type VaryingDataType []VaryingDataTypeValue +func RegisterVaryingDataType(in interface{}, values ...VaryingDataTypeValue) (err error) { + _, ok := in.(VaryingDataType) + if !ok { + err = fmt.Errorf("%T is not a VaryingDataType", in) + } + + t := reflect.TypeOf(in) + key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) + + _, ok = vdtCache[key] + if !ok { + vdtCache[key] = make(map[uint]VaryingDataTypeValue) + } + for _, val := range values { + vdtCache[key][val.Index()] = val + } + return +} + // VaryingDataType is used to represent scale encodable types type VaryingDataTypeValue interface { Index() uint From 0fdb5c8d31fbe5c394f92372d9fe860e6d081e44 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 28 May 2021 16:07:37 -0400 Subject: [PATCH 147/245] add optionality testing --- pkg/scale/decode_test.go | 68 +++++++++++++++++++--------------------- pkg/scale/encode_test.go | 13 -------- 2 files changed, 33 insertions(+), 48 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index dc8e43ecae..b27d6f1f25 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -318,7 +318,6 @@ func Test_decodeState_decodeStructManual(t *testing.T) { if !reflect.DeepEqual(dst2, want) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) } - } func Test_decodeState_decodeStruct(t *testing.T) { @@ -330,40 +329,6 @@ func Test_decodeState_decodeStruct(t *testing.T) { if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - // dstv := reflect.ValueOf(dst) - // inv := reflect.ValueOf(tt.in) - - // if dstv.Kind() != inv.Kind() { - // t.Errorf("decodeState.unmarshal() differing kind = %T, want %T", dst, tt.in) - // return - // } - - // switch inv.Kind() { - // case reflect.Ptr: - // fmt.Println(dst, dstv.Interface(), tt.in, inv.Interface()) - // if inv.IsZero() { - // fmt.Println(dst, tt.in, dstv.Interface(), inv.Interface()) - // if !reflect.DeepEqual(dstv.Interface(), tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } else if inv.IsNil() { - // if !reflect.DeepEqual(dst, tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } else { - // // // have to do this since reflect.DeepEqual won't come back true for different addresses - // // if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { - // // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // // } - // if !reflect.DeepEqual(dst, inv) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } - // default: - // if !reflect.DeepEqual(dst, tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } if !reflect.DeepEqual(dst, tt.in) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } @@ -399,3 +364,36 @@ func Test_decodeState_decodeSlice(t *testing.T) { }) } } + +func Test_unmarshal_optionality(t *testing.T) { + var ptrTests tests + for _, t := range allTests { + ptrTest := test{ + name: t.name, + in: t.in, + wantErr: t.wantErr, + want: t.want, + } + switch t.in { + case nil: + // this doesn't actually happen since none of the tests have nil value for tt.in + ptrTest.want = []byte{0x00} + default: + ptrTest.want = append([]byte{0x01}, t.want...) + } + ptrTests = append(ptrTests, ptrTest) + } + for _, tt := range ptrTests { + t.Run(tt.name, func(t *testing.T) { + // this becomes a pointer to a zero value of the underlying value + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + // es := &encodeState{fieldScaleIndicesCache: cache} + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, &tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index f7ea5e9309..146f07ed74 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -26,7 +26,6 @@ import ( type test struct { name string in interface{} - dst interface{} wantErr bool want []byte out interface{} @@ -533,7 +532,6 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool}", @@ -547,7 +545,6 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool} with untagged attributes", @@ -816,61 +813,51 @@ var ( name: "[4]int{1, 2, 3, 4}", in: [4]int{1, 2, 3, 4}, want: []byte{0x04, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{16384, 2, 3, 4}", in: [4]int{16384, 2, 3, 4}, want: []byte{0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{1073741824, 2, 3, 4}", in: [4]int{1073741824, 2, 3, 4}, want: []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{1 << 32, 2, 3, 1 << 32}", in: [4]int{1 << 32, 2, 3, 1 << 32}, want: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, - dst: [4]int{}, }, { name: "[3]bool{true, false, true}", in: [3]bool{true, false, true}, want: []byte{0x01, 0x00, 0x01}, - dst: [3]bool{}, }, { name: "[2][]int{{0, 1}, {1, 0}}", in: [2][]int{{0, 1}, {1, 0}}, want: []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, - dst: [2]int{}, }, { name: "[2][2]int{{0, 1}, {1, 0}}", in: [2][2]int{{0, 1}, {1, 0}}, want: []byte{0x00, 0x04, 0x04, 0x00}, - dst: [2][2]int{}, }, { name: "[2]*big.Int{big.NewInt(0), big.NewInt(1)}", in: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, want: []byte{0x00, 0x04}, - dst: [2]*big.Int{}, }, { name: "[2][]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, - // dst: [2][]byte{{0x00, 0x01}, {0x01, 0x00}} }, { name: "[2][2]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x00, 0x01, 0x01, 0x00}, - dst: [2][2]byte{}, }, } From 1392c041e625e3ced54b84329aa7fc1c2d0801da Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 31 May 2021 13:42:36 -0400 Subject: [PATCH 148/245] fix struct tests and optionality tests --- go.mod | 4 ++++ go.sum | 5 ++++ pkg/scale/decode_test.go | 50 ++++++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 01a0602622..dd50017e94 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,11 @@ require ( github.com/go-playground/validator/v10 v10.4.1 github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect +<<<<<<< HEAD github.com/google/go-cmp v0.5.6 +======= + github.com/google/go-cmp v0.5.6 // indirect +>>>>>>> fix struct tests and optionality tests github.com/google/uuid v1.1.5 // indirect github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 diff --git a/go.sum b/go.sum index cbd5a83445..6d7ec116f2 100644 --- a/go.sum +++ b/go.sum @@ -182,12 +182,17 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +<<<<<<< HEAD github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +======= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +>>>>>>> fix struct tests and optionality tests github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= github.com/google/gopacket v1.1.18/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index b27d6f1f25..f8eccb1f41 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -243,7 +243,6 @@ func Test_decodeState_decodeBigInt(t *testing.T) { for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { var dst *big.Int - // dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -271,7 +270,6 @@ func Test_decodeState_decodeBytes(t *testing.T) { func Test_decodeState_decodeBool(t *testing.T) { for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() var dst bool if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) @@ -289,8 +287,6 @@ func Test_decodeState_decodeStructManual(t *testing.T) { var b = []byte{0} var want *MyStruct = nil - // dst = structTests[0].dst - err := Unmarshal(b, &dst) if err != nil { t.Errorf("unexpected error: %v", err) @@ -321,16 +317,20 @@ func Test_decodeState_decodeStructManual(t *testing.T) { } func Test_decodeState_decodeStruct(t *testing.T) { - for _, tt := range append([]test{}, structTests[0]) { + for _, tt := range structTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() - // dst := tt.dst + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + var diff string + if tt.out != nil { + diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(tt.in)) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) } }) } @@ -338,8 +338,7 @@ func Test_decodeState_decodeStruct(t *testing.T) { func Test_decodeState_decodeArray(t *testing.T) { for _, tt := range arrayTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -353,8 +352,7 @@ func Test_decodeState_decodeArray(t *testing.T) { func Test_decodeState_decodeSlice(t *testing.T) { for _, tt := range sliceTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -373,6 +371,7 @@ func Test_unmarshal_optionality(t *testing.T) { in: t.in, wantErr: t.wantErr, want: t.want, + out: t.out, } switch t.in { case nil: @@ -387,12 +386,29 @@ func Test_unmarshal_optionality(t *testing.T) { t.Run(tt.name, func(t *testing.T) { // this becomes a pointer to a zero value of the underlying value dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - // es := &encodeState{fieldScaleIndicesCache: cache} if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - if !reflect.DeepEqual(dst, &tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + + if tt.wantErr == true { + return + } + + switch reflect.TypeOf(tt.in).Kind() { + case reflect.Struct: + var diff string + if tt.out != nil { + diff = cmp.Diff(dst, &tt.out, cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(dst, &tt.in, cmpopts.IgnoreUnexported(tt.in)) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + default: + if !reflect.DeepEqual(dst, &tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } } }) } From e08acda996336d5326dbc173451ddecd551de08a Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 31 May 2021 22:54:57 -0400 Subject: [PATCH 149/245] test VaryingDataType --- pkg/scale/decode.go | 16 +++++++++++++++- pkg/scale/decode_test.go | 30 ++++++++++-------------------- pkg/scale/encode.go | 17 ++++++++++++++++- pkg/scale/scale.go | 30 ------------------------------ 4 files changed, 41 insertions(+), 52 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index b8ccff16a9..096ff0cf9a 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -139,7 +139,21 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Slice: err = ds.decodeSlice(dstv) default: - err = fmt.Errorf("unsupported type: %T", in) + _, ok := in.(VaryingDataTypeValue) + switch ok { + case true: + t := reflect.TypeOf(in) + switch t.Kind() { + // TODO: support more primitive types. Do we need to support arrays and slices as well? + case reflect.Int: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + case reflect.Int16: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + } + in, err = ds.unmarshal(reflect.ValueOf(in)) + default: + err = fmt.Errorf("unsupported type: %T", in) + } } } return diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index f8eccb1f41..5ebf4fa514 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -327,7 +327,7 @@ func Test_decodeState_decodeStruct(t *testing.T) { if tt.out != nil { diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in)) } else { - diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(tt.in)) + diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(big.Int{}, tt.in, VDTValue2{}, MyStructWithIgnore{})) } if diff != "" { t.Errorf("decodeState.unmarshal() = %s", diff) @@ -388,28 +388,18 @@ func Test_unmarshal_optionality(t *testing.T) { dst := reflect.New(reflect.TypeOf(tt.in)).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - - if tt.wantErr == true { return } - - switch reflect.TypeOf(tt.in).Kind() { - case reflect.Struct: - var diff string - if tt.out != nil { - diff = cmp.Diff(dst, &tt.out, cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(dst, &tt.in, cmpopts.IgnoreUnexported(tt.in)) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - default: - if !reflect.DeepEqual(dst, &tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) } } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index cd6ff6890f..1c50802f61 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -106,7 +106,22 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - err = fmt.Errorf("unsupported type: %T", in) + _, ok := in.(VaryingDataTypeValue) + switch ok { + case true: + t := reflect.TypeOf(in) + switch t.Kind() { + // TODO: support more primitive types. Do we need to support arrays and slices as well? + case reflect.Int: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + case reflect.Int16: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + } + err = es.marshal(in) + default: + err = fmt.Errorf("unsupported type: %T", in) + } + } } return diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 698ad6983f..855016b8ca 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -24,36 +24,6 @@ import ( "sync" ) -type varyingDataTypeCache map[string]map[uint]VaryingDataTypeValue - -var vdtCache varyingDataTypeCache = make(varyingDataTypeCache) - -type VaryingDataType []VaryingDataTypeValue - -func RegisterVaryingDataType(in interface{}, values ...VaryingDataTypeValue) (err error) { - _, ok := in.(VaryingDataType) - if !ok { - err = fmt.Errorf("%T is not a VaryingDataType", in) - } - - t := reflect.TypeOf(in) - key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) - - _, ok = vdtCache[key] - if !ok { - vdtCache[key] = make(map[uint]VaryingDataTypeValue) - } - for _, val := range values { - vdtCache[key][val.Index()] = val - } - return -} - -// VaryingDataType is used to represent scale encodable types -type VaryingDataTypeValue interface { - Index() uint -} - // package level cache for fieldScaleIndicies var cache = &fieldScaleIndicesCache{ cache: make(map[string]fieldScaleIndices), From 6f985395f562e9bd9a0b8a63f84db9cabe9f5bcb Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 10:52:45 -0400 Subject: [PATCH 150/245] wip decode refactor, use reflect.Value as passed param --- pkg/scale/decode.go | 14 +++++++++----- pkg/scale/decode_test.go | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 096ff0cf9a..d5f85a8ce2 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -150,7 +150,6 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Int16: in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() } - in, err = ds.unmarshal(reflect.ValueOf(in)) default: err = fmt.Errorf("unsupported type: %T", in) } @@ -504,7 +503,7 @@ func (ds *decodeState) decodeBytes(dstv reflect.Value) (err error) { return } - b = make([]byte, length) + b := make([]byte, length) _, err = ds.Read(b) if err != nil { return @@ -665,11 +664,16 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { // decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 // if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise // it returns nil and error -func (ds *decodeState) decodeUint128() (ui *Uint128, err error) { +func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { buf := make([]byte, 16) err = binary.Read(ds, binary.LittleEndian, buf) if err != nil { - return nil, err + return + } + ui128, err := NewUint128(buf) + if err != nil { + return } - return NewUint128(buf) + dstv.Set(reflect.ValueOf(ui128)) + return } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 5ebf4fa514..1dadd954f2 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -256,9 +256,10 @@ func Test_decodeState_decodeBigInt(t *testing.T) { func Test_decodeState_decodeBytes(t *testing.T) { for _, tt := range stringTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return } if !reflect.DeepEqual(dst, tt.in) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) From 44ae53889e47cbaea45d8a864a173a9cd8da7efe Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 17:41:07 -0400 Subject: [PATCH 151/245] repurpose int and uint as compact length encoded integers, remove unnecessary handling of []int --- pkg/scale/comparison_test.go | 20 ++++++++++++++++++++ pkg/scale/decode.go | 14 ++++++++++++-- pkg/scale/decode_test.go | 35 ----------------------------------- pkg/scale/encode.go | 1 + pkg/scale/uint128.go | 1 + 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 56050ec075..b7c2a3af79 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -17,6 +17,8 @@ package scale import ( + "fmt" + "math/big" "reflect" "testing" @@ -173,3 +175,21 @@ func BenchmarkMarshal(b *testing.B) { } } } + +func TestSomething(t *testing.T) { + i := big.NewInt(0) + expectedVal := *common.Uint128FromBigInt(i) + + encByts, err := oldScale.Encode(expectedVal) + if err != nil { + t.Errorf("%v", err) + return + } + encBytes2, err := oldScale.Encode(i) + if err != nil { + t.Errorf("%v", err) + return + } + + fmt.Printf("%+v, %+v", encByts, encBytes2) +} diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index d5f85a8ce2..da1317a041 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -142,14 +142,24 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { _, ok := in.(VaryingDataTypeValue) switch ok { case true: + var temp reflect.Value t := reflect.TypeOf(in) switch t.Kind() { // TODO: support more primitive types. Do we need to support arrays and slices as well? case reflect.Int: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + temp = reflect.New(reflect.TypeOf(int(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } case reflect.Int16: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + temp = reflect.New(reflect.TypeOf(int16(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } } + dstv.Set(temp.Elem().Convert(t)) default: err = fmt.Errorf("unsupported type: %T", in) } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 1dadd954f2..2cc76640bf 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -282,41 +282,6 @@ func Test_decodeState_decodeBool(t *testing.T) { } } -func Test_decodeState_decodeStructManual(t *testing.T) { - // nil case - var dst *MyStruct = nil - var b = []byte{0} - var want *MyStruct = nil - - err := Unmarshal(b, &dst) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } - - // zero case MyStruct - var dst1 *MyStruct = &MyStruct{} - err = Unmarshal(b, &dst1) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst1, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } - - // zero case MyStruct - var dst2 *MyStruct = &MyStruct{Baz: true} - err = Unmarshal(b, &dst2) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst2, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } -} - func Test_decodeState_decodeStruct(t *testing.T) { for _, tt := range structTests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 1c50802f61..6efb0ed448 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -381,5 +381,6 @@ func (es *encodeState) encodeUint(i uint) (err error) { // encodeUint128 encodes a Uint128 func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) + fmt.Println("bytes", es.Bytes(), i.Bytes()) return } diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index 12e00c42b9..17fc09952c 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -90,6 +90,7 @@ func NewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128, err erro // Bytes returns the Uint128 in little endian format by default. A variadic parameter // order can be used to specify the binary.ByteOrder used func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { + fmt.Println("called bytes!") var o binary.ByteOrder = binary.LittleEndian if len(order) > 0 { o = order[0] From dc07d71b273b5c49375746b9a18f2ef4f12746ed Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 21:42:36 -0400 Subject: [PATCH 152/245] cleanup, and all tests benchmark --- pkg/scale/comparison_test.go | 42 ++++++++++++++++++++---------------- pkg/scale/encode.go | 1 - pkg/scale/uint128.go | 1 - 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index b7c2a3af79..a50098cecc 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -17,8 +17,6 @@ package scale import ( - "fmt" - "math/big" "reflect" "testing" @@ -132,7 +130,7 @@ func TestOldVsNewEncoding(t *testing.T) { ), } - newEncode, err := scale.Marshal(newDigest) + newEncode, err := Marshal(newDigest) if err != nil { t.Errorf("unexpected err: %v", err) return @@ -176,20 +174,28 @@ func BenchmarkMarshal(b *testing.B) { } } -func TestSomething(t *testing.T) { - i := big.NewInt(0) - expectedVal := *common.Uint128FromBigInt(i) - - encByts, err := oldScale.Encode(expectedVal) - if err != nil { - t.Errorf("%v", err) - return - } - encBytes2, err := oldScale.Encode(i) - if err != nil { - t.Errorf("%v", err) - return +func BenchmarkUnmarshal(b *testing.B) { + for _, tt := range allTests { + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } } - - fmt.Printf("%+v, %+v", encByts, encBytes2) } + +// func BenchmarkDecode(b *testing.B) { +// for _, tt := range variableWidthIntegerTests { +// dst := reflect.New(reflect.TypeOf(tt.in)).Interface() +// fmt.Printf("%v %T\n", dst, dst) +// // if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { +// // b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// // return +// // } +// _, err := oldScale.Decode(tt.want, dst) +// if err != nil { +// b.Errorf("%v", err) +// return +// } +// } +// } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 6efb0ed448..1c50802f61 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -381,6 +381,5 @@ func (es *encodeState) encodeUint(i uint) (err error) { // encodeUint128 encodes a Uint128 func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) - fmt.Println("bytes", es.Bytes(), i.Bytes()) return } diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index 17fc09952c..12e00c42b9 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -90,7 +90,6 @@ func NewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128, err erro // Bytes returns the Uint128 in little endian format by default. A variadic parameter // order can be used to specify the binary.ByteOrder used func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { - fmt.Println("called bytes!") var o binary.ByteOrder = binary.LittleEndian if len(order) > 0 { o = order[0] From 9c199cfd081c1d30975f2510feea2bd5222141e5 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Jun 2021 15:13:11 -0400 Subject: [PATCH 153/245] add README --- pkg/scale/decode.go | 3 +++ pkg/scale/encode.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index da1317a041..5c8280067b 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -158,6 +158,9 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { if err != nil { break } + default: + err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + return } dstv.Set(temp.Elem().Convert(t)) default: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 1c50802f61..30b41e333a 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -116,6 +116,9 @@ func (es *encodeState) marshal(in interface{}) (err error) { in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() case reflect.Int16: in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + default: + err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + return } err = es.marshal(in) default: From 30ff11530b340531f8b90ba08c06a1e88b8dc39d Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 3 Jun 2021 15:34:36 -0400 Subject: [PATCH 154/245] rResult encode/decode and RegisterResult --- pkg/scale/decode.go | 9 ++++++++- pkg/scale/encode.go | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 5c8280067b..17db227433 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -133,7 +133,14 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Ptr: err = ds.decodePointer(dstv) case reflect.Struct: - err = ds.decodeStruct(dstv) + t := reflect.TypeOf(in) + // check if this is a convertible to Result, if so encode using decodeResult + switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + case true: + err = ds.decodeResult(dstv) + case false: + err = ds.decodeStruct(dstv) + } case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 30b41e333a..4c33db8405 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/binary" "fmt" - "log" "math/big" "reflect" ) @@ -87,7 +86,16 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: - err = es.encodeStruct(in) + t := reflect.TypeOf(in) + // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType + switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + case true: + resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) + err = es.encodeResult(resv.Interface().(Result)) + case false: + err = es.encodeStruct(in) + } + case reflect.Array: err = es.encodeArray(in) case reflect.Slice: @@ -96,12 +104,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { case true: invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) - switch in := invdt.Interface().(type) { - case VaryingDataType: - err = es.encodeVaryingDataType(in) - default: - log.Panicf("this should never happen") - } + err = es.encodeVaryingDataType(invdt.Interface().(VaryingDataType)) case false: err = es.encodeSlice(in) } From aaf9e6a0250910efc8fa5f990684edcb99398902 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 4 Jun 2021 15:26:42 -0400 Subject: [PATCH 155/245] wip cr feedback --- pkg/scale/scale_test.go | 73 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 pkg/scale/scale_test.go diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go new file mode 100644 index 0000000000..a5bdac2364 --- /dev/null +++ b/pkg/scale/scale_test.go @@ -0,0 +1,73 @@ +package scale + +import ( + "reflect" + "testing" +) + +func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { + tests := []struct { + name string + in interface{} + wantIndices fieldScaleIndices + wantErr bool + }{ + // TODO: Add test cases. + { + in: struct{ Foo int }{}, + wantIndices: fieldScaleIndices{ + { + fieldIndex: 0, + }, + }, + }, + { + in: struct { + End1 bool + Baz bool `scale:"3"` + End2 []byte + Bar int32 `scale:"2"` + End3 []byte + Foo []byte `scale:"1"` + }{}, + wantIndices: fieldScaleIndices{ + { + fieldIndex: 5, + scaleIndex: newStringPtr("1"), + }, + { + fieldIndex: 3, + scaleIndex: newStringPtr("2"), + }, + { + fieldIndex: 1, + scaleIndex: newStringPtr("3"), + }, + { + fieldIndex: 0, + }, + { + fieldIndex: 2, + }, + { + fieldIndex: 4, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fsic := &fieldScaleIndicesCache{ + cache: make(map[string]fieldScaleIndices), + } + _, gotIndices, err := fsic.fieldScaleIndices(tt.in) + if (err != nil) != tt.wantErr { + t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotIndices, tt.wantIndices) { + t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() gotIndices = %v, want %v", gotIndices, tt.wantIndices) + } + }) + } +} From eb3eadd87bfa91df716f1e6b3cb19f026fca7406 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 7 Jun 2021 14:57:50 -0400 Subject: [PATCH 156/245] add licenses --- pkg/scale/comparison_test.go | 16 ---------------- pkg/scale/scale_test.go | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index a50098cecc..6b2fa07f57 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -183,19 +183,3 @@ func BenchmarkUnmarshal(b *testing.B) { } } } - -// func BenchmarkDecode(b *testing.B) { -// for _, tt := range variableWidthIntegerTests { -// dst := reflect.New(reflect.TypeOf(tt.in)).Interface() -// fmt.Printf("%v %T\n", dst, dst) -// // if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { -// // b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// // return -// // } -// _, err := oldScale.Decode(tt.want, dst) -// if err != nil { -// b.Errorf("%v", err) -// return -// } -// } -// } diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index a5bdac2364..632ab804d4 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( From 7da8a9fb843a9fbf9b331881ad22a312f8c5bd47 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 7 Jun 2021 17:04:20 -0400 Subject: [PATCH 157/245] add custom primitive encode/decode --- pkg/scale/decode.go | 113 +++++++++++++++++++++++++++++++++----------- pkg/scale/encode.go | 36 +++++++------- 2 files changed, 104 insertions(+), 45 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 17db227433..8b758ed60b 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -146,33 +146,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Slice: err = ds.decodeSlice(dstv) default: - _, ok := in.(VaryingDataTypeValue) - switch ok { - case true: - var temp reflect.Value - t := reflect.TypeOf(in) - switch t.Kind() { - // TODO: support more primitive types. Do we need to support arrays and slices as well? - case reflect.Int: - temp = reflect.New(reflect.TypeOf(int(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int16: - temp = reflect.New(reflect.TypeOf(int16(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - default: - err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - return - } - dstv.Set(temp.Elem().Convert(t)) - default: - err = fmt.Errorf("unsupported type: %T", in) - } + err = fmt.Errorf("unsupported type: %T", in) } } return @@ -263,6 +237,91 @@ func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { return } +func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { + in := dstv.Interface() + inType := reflect.TypeOf(in) + var temp reflect.Value + switch inType.Kind() { + case reflect.Bool: + temp = reflect.New(reflect.TypeOf(false)) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int: + temp = reflect.New(reflect.TypeOf(int(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int8: + temp = reflect.New(reflect.TypeOf(int8(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int16: + temp = reflect.New(reflect.TypeOf(int16(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int32: + temp = reflect.New(reflect.TypeOf(int32(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int64: + temp = reflect.New(reflect.TypeOf(int64(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.String: + temp = reflect.New(reflect.TypeOf("")) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint: + temp = reflect.New(reflect.TypeOf(uint(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint8: + temp = reflect.New(reflect.TypeOf(uint8(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint16: + temp = reflect.New(reflect.TypeOf(uint16(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint32: + temp = reflect.New(reflect.TypeOf(uint32(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint64: + temp = reflect.New(reflect.TypeOf(uint64(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + default: + err = fmt.Errorf("unsupported type for custom primitive: %T", in) + return + } + dstv.Set(temp.Elem().Convert(inType)) + return +} + func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { res := dstv.Interface().(Result) var rb byte diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 4c33db8405..610df7cd6c 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -109,24 +109,24 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - _, ok := in.(VaryingDataTypeValue) - switch ok { - case true: - t := reflect.TypeOf(in) - switch t.Kind() { - // TODO: support more primitive types. Do we need to support arrays and slices as well? - case reflect.Int: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() - case reflect.Int16: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() - default: - err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - return - } - err = es.marshal(in) - default: - err = fmt.Errorf("unsupported type: %T", in) - } + // _, ok := in.(VaryingDataTypeValue) + // switch ok { + // case true: + // t := reflect.TypeOf(in) + // switch t.Kind() { + // // TODO: support more primitive types. Do we need to support arrays and slices as well? + // case reflect.Int: + // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + // case reflect.Int16: + // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + // default: + // err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + // return + // } + // err = es.marshal(in) + // default: + err = fmt.Errorf("unsupported type: %T", in) + // } } } From 5b0bf5c08e4a43d7fc2f3228d33905b1c6d44ff9 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 8 Jun 2021 13:57:33 -0400 Subject: [PATCH 158/245] more cr feedback --- pkg/scale/comparison_test.go | 24 ++++++++++++---- pkg/scale/decode_test.go | 53 +++++++++++++++++++++++++++++++----- pkg/scale/encode.go | 18 ------------ pkg/scale/encode_test.go | 30 ++++++++++++++++++++ pkg/scale/scale_test.go | 1 - 5 files changed, 95 insertions(+), 31 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 6b2fa07f57..b2367c0239 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -175,11 +175,25 @@ func BenchmarkMarshal(b *testing.B) { } func BenchmarkUnmarshal(b *testing.B) { - for _, tt := range allTests { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return + for i := 0; i < b.N; i++ { + for _, tt := range allTests { + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + } + } +} + +func BenchmarkMarshal(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, tt := range allTests { + // dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if _, err := Marshal(tt.in); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } } } } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 2cc76640bf..0fc1d35a5d 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -339,13 +339,8 @@ func Test_unmarshal_optionality(t *testing.T) { want: t.want, out: t.out, } - switch t.in { - case nil: - // this doesn't actually happen since none of the tests have nil value for tt.in - ptrTest.want = []byte{0x00} - default: - ptrTest.want = append([]byte{0x01}, t.want...) - } + + ptrTest.want = append([]byte{0x01}, t.want...) ptrTests = append(ptrTests, ptrTest) } for _, tt := range ptrTests { @@ -369,3 +364,47 @@ func Test_unmarshal_optionality(t *testing.T) { }) } } + +func Test_unmarshal_optionality_nil_case(t *testing.T) { + var ptrTests tests + for _, t := range allTests { + ptrTest := test{ + name: t.name, + in: t.in, + wantErr: t.wantErr, + want: t.want, + // ignore out, since we are testing nil case + // out: t.out, + } + ptrTest.want = []byte{0x00} + + temp := reflect.New(reflect.TypeOf(t.in)) + // create a new pointer to type of temp + tempv := reflect.New(reflect.PtrTo(temp.Type()).Elem()) + // set zero value to elem of **temp so that is nil + tempv.Elem().Set(reflect.Zero(tempv.Elem().Type())) + // set test.in to *temp + ptrTest.in = tempv.Elem().Interface() + + ptrTests = append(ptrTests, ptrTest) + } + for _, tt := range ptrTests { + t.Run(tt.name, func(t *testing.T) { + // this becomes a pointer to a zero value of the underlying value + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface()) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) + } +} diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 610df7cd6c..ecc1b4234e 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -109,25 +109,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - // _, ok := in.(VaryingDataTypeValue) - // switch ok { - // case true: - // t := reflect.TypeOf(in) - // switch t.Kind() { - // // TODO: support more primitive types. Do we need to support arrays and slices as well? - // case reflect.Int: - // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() - // case reflect.Int16: - // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() - // default: - // err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - // return - // } - // err = es.marshal(in) - // default: err = fmt.Errorf("unsupported type: %T", in) - // } - } } return diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 146f07ed74..6681662f65 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -169,6 +169,11 @@ var ( in: uint(9223372036854775807), want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomUint(9223372036854775807)", + in: myCustomUint(9223372036854775807), + want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } int64Tests = tests{ { @@ -200,6 +205,11 @@ var ( in: int64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomInt64(9223372036854775807)", + in: myCustomInt64(9223372036854775807), + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } uint64Tests = tests{ { @@ -229,6 +239,11 @@ var ( in: uint64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomUint64(9223372036854775807)", + in: myCustomUint64(9223372036854775807), + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } int32Tests = tests{ { @@ -275,6 +290,11 @@ var ( in: uint32(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f}, }, + { + name: "uint32(1073741823)", + in: myCustomUint32(1073741823), + want: []byte{0xff, 0xff, 0xff, 0x3f}, + }, } int8Tests = tests{ { @@ -301,6 +321,11 @@ var ( in: uint8(1), want: []byte{0x01}, }, + { + name: "myCustomInt8(1)", + in: myCustomUint8(1), + want: []byte{0x01}, + }, } int16Tests = tests{ { @@ -320,6 +345,11 @@ var ( in: int16(16383), want: []byte{0xff, 0x3f}, }, + { + name: "myCustomInt16(16383)", + in: myCustomInt16(16383), + want: []byte{0xff, 0x3f}, + }, } uint16Tests = tests{ { diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index 632ab804d4..a96ab2b539 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -28,7 +28,6 @@ func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { wantIndices fieldScaleIndices wantErr bool }{ - // TODO: Add test cases. { in: struct{ Foo int }{}, wantIndices: fieldScaleIndices{ From d44f0ba5bf8530a07884651b4ca2b941df0a9c31 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 14 Jun 2021 12:04:15 -0400 Subject: [PATCH 159/245] wip --- pkg/scale/decode.go | 20 ++++++- pkg/scale/encode.go | 25 ++++++-- pkg/scale/result_test.go | 51 ++++++++++++++++ pkg/scale/something_test.go | 114 ++++++++++++++++++++++++++++++++++++ 4 files changed, 202 insertions(+), 8 deletions(-) create mode 100644 pkg/scale/something_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 8b758ed60b..0bc90c5663 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -134,13 +134,27 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { err = ds.decodePointer(dstv) case reflect.Struct: t := reflect.TypeOf(in) - // check if this is a convertible to Result, if so encode using decodeResult - switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + field, ok := t.FieldByName("Result") + switch ok { case true: + if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { + err = fmt.Errorf("%T is not a Result", in) + return + } + // res := reflect.ValueOf(in).FieldByName("Result") + // fmt.Println("yao!", res) err = ds.decodeResult(dstv) - case false: + default: err = ds.decodeStruct(dstv) } + + // // check if this is a convertible to Result, if so encode using decodeResult + // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + // case true: + // err = ds.decodeResult(dstv) + // case false: + // err = ds.decodeStruct(dstv) + // } case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index ecc1b4234e..a9e9ab789d 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -86,16 +86,31 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: + // fmt.Println("in here!") t := reflect.TypeOf(in) - // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType - switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + field, ok := t.FieldByName("Result") + switch ok { case true: - resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) - err = es.encodeResult(resv.Interface().(Result)) - case false: + if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { + err = fmt.Errorf("%T is not a Result", in) + return + } + res := reflect.ValueOf(in).FieldByName("Result").Interface().(Result) + // fmt.Println("yao!", res) + err = es.encodeResult(res) + default: err = es.encodeStruct(in) } + // check if this is a type with an embedded Result, aka a registered result + // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + // case true: + // resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) + // err = es.encodeResult(resv.Interface().(Result)) + // case false: + // err = es.encodeStruct(in) + // } + case reflect.Array: err = es.encodeArray(in) case reflect.Slice: diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index d5b5320974..52f4c19edf 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -317,3 +317,54 @@ func TestResult_Set(t *testing.T) { }) } } + +// func TestNilOk(t *testing.T) { +// mr := MyResult{} +// mr.SetOk(nil) +// bytes, err := Marshal(mr) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x00}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } + +// mr1 := MyResult{} +// mr1.SetErr(true) +// bytes, err = Marshal(mr1) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } +// } + +// func TestBothNil(t *testing.T) { +// mr := MyResult{} +// mr.SetOk(nil) +// bytes, err := Marshal(mr) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x00}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } + +// mr1 := MyResult{} +// mr1.SetErr(nil) +// bytes, err = Marshal(mr1) +// if err != nil { +// t.Errorf("%v", err) +// return +// } +// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } +// } diff --git a/pkg/scale/something_test.go b/pkg/scale/something_test.go new file mode 100644 index 0000000000..8194c32890 --- /dev/null +++ b/pkg/scale/something_test.go @@ -0,0 +1,114 @@ +package scale_test + +import ( + "fmt" + "testing" + + "github.com/ChainSafe/gossamer/pkg/scale" +) + +type MyStruct struct { + Baz bool + Bar uint32 + Foo []byte +} + +func (ms MyStruct) Index() uint { + return 1 +} + +type MyOtherStruct struct { + Foo string + Bar uint64 + Baz uint +} + +func (mos MyOtherStruct) Index() uint { + return 2 +} + +type MyInt16 int16 + +func (mi16 MyInt16) Index() uint { + return 3 +} + +type MyVaryingDataType scale.VaryingDataType + +func varyingDataTypeExample() { + err := scale.RegisterVaryingDataType(MyVaryingDataType{}, MyStruct{}, MyOtherStruct{}, MyInt16(0)) + if err != nil { + panic(err) + } + + mvdt := MyVaryingDataType{ + MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + }, + MyOtherStruct{ + Foo: "hello", + Bar: 999, + Baz: 888, + }, + MyInt16(111), + } + bytes, err := scale.Marshal(mvdt) + if err != nil { + panic(err) + } + + var unmarshaled MyVaryingDataType + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // [{Baz:true Bar:999 Foo:[1 2]} {Foo:hello Bar:999 Baz:888} 111] + fmt.Printf("%+v", unmarshaled) +} +func structExample() { + type MyStruct struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + } + var ms = MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + } + bytes, err := scale.Marshal(ms) + if err != nil { + panic(err) + } + + var unmarshaled MyStruct + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // Baz:true Bar:999 Foo:[1 2]} + fmt.Printf("%+v", unmarshaled) +} +func TestSomething(t *testing.T) { + // // compact length encoded uint + // var ui uint = 999 + // bytes, err := scale.Marshal(ui) + // if err != nil { + // panic(err) + // } + + // var unmarshaled uint + // err = scale.Unmarshal(bytes, &unmarshaled) + // if err != nil { + // panic(err) + // } + + // fmt.Printf("%d", unmarshaled) + + // structExample() + varyingDataTypeExample() +} From bee10b45be83531212d06dd83e3b562dcdf0a885 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 16 Jun 2021 15:37:38 -0400 Subject: [PATCH 160/245] revise Result --- pkg/scale/decode.go | 23 +------ pkg/scale/encode.go | 26 +------- pkg/scale/result_test.go | 120 +++++++++++++++++++++--------------- pkg/scale/something_test.go | 114 ---------------------------------- 4 files changed, 72 insertions(+), 211 deletions(-) delete mode 100644 pkg/scale/something_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 0bc90c5663..e551c6a9d1 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -133,28 +133,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Ptr: err = ds.decodePointer(dstv) case reflect.Struct: - t := reflect.TypeOf(in) - field, ok := t.FieldByName("Result") - switch ok { - case true: - if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { - err = fmt.Errorf("%T is not a Result", in) - return - } - // res := reflect.ValueOf(in).FieldByName("Result") - // fmt.Println("yao!", res) - err = ds.decodeResult(dstv) - default: - err = ds.decodeStruct(dstv) - } - - // // check if this is a convertible to Result, if so encode using decodeResult - // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { - // case true: - // err = ds.decodeResult(dstv) - // case false: - // err = ds.decodeStruct(dstv) - // } + err = ds.decodeStruct(dstv) case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index a9e9ab789d..7c5e80afca 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -86,31 +86,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: - // fmt.Println("in here!") - t := reflect.TypeOf(in) - field, ok := t.FieldByName("Result") - switch ok { - case true: - if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { - err = fmt.Errorf("%T is not a Result", in) - return - } - res := reflect.ValueOf(in).FieldByName("Result").Interface().(Result) - // fmt.Println("yao!", res) - err = es.encodeResult(res) - default: - err = es.encodeStruct(in) - } - - // check if this is a type with an embedded Result, aka a registered result - // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { - // case true: - // resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) - // err = es.encodeResult(resv.Interface().(Result)) - // case false: - // err = es.encodeStruct(in) - // } - + err = es.encodeStruct(in) case reflect.Array: err = es.encodeArray(in) case reflect.Slice: diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 52f4c19edf..62efd25068 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -318,53 +318,73 @@ func TestResult_Set(t *testing.T) { } } -// func TestNilOk(t *testing.T) { -// mr := MyResult{} -// mr.SetOk(nil) -// bytes, err := Marshal(mr) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x00}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } - -// mr1 := MyResult{} -// mr1.SetErr(true) -// bytes, err = Marshal(mr1) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } -// } - -// func TestBothNil(t *testing.T) { -// mr := MyResult{} -// mr.SetOk(nil) -// bytes, err := Marshal(mr) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x00}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } - -// mr1 := MyResult{} -// mr1.SetErr(nil) -// bytes, err = Marshal(mr1) -// if err != nil { -// t.Errorf("%v", err) -// return -// } -// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } -// } +func TestResult_Set(t *testing.T) { + type args struct { + mode ResultMode + in interface{} + } + tests := []struct { + name string + res Result + args args + wantErr bool + }{ + // TODO: Add test cases. + { + args: args{ + mode: Unset, + }, + wantErr: true, + }, + { + args: args{ + mode: OK, + in: nil, + }, + }, + { + args: args{ + mode: Err, + in: nil, + }, + }, + { + args: args{ + mode: OK, + in: true, + }, + res: NewResult(true, nil), + }, + { + args: args{ + mode: Err, + in: true, + }, + res: NewResult(nil, true), + }, + { + args: args{ + mode: OK, + in: true, + }, + res: NewResult("ok", "err"), + wantErr: true, + }, + { + args: args{ + mode: Err, + in: nil, + }, + res: NewResult(nil, true), + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := tt.res + if err := r.Set(tt.args.mode, tt.args.in); (err != nil) != tt.wantErr { + t.Errorf("Result.Set() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/scale/something_test.go b/pkg/scale/something_test.go deleted file mode 100644 index 8194c32890..0000000000 --- a/pkg/scale/something_test.go +++ /dev/null @@ -1,114 +0,0 @@ -package scale_test - -import ( - "fmt" - "testing" - - "github.com/ChainSafe/gossamer/pkg/scale" -) - -type MyStruct struct { - Baz bool - Bar uint32 - Foo []byte -} - -func (ms MyStruct) Index() uint { - return 1 -} - -type MyOtherStruct struct { - Foo string - Bar uint64 - Baz uint -} - -func (mos MyOtherStruct) Index() uint { - return 2 -} - -type MyInt16 int16 - -func (mi16 MyInt16) Index() uint { - return 3 -} - -type MyVaryingDataType scale.VaryingDataType - -func varyingDataTypeExample() { - err := scale.RegisterVaryingDataType(MyVaryingDataType{}, MyStruct{}, MyOtherStruct{}, MyInt16(0)) - if err != nil { - panic(err) - } - - mvdt := MyVaryingDataType{ - MyStruct{ - Baz: true, - Bar: 999, - Foo: []byte{1, 2}, - }, - MyOtherStruct{ - Foo: "hello", - Bar: 999, - Baz: 888, - }, - MyInt16(111), - } - bytes, err := scale.Marshal(mvdt) - if err != nil { - panic(err) - } - - var unmarshaled MyVaryingDataType - err = scale.Unmarshal(bytes, &unmarshaled) - if err != nil { - panic(err) - } - - // [{Baz:true Bar:999 Foo:[1 2]} {Foo:hello Bar:999 Baz:888} 111] - fmt.Printf("%+v", unmarshaled) -} -func structExample() { - type MyStruct struct { - Baz bool `scale:"3"` - Bar int32 `scale:"2"` - Foo []byte `scale:"1"` - } - var ms = MyStruct{ - Baz: true, - Bar: 999, - Foo: []byte{1, 2}, - } - bytes, err := scale.Marshal(ms) - if err != nil { - panic(err) - } - - var unmarshaled MyStruct - err = scale.Unmarshal(bytes, &unmarshaled) - if err != nil { - panic(err) - } - - // Baz:true Bar:999 Foo:[1 2]} - fmt.Printf("%+v", unmarshaled) -} -func TestSomething(t *testing.T) { - // // compact length encoded uint - // var ui uint = 999 - // bytes, err := scale.Marshal(ui) - // if err != nil { - // panic(err) - // } - - // var unmarshaled uint - // err = scale.Unmarshal(bytes, &unmarshaled) - // if err != nil { - // panic(err) - // } - - // fmt.Printf("%d", unmarshaled) - - // structExample() - varyingDataTypeExample() -} From 43f21af766ac78b147f817419aa7c0fe25318f10 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 18 Jun 2021 22:13:39 -0400 Subject: [PATCH 161/245] wip VaryingDataType and VaryingDataTypeSlice --- pkg/scale/decode_test.go | 46 ++++-- pkg/scale/encode.go | 10 +- pkg/scale/varying_data_type_test.go | 231 ++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+), 24 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 0fc1d35a5d..2a3400d907 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -345,22 +345,38 @@ func Test_unmarshal_optionality(t *testing.T) { } for _, tt := range ptrTests { t.Run(tt.name, func(t *testing.T) { - // this becomes a pointer to a zero value of the underlying value - dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - var diff string - if tt.out != nil { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } + switch in := tt.in.(type) { + case VaryingDataType: + // copy the inputted vdt cause we need the cached values + copy := in + vdt := copy + vdt.value = nil + var dst interface{} = &vdt + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + diff := cmp.Diff(vdt.value, tt.in.(VaryingDataType).value, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + default: + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + } }) } } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 7c5e80afca..7c62a77bd5 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -90,15 +90,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { case reflect.Array: err = es.encodeArray(in) case reflect.Slice: - t := reflect.TypeOf(in) - // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType - switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { - case true: - invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) - err = es.encodeVaryingDataType(invdt.Interface().(VaryingDataType)) - case false: - err = es.encodeSlice(in) - } + err = es.encodeSlice(in) default: err = fmt.Errorf("unsupported type: %T", in) } diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index d92dd7c723..0abe0569cb 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -638,3 +638,234 @@ func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { }) } } + +func TestNewVaryingDataType(t *testing.T) { + type args struct { + values []VaryingDataTypeValue + } + tests := []struct { + name string + args args + wantVdt VaryingDataType + wantErr bool + }{ + { + args: args{ + values: []VaryingDataTypeValue{}, + }, + wantErr: true, + }, + { + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + }, + }, + wantVdt: VaryingDataType{ + cache: map[uint]VaryingDataTypeValue{ + VDTValue{}.Index(): VDTValue{}, + VDTValue1{}.Index(): VDTValue1{}, + VDTValue2{}.Index(): VDTValue2{}, + VDTValue3(0).Index(): VDTValue3(0), + }, + }, + }, + { + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), VDTValue{}, + }, + }, + wantVdt: VaryingDataType{ + cache: map[uint]VaryingDataTypeValue{ + VDTValue{}.Index(): VDTValue{}, + VDTValue1{}.Index(): VDTValue1{}, + VDTValue2{}.Index(): VDTValue2{}, + VDTValue3(0).Index(): VDTValue3(0), + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotVdt, err := NewVaryingDataType(tt.args.values...) + if (err != nil) != tt.wantErr { + t.Errorf("NewVaryingDataType() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotVdt, tt.wantVdt) { + t.Errorf("NewVaryingDataType() = %v, want %v", gotVdt, tt.wantVdt) + } + }) + } +} + +func TestVaryingDataType_Set(t *testing.T) { + type args struct { + value VaryingDataTypeValue + } + tests := []struct { + name string + vdt VaryingDataType + args args + wantErr bool + }{ + { + vdt: mustNewVaryingDataType(VDTValue1{}), + args: args{ + value: VDTValue1{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue1{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue2{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue3(0), + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + vdt := tt.vdt + if err := vdt.Set(tt.args.value); (err != nil) != tt.wantErr { + t.Errorf("VaryingDataType.SetValue() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestVaryingDataTypeSlice_Add(t *testing.T) { + type args struct { + values []VaryingDataTypeValue + } + tests := []struct { + name string + vdts VaryingDataTypeSlice + args args + wantErr bool + wantValues []VaryingDataType + }{ + { + name: "happy path", + vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0))), + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{ + B: 1, + }, + }, + }, + wantValues: []VaryingDataType{ + mustNewVaryingDataTypeAndSet( + VDTValue{ + B: 1, + }, + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + }, + }, + { + name: "invalid value error case", + vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{})), + args: args{ + values: []VaryingDataTypeValue{ + VDTValue3(0), + }, + }, + wantValues: []VaryingDataType{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + vdts := &tt.vdts + if err := vdts.Add(tt.args.values...); (err != nil) != tt.wantErr { + t.Errorf("VaryingDataTypeSlice.Add() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(vdts.Values, tt.wantValues) { + t.Errorf("NewVaryingDataType() = %v, want %v", vdts.Values, tt.wantValues) + } + }) + } +} + +var varyingDataTypeSliceTests = tests{ + { + in: mustNewVaryingDataTypeSliceAndSet( + mustNewVaryingDataType( + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, + ), + want: []byte{ + // length + 4, + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + }, +} + +func Test_encodeState_encodeVaryingDataTypeSlice(t *testing.T) { + for _, tt := range varyingDataTypeSliceTests { + t.Run(tt.name, func(t *testing.T) { + vdt := tt.in.(VaryingDataTypeSlice) + b, err := Marshal(vdt) + if (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(b, tt.want) { + t.Errorf("encodeState.encodeStruct() = %v, want %v", b, tt.want) + } + }) + } +} + +// func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { +// for _, tt := range varyingDataTypeTests { +// t.Run(tt.name, func(t *testing.T) { +// dst, err := NewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0)) +// if err != nil { +// t.Errorf("%v", err) +// return +// } +// if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { +// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// return +// } +// vdt := tt.in.(VaryingDataType) +// diff := cmp.Diff(dst.Value(), vdt.Value(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) +// if diff != "" { +// t.Errorf("decodeState.unmarshal() = %s", diff) +// } +// }) +// } +// } From d1bd74f43fdd611906d10f00583b95ff127539a8 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 21 Jun 2021 11:00:07 -0400 Subject: [PATCH 162/245] refactor VaryingDataType and add VaryingDataTypeSlice --- pkg/scale/varying_data_type_test.go | 157 ++++++++++++++++++++-------- 1 file changed, 114 insertions(+), 43 deletions(-) diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index 0abe0569cb..d63063acfd 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -810,27 +810,97 @@ var varyingDataTypeSliceTests = tests{ ), VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, ), - want: []byte{ - // length - 4, - // index - 2, - // value - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - }, + want: newWant( + []byte{ + // length + 4, + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + ), + }, + { + in: mustNewVaryingDataTypeSliceAndSet( + mustNewVaryingDataType( + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, + VDTValue{ + A: big.NewInt(1073741823), + B: int(1073741823), + C: uint(1073741823), + D: int8(1), + E: uint8(1), + F: int16(16383), + G: uint16(16383), + H: int32(1073741823), + I: uint32(1073741823), + J: int64(9223372036854775807), + K: uint64(9223372036854775807), + L: byteArray(64), + M: testStrings[1], + N: true, + }, + ), + want: newWant( + []byte{ + // length + 8, + }, + []byte{ + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + []byte{ + // index + 1, + // value + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, + 0x01, + 0x01, + 0xff, 0x3f, + 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + }, + append([]byte{0x01, 0x01}, byteArray(64)...), + append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), + []byte{0x01}, + ), }, } @@ -840,32 +910,33 @@ func Test_encodeState_encodeVaryingDataTypeSlice(t *testing.T) { vdt := tt.in.(VaryingDataTypeSlice) b, err := Marshal(vdt) if (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr) } if !reflect.DeepEqual(b, tt.want) { - t.Errorf("encodeState.encodeStruct() = %v, want %v", b, tt.want) + t.Errorf("Marshal() = %v, want %v", b, tt.want) } }) } } -// func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { -// for _, tt := range varyingDataTypeTests { -// t.Run(tt.name, func(t *testing.T) { -// dst, err := NewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0)) -// if err != nil { -// t.Errorf("%v", err) -// return -// } -// if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { -// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// vdt := tt.in.(VaryingDataType) -// diff := cmp.Diff(dst.Value(), vdt.Value(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) -// if diff != "" { -// t.Errorf("decodeState.unmarshal() = %s", diff) -// } -// }) -// } -// } +func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { + opt := cmp.Comparer(func(x, y VaryingDataType) bool { + return reflect.DeepEqual(x.value, y.value) && reflect.DeepEqual(x.cache, y.cache) + }) + + for _, tt := range varyingDataTypeSliceTests { + t.Run(tt.name, func(t *testing.T) { + dst := tt.in.(VaryingDataTypeSlice) + dst.Types = make([]VaryingDataType, 0) + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + vdts := tt.in.(VaryingDataTypeSlice) + diff := cmp.Diff(dst, vdts, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}), opt) + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) + } +} From 20f84c2228287c7ea52c1b295e9bfd16d8a7560b Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 21 Jun 2021 11:03:11 -0600 Subject: [PATCH 163/245] integrate scale pkg into lib/transaction --- go.mod | 4 ++++ lib/transaction/types.go | 35 ----------------------------------- lib/transaction/types_test.go | 3 ++- 3 files changed, 6 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index dd50017e94..f25e18981e 100644 --- a/go.mod +++ b/go.mod @@ -19,11 +19,15 @@ require ( github.com/go-playground/validator/v10 v10.4.1 github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect +<<<<<<< HEAD <<<<<<< HEAD github.com/google/go-cmp v0.5.6 ======= github.com/google/go-cmp v0.5.6 // indirect >>>>>>> fix struct tests and optionality tests +======= + github.com/google/go-cmp v0.5.6 +>>>>>>> integrate scale pkg into lib/transaction github.com/google/uuid v1.1.5 // indirect github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 diff --git a/lib/transaction/types.go b/lib/transaction/types.go index 9c743c84a3..6d538ab37f 100644 --- a/lib/transaction/types.go +++ b/lib/transaction/types.go @@ -17,10 +17,7 @@ package transaction import ( - "encoding/binary" - "github.com/ChainSafe/gossamer/dot/types" - "github.com/ChainSafe/gossamer/lib/scale" ) // Validity struct see: https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/sr-primitives/src/transaction_validity.rs#L178 @@ -56,35 +53,3 @@ func NewValidTransaction(extrinsic types.Extrinsic, validity *Validity) *ValidTr Validity: validity, } } - -// Encode SCALE encodes the transaction -func (vt *ValidTransaction) Encode() ([]byte, error) { - enc := []byte(vt.Extrinsic) - - buf := make([]byte, 8) - binary.LittleEndian.PutUint64(buf, vt.Validity.Priority) - enc = append(enc, buf...) - - d, err := scale.Encode(vt.Validity.Requires) - if err != nil { - return nil, err - } - enc = append(enc, d...) - - d, err = scale.Encode(vt.Validity.Provides) - if err != nil { - return nil, err - } - enc = append(enc, d...) - - binary.LittleEndian.PutUint64(buf, vt.Validity.Longevity) - enc = append(enc, buf...) - - if vt.Validity.Propagate { - enc = append(enc, 1) - } else { - enc = append(enc, 0) - } - - return enc, nil -} diff --git a/lib/transaction/types_test.go b/lib/transaction/types_test.go index b1e9062a27..d59545dfc8 100644 --- a/lib/transaction/types_test.go +++ b/lib/transaction/types_test.go @@ -3,6 +3,7 @@ package transaction import ( "testing" + "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) @@ -18,7 +19,7 @@ func TestValidTransaction_Encode(t *testing.T) { extrinsic := []byte("nootwashere") vt := NewValidTransaction(extrinsic, validity) - enc, err := vt.Encode() + enc, err := scale.Marshal(vt) require.NoError(t, err) if len(enc) == 0 { From c1089fc7cc6265e74031032607af754dbecf2636 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 11:01:44 -0600 Subject: [PATCH 164/245] WIP/Integrate scale pkg into lib/babe --- lib/babe/errors.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index d8f4b41d94..d69cea7a65 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -16,9 +16,8 @@ package babe import ( "errors" "fmt" - "github.com/ChainSafe/gossamer/lib/common/optional" - "github.com/ChainSafe/gossamer/lib/scale" + "github.com/ChainSafe/gossamer/pkg/scale" ) var ( @@ -95,8 +94,15 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { switch res[0] { case 0: - unKnownError, _ := scale.Decode(res[1:], []byte{}) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", string(unKnownError.([]byte)))} + //unKnownError, _ := scale.Decode(res[1:], []byte{}) + //fmt.Sprintf("Output: %s", string(unKnownError.([]byte))) + //fmt.Println("Output:") + // This has not been working + var v []byte + fmt.Println(res) + unKnownError := scale.Unmarshal(res[1:], &v) + fmt.Println("Output: " + unKnownError.Error()) + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", unKnownError.Error())} case 1: return &DispatchOutcomeError{"failed lookup"} case 2: From e642765856cb46972afb55b4d32cded5d3dd2e29 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 11:26:59 -0600 Subject: [PATCH 165/245] fixed error unmarshaling in errors.go --- lib/babe/errors.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index d69cea7a65..084dfa87ad 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -94,15 +94,14 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { switch res[0] { case 0: - //unKnownError, _ := scale.Decode(res[1:], []byte{}) - //fmt.Sprintf("Output: %s", string(unKnownError.([]byte))) - //fmt.Println("Output:") - // This has not been working var v []byte - fmt.Println(res) - unKnownError := scale.Unmarshal(res[1:], &v) - fmt.Println("Output: " + unKnownError.Error()) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", unKnownError.Error())} + err := scale.Unmarshal(res[1:], &v) + if err != nil { + // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure + // TODO Create stucts for errors and integrate into Varying data type + return errInvalidResult + } + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} case 1: return &DispatchOutcomeError{"failed lookup"} case 2: From ca9546886507abb7d125963aa9c91862a8b82664 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 21 Jun 2021 11:03:11 -0600 Subject: [PATCH 166/245] integrate scale pkg into lib/transaction --- go.mod | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/go.mod b/go.mod index f25e18981e..52d7b691ab 100644 --- a/go.mod +++ b/go.mod @@ -20,11 +20,15 @@ require ( github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect <<<<<<< HEAD +<<<<<<< HEAD <<<<<<< HEAD github.com/google/go-cmp v0.5.6 ======= github.com/google/go-cmp v0.5.6 // indirect >>>>>>> fix struct tests and optionality tests +======= + github.com/google/go-cmp v0.5.6 +>>>>>>> integrate scale pkg into lib/transaction ======= github.com/google/go-cmp v0.5.6 >>>>>>> integrate scale pkg into lib/transaction From 5963e8aa61e3a041ad70b3c66dd27f1c2a9ece0c Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 12:03:02 -0600 Subject: [PATCH 167/245] WIP/Integrate scale into lib/babe --- lib/babe/build.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index 66b181caf8..b821d73e60 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -31,6 +31,7 @@ import ( "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" + scale2 "github.com/ChainSafe/gossamer/pkg/scale" ) const ( @@ -347,16 +348,17 @@ func hasSlotEnded(slot Slot) bool { return time.Since(slotEnd) >= 0 } -// ExtrinsicsToBody returns scale encoded block body which contains inherent and extrinsic. +// ExtrinsicsToBody returns scale2 encoded block body which contains inherent and extrinsic. func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (*types.Body, error) { extrinsics := types.BytesArrayToExtrinsics(inherents) for _, tx := range txs { - decExt, err := scale.Decode(tx.Extrinsic, []byte{}) + var decExt []byte + err := scale2.Unmarshal(tx.Extrinsic, &decExt) if err != nil { return nil, err } - extrinsics = append(extrinsics, decExt.([]byte)) + extrinsics = append(extrinsics, decExt) } return types.NewBodyFromExtrinsics(extrinsics) From 44022beb2154bcbcfe107f82c76d209245bd5428 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 13:31:50 -0600 Subject: [PATCH 168/245] integrate scale package into babe library --- lib/babe/build.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index b821d73e60..1972e265b0 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -29,9 +29,8 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/runtime" - "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" - scale2 "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/ChainSafe/gossamer/pkg/scale" ) const ( @@ -306,14 +305,15 @@ func (b *BlockBuilder) buildBlockInherents(slot Slot) ([][]byte, error) { } // decode inherent extrinsics - exts, err := scale.Decode(inherentExts, [][]byte{}) + var exts [][]byte + err = scale.Unmarshal(inherentExts, &exts) if err != nil { return nil, err } // apply each inherent extrinsic - for _, ext := range exts.([][]byte) { - in, err := scale.Encode(ext) + for _, ext := range exts { + in, err := scale.Marshal(ext) if err != nil { return nil, err } @@ -329,7 +329,7 @@ func (b *BlockBuilder) buildBlockInherents(slot Slot) ([][]byte, error) { } } - return exts.([][]byte), nil + return exts, nil } func (b *BlockBuilder) addToQueue(txs []*transaction.ValidTransaction) { @@ -354,7 +354,7 @@ func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) ( for _, tx := range txs { var decExt []byte - err := scale2.Unmarshal(tx.Extrinsic, &decExt) + err := scale.Unmarshal(tx.Extrinsic, &decExt) if err != nil { return nil, err } From 7269caefb1986a64c73e05cf9073f3456564881c Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 22 Jun 2021 17:01:03 -0400 Subject: [PATCH 169/245] fix result.set with nil value --- pkg/scale/result_test.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 62efd25068..c63c196ae0 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -329,24 +329,39 @@ func TestResult_Set(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. { args: args{ mode: Unset, }, + res: NewResult(nil, nil), wantErr: true, + wantResult: Result{ + ok: empty{}, err: empty{}, + }, }, { args: args{ mode: OK, in: nil, }, + res: NewResult(nil, nil), + wantResult: Result{ + ok: empty{}, + err: empty{}, + mode: OK, + }, }, { args: args{ mode: Err, in: nil, }, + res: NewResult(nil, nil), + wantResult: Result{ + ok: empty{}, + err: empty{}, + mode: Err, + }, }, { args: args{ @@ -354,6 +369,11 @@ func TestResult_Set(t *testing.T) { in: true, }, res: NewResult(true, nil), + wantResult: Result{ + ok: true, + err: empty{}, + mode: OK, + }, }, { args: args{ @@ -361,6 +381,11 @@ func TestResult_Set(t *testing.T) { in: true, }, res: NewResult(nil, true), + wantResult: Result{ + ok: empty{}, + err: true, + mode: Err, + }, }, { args: args{ @@ -369,6 +394,10 @@ func TestResult_Set(t *testing.T) { }, res: NewResult("ok", "err"), wantErr: true, + wantResult: Result{ + ok: "ok", + err: "err", + }, }, { args: args{ @@ -377,6 +406,10 @@ func TestResult_Set(t *testing.T) { }, res: NewResult(nil, true), wantErr: true, + wantResult: Result{ + ok: empty{}, + err: true, + }, }, } for _, tt := range tests { @@ -385,6 +418,9 @@ func TestResult_Set(t *testing.T) { if err := r.Set(tt.args.mode, tt.args.in); (err != nil) != tt.wantErr { t.Errorf("Result.Set() error = %v, wantErr %v", err, tt.wantErr) } + if !reflect.DeepEqual(tt.wantResult, r) { + t.Errorf("Result.Unwrap() = %v, want %v", tt.wantResult, r) + } }) } } From ea9f3d78cfa53a1c2e5186398b8a1d8c7100faf3 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 21 Jun 2021 11:03:11 -0600 Subject: [PATCH 170/245] integrate scale pkg into lib/transaction --- go.mod | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/go.mod b/go.mod index 52d7b691ab..b41e086388 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect <<<<<<< HEAD <<<<<<< HEAD +<<<<<<< HEAD <<<<<<< HEAD github.com/google/go-cmp v0.5.6 ======= @@ -29,6 +30,9 @@ require ( ======= github.com/google/go-cmp v0.5.6 >>>>>>> integrate scale pkg into lib/transaction +======= + github.com/google/go-cmp v0.5.6 +>>>>>>> integrate scale pkg into lib/transaction ======= github.com/google/go-cmp v0.5.6 >>>>>>> integrate scale pkg into lib/transaction From 6da02849a7c8429f7e3011547a16f49644af4ed2 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 15:06:02 -0600 Subject: [PATCH 171/245] WIP/refactor babe errors to utilize new scale pkg --- lib/babe/errors.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 084dfa87ad..cd43eae711 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -150,9 +150,36 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } +type UnknownError struct { + Err string +} + +func (err UnknownError) Index() uint { + return 1 +} + +type FailedLookup struct { + Err string +} + +func (err FailedLookup) Index() uint { + return 2 +} + +type BadOrigin struct { + Err string +} + +func (err BadOrigin) Index() uint { + return 3 +} + func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome + result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) + fmt.Println("Result err") + fmt.Println(result) switch res[1] { case 0: return nil From 8ea705e34134a4691600f4c7496a91bff12c0681 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 15:56:28 -0600 Subject: [PATCH 172/245] Integrate scale pkg into babe tests --- lib/babe/build_test.go | 7 ++++--- lib/babe/errors.go | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/babe/build_test.go b/lib/babe/build_test.go index 73bb4b3165..d18247c833 100644 --- a/lib/babe/build_test.go +++ b/lib/babe/build_test.go @@ -26,8 +26,8 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/sr25519" - "github.com/ChainSafe/gossamer/lib/scale" "github.com/ChainSafe/gossamer/lib/transaction" + "github.com/ChainSafe/gossamer/pkg/scale" log "github.com/ChainSafe/log15" cscale "github.com/centrifuge/go-substrate-rpc-client/v2/scale" "github.com/centrifuge/go-substrate-rpc-client/v2/signature" @@ -223,11 +223,12 @@ func TestBuildAndApplyExtrinsic(t *testing.T) { // build extrinsic rawMeta, err := babeService.rt.Metadata() require.NoError(t, err) - decoded, err := scale.Decode(rawMeta, []byte{}) + var decoded []byte + err = scale.Unmarshal(rawMeta, []byte{}) require.NoError(t, err) meta := &ctypes.Metadata{} - err = ctypes.DecodeFromBytes(decoded.([]byte), meta) + err = ctypes.DecodeFromBytes(decoded, meta) require.NoError(t, err) rv, err := babeService.rt.Version() diff --git a/lib/babe/errors.go b/lib/babe/errors.go index cd43eae711..60133ac699 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -178,7 +178,11 @@ func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) - fmt.Println("Result err") + //err := result.Set(scale.OK, nil) + //if err != nil { + // panic(err) + //} + fmt.Println("Result") fmt.Println(result) switch res[1] { case 0: From 51a81c37c71c1f78c48309dbb130593324b18f77 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 16:22:12 -0600 Subject: [PATCH 173/245] create init structs for dispatch outcome errors --- lib/babe/errors.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 60133ac699..eb48825b43 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -174,10 +174,18 @@ func (err BadOrigin) Index() uint { return 3 } +type CustomModuleError struct { + Err string +} + +func (err CustomModuleError) Index() uint { + return 4 +} + func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) + result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) //err := result.Set(scale.OK, nil) //if err != nil { // panic(err) From f0d7d0d30e47c7115de3531b4915b278fb2164aa Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 16:49:47 -0600 Subject: [PATCH 174/245] WIP/refactor babe error handling to utiliize new scale pkg features --- lib/babe/errors.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index eb48825b43..522e4f636d 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -151,7 +151,7 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - Err string + Err DispatchOutcomeError } func (err UnknownError) Index() uint { @@ -159,7 +159,7 @@ func (err UnknownError) Index() uint { } type FailedLookup struct { - Err string + err DispatchOutcomeError } func (err FailedLookup) Index() uint { @@ -167,7 +167,7 @@ func (err FailedLookup) Index() uint { } type BadOrigin struct { - Err string + Err DispatchOutcomeError } func (err BadOrigin) Index() uint { @@ -175,7 +175,7 @@ func (err BadOrigin) Index() uint { } type CustomModuleError struct { - Err string + Err DispatchOutcomeError } func (err CustomModuleError) Index() uint { @@ -190,6 +190,11 @@ func determineErr(res []byte) error { //if err != nil { // panic(err) //} + + // This code chunk works + v := FailedLookup{err: DispatchOutcomeError{"failed lookup"}} + fmt.Println(v.err) + fmt.Println("Result") fmt.Println(result) switch res[1] { From 0bad24869dcc5d1630aff3096d6cf07e7cbe90fc Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 15:44:21 -0600 Subject: [PATCH 175/245] WIP scale/babe integration. Closer but still errors --- lib/babe/errors.go | 116 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 91 insertions(+), 25 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 522e4f636d..e03a5c0525 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -63,10 +63,13 @@ var ( ) // A DispatchOutcomeError is outcome of dispatching the extrinsic +// Can I make this not a struct? type DispatchOutcomeError struct { msg string // description of error } +//type DispatchOutcomeError string + func (e DispatchOutcomeError) Error() string { return fmt.Sprintf("dispatch outcome error: %s", e.msg) } @@ -91,24 +94,81 @@ func determineCustomModuleErr(res []byte) error { return fmt.Errorf("index: %d code: %d message: %s", res[0], res[1], errMsg.String()) } +//func determineDispatchErr(res []byte) error { +// var v []byte +// err := scale.Unmarshal(res[1:], &v) +// if err != nil { +// // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure +// // TODO Create stucts for errors and integrate into Varying data type +// return errInvalidResult +// } +// +// switch res[0] { +// case 0: +// var v []byte +// err := scale.Unmarshal(res[1:], &v) +// if err != nil { +// // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure +// // TODO Create stucts for errors and integrate into Varying data type +// return errInvalidResult +// } +// return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} +// case 1: +// return &DispatchOutcomeError{"failed lookup"} +// case 2: +// return &DispatchOutcomeError{"bad origin"} +// case 3: +// return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))} +// } +// +// return errInvalidResult +//} + func determineDispatchErr(res []byte) error { - switch res[0] { - case 0: - var v []byte - err := scale.Unmarshal(res[1:], &v) - if err != nil { - // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure - // TODO Create stucts for errors and integrate into Varying data type - return errInvalidResult - } - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} - case 1: + //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) + vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) + //fmt.Println("Result") + //fmt.Println(result) + + //var v []byte + err := scale.Unmarshal(res[1:], &vdt) + if err != nil { + // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure + // TODO Create stucts for errors and integrate into Varying data type + return errInvalidResult + } + + + //fmt.Println("Unmarsalled dispatchErr Type:") + //fmt.Println(vdt.Value()) + + switch val := vdt.Value().(type){ + case UnknownError: + // For some reason its not entering here, going to customModuleError instead + // Maybe cuz struct is wrong + fmt.Println("Val:") + fmt.Println(val) + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val.Err)} + case FailedLookup: return &DispatchOutcomeError{"failed lookup"} - case 2: + case BadOrigin: return &DispatchOutcomeError{"bad origin"} - case 3: - return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))} + case CustomModuleError: + // Printing nice, not getting the correct values + return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} } + //// if custon error module + //if vdt.Value().Index() == 4 { + // err = vdt.Set(CustomModuleError{ + // Err: DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))}, + // }) + // if err != nil { + // panic(err) + // } + //} + //fmt.Println("Unmarsalled dispatchErr Value2:") + //fmt.Println(vdt.Value()) + return errInvalidResult } @@ -151,7 +211,7 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - Err DispatchOutcomeError + Err string } func (err UnknownError) Index() uint { @@ -159,7 +219,7 @@ func (err UnknownError) Index() uint { } type FailedLookup struct { - err DispatchOutcomeError + Err string } func (err FailedLookup) Index() uint { @@ -167,7 +227,7 @@ func (err FailedLookup) Index() uint { } type BadOrigin struct { - Err DispatchOutcomeError + Err string } func (err BadOrigin) Index() uint { @@ -175,28 +235,34 @@ func (err BadOrigin) Index() uint { } type CustomModuleError struct { - Err DispatchOutcomeError + index uint8 `scale:"3"` + err uint8 `scale:"2"` + message string `scale:"1"` // might need to be *string } func (err CustomModuleError) Index() uint { return 4 } +func (err CustomModuleError) String() string { + return fmt.Sprintf("index: %d code: %d message: %s", err.index, err.err, err.message) +} + func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) + //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) //err := result.Set(scale.OK, nil) //if err != nil { // panic(err) //} - // This code chunk works - v := FailedLookup{err: DispatchOutcomeError{"failed lookup"}} - fmt.Println(v.err) - - fmt.Println("Result") - fmt.Println(result) + //// This code chunk works + //v := FailedLookup{Err: DispatchOutcomeError{"failed lookup"}} + //fmt.Println(v.Err) + // + //fmt.Println("Result") + //fmt.Println(result) switch res[1] { case 0: return nil From f99b1dd577b253cce48f1b6a4f2f10ce5bf6d4e1 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 16:29:40 -0600 Subject: [PATCH 176/245] WIP/Fix type issues in babe refactor --- lib/babe/errors.go | 55 ++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 39 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index e03a5c0525..92515d0bb2 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -63,7 +63,6 @@ var ( ) // A DispatchOutcomeError is outcome of dispatching the extrinsic -// Can I make this not a struct? type DispatchOutcomeError struct { msg string // description of error } @@ -125,49 +124,39 @@ func determineCustomModuleErr(res []byte) error { //} func determineDispatchErr(res []byte) error { - //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) + // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - //fmt.Println("Result") - //fmt.Println(result) - //var v []byte + // Am I unmarshalling the right thing here? Make sure should be res[1] err := scale.Unmarshal(res[1:], &vdt) if err != nil { - // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure - // TODO Create stucts for errors and integrate into Varying data type + // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } - - //fmt.Println("Unmarsalled dispatchErr Type:") - //fmt.Println(vdt.Value()) - + // Might have to change testing to adjust to new types + // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ case UnknownError: // For some reason its not entering here, going to customModuleError instead - // Maybe cuz struct is wrong + // Maybe cuz struct is wrong? fmt.Println("Val:") fmt.Println(val) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val.Err)} + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case FailedLookup: + // Add testing for this case, make sure struct is correct + fmt.Println("failed lookup") return &DispatchOutcomeError{"failed lookup"} case BadOrigin: + // Add testing for this case, make sure struct is correct + fmt.Println("bad origin") return &DispatchOutcomeError{"bad origin"} case CustomModuleError: // Printing nice, not getting the correct values + fmt.Println("Val2:") + fmt.Println(val) return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} } - //// if custon error module - //if vdt.Value().Index() == 4 { - // err = vdt.Set(CustomModuleError{ - // Err: DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))}, - // }) - // if err != nil { - // panic(err) - // } - //} - //fmt.Println("Unmarsalled dispatchErr Value2:") - //fmt.Println(vdt.Value()) return errInvalidResult } @@ -211,7 +200,7 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - Err string + err string } func (err UnknownError) Index() uint { @@ -219,7 +208,7 @@ func (err UnknownError) Index() uint { } type FailedLookup struct { - Err string + err string } func (err FailedLookup) Index() uint { @@ -227,7 +216,7 @@ func (err FailedLookup) Index() uint { } type BadOrigin struct { - Err string + err string } func (err BadOrigin) Index() uint { @@ -251,18 +240,6 @@ func (err CustomModuleError) String() string { func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) - //err := result.Set(scale.OK, nil) - //if err != nil { - // panic(err) - //} - - //// This code chunk works - //v := FailedLookup{Err: DispatchOutcomeError{"failed lookup"}} - //fmt.Println(v.Err) - // - //fmt.Println("Result") - //fmt.Println(result) switch res[1] { case 0: return nil From 261842a8a1926b7a037b0cf12dc288de71a793fa Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 16:35:55 -0600 Subject: [PATCH 177/245] Comments on issues to fix in for babe error handling --- lib/babe/errors.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 92515d0bb2..feb7c8e039 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -123,7 +123,13 @@ func determineCustomModuleErr(res []byte) error { // return errInvalidResult //} +/* + Two main issues I need to fix: + 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling + 2) CustomModuleError data isnt being decoded. The type is but the struct is empty + */ func determineDispatchErr(res []byte) error { + // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) From fd28d2c6d698edc04f48bbbed389bc466f7c0b08 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 09:22:10 -0600 Subject: [PATCH 178/245] WIP/refactor babe error handling with new scale pkg --- lib/babe/errors.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index feb7c8e039..26b9851b79 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -131,7 +131,8 @@ func determineCustomModuleErr(res []byte) error { func determineDispatchErr(res []byte) error { // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError - vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) + //var e := &UnknownError + vdt := scale.MustNewVaryingDataType(&UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) // Am I unmarshalling the right thing here? Make sure should be res[1] err := scale.Unmarshal(res[1:], &vdt) @@ -143,12 +144,12 @@ func determineDispatchErr(res []byte) error { // Might have to change testing to adjust to new types // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case UnknownError: + case *UnknownError: // For some reason its not entering here, going to customModuleError instead // Maybe cuz struct is wrong? fmt.Println("Val:") fmt.Println(val) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %p", val)} case FailedLookup: // Add testing for this case, make sure struct is correct fmt.Println("failed lookup") @@ -206,9 +207,11 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - err string + err *string } +//type UnknownError *string + func (err UnknownError) Index() uint { return 1 } @@ -232,7 +235,7 @@ func (err BadOrigin) Index() uint { type CustomModuleError struct { index uint8 `scale:"3"` err uint8 `scale:"2"` - message string `scale:"1"` // might need to be *string + message string `scale:"1"` // might need to be *string or an option } func (err CustomModuleError) Index() uint { From 1e1b0203469f7ca6d34d2865fe7ae800c6891f00 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:25:18 -0600 Subject: [PATCH 179/245] resolve type issue --- lib/babe/errors.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 26b9851b79..572b4d523c 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -129,27 +129,31 @@ func determineCustomModuleErr(res []byte) error { 2) CustomModuleError data isnt being decoded. The type is but the struct is empty */ func determineDispatchErr(res []byte) error { + fmt.Println("In the method") // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError //var e := &UnknownError - vdt := scale.MustNewVaryingDataType(&UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - + vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) + fmt.Println("Past vdt") // Am I unmarshalling the right thing here? Make sure should be res[1] - err := scale.Unmarshal(res[1:], &vdt) + err := scale.Unmarshal(res, &vdt) if err != nil { // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } + fmt.Println("Vdt") + fmt.Println(vdt.Value()) + // Might have to change testing to adjust to new types // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case *UnknownError: + case UnknownError: // For some reason its not entering here, going to customModuleError instead // Maybe cuz struct is wrong? fmt.Println("Val:") fmt.Println(val) - return &DispatchOutcomeError{fmt.Sprintf("unknown error: %p", val)} + return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case FailedLookup: // Add testing for this case, make sure struct is correct fmt.Println("failed lookup") @@ -163,6 +167,8 @@ func determineDispatchErr(res []byte) error { fmt.Println("Val2:") fmt.Println(val) return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} + default: // Remove this before PR lol + fmt.Println("Something is most definitly afoot at the circle K") } return errInvalidResult @@ -207,13 +213,13 @@ func determineUnknownTxnErr(res []byte) error { } type UnknownError struct { - err *string + err string } //type UnknownError *string func (err UnknownError) Index() uint { - return 1 + return 0 } type FailedLookup struct { @@ -221,7 +227,7 @@ type FailedLookup struct { } func (err FailedLookup) Index() uint { - return 2 + return 1 } type BadOrigin struct { @@ -229,7 +235,7 @@ type BadOrigin struct { } func (err BadOrigin) Index() uint { - return 3 + return 2 } type CustomModuleError struct { @@ -239,7 +245,7 @@ type CustomModuleError struct { } func (err CustomModuleError) Index() uint { - return 4 + return 3 } func (err CustomModuleError) String() string { From b3e554ea8185796110e7a36af1d349259771a5de Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:36:34 -0600 Subject: [PATCH 180/245] fixed unknown error data populating issue --- lib/babe/errors.go | 22 ++++++++-------------- lib/babe/errors_test.go | 1 + 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 572b4d523c..17a52a0dea 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -125,16 +125,14 @@ func determineCustomModuleErr(res []byte) error { /* Two main issues I need to fix: - 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling + 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling - fixed 2) CustomModuleError data isnt being decoded. The type is but the struct is empty */ func determineDispatchErr(res []byte) error { - fmt.Println("In the method") // Maybe I need to do something with this first status byte? unsure of what tho // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError - //var e := &UnknownError - vdt := scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - fmt.Println("Past vdt") + var e UnknownError + vdt := scale.MustNewVaryingDataType(e, FailedLookup{}, BadOrigin{}, CustomModuleError{}) // Am I unmarshalling the right thing here? Make sure should be res[1] err := scale.Unmarshal(res, &vdt) if err != nil { @@ -148,11 +146,7 @@ func determineDispatchErr(res []byte) error { // Might have to change testing to adjust to new types // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case UnknownError: - // For some reason its not entering here, going to customModuleError instead - // Maybe cuz struct is wrong? - fmt.Println("Val:") - fmt.Println(val) + case UnknownError: // Got it! return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case FailedLookup: // Add testing for this case, make sure struct is correct @@ -212,11 +206,11 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } -type UnknownError struct { - err string -} +//type UnknownError struct { +// err string +//} -//type UnknownError *string +type UnknownError string func (err UnknownError) Index() uint { return 0 diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 8ec2923c6d..a318f19032 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -61,6 +61,7 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } + t.Log(err.Error()) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) From a1611ae4563ae0f854cc28bd8e8ec8a99c1f13e0 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:56:37 -0600 Subject: [PATCH 181/245] WIP/Get module data to be populated when unmarshalling --- lib/babe/errors.go | 12 ++++-------- lib/babe/errors_test.go | 1 - 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 17a52a0dea..93d4f30c1c 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -206,10 +206,6 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } -//type UnknownError struct { -// err string -//} - type UnknownError string func (err UnknownError) Index() uint { @@ -233,9 +229,9 @@ func (err BadOrigin) Index() uint { } type CustomModuleError struct { - index uint8 `scale:"3"` - err uint8 `scale:"2"` - message string `scale:"1"` // might need to be *string or an option + index uint8 `scale:"3"` + err uint8 `scale:"2"` + message *string `scale:"1"` // Does scale need to consider that rust has multiple string types? } func (err CustomModuleError) Index() uint { @@ -243,7 +239,7 @@ func (err CustomModuleError) Index() uint { } func (err CustomModuleError) String() string { - return fmt.Sprintf("index: %d code: %d message: %s", err.index, err.err, err.message) + return fmt.Sprintf("index: %d code: %d message: %p", err.index, err.err, err.message) } func determineErr(res []byte) error { diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index a318f19032..8ec2923c6d 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -61,7 +61,6 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } - t.Log(err.Error()) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) From 00521173a8171f414ccc30466c8b02ad918878a0 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 16:31:08 -0600 Subject: [PATCH 182/245] resolved data population issue --- lib/babe/errors.go | 60 +++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 93d4f30c1c..3076232b2c 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -124,45 +124,34 @@ func determineCustomModuleErr(res []byte) error { //} /* - Two main issues I need to fix: - 1) Types arent being unmarshalled correctly: probably because of how I constructed them or how I am unmarshalling - fixed - 2) CustomModuleError data isnt being decoded. The type is but the struct is empty + TODO: + 1) Add test cases for 2 middle cases + 2) Expand on this to include other error types + 3) Rebase + 4) Clean up code + 5) Make sure everything I do is okay (errors returned and printing as hex instead of string). This could be included in a pr + 6) PR??? */ -func determineDispatchErr(res []byte) error { - // Maybe I need to do something with this first status byte? unsure of what tho - // If not encoded with thees types, will they still evaluate? Maybe thats why they are going to customModuleError - var e UnknownError - vdt := scale.MustNewVaryingDataType(e, FailedLookup{}, BadOrigin{}, CustomModuleError{}) - // Am I unmarshalling the right thing here? Make sure should be res[1] +func determineDispatchErr(res []byte) error { // This works yay! + var e Other + vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) if err != nil { // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } - fmt.Println("Vdt") - fmt.Println(vdt.Value()) - - // Might have to change testing to adjust to new types - // Something is wrong with my types: Not being properly recognized switch val := vdt.Value().(type){ - case UnknownError: // Got it! + case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} - case FailedLookup: + case CannotLookup: // Add testing for this case, make sure struct is correct - fmt.Println("failed lookup") return &DispatchOutcomeError{"failed lookup"} case BadOrigin: // Add testing for this case, make sure struct is correct - fmt.Println("bad origin") return &DispatchOutcomeError{"bad origin"} - case CustomModuleError: - // Printing nice, not getting the correct values - fmt.Println("Val2:") - fmt.Println(val) + case Module: return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} - default: // Remove this before PR lol - fmt.Println("Something is most definitly afoot at the circle K") } return errInvalidResult @@ -206,17 +195,17 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } -type UnknownError string +type Other string -func (err UnknownError) Index() uint { +func (err Other) Index() uint { return 0 } -type FailedLookup struct { +type CannotLookup struct { err string } -func (err FailedLookup) Index() uint { +func (err CannotLookup) Index() uint { return 1 } @@ -228,18 +217,19 @@ func (err BadOrigin) Index() uint { return 2 } -type CustomModuleError struct { - index uint8 `scale:"3"` - err uint8 `scale:"2"` - message *string `scale:"1"` // Does scale need to consider that rust has multiple string types? +type Module struct { // add in `scale:"1"` after + Idx uint8 + Err uint8 + Message *string } -func (err CustomModuleError) Index() uint { +func (err Module) Index() uint { return 3 } -func (err CustomModuleError) String() string { - return fmt.Sprintf("index: %d code: %d message: %p", err.index, err.err, err.message) +func (err Module) String() string { + // Make sure this is okay as opposed to being a string. Should be fine since its wrapped in the error + return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } func determineErr(res []byte) error { From c2a2cf3c4cda4ab2d59a34f2ad630b06990c86ed Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 30 Apr 2021 12:11:04 -0400 Subject: [PATCH 183/245] add mtx --- pkg/scale/scale.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 855016b8ca..0b1dd49ffb 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -17,7 +17,10 @@ package scale import ( + "bytes" + "encoding/binary" "fmt" + "math/big" "reflect" "sort" "strings" From a2b2ccb8fc5c809761dec922e43e8595a771e7d8 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 4 May 2021 10:57:34 -0400 Subject: [PATCH 184/245] add variable data type interface, test to compare old vs new encoding --- pkg/scale/scale.go | 10 +++-- pkg/scale/scale_test.go | 88 ----------------------------------------- 2 files changed, 7 insertions(+), 91 deletions(-) delete mode 100644 pkg/scale/scale_test.go diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 0b1dd49ffb..ac6530bae3 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -17,16 +17,20 @@ package scale import ( - "bytes" - "encoding/binary" "fmt" - "math/big" "reflect" "sort" "strings" "sync" ) +type VaryingDataType []VaryingDataTypeValue + +// VaryingDataType is used to represent scale encodable types +type VaryingDataTypeValue interface { + Index() uint +} + // package level cache for fieldScaleIndicies var cache = &fieldScaleIndicesCache{ cache: make(map[string]fieldScaleIndices), diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go deleted file mode 100644 index a96ab2b539..0000000000 --- a/pkg/scale/scale_test.go +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package scale - -import ( - "reflect" - "testing" -) - -func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { - tests := []struct { - name string - in interface{} - wantIndices fieldScaleIndices - wantErr bool - }{ - { - in: struct{ Foo int }{}, - wantIndices: fieldScaleIndices{ - { - fieldIndex: 0, - }, - }, - }, - { - in: struct { - End1 bool - Baz bool `scale:"3"` - End2 []byte - Bar int32 `scale:"2"` - End3 []byte - Foo []byte `scale:"1"` - }{}, - wantIndices: fieldScaleIndices{ - { - fieldIndex: 5, - scaleIndex: newStringPtr("1"), - }, - { - fieldIndex: 3, - scaleIndex: newStringPtr("2"), - }, - { - fieldIndex: 1, - scaleIndex: newStringPtr("3"), - }, - { - fieldIndex: 0, - }, - { - fieldIndex: 2, - }, - { - fieldIndex: 4, - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fsic := &fieldScaleIndicesCache{ - cache: make(map[string]fieldScaleIndices), - } - _, gotIndices, err := fsic.fieldScaleIndices(tt.in) - if (err != nil) != tt.wantErr { - t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(gotIndices, tt.wantIndices) { - t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() gotIndices = %v, want %v", gotIndices, tt.wantIndices) - } - }) - } -} From dc52cf9f21a467820851d77cbe8f6f7c0b1df9e0 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 6 May 2021 11:29:34 -0400 Subject: [PATCH 185/245] wip --- pkg/scale/decode.go | 12 ++++ pkg/scale/decode_test.go | 118 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index e551c6a9d1..ab18b05439 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -749,3 +749,15 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { dstv.Set(reflect.ValueOf(ui128)) return } + +// decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 +// if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise +// it returns nil and error +func (ds *decodeState) decodeUint128() (out *Uint128, err error) { + buf := make([]byte, 16) + err = binary.Read(ds, binary.LittleEndian, buf) + if err != nil { + return nil, err + } + return NewUint128(buf) +} diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 2a3400d907..ad8f79b1bd 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -424,3 +424,121 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { }) } } + +func Test_decodeState_decodeBigInt(t *testing.T) { + var ( + bi *big.Int + ) + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + name: "error case, ensure **big.Int", + args: args{ + data: []byte{0x00}, + dst: bi, + }, + wantErr: true, + }, + { + args: args{ + data: []byte{0x00}, + dst: &bi, + }, + want: big.NewInt(0), + }, + { + args: args{ + data: []byte{0x04}, + dst: &bi, + }, + want: big.NewInt(1), + }, + { + args: args{ + data: []byte{0xa8}, + dst: &bi, + }, + want: big.NewInt(42), + }, + { + args: args{ + data: []byte{0x01, 0x01}, + dst: &bi, + }, + want: big.NewInt(64), + }, + { + args: args{ + data: []byte{0x15, 0x01}, + dst: &bi, + }, + want: big.NewInt(69), + }, + { + args: args{ + data: []byte{0xfd, 0xff}, + dst: &bi, + }, + want: big.NewInt(16383), + }, + { + args: args{ + data: []byte{0x02, 0x00, 0x01, 0x00}, + dst: &bi, + }, + want: big.NewInt(16384), + }, + { + args: args{ + data: []byte{0xfe, 0xff, 0xff, 0xff}, + dst: &bi, + }, + want: big.NewInt(1073741823), + }, + { + args: args{ + data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, + dst: &bi, + }, + want: big.NewInt(1073741824), + }, + { + args: args{ + data: []byte{0x03, 0xff, 0xff, 0xff, 0xff}, + dst: &bi, + }, + want: big.NewInt(1<<32 - 1), + }, + { + args: args{ + data: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, + dst: &bi, + }, + want: big.NewInt(1 << 32), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From 23ace4a045d793c4c34b011565bc519107316c96 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 14 May 2021 12:14:16 -0400 Subject: [PATCH 186/245] []byte, string decoding --- pkg/scale/decode.go | 4 +- pkg/scale/decode_test.go | 117 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index ab18b05439..7ee242bd6c 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -575,7 +575,7 @@ func (ds *decodeState) decodeBytes(dstv reflect.Value) (err error) { return } - b := make([]byte, length) + b = make([]byte, length) _, err = ds.Read(b) if err != nil { return @@ -753,7 +753,7 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { // decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 // if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise // it returns nil and error -func (ds *decodeState) decodeUint128() (out *Uint128, err error) { +func (ds *decodeState) decodeUint128() (ui *Uint128, err error) { buf := make([]byte, 16) err = binary.Read(ds, binary.LittleEndian, buf) if err != nil { diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index ad8f79b1bd..ca0ea4d69b 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -542,3 +542,120 @@ func Test_decodeState_decodeBigInt(t *testing.T) { }) } } + +func Test_decodeState_decodeBytes(t *testing.T) { + var b []byte + var s string + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + args: args{ + data: []byte{0x04, 0x01}, + dst: &b, + }, + want: []byte{0x01}, + }, + { + args: args{ + data: []byte{0x04, 0xff}, + dst: &b, + }, + want: []byte{0xff}, + }, + { + args: args{ + data: []byte{0x08, 0x01, 0x01}, + dst: &b, + }, + want: []byte{0x01, 0x01}, + }, + { + args: args{ + data: append([]byte{0x01, 0x01}, byteArray(64)...), + dst: &b, + }, + want: byteArray(64), + }, + { + args: args{ + data: append([]byte{0xfd, 0xff}, byteArray(16383)...), + dst: &b, + }, + want: byteArray(16383), + }, + { + args: args{ + data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), + dst: &b, + }, + want: byteArray(16384), + }, + // string + { + args: args{ + data: []byte{0x04, 0x01}, + dst: &s, + }, + want: string([]byte{0x01}), + }, + { + args: args{ + data: []byte{0x04, 0xff}, + dst: &s, + }, + want: string([]byte{0xff}), + }, + { + args: args{ + data: []byte{0x08, 0x01, 0x01}, + dst: &s, + }, + want: string([]byte{0x01, 0x01}), + }, + { + args: args{ + data: append([]byte{0x01, 0x01}, byteArray(64)...), + dst: &s, + }, + want: string(byteArray(64)), + }, + { + args: args{ + data: append([]byte{0xfd, 0xff}, byteArray(16383)...), + dst: &s, + }, + want: string(byteArray(16383)), + }, + { + args: args{ + data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), + dst: &s, + }, + want: string(byteArray(16384)), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From 91731d3f647100980ba3387f737b262369841c73 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 14 May 2021 13:32:09 -0400 Subject: [PATCH 187/245] encodeBool and tests --- pkg/scale/decode_test.go | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index ca0ea4d69b..b69db5067d 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -659,3 +659,56 @@ func Test_decodeState_decodeBytes(t *testing.T) { }) } } + +func Test_decodeState_decodeBool(t *testing.T) { + var b bool + type args struct { + data []byte + dst interface{} + } + tests := []struct { + name string + args args + wantErr bool + want interface{} + }{ + { + args: args{ + data: []byte{0x01}, + dst: &b, + }, + want: true, + }, + { + args: args{ + data: []byte{0x00}, + dst: &b, + }, + want: false, + }, + { + name: "error case", + args: args{ + data: []byte{0x03}, + dst: &b, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Unmarshal(tt.args.data, tt.args.dst) + if (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil { + return + } + got := reflect.ValueOf(tt.args.dst).Elem().Interface() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + } + }) + } +} From a5dcf600c36237177e6e4a68d0ba4075ff91a34f Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 19 May 2021 22:24:58 -0400 Subject: [PATCH 188/245] refactor tests, include optionality tests --- pkg/scale/encode_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 6681662f65..fac6fb4669 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -169,6 +169,8 @@ var ( in: uint(9223372036854775807), want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + int64Tests = tests{ { name: "myCustomUint(9223372036854775807)", in: myCustomUint(9223372036854775807), @@ -205,6 +207,8 @@ var ( in: int64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + uint64Tests = tests{ { name: "myCustomInt64(9223372036854775807)", in: myCustomInt64(9223372036854775807), @@ -239,6 +243,8 @@ var ( in: uint64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + } + int32Tests = tests{ { name: "myCustomUint64(9223372036854775807)", in: myCustomUint64(9223372036854775807), @@ -290,6 +296,8 @@ var ( in: uint32(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f}, }, + } + int8Tests = tests{ { name: "uint32(1073741823)", in: myCustomUint32(1073741823), @@ -321,6 +329,8 @@ var ( in: uint8(1), want: []byte{0x01}, }, + } + int16Tests = tests{ { name: "myCustomInt8(1)", in: myCustomUint8(1), @@ -345,6 +355,8 @@ var ( in: int16(16383), want: []byte{0xff, 0x3f}, }, + } + uint16Tests = tests{ { name: "myCustomInt16(16383)", in: myCustomInt16(16383), From 73172f6e0025fb5e7ec010c40247881aeac4b286 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 21 May 2021 17:17:04 -0400 Subject: [PATCH 189/245] use shared tests in decode --- pkg/scale/decode_test.go | 330 ++++++++------------------------------- 1 file changed, 68 insertions(+), 262 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index b69db5067d..04a2796512 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -17,7 +17,6 @@ package scale import ( - "math/big" "reflect" "testing" @@ -426,289 +425,96 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { } func Test_decodeState_decodeBigInt(t *testing.T) { - var ( - bi *big.Int - ) - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - name: "error case, ensure **big.Int", - args: args{ - data: []byte{0x00}, - dst: bi, - }, - wantErr: true, - }, - { - args: args{ - data: []byte{0x00}, - dst: &bi, - }, - want: big.NewInt(0), - }, - { - args: args{ - data: []byte{0x04}, - dst: &bi, - }, - want: big.NewInt(1), - }, - { - args: args{ - data: []byte{0xa8}, - dst: &bi, - }, - want: big.NewInt(42), - }, - { - args: args{ - data: []byte{0x01, 0x01}, - dst: &bi, - }, - want: big.NewInt(64), - }, - { - args: args{ - data: []byte{0x15, 0x01}, - dst: &bi, - }, - want: big.NewInt(69), - }, - { - args: args{ - data: []byte{0xfd, 0xff}, - dst: &bi, - }, - want: big.NewInt(16383), - }, - { - args: args{ - data: []byte{0x02, 0x00, 0x01, 0x00}, - dst: &bi, - }, - want: big.NewInt(16384), - }, - { - args: args{ - data: []byte{0xfe, 0xff, 0xff, 0xff}, - dst: &bi, - }, - want: big.NewInt(1073741823), - }, - { - args: args{ - data: []byte{0x03, 0x00, 0x00, 0x00, 0x40}, - dst: &bi, - }, - want: big.NewInt(1073741824), - }, - { - args: args{ - data: []byte{0x03, 0xff, 0xff, 0xff, 0xff}, - dst: &bi, - }, - want: big.NewInt(1<<32 - 1), - }, - { - args: args{ - data: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, - dst: &bi, - }, - want: big.NewInt(1 << 32), - }, - } - for _, tt := range tests { + for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } func Test_decodeState_decodeBytes(t *testing.T) { - var b []byte - var s string - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - args: args{ - data: []byte{0x04, 0x01}, - dst: &b, - }, - want: []byte{0x01}, - }, - { - args: args{ - data: []byte{0x04, 0xff}, - dst: &b, - }, - want: []byte{0xff}, - }, - { - args: args{ - data: []byte{0x08, 0x01, 0x01}, - dst: &b, - }, - want: []byte{0x01, 0x01}, - }, - { - args: args{ - data: append([]byte{0x01, 0x01}, byteArray(64)...), - dst: &b, - }, - want: byteArray(64), - }, - { - args: args{ - data: append([]byte{0xfd, 0xff}, byteArray(16383)...), - dst: &b, - }, - want: byteArray(16383), - }, - { - args: args{ - data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), - dst: &b, - }, - want: byteArray(16384), - }, - // string - { - args: args{ - data: []byte{0x04, 0x01}, - dst: &s, - }, - want: string([]byte{0x01}), - }, - { - args: args{ - data: []byte{0x04, 0xff}, - dst: &s, - }, - want: string([]byte{0xff}), - }, - { - args: args{ - data: []byte{0x08, 0x01, 0x01}, - dst: &s, - }, - want: string([]byte{0x01, 0x01}), - }, - { - args: args{ - data: append([]byte{0x01, 0x01}, byteArray(64)...), - dst: &s, - }, - want: string(byteArray(64)), - }, - { - args: args{ - data: append([]byte{0xfd, 0xff}, byteArray(16383)...), - dst: &s, - }, - want: string(byteArray(16383)), - }, - { - args: args{ - data: append([]byte{0x02, 0x00, 0x01, 0x00}, byteArray(16384)...), - dst: &s, - }, - want: string(byteArray(16384)), - }, - } - for _, tt := range tests { + for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return } - if err != nil { - return - } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } func Test_decodeState_decodeBool(t *testing.T) { - var b bool - type args struct { - data []byte - dst interface{} - } - tests := []struct { - name string - args args - wantErr bool - want interface{} - }{ - { - args: args{ - data: []byte{0x01}, - dst: &b, - }, - want: true, - }, - { - args: args{ - data: []byte{0x00}, - dst: &b, - }, - want: false, - }, - { - name: "error case", - args: args{ - data: []byte{0x03}, - dst: &b, - }, - wantErr: true, - }, - } - for _, tt := range tests { + for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - err := Unmarshal(tt.args.data, tt.args.dst) - if (err != nil) != tt.wantErr { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return } - got := reflect.ValueOf(tt.args.dst).Elem().Interface() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } } + +// func Test_decodeState_decodeBool(t *testing.T) { +// var b bool +// type args struct { +// data []byte +// dst interface{} +// } +// tests := []struct { +// name string +// args args +// wantErr bool +// want interface{} +// }{ +// { +// args: args{ +// data: []byte{0x01}, +// dst: &b, +// }, +// want: true, +// }, +// { +// args: args{ +// data: []byte{0x00}, +// dst: &b, +// }, +// want: false, +// }, +// { +// name: "error case", +// args: args{ +// data: []byte{0x03}, +// dst: &b, +// }, +// wantErr: true, +// }, +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// err := Unmarshal(tt.args.data, tt.args.dst) +// if (err != nil) != tt.wantErr { +// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// return +// } +// if err != nil { +// return +// } +// got := reflect.ValueOf(tt.args.dst).Elem().Interface() +// if !reflect.DeepEqual(got, tt.want) { +// t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) +// } +// }) +// } +// } From 681384acd53ed681e0c66ada6303e56ce8a53897 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 26 May 2021 11:25:29 -0400 Subject: [PATCH 190/245] struct decode tests, and unmarshal refactor --- pkg/scale/decode_test.go | 81 ++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 53 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 04a2796512..4d685f2f19 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -439,7 +439,7 @@ func Test_decodeState_decodeBigInt(t *testing.T) { } func Test_decodeState_decodeBytes(t *testing.T) { - for _, tt := range boolTests { + for _, tt := range stringTests { t.Run(tt.name, func(t *testing.T) { dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { @@ -466,55 +466,30 @@ func Test_decodeState_decodeBool(t *testing.T) { } } -// func Test_decodeState_decodeBool(t *testing.T) { -// var b bool -// type args struct { -// data []byte -// dst interface{} -// } -// tests := []struct { -// name string -// args args -// wantErr bool -// want interface{} -// }{ -// { -// args: args{ -// data: []byte{0x01}, -// dst: &b, -// }, -// want: true, -// }, -// { -// args: args{ -// data: []byte{0x00}, -// dst: &b, -// }, -// want: false, -// }, -// { -// name: "error case", -// args: args{ -// data: []byte{0x03}, -// dst: &b, -// }, -// wantErr: true, -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// err := Unmarshal(tt.args.data, tt.args.dst) -// if (err != nil) != tt.wantErr { -// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// if err != nil { -// return -// } -// got := reflect.ValueOf(tt.args.dst).Elem().Interface() -// if !reflect.DeepEqual(got, tt.want) { -// t.Errorf("decodeState.unmarshal() = %v, want %v", got, tt.want) -// } -// }) -// } -// } +func Test_decodeState_decodeStruct(t *testing.T) { + for _, tt := range structTests { + t.Run(tt.name, func(t *testing.T) { + dst := reflect.ValueOf(tt.in).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + switch reflect.ValueOf(tt.in).Kind() { + case reflect.Ptr: + if reflect.ValueOf(dst).IsZero() { + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } else { + // have to do this since reflect.DeepEqual won't come back true for different addresses + if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } + default: + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + } + }) + } +} From 0770139771b6378f84cb3e8080e7580e6c9030b6 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 27 May 2021 16:00:27 -0400 Subject: [PATCH 191/245] wip --- pkg/scale/decode_test.go | 132 +++++++++++++++++++++++++++++++++------ pkg/scale/encode_test.go | 13 ++++ 2 files changed, 125 insertions(+), 20 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 4d685f2f19..425384d4d9 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -17,6 +17,7 @@ package scale import ( + "math/big" "reflect" "testing" @@ -427,7 +428,8 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { func Test_decodeState_decodeBigInt(t *testing.T) { for _, tt := range bigIntTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + var dst *big.Int + // dst := reflect.ValueOf(tt.in).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -455,7 +457,8 @@ func Test_decodeState_decodeBytes(t *testing.T) { func Test_decodeState_decodeBool(t *testing.T) { for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + // dst := reflect.ValueOf(tt.in).Interface() + var dst bool if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -466,29 +469,118 @@ func Test_decodeState_decodeBool(t *testing.T) { } } +func Test_decodeState_decodeStructManual(t *testing.T) { + // nil case + var dst *MyStruct = nil + var b = []byte{0} + var want *MyStruct = nil + + // dst = structTests[0].dst + + err := Unmarshal(b, &dst) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + + // zero case MyStruct + var dst1 *MyStruct = &MyStruct{} + err = Unmarshal(b, &dst1) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst1, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + + // zero case MyStruct + var dst2 *MyStruct = &MyStruct{Baz: true} + err = Unmarshal(b, &dst2) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !reflect.DeepEqual(dst2, want) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) + } + +} + func Test_decodeState_decodeStruct(t *testing.T) { - for _, tt := range structTests { + for _, tt := range append([]test{}, structTests[0]) { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + // dst := tt.dst if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - switch reflect.ValueOf(tt.in).Kind() { - case reflect.Ptr: - if reflect.ValueOf(dst).IsZero() { - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - } else { - // have to do this since reflect.DeepEqual won't come back true for different addresses - if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - } - default: - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } + // dstv := reflect.ValueOf(dst) + // inv := reflect.ValueOf(tt.in) + + // if dstv.Kind() != inv.Kind() { + // t.Errorf("decodeState.unmarshal() differing kind = %T, want %T", dst, tt.in) + // return + // } + + // switch inv.Kind() { + // case reflect.Ptr: + // fmt.Println(dst, dstv.Interface(), tt.in, inv.Interface()) + // if inv.IsZero() { + // fmt.Println(dst, tt.in, dstv.Interface(), inv.Interface()) + // if !reflect.DeepEqual(dstv.Interface(), tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } else if inv.IsNil() { + // if !reflect.DeepEqual(dst, tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } else { + // // // have to do this since reflect.DeepEqual won't come back true for different addresses + // // if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { + // // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // // } + // if !reflect.DeepEqual(dst, inv) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } + // default: + // if !reflect.DeepEqual(dst, tt.in) { + // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + // } + // } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} +func Test_decodeState_decodeArray(t *testing.T) { + for _, tt := range arrayTests { + t.Run(tt.name, func(t *testing.T) { + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} + +func Test_decodeState_decodeSlice(t *testing.T) { + for _, tt := range sliceTests { + t.Run(tt.name, func(t *testing.T) { + // dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } }) } diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index fac6fb4669..86c50ec32e 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -26,6 +26,7 @@ import ( type test struct { name string in interface{} + dst interface{} wantErr bool want []byte out interface{} @@ -574,6 +575,7 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool}", @@ -587,6 +589,7 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, + dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool} with untagged attributes", @@ -855,51 +858,61 @@ var ( name: "[4]int{1, 2, 3, 4}", in: [4]int{1, 2, 3, 4}, want: []byte{0x04, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{16384, 2, 3, 4}", in: [4]int{16384, 2, 3, 4}, want: []byte{0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{1073741824, 2, 3, 4}", in: [4]int{1073741824, 2, 3, 4}, want: []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, + dst: [4]int{}, }, { name: "[4]int{1 << 32, 2, 3, 1 << 32}", in: [4]int{1 << 32, 2, 3, 1 << 32}, want: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, + dst: [4]int{}, }, { name: "[3]bool{true, false, true}", in: [3]bool{true, false, true}, want: []byte{0x01, 0x00, 0x01}, + dst: [3]bool{}, }, { name: "[2][]int{{0, 1}, {1, 0}}", in: [2][]int{{0, 1}, {1, 0}}, want: []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, + dst: [2]int{}, }, { name: "[2][2]int{{0, 1}, {1, 0}}", in: [2][2]int{{0, 1}, {1, 0}}, want: []byte{0x00, 0x04, 0x04, 0x00}, + dst: [2][2]int{}, }, { name: "[2]*big.Int{big.NewInt(0), big.NewInt(1)}", in: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, want: []byte{0x00, 0x04}, + dst: [2]*big.Int{}, }, { name: "[2][]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, + // dst: [2][]byte{{0x00, 0x01}, {0x01, 0x00}} }, { name: "[2][2]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x00, 0x01, 0x01, 0x00}, + dst: [2][2]byte{}, }, } From 9cb4d82033c82653b376526652bd29eb39bf831e Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 28 May 2021 10:37:33 -0400 Subject: [PATCH 192/245] decode of VariantDataType, wip tests --- pkg/scale/comparison_test.go | 2 +- pkg/scale/encode.go | 16 +++++++++++++++- pkg/scale/scale.go | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index b2367c0239..c56f721d64 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -130,7 +130,7 @@ func TestOldVsNewEncoding(t *testing.T) { ), } - newEncode, err := Marshal(newDigest) + newEncode, err := scale.Marshal(newDigest) if err != nil { t.Errorf("unexpected err: %v", err) return diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 7c62a77bd5..cd6ff6890f 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/binary" "fmt" + "log" "math/big" "reflect" ) @@ -90,7 +91,20 @@ func (es *encodeState) marshal(in interface{}) (err error) { case reflect.Array: err = es.encodeArray(in) case reflect.Slice: - err = es.encodeSlice(in) + t := reflect.TypeOf(in) + // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType + switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { + case true: + invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) + switch in := invdt.Interface().(type) { + case VaryingDataType: + err = es.encodeVaryingDataType(in) + default: + log.Panicf("this should never happen") + } + case false: + err = es.encodeSlice(in) + } default: err = fmt.Errorf("unsupported type: %T", in) } diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index ac6530bae3..698ad6983f 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -24,8 +24,31 @@ import ( "sync" ) +type varyingDataTypeCache map[string]map[uint]VaryingDataTypeValue + +var vdtCache varyingDataTypeCache = make(varyingDataTypeCache) + type VaryingDataType []VaryingDataTypeValue +func RegisterVaryingDataType(in interface{}, values ...VaryingDataTypeValue) (err error) { + _, ok := in.(VaryingDataType) + if !ok { + err = fmt.Errorf("%T is not a VaryingDataType", in) + } + + t := reflect.TypeOf(in) + key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) + + _, ok = vdtCache[key] + if !ok { + vdtCache[key] = make(map[uint]VaryingDataTypeValue) + } + for _, val := range values { + vdtCache[key][val.Index()] = val + } + return +} + // VaryingDataType is used to represent scale encodable types type VaryingDataTypeValue interface { Index() uint From 1b7bc6b1a19c670a0218a8d890af3f511f6e01c9 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 28 May 2021 16:07:37 -0400 Subject: [PATCH 193/245] add optionality testing --- pkg/scale/decode_test.go | 68 +++++++++++++++++++--------------------- pkg/scale/encode_test.go | 13 -------- 2 files changed, 33 insertions(+), 48 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 425384d4d9..c5a680ea4f 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -504,7 +504,6 @@ func Test_decodeState_decodeStructManual(t *testing.T) { if !reflect.DeepEqual(dst2, want) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) } - } func Test_decodeState_decodeStruct(t *testing.T) { @@ -516,40 +515,6 @@ func Test_decodeState_decodeStruct(t *testing.T) { if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - // dstv := reflect.ValueOf(dst) - // inv := reflect.ValueOf(tt.in) - - // if dstv.Kind() != inv.Kind() { - // t.Errorf("decodeState.unmarshal() differing kind = %T, want %T", dst, tt.in) - // return - // } - - // switch inv.Kind() { - // case reflect.Ptr: - // fmt.Println(dst, dstv.Interface(), tt.in, inv.Interface()) - // if inv.IsZero() { - // fmt.Println(dst, tt.in, dstv.Interface(), inv.Interface()) - // if !reflect.DeepEqual(dstv.Interface(), tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } else if inv.IsNil() { - // if !reflect.DeepEqual(dst, tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } else { - // // // have to do this since reflect.DeepEqual won't come back true for different addresses - // // if !reflect.DeepEqual(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Elem().Interface()) { - // // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // // } - // if !reflect.DeepEqual(dst, inv) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } - // default: - // if !reflect.DeepEqual(dst, tt.in) { - // t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - // } - // } if !reflect.DeepEqual(dst, tt.in) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) } @@ -585,3 +550,36 @@ func Test_decodeState_decodeSlice(t *testing.T) { }) } } + +func Test_unmarshal_optionality(t *testing.T) { + var ptrTests tests + for _, t := range allTests { + ptrTest := test{ + name: t.name, + in: t.in, + wantErr: t.wantErr, + want: t.want, + } + switch t.in { + case nil: + // this doesn't actually happen since none of the tests have nil value for tt.in + ptrTest.want = []byte{0x00} + default: + ptrTest.want = append([]byte{0x01}, t.want...) + } + ptrTests = append(ptrTests, ptrTest) + } + for _, tt := range ptrTests { + t.Run(tt.name, func(t *testing.T) { + // this becomes a pointer to a zero value of the underlying value + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + // es := &encodeState{fieldScaleIndicesCache: cache} + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(dst, &tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } + }) + } +} diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 86c50ec32e..fac6fb4669 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -26,7 +26,6 @@ import ( type test struct { name string in interface{} - dst interface{} wantErr bool want []byte out interface{} @@ -575,7 +574,6 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool}", @@ -589,7 +587,6 @@ var ( Baz: true, }, want: []byte{0x04, 0x01, 0x02, 0, 0, 0, 0x01}, - dst: MyStruct{}, }, { name: "struct {[]byte, int32, bool} with untagged attributes", @@ -858,61 +855,51 @@ var ( name: "[4]int{1, 2, 3, 4}", in: [4]int{1, 2, 3, 4}, want: []byte{0x04, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{16384, 2, 3, 4}", in: [4]int{16384, 2, 3, 4}, want: []byte{0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{1073741824, 2, 3, 4}", in: [4]int{1073741824, 2, 3, 4}, want: []byte{0x03, 0x00, 0x00, 0x00, 0x40, 0x08, 0x0c, 0x10}, - dst: [4]int{}, }, { name: "[4]int{1 << 32, 2, 3, 1 << 32}", in: [4]int{1 << 32, 2, 3, 1 << 32}, want: []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01}, - dst: [4]int{}, }, { name: "[3]bool{true, false, true}", in: [3]bool{true, false, true}, want: []byte{0x01, 0x00, 0x01}, - dst: [3]bool{}, }, { name: "[2][]int{{0, 1}, {1, 0}}", in: [2][]int{{0, 1}, {1, 0}}, want: []byte{0x08, 0x00, 0x04, 0x08, 0x04, 0x00}, - dst: [2]int{}, }, { name: "[2][2]int{{0, 1}, {1, 0}}", in: [2][2]int{{0, 1}, {1, 0}}, want: []byte{0x00, 0x04, 0x04, 0x00}, - dst: [2][2]int{}, }, { name: "[2]*big.Int{big.NewInt(0), big.NewInt(1)}", in: [2]*big.Int{big.NewInt(0), big.NewInt(1)}, want: []byte{0x00, 0x04}, - dst: [2]*big.Int{}, }, { name: "[2][]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x08, 0x00, 0x01, 0x08, 0x01, 0x00}, - // dst: [2][]byte{{0x00, 0x01}, {0x01, 0x00}} }, { name: "[2][2]byte{{0x00, 0x01}, {0x01, 0x00}}", in: [2][2]byte{{0x00, 0x01}, {0x01, 0x00}}, want: []byte{0x00, 0x01, 0x01, 0x00}, - dst: [2][2]byte{}, }, } From 7c87050de2234fccbc71a7a4b1f37bd47e8305fe Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 31 May 2021 13:42:36 -0400 Subject: [PATCH 194/245] fix struct tests and optionality tests --- go.mod | 6 +++++ pkg/scale/decode_test.go | 49 +++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index b41e086388..8472dcecb6 100644 --- a/go.mod +++ b/go.mod @@ -23,10 +23,14 @@ require ( <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD +<<<<<<< HEAD +======= +>>>>>>> fix struct tests and optionality tests github.com/google/go-cmp v0.5.6 ======= github.com/google/go-cmp v0.5.6 // indirect >>>>>>> fix struct tests and optionality tests +<<<<<<< HEAD ======= github.com/google/go-cmp v0.5.6 >>>>>>> integrate scale pkg into lib/transaction @@ -36,6 +40,8 @@ require ( ======= github.com/google/go-cmp v0.5.6 >>>>>>> integrate scale pkg into lib/transaction +======= +>>>>>>> fix struct tests and optionality tests github.com/google/uuid v1.1.5 // indirect github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index c5a680ea4f..6de40ee070 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -457,7 +457,6 @@ func Test_decodeState_decodeBytes(t *testing.T) { func Test_decodeState_decodeBool(t *testing.T) { for _, tt := range boolTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() var dst bool if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) @@ -475,8 +474,6 @@ func Test_decodeState_decodeStructManual(t *testing.T) { var b = []byte{0} var want *MyStruct = nil - // dst = structTests[0].dst - err := Unmarshal(b, &dst) if err != nil { t.Errorf("unexpected error: %v", err) @@ -507,16 +504,20 @@ func Test_decodeState_decodeStructManual(t *testing.T) { } func Test_decodeState_decodeStruct(t *testing.T) { - for _, tt := range append([]test{}, structTests[0]) { + for _, tt := range structTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() - // dst := tt.dst + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + var diff string + if tt.out != nil { + diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(tt.in)) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) } }) } @@ -524,8 +525,7 @@ func Test_decodeState_decodeStruct(t *testing.T) { func Test_decodeState_decodeArray(t *testing.T) { for _, tt := range arrayTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -539,8 +539,7 @@ func Test_decodeState_decodeArray(t *testing.T) { func Test_decodeState_decodeSlice(t *testing.T) { for _, tt := range sliceTests { t.Run(tt.name, func(t *testing.T) { - // dst := reflect.ValueOf(tt.in).Interface() - dst := reflect.New(reflect.Indirect(reflect.ValueOf(tt.in)).Type()).Elem().Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } @@ -559,6 +558,7 @@ func Test_unmarshal_optionality(t *testing.T) { in: t.in, wantErr: t.wantErr, want: t.want, + out: t.out, } switch t.in { case nil: @@ -573,12 +573,29 @@ func Test_unmarshal_optionality(t *testing.T) { t.Run(tt.name, func(t *testing.T) { // this becomes a pointer to a zero value of the underlying value dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - // es := &encodeState{fieldScaleIndicesCache: cache} if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) } - if !reflect.DeepEqual(dst, &tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + + if tt.wantErr == true { + return + } + + switch reflect.TypeOf(tt.in).Kind() { + case reflect.Struct: + var diff string + if tt.out != nil { + diff = cmp.Diff(dst, &tt.out, cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(dst, &tt.in, cmpopts.IgnoreUnexported(tt.in)) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + default: + if !reflect.DeepEqual(dst, &tt.in) { + t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) + } } }) } From 1526f7447e2dd2781dee7c928bcbebb1459a7b2d Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 31 May 2021 22:54:57 -0400 Subject: [PATCH 195/245] test VaryingDataType --- pkg/scale/decode.go | 16 +++++++++++++++- pkg/scale/decode_test.go | 30 ++++++++++-------------------- pkg/scale/encode.go | 17 ++++++++++++++++- pkg/scale/scale.go | 30 ------------------------------ 4 files changed, 41 insertions(+), 52 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 7ee242bd6c..7d3f231fa3 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -139,7 +139,21 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Slice: err = ds.decodeSlice(dstv) default: - err = fmt.Errorf("unsupported type: %T", in) + _, ok := in.(VaryingDataTypeValue) + switch ok { + case true: + t := reflect.TypeOf(in) + switch t.Kind() { + // TODO: support more primitive types. Do we need to support arrays and slices as well? + case reflect.Int: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + case reflect.Int16: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + } + in, err = ds.unmarshal(reflect.ValueOf(in)) + default: + err = fmt.Errorf("unsupported type: %T", in) + } } } return diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 6de40ee070..fab0147b0a 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -514,7 +514,7 @@ func Test_decodeState_decodeStruct(t *testing.T) { if tt.out != nil { diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in)) } else { - diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(tt.in)) + diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(big.Int{}, tt.in, VDTValue2{}, MyStructWithIgnore{})) } if diff != "" { t.Errorf("decodeState.unmarshal() = %s", diff) @@ -575,28 +575,18 @@ func Test_unmarshal_optionality(t *testing.T) { dst := reflect.New(reflect.TypeOf(tt.in)).Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - - if tt.wantErr == true { return } - - switch reflect.TypeOf(tt.in).Kind() { - case reflect.Struct: - var diff string - if tt.out != nil { - diff = cmp.Diff(dst, &tt.out, cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(dst, &tt.in, cmpopts.IgnoreUnexported(tt.in)) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - default: - if !reflect.DeepEqual(dst, &tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) } } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index cd6ff6890f..1c50802f61 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -106,7 +106,22 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - err = fmt.Errorf("unsupported type: %T", in) + _, ok := in.(VaryingDataTypeValue) + switch ok { + case true: + t := reflect.TypeOf(in) + switch t.Kind() { + // TODO: support more primitive types. Do we need to support arrays and slices as well? + case reflect.Int: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + case reflect.Int16: + in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + } + err = es.marshal(in) + default: + err = fmt.Errorf("unsupported type: %T", in) + } + } } return diff --git a/pkg/scale/scale.go b/pkg/scale/scale.go index 698ad6983f..855016b8ca 100644 --- a/pkg/scale/scale.go +++ b/pkg/scale/scale.go @@ -24,36 +24,6 @@ import ( "sync" ) -type varyingDataTypeCache map[string]map[uint]VaryingDataTypeValue - -var vdtCache varyingDataTypeCache = make(varyingDataTypeCache) - -type VaryingDataType []VaryingDataTypeValue - -func RegisterVaryingDataType(in interface{}, values ...VaryingDataTypeValue) (err error) { - _, ok := in.(VaryingDataType) - if !ok { - err = fmt.Errorf("%T is not a VaryingDataType", in) - } - - t := reflect.TypeOf(in) - key := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()) - - _, ok = vdtCache[key] - if !ok { - vdtCache[key] = make(map[uint]VaryingDataTypeValue) - } - for _, val := range values { - vdtCache[key][val.Index()] = val - } - return -} - -// VaryingDataType is used to represent scale encodable types -type VaryingDataTypeValue interface { - Index() uint -} - // package level cache for fieldScaleIndicies var cache = &fieldScaleIndicesCache{ cache: make(map[string]fieldScaleIndices), From 7cc203d9d91d63bfe0a79f0b70e94dc47383b4a4 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 10:52:45 -0400 Subject: [PATCH 196/245] wip decode refactor, use reflect.Value as passed param --- pkg/scale/decode.go | 14 +++++++++----- pkg/scale/decode_test.go | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 7d3f231fa3..6bca6e835c 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -150,7 +150,6 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Int16: in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() } - in, err = ds.unmarshal(reflect.ValueOf(in)) default: err = fmt.Errorf("unsupported type: %T", in) } @@ -589,7 +588,7 @@ func (ds *decodeState) decodeBytes(dstv reflect.Value) (err error) { return } - b = make([]byte, length) + b := make([]byte, length) _, err = ds.Read(b) if err != nil { return @@ -767,11 +766,16 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { // decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 // if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise // it returns nil and error -func (ds *decodeState) decodeUint128() (ui *Uint128, err error) { +func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { buf := make([]byte, 16) err = binary.Read(ds, binary.LittleEndian, buf) if err != nil { - return nil, err + return + } + ui128, err := NewUint128(buf) + if err != nil { + return } - return NewUint128(buf) + dstv.Set(reflect.ValueOf(ui128)) + return } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index fab0147b0a..c469a515b1 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -443,9 +443,10 @@ func Test_decodeState_decodeBigInt(t *testing.T) { func Test_decodeState_decodeBytes(t *testing.T) { for _, tt := range stringTests { t.Run(tt.name, func(t *testing.T) { - dst := reflect.ValueOf(tt.in).Interface() + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return } if !reflect.DeepEqual(dst, tt.in) { t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) From bfbbe9a58c47a63e2c01241e4e74d32cae20fc6e Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 17:41:07 -0400 Subject: [PATCH 197/245] repurpose int and uint as compact length encoded integers, remove unnecessary handling of []int --- pkg/scale/comparison_test.go | 20 ++++++++++++++++++++ pkg/scale/decode.go | 14 ++++++++++++-- pkg/scale/decode_test.go | 35 ----------------------------------- pkg/scale/encode.go | 1 + pkg/scale/uint128.go | 1 + 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index c56f721d64..311737d335 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -17,6 +17,8 @@ package scale import ( + "fmt" + "math/big" "reflect" "testing" @@ -197,3 +199,21 @@ func BenchmarkMarshal(b *testing.B) { } } } + +func TestSomething(t *testing.T) { + i := big.NewInt(0) + expectedVal := *common.Uint128FromBigInt(i) + + encByts, err := oldScale.Encode(expectedVal) + if err != nil { + t.Errorf("%v", err) + return + } + encBytes2, err := oldScale.Encode(i) + if err != nil { + t.Errorf("%v", err) + return + } + + fmt.Printf("%+v, %+v", encByts, encBytes2) +} diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 6bca6e835c..b2d7a2e81f 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -142,14 +142,24 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { _, ok := in.(VaryingDataTypeValue) switch ok { case true: + var temp reflect.Value t := reflect.TypeOf(in) switch t.Kind() { // TODO: support more primitive types. Do we need to support arrays and slices as well? case reflect.Int: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + temp = reflect.New(reflect.TypeOf(int(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } case reflect.Int16: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + temp = reflect.New(reflect.TypeOf(int16(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } } + dstv.Set(temp.Elem().Convert(t)) default: err = fmt.Errorf("unsupported type: %T", in) } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index c469a515b1..cd86bba023 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -469,41 +469,6 @@ func Test_decodeState_decodeBool(t *testing.T) { } } -func Test_decodeState_decodeStructManual(t *testing.T) { - // nil case - var dst *MyStruct = nil - var b = []byte{0} - var want *MyStruct = nil - - err := Unmarshal(b, &dst) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } - - // zero case MyStruct - var dst1 *MyStruct = &MyStruct{} - err = Unmarshal(b, &dst1) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst1, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } - - // zero case MyStruct - var dst2 *MyStruct = &MyStruct{Baz: true} - err = Unmarshal(b, &dst2) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if !reflect.DeepEqual(dst2, want) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, want) - } -} - func Test_decodeState_decodeStruct(t *testing.T) { for _, tt := range structTests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 1c50802f61..6efb0ed448 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -381,5 +381,6 @@ func (es *encodeState) encodeUint(i uint) (err error) { // encodeUint128 encodes a Uint128 func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) + fmt.Println("bytes", es.Bytes(), i.Bytes()) return } diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index 12e00c42b9..17fc09952c 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -90,6 +90,7 @@ func NewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128, err erro // Bytes returns the Uint128 in little endian format by default. A variadic parameter // order can be used to specify the binary.ByteOrder used func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { + fmt.Println("called bytes!") var o binary.ByteOrder = binary.LittleEndian if len(order) > 0 { o = order[0] From a468d7dbae9ce2d7e16bc40133158c011474d935 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 1 Jun 2021 21:42:36 -0400 Subject: [PATCH 198/245] cleanup, and all tests benchmark --- pkg/scale/comparison_test.go | 42 ++++++++++++++++++++---------------- pkg/scale/encode.go | 1 - pkg/scale/uint128.go | 1 - 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 311737d335..3c6658e6cb 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -17,8 +17,6 @@ package scale import ( - "fmt" - "math/big" "reflect" "testing" @@ -132,7 +130,7 @@ func TestOldVsNewEncoding(t *testing.T) { ), } - newEncode, err := scale.Marshal(newDigest) + newEncode, err := Marshal(newDigest) if err != nil { t.Errorf("unexpected err: %v", err) return @@ -200,20 +198,28 @@ func BenchmarkMarshal(b *testing.B) { } } -func TestSomething(t *testing.T) { - i := big.NewInt(0) - expectedVal := *common.Uint128FromBigInt(i) - - encByts, err := oldScale.Encode(expectedVal) - if err != nil { - t.Errorf("%v", err) - return - } - encBytes2, err := oldScale.Encode(i) - if err != nil { - t.Errorf("%v", err) - return +func BenchmarkUnmarshal(b *testing.B) { + for _, tt := range allTests { + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } } - - fmt.Printf("%+v, %+v", encByts, encBytes2) } + +// func BenchmarkDecode(b *testing.B) { +// for _, tt := range variableWidthIntegerTests { +// dst := reflect.New(reflect.TypeOf(tt.in)).Interface() +// fmt.Printf("%v %T\n", dst, dst) +// // if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { +// // b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// // return +// // } +// _, err := oldScale.Decode(tt.want, dst) +// if err != nil { +// b.Errorf("%v", err) +// return +// } +// } +// } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 6efb0ed448..1c50802f61 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -381,6 +381,5 @@ func (es *encodeState) encodeUint(i uint) (err error) { // encodeUint128 encodes a Uint128 func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) - fmt.Println("bytes", es.Bytes(), i.Bytes()) return } diff --git a/pkg/scale/uint128.go b/pkg/scale/uint128.go index 17fc09952c..12e00c42b9 100644 --- a/pkg/scale/uint128.go +++ b/pkg/scale/uint128.go @@ -90,7 +90,6 @@ func NewUint128(in interface{}, order ...binary.ByteOrder) (u *Uint128, err erro // Bytes returns the Uint128 in little endian format by default. A variadic parameter // order can be used to specify the binary.ByteOrder used func (u *Uint128) Bytes(order ...binary.ByteOrder) (b []byte) { - fmt.Println("called bytes!") var o binary.ByteOrder = binary.LittleEndian if len(order) > 0 { o = order[0] From 8c764855432cd4efe7a61d827dee1fe6b5b8b19e Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Jun 2021 15:13:11 -0400 Subject: [PATCH 199/245] add README --- pkg/scale/decode.go | 3 +++ pkg/scale/encode.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index b2d7a2e81f..b0de51277a 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -158,6 +158,9 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { if err != nil { break } + default: + err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + return } dstv.Set(temp.Elem().Convert(t)) default: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 1c50802f61..30b41e333a 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -116,6 +116,9 @@ func (es *encodeState) marshal(in interface{}) (err error) { in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() case reflect.Int16: in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + default: + err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + return } err = es.marshal(in) default: From 4bc40b240eebc3983875d8ca387f6e728a9c90d7 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 3 Jun 2021 15:34:36 -0400 Subject: [PATCH 200/245] rResult encode/decode and RegisterResult --- pkg/scale/decode.go | 9 ++++++++- pkg/scale/encode.go | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index b0de51277a..b3bfe4c7cd 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -133,7 +133,14 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Ptr: err = ds.decodePointer(dstv) case reflect.Struct: - err = ds.decodeStruct(dstv) + t := reflect.TypeOf(in) + // check if this is a convertible to Result, if so encode using decodeResult + switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + case true: + err = ds.decodeResult(dstv) + case false: + err = ds.decodeStruct(dstv) + } case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 30b41e333a..4c33db8405 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/binary" "fmt" - "log" "math/big" "reflect" ) @@ -87,7 +86,16 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: - err = es.encodeStruct(in) + t := reflect.TypeOf(in) + // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType + switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + case true: + resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) + err = es.encodeResult(resv.Interface().(Result)) + case false: + err = es.encodeStruct(in) + } + case reflect.Array: err = es.encodeArray(in) case reflect.Slice: @@ -96,12 +104,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { case true: invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) - switch in := invdt.Interface().(type) { - case VaryingDataType: - err = es.encodeVaryingDataType(in) - default: - log.Panicf("this should never happen") - } + err = es.encodeVaryingDataType(invdt.Interface().(VaryingDataType)) case false: err = es.encodeSlice(in) } From 9c357c7982d1b164358e47161e5a46fae62a0c8c Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 4 Jun 2021 15:26:42 -0400 Subject: [PATCH 201/245] wip cr feedback --- pkg/scale/scale_test.go | 73 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 pkg/scale/scale_test.go diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go new file mode 100644 index 0000000000..a5bdac2364 --- /dev/null +++ b/pkg/scale/scale_test.go @@ -0,0 +1,73 @@ +package scale + +import ( + "reflect" + "testing" +) + +func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { + tests := []struct { + name string + in interface{} + wantIndices fieldScaleIndices + wantErr bool + }{ + // TODO: Add test cases. + { + in: struct{ Foo int }{}, + wantIndices: fieldScaleIndices{ + { + fieldIndex: 0, + }, + }, + }, + { + in: struct { + End1 bool + Baz bool `scale:"3"` + End2 []byte + Bar int32 `scale:"2"` + End3 []byte + Foo []byte `scale:"1"` + }{}, + wantIndices: fieldScaleIndices{ + { + fieldIndex: 5, + scaleIndex: newStringPtr("1"), + }, + { + fieldIndex: 3, + scaleIndex: newStringPtr("2"), + }, + { + fieldIndex: 1, + scaleIndex: newStringPtr("3"), + }, + { + fieldIndex: 0, + }, + { + fieldIndex: 2, + }, + { + fieldIndex: 4, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fsic := &fieldScaleIndicesCache{ + cache: make(map[string]fieldScaleIndices), + } + _, gotIndices, err := fsic.fieldScaleIndices(tt.in) + if (err != nil) != tt.wantErr { + t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotIndices, tt.wantIndices) { + t.Errorf("fieldScaleIndicesCache.fieldScaleIndices() gotIndices = %v, want %v", gotIndices, tt.wantIndices) + } + }) + } +} From 2d3c8c81215959c359e88fb6db7a12fe50bcb619 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 7 Jun 2021 14:57:50 -0400 Subject: [PATCH 202/245] add licenses --- pkg/scale/comparison_test.go | 16 ---------------- pkg/scale/scale_test.go | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 3c6658e6cb..9dbd1a5a7e 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -207,19 +207,3 @@ func BenchmarkUnmarshal(b *testing.B) { } } } - -// func BenchmarkDecode(b *testing.B) { -// for _, tt := range variableWidthIntegerTests { -// dst := reflect.New(reflect.TypeOf(tt.in)).Interface() -// fmt.Printf("%v %T\n", dst, dst) -// // if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { -// // b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// // return -// // } -// _, err := oldScale.Decode(tt.want, dst) -// if err != nil { -// b.Errorf("%v", err) -// return -// } -// } -// } diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index a5bdac2364..632ab804d4 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -1,3 +1,19 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + package scale import ( From 56211c1ebdb9a688fe6006ebc207cf70b2c44022 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 7 Jun 2021 17:04:20 -0400 Subject: [PATCH 203/245] add custom primitive encode/decode --- pkg/scale/decode.go | 113 +++++++++++++++++++++++++++++++++----------- pkg/scale/encode.go | 36 +++++++------- 2 files changed, 104 insertions(+), 45 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index b3bfe4c7cd..22f1ef4d3f 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -146,33 +146,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Slice: err = ds.decodeSlice(dstv) default: - _, ok := in.(VaryingDataTypeValue) - switch ok { - case true: - var temp reflect.Value - t := reflect.TypeOf(in) - switch t.Kind() { - // TODO: support more primitive types. Do we need to support arrays and slices as well? - case reflect.Int: - temp = reflect.New(reflect.TypeOf(int(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int16: - temp = reflect.New(reflect.TypeOf(int16(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - default: - err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - return - } - dstv.Set(temp.Elem().Convert(t)) - default: - err = fmt.Errorf("unsupported type: %T", in) - } + err = fmt.Errorf("unsupported type: %T", in) } } return @@ -348,6 +322,91 @@ func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { return } +func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { + in := dstv.Interface() + inType := reflect.TypeOf(in) + var temp reflect.Value + switch inType.Kind() { + case reflect.Bool: + temp = reflect.New(reflect.TypeOf(false)) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int: + temp = reflect.New(reflect.TypeOf(int(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int8: + temp = reflect.New(reflect.TypeOf(int8(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int16: + temp = reflect.New(reflect.TypeOf(int16(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int32: + temp = reflect.New(reflect.TypeOf(int32(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Int64: + temp = reflect.New(reflect.TypeOf(int64(1))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.String: + temp = reflect.New(reflect.TypeOf("")) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint: + temp = reflect.New(reflect.TypeOf(uint(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint8: + temp = reflect.New(reflect.TypeOf(uint8(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint16: + temp = reflect.New(reflect.TypeOf(uint16(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint32: + temp = reflect.New(reflect.TypeOf(uint32(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + case reflect.Uint64: + temp = reflect.New(reflect.TypeOf(uint64(0))) + err = ds.unmarshal(temp.Elem()) + if err != nil { + break + } + default: + err = fmt.Errorf("unsupported type for custom primitive: %T", in) + return + } + dstv.Set(temp.Elem().Convert(inType)) + return +} + func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { res := dstv.Interface().(Result) var rb byte diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 4c33db8405..610df7cd6c 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -109,24 +109,24 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - _, ok := in.(VaryingDataTypeValue) - switch ok { - case true: - t := reflect.TypeOf(in) - switch t.Kind() { - // TODO: support more primitive types. Do we need to support arrays and slices as well? - case reflect.Int: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() - case reflect.Int16: - in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() - default: - err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - return - } - err = es.marshal(in) - default: - err = fmt.Errorf("unsupported type: %T", in) - } + // _, ok := in.(VaryingDataTypeValue) + // switch ok { + // case true: + // t := reflect.TypeOf(in) + // switch t.Kind() { + // // TODO: support more primitive types. Do we need to support arrays and slices as well? + // case reflect.Int: + // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() + // case reflect.Int16: + // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() + // default: + // err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) + // return + // } + // err = es.marshal(in) + // default: + err = fmt.Errorf("unsupported type: %T", in) + // } } } From 536e60e9b55d9562a3f3a67c89364a343e658c83 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 8 Jun 2021 13:57:33 -0400 Subject: [PATCH 204/245] more cr feedback --- pkg/scale/comparison_test.go | 24 ++++++++++++---- pkg/scale/decode_test.go | 53 +++++++++++++++++++++++++++++++----- pkg/scale/encode.go | 18 ------------ pkg/scale/encode_test.go | 30 ++++++++++++++++++++ pkg/scale/scale_test.go | 1 - 5 files changed, 95 insertions(+), 31 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 9dbd1a5a7e..52b418556b 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -199,11 +199,25 @@ func BenchmarkMarshal(b *testing.B) { } func BenchmarkUnmarshal(b *testing.B) { - for _, tt := range allTests { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return + for i := 0; i < b.N; i++ { + for _, tt := range allTests { + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + } + } +} + +func BenchmarkMarshal(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, tt := range allTests { + // dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() + if _, err := Marshal(tt.in); (err != nil) != tt.wantErr { + b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } } } } diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index cd86bba023..5e2df00d14 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -526,13 +526,8 @@ func Test_unmarshal_optionality(t *testing.T) { want: t.want, out: t.out, } - switch t.in { - case nil: - // this doesn't actually happen since none of the tests have nil value for tt.in - ptrTest.want = []byte{0x00} - default: - ptrTest.want = append([]byte{0x01}, t.want...) - } + + ptrTest.want = append([]byte{0x01}, t.want...) ptrTests = append(ptrTests, ptrTest) } for _, tt := range ptrTests { @@ -556,3 +551,47 @@ func Test_unmarshal_optionality(t *testing.T) { }) } } + +func Test_unmarshal_optionality_nil_case(t *testing.T) { + var ptrTests tests + for _, t := range allTests { + ptrTest := test{ + name: t.name, + in: t.in, + wantErr: t.wantErr, + want: t.want, + // ignore out, since we are testing nil case + // out: t.out, + } + ptrTest.want = []byte{0x00} + + temp := reflect.New(reflect.TypeOf(t.in)) + // create a new pointer to type of temp + tempv := reflect.New(reflect.PtrTo(temp.Type()).Elem()) + // set zero value to elem of **temp so that is nil + tempv.Elem().Set(reflect.Zero(tempv.Elem().Type())) + // set test.in to *temp + ptrTest.in = tempv.Elem().Interface() + + ptrTests = append(ptrTests, ptrTest) + } + for _, tt := range ptrTests { + t.Run(tt.name, func(t *testing.T) { + // this becomes a pointer to a zero value of the underlying value + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface()) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) + } +} diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 610df7cd6c..ecc1b4234e 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -109,25 +109,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.encodeSlice(in) } default: - // _, ok := in.(VaryingDataTypeValue) - // switch ok { - // case true: - // t := reflect.TypeOf(in) - // switch t.Kind() { - // // TODO: support more primitive types. Do we need to support arrays and slices as well? - // case reflect.Int: - // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int(1))).Interface() - // case reflect.Int16: - // in = reflect.ValueOf(in).Convert(reflect.TypeOf(int16(1))).Interface() - // default: - // err = fmt.Errorf("unsupported kind for VaryingDataTypeValue: %s", t.Kind()) - // return - // } - // err = es.marshal(in) - // default: err = fmt.Errorf("unsupported type: %T", in) - // } - } } return diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index fac6fb4669..10724dd6b4 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -169,6 +169,11 @@ var ( in: uint(9223372036854775807), want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomUint(9223372036854775807)", + in: myCustomUint(9223372036854775807), + want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } int64Tests = tests{ { @@ -207,6 +212,11 @@ var ( in: int64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomInt64(9223372036854775807)", + in: myCustomInt64(9223372036854775807), + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } uint64Tests = tests{ { @@ -243,6 +253,11 @@ var ( in: uint64(9223372036854775807), want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, + { + name: "myCustomUint64(9223372036854775807)", + in: myCustomUint64(9223372036854775807), + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, + }, } int32Tests = tests{ { @@ -296,6 +311,11 @@ var ( in: uint32(1073741823), want: []byte{0xff, 0xff, 0xff, 0x3f}, }, + { + name: "uint32(1073741823)", + in: myCustomUint32(1073741823), + want: []byte{0xff, 0xff, 0xff, 0x3f}, + }, } int8Tests = tests{ { @@ -329,6 +349,11 @@ var ( in: uint8(1), want: []byte{0x01}, }, + { + name: "myCustomInt8(1)", + in: myCustomUint8(1), + want: []byte{0x01}, + }, } int16Tests = tests{ { @@ -355,6 +380,11 @@ var ( in: int16(16383), want: []byte{0xff, 0x3f}, }, + { + name: "myCustomInt16(16383)", + in: myCustomInt16(16383), + want: []byte{0xff, 0x3f}, + }, } uint16Tests = tests{ { diff --git a/pkg/scale/scale_test.go b/pkg/scale/scale_test.go index 632ab804d4..a96ab2b539 100644 --- a/pkg/scale/scale_test.go +++ b/pkg/scale/scale_test.go @@ -28,7 +28,6 @@ func Test_fieldScaleIndicesCache_fieldScaleIndices(t *testing.T) { wantIndices fieldScaleIndices wantErr bool }{ - // TODO: Add test cases. { in: struct{ Foo int }{}, wantIndices: fieldScaleIndices{ From 7c44925f8f212fb6c71872bda2c1b6f95eaf69d0 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 14 Jun 2021 12:04:15 -0400 Subject: [PATCH 205/245] wip --- pkg/scale/decode.go | 20 ++++++- pkg/scale/encode.go | 25 ++++++-- pkg/scale/result_test.go | 51 ++++++++++++++++ pkg/scale/something_test.go | 114 ++++++++++++++++++++++++++++++++++++ 4 files changed, 202 insertions(+), 8 deletions(-) create mode 100644 pkg/scale/something_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 22f1ef4d3f..727d2b001c 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -134,13 +134,27 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { err = ds.decodePointer(dstv) case reflect.Struct: t := reflect.TypeOf(in) - // check if this is a convertible to Result, if so encode using decodeResult - switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + field, ok := t.FieldByName("Result") + switch ok { case true: + if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { + err = fmt.Errorf("%T is not a Result", in) + return + } + // res := reflect.ValueOf(in).FieldByName("Result") + // fmt.Println("yao!", res) err = ds.decodeResult(dstv) - case false: + default: err = ds.decodeStruct(dstv) } + + // // check if this is a convertible to Result, if so encode using decodeResult + // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + // case true: + // err = ds.decodeResult(dstv) + // case false: + // err = ds.decodeStruct(dstv) + // } case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index ecc1b4234e..a9e9ab789d 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -86,16 +86,31 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: + // fmt.Println("in here!") t := reflect.TypeOf(in) - // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType - switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + field, ok := t.FieldByName("Result") + switch ok { case true: - resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) - err = es.encodeResult(resv.Interface().(Result)) - case false: + if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { + err = fmt.Errorf("%T is not a Result", in) + return + } + res := reflect.ValueOf(in).FieldByName("Result").Interface().(Result) + // fmt.Println("yao!", res) + err = es.encodeResult(res) + default: err = es.encodeStruct(in) } + // check if this is a type with an embedded Result, aka a registered result + // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { + // case true: + // resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) + // err = es.encodeResult(resv.Interface().(Result)) + // case false: + // err = es.encodeStruct(in) + // } + case reflect.Array: err = es.encodeArray(in) case reflect.Slice: diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index c63c196ae0..ec27d50730 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -424,3 +424,54 @@ func TestResult_Set(t *testing.T) { }) } } + +// func TestNilOk(t *testing.T) { +// mr := MyResult{} +// mr.SetOk(nil) +// bytes, err := Marshal(mr) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x00}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } + +// mr1 := MyResult{} +// mr1.SetErr(true) +// bytes, err = Marshal(mr1) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } +// } + +// func TestBothNil(t *testing.T) { +// mr := MyResult{} +// mr.SetOk(nil) +// bytes, err := Marshal(mr) +// if err != nil { +// t.Errorf("%v", err) +// return +// } + +// if !reflect.DeepEqual([]byte{0x00}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } + +// mr1 := MyResult{} +// mr1.SetErr(nil) +// bytes, err = Marshal(mr1) +// if err != nil { +// t.Errorf("%v", err) +// return +// } +// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { +// t.Errorf("unexpected bytes: %v", bytes) +// } +// } diff --git a/pkg/scale/something_test.go b/pkg/scale/something_test.go new file mode 100644 index 0000000000..8194c32890 --- /dev/null +++ b/pkg/scale/something_test.go @@ -0,0 +1,114 @@ +package scale_test + +import ( + "fmt" + "testing" + + "github.com/ChainSafe/gossamer/pkg/scale" +) + +type MyStruct struct { + Baz bool + Bar uint32 + Foo []byte +} + +func (ms MyStruct) Index() uint { + return 1 +} + +type MyOtherStruct struct { + Foo string + Bar uint64 + Baz uint +} + +func (mos MyOtherStruct) Index() uint { + return 2 +} + +type MyInt16 int16 + +func (mi16 MyInt16) Index() uint { + return 3 +} + +type MyVaryingDataType scale.VaryingDataType + +func varyingDataTypeExample() { + err := scale.RegisterVaryingDataType(MyVaryingDataType{}, MyStruct{}, MyOtherStruct{}, MyInt16(0)) + if err != nil { + panic(err) + } + + mvdt := MyVaryingDataType{ + MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + }, + MyOtherStruct{ + Foo: "hello", + Bar: 999, + Baz: 888, + }, + MyInt16(111), + } + bytes, err := scale.Marshal(mvdt) + if err != nil { + panic(err) + } + + var unmarshaled MyVaryingDataType + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // [{Baz:true Bar:999 Foo:[1 2]} {Foo:hello Bar:999 Baz:888} 111] + fmt.Printf("%+v", unmarshaled) +} +func structExample() { + type MyStruct struct { + Baz bool `scale:"3"` + Bar int32 `scale:"2"` + Foo []byte `scale:"1"` + } + var ms = MyStruct{ + Baz: true, + Bar: 999, + Foo: []byte{1, 2}, + } + bytes, err := scale.Marshal(ms) + if err != nil { + panic(err) + } + + var unmarshaled MyStruct + err = scale.Unmarshal(bytes, &unmarshaled) + if err != nil { + panic(err) + } + + // Baz:true Bar:999 Foo:[1 2]} + fmt.Printf("%+v", unmarshaled) +} +func TestSomething(t *testing.T) { + // // compact length encoded uint + // var ui uint = 999 + // bytes, err := scale.Marshal(ui) + // if err != nil { + // panic(err) + // } + + // var unmarshaled uint + // err = scale.Unmarshal(bytes, &unmarshaled) + // if err != nil { + // panic(err) + // } + + // fmt.Printf("%d", unmarshaled) + + // structExample() + varyingDataTypeExample() +} From 8e3b2438d43aa2ef253382a71eba3d8743b40a6a Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 16 Jun 2021 15:37:38 -0400 Subject: [PATCH 206/245] revise Result --- pkg/scale/decode.go | 23 +------ pkg/scale/encode.go | 26 +------- pkg/scale/result_test.go | 120 +++++++++++++++++++++--------------- pkg/scale/something_test.go | 114 ---------------------------------- 4 files changed, 72 insertions(+), 211 deletions(-) delete mode 100644 pkg/scale/something_test.go diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 727d2b001c..1b9a413afd 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -133,28 +133,7 @@ func (ds *decodeState) unmarshal(dstv reflect.Value) (err error) { case reflect.Ptr: err = ds.decodePointer(dstv) case reflect.Struct: - t := reflect.TypeOf(in) - field, ok := t.FieldByName("Result") - switch ok { - case true: - if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { - err = fmt.Errorf("%T is not a Result", in) - return - } - // res := reflect.ValueOf(in).FieldByName("Result") - // fmt.Println("yao!", res) - err = ds.decodeResult(dstv) - default: - err = ds.decodeStruct(dstv) - } - - // // check if this is a convertible to Result, if so encode using decodeResult - // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { - // case true: - // err = ds.decodeResult(dstv) - // case false: - // err = ds.decodeStruct(dstv) - // } + err = ds.decodeStruct(dstv) case reflect.Array: err = ds.decodeArray(dstv) case reflect.Slice: diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index a9e9ab789d..7c5e80afca 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -86,31 +86,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { err = es.marshal(elem.Interface()) } case reflect.Struct: - // fmt.Println("in here!") - t := reflect.TypeOf(in) - field, ok := t.FieldByName("Result") - switch ok { - case true: - if !field.Type.ConvertibleTo(reflect.TypeOf(Result{})) { - err = fmt.Errorf("%T is not a Result", in) - return - } - res := reflect.ValueOf(in).FieldByName("Result").Interface().(Result) - // fmt.Println("yao!", res) - err = es.encodeResult(res) - default: - err = es.encodeStruct(in) - } - - // check if this is a type with an embedded Result, aka a registered result - // switch t.ConvertibleTo(reflect.TypeOf(Result{})) { - // case true: - // resv := reflect.ValueOf(in).Convert(reflect.TypeOf(Result{})) - // err = es.encodeResult(resv.Interface().(Result)) - // case false: - // err = es.encodeStruct(in) - // } - + err = es.encodeStruct(in) case reflect.Array: err = es.encodeArray(in) case reflect.Slice: diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index ec27d50730..eb83873a92 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -425,53 +425,73 @@ func TestResult_Set(t *testing.T) { } } -// func TestNilOk(t *testing.T) { -// mr := MyResult{} -// mr.SetOk(nil) -// bytes, err := Marshal(mr) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x00}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } - -// mr1 := MyResult{} -// mr1.SetErr(true) -// bytes, err = Marshal(mr1) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } -// } - -// func TestBothNil(t *testing.T) { -// mr := MyResult{} -// mr.SetOk(nil) -// bytes, err := Marshal(mr) -// if err != nil { -// t.Errorf("%v", err) -// return -// } - -// if !reflect.DeepEqual([]byte{0x00}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } - -// mr1 := MyResult{} -// mr1.SetErr(nil) -// bytes, err = Marshal(mr1) -// if err != nil { -// t.Errorf("%v", err) -// return -// } -// if !reflect.DeepEqual([]byte{0x01, 0x01}, bytes) { -// t.Errorf("unexpected bytes: %v", bytes) -// } -// } +func TestResult_Set(t *testing.T) { + type args struct { + mode ResultMode + in interface{} + } + tests := []struct { + name string + res Result + args args + wantErr bool + }{ + // TODO: Add test cases. + { + args: args{ + mode: Unset, + }, + wantErr: true, + }, + { + args: args{ + mode: OK, + in: nil, + }, + }, + { + args: args{ + mode: Err, + in: nil, + }, + }, + { + args: args{ + mode: OK, + in: true, + }, + res: NewResult(true, nil), + }, + { + args: args{ + mode: Err, + in: true, + }, + res: NewResult(nil, true), + }, + { + args: args{ + mode: OK, + in: true, + }, + res: NewResult("ok", "err"), + wantErr: true, + }, + { + args: args{ + mode: Err, + in: nil, + }, + res: NewResult(nil, true), + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := tt.res + if err := r.Set(tt.args.mode, tt.args.in); (err != nil) != tt.wantErr { + t.Errorf("Result.Set() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/scale/something_test.go b/pkg/scale/something_test.go deleted file mode 100644 index 8194c32890..0000000000 --- a/pkg/scale/something_test.go +++ /dev/null @@ -1,114 +0,0 @@ -package scale_test - -import ( - "fmt" - "testing" - - "github.com/ChainSafe/gossamer/pkg/scale" -) - -type MyStruct struct { - Baz bool - Bar uint32 - Foo []byte -} - -func (ms MyStruct) Index() uint { - return 1 -} - -type MyOtherStruct struct { - Foo string - Bar uint64 - Baz uint -} - -func (mos MyOtherStruct) Index() uint { - return 2 -} - -type MyInt16 int16 - -func (mi16 MyInt16) Index() uint { - return 3 -} - -type MyVaryingDataType scale.VaryingDataType - -func varyingDataTypeExample() { - err := scale.RegisterVaryingDataType(MyVaryingDataType{}, MyStruct{}, MyOtherStruct{}, MyInt16(0)) - if err != nil { - panic(err) - } - - mvdt := MyVaryingDataType{ - MyStruct{ - Baz: true, - Bar: 999, - Foo: []byte{1, 2}, - }, - MyOtherStruct{ - Foo: "hello", - Bar: 999, - Baz: 888, - }, - MyInt16(111), - } - bytes, err := scale.Marshal(mvdt) - if err != nil { - panic(err) - } - - var unmarshaled MyVaryingDataType - err = scale.Unmarshal(bytes, &unmarshaled) - if err != nil { - panic(err) - } - - // [{Baz:true Bar:999 Foo:[1 2]} {Foo:hello Bar:999 Baz:888} 111] - fmt.Printf("%+v", unmarshaled) -} -func structExample() { - type MyStruct struct { - Baz bool `scale:"3"` - Bar int32 `scale:"2"` - Foo []byte `scale:"1"` - } - var ms = MyStruct{ - Baz: true, - Bar: 999, - Foo: []byte{1, 2}, - } - bytes, err := scale.Marshal(ms) - if err != nil { - panic(err) - } - - var unmarshaled MyStruct - err = scale.Unmarshal(bytes, &unmarshaled) - if err != nil { - panic(err) - } - - // Baz:true Bar:999 Foo:[1 2]} - fmt.Printf("%+v", unmarshaled) -} -func TestSomething(t *testing.T) { - // // compact length encoded uint - // var ui uint = 999 - // bytes, err := scale.Marshal(ui) - // if err != nil { - // panic(err) - // } - - // var unmarshaled uint - // err = scale.Unmarshal(bytes, &unmarshaled) - // if err != nil { - // panic(err) - // } - - // fmt.Printf("%d", unmarshaled) - - // structExample() - varyingDataTypeExample() -} From aca7ffadb9df6fa5cdbe3d0bbd71cf2bbde71a13 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 18 Jun 2021 22:13:39 -0400 Subject: [PATCH 207/245] wip VaryingDataType and VaryingDataTypeSlice --- pkg/scale/decode_test.go | 46 ++++-- pkg/scale/encode.go | 10 +- pkg/scale/varying_data_type_test.go | 231 ++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+), 24 deletions(-) diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index 5e2df00d14..ed0b030d56 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -532,22 +532,38 @@ func Test_unmarshal_optionality(t *testing.T) { } for _, tt := range ptrTests { t.Run(tt.name, func(t *testing.T) { - // this becomes a pointer to a zero value of the underlying value - dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - var diff string - if tt.out != nil { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } + switch in := tt.in.(type) { + case VaryingDataType: + // copy the inputted vdt cause we need the cached values + copy := in + vdt := copy + vdt.value = nil + var dst interface{} = &vdt + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + diff := cmp.Diff(vdt.value, tt.in.(VaryingDataType).value, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + default: + dst := reflect.New(reflect.TypeOf(tt.in)).Interface() + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + var diff string + if tt.out != nil { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) + } else { + diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) + } + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + } }) } } diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 7c5e80afca..7c62a77bd5 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -90,15 +90,7 @@ func (es *encodeState) marshal(in interface{}) (err error) { case reflect.Array: err = es.encodeArray(in) case reflect.Slice: - t := reflect.TypeOf(in) - // check if this is a convertible to VaryingDataType, if so encode using encodeVaryingDataType - switch t.ConvertibleTo(reflect.TypeOf(VaryingDataType{})) { - case true: - invdt := reflect.ValueOf(in).Convert(reflect.TypeOf(VaryingDataType{})) - err = es.encodeVaryingDataType(invdt.Interface().(VaryingDataType)) - case false: - err = es.encodeSlice(in) - } + err = es.encodeSlice(in) default: err = fmt.Errorf("unsupported type: %T", in) } diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index d63063acfd..c4fb75027c 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -940,3 +940,234 @@ func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { }) } } + +func TestNewVaryingDataType(t *testing.T) { + type args struct { + values []VaryingDataTypeValue + } + tests := []struct { + name string + args args + wantVdt VaryingDataType + wantErr bool + }{ + { + args: args{ + values: []VaryingDataTypeValue{}, + }, + wantErr: true, + }, + { + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + }, + }, + wantVdt: VaryingDataType{ + cache: map[uint]VaryingDataTypeValue{ + VDTValue{}.Index(): VDTValue{}, + VDTValue1{}.Index(): VDTValue1{}, + VDTValue2{}.Index(): VDTValue2{}, + VDTValue3(0).Index(): VDTValue3(0), + }, + }, + }, + { + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), VDTValue{}, + }, + }, + wantVdt: VaryingDataType{ + cache: map[uint]VaryingDataTypeValue{ + VDTValue{}.Index(): VDTValue{}, + VDTValue1{}.Index(): VDTValue1{}, + VDTValue2{}.Index(): VDTValue2{}, + VDTValue3(0).Index(): VDTValue3(0), + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotVdt, err := NewVaryingDataType(tt.args.values...) + if (err != nil) != tt.wantErr { + t.Errorf("NewVaryingDataType() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotVdt, tt.wantVdt) { + t.Errorf("NewVaryingDataType() = %v, want %v", gotVdt, tt.wantVdt) + } + }) + } +} + +func TestVaryingDataType_Set(t *testing.T) { + type args struct { + value VaryingDataTypeValue + } + tests := []struct { + name string + vdt VaryingDataType + args args + wantErr bool + }{ + { + vdt: mustNewVaryingDataType(VDTValue1{}), + args: args{ + value: VDTValue1{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue1{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue2{}, + }, + }, + { + vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), + args: args{ + value: VDTValue3(0), + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + vdt := tt.vdt + if err := vdt.Set(tt.args.value); (err != nil) != tt.wantErr { + t.Errorf("VaryingDataType.SetValue() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestVaryingDataTypeSlice_Add(t *testing.T) { + type args struct { + values []VaryingDataTypeValue + } + tests := []struct { + name string + vdts VaryingDataTypeSlice + args args + wantErr bool + wantValues []VaryingDataType + }{ + { + name: "happy path", + vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0))), + args: args{ + values: []VaryingDataTypeValue{ + VDTValue{ + B: 1, + }, + }, + }, + wantValues: []VaryingDataType{ + mustNewVaryingDataTypeAndSet( + VDTValue{ + B: 1, + }, + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + }, + }, + { + name: "invalid value error case", + vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{})), + args: args{ + values: []VaryingDataTypeValue{ + VDTValue3(0), + }, + }, + wantValues: []VaryingDataType{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + vdts := &tt.vdts + if err := vdts.Add(tt.args.values...); (err != nil) != tt.wantErr { + t.Errorf("VaryingDataTypeSlice.Add() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(vdts.Values, tt.wantValues) { + t.Errorf("NewVaryingDataType() = %v, want %v", vdts.Values, tt.wantValues) + } + }) + } +} + +var varyingDataTypeSliceTests = tests{ + { + in: mustNewVaryingDataTypeSliceAndSet( + mustNewVaryingDataType( + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, + ), + want: []byte{ + // length + 4, + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + }, +} + +func Test_encodeState_encodeVaryingDataTypeSlice(t *testing.T) { + for _, tt := range varyingDataTypeSliceTests { + t.Run(tt.name, func(t *testing.T) { + vdt := tt.in.(VaryingDataTypeSlice) + b, err := Marshal(vdt) + if (err != nil) != tt.wantErr { + t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(b, tt.want) { + t.Errorf("encodeState.encodeStruct() = %v, want %v", b, tt.want) + } + }) + } +} + +// func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { +// for _, tt := range varyingDataTypeTests { +// t.Run(tt.name, func(t *testing.T) { +// dst, err := NewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0)) +// if err != nil { +// t.Errorf("%v", err) +// return +// } +// if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { +// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) +// return +// } +// vdt := tt.in.(VaryingDataType) +// diff := cmp.Diff(dst.Value(), vdt.Value(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) +// if diff != "" { +// t.Errorf("decodeState.unmarshal() = %s", diff) +// } +// }) +// } +// } From 69f10dec60f891d8003173e1c330dcef7d9a4577 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Mon, 21 Jun 2021 11:00:07 -0400 Subject: [PATCH 208/245] refactor VaryingDataType and add VaryingDataTypeSlice --- pkg/scale/varying_data_type_test.go | 157 ++++++++++++++++++++-------- 1 file changed, 114 insertions(+), 43 deletions(-) diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index c4fb75027c..62423ba273 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -1112,27 +1112,97 @@ var varyingDataTypeSliceTests = tests{ ), VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, ), - want: []byte{ - // length - 4, - // index - 2, - // value - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - }, + want: newWant( + []byte{ + // length + 4, + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + ), + }, + { + in: mustNewVaryingDataTypeSliceAndSet( + mustNewVaryingDataType( + VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), + ), + VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, + VDTValue{ + A: big.NewInt(1073741823), + B: int(1073741823), + C: uint(1073741823), + D: int8(1), + E: uint8(1), + F: int16(16383), + G: uint16(16383), + H: int32(1073741823), + I: uint32(1073741823), + J: int64(9223372036854775807), + K: uint64(9223372036854775807), + L: byteArray(64), + M: testStrings[1], + N: true, + }, + ), + want: newWant( + []byte{ + // length + 8, + }, + []byte{ + // index + 2, + // value + 0x01, 0xfe, 0xff, 0xff, 0xff, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + }, + []byte{ + // index + 1, + // value + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, + 0xfe, 0xff, 0xff, 0xff, + 0x01, + 0x01, + 0xff, 0x3f, + 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0x3f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, + }, + append([]byte{0x01, 0x01}, byteArray(64)...), + append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), + []byte{0x01}, + ), }, } @@ -1142,32 +1212,33 @@ func Test_encodeState_encodeVaryingDataTypeSlice(t *testing.T) { vdt := tt.in.(VaryingDataTypeSlice) b, err := Marshal(vdt) if (err != nil) != tt.wantErr { - t.Errorf("encodeState.encodeStruct() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr) } if !reflect.DeepEqual(b, tt.want) { - t.Errorf("encodeState.encodeStruct() = %v, want %v", b, tt.want) + t.Errorf("Marshal() = %v, want %v", b, tt.want) } }) } } -// func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { -// for _, tt := range varyingDataTypeTests { -// t.Run(tt.name, func(t *testing.T) { -// dst, err := NewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0)) -// if err != nil { -// t.Errorf("%v", err) -// return -// } -// if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { -// t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// vdt := tt.in.(VaryingDataType) -// diff := cmp.Diff(dst.Value(), vdt.Value(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{})) -// if diff != "" { -// t.Errorf("decodeState.unmarshal() = %s", diff) -// } -// }) -// } -// } +func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { + opt := cmp.Comparer(func(x, y VaryingDataType) bool { + return reflect.DeepEqual(x.value, y.value) && reflect.DeepEqual(x.cache, y.cache) + }) + + for _, tt := range varyingDataTypeSliceTests { + t.Run(tt.name, func(t *testing.T) { + dst := tt.in.(VaryingDataTypeSlice) + dst.Types = make([]VaryingDataType, 0) + if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { + t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + vdts := tt.in.(VaryingDataTypeSlice) + diff := cmp.Diff(dst, vdts, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}), opt) + if diff != "" { + t.Errorf("decodeState.unmarshal() = %s", diff) + } + }) + } +} From 8419c26cccb435bb55ebd815ba66580f1fa69227 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 21 Jun 2021 11:03:11 -0600 Subject: [PATCH 209/245] integrate scale pkg into lib/transaction --- go.mod | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/go.mod b/go.mod index 8472dcecb6..01a0602622 100644 --- a/go.mod +++ b/go.mod @@ -19,29 +19,7 @@ require ( github.com/go-playground/validator/v10 v10.4.1 github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect -<<<<<<< HEAD -<<<<<<< HEAD -<<<<<<< HEAD -<<<<<<< HEAD -<<<<<<< HEAD -======= ->>>>>>> fix struct tests and optionality tests github.com/google/go-cmp v0.5.6 -======= - github.com/google/go-cmp v0.5.6 // indirect ->>>>>>> fix struct tests and optionality tests -<<<<<<< HEAD -======= - github.com/google/go-cmp v0.5.6 ->>>>>>> integrate scale pkg into lib/transaction -======= - github.com/google/go-cmp v0.5.6 ->>>>>>> integrate scale pkg into lib/transaction -======= - github.com/google/go-cmp v0.5.6 ->>>>>>> integrate scale pkg into lib/transaction -======= ->>>>>>> fix struct tests and optionality tests github.com/google/uuid v1.1.5 // indirect github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 From 473fc1f2af0192059609a2da63484617e3e678a5 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 12:03:02 -0600 Subject: [PATCH 210/245] WIP/Integrate scale into lib/babe --- lib/babe/build.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/babe/build.go b/lib/babe/build.go index 1972e265b0..f67d09d51c 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -354,7 +354,11 @@ func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) ( for _, tx := range txs { var decExt []byte +<<<<<<< HEAD err := scale.Unmarshal(tx.Extrinsic, &decExt) +======= + err := scale2.Unmarshal(tx.Extrinsic, &decExt) +>>>>>>> WIP/Integrate scale into lib/babe if err != nil { return nil, err } From 63146c9919401007b8f1abfbb36caaebc6e4f7d7 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 13:31:50 -0600 Subject: [PATCH 211/245] integrate scale package into babe library --- lib/babe/build.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index f67d09d51c..1972e265b0 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -354,11 +354,7 @@ func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) ( for _, tx := range txs { var decExt []byte -<<<<<<< HEAD err := scale.Unmarshal(tx.Extrinsic, &decExt) -======= - err := scale2.Unmarshal(tx.Extrinsic, &decExt) ->>>>>>> WIP/Integrate scale into lib/babe if err != nil { return nil, err } From 62e2d3405013272ec15b611d96449fb1011f6ecb Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Tue, 22 Jun 2021 17:01:03 -0400 Subject: [PATCH 212/245] fix result.set with nil value --- pkg/scale/result_test.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index eb83873a92..38e9f48127 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -436,24 +436,39 @@ func TestResult_Set(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. { args: args{ mode: Unset, }, + res: NewResult(nil, nil), wantErr: true, + wantResult: Result{ + ok: empty{}, err: empty{}, + }, }, { args: args{ mode: OK, in: nil, }, + res: NewResult(nil, nil), + wantResult: Result{ + ok: empty{}, + err: empty{}, + mode: OK, + }, }, { args: args{ mode: Err, in: nil, }, + res: NewResult(nil, nil), + wantResult: Result{ + ok: empty{}, + err: empty{}, + mode: Err, + }, }, { args: args{ @@ -461,6 +476,11 @@ func TestResult_Set(t *testing.T) { in: true, }, res: NewResult(true, nil), + wantResult: Result{ + ok: true, + err: empty{}, + mode: OK, + }, }, { args: args{ @@ -468,6 +488,11 @@ func TestResult_Set(t *testing.T) { in: true, }, res: NewResult(nil, true), + wantResult: Result{ + ok: empty{}, + err: true, + mode: Err, + }, }, { args: args{ @@ -476,6 +501,10 @@ func TestResult_Set(t *testing.T) { }, res: NewResult("ok", "err"), wantErr: true, + wantResult: Result{ + ok: "ok", + err: "err", + }, }, { args: args{ @@ -484,6 +513,10 @@ func TestResult_Set(t *testing.T) { }, res: NewResult(nil, true), wantErr: true, + wantResult: Result{ + ok: empty{}, + err: true, + }, }, } for _, tt := range tests { @@ -492,6 +525,9 @@ func TestResult_Set(t *testing.T) { if err := r.Set(tt.args.mode, tt.args.in); (err != nil) != tt.wantErr { t.Errorf("Result.Set() error = %v, wantErr %v", err, tt.wantErr) } + if !reflect.DeepEqual(tt.wantResult, r) { + t.Errorf("Result.Unwrap() = %v, want %v", tt.wantResult, r) + } }) } } From 893559045a02ba49753a46682d1a59a0eee337f8 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 15:06:02 -0600 Subject: [PATCH 213/245] WIP/refactor babe errors to utilize new scale pkg --- lib/babe/errors.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 3076232b2c..7b14da3d8d 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -235,6 +235,9 @@ func (err Module) String() string { func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome + result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) + fmt.Println("Result err") + fmt.Println(result) switch res[1] { case 0: return nil From 8cfcb122b3b5fc15239d03fc30503c11a81de277 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 15:56:28 -0600 Subject: [PATCH 214/245] Integrate scale pkg into babe tests --- lib/babe/errors.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 7b14da3d8d..aa40761f4e 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -236,7 +236,11 @@ func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) - fmt.Println("Result err") + //err := result.Set(scale.OK, nil) + //if err != nil { + // panic(err) + //} + fmt.Println("Result") fmt.Println(result) switch res[1] { case 0: From 30b14c5f92d9e6ef0707c9f46bcf4f42fc4a878f Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 16:22:12 -0600 Subject: [PATCH 215/245] create init structs for dispatch outcome errors --- lib/babe/errors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index aa40761f4e..50e738a40b 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -235,7 +235,7 @@ func (err Module) String() string { func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{})) + result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) //err := result.Set(scale.OK, nil) //if err != nil { // panic(err) From e6b29183ce72ca2e7ba2d42809f4259dc1507665 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 22 Jun 2021 16:49:47 -0600 Subject: [PATCH 216/245] WIP/refactor babe error handling to utiliize new scale pkg features --- lib/babe/errors.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 50e738a40b..3a896f1574 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -240,6 +240,11 @@ func determineErr(res []byte) error { //if err != nil { // panic(err) //} + + // This code chunk works + v := FailedLookup{err: DispatchOutcomeError{"failed lookup"}} + fmt.Println(v.err) + fmt.Println("Result") fmt.Println(result) switch res[1] { From 961fb1164243a30a9997056556ec2408075cf91b Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 15:44:21 -0600 Subject: [PATCH 217/245] WIP scale/babe integration. Closer but still errors --- lib/babe/errors.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 3a896f1574..1ae3920a86 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -63,6 +63,7 @@ var ( ) // A DispatchOutcomeError is outcome of dispatching the extrinsic +// Can I make this not a struct? type DispatchOutcomeError struct { msg string // description of error } @@ -232,21 +233,25 @@ func (err Module) String() string { return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } +func (err CustomModuleError) String() string { + return fmt.Sprintf("index: %d code: %d message: %s", err.index, err.err, err.message) +} + func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) + //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) //err := result.Set(scale.OK, nil) //if err != nil { // panic(err) //} - // This code chunk works - v := FailedLookup{err: DispatchOutcomeError{"failed lookup"}} - fmt.Println(v.err) - - fmt.Println("Result") - fmt.Println(result) + //// This code chunk works + //v := FailedLookup{Err: DispatchOutcomeError{"failed lookup"}} + //fmt.Println(v.Err) + // + //fmt.Println("Result") + //fmt.Println(result) switch res[1] { case 0: return nil From 93929d15a964bb06df01ded5a615707a483b7d97 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 23 Jun 2021 16:29:40 -0600 Subject: [PATCH 218/245] WIP/Fix type issues in babe refactor --- lib/babe/errors.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 1ae3920a86..27af04df50 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -63,7 +63,6 @@ var ( ) // A DispatchOutcomeError is outcome of dispatching the extrinsic -// Can I make this not a struct? type DispatchOutcomeError struct { msg string // description of error } @@ -240,18 +239,6 @@ func (err CustomModuleError) String() string { func determineErr(res []byte) error { switch res[0] { case 0: // DispatchOutcome - //result := scale.NewResult(nil, scale.MustNewVaryingDataType(UnknownError{}, FailedLookup{}, BadOrigin{}, CustomModuleError{})) - //err := result.Set(scale.OK, nil) - //if err != nil { - // panic(err) - //} - - //// This code chunk works - //v := FailedLookup{Err: DispatchOutcomeError{"failed lookup"}} - //fmt.Println(v.Err) - // - //fmt.Println("Result") - //fmt.Println(result) switch res[1] { case 0: return nil From 849eaf505bd48099d66bd73134f6a83c8ad5362c Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:25:18 -0600 Subject: [PATCH 219/245] resolve type issue --- lib/babe/errors.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 27af04df50..28bd5e0f05 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -152,6 +152,8 @@ func determineDispatchErr(res []byte) error { // This works yay! return &DispatchOutcomeError{"bad origin"} case Module: return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} + default: // Remove this before PR lol + fmt.Println("Something is most definitly afoot at the circle K") } return errInvalidResult From 1a614b6ff5c0962e09bd400697750110d62ac1e1 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:36:34 -0600 Subject: [PATCH 220/245] fixed unknown error data populating issue --- lib/babe/errors_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 8ec2923c6d..a318f19032 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -61,6 +61,7 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } + t.Log(err.Error()) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) From 097c3cfd3e8fe0a7dba9e4b4db9343517d6877db Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 10:56:37 -0600 Subject: [PATCH 221/245] WIP/Get module data to be populated when unmarshalling --- lib/babe/errors.go | 2 +- lib/babe/errors_test.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 28bd5e0f05..245febb355 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -235,7 +235,7 @@ func (err Module) String() string { } func (err CustomModuleError) String() string { - return fmt.Sprintf("index: %d code: %d message: %s", err.index, err.err, err.message) + return fmt.Sprintf("index: %d code: %d message: %p", err.index, err.err, err.message) } func determineErr(res []byte) error { diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index a318f19032..8ec2923c6d 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -61,7 +61,6 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } - t.Log(err.Error()) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) From 0bc78ef889b8b136e4b9c14da3ef68671d774e6c Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 24 Jun 2021 16:31:08 -0600 Subject: [PATCH 222/245] resolved data population issue --- lib/babe/errors.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 245febb355..be7868bb03 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -152,8 +152,6 @@ func determineDispatchErr(res []byte) error { // This works yay! return &DispatchOutcomeError{"bad origin"} case Module: return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} - default: // Remove this before PR lol - fmt.Println("Something is most definitly afoot at the circle K") } return errInvalidResult From c333acc99f61ed580fe02463a86716f9c2f28f36 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 10:58:50 -0600 Subject: [PATCH 223/245] WIP/Finish babe error handling refactor --- lib/babe/errors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index be7868bb03..7ff981c934 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -141,7 +141,7 @@ func determineDispatchErr(res []byte) error { // This works yay! return errInvalidResult } - switch val := vdt.Value().(type){ + switch val := vdt.Value().(type) { case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case CannotLookup: From 77387ad069abfc6a35fe93ee2dccbe1f8ffc5a7c Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 12:48:57 -0600 Subject: [PATCH 224/245] rebase with development --- pkg/scale/decode.go | 104 +------------------------------------------- 1 file changed, 1 insertion(+), 103 deletions(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 1b9a413afd..83914df404 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -315,91 +315,6 @@ func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { return } -func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { - in := dstv.Interface() - inType := reflect.TypeOf(in) - var temp reflect.Value - switch inType.Kind() { - case reflect.Bool: - temp = reflect.New(reflect.TypeOf(false)) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int: - temp = reflect.New(reflect.TypeOf(int(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int8: - temp = reflect.New(reflect.TypeOf(int8(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int16: - temp = reflect.New(reflect.TypeOf(int16(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int32: - temp = reflect.New(reflect.TypeOf(int32(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int64: - temp = reflect.New(reflect.TypeOf(int64(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.String: - temp = reflect.New(reflect.TypeOf("")) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint: - temp = reflect.New(reflect.TypeOf(uint(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint8: - temp = reflect.New(reflect.TypeOf(uint8(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint16: - temp = reflect.New(reflect.TypeOf(uint16(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint32: - temp = reflect.New(reflect.TypeOf(uint32(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint64: - temp = reflect.New(reflect.TypeOf(uint64(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - default: - err = fmt.Errorf("unsupported type for custom primitive: %T", in) - return - } - dstv.Set(temp.Elem().Convert(inType)) - return -} - func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { res := dstv.Interface().(Result) var rb byte @@ -833,21 +748,4 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { } dstv.Set(reflect.ValueOf(ui128)) return -} - -// decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 -// if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise -// it returns nil and error -func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { - buf := make([]byte, 16) - err = binary.Read(ds, binary.LittleEndian, buf) - if err != nil { - return - } - ui128, err := NewUint128(buf) - if err != nil { - return - } - dstv.Set(reflect.ValueOf(ui128)) - return -} +} \ No newline at end of file From 0c2d5e3853451b17f00519d22bda7d5f3a0be420 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 12:58:43 -0600 Subject: [PATCH 225/245] final merge changes --- go.sum | 150 --------------------------------------------------------- 1 file changed, 150 deletions(-) diff --git a/go.sum b/go.sum index 6d7ec116f2..2165343fb3 100644 --- a/go.sum +++ b/go.sum @@ -182,62 +182,44 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -<<<<<<< HEAD github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= -======= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= -github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= ->>>>>>> fix struct tests and optionality tests github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= github.com/google/gopacket v1.1.18/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= -github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/uuid v1.1.5 h1:kxhtnfFVi+rYdOALN0B3k9UT86zVJKfBimRaciULW4I= github.com/google/uuid v1.1.5/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY= github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/gorilla/rpc v1.2.0 h1:WvvdC2lNeT1SP32zrIce5l0ECBfbAlmrmSBsuc57wfk= github.com/gorilla/rpc v1.2.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw= github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= -github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is= github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= -github.com/gtank/ristretto255 v0.1.2 h1:JEqUCPA1NvLq5DwYtuzigd7ss8fwbYay9fi4/5uMzcc= github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIvY4OmlYW69o= github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU= github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48= -github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI= github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= -github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs= github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao= github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc= -github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031 h1:HarGZ5h9HD9LgEg1yRVMXyfiw4wlXiLiYM2oMjeA/SE= github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031/go.mod h1:nNs7wvRfN1eKaMknBydLNQU6146XQim8t4h+q90biWo= github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= @@ -247,7 +229,6 @@ github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUP github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj6+M= github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-cid v0.0.6/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= -github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY= github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.1.0/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= @@ -255,62 +236,46 @@ github.com/ipfs/go-datastore v0.1.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRV github.com/ipfs/go-datastore v0.4.0/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= github.com/ipfs/go-datastore v0.4.1/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= github.com/ipfs/go-datastore v0.4.4/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= -github.com/ipfs/go-datastore v0.4.5 h1:cwOUcGMLdLPWgu3SlrCckCMznaGADbPqE0r8h768/Dg= github.com/ipfs/go-datastore v0.4.5/go.mod h1:eXTcaaiN6uOlVCLS9GjJUJtlvJfM3xk23w3fyfrmmJs= -github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk= github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-ds-badger v0.0.2/go.mod h1:Y3QpeSFWQf6MopLTiZD+VT6IC1yZqaGmjvRcKeSGij8= github.com/ipfs/go-ds-badger v0.0.5/go.mod h1:g5AuuCGmr7efyzQhLL8MzwqcauPojGPUaHzfGTzuE3s= github.com/ipfs/go-ds-badger v0.0.7/go.mod h1:qt0/fWzZDoPW6jpQeqUjR5kBfhDNB65jd9YlmAvpQBk= github.com/ipfs/go-ds-badger v0.2.1/go.mod h1:Tx7l3aTph3FMFrRS838dcSJh+jjA7cX9DrGVwx/NOwE= -github.com/ipfs/go-ds-badger v0.2.3 h1:J27YvAcpuA5IvZUbeBxOcQgqnYHUPxoygc6QxxkodZ4= github.com/ipfs/go-ds-badger v0.2.3/go.mod h1:pEYw0rgg3FIrywKKnL+Snr+w/LjJZVMTBRn4FS6UHUk= -github.com/ipfs/go-ds-badger2 v0.1.0 h1:784py6lXkwlVF+K6XSuqmdMgy5l8GI6k60ngBokb9Fg= github.com/ipfs/go-ds-badger2 v0.1.0/go.mod h1:pbR1p817OZbdId9EvLOhKBgUVTM3BMCSTan78lDDVaw= github.com/ipfs/go-ds-leveldb v0.0.1/go.mod h1:feO8V3kubwsEF22n0YRQCffeb79OOYIykR4L04tMOYc= github.com/ipfs/go-ds-leveldb v0.1.0/go.mod h1:hqAW8y4bwX5LWcCtku2rFNX3vjDZCy5LZCg+cSZvYb8= github.com/ipfs/go-ds-leveldb v0.4.1/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s= -github.com/ipfs/go-ds-leveldb v0.4.2 h1:QmQoAJ9WkPMUfBLnu1sBVy0xWWlJPg0m4kRAiJL9iaw= github.com/ipfs/go-ds-leveldb v0.4.2/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s= github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw= github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc= -github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8= github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ= -github.com/ipfs/go-ipns v0.0.2 h1:oq4ErrV4hNQ2Eim257RTYRgfOSV/s8BDaf9iIl4NwFs= github.com/ipfs/go-ipns v0.0.2/go.mod h1:WChil4e0/m9cIINWLxZe1Jtf77oz5L05rO2ei/uKJ5U= github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM= github.com/ipfs/go-log v1.0.2/go.mod h1:1MNjMxe0u6xvJZgeqbJ8vdo2TKaGwZ1a0Bpza+sr2Sk= github.com/ipfs/go-log v1.0.3/go.mod h1:OsLySYkwIbiSUR/yBTdv1qPtcE4FW3WPWk/ewz9Ru+A= -github.com/ipfs/go-log v1.0.4 h1:6nLQdX4W8P9yZZFH7mO+X/PzjN8Laozm/lMJ6esdgzY= github.com/ipfs/go-log v1.0.4/go.mod h1:oDCg2FkjogeFOhqqb+N39l2RpTNPL6F/StPkB3kPgcs= github.com/ipfs/go-log/v2 v2.0.2/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= github.com/ipfs/go-log/v2 v2.0.3/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= github.com/ipfs/go-log/v2 v2.0.5/go.mod h1:eZs4Xt4ZUJQFM3DlanGhy7TkwwawCZcSByscwkWG+dw= github.com/ipfs/go-log/v2 v2.1.1/go.mod h1:2v2nsGfZsvvAJz13SyFzf9ObaqwHiHxsPLEHntrv9KM= -github.com/ipfs/go-log/v2 v2.1.3 h1:1iS3IU7aXRlbgUpN8yTTpJ53NXYjAe37vcI5+5nYrzk= github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g= github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA= github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= -github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jbenet/go-cienv v0.0.0-20150120210510-1bb1476777ec/go.mod h1:rGaEvXB4uRSZMmzKNLoXvTu1sfx+1kv/DojUlPrSZGs= -github.com/jbenet/go-cienv v0.1.0 h1:Vc/s0QbQtoxX8MwwSLWWh+xNNZvM3Lw7NsTcHrvvhMc= github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA= github.com/jbenet/go-temp-err-catcher v0.0.0-20150120210811-aac704a3f4f2/go.mod h1:8GXXJV31xl8whumTzdZsTt3RnUIiPqzkyf7mxToRCMs= -github.com/jbenet/go-temp-err-catcher v0.1.0 h1:zpb3ZH6wIE8Shj2sKS+khgRvf7T7RABoLk/+KKHggpk= github.com/jbenet/go-temp-err-catcher v0.1.0/go.mod h1:0kJRvmDZXNMIiJirNPEYfhpPwbGVtZVWC34vc5WLsDk= github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY= github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= -github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o= github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= -github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 h1:EFT6MH3igZK/dIVqgGbTqWVvkZ7wJ5iGN03SVtvvdd8= github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25/go.mod h1:sWkGw/wsaHtRsT9zGQ/WyJCotGWG/Anow/9hsAcBWRw= github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= -github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= -github.com/jpillora/ipfilter v1.2.2 h1:lfENG7V1/T+ZutAtSbt6gssvzj3Ql0JmcFlqS/BES2E= github.com/jpillora/ipfilter v1.2.2/go.mod h1:xvAYjA+48eM9E5+sg9yI55N5lE9sefckjsnDvSiEA+g= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= @@ -322,62 +287,46 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d h1:68u9r4wEvL3gYg2jvAOgROwZ3H+Y3hIDk4tbbmIjcYQ= github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= -github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/libp2p/go-addr-util v0.0.1/go.mod h1:4ac6O7n9rIAKB1dnd+s8IbbMXkt+oBpzX4/+RACcnlQ= -github.com/libp2p/go-addr-util v0.0.2 h1:7cWK5cdA5x72jX0g8iLrQWm5TRJZ6CzGdPEhWj7plWU= github.com/libp2p/go-addr-util v0.0.2/go.mod h1:Ecd6Fb3yIuLzq4bD7VcywcVSBtefcAwnUISBM3WG15E= github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ= -github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs= github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM= -github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38yPW7c= github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-conn-security-multistream v0.1.0/go.mod h1:aw6eD7LOsHEX7+2hJkDxw1MteijaVcI+/eP2/x3J1xc= github.com/libp2p/go-conn-security-multistream v0.2.0/go.mod h1:hZN4MjlNetKD3Rq5Jb/P5ohUnFLNzEAR4DLSzpn2QLU= -github.com/libp2p/go-conn-security-multistream v0.2.1 h1:ft6/POSK7F+vl/2qzegnHDaXFU0iWB4yVTYrioC6Zy0= github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70= github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UGQf8A2eEmx4= -github.com/libp2p/go-eventbus v0.2.1 h1:VanAdErQnpTioN2TowqNcOijf6YwhuODe4pPKSDpxGc= github.com/libp2p/go-eventbus v0.2.1/go.mod h1:jc2S4SoEVPP48H9Wpzm5aiGwUCBMfGhVhhBjyhhCJs8= github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8= github.com/libp2p/go-flow-metrics v0.0.2/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= -github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM= github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= github.com/libp2p/go-libp2p v0.6.1/go.mod h1:CTFnWXogryAHjXAKEbOf1OWY+VeAP3lDMZkfEI5sT54= github.com/libp2p/go-libp2p v0.7.0/go.mod h1:hZJf8txWeCduQRDC/WSqBGMxaTHCOYHt2xSU1ivxn0k= github.com/libp2p/go-libp2p v0.7.4/go.mod h1:oXsBlTLF1q7pxr+9w6lqzS1ILpyHsaBPniVO7zIHGMw= github.com/libp2p/go-libp2p v0.8.1/go.mod h1:QRNH9pwdbEBpx5DTJYg+qxcVaDMAz3Ee/qDKwXujH5o= github.com/libp2p/go-libp2p v0.12.0/go.mod h1:FpHZrfC1q7nA8jitvdjKBDF31hguaC676g/nT9PgQM0= -github.com/libp2p/go-libp2p v0.14.2 h1:qs0ABtjjNjS+RIXT1uM7sMJEvIc0pq2nKR0VQxFXhHI= github.com/libp2p/go-libp2p v0.14.2/go.mod h1:0PQMADQEjCM2l8cSMYDpTgsb8gr6Zq7i4LUgq1mlW2E= -github.com/libp2p/go-libp2p-asn-util v0.0.0-20200825225859-85005c6cf052 h1:BM7aaOF7RpmNn9+9g6uTjGJ0cTzWr5j9i9IKeun2M8U= github.com/libp2p/go-libp2p-asn-util v0.0.0-20200825225859-85005c6cf052/go.mod h1:nRMRTab+kZuk0LnKZpxhOVH/ndsdr2Nr//Zltc/vwgo= github.com/libp2p/go-libp2p-autonat v0.1.1/go.mod h1:OXqkeGOY2xJVWKAGV2inNF5aKN/djNA3fdpCWloIudE= github.com/libp2p/go-libp2p-autonat v0.2.0/go.mod h1:DX+9teU4pEEoZUqR1PiMlqliONQdNbfzE1C718tcViI= github.com/libp2p/go-libp2p-autonat v0.2.1/go.mod h1:MWtAhV5Ko1l6QBsHQNSuM6b1sRkXrpk0/LqCr+vCVxI= github.com/libp2p/go-libp2p-autonat v0.2.2/go.mod h1:HsM62HkqZmHR2k1xgX34WuWDzk/nBwNHoeyyT4IWV6A= github.com/libp2p/go-libp2p-autonat v0.4.0/go.mod h1:YxaJlpr81FhdOv3W3BTconZPfhaYivRdf53g+S2wobk= -github.com/libp2p/go-libp2p-autonat v0.4.2 h1:YMp7StMi2dof+baaxkbxaizXjY1RPvU71CXfxExzcUU= github.com/libp2p/go-libp2p-autonat v0.4.2/go.mod h1:YxaJlpr81FhdOv3W3BTconZPfhaYivRdf53g+S2wobk= github.com/libp2p/go-libp2p-blankhost v0.1.1/go.mod h1:pf2fvdLJPsC1FsVrNP3DUUvMzUts2dsLLBEpo1vW1ro= github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU= -github.com/libp2p/go-libp2p-blankhost v0.2.0 h1:3EsGAi0CBGcZ33GwRuXEYJLLPoVWyXJ1bcJzAJjINkk= github.com/libp2p/go-libp2p-blankhost v0.2.0/go.mod h1:eduNKXGTioTuQAUcZ5epXi9vMl+t4d8ugUBRQ4SqaNQ= github.com/libp2p/go-libp2p-circuit v0.1.4/go.mod h1:CY67BrEjKNDhdTk8UgBX1Y/H5c3xkAcs3gnksxY7osU= github.com/libp2p/go-libp2p-circuit v0.2.1/go.mod h1:BXPwYDN5A8z4OEY9sOfr2DUQMLQvKt/6oku45YUmjIo= -github.com/libp2p/go-libp2p-circuit v0.4.0 h1:eqQ3sEYkGTtybWgr6JLqJY6QLtPWRErvFjFDfAOO1wc= github.com/libp2p/go-libp2p-circuit v0.4.0/go.mod h1:t/ktoFIUzM6uLQ+o1G6NuBl2ANhBKN9Bc8jRIk31MoA= github.com/libp2p/go-libp2p-core v0.0.1/go.mod h1:g/VxnTZ/1ygHxH3dKok7Vno1VfpvGcGip57wjTU4fco= github.com/libp2p/go-libp2p-core v0.0.4/go.mod h1:jyuCQP356gzfCFtRKyvAbNkyeuxb7OlyhWZ3nls5d2I= @@ -401,16 +350,12 @@ github.com/libp2p/go-libp2p-core v0.7.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB github.com/libp2p/go-libp2p-core v0.8.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-core v0.8.1/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-core v0.8.2/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= -github.com/libp2p/go-libp2p-core v0.8.5 h1:aEgbIcPGsKy6zYcC+5AJivYFedhYa4sW7mIpWpUaLKw= github.com/libp2p/go-libp2p-core v0.8.5/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= github.com/libp2p/go-libp2p-discovery v0.3.0/go.mod h1:o03drFnz9BVAZdzC/QUQ+NeQOu38Fu7LJGEOK2gQltw= -github.com/libp2p/go-libp2p-discovery v0.5.0 h1:Qfl+e5+lfDgwdrXdu4YNCWyEo3fWuP+WgN9mN0iWviQ= github.com/libp2p/go-libp2p-discovery v0.5.0/go.mod h1:+srtPIU9gDaBNu//UHvcdliKBIcr4SfDcm0/PfPJLug= -github.com/libp2p/go-libp2p-kad-dht v0.11.1 h1:FsriVQhOUZpCotWIjyFSjEDNJmUzuMma/RyyTDZanwc= github.com/libp2p/go-libp2p-kad-dht v0.11.1/go.mod h1:5ojtR2acDPqh/jXf5orWy8YGb8bHQDS+qeDcoscL/PI= -github.com/libp2p/go-libp2p-kbucket v0.4.7 h1:spZAcgxifvFZHBD8tErvppbnNiKA5uokDu3CV7axu70= github.com/libp2p/go-libp2p-kbucket v0.4.7/go.mod h1:XyVo99AfQH0foSf176k4jY1xUJ2+jUJIZCSDm7r2YKk= github.com/libp2p/go-libp2p-loggables v0.1.0/go.mod h1:EyumB2Y6PrYjr55Q3/tiJ/o3xoDasoRYM7nOzEpoa90= github.com/libp2p/go-libp2p-mplex v0.2.0/go.mod h1:Ejl9IyjvXJ0T9iqUTE1jpYATQ9NM3g+OtR+EMMODbKo= @@ -419,15 +364,11 @@ github.com/libp2p/go-libp2p-mplex v0.2.2/go.mod h1:74S9eum0tVQdAfFiKxAyKzNdSuLqw github.com/libp2p/go-libp2p-mplex v0.2.3/go.mod h1:CK3p2+9qH9x+7ER/gWWDYJ3QW5ZxWDkm+dVvjfuG3ek= github.com/libp2p/go-libp2p-mplex v0.3.0/go.mod h1:l9QWxRbbb5/hQMECEb908GbS9Sm2UAR2KFZKUJEynEs= github.com/libp2p/go-libp2p-mplex v0.4.0/go.mod h1:yCyWJE2sc6TBTnFpjvLuEJgTSw/u+MamvzILKdX7asw= -github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc= github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g= github.com/libp2p/go-libp2p-nat v0.0.5/go.mod h1:1qubaE5bTZMJE+E/uu2URroMbzdubFz1ChgiN79yKPE= -github.com/libp2p/go-libp2p-nat v0.0.6 h1:wMWis3kYynCbHoyKLPBEMu4YRLltbm8Mk08HGSfvTkU= github.com/libp2p/go-libp2p-nat v0.0.6/go.mod h1:iV59LVhB3IkFvS6S6sauVTSOrNEANnINbI/fkaLimiw= -github.com/libp2p/go-libp2p-netutil v0.1.0 h1:zscYDNVEcGxyUpMd0JReUZTrpMfia8PmLKcKF72EAMQ= github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCThNdbQD54k3TqjpbFU= github.com/libp2p/go-libp2p-noise v0.1.1/go.mod h1:QDFLdKX7nluB7DEnlVPbz7xlLHdwHFA9HiohJRr3vwM= -github.com/libp2p/go-libp2p-noise v0.2.0 h1:wmk5nhB9a2w2RxMOyvsoKjizgJOEaJdfAakr0jN8gds= github.com/libp2p/go-libp2p-noise v0.2.0/go.mod h1:IEbYhBBzGyvdLBoxxULL/SGbJARhUeqlO8lVSREYu2Q= github.com/libp2p/go-libp2p-peer v0.2.0/go.mod h1:RCffaCvUyW2CJmG2gAWVqwePwW7JMgxjsHm7+J5kjWY= github.com/libp2p/go-libp2p-peerstore v0.1.0/go.mod h1:2CeHkQsr8svp4fZ+Oi9ykN1HBb6u0MOvdJ7YIsmcwtY= @@ -437,21 +378,15 @@ github.com/libp2p/go-libp2p-peerstore v0.2.0/go.mod h1:N2l3eVIeAitSg3Pi2ipSrJYnq github.com/libp2p/go-libp2p-peerstore v0.2.1/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRjwRLBr4TYKfNgrUkOPA= github.com/libp2p/go-libp2p-peerstore v0.2.2/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRjwRLBr4TYKfNgrUkOPA= github.com/libp2p/go-libp2p-peerstore v0.2.6/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= -github.com/libp2p/go-libp2p-peerstore v0.2.7 h1:83JoLxyR9OYTnNfB5vvFqvMUv/xDNa6NoPHnENhBsGw= github.com/libp2p/go-libp2p-peerstore v0.2.7/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= -github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= -github.com/libp2p/go-libp2p-quic-transport v0.10.0 h1:koDCbWD9CCHwcHZL3/WEvP2A+e/o5/W5L3QS/2SPMA0= github.com/libp2p/go-libp2p-quic-transport v0.10.0/go.mod h1:RfJbZ8IqXIhxBRm5hqUEJqjiiY8xmEuq3HUDS993MkA= github.com/libp2p/go-libp2p-record v0.1.2/go.mod h1:pal0eNcT5nqZaTV7UGhqeGqxFgGdsU/9W//C8dqjQDk= -github.com/libp2p/go-libp2p-record v0.1.3 h1:R27hoScIhQf/A8XJZ8lYpnqh9LatJ5YbHs28kCIfql0= github.com/libp2p/go-libp2p-record v0.1.3/go.mod h1:yNUff/adKIfPnYQXgp6FQmNu3gLJ6EMg7+/vv2+9pY4= -github.com/libp2p/go-libp2p-routing-helpers v0.2.3 h1:xY61alxJ6PurSi+MXbywZpelvuU4U4p/gPTxjqCqTzY= github.com/libp2p/go-libp2p-routing-helpers v0.2.3/go.mod h1:795bh+9YeoFl99rMASoiVgHdi5bjack0N1+AFAdbvBw= github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8= github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g= github.com/libp2p/go-libp2p-secio v0.2.1/go.mod h1:cWtZpILJqkqrSkiYcDBh5lA3wbT2Q+hz3rJQq3iftD8= -github.com/libp2p/go-libp2p-secio v0.2.2 h1:rLLPvShPQAcY6eNurKNZq3eZjPWfU9kXF2eI9jIYdrg= github.com/libp2p/go-libp2p-secio v0.2.2/go.mod h1:wP3bS+m5AUnFA+OFO7Er03uO1mncHG0uVwGrwvjYlNY= github.com/libp2p/go-libp2p-swarm v0.1.0/go.mod h1:wQVsCdjsuZoc730CgOvh5ox6K8evllckjebkdiY5ta4= github.com/libp2p/go-libp2p-swarm v0.2.2/go.mod h1:fvmtQ0T1nErXym1/aa1uJEyN7JzaTNyBcHImCxRpPKU= @@ -459,7 +394,6 @@ github.com/libp2p/go-libp2p-swarm v0.2.3/go.mod h1:P2VO/EpxRyDxtChXz/VPVXyTnszHv github.com/libp2p/go-libp2p-swarm v0.2.8/go.mod h1:JQKMGSth4SMqonruY0a8yjlPVIkb0mdNSwckW7OYziM= github.com/libp2p/go-libp2p-swarm v0.3.0/go.mod h1:hdv95GWCTmzkgeJpP+GK/9D9puJegb7H57B5hWQR5Kk= github.com/libp2p/go-libp2p-swarm v0.3.1/go.mod h1:hdv95GWCTmzkgeJpP+GK/9D9puJegb7H57B5hWQR5Kk= -github.com/libp2p/go-libp2p-swarm v0.5.0 h1:HIK0z3Eqoo8ugmN8YqWAhD2RORgR+3iNXYG4U2PFd1E= github.com/libp2p/go-libp2p-swarm v0.5.0/go.mod h1:sU9i6BoHE0Ve5SKz3y9WfKrh8dUat6JknzUehFx8xW4= github.com/libp2p/go-libp2p-testing v0.0.2/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= github.com/libp2p/go-libp2p-testing v0.0.3/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= @@ -468,14 +402,11 @@ github.com/libp2p/go-libp2p-testing v0.1.0/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eq github.com/libp2p/go-libp2p-testing v0.1.1/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0= github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod h1:Qy8sAncLKpwXtS2dSnDOP8ktexIAHKu+J+pnZOFZLTc= github.com/libp2p/go-libp2p-testing v0.3.0/go.mod h1:efZkql4UZ7OVsEfaxNHZPzIehtsBXMrXnCfJIgDti5g= -github.com/libp2p/go-libp2p-testing v0.4.0 h1:PrwHRi0IGqOwVQWR3xzgigSlhlLfxgfXgkHxr77EghQ= github.com/libp2p/go-libp2p-testing v0.4.0/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0= -github.com/libp2p/go-libp2p-tls v0.1.3 h1:twKMhMu44jQO+HgQK9X8NHO5HkeJu2QbhLzLJpa8oNM= github.com/libp2p/go-libp2p-tls v0.1.3/go.mod h1:wZfuewxOndz5RTnCAxFliGjvYSDA40sKitV4c50uI1M= github.com/libp2p/go-libp2p-transport-upgrader v0.1.1/go.mod h1:IEtA6or8JUbsV07qPW4r01GnTenLW4oi3lOPbUMGJJA= github.com/libp2p/go-libp2p-transport-upgrader v0.2.0/go.mod h1:mQcrHj4asu6ArfSoMuyojOdjx73Q47cYD7s5+gZOlns= github.com/libp2p/go-libp2p-transport-upgrader v0.3.0/go.mod h1:i+SKzbRnvXdVbU3D1dwydnTmKRPXiAR/fyvi1dXuL4o= -github.com/libp2p/go-libp2p-transport-upgrader v0.4.2 h1:4JsnbfJzgZeRS9AWN7B9dPqn/LY/HoQTlO9gtdJTIYM= github.com/libp2p/go-libp2p-transport-upgrader v0.4.2/go.mod h1:NR8ne1VwfreD5VIWIU62Agt/J18ekORFU/j1i2y8zvk= github.com/libp2p/go-libp2p-yamux v0.2.0/go.mod h1:Db2gU+XfLpm6E4rG5uGCFX6uXA8MEXOxFcRoXUODaK8= github.com/libp2p/go-libp2p-yamux v0.2.2/go.mod h1:lIohaR0pT6mOt0AZ0L2dFze9hds9Req3OfS+B+dv4qw= @@ -484,59 +415,46 @@ github.com/libp2p/go-libp2p-yamux v0.2.7/go.mod h1:X28ENrBMU/nm4I3Nx4sZ4dgjZ6VhL github.com/libp2p/go-libp2p-yamux v0.2.8/go.mod h1:/t6tDqeuZf0INZMTgd0WxIRbtK2EzI2h7HbFm9eAKI4= github.com/libp2p/go-libp2p-yamux v0.4.0/go.mod h1:+DWDjtFMzoAwYLVkNZftoucn7PelNoy5nm3tZ3/Zw30= github.com/libp2p/go-libp2p-yamux v0.5.0/go.mod h1:AyR8k5EzyM2QN9Bbdg6X1SkVVuqLwTGf0L4DFq9g6po= -github.com/libp2p/go-libp2p-yamux v0.5.4 h1:/UOPtT/6DHPtr3TtKXBHa6g0Le0szYuI33Xc/Xpd7fQ= github.com/libp2p/go-libp2p-yamux v0.5.4/go.mod h1:tfrXbyaTqqSU654GTvK3ocnSZL3BuHoeTSqhcel1wsE= github.com/libp2p/go-maddr-filter v0.0.4/go.mod h1:6eT12kSQMA9x2pvFQa+xesMKUBlj9VImZbj3B9FBH/Q= github.com/libp2p/go-maddr-filter v0.0.5/go.mod h1:Jk+36PMfIqCJhAnaASRH83bdAvfDRp/w6ENFaC9bG+M= -github.com/libp2p/go-maddr-filter v0.1.0 h1:4ACqZKw8AqiuJfwFGq1CYDFugfXTOos+qQ3DETkhtCE= github.com/libp2p/go-maddr-filter v0.1.0/go.mod h1:VzZhTXkMucEGGEOSKddrwGiOv0tUhgnKqNEmIAz/bPU= github.com/libp2p/go-mplex v0.0.3/go.mod h1:pK5yMLmOoBR1pNCqDlA2GQrdAVTMkqFalaTWe7l4Yd0= github.com/libp2p/go-mplex v0.1.0/go.mod h1:SXgmdki2kwCUlCCbfGLEgHjC4pFqhTp0ZoV6aiKgxDU= github.com/libp2p/go-mplex v0.1.1/go.mod h1:Xgz2RDCi3co0LeZfgjm4OgUF15+sVR8SRcu3SFXI1lk= github.com/libp2p/go-mplex v0.1.2/go.mod h1:Xgz2RDCi3co0LeZfgjm4OgUF15+sVR8SRcu3SFXI1lk= github.com/libp2p/go-mplex v0.2.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= -github.com/libp2p/go-mplex v0.3.0 h1:U1T+vmCYJaEoDJPV1aq31N56hS+lJgb397GsylNSgrU= github.com/libp2p/go-mplex v0.3.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= github.com/libp2p/go-msgio v0.0.2/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= -github.com/libp2p/go-msgio v0.0.6 h1:lQ7Uc0kS1wb1EfRxO2Eir/RJoHkHn7t6o+EiwsYIKJA= github.com/libp2p/go-msgio v0.0.6/go.mod h1:4ecVB6d9f4BDSL5fqvPiC4A3KivjWn+Venn/1ALLMWA= github.com/libp2p/go-nat v0.0.4/go.mod h1:Nmw50VAvKuk38jUBcmNh6p9lUJLoODbJRvYAa/+KSDo= -github.com/libp2p/go-nat v0.0.5 h1:qxnwkco8RLKqVh1NmjQ+tJ8p8khNLFxuElYG/TwqW4Q= github.com/libp2p/go-nat v0.0.5/go.mod h1:B7NxsVNPZmRLvMOwiEO1scOSyjA56zxYAGv1yQgRkEU= github.com/libp2p/go-netroute v0.1.2/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= github.com/libp2p/go-netroute v0.1.3/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= -github.com/libp2p/go-netroute v0.1.6 h1:ruPJStbYyXVYGQ81uzEDzuvbYRLKRrLvTYd33yomC38= github.com/libp2p/go-netroute v0.1.6/go.mod h1:AqhkMh0VuWmfgtxKPp3Oc1LdU5QSWS7wl0QLhSZqXxQ= github.com/libp2p/go-openssl v0.0.2/go.mod h1:v8Zw2ijCSWBQi8Pq5GAixw6DbFfa9u6VIYDXnvOXkc0= github.com/libp2p/go-openssl v0.0.3/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-openssl v0.0.4/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-openssl v0.0.5/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= -github.com/libp2p/go-openssl v0.0.7 h1:eCAzdLejcNVBzP/iZM9vqHnQm+XyCEbSSIheIPRGNsw= github.com/libp2p/go-openssl v0.0.7/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-reuseport v0.0.1/go.mod h1:jn6RmB1ufnQwl0Q1f+YxAj8isJgDCQzaaxIFYDhcYEA= -github.com/libp2p/go-reuseport v0.0.2 h1:XSG94b1FJfGA01BUrT82imejHQyTxO4jEWqheyCXYvU= github.com/libp2p/go-reuseport v0.0.2/go.mod h1:SPD+5RwGC7rcnzngoYC86GjPzjSywuQyMVAheVBD9nQ= github.com/libp2p/go-reuseport-transport v0.0.2/go.mod h1:YkbSDrvjUVDL6b8XqriyA20obEtsW9BLkuOUyQAOCbs= github.com/libp2p/go-reuseport-transport v0.0.3/go.mod h1:Spv+MPft1exxARzP2Sruj2Wb5JSyHNncjf1Oi2dEbzM= -github.com/libp2p/go-reuseport-transport v0.0.4 h1:OZGz0RB620QDGpv300n1zaOcKGGAoGVf8h9txtt/1uM= github.com/libp2p/go-reuseport-transport v0.0.4/go.mod h1:trPa7r/7TJK/d+0hdBLOCGvpQQVOU74OXbNCIMkufGw= github.com/libp2p/go-sockaddr v0.0.2/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= -github.com/libp2p/go-sockaddr v0.1.1 h1:yD80l2ZOdGksnOyHrhxDdTDFrf7Oy+v3FMVArIRgZxQ= github.com/libp2p/go-sockaddr v0.1.1/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= github.com/libp2p/go-stream-muxer v0.0.1/go.mod h1:bAo8x7YkSpadMTbtTaxGVHWUQsR/l5MEaHbKaliuT14= github.com/libp2p/go-stream-muxer-multistream v0.2.0/go.mod h1:j9eyPol/LLRqT+GPLSxvimPhNph4sfYfMoDPd7HkzIc= -github.com/libp2p/go-stream-muxer-multistream v0.3.0 h1:TqnSHPJEIqDEO7h1wZZ0p3DXdvDSiLHQidKKUGZtiOY= github.com/libp2p/go-stream-muxer-multistream v0.3.0/go.mod h1:yDh8abSIzmZtqtOt64gFJUXEryejzNb0lisTt+fAMJA= github.com/libp2p/go-tcp-transport v0.1.0/go.mod h1:oJ8I5VXryj493DEJ7OsBieu8fcg2nHGctwtInJVpipc= github.com/libp2p/go-tcp-transport v0.1.1/go.mod h1:3HzGvLbx6etZjnFlERyakbaYPdfjg2pWP97dFZworkY= github.com/libp2p/go-tcp-transport v0.2.0/go.mod h1:vX2U0CnWimU4h0SGSEsg++AzvBcroCGYw28kh94oLe0= -github.com/libp2p/go-tcp-transport v0.2.1 h1:ExZiVQV+h+qL16fzCWtd1HSzPsqWottJ8KXwWaVi8Ns= github.com/libp2p/go-tcp-transport v0.2.1/go.mod h1:zskiJ70MEfWz2MKxvFB/Pv+tPIB1PpPUrHIWQ8aFw7M= github.com/libp2p/go-ws-transport v0.2.0/go.mod h1:9BHJz/4Q5A9ludYWKoGCFC5gUElzlHoKzu0yY9p/klM= github.com/libp2p/go-ws-transport v0.3.0/go.mod h1:bpgTJmRZAvVHrgHybCVyqoBmyLQ1fiZuEaBYusP5zsk= github.com/libp2p/go-ws-transport v0.3.1/go.mod h1:bpgTJmRZAvVHrgHybCVyqoBmyLQ1fiZuEaBYusP5zsk= -github.com/libp2p/go-ws-transport v0.4.0 h1:9tvtQ9xbws6cA5LvqdE6Ne3vcmGB4f1z9SByggk4s0k= github.com/libp2p/go-ws-transport v0.4.0/go.mod h1:EcIEKqf/7GDjth6ksuS/6p7R49V4CBY6/E7R/iyhYUA= github.com/libp2p/go-yamux v1.2.2/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/libp2p/go-yamux v1.3.0/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= @@ -544,30 +462,23 @@ github.com/libp2p/go-yamux v1.3.3/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZ github.com/libp2p/go-yamux v1.3.5/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/libp2p/go-yamux v1.3.7/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux v1.4.0/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= -github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI= github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= -github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= -github.com/lucas-clemente/quic-go v0.19.3 h1:eCDQqvGBB+kCTkA0XrAFtNe81FMa0/fn4QSoeAbmiF4= github.com/lucas-clemente/quic-go v0.19.3/go.mod h1:ADXpNbTQjq1hIzCpB+y/k5iz4n4z4IwqoLb94Kh5Hu8= github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/marten-seemann/qpack v0.2.1/go.mod h1:F7Gl5L1jIgN1D11ucXefiuJS9UMVP2opoCp2jDKb7wc= -github.com/marten-seemann/qtls v0.10.0 h1:ECsuYUKalRL240rRD4Ri33ISb7kAQ3qGDlrrl55b2pc= github.com/marten-seemann/qtls v0.10.0/go.mod h1:UvMd1oaYDACI99/oZUYLzMCkBXQVT0aGm99sJhbT8hs= -github.com/marten-seemann/qtls-go1-15 v0.1.1 h1:LIH6K34bPVttyXnUWixk0bzH6/N07VxbSabxn5A5gZQ= github.com/marten-seemann/qtls-go1-15 v0.1.1/go.mod h1:GyFwywLKkRt+6mfU99csTEY1joMZz5vmB1WNZH3P81I= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= @@ -575,17 +486,13 @@ github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00v github.com/miekg/dns v1.1.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.28/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= -github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY= github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= -github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 h1:hLDRPB66XQT/8+wG9WsDpiCvZf1yKO7sz7scAjSlBa0= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= -github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= -github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= @@ -595,11 +502,8 @@ github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVq github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= -github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= -github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI= github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= -github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4= github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM= github.com/multiformats/go-multiaddr v0.0.1/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= github.com/multiformats/go-multiaddr v0.0.2/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= @@ -610,15 +514,12 @@ github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y9 github.com/multiformats/go-multiaddr v0.2.1/go.mod h1:s/Apk6IyxfvMjDafnhJgJ3/46z7tZ04iMk5wP4QMGGE= github.com/multiformats/go-multiaddr v0.2.2/go.mod h1:NtfXiOtHvghW9KojvtySjH5y0u0xW5UouOmQQrn6a3Y= github.com/multiformats/go-multiaddr v0.3.0/go.mod h1:dF9kph9wfJ+3VLAaeBqo9Of8x4fJxp6ggJGteB8HQTI= -github.com/multiformats/go-multiaddr v0.3.1 h1:1bxa+W7j9wZKTZREySx1vPMs2TqrYWjVZ7zE6/XLG1I= github.com/multiformats/go-multiaddr v0.3.1/go.mod h1:uPbspcUPd5AfaP6ql3ujFY+QWzmBD8uLLL4bXW0XfGc= github.com/multiformats/go-multiaddr-dns v0.0.1/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.0.2/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.2.0/go.mod h1:TJ5pr5bBO7Y1B18djPuRsVkduhQH2YqYSbxWJzYGdK0= -github.com/multiformats/go-multiaddr-dns v0.3.1 h1:QgQgR+LQVt3NPTjbrLLpsaT2ufAA2y0Mkk+QRVJbW3A= github.com/multiformats/go-multiaddr-dns v0.3.1/go.mod h1:G/245BRQ6FJGmryJCrOuTdB37AMA5AMOVuO6NY3JwTk= github.com/multiformats/go-multiaddr-fmt v0.0.1/go.mod h1:aBYjqL4T/7j4Qx+R73XSv/8JsgnRFlf0w2KGLCmXl3Q= -github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E= github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo= github.com/multiformats/go-multiaddr-net v0.0.1/go.mod h1:nw6HSxNmCIQH27XPGBuX+d1tnvM7ihcFwHMSstNAVUU= github.com/multiformats/go-multiaddr-net v0.1.0/go.mod h1:5JNbcfBOP4dnhoZOv10JJVkJO0pCCEf8mTnipAo2UZQ= @@ -627,10 +528,8 @@ github.com/multiformats/go-multiaddr-net v0.1.2/go.mod h1:QsWt3XK/3hwvNxZJp92iMQ github.com/multiformats/go-multiaddr-net v0.1.3/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= github.com/multiformats/go-multiaddr-net v0.1.4/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= github.com/multiformats/go-multiaddr-net v0.1.5/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= -github.com/multiformats/go-multiaddr-net v0.2.0 h1:MSXRGN0mFymt6B1yo/6BPnIRpLPEnKgQNvVfCX5VDJk= github.com/multiformats/go-multiaddr-net v0.2.0/go.mod h1:gGdH3UXny6U3cKKYCvpXI5rnK7YaOIEOPVDI9tsJbEA= github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs= -github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk= github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc= github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po= @@ -638,60 +537,45 @@ github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa github.com/multiformats/go-multihash v0.0.9/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= github.com/multiformats/go-multihash v0.0.10/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= -github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I= github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= github.com/multiformats/go-multistream v0.2.0/go.mod h1:5GZPQZbkWOLOn3J2y4Y99vVW7vOfsAflxARk3x14o6k= github.com/multiformats/go-multistream v0.2.1/go.mod h1:5GZPQZbkWOLOn3J2y4Y99vVW7vOfsAflxARk3x14o6k= -github.com/multiformats/go-multistream v0.2.2 h1:TCYu1BHTDr1F/Qm75qwYISQdzGcRdC21nFgQW7l7GBo= github.com/multiformats/go-multistream v0.2.2/go.mod h1:UIcnm7Zuo8HKG+HkWgfQsGL+/MIEhyTqbODbIUwSXKs= github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= -github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY= github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= -github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975 h1:zm/Rb2OsnLWCY88Njoqgo4X6yt/lx3oBNWhepX0AOMU= github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975/go.mod h1:4Mct/lWCFf1jzQTTAaWtOI7sXqmG+wBeiBfT4CxoaJk= -github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks= github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= -github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 h1:shk/vn9oCoOTmwcouEdwIeOtOGA/ELRUw/GwvxwfT+0= github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= -github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA= -github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea h1:okKoivlkNRRLqXraEtatHfEhW+D71QTwkaj+4n4M2Xc= github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea/go.mod h1:3KEU5Dm8MAYWZqity880wOFJ9PhQjyKVZGwAEfc5Q4E= -github.com/phuslu/iploc v1.0.20200807 h1:LIBm2Y9l5zmUvnJhQgMcLZ0iVwuG+5/L6AgbMwSOpE4= github.com/phuslu/iploc v1.0.20200807/go.mod h1:Q/0VX0txvbxekt4NhWIi3Q3eyZ139lHhnlzvDxyXhuc= -github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo= github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= @@ -700,7 +584,6 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7q github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= -github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -733,10 +616,8 @@ github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qt github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE= github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA= github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0= -github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU= github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= -github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= @@ -746,41 +627,30 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/src-d/envconfig v1.0.0/go.mod h1:Q9YQZ7BKITldTBnoxsE5gOeB5y66RyPXeue/R4aaNBc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= -github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca h1:Ld/zXl5t4+D69SiV4JoN7kkfvJdOWlPpfxrzxpLMoUk= github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= -github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc= github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= -github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI= github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= -github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= -github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= -github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec h1:VElCeVyfCWNmCv6UisKQrr+P2/JRG0uf4/FIdCB4pL0= github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec/go.mod h1:VGyarTzasuS7k5KhSIGpM3tciSZlkP31Mp9VJTHMMeI= -github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= github.com/whyrusleeping/go-logging v0.0.1/go.mod h1:lDPYj54zutzG1XYfHAhcc7oNXEburHQBn+Iqd4yS4vE= github.com/whyrusleeping/mafmt v1.2.8/go.mod h1:faQJFPbLSxzD9xpA02ttW/tS9vZykNvXwGvqIpk20FA= -github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 h1:Y1/FEOpaCpD21WxrmfeIYCFPuVPRCY2XZTWzTNHGw30= github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4= -github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI= github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= @@ -792,23 +662,18 @@ go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M= go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= -go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/goleak v1.0.0 h1:qsup4IcBdlmsnGfqyLl4Ntn3C2XCCuKAE7DwHpScyUo= go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= -go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.14.1/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= -go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM= go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE= golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw= @@ -843,12 +708,10 @@ golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTk golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k= golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -888,7 +751,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180810173357-98c5dad5d1a0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -926,14 +788,12 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 h1:JWgyZ1qgdTaF3N3oxC+MdTV7qvEEgHo3otj+HB5CM7Q= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -956,12 +816,10 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20210106214847-113979e3529a h1:CB3a9Nez8M13wwlr/E2YtwoU+qYHKfC+JrDa45RXXoQ= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= @@ -971,7 +829,6 @@ google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -992,7 +849,6 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.28.1/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= -google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= @@ -1009,30 +865,24 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= -gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU= gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c= gopkg.in/src-d/go-cli.v0 v0.0.0-20181105080154-d492247bbc0d/go.mod h1:z+K8VcOYVYcSwSjGebuDL6176A1XskgbtNl64NSg+n8= gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= From 0d23e3258751dc8d85373d283bda3d01b7d9d30f Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 13:10:17 -0600 Subject: [PATCH 226/245] added further testing for dispatch errors --- go.sum | 40 ++++++++++++++++++++++++++++++++++++++++ lib/babe/errors.go | 5 ++--- lib/babe/errors_test.go | 10 ++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/go.sum b/go.sum index 2165343fb3..1a7f5c763d 100644 --- a/go.sum +++ b/go.sum @@ -184,11 +184,13 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= github.com/google/gopacket v1.1.18/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= +github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= @@ -260,6 +262,7 @@ github.com/ipfs/go-log/v2 v2.0.2/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBW github.com/ipfs/go-log/v2 v2.0.3/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= github.com/ipfs/go-log/v2 v2.0.5/go.mod h1:eZs4Xt4ZUJQFM3DlanGhy7TkwwawCZcSByscwkWG+dw= github.com/ipfs/go-log/v2 v2.1.1/go.mod h1:2v2nsGfZsvvAJz13SyFzf9ObaqwHiHxsPLEHntrv9KM= +github.com/ipfs/go-log/v2 v2.1.3 h1:1iS3IU7aXRlbgUpN8yTTpJ53NXYjAe37vcI5+5nYrzk= github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g= github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA= github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= @@ -290,6 +293,7 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -303,6 +307,7 @@ github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoR github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-conn-security-multistream v0.1.0/go.mod h1:aw6eD7LOsHEX7+2hJkDxw1MteijaVcI+/eP2/x3J1xc= github.com/libp2p/go-conn-security-multistream v0.2.0/go.mod h1:hZN4MjlNetKD3Rq5Jb/P5ohUnFLNzEAR4DLSzpn2QLU= +github.com/libp2p/go-conn-security-multistream v0.2.1 h1:ft6/POSK7F+vl/2qzegnHDaXFU0iWB4yVTYrioC6Zy0= github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70= github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UGQf8A2eEmx4= github.com/libp2p/go-eventbus v0.2.1/go.mod h1:jc2S4SoEVPP48H9Wpzm5aiGwUCBMfGhVhhBjyhhCJs8= @@ -314,6 +319,7 @@ github.com/libp2p/go-libp2p v0.7.0/go.mod h1:hZJf8txWeCduQRDC/WSqBGMxaTHCOYHt2xS github.com/libp2p/go-libp2p v0.7.4/go.mod h1:oXsBlTLF1q7pxr+9w6lqzS1ILpyHsaBPniVO7zIHGMw= github.com/libp2p/go-libp2p v0.8.1/go.mod h1:QRNH9pwdbEBpx5DTJYg+qxcVaDMAz3Ee/qDKwXujH5o= github.com/libp2p/go-libp2p v0.12.0/go.mod h1:FpHZrfC1q7nA8jitvdjKBDF31hguaC676g/nT9PgQM0= +github.com/libp2p/go-libp2p v0.14.2 h1:qs0ABtjjNjS+RIXT1uM7sMJEvIc0pq2nKR0VQxFXhHI= github.com/libp2p/go-libp2p v0.14.2/go.mod h1:0PQMADQEjCM2l8cSMYDpTgsb8gr6Zq7i4LUgq1mlW2E= github.com/libp2p/go-libp2p-asn-util v0.0.0-20200825225859-85005c6cf052/go.mod h1:nRMRTab+kZuk0LnKZpxhOVH/ndsdr2Nr//Zltc/vwgo= github.com/libp2p/go-libp2p-autonat v0.1.1/go.mod h1:OXqkeGOY2xJVWKAGV2inNF5aKN/djNA3fdpCWloIudE= @@ -321,6 +327,7 @@ github.com/libp2p/go-libp2p-autonat v0.2.0/go.mod h1:DX+9teU4pEEoZUqR1PiMlqliONQ github.com/libp2p/go-libp2p-autonat v0.2.1/go.mod h1:MWtAhV5Ko1l6QBsHQNSuM6b1sRkXrpk0/LqCr+vCVxI= github.com/libp2p/go-libp2p-autonat v0.2.2/go.mod h1:HsM62HkqZmHR2k1xgX34WuWDzk/nBwNHoeyyT4IWV6A= github.com/libp2p/go-libp2p-autonat v0.4.0/go.mod h1:YxaJlpr81FhdOv3W3BTconZPfhaYivRdf53g+S2wobk= +github.com/libp2p/go-libp2p-autonat v0.4.2 h1:YMp7StMi2dof+baaxkbxaizXjY1RPvU71CXfxExzcUU= github.com/libp2p/go-libp2p-autonat v0.4.2/go.mod h1:YxaJlpr81FhdOv3W3BTconZPfhaYivRdf53g+S2wobk= github.com/libp2p/go-libp2p-blankhost v0.1.1/go.mod h1:pf2fvdLJPsC1FsVrNP3DUUvMzUts2dsLLBEpo1vW1ro= github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU= @@ -350,6 +357,7 @@ github.com/libp2p/go-libp2p-core v0.7.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB github.com/libp2p/go-libp2p-core v0.8.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-core v0.8.1/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-core v0.8.2/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= +github.com/libp2p/go-libp2p-core v0.8.5 h1:aEgbIcPGsKy6zYcC+5AJivYFedhYa4sW7mIpWpUaLKw= github.com/libp2p/go-libp2p-core v0.8.5/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= @@ -364,11 +372,13 @@ github.com/libp2p/go-libp2p-mplex v0.2.2/go.mod h1:74S9eum0tVQdAfFiKxAyKzNdSuLqw github.com/libp2p/go-libp2p-mplex v0.2.3/go.mod h1:CK3p2+9qH9x+7ER/gWWDYJ3QW5ZxWDkm+dVvjfuG3ek= github.com/libp2p/go-libp2p-mplex v0.3.0/go.mod h1:l9QWxRbbb5/hQMECEb908GbS9Sm2UAR2KFZKUJEynEs= github.com/libp2p/go-libp2p-mplex v0.4.0/go.mod h1:yCyWJE2sc6TBTnFpjvLuEJgTSw/u+MamvzILKdX7asw= +github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc= github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g= github.com/libp2p/go-libp2p-nat v0.0.5/go.mod h1:1qubaE5bTZMJE+E/uu2URroMbzdubFz1ChgiN79yKPE= github.com/libp2p/go-libp2p-nat v0.0.6/go.mod h1:iV59LVhB3IkFvS6S6sauVTSOrNEANnINbI/fkaLimiw= github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCThNdbQD54k3TqjpbFU= github.com/libp2p/go-libp2p-noise v0.1.1/go.mod h1:QDFLdKX7nluB7DEnlVPbz7xlLHdwHFA9HiohJRr3vwM= +github.com/libp2p/go-libp2p-noise v0.2.0 h1:wmk5nhB9a2w2RxMOyvsoKjizgJOEaJdfAakr0jN8gds= github.com/libp2p/go-libp2p-noise v0.2.0/go.mod h1:IEbYhBBzGyvdLBoxxULL/SGbJARhUeqlO8lVSREYu2Q= github.com/libp2p/go-libp2p-peer v0.2.0/go.mod h1:RCffaCvUyW2CJmG2gAWVqwePwW7JMgxjsHm7+J5kjWY= github.com/libp2p/go-libp2p-peerstore v0.1.0/go.mod h1:2CeHkQsr8svp4fZ+Oi9ykN1HBb6u0MOvdJ7YIsmcwtY= @@ -378,8 +388,10 @@ github.com/libp2p/go-libp2p-peerstore v0.2.0/go.mod h1:N2l3eVIeAitSg3Pi2ipSrJYnq github.com/libp2p/go-libp2p-peerstore v0.2.1/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRjwRLBr4TYKfNgrUkOPA= github.com/libp2p/go-libp2p-peerstore v0.2.2/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRjwRLBr4TYKfNgrUkOPA= github.com/libp2p/go-libp2p-peerstore v0.2.6/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= +github.com/libp2p/go-libp2p-peerstore v0.2.7 h1:83JoLxyR9OYTnNfB5vvFqvMUv/xDNa6NoPHnENhBsGw= github.com/libp2p/go-libp2p-peerstore v0.2.7/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= +github.com/libp2p/go-libp2p-quic-transport v0.10.0 h1:koDCbWD9CCHwcHZL3/WEvP2A+e/o5/W5L3QS/2SPMA0= github.com/libp2p/go-libp2p-quic-transport v0.10.0/go.mod h1:RfJbZ8IqXIhxBRm5hqUEJqjiiY8xmEuq3HUDS993MkA= github.com/libp2p/go-libp2p-record v0.1.2/go.mod h1:pal0eNcT5nqZaTV7UGhqeGqxFgGdsU/9W//C8dqjQDk= github.com/libp2p/go-libp2p-record v0.1.3/go.mod h1:yNUff/adKIfPnYQXgp6FQmNu3gLJ6EMg7+/vv2+9pY4= @@ -394,6 +406,7 @@ github.com/libp2p/go-libp2p-swarm v0.2.3/go.mod h1:P2VO/EpxRyDxtChXz/VPVXyTnszHv github.com/libp2p/go-libp2p-swarm v0.2.8/go.mod h1:JQKMGSth4SMqonruY0a8yjlPVIkb0mdNSwckW7OYziM= github.com/libp2p/go-libp2p-swarm v0.3.0/go.mod h1:hdv95GWCTmzkgeJpP+GK/9D9puJegb7H57B5hWQR5Kk= github.com/libp2p/go-libp2p-swarm v0.3.1/go.mod h1:hdv95GWCTmzkgeJpP+GK/9D9puJegb7H57B5hWQR5Kk= +github.com/libp2p/go-libp2p-swarm v0.5.0 h1:HIK0z3Eqoo8ugmN8YqWAhD2RORgR+3iNXYG4U2PFd1E= github.com/libp2p/go-libp2p-swarm v0.5.0/go.mod h1:sU9i6BoHE0Ve5SKz3y9WfKrh8dUat6JknzUehFx8xW4= github.com/libp2p/go-libp2p-testing v0.0.2/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= github.com/libp2p/go-libp2p-testing v0.0.3/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= @@ -402,11 +415,13 @@ github.com/libp2p/go-libp2p-testing v0.1.0/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eq github.com/libp2p/go-libp2p-testing v0.1.1/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0= github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod h1:Qy8sAncLKpwXtS2dSnDOP8ktexIAHKu+J+pnZOFZLTc= github.com/libp2p/go-libp2p-testing v0.3.0/go.mod h1:efZkql4UZ7OVsEfaxNHZPzIehtsBXMrXnCfJIgDti5g= +github.com/libp2p/go-libp2p-testing v0.4.0 h1:PrwHRi0IGqOwVQWR3xzgigSlhlLfxgfXgkHxr77EghQ= github.com/libp2p/go-libp2p-testing v0.4.0/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0= github.com/libp2p/go-libp2p-tls v0.1.3/go.mod h1:wZfuewxOndz5RTnCAxFliGjvYSDA40sKitV4c50uI1M= github.com/libp2p/go-libp2p-transport-upgrader v0.1.1/go.mod h1:IEtA6or8JUbsV07qPW4r01GnTenLW4oi3lOPbUMGJJA= github.com/libp2p/go-libp2p-transport-upgrader v0.2.0/go.mod h1:mQcrHj4asu6ArfSoMuyojOdjx73Q47cYD7s5+gZOlns= github.com/libp2p/go-libp2p-transport-upgrader v0.3.0/go.mod h1:i+SKzbRnvXdVbU3D1dwydnTmKRPXiAR/fyvi1dXuL4o= +github.com/libp2p/go-libp2p-transport-upgrader v0.4.2 h1:4JsnbfJzgZeRS9AWN7B9dPqn/LY/HoQTlO9gtdJTIYM= github.com/libp2p/go-libp2p-transport-upgrader v0.4.2/go.mod h1:NR8ne1VwfreD5VIWIU62Agt/J18ekORFU/j1i2y8zvk= github.com/libp2p/go-libp2p-yamux v0.2.0/go.mod h1:Db2gU+XfLpm6E4rG5uGCFX6uXA8MEXOxFcRoXUODaK8= github.com/libp2p/go-libp2p-yamux v0.2.2/go.mod h1:lIohaR0pT6mOt0AZ0L2dFze9hds9Req3OfS+B+dv4qw= @@ -415,15 +430,18 @@ github.com/libp2p/go-libp2p-yamux v0.2.7/go.mod h1:X28ENrBMU/nm4I3Nx4sZ4dgjZ6VhL github.com/libp2p/go-libp2p-yamux v0.2.8/go.mod h1:/t6tDqeuZf0INZMTgd0WxIRbtK2EzI2h7HbFm9eAKI4= github.com/libp2p/go-libp2p-yamux v0.4.0/go.mod h1:+DWDjtFMzoAwYLVkNZftoucn7PelNoy5nm3tZ3/Zw30= github.com/libp2p/go-libp2p-yamux v0.5.0/go.mod h1:AyR8k5EzyM2QN9Bbdg6X1SkVVuqLwTGf0L4DFq9g6po= +github.com/libp2p/go-libp2p-yamux v0.5.4 h1:/UOPtT/6DHPtr3TtKXBHa6g0Le0szYuI33Xc/Xpd7fQ= github.com/libp2p/go-libp2p-yamux v0.5.4/go.mod h1:tfrXbyaTqqSU654GTvK3ocnSZL3BuHoeTSqhcel1wsE= github.com/libp2p/go-maddr-filter v0.0.4/go.mod h1:6eT12kSQMA9x2pvFQa+xesMKUBlj9VImZbj3B9FBH/Q= github.com/libp2p/go-maddr-filter v0.0.5/go.mod h1:Jk+36PMfIqCJhAnaASRH83bdAvfDRp/w6ENFaC9bG+M= +github.com/libp2p/go-maddr-filter v0.1.0 h1:4ACqZKw8AqiuJfwFGq1CYDFugfXTOos+qQ3DETkhtCE= github.com/libp2p/go-maddr-filter v0.1.0/go.mod h1:VzZhTXkMucEGGEOSKddrwGiOv0tUhgnKqNEmIAz/bPU= github.com/libp2p/go-mplex v0.0.3/go.mod h1:pK5yMLmOoBR1pNCqDlA2GQrdAVTMkqFalaTWe7l4Yd0= github.com/libp2p/go-mplex v0.1.0/go.mod h1:SXgmdki2kwCUlCCbfGLEgHjC4pFqhTp0ZoV6aiKgxDU= github.com/libp2p/go-mplex v0.1.1/go.mod h1:Xgz2RDCi3co0LeZfgjm4OgUF15+sVR8SRcu3SFXI1lk= github.com/libp2p/go-mplex v0.1.2/go.mod h1:Xgz2RDCi3co0LeZfgjm4OgUF15+sVR8SRcu3SFXI1lk= github.com/libp2p/go-mplex v0.2.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= +github.com/libp2p/go-mplex v0.3.0 h1:U1T+vmCYJaEoDJPV1aq31N56hS+lJgb397GsylNSgrU= github.com/libp2p/go-mplex v0.3.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= github.com/libp2p/go-msgio v0.0.2/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= @@ -432,6 +450,7 @@ github.com/libp2p/go-nat v0.0.4/go.mod h1:Nmw50VAvKuk38jUBcmNh6p9lUJLoODbJRvYAa/ github.com/libp2p/go-nat v0.0.5/go.mod h1:B7NxsVNPZmRLvMOwiEO1scOSyjA56zxYAGv1yQgRkEU= github.com/libp2p/go-netroute v0.1.2/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= github.com/libp2p/go-netroute v0.1.3/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= +github.com/libp2p/go-netroute v0.1.6 h1:ruPJStbYyXVYGQ81uzEDzuvbYRLKRrLvTYd33yomC38= github.com/libp2p/go-netroute v0.1.6/go.mod h1:AqhkMh0VuWmfgtxKPp3Oc1LdU5QSWS7wl0QLhSZqXxQ= github.com/libp2p/go-openssl v0.0.2/go.mod h1:v8Zw2ijCSWBQi8Pq5GAixw6DbFfa9u6VIYDXnvOXkc0= github.com/libp2p/go-openssl v0.0.3/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= @@ -444,6 +463,7 @@ github.com/libp2p/go-reuseport-transport v0.0.2/go.mod h1:YkbSDrvjUVDL6b8XqriyA2 github.com/libp2p/go-reuseport-transport v0.0.3/go.mod h1:Spv+MPft1exxARzP2Sruj2Wb5JSyHNncjf1Oi2dEbzM= github.com/libp2p/go-reuseport-transport v0.0.4/go.mod h1:trPa7r/7TJK/d+0hdBLOCGvpQQVOU74OXbNCIMkufGw= github.com/libp2p/go-sockaddr v0.0.2/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= +github.com/libp2p/go-sockaddr v0.1.1 h1:yD80l2ZOdGksnOyHrhxDdTDFrf7Oy+v3FMVArIRgZxQ= github.com/libp2p/go-sockaddr v0.1.1/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= github.com/libp2p/go-stream-muxer v0.0.1/go.mod h1:bAo8x7YkSpadMTbtTaxGVHWUQsR/l5MEaHbKaliuT14= github.com/libp2p/go-stream-muxer-multistream v0.2.0/go.mod h1:j9eyPol/LLRqT+GPLSxvimPhNph4sfYfMoDPd7HkzIc= @@ -455,6 +475,7 @@ github.com/libp2p/go-tcp-transport v0.2.1/go.mod h1:zskiJ70MEfWz2MKxvFB/Pv+tPIB1 github.com/libp2p/go-ws-transport v0.2.0/go.mod h1:9BHJz/4Q5A9ludYWKoGCFC5gUElzlHoKzu0yY9p/klM= github.com/libp2p/go-ws-transport v0.3.0/go.mod h1:bpgTJmRZAvVHrgHybCVyqoBmyLQ1fiZuEaBYusP5zsk= github.com/libp2p/go-ws-transport v0.3.1/go.mod h1:bpgTJmRZAvVHrgHybCVyqoBmyLQ1fiZuEaBYusP5zsk= +github.com/libp2p/go-ws-transport v0.4.0 h1:9tvtQ9xbws6cA5LvqdE6Ne3vcmGB4f1z9SByggk4s0k= github.com/libp2p/go-ws-transport v0.4.0/go.mod h1:EcIEKqf/7GDjth6ksuS/6p7R49V4CBY6/E7R/iyhYUA= github.com/libp2p/go-yamux v1.2.2/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/libp2p/go-yamux v1.3.0/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= @@ -462,15 +483,20 @@ github.com/libp2p/go-yamux v1.3.3/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZ github.com/libp2p/go-yamux v1.3.5/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow= github.com/libp2p/go-yamux v1.3.7/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux v1.4.0/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= +github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI= github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= +github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= +github.com/lucas-clemente/quic-go v0.19.3 h1:eCDQqvGBB+kCTkA0XrAFtNe81FMa0/fn4QSoeAbmiF4= github.com/lucas-clemente/quic-go v0.19.3/go.mod h1:ADXpNbTQjq1hIzCpB+y/k5iz4n4z4IwqoLb94Kh5Hu8= github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/marten-seemann/qpack v0.2.1/go.mod h1:F7Gl5L1jIgN1D11ucXefiuJS9UMVP2opoCp2jDKb7wc= +github.com/marten-seemann/qtls v0.10.0 h1:ECsuYUKalRL240rRD4Ri33ISb7kAQ3qGDlrrl55b2pc= github.com/marten-seemann/qtls v0.10.0/go.mod h1:UvMd1oaYDACI99/oZUYLzMCkBXQVT0aGm99sJhbT8hs= +github.com/marten-seemann/qtls-go1-15 v0.1.1 h1:LIH6K34bPVttyXnUWixk0bzH6/N07VxbSabxn5A5gZQ= github.com/marten-seemann/qtls-go1-15 v0.1.1/go.mod h1:GyFwywLKkRt+6mfU99csTEY1joMZz5vmB1WNZH3P81I= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= @@ -486,6 +512,7 @@ github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00v github.com/miekg/dns v1.1.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.28/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= +github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY= github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= @@ -518,6 +545,7 @@ github.com/multiformats/go-multiaddr v0.3.1/go.mod h1:uPbspcUPd5AfaP6ql3ujFY+QWz github.com/multiformats/go-multiaddr-dns v0.0.1/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.0.2/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.2.0/go.mod h1:TJ5pr5bBO7Y1B18djPuRsVkduhQH2YqYSbxWJzYGdK0= +github.com/multiformats/go-multiaddr-dns v0.3.1 h1:QgQgR+LQVt3NPTjbrLLpsaT2ufAA2y0Mkk+QRVJbW3A= github.com/multiformats/go-multiaddr-dns v0.3.1/go.mod h1:G/245BRQ6FJGmryJCrOuTdB37AMA5AMOVuO6NY3JwTk= github.com/multiformats/go-multiaddr-fmt v0.0.1/go.mod h1:aBYjqL4T/7j4Qx+R73XSv/8JsgnRFlf0w2KGLCmXl3Q= github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo= @@ -542,6 +570,7 @@ github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wS github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= github.com/multiformats/go-multistream v0.2.0/go.mod h1:5GZPQZbkWOLOn3J2y4Y99vVW7vOfsAflxARk3x14o6k= github.com/multiformats/go-multistream v0.2.1/go.mod h1:5GZPQZbkWOLOn3J2y4Y99vVW7vOfsAflxARk3x14o6k= +github.com/multiformats/go-multistream v0.2.2 h1:TCYu1BHTDr1F/Qm75qwYISQdzGcRdC21nFgQW7l7GBo= github.com/multiformats/go-multistream v0.2.2/go.mod h1:UIcnm7Zuo8HKG+HkWgfQsGL+/MIEhyTqbODbIUwSXKs= github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= @@ -638,6 +667,7 @@ github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpP github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= +github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI= github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= @@ -662,13 +692,16 @@ go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M= go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= +go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= @@ -708,6 +741,7 @@ golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTk golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k= golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= @@ -751,6 +785,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180810173357-98c5dad5d1a0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -788,12 +823,14 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 h1:JWgyZ1qgdTaF3N3oxC+MdTV7qvEEgHo3otj+HB5CM7Q= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -816,6 +853,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a h1:CB3a9Nez8M13wwlr/E2YtwoU+qYHKfC+JrDa45RXXoQ= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -849,6 +887,7 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.28.1/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= @@ -865,6 +904,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 7ff981c934..f95f02daa1 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -228,7 +228,6 @@ func (err Module) Index() uint { } func (err Module) String() string { - // Make sure this is okay as opposed to being a string. Should be fine since its wrapped in the error return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } @@ -238,7 +237,7 @@ func (err CustomModuleError) String() string { func determineErr(res []byte) error { switch res[0] { - case 0: // DispatchOutcome + case 0: switch res[1] { case 0: return nil @@ -247,7 +246,7 @@ func determineErr(res []byte) error { default: return errInvalidResult } - case 1: // TransactionValidityError + case 1: switch res[1] { case 0: return determineInvalidTxnErr(res[2:]) diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 8ec2923c6d..c495cde251 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -32,6 +32,16 @@ func TestApplyExtrinsicErrors(t *testing.T) { test: []byte{0, 1, 0, 0x04, 65}, expected: "dispatch outcome error: unknown error: A", }, + { + name: "Dispatch failed lookup", + test: []byte{0, 1, 1}, + expected: "dispatch outcome error: failed lookup", + }, + { + name: "Dispatch bad origin", + test: []byte{0, 1, 2}, + expected: "dispatch outcome error: bad origin", + }, { name: "Invalid txn payment error", test: []byte{1, 0, 1}, From 3d4463becf9fb5dad5a459d10d99dce903249be4 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 13:28:00 -0600 Subject: [PATCH 227/245] refactor unknown transaction errors --- lib/babe/errors.go | 182 +++++++++++++++++++++++---------------------- 1 file changed, 92 insertions(+), 90 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index f95f02daa1..207f7b2ccd 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -16,7 +16,6 @@ package babe import ( "errors" "fmt" - "github.com/ChainSafe/gossamer/lib/common/optional" "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -67,8 +66,6 @@ type DispatchOutcomeError struct { msg string // description of error } -//type DispatchOutcomeError string - func (e DispatchOutcomeError) Error() string { return fmt.Sprintf("dispatch outcome error: %s", e.msg) } @@ -82,62 +79,84 @@ func (e TransactionValidityError) Error() string { return fmt.Sprintf("transaction validity error: %s", e.msg) } -func determineCustomModuleErr(res []byte) error { - if len(res) < 3 { - return errInvalidResult - } - errMsg, err := optional.NewBytes(false, nil).DecodeBytes(res[2:]) - if err != nil { - return err - } - return fmt.Errorf("index: %d code: %d message: %s", res[0], res[1], errMsg.String()) +// Dispatch Errors +type Other string + +type CannotLookup struct { + err string } -//func determineDispatchErr(res []byte) error { -// var v []byte -// err := scale.Unmarshal(res[1:], &v) -// if err != nil { -// // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure -// // TODO Create stucts for errors and integrate into Varying data type -// return errInvalidResult -// } -// -// switch res[0] { -// case 0: -// var v []byte -// err := scale.Unmarshal(res[1:], &v) -// if err != nil { -// // this err is thrown if we can't unmarshal, so prolly `errInvalidResult`. Check to make sure -// // TODO Create stucts for errors and integrate into Varying data type -// return errInvalidResult -// } -// return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", v)} -// case 1: -// return &DispatchOutcomeError{"failed lookup"} -// case 2: -// return &DispatchOutcomeError{"bad origin"} -// case 3: -// return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", determineCustomModuleErr(res[1:]))} -// } -// -// return errInvalidResult -//} +type BadOrigin struct { + err string +} + +type Module struct { // add in `scale:"1"` after + Idx uint8 + Err uint8 + Message *string +} + +// Dispatch Receivers +func (err Other) Index() uint { + return 0 +} + +func (err CannotLookup) Index() uint { + return 1 +} + +func (err BadOrigin) Index() uint { + return 2 +} + +func (err Module) Index() uint { + return 3 +} + +func (err Module) String() string { + return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) +} + +// Unknown Transaction Errors +type ValidityCannotLookup struct { + err string +} + +type NoUnsignedValidator struct { + err string +} + +type Custom uint8 + +// Unknown Transaction Receivers +func (err ValidityCannotLookup) Index() uint { + return 0 +} + +func (err NoUnsignedValidator) Index() uint { + return 1 +} + +func (err Custom) Index() uint { + return 2 +} + +// Invalid Transaction Errors + +// Invalid Transaction Receivers /* TODO: - 1) Add test cases for 2 middle cases - 2) Expand on this to include other error types - 3) Rebase - 4) Clean up code - 5) Make sure everything I do is okay (errors returned and printing as hex instead of string). This could be included in a pr - 6) PR??? + 1) Expand on this to include other error types + 2) Clean up code + 3) Make sure everything I do is okay (errors returned and printing as hex instead of string). This could be included in a pr + 4) PR??? */ func determineDispatchErr(res []byte) error { // This works yay! var e Other vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) if err != nil { - // Unmarshalling error. Check to make sure this is the correct return type for this case return errInvalidResult } @@ -145,10 +164,8 @@ func determineDispatchErr(res []byte) error { // This works yay! case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case CannotLookup: - // Add testing for this case, make sure struct is correct return &DispatchOutcomeError{"failed lookup"} case BadOrigin: - // Add testing for this case, make sure struct is correct return &DispatchOutcomeError{"bad origin"} case Module: return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} @@ -183,53 +200,38 @@ func determineInvalidTxnErr(res []byte) error { return errInvalidResult } +//func determineUnknownTxnErr(res []byte) error { +// switch res[0] { +// case 0: +// return &TransactionValidityError{"lookup failed"} +// case 1: +// return &TransactionValidityError{"validator not found"} +// case 2: +// return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} +// } +// return errInvalidResult +//} + func determineUnknownTxnErr(res []byte) error { - switch res[0] { - case 0: + var c Custom + vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) + err := scale.Unmarshal(res, &vdt) + if err != nil { + return errInvalidResult + } + + switch val := vdt.Value().(type) { + case ValidityCannotLookup: return &TransactionValidityError{"lookup failed"} - case 1: + case NoUnsignedValidator: return &TransactionValidityError{"validator not found"} - case 2: - return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} + case Custom: + return &TransactionValidityError{fmt.Sprintf("unknown error: %d", val)} } - return errInvalidResult -} - -type Other string - -func (err Other) Index() uint { - return 0 -} - -type CannotLookup struct { - err string -} - -func (err CannotLookup) Index() uint { - return 1 -} - -type BadOrigin struct { - err string -} - -func (err BadOrigin) Index() uint { - return 2 -} -type Module struct { // add in `scale:"1"` after - Idx uint8 - Err uint8 - Message *string -} - -func (err Module) Index() uint { - return 3 + return errInvalidResult } -func (err Module) String() string { - return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) -} func (err CustomModuleError) String() string { return fmt.Sprintf("index: %d code: %d message: %p", err.index, err.err, err.message) From 25bd7e914c3289d50bfbac68d7c47afe5c1dacae Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 13:45:38 -0600 Subject: [PATCH 228/245] refactor error handling --- lib/babe/errors.go | 171 +++++++++++++++++++++++++++++++++------------ 1 file changed, 125 insertions(+), 46 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 207f7b2ccd..3141584e31 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -97,53 +97,109 @@ type Module struct { // add in `scale:"1"` after } // Dispatch Receivers -func (err Other) Index() uint { - return 0 +func (err Other) Index() uint {return 0} +func (err CannotLookup) Index() uint {return 1} +func (err BadOrigin) Index() uint {return 2} +func (err Module) Index() uint {return 3} +func (err Module) String() string { + return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } -func (err CannotLookup) Index() uint { - return 1 +// Unknown Transaction Errors +type ValidityCannotLookup struct { + err string } -func (err BadOrigin) Index() uint { - return 2 +type NoUnsignedValidator struct { + err string } -func (err Module) Index() uint { - return 3 +type UnknownCustom uint8 + +// Unknown Transaction Receivers +func (err ValidityCannotLookup) Index() uint {return 0} +func (err NoUnsignedValidator) Index() uint {return 1} +func (err UnknownCustom) Index() uint {return 2} + +// Invalid Transaction Errors +type Call struct { + err string } -func (err Module) String() string { - return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) +type Payment struct { + err string } -// Unknown Transaction Errors -type ValidityCannotLookup struct { +type Future struct { err string } -type NoUnsignedValidator struct { +type Stale struct { err string } -type Custom uint8 +type BadProof struct { + err string +} -// Unknown Transaction Receivers -func (err ValidityCannotLookup) Index() uint { +type AncientBirthBlock struct { + err string +} + +type ExhaustsResources struct { + err string +} + +type InvalidCustom uint8 + +type BadMandatory struct { + err string +} + +type MandatoryDispatch struct { + err string +} + +// Invalid Transaction Receivers +func (err Call) Index() uint { return 0 } -func (err NoUnsignedValidator) Index() uint { +func (err Payment) Index() uint { return 1 } -func (err Custom) Index() uint { +func (err Future) Index() uint { return 2 } -// Invalid Transaction Errors +func (err Stale) Index() uint { + return 3 +} -// Invalid Transaction Receivers +func (err BadProof) Index() uint { + return 4 +} + +func (err AncientBirthBlock) Index() uint { + return 5 +} + +func (err ExhaustsResources) Index() uint { + return 6 +} + +func (err InvalidCustom) Index() uint { + return 7 +} + +func (err BadMandatory) Index() uint { + return 8 +} + +func (err MandatoryDispatch) Index() uint { + return 9 +} /* TODO: @@ -174,46 +230,69 @@ func determineDispatchErr(res []byte) error { // This works yay! return errInvalidResult } +//func determineInvalidTxnErr(res []byte) error { +// switch res[0] { +// case 0: +// return &TransactionValidityError{"call of the transaction is not expected"} +// case 1: +// return &TransactionValidityError{"invalid payment"} +// case 2: +// return &TransactionValidityError{"invalid transaction"} +// case 3: +// return &TransactionValidityError{"outdated transaction"} +// case 4: +// return &TransactionValidityError{"bad proof"} +// case 5: +// return &TransactionValidityError{"ancient birth block"} +// case 6: +// return &TransactionValidityError{"exhausts resources"} +// case 7: +// return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} +// case 8: +// return &TransactionValidityError{"mandatory dispatch error"} +// case 9: +// return &TransactionValidityError{"invalid mandatory dispatch"} +// } +// return errInvalidResult +//} + func determineInvalidTxnErr(res []byte) error { - switch res[0] { - case 0: + var c InvalidCustom + vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, + ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) + err := scale.Unmarshal(res, &vdt) + if err != nil { + return errInvalidResult + } + + switch val := vdt.Value().(type) { + case Call: return &TransactionValidityError{"call of the transaction is not expected"} - case 1: + case Payment: return &TransactionValidityError{"invalid payment"} - case 2: + case Future: return &TransactionValidityError{"invalid transaction"} - case 3: + case Stale: return &TransactionValidityError{"outdated transaction"} - case 4: + case BadProof: return &TransactionValidityError{"bad proof"} - case 5: + case AncientBirthBlock: return &TransactionValidityError{"ancient birth block"} - case 6: + case ExhaustsResources: return &TransactionValidityError{"exhausts resources"} - case 7: - return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} - case 8: + case InvalidCustom: + return &TransactionValidityError{fmt.Sprintf("unknown error: %d", val)} + case BadMandatory: return &TransactionValidityError{"mandatory dispatch error"} - case 9: + case MandatoryDispatch: return &TransactionValidityError{"invalid mandatory dispatch"} } + return errInvalidResult } -//func determineUnknownTxnErr(res []byte) error { -// switch res[0] { -// case 0: -// return &TransactionValidityError{"lookup failed"} -// case 1: -// return &TransactionValidityError{"validator not found"} -// case 2: -// return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} -// } -// return errInvalidResult -//} - func determineUnknownTxnErr(res []byte) error { - var c Custom + var c UnknownCustom vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) err := scale.Unmarshal(res, &vdt) if err != nil { @@ -225,7 +304,7 @@ func determineUnknownTxnErr(res []byte) error { return &TransactionValidityError{"lookup failed"} case NoUnsignedValidator: return &TransactionValidityError{"validator not found"} - case Custom: + case UnknownCustom: return &TransactionValidityError{fmt.Sprintf("unknown error: %d", val)} } From 0b9baf7eeb38d337ab38f86d02b3c80be47e6f9c Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 13:52:12 -0600 Subject: [PATCH 229/245] inital integration of scale pkg into babe --- lib/babe/errors.go | 82 ++++++---------------------------------------- 1 file changed, 10 insertions(+), 72 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 3141584e31..343868bf3f 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -161,53 +161,17 @@ type MandatoryDispatch struct { } // Invalid Transaction Receivers -func (err Call) Index() uint { - return 0 -} - -func (err Payment) Index() uint { - return 1 -} - -func (err Future) Index() uint { - return 2 -} - -func (err Stale) Index() uint { - return 3 -} - -func (err BadProof) Index() uint { - return 4 -} - -func (err AncientBirthBlock) Index() uint { - return 5 -} - -func (err ExhaustsResources) Index() uint { - return 6 -} - -func (err InvalidCustom) Index() uint { - return 7 -} +func (err Call) Index() uint {return 0} +func (err Payment) Index() uint {return 1} +func (err Future) Index() uint {return 2} +func (err Stale) Index() uint {return 3} +func (err BadProof) Index() uint {return 4} +func (err AncientBirthBlock) Index() uint {return 5} +func (err ExhaustsResources) Index() uint {return 6} +func (err InvalidCustom) Index() uint {return 7} +func (err BadMandatory) Index() uint {return 8} +func (err MandatoryDispatch) Index() uint {return 9} -func (err BadMandatory) Index() uint { - return 8 -} - -func (err MandatoryDispatch) Index() uint { - return 9 -} - -/* - TODO: - 1) Expand on this to include other error types - 2) Clean up code - 3) Make sure everything I do is okay (errors returned and printing as hex instead of string). This could be included in a pr - 4) PR??? - */ func determineDispatchErr(res []byte) error { // This works yay! var e Other vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) @@ -230,32 +194,6 @@ func determineDispatchErr(res []byte) error { // This works yay! return errInvalidResult } -//func determineInvalidTxnErr(res []byte) error { -// switch res[0] { -// case 0: -// return &TransactionValidityError{"call of the transaction is not expected"} -// case 1: -// return &TransactionValidityError{"invalid payment"} -// case 2: -// return &TransactionValidityError{"invalid transaction"} -// case 3: -// return &TransactionValidityError{"outdated transaction"} -// case 4: -// return &TransactionValidityError{"bad proof"} -// case 5: -// return &TransactionValidityError{"ancient birth block"} -// case 6: -// return &TransactionValidityError{"exhausts resources"} -// case 7: -// return &TransactionValidityError{fmt.Sprintf("unknown error: %d", res[1])} -// case 8: -// return &TransactionValidityError{"mandatory dispatch error"} -// case 9: -// return &TransactionValidityError{"invalid mandatory dispatch"} -// } -// return errInvalidResult -//} - func determineInvalidTxnErr(res []byte) error { var c InvalidCustom vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, From 187f2555a9525f60c9dc51b0ee6b1a919daa420c Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Fri, 25 Jun 2021 13:56:08 -0600 Subject: [PATCH 230/245] remove changes in scale pkg --- pkg/scale/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 83914df404..e551c6a9d1 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -748,4 +748,4 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { } dstv.Set(reflect.ValueOf(ui128)) return -} \ No newline at end of file +} From 6968e587e1a2314ca0aeea727d4ad05418e2e93f Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 28 Jun 2021 08:20:49 -0600 Subject: [PATCH 231/245] fix leftover comments --- lib/babe/build.go | 2 +- lib/babe/errors.go | 44 ++++++++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index 1972e265b0..03b6b44a57 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -348,7 +348,7 @@ func hasSlotEnded(slot Slot) bool { return time.Since(slotEnd) >= 0 } -// ExtrinsicsToBody returns scale2 encoded block body which contains inherent and extrinsic. +// ExtrinsicsToBody returns scale encoded block body which contains inherent and extrinsic. func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (*types.Body, error) { extrinsics := types.BytesArrayToExtrinsics(inherents) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 343868bf3f..e4053b4553 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -91,16 +91,16 @@ type BadOrigin struct { } type Module struct { // add in `scale:"1"` after - Idx uint8 - Err uint8 + Idx uint8 + Err uint8 Message *string } // Dispatch Receivers -func (err Other) Index() uint {return 0} -func (err CannotLookup) Index() uint {return 1} -func (err BadOrigin) Index() uint {return 2} -func (err Module) Index() uint {return 3} +func (err Other) Index() uint { return 0 } +func (err CannotLookup) Index() uint { return 1 } +func (err BadOrigin) Index() uint { return 2 } +func (err Module) Index() uint { return 3 } func (err Module) String() string { return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } @@ -117,9 +117,9 @@ type NoUnsignedValidator struct { type UnknownCustom uint8 // Unknown Transaction Receivers -func (err ValidityCannotLookup) Index() uint {return 0} -func (err NoUnsignedValidator) Index() uint {return 1} -func (err UnknownCustom) Index() uint {return 2} +func (err ValidityCannotLookup) Index() uint { return 0 } +func (err NoUnsignedValidator) Index() uint { return 1 } +func (err UnknownCustom) Index() uint { return 2 } // Invalid Transaction Errors type Call struct { @@ -161,18 +161,18 @@ type MandatoryDispatch struct { } // Invalid Transaction Receivers -func (err Call) Index() uint {return 0} -func (err Payment) Index() uint {return 1} -func (err Future) Index() uint {return 2} -func (err Stale) Index() uint {return 3} -func (err BadProof) Index() uint {return 4} -func (err AncientBirthBlock) Index() uint {return 5} -func (err ExhaustsResources) Index() uint {return 6} -func (err InvalidCustom) Index() uint {return 7} -func (err BadMandatory) Index() uint {return 8} -func (err MandatoryDispatch) Index() uint {return 9} - -func determineDispatchErr(res []byte) error { // This works yay! +func (err Call) Index() uint { return 0 } +func (err Payment) Index() uint { return 1 } +func (err Future) Index() uint { return 2 } +func (err Stale) Index() uint { return 3 } +func (err BadProof) Index() uint { return 4 } +func (err AncientBirthBlock) Index() uint { return 5 } +func (err ExhaustsResources) Index() uint { return 6 } +func (err InvalidCustom) Index() uint { return 7 } +func (err BadMandatory) Index() uint { return 8 } +func (err MandatoryDispatch) Index() uint { return 9 } + +func determineDispatchErr(res []byte) error { var e Other vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) @@ -197,7 +197,7 @@ func determineDispatchErr(res []byte) error { // This works yay! func determineInvalidTxnErr(res []byte) error { var c InvalidCustom vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, - ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) + ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) err := scale.Unmarshal(res, &vdt) if err != nil { return errInvalidResult From 52e0a5dd3027a919339f6ecd5648d29e623f5d4e Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 28 Jun 2021 09:14:16 -0600 Subject: [PATCH 232/245] reorganize receivers --- lib/babe/errors.go | 53 +++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index e4053b4553..7dcf3ba148 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -79,97 +79,106 @@ func (e TransactionValidityError) Error() string { return fmt.Sprintf("transaction validity error: %s", e.msg) } -// Dispatch Errors type Other string +func (err Other) Index() uint { return 0 } + type CannotLookup struct { err string } +func (err CannotLookup) Index() uint { return 1 } + type BadOrigin struct { err string } +func (err BadOrigin) Index() uint { return 2 } + type Module struct { // add in `scale:"1"` after Idx uint8 Err uint8 Message *string } -// Dispatch Receivers -func (err Other) Index() uint { return 0 } -func (err CannotLookup) Index() uint { return 1 } -func (err BadOrigin) Index() uint { return 2 } -func (err Module) Index() uint { return 3 } +func (err Module) Index() uint { return 3 } + func (err Module) String() string { return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } -// Unknown Transaction Errors type ValidityCannotLookup struct { err string } +func (err ValidityCannotLookup) Index() uint { return 0 } + type NoUnsignedValidator struct { err string } +func (err NoUnsignedValidator) Index() uint { return 1 } + type UnknownCustom uint8 -// Unknown Transaction Receivers -func (err ValidityCannotLookup) Index() uint { return 0 } -func (err NoUnsignedValidator) Index() uint { return 1 } -func (err UnknownCustom) Index() uint { return 2 } +func (err UnknownCustom) Index() uint { return 2 } -// Invalid Transaction Errors type Call struct { err string } +func (err Call) Index() uint { return 0 } + type Payment struct { err string } +func (err Payment) Index() uint { return 1 } + type Future struct { err string } +func (err Future) Index() uint { return 2 } + type Stale struct { err string } +func (err Stale) Index() uint { return 3 } + type BadProof struct { err string } +func (err BadProof) Index() uint { return 4 } + type AncientBirthBlock struct { err string } +func (err AncientBirthBlock) Index() uint { return 5 } + type ExhaustsResources struct { err string } +func (err ExhaustsResources) Index() uint { return 6 } + type InvalidCustom uint8 +func (err InvalidCustom) Index() uint { return 7 } + type BadMandatory struct { err string } +func (err BadMandatory) Index() uint { return 8 } + type MandatoryDispatch struct { err string } -// Invalid Transaction Receivers -func (err Call) Index() uint { return 0 } -func (err Payment) Index() uint { return 1 } -func (err Future) Index() uint { return 2 } -func (err Stale) Index() uint { return 3 } -func (err BadProof) Index() uint { return 4 } -func (err AncientBirthBlock) Index() uint { return 5 } -func (err ExhaustsResources) Index() uint { return 6 } -func (err InvalidCustom) Index() uint { return 7 } -func (err BadMandatory) Index() uint { return 8 } func (err MandatoryDispatch) Index() uint { return 9 } func determineDispatchErr(res []byte) error { From 580af87291cffee1eb535da254621c0c81e2e049 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 28 Jun 2021 11:05:10 -0600 Subject: [PATCH 233/245] formatting --- go.sum | 105 +++++++++++++++++++++++++++++++++++++++++++++ lib/babe/errors.go | 20 ++++++++- 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/go.sum b/go.sum index 1a7f5c763d..cbd5a83445 100644 --- a/go.sum +++ b/go.sum @@ -197,31 +197,42 @@ github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OI github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.1.5 h1:kxhtnfFVi+rYdOALN0B3k9UT86zVJKfBimRaciULW4I= github.com/google/uuid v1.1.5/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY= github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/rpc v1.2.0 h1:WvvdC2lNeT1SP32zrIce5l0ECBfbAlmrmSBsuc57wfk= github.com/gorilla/rpc v1.2.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw= github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= +github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is= github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= +github.com/gtank/ristretto255 v0.1.2 h1:JEqUCPA1NvLq5DwYtuzigd7ss8fwbYay9fi4/5uMzcc= github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIvY4OmlYW69o= github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU= github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI= github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs= github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao= github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc= +github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031 h1:HarGZ5h9HD9LgEg1yRVMXyfiw4wlXiLiYM2oMjeA/SE= github.com/huin/goupnp v1.0.1-0.20200620063722-49508fba0031/go.mod h1:nNs7wvRfN1eKaMknBydLNQU6146XQim8t4h+q90biWo= github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= @@ -231,6 +242,7 @@ github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUP github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj6+M= github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-cid v0.0.6/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= +github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY= github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.1.0/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= @@ -238,25 +250,33 @@ github.com/ipfs/go-datastore v0.1.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRV github.com/ipfs/go-datastore v0.4.0/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= github.com/ipfs/go-datastore v0.4.1/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= github.com/ipfs/go-datastore v0.4.4/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= +github.com/ipfs/go-datastore v0.4.5 h1:cwOUcGMLdLPWgu3SlrCckCMznaGADbPqE0r8h768/Dg= github.com/ipfs/go-datastore v0.4.5/go.mod h1:eXTcaaiN6uOlVCLS9GjJUJtlvJfM3xk23w3fyfrmmJs= +github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk= github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-ds-badger v0.0.2/go.mod h1:Y3QpeSFWQf6MopLTiZD+VT6IC1yZqaGmjvRcKeSGij8= github.com/ipfs/go-ds-badger v0.0.5/go.mod h1:g5AuuCGmr7efyzQhLL8MzwqcauPojGPUaHzfGTzuE3s= github.com/ipfs/go-ds-badger v0.0.7/go.mod h1:qt0/fWzZDoPW6jpQeqUjR5kBfhDNB65jd9YlmAvpQBk= github.com/ipfs/go-ds-badger v0.2.1/go.mod h1:Tx7l3aTph3FMFrRS838dcSJh+jjA7cX9DrGVwx/NOwE= +github.com/ipfs/go-ds-badger v0.2.3 h1:J27YvAcpuA5IvZUbeBxOcQgqnYHUPxoygc6QxxkodZ4= github.com/ipfs/go-ds-badger v0.2.3/go.mod h1:pEYw0rgg3FIrywKKnL+Snr+w/LjJZVMTBRn4FS6UHUk= +github.com/ipfs/go-ds-badger2 v0.1.0 h1:784py6lXkwlVF+K6XSuqmdMgy5l8GI6k60ngBokb9Fg= github.com/ipfs/go-ds-badger2 v0.1.0/go.mod h1:pbR1p817OZbdId9EvLOhKBgUVTM3BMCSTan78lDDVaw= github.com/ipfs/go-ds-leveldb v0.0.1/go.mod h1:feO8V3kubwsEF22n0YRQCffeb79OOYIykR4L04tMOYc= github.com/ipfs/go-ds-leveldb v0.1.0/go.mod h1:hqAW8y4bwX5LWcCtku2rFNX3vjDZCy5LZCg+cSZvYb8= github.com/ipfs/go-ds-leveldb v0.4.1/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s= +github.com/ipfs/go-ds-leveldb v0.4.2 h1:QmQoAJ9WkPMUfBLnu1sBVy0xWWlJPg0m4kRAiJL9iaw= github.com/ipfs/go-ds-leveldb v0.4.2/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s= github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw= github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc= +github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8= github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ= +github.com/ipfs/go-ipns v0.0.2 h1:oq4ErrV4hNQ2Eim257RTYRgfOSV/s8BDaf9iIl4NwFs= github.com/ipfs/go-ipns v0.0.2/go.mod h1:WChil4e0/m9cIINWLxZe1Jtf77oz5L05rO2ei/uKJ5U= github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM= github.com/ipfs/go-log v1.0.2/go.mod h1:1MNjMxe0u6xvJZgeqbJ8vdo2TKaGwZ1a0Bpza+sr2Sk= github.com/ipfs/go-log v1.0.3/go.mod h1:OsLySYkwIbiSUR/yBTdv1qPtcE4FW3WPWk/ewz9Ru+A= +github.com/ipfs/go-log v1.0.4 h1:6nLQdX4W8P9yZZFH7mO+X/PzjN8Laozm/lMJ6esdgzY= github.com/ipfs/go-log v1.0.4/go.mod h1:oDCg2FkjogeFOhqqb+N39l2RpTNPL6F/StPkB3kPgcs= github.com/ipfs/go-log/v2 v2.0.2/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= github.com/ipfs/go-log/v2 v2.0.3/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= @@ -266,19 +286,26 @@ github.com/ipfs/go-log/v2 v2.1.3 h1:1iS3IU7aXRlbgUpN8yTTpJ53NXYjAe37vcI5+5nYrzk= github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g= github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA= github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= +github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jbenet/go-cienv v0.0.0-20150120210510-1bb1476777ec/go.mod h1:rGaEvXB4uRSZMmzKNLoXvTu1sfx+1kv/DojUlPrSZGs= +github.com/jbenet/go-cienv v0.1.0 h1:Vc/s0QbQtoxX8MwwSLWWh+xNNZvM3Lw7NsTcHrvvhMc= github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA= github.com/jbenet/go-temp-err-catcher v0.0.0-20150120210811-aac704a3f4f2/go.mod h1:8GXXJV31xl8whumTzdZsTt3RnUIiPqzkyf7mxToRCMs= +github.com/jbenet/go-temp-err-catcher v0.1.0 h1:zpb3ZH6wIE8Shj2sKS+khgRvf7T7RABoLk/+KKHggpk= github.com/jbenet/go-temp-err-catcher v0.1.0/go.mod h1:0kJRvmDZXNMIiJirNPEYfhpPwbGVtZVWC34vc5WLsDk= github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY= github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= +github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o= github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= +github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 h1:EFT6MH3igZK/dIVqgGbTqWVvkZ7wJ5iGN03SVtvvdd8= github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25/go.mod h1:sWkGw/wsaHtRsT9zGQ/WyJCotGWG/Anow/9hsAcBWRw= github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= +github.com/jpillora/ipfilter v1.2.2 h1:lfENG7V1/T+ZutAtSbt6gssvzj3Ql0JmcFlqS/BES2E= github.com/jpillora/ipfilter v1.2.2/go.mod h1:xvAYjA+48eM9E5+sg9yI55N5lE9sefckjsnDvSiEA+g= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= @@ -290,6 +317,7 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d h1:68u9r4wEvL3gYg2jvAOgROwZ3H+Y3hIDk4tbbmIjcYQ= github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= @@ -297,22 +325,30 @@ github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/libp2p/go-addr-util v0.0.1/go.mod h1:4ac6O7n9rIAKB1dnd+s8IbbMXkt+oBpzX4/+RACcnlQ= +github.com/libp2p/go-addr-util v0.0.2 h1:7cWK5cdA5x72jX0g8iLrQWm5TRJZ6CzGdPEhWj7plWU= github.com/libp2p/go-addr-util v0.0.2/go.mod h1:Ecd6Fb3yIuLzq4bD7VcywcVSBtefcAwnUISBM3WG15E= github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ= +github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs= github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM= +github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38yPW7c= github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-conn-security-multistream v0.1.0/go.mod h1:aw6eD7LOsHEX7+2hJkDxw1MteijaVcI+/eP2/x3J1xc= github.com/libp2p/go-conn-security-multistream v0.2.0/go.mod h1:hZN4MjlNetKD3Rq5Jb/P5ohUnFLNzEAR4DLSzpn2QLU= github.com/libp2p/go-conn-security-multistream v0.2.1 h1:ft6/POSK7F+vl/2qzegnHDaXFU0iWB4yVTYrioC6Zy0= github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70= github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UGQf8A2eEmx4= +github.com/libp2p/go-eventbus v0.2.1 h1:VanAdErQnpTioN2TowqNcOijf6YwhuODe4pPKSDpxGc= github.com/libp2p/go-eventbus v0.2.1/go.mod h1:jc2S4SoEVPP48H9Wpzm5aiGwUCBMfGhVhhBjyhhCJs8= github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8= github.com/libp2p/go-flow-metrics v0.0.2/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= +github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM= github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= github.com/libp2p/go-libp2p v0.6.1/go.mod h1:CTFnWXogryAHjXAKEbOf1OWY+VeAP3lDMZkfEI5sT54= github.com/libp2p/go-libp2p v0.7.0/go.mod h1:hZJf8txWeCduQRDC/WSqBGMxaTHCOYHt2xSU1ivxn0k= @@ -321,6 +357,7 @@ github.com/libp2p/go-libp2p v0.8.1/go.mod h1:QRNH9pwdbEBpx5DTJYg+qxcVaDMAz3Ee/qD github.com/libp2p/go-libp2p v0.12.0/go.mod h1:FpHZrfC1q7nA8jitvdjKBDF31hguaC676g/nT9PgQM0= github.com/libp2p/go-libp2p v0.14.2 h1:qs0ABtjjNjS+RIXT1uM7sMJEvIc0pq2nKR0VQxFXhHI= github.com/libp2p/go-libp2p v0.14.2/go.mod h1:0PQMADQEjCM2l8cSMYDpTgsb8gr6Zq7i4LUgq1mlW2E= +github.com/libp2p/go-libp2p-asn-util v0.0.0-20200825225859-85005c6cf052 h1:BM7aaOF7RpmNn9+9g6uTjGJ0cTzWr5j9i9IKeun2M8U= github.com/libp2p/go-libp2p-asn-util v0.0.0-20200825225859-85005c6cf052/go.mod h1:nRMRTab+kZuk0LnKZpxhOVH/ndsdr2Nr//Zltc/vwgo= github.com/libp2p/go-libp2p-autonat v0.1.1/go.mod h1:OXqkeGOY2xJVWKAGV2inNF5aKN/djNA3fdpCWloIudE= github.com/libp2p/go-libp2p-autonat v0.2.0/go.mod h1:DX+9teU4pEEoZUqR1PiMlqliONQdNbfzE1C718tcViI= @@ -331,9 +368,11 @@ github.com/libp2p/go-libp2p-autonat v0.4.2 h1:YMp7StMi2dof+baaxkbxaizXjY1RPvU71C github.com/libp2p/go-libp2p-autonat v0.4.2/go.mod h1:YxaJlpr81FhdOv3W3BTconZPfhaYivRdf53g+S2wobk= github.com/libp2p/go-libp2p-blankhost v0.1.1/go.mod h1:pf2fvdLJPsC1FsVrNP3DUUvMzUts2dsLLBEpo1vW1ro= github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU= +github.com/libp2p/go-libp2p-blankhost v0.2.0 h1:3EsGAi0CBGcZ33GwRuXEYJLLPoVWyXJ1bcJzAJjINkk= github.com/libp2p/go-libp2p-blankhost v0.2.0/go.mod h1:eduNKXGTioTuQAUcZ5epXi9vMl+t4d8ugUBRQ4SqaNQ= github.com/libp2p/go-libp2p-circuit v0.1.4/go.mod h1:CY67BrEjKNDhdTk8UgBX1Y/H5c3xkAcs3gnksxY7osU= github.com/libp2p/go-libp2p-circuit v0.2.1/go.mod h1:BXPwYDN5A8z4OEY9sOfr2DUQMLQvKt/6oku45YUmjIo= +github.com/libp2p/go-libp2p-circuit v0.4.0 h1:eqQ3sEYkGTtybWgr6JLqJY6QLtPWRErvFjFDfAOO1wc= github.com/libp2p/go-libp2p-circuit v0.4.0/go.mod h1:t/ktoFIUzM6uLQ+o1G6NuBl2ANhBKN9Bc8jRIk31MoA= github.com/libp2p/go-libp2p-core v0.0.1/go.mod h1:g/VxnTZ/1ygHxH3dKok7Vno1VfpvGcGip57wjTU4fco= github.com/libp2p/go-libp2p-core v0.0.4/go.mod h1:jyuCQP356gzfCFtRKyvAbNkyeuxb7OlyhWZ3nls5d2I= @@ -362,8 +401,11 @@ github.com/libp2p/go-libp2p-core v0.8.5/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= github.com/libp2p/go-libp2p-discovery v0.3.0/go.mod h1:o03drFnz9BVAZdzC/QUQ+NeQOu38Fu7LJGEOK2gQltw= +github.com/libp2p/go-libp2p-discovery v0.5.0 h1:Qfl+e5+lfDgwdrXdu4YNCWyEo3fWuP+WgN9mN0iWviQ= github.com/libp2p/go-libp2p-discovery v0.5.0/go.mod h1:+srtPIU9gDaBNu//UHvcdliKBIcr4SfDcm0/PfPJLug= +github.com/libp2p/go-libp2p-kad-dht v0.11.1 h1:FsriVQhOUZpCotWIjyFSjEDNJmUzuMma/RyyTDZanwc= github.com/libp2p/go-libp2p-kad-dht v0.11.1/go.mod h1:5ojtR2acDPqh/jXf5orWy8YGb8bHQDS+qeDcoscL/PI= +github.com/libp2p/go-libp2p-kbucket v0.4.7 h1:spZAcgxifvFZHBD8tErvppbnNiKA5uokDu3CV7axu70= github.com/libp2p/go-libp2p-kbucket v0.4.7/go.mod h1:XyVo99AfQH0foSf176k4jY1xUJ2+jUJIZCSDm7r2YKk= github.com/libp2p/go-libp2p-loggables v0.1.0/go.mod h1:EyumB2Y6PrYjr55Q3/tiJ/o3xoDasoRYM7nOzEpoa90= github.com/libp2p/go-libp2p-mplex v0.2.0/go.mod h1:Ejl9IyjvXJ0T9iqUTE1jpYATQ9NM3g+OtR+EMMODbKo= @@ -375,7 +417,9 @@ github.com/libp2p/go-libp2p-mplex v0.4.0/go.mod h1:yCyWJE2sc6TBTnFpjvLuEJgTSw/u+ github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc= github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g= github.com/libp2p/go-libp2p-nat v0.0.5/go.mod h1:1qubaE5bTZMJE+E/uu2URroMbzdubFz1ChgiN79yKPE= +github.com/libp2p/go-libp2p-nat v0.0.6 h1:wMWis3kYynCbHoyKLPBEMu4YRLltbm8Mk08HGSfvTkU= github.com/libp2p/go-libp2p-nat v0.0.6/go.mod h1:iV59LVhB3IkFvS6S6sauVTSOrNEANnINbI/fkaLimiw= +github.com/libp2p/go-libp2p-netutil v0.1.0 h1:zscYDNVEcGxyUpMd0JReUZTrpMfia8PmLKcKF72EAMQ= github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCThNdbQD54k3TqjpbFU= github.com/libp2p/go-libp2p-noise v0.1.1/go.mod h1:QDFLdKX7nluB7DEnlVPbz7xlLHdwHFA9HiohJRr3vwM= github.com/libp2p/go-libp2p-noise v0.2.0 h1:wmk5nhB9a2w2RxMOyvsoKjizgJOEaJdfAakr0jN8gds= @@ -390,15 +434,19 @@ github.com/libp2p/go-libp2p-peerstore v0.2.2/go.mod h1:NQxhNjWxf1d4w6PihR8btWIRj github.com/libp2p/go-libp2p-peerstore v0.2.6/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= github.com/libp2p/go-libp2p-peerstore v0.2.7 h1:83JoLxyR9OYTnNfB5vvFqvMUv/xDNa6NoPHnENhBsGw= github.com/libp2p/go-libp2p-peerstore v0.2.7/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= +github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= github.com/libp2p/go-libp2p-quic-transport v0.10.0 h1:koDCbWD9CCHwcHZL3/WEvP2A+e/o5/W5L3QS/2SPMA0= github.com/libp2p/go-libp2p-quic-transport v0.10.0/go.mod h1:RfJbZ8IqXIhxBRm5hqUEJqjiiY8xmEuq3HUDS993MkA= github.com/libp2p/go-libp2p-record v0.1.2/go.mod h1:pal0eNcT5nqZaTV7UGhqeGqxFgGdsU/9W//C8dqjQDk= +github.com/libp2p/go-libp2p-record v0.1.3 h1:R27hoScIhQf/A8XJZ8lYpnqh9LatJ5YbHs28kCIfql0= github.com/libp2p/go-libp2p-record v0.1.3/go.mod h1:yNUff/adKIfPnYQXgp6FQmNu3gLJ6EMg7+/vv2+9pY4= +github.com/libp2p/go-libp2p-routing-helpers v0.2.3 h1:xY61alxJ6PurSi+MXbywZpelvuU4U4p/gPTxjqCqTzY= github.com/libp2p/go-libp2p-routing-helpers v0.2.3/go.mod h1:795bh+9YeoFl99rMASoiVgHdi5bjack0N1+AFAdbvBw= github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8= github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g= github.com/libp2p/go-libp2p-secio v0.2.1/go.mod h1:cWtZpILJqkqrSkiYcDBh5lA3wbT2Q+hz3rJQq3iftD8= +github.com/libp2p/go-libp2p-secio v0.2.2 h1:rLLPvShPQAcY6eNurKNZq3eZjPWfU9kXF2eI9jIYdrg= github.com/libp2p/go-libp2p-secio v0.2.2/go.mod h1:wP3bS+m5AUnFA+OFO7Er03uO1mncHG0uVwGrwvjYlNY= github.com/libp2p/go-libp2p-swarm v0.1.0/go.mod h1:wQVsCdjsuZoc730CgOvh5ox6K8evllckjebkdiY5ta4= github.com/libp2p/go-libp2p-swarm v0.2.2/go.mod h1:fvmtQ0T1nErXym1/aa1uJEyN7JzaTNyBcHImCxRpPKU= @@ -417,6 +465,7 @@ github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod github.com/libp2p/go-libp2p-testing v0.3.0/go.mod h1:efZkql4UZ7OVsEfaxNHZPzIehtsBXMrXnCfJIgDti5g= github.com/libp2p/go-libp2p-testing v0.4.0 h1:PrwHRi0IGqOwVQWR3xzgigSlhlLfxgfXgkHxr77EghQ= github.com/libp2p/go-libp2p-testing v0.4.0/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0= +github.com/libp2p/go-libp2p-tls v0.1.3 h1:twKMhMu44jQO+HgQK9X8NHO5HkeJu2QbhLzLJpa8oNM= github.com/libp2p/go-libp2p-tls v0.1.3/go.mod h1:wZfuewxOndz5RTnCAxFliGjvYSDA40sKitV4c50uI1M= github.com/libp2p/go-libp2p-transport-upgrader v0.1.1/go.mod h1:IEtA6or8JUbsV07qPW4r01GnTenLW4oi3lOPbUMGJJA= github.com/libp2p/go-libp2p-transport-upgrader v0.2.0/go.mod h1:mQcrHj4asu6ArfSoMuyojOdjx73Q47cYD7s5+gZOlns= @@ -445,8 +494,10 @@ github.com/libp2p/go-mplex v0.3.0 h1:U1T+vmCYJaEoDJPV1aq31N56hS+lJgb397GsylNSgrU github.com/libp2p/go-mplex v0.3.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= github.com/libp2p/go-msgio v0.0.2/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= +github.com/libp2p/go-msgio v0.0.6 h1:lQ7Uc0kS1wb1EfRxO2Eir/RJoHkHn7t6o+EiwsYIKJA= github.com/libp2p/go-msgio v0.0.6/go.mod h1:4ecVB6d9f4BDSL5fqvPiC4A3KivjWn+Venn/1ALLMWA= github.com/libp2p/go-nat v0.0.4/go.mod h1:Nmw50VAvKuk38jUBcmNh6p9lUJLoODbJRvYAa/+KSDo= +github.com/libp2p/go-nat v0.0.5 h1:qxnwkco8RLKqVh1NmjQ+tJ8p8khNLFxuElYG/TwqW4Q= github.com/libp2p/go-nat v0.0.5/go.mod h1:B7NxsVNPZmRLvMOwiEO1scOSyjA56zxYAGv1yQgRkEU= github.com/libp2p/go-netroute v0.1.2/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= github.com/libp2p/go-netroute v0.1.3/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= @@ -456,21 +507,26 @@ github.com/libp2p/go-openssl v0.0.2/go.mod h1:v8Zw2ijCSWBQi8Pq5GAixw6DbFfa9u6VIY github.com/libp2p/go-openssl v0.0.3/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-openssl v0.0.4/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-openssl v0.0.5/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= +github.com/libp2p/go-openssl v0.0.7 h1:eCAzdLejcNVBzP/iZM9vqHnQm+XyCEbSSIheIPRGNsw= github.com/libp2p/go-openssl v0.0.7/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= github.com/libp2p/go-reuseport v0.0.1/go.mod h1:jn6RmB1ufnQwl0Q1f+YxAj8isJgDCQzaaxIFYDhcYEA= +github.com/libp2p/go-reuseport v0.0.2 h1:XSG94b1FJfGA01BUrT82imejHQyTxO4jEWqheyCXYvU= github.com/libp2p/go-reuseport v0.0.2/go.mod h1:SPD+5RwGC7rcnzngoYC86GjPzjSywuQyMVAheVBD9nQ= github.com/libp2p/go-reuseport-transport v0.0.2/go.mod h1:YkbSDrvjUVDL6b8XqriyA20obEtsW9BLkuOUyQAOCbs= github.com/libp2p/go-reuseport-transport v0.0.3/go.mod h1:Spv+MPft1exxARzP2Sruj2Wb5JSyHNncjf1Oi2dEbzM= +github.com/libp2p/go-reuseport-transport v0.0.4 h1:OZGz0RB620QDGpv300n1zaOcKGGAoGVf8h9txtt/1uM= github.com/libp2p/go-reuseport-transport v0.0.4/go.mod h1:trPa7r/7TJK/d+0hdBLOCGvpQQVOU74OXbNCIMkufGw= github.com/libp2p/go-sockaddr v0.0.2/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= github.com/libp2p/go-sockaddr v0.1.1 h1:yD80l2ZOdGksnOyHrhxDdTDFrf7Oy+v3FMVArIRgZxQ= github.com/libp2p/go-sockaddr v0.1.1/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= github.com/libp2p/go-stream-muxer v0.0.1/go.mod h1:bAo8x7YkSpadMTbtTaxGVHWUQsR/l5MEaHbKaliuT14= github.com/libp2p/go-stream-muxer-multistream v0.2.0/go.mod h1:j9eyPol/LLRqT+GPLSxvimPhNph4sfYfMoDPd7HkzIc= +github.com/libp2p/go-stream-muxer-multistream v0.3.0 h1:TqnSHPJEIqDEO7h1wZZ0p3DXdvDSiLHQidKKUGZtiOY= github.com/libp2p/go-stream-muxer-multistream v0.3.0/go.mod h1:yDh8abSIzmZtqtOt64gFJUXEryejzNb0lisTt+fAMJA= github.com/libp2p/go-tcp-transport v0.1.0/go.mod h1:oJ8I5VXryj493DEJ7OsBieu8fcg2nHGctwtInJVpipc= github.com/libp2p/go-tcp-transport v0.1.1/go.mod h1:3HzGvLbx6etZjnFlERyakbaYPdfjg2pWP97dFZworkY= github.com/libp2p/go-tcp-transport v0.2.0/go.mod h1:vX2U0CnWimU4h0SGSEsg++AzvBcroCGYw28kh94oLe0= +github.com/libp2p/go-tcp-transport v0.2.1 h1:ExZiVQV+h+qL16fzCWtd1HSzPsqWottJ8KXwWaVi8Ns= github.com/libp2p/go-tcp-transport v0.2.1/go.mod h1:zskiJ70MEfWz2MKxvFB/Pv+tPIB1PpPUrHIWQ8aFw7M= github.com/libp2p/go-ws-transport v0.2.0/go.mod h1:9BHJz/4Q5A9ludYWKoGCFC5gUElzlHoKzu0yY9p/klM= github.com/libp2p/go-ws-transport v0.3.0/go.mod h1:bpgTJmRZAvVHrgHybCVyqoBmyLQ1fiZuEaBYusP5zsk= @@ -501,10 +557,12 @@ github.com/marten-seemann/qtls-go1-15 v0.1.1/go.mod h1:GyFwywLKkRt+6mfU99csTEY1j github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= @@ -514,12 +572,15 @@ github.com/miekg/dns v1.1.28/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7 github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY= github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= +github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 h1:hLDRPB66XQT/8+wG9WsDpiCvZf1yKO7sz7scAjSlBa0= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= +github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= +github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= @@ -529,8 +590,11 @@ github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVq github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= +github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= +github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI= github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= +github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4= github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM= github.com/multiformats/go-multiaddr v0.0.1/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= github.com/multiformats/go-multiaddr v0.0.2/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= @@ -541,6 +605,7 @@ github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y9 github.com/multiformats/go-multiaddr v0.2.1/go.mod h1:s/Apk6IyxfvMjDafnhJgJ3/46z7tZ04iMk5wP4QMGGE= github.com/multiformats/go-multiaddr v0.2.2/go.mod h1:NtfXiOtHvghW9KojvtySjH5y0u0xW5UouOmQQrn6a3Y= github.com/multiformats/go-multiaddr v0.3.0/go.mod h1:dF9kph9wfJ+3VLAaeBqo9Of8x4fJxp6ggJGteB8HQTI= +github.com/multiformats/go-multiaddr v0.3.1 h1:1bxa+W7j9wZKTZREySx1vPMs2TqrYWjVZ7zE6/XLG1I= github.com/multiformats/go-multiaddr v0.3.1/go.mod h1:uPbspcUPd5AfaP6ql3ujFY+QWzmBD8uLLL4bXW0XfGc= github.com/multiformats/go-multiaddr-dns v0.0.1/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= github.com/multiformats/go-multiaddr-dns v0.0.2/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= @@ -548,6 +613,7 @@ github.com/multiformats/go-multiaddr-dns v0.2.0/go.mod h1:TJ5pr5bBO7Y1B18djPuRsV github.com/multiformats/go-multiaddr-dns v0.3.1 h1:QgQgR+LQVt3NPTjbrLLpsaT2ufAA2y0Mkk+QRVJbW3A= github.com/multiformats/go-multiaddr-dns v0.3.1/go.mod h1:G/245BRQ6FJGmryJCrOuTdB37AMA5AMOVuO6NY3JwTk= github.com/multiformats/go-multiaddr-fmt v0.0.1/go.mod h1:aBYjqL4T/7j4Qx+R73XSv/8JsgnRFlf0w2KGLCmXl3Q= +github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E= github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo= github.com/multiformats/go-multiaddr-net v0.0.1/go.mod h1:nw6HSxNmCIQH27XPGBuX+d1tnvM7ihcFwHMSstNAVUU= github.com/multiformats/go-multiaddr-net v0.1.0/go.mod h1:5JNbcfBOP4dnhoZOv10JJVkJO0pCCEf8mTnipAo2UZQ= @@ -556,8 +622,10 @@ github.com/multiformats/go-multiaddr-net v0.1.2/go.mod h1:QsWt3XK/3hwvNxZJp92iMQ github.com/multiformats/go-multiaddr-net v0.1.3/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= github.com/multiformats/go-multiaddr-net v0.1.4/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= github.com/multiformats/go-multiaddr-net v0.1.5/go.mod h1:ilNnaM9HbmVFqsb/qcNysjCu4PVONlrBZpHIrw/qQuA= +github.com/multiformats/go-multiaddr-net v0.2.0 h1:MSXRGN0mFymt6B1yo/6BPnIRpLPEnKgQNvVfCX5VDJk= github.com/multiformats/go-multiaddr-net v0.2.0/go.mod h1:gGdH3UXny6U3cKKYCvpXI5rnK7YaOIEOPVDI9tsJbEA= github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs= +github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk= github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc= github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po= @@ -565,6 +633,7 @@ github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa github.com/multiformats/go-multihash v0.0.9/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= github.com/multiformats/go-multihash v0.0.10/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= +github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I= github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= github.com/multiformats/go-multistream v0.1.1/go.mod h1:KmHZ40hzVxiaiwlj3MEbYgK9JFk2/9UktWZAF54Du38= @@ -575,36 +644,49 @@ github.com/multiformats/go-multistream v0.2.2/go.mod h1:UIcnm7Zuo8HKG+HkWgfQsGL+ github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= +github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY= github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= +github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975 h1:zm/Rb2OsnLWCY88Njoqgo4X6yt/lx3oBNWhepX0AOMU= github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975/go.mod h1:4Mct/lWCFf1jzQTTAaWtOI7sXqmG+wBeiBfT4CxoaJk= +github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks= github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= +github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 h1:shk/vn9oCoOTmwcouEdwIeOtOGA/ELRUw/GwvxwfT+0= github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA= +github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea h1:okKoivlkNRRLqXraEtatHfEhW+D71QTwkaj+4n4M2Xc= github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea/go.mod h1:3KEU5Dm8MAYWZqity880wOFJ9PhQjyKVZGwAEfc5Q4E= +github.com/phuslu/iploc v1.0.20200807 h1:LIBm2Y9l5zmUvnJhQgMcLZ0iVwuG+5/L6AgbMwSOpE4= github.com/phuslu/iploc v1.0.20200807/go.mod h1:Q/0VX0txvbxekt4NhWIi3Q3eyZ139lHhnlzvDxyXhuc= +github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo= github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= @@ -613,6 +695,7 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7q github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= +github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -645,8 +728,10 @@ github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qt github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE= github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA= github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0= +github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU= github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= @@ -656,31 +741,41 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/src-d/envconfig v1.0.0/go.mod h1:Q9YQZ7BKITldTBnoxsE5gOeB5y66RyPXeue/R4aaNBc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= +github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca h1:Ld/zXl5t4+D69SiV4JoN7kkfvJdOWlPpfxrzxpLMoUk= github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= +github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc= github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI= github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= +github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= +github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec h1:VElCeVyfCWNmCv6UisKQrr+P2/JRG0uf4/FIdCB4pL0= github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec/go.mod h1:VGyarTzasuS7k5KhSIGpM3tciSZlkP31Mp9VJTHMMeI= +github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= github.com/whyrusleeping/go-logging v0.0.1/go.mod h1:lDPYj54zutzG1XYfHAhcc7oNXEburHQBn+Iqd4yS4vE= github.com/whyrusleeping/mafmt v1.2.8/go.mod h1:faQJFPbLSxzD9xpA02ttW/tS9vZykNvXwGvqIpk20FA= +github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 h1:Y1/FEOpaCpD21WxrmfeIYCFPuVPRCY2XZTWzTNHGw30= github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4= +github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI= github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= @@ -698,6 +793,7 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.0.0 h1:qsup4IcBdlmsnGfqyLl4Ntn3C2XCCuKAE7DwHpScyUo= go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= @@ -707,6 +803,7 @@ go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9E go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.14.1/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= +go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM= go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE= golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw= @@ -746,6 +843,7 @@ golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPI golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -858,6 +956,7 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= @@ -867,6 +966,7 @@ google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -909,20 +1009,25 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU= gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c= gopkg.in/src-d/go-cli.v0 v0.0.0-20181105080154-d492247bbc0d/go.mod h1:z+K8VcOYVYcSwSjGebuDL6176A1XskgbtNl64NSg+n8= gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 7dcf3ba148..f4ae860d8b 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -16,6 +16,7 @@ package babe import ( "errors" "fmt" + "github.com/ChainSafe/gossamer/pkg/scale" ) @@ -79,23 +80,27 @@ func (e TransactionValidityError) Error() string { return fmt.Sprintf("transaction validity error: %s", e.msg) } +// Other Some error occurred type Other string func (err Other) Index() uint { return 0 } +// CannotLookup Failed to lookup some data type CannotLookup struct { err string } func (err CannotLookup) Index() uint { return 1 } +// BadOrigin A bad origin type BadOrigin struct { err string } func (err BadOrigin) Index() uint { return 2 } -type Module struct { // add in `scale:"1"` after +// Module A custom error in a module +type Module struct { Idx uint8 Err uint8 Message *string @@ -107,74 +112,87 @@ func (err Module) String() string { return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } +// ValidityCannotLookup Could not lookup some information that is required to validate the transaction type ValidityCannotLookup struct { err string } func (err ValidityCannotLookup) Index() uint { return 0 } +// NoUnsignedValidator No validator found for the given unsigned transaction type NoUnsignedValidator struct { err string } func (err NoUnsignedValidator) Index() uint { return 1 } +// UnknownCustom Any other custom unknown validity that is not covered type UnknownCustom uint8 func (err UnknownCustom) Index() uint { return 2 } +// Call The call of the transaction is not expected type Call struct { err string } func (err Call) Index() uint { return 0 } +// Payment General error to do with the inability to pay some fees (e.g. account balance too low) type Payment struct { err string } func (err Payment) Index() uint { return 1 } +// Future General error to do with the transaction not yet being valid (e.g. nonce too high) type Future struct { err string } func (err Future) Index() uint { return 2 } +// Stale General error to do with the transaction being outdated (e.g. nonce too low) type Stale struct { err string } func (err Stale) Index() uint { return 3 } +// BadProof General error to do with the transaction’s proofs (e.g. signature) type BadProof struct { err string } func (err BadProof) Index() uint { return 4 } +// AncientBirthBlock The transaction birth block is ancient type AncientBirthBlock struct { err string } func (err AncientBirthBlock) Index() uint { return 5 } +// ExhaustsResources The transaction would exhaust the resources of current block type ExhaustsResources struct { err string } func (err ExhaustsResources) Index() uint { return 6 } +// InvalidCustom Any other custom invalid validity that is not covered type InvalidCustom uint8 func (err InvalidCustom) Index() uint { return 7 } +// BadMandatory An extrinsic with a Mandatory dispatch resulted in Error type BadMandatory struct { err string } func (err BadMandatory) Index() uint { return 8 } +// MandatoryDispatch A transaction with a mandatory dispatch type MandatoryDispatch struct { err string } From b9b113357ba5af89ce644ea5a52664056d752dc7 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 28 Jun 2021 11:49:08 -0600 Subject: [PATCH 234/245] lint and tidy --- lib/babe/errors.go | 88 +++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index f4ae860d8b..a1d480d4fd 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -80,23 +80,31 @@ func (e TransactionValidityError) Error() string { return fmt.Sprintf("transaction validity error: %s", e.msg) } +// A UnmarshallingError is when unmarshalling fails +type UnmarshallingError struct { + msg string +} + +func (e UnmarshallingError) Error() string { + return fmt.Sprintf("unmarshalling error: %s", e.msg) +} + // Other Some error occurred type Other string +// Index Returns VDT index func (err Other) Index() uint { return 0 } // CannotLookup Failed to lookup some data -type CannotLookup struct { - err string -} +type CannotLookup struct{} +// Index Returns VDT index func (err CannotLookup) Index() uint { return 1 } // BadOrigin A bad origin -type BadOrigin struct { - err string -} +type BadOrigin struct{} +// Index Returns VDT index func (err BadOrigin) Index() uint { return 2 } // Module A custom error in a module @@ -106,97 +114,89 @@ type Module struct { Message *string } +// Index Returns VDT index func (err Module) Index() uint { return 3 } -func (err Module) String() string { +func (err Module) string() string { return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message) } // ValidityCannotLookup Could not lookup some information that is required to validate the transaction -type ValidityCannotLookup struct { - err string -} +type ValidityCannotLookup struct{} +// Index Returns VDT index func (err ValidityCannotLookup) Index() uint { return 0 } // NoUnsignedValidator No validator found for the given unsigned transaction -type NoUnsignedValidator struct { - err string -} +type NoUnsignedValidator struct{} +// Index Returns VDT index func (err NoUnsignedValidator) Index() uint { return 1 } // UnknownCustom Any other custom unknown validity that is not covered type UnknownCustom uint8 +// Index Returns VDT index func (err UnknownCustom) Index() uint { return 2 } // Call The call of the transaction is not expected -type Call struct { - err string -} +type Call struct{} +// Index Returns VDT index func (err Call) Index() uint { return 0 } // Payment General error to do with the inability to pay some fees (e.g. account balance too low) -type Payment struct { - err string -} +type Payment struct{} +// Index Returns VDT index func (err Payment) Index() uint { return 1 } // Future General error to do with the transaction not yet being valid (e.g. nonce too high) -type Future struct { - err string -} +type Future struct{} +// Index Returns VDT index func (err Future) Index() uint { return 2 } // Stale General error to do with the transaction being outdated (e.g. nonce too low) -type Stale struct { - err string -} +type Stale struct{} +// Index Returns VDT index func (err Stale) Index() uint { return 3 } // BadProof General error to do with the transaction’s proofs (e.g. signature) -type BadProof struct { - err string -} +type BadProof struct{} +// Index Returns VDT index func (err BadProof) Index() uint { return 4 } // AncientBirthBlock The transaction birth block is ancient -type AncientBirthBlock struct { - err string -} +type AncientBirthBlock struct{} +// Index Returns VDT index func (err AncientBirthBlock) Index() uint { return 5 } // ExhaustsResources The transaction would exhaust the resources of current block -type ExhaustsResources struct { - err string -} +type ExhaustsResources struct{} +// Index Returns VDT index func (err ExhaustsResources) Index() uint { return 6 } // InvalidCustom Any other custom invalid validity that is not covered type InvalidCustom uint8 +// Index Returns VDT index func (err InvalidCustom) Index() uint { return 7 } // BadMandatory An extrinsic with a Mandatory dispatch resulted in Error -type BadMandatory struct { - err string -} +type BadMandatory struct{} +// Index Returns VDT index func (err BadMandatory) Index() uint { return 8 } // MandatoryDispatch A transaction with a mandatory dispatch -type MandatoryDispatch struct { - err string -} +type MandatoryDispatch struct{} +// Index Returns VDT index func (err MandatoryDispatch) Index() uint { return 9 } func determineDispatchErr(res []byte) error { @@ -204,7 +204,7 @@ func determineDispatchErr(res []byte) error { vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) if err != nil { - return errInvalidResult + return &UnmarshallingError{err.Error()} } switch val := vdt.Value().(type) { @@ -215,7 +215,7 @@ func determineDispatchErr(res []byte) error { case BadOrigin: return &DispatchOutcomeError{"bad origin"} case Module: - return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.String())} + return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.string())} } return errInvalidResult @@ -227,7 +227,7 @@ func determineInvalidTxnErr(res []byte) error { ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) err := scale.Unmarshal(res, &vdt) if err != nil { - return errInvalidResult + return &UnmarshallingError{err.Error()} } switch val := vdt.Value().(type) { @@ -261,7 +261,7 @@ func determineUnknownTxnErr(res []byte) error { vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) err := scale.Unmarshal(res, &vdt) if err != nil { - return errInvalidResult + return &UnmarshallingError{err.Error()} } switch val := vdt.Value().(type) { From a6358c4f6ee337872daac378d85de7a9a3aba66b Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Mon, 28 Jun 2021 13:22:56 -0600 Subject: [PATCH 235/245] use marshal to encode block body --- lib/babe/build.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/babe/build.go b/lib/babe/build.go index 03b6b44a57..3c2d3ecbd2 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -361,5 +361,10 @@ func ExtrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) ( extrinsics = append(extrinsics, decExt) } - return types.NewBodyFromExtrinsics(extrinsics) + enc, err := scale.Marshal(extrinsics) + if err != nil { + return nil, err + } + body := types.Body(enc) + return &body, nil } From 949aba00d0993d58866cb583d705a74f179e6132 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Tue, 29 Jun 2021 08:24:58 -0600 Subject: [PATCH 236/245] cr revisions --- lib/babe/errors.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index a1d480d4fd..7512ce14f5 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -80,13 +80,13 @@ func (e TransactionValidityError) Error() string { return fmt.Sprintf("transaction validity error: %s", e.msg) } -// A UnmarshallingError is when unmarshalling fails -type UnmarshallingError struct { +// A UnmarshalError is when unmarshalling fails +type UnmarshalError struct { msg string } -func (e UnmarshallingError) Error() string { - return fmt.Sprintf("unmarshalling error: %s", e.msg) +func (e UnmarshalError) Error() string { + return fmt.Sprintf("unmarshal error: %s", e.msg) } // Other Some error occurred @@ -204,7 +204,7 @@ func determineDispatchErr(res []byte) error { vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) err := scale.Unmarshal(res, &vdt) if err != nil { - return &UnmarshallingError{err.Error()} + return &UnmarshalError{err.Error()} } switch val := vdt.Value().(type) { @@ -227,7 +227,7 @@ func determineInvalidTxnErr(res []byte) error { ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) err := scale.Unmarshal(res, &vdt) if err != nil { - return &UnmarshallingError{err.Error()} + return &UnmarshalError{err.Error()} } switch val := vdt.Value().(type) { @@ -261,7 +261,7 @@ func determineUnknownTxnErr(res []byte) error { vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) err := scale.Unmarshal(res, &vdt) if err != nil { - return &UnmarshallingError{err.Error()} + return &UnmarshalError{err.Error()} } switch val := vdt.Value().(type) { From c7bfc64fd74a28214a728a8a0f2f4ff4f38ed5c0 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 30 Jun 2021 13:15:14 -0600 Subject: [PATCH 237/245] WIP/use results for unmarshalling --- lib/babe/errors.go | 73 +++++++++++++++++++++++++--------- lib/babe/errors_test.go | 2 + pkg/scale/decode.go | 4 ++ pkg/scale/varying_data_type.go | 4 ++ 4 files changed, 64 insertions(+), 19 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 7512ce14f5..0cc84924ef 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -199,15 +199,30 @@ type MandatoryDispatch struct{} // Index Returns VDT index func (err MandatoryDispatch) Index() uint { return 9 } -func determineDispatchErr(res []byte) error { - var e Other - vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) - err := scale.Unmarshal(res, &vdt) - if err != nil { - return &UnmarshalError{err.Error()} - } +//func determineDispatchErr(res []byte) error { +// var e Other +// vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) +// err := scale.Unmarshal(res, &vdt) +// if err != nil { +// return &UnmarshalError{err.Error()} +// } +// +// switch val := vdt.Value().(type) { +// case Other: +// return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} +// case CannotLookup: +// return &DispatchOutcomeError{"failed lookup"} +// case BadOrigin: +// return &DispatchOutcomeError{"bad origin"} +// case Module: +// return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.string())} +// } +// +// return errInvalidResult +//} - switch val := vdt.Value().(type) { +func determineDispatchErr(i interface{}) error { + switch val := i.(type) { case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case CannotLookup: @@ -276,22 +291,42 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } - -func (err CustomModuleError) String() string { - return fmt.Sprintf("index: %d code: %d message: %p", err.index, err.err, err.message) -} - +//Result{ +//OK: Result{ OK: nil, Err: VaryingDataType(string, FailedLookup, BadOrigin, Other?) }. +//Err: VaryingDataType( VaryingDataType{all the invalid tx errors}, VaryingDataType{TxValidityErros}, +//} func determineErr(res []byte) error { switch res[0] { case 0: - switch res[1] { - case 0: - return nil - case 1: - return determineDispatchErr(res[2:]) - default: + var e Other + vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) + r := scale.NewResult(nil, vdt) + err := scale.Unmarshal(res[1:], &r) + if err != nil { + //fmt.Println("error1") + return err + } + + ok, err := r.Unwrap() + //fmt.Println(err) + if err != nil { + er := determineDispatchErr(err) + //fmt.Println(er) + return er + } + //fmt.Println(ok) + if ok != nil { return errInvalidResult } + return nil + //switch res[1] { + //case 0: + // return nil + //case 1: + // return determineDispatchErr(res[2:]) + //default: + // return errInvalidResult + //} case 1: switch res[1] { case 0: diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index c495cde251..921f30168f 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -1,6 +1,7 @@ package babe import ( + "fmt" "testing" "github.com/stretchr/testify/require" @@ -71,6 +72,7 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } + fmt.Println(err) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index e551c6a9d1..9404209a68 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -406,12 +406,16 @@ func (ds *decodeState) decodeVaryingDataTypeSlice(dstv reflect.Value) (err error func (ds *decodeState) decodeVaryingDataType(dstv reflect.Value) (err error) { var b byte b, err = ds.ReadByte() + fmt.Println("expected index") + fmt.Println(b) if err != nil { return } vdt := dstv.Interface().(VaryingDataType) val, ok := vdt.cache[uint(b)] + fmt.Println("cache at index") + fmt.Println(vdt.cache[uint(b)]) if !ok { err = fmt.Errorf("unable to find VaryingDataTypeValue with index: %d", uint(b)) return diff --git a/pkg/scale/varying_data_type.go b/pkg/scale/varying_data_type.go index de524014e2..4f6438f11e 100644 --- a/pkg/scale/varying_data_type.go +++ b/pkg/scale/varying_data_type.go @@ -90,12 +90,16 @@ func NewVaryingDataType(values ...VaryingDataTypeValue) (vdt VaryingDataType, er } vdt.cache = make(map[uint]VaryingDataTypeValue) for _, value := range values { + //fmt.Println("index") + //fmt.Println(value.Index()) _, ok := vdt.cache[value.Index()] if ok { err = fmt.Errorf("duplicate index with VaryingDataType: %T with index: %d", value, value.Index()) return } vdt.cache[value.Index()] = value + //fmt.Println("value at cache") + //fmt.Println(vdt.cache[value.Index()]) } return } From b64ffb96c774d0ed3715b23447e52e7c1662e5ef Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 30 Jun 2021 15:36:37 -0400 Subject: [PATCH 238/245] fixed it --- lib/babe/errors.go | 51 +++++++++++++++-------- lib/babe/errors_test.go | 90 ++++++++++++++++++++--------------------- pkg/scale/decode.go | 2 + 3 files changed, 81 insertions(+), 62 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 0cc84924ef..4742e459df 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -300,6 +300,7 @@ func determineErr(res []byte) error { case 0: var e Other vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) + r := scale.NewResult(nil, vdt) err := scale.Unmarshal(res[1:], &r) if err != nil { @@ -308,25 +309,41 @@ func determineErr(res []byte) error { } ok, err := r.Unwrap() - //fmt.Println(err) if err != nil { - er := determineDispatchErr(err) - //fmt.Println(er) - return er - } - //fmt.Println(ok) - if ok != nil { - return errInvalidResult + // error case + fmt.Printf("err %v\n", err) + switch err := err.(type) { + case scale.WrappedErr: + vdt := err.Err.(scale.VaryingDataType) + switch val := vdt.Value().(type) { + case Module, BadOrigin, CannotLookup, Other: + fmt.Printf("in here! %T %v\n", val, val) + } + + } + } else { + // ok case + fmt.Printf("ok %v\n", ok) } - return nil - //switch res[1] { - //case 0: - // return nil - //case 1: - // return determineDispatchErr(res[2:]) - //default: - // return errInvalidResult - //} + // //fmt.Println(err) + // if err != nil { + // er := determineDispatchErr(err) + // //fmt.Println(er) + // return er + // } + // //fmt.Println(ok) + // if ok != nil { + // return errInvalidResult + // } + // return nil + // //switch res[1] { + // //case 0: + // // return nil + // //case 1: + // // return determineDispatchErr(res[2:]) + // //default: + // // return errInvalidResult + // //} case 1: switch res[1] { case 0: diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 921f30168f..07cb6d3d98 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -13,56 +13,56 @@ func TestApplyExtrinsicErrors(t *testing.T) { test []byte expected string }{ - { - name: "Valid extrinsic", - test: []byte{0, 0}, - expected: "", - }, + // { + // name: "Valid extrinsic", + // test: []byte{0, 0}, + // expected: "", + // }, { name: "Dispatch custom module error empty", test: []byte{0, 1, 3, 4, 5, 1, 0}, expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: ", }, - { - name: "Dispatch custom module error", - test: []byte{0, 1, 3, 4, 5, 1, 0x04, 0x65}, - expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: 65", - }, - { - name: "Dispatch unknown error", - test: []byte{0, 1, 0, 0x04, 65}, - expected: "dispatch outcome error: unknown error: A", - }, - { - name: "Dispatch failed lookup", - test: []byte{0, 1, 1}, - expected: "dispatch outcome error: failed lookup", - }, - { - name: "Dispatch bad origin", - test: []byte{0, 1, 2}, - expected: "dispatch outcome error: bad origin", - }, - { - name: "Invalid txn payment error", - test: []byte{1, 0, 1}, - expected: "transaction validity error: invalid payment", - }, - { - name: "Invalid txn payment error", - test: []byte{1, 0, 7, 65}, - expected: "transaction validity error: unknown error: 65", - }, - { - name: "Unknown txn lookup failed error", - test: []byte{1, 1, 0}, - expected: "transaction validity error: lookup failed", - }, - { - name: "Invalid txn unknown error", - test: []byte{1, 1, 2, 75}, - expected: "transaction validity error: unknown error: 75", - }, + // { + // name: "Dispatch custom module error", + // test: []byte{0, 1, 3, 4, 5, 1, 0x04, 0x65}, + // expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: 65", + // }, + // { + // name: "Dispatch unknown error", + // test: []byte{0, 1, 0, 0x04, 65}, + // expected: "dispatch outcome error: unknown error: A", + // }, + // { + // name: "Dispatch failed lookup", + // test: []byte{0, 1, 1}, + // expected: "dispatch outcome error: failed lookup", + // }, + // { + // name: "Dispatch bad origin", + // test: []byte{0, 1, 2}, + // expected: "dispatch outcome error: bad origin", + // }, + // { + // name: "Invalid txn payment error", + // test: []byte{1, 0, 1}, + // expected: "transaction validity error: invalid payment", + // }, + // { + // name: "Invalid txn payment error", + // test: []byte{1, 0, 7, 65}, + // expected: "transaction validity error: unknown error: 65", + // }, + // { + // name: "Unknown txn lookup failed error", + // test: []byte{1, 1, 0}, + // expected: "transaction validity error: lookup failed", + // }, + // { + // name: "Invalid txn unknown error", + // test: []byte{1, 1, 2, 75}, + // expected: "transaction validity error: unknown error: 75", + // }, } for _, c := range testCases { diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 9404209a68..400c7df45d 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -325,6 +325,7 @@ func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { switch rb { case 0x00: tempElem := reflect.New(reflect.TypeOf(res.ok)) + tempElem.Elem().Set(reflect.ValueOf(res.ok)) err = ds.unmarshal(tempElem.Elem()) if err != nil { return @@ -336,6 +337,7 @@ func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { dstv.Set(reflect.ValueOf(res)) case 0x01: tempElem := reflect.New(reflect.TypeOf(res.err)) + tempElem.Elem().Set(reflect.ValueOf(res.err)) err = ds.unmarshal(tempElem.Elem()) if err != nil { return From e5eb8882748c4492d1a151617f1f212b607b3feb Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 30 Jun 2021 14:09:52 -0600 Subject: [PATCH 239/245] use result type for dispatch error --- lib/babe/errors.go | 38 ++------------ lib/babe/errors_test.go | 90 +++++++++++++++++----------------- pkg/scale/decode.go | 4 -- pkg/scale/varying_data_type.go | 4 -- 4 files changed, 50 insertions(+), 86 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 4742e459df..98aac2bd47 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -221,8 +221,8 @@ func (err MandatoryDispatch) Index() uint { return 9 } // return errInvalidResult //} -func determineDispatchErr(i interface{}) error { - switch val := i.(type) { +func determineDispatchErr(vdt scale.VaryingDataType) error { + switch val := vdt.Value().(type) { case Other: return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} case CannotLookup: @@ -300,50 +300,22 @@ func determineErr(res []byte) error { case 0: var e Other vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) - r := scale.NewResult(nil, vdt) err := scale.Unmarshal(res[1:], &r) if err != nil { - //fmt.Println("error1") return err } - ok, err := r.Unwrap() + _, err = r.Unwrap() if err != nil { - // error case - fmt.Printf("err %v\n", err) switch err := err.(type) { case scale.WrappedErr: vdt := err.Err.(scale.VaryingDataType) - switch val := vdt.Value().(type) { - case Module, BadOrigin, CannotLookup, Other: - fmt.Printf("in here! %T %v\n", val, val) - } - + return determineDispatchErr(vdt) } } else { - // ok case - fmt.Printf("ok %v\n", ok) + return nil } - // //fmt.Println(err) - // if err != nil { - // er := determineDispatchErr(err) - // //fmt.Println(er) - // return er - // } - // //fmt.Println(ok) - // if ok != nil { - // return errInvalidResult - // } - // return nil - // //switch res[1] { - // //case 0: - // // return nil - // //case 1: - // // return determineDispatchErr(res[2:]) - // //default: - // // return errInvalidResult - // //} case 1: switch res[1] { case 0: diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 07cb6d3d98..921f30168f 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -13,56 +13,56 @@ func TestApplyExtrinsicErrors(t *testing.T) { test []byte expected string }{ - // { - // name: "Valid extrinsic", - // test: []byte{0, 0}, - // expected: "", - // }, + { + name: "Valid extrinsic", + test: []byte{0, 0}, + expected: "", + }, { name: "Dispatch custom module error empty", test: []byte{0, 1, 3, 4, 5, 1, 0}, expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: ", }, - // { - // name: "Dispatch custom module error", - // test: []byte{0, 1, 3, 4, 5, 1, 0x04, 0x65}, - // expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: 65", - // }, - // { - // name: "Dispatch unknown error", - // test: []byte{0, 1, 0, 0x04, 65}, - // expected: "dispatch outcome error: unknown error: A", - // }, - // { - // name: "Dispatch failed lookup", - // test: []byte{0, 1, 1}, - // expected: "dispatch outcome error: failed lookup", - // }, - // { - // name: "Dispatch bad origin", - // test: []byte{0, 1, 2}, - // expected: "dispatch outcome error: bad origin", - // }, - // { - // name: "Invalid txn payment error", - // test: []byte{1, 0, 1}, - // expected: "transaction validity error: invalid payment", - // }, - // { - // name: "Invalid txn payment error", - // test: []byte{1, 0, 7, 65}, - // expected: "transaction validity error: unknown error: 65", - // }, - // { - // name: "Unknown txn lookup failed error", - // test: []byte{1, 1, 0}, - // expected: "transaction validity error: lookup failed", - // }, - // { - // name: "Invalid txn unknown error", - // test: []byte{1, 1, 2, 75}, - // expected: "transaction validity error: unknown error: 75", - // }, + { + name: "Dispatch custom module error", + test: []byte{0, 1, 3, 4, 5, 1, 0x04, 0x65}, + expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: 65", + }, + { + name: "Dispatch unknown error", + test: []byte{0, 1, 0, 0x04, 65}, + expected: "dispatch outcome error: unknown error: A", + }, + { + name: "Dispatch failed lookup", + test: []byte{0, 1, 1}, + expected: "dispatch outcome error: failed lookup", + }, + { + name: "Dispatch bad origin", + test: []byte{0, 1, 2}, + expected: "dispatch outcome error: bad origin", + }, + { + name: "Invalid txn payment error", + test: []byte{1, 0, 1}, + expected: "transaction validity error: invalid payment", + }, + { + name: "Invalid txn payment error", + test: []byte{1, 0, 7, 65}, + expected: "transaction validity error: unknown error: 65", + }, + { + name: "Unknown txn lookup failed error", + test: []byte{1, 1, 0}, + expected: "transaction validity error: lookup failed", + }, + { + name: "Invalid txn unknown error", + test: []byte{1, 1, 2, 75}, + expected: "transaction validity error: unknown error: 75", + }, } for _, c := range testCases { diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 400c7df45d..231d865e87 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -408,16 +408,12 @@ func (ds *decodeState) decodeVaryingDataTypeSlice(dstv reflect.Value) (err error func (ds *decodeState) decodeVaryingDataType(dstv reflect.Value) (err error) { var b byte b, err = ds.ReadByte() - fmt.Println("expected index") - fmt.Println(b) if err != nil { return } vdt := dstv.Interface().(VaryingDataType) val, ok := vdt.cache[uint(b)] - fmt.Println("cache at index") - fmt.Println(vdt.cache[uint(b)]) if !ok { err = fmt.Errorf("unable to find VaryingDataTypeValue with index: %d", uint(b)) return diff --git a/pkg/scale/varying_data_type.go b/pkg/scale/varying_data_type.go index 4f6438f11e..de524014e2 100644 --- a/pkg/scale/varying_data_type.go +++ b/pkg/scale/varying_data_type.go @@ -90,16 +90,12 @@ func NewVaryingDataType(values ...VaryingDataTypeValue) (vdt VaryingDataType, er } vdt.cache = make(map[uint]VaryingDataTypeValue) for _, value := range values { - //fmt.Println("index") - //fmt.Println(value.Index()) _, ok := vdt.cache[value.Index()] if ok { err = fmt.Errorf("duplicate index with VaryingDataType: %T with index: %d", value, value.Index()) return } vdt.cache[value.Index()] = value - //fmt.Println("value at cache") - //fmt.Println(vdt.cache[value.Index()]) } return } From 67cf81c264c010c8a612f8ee89355745333934a5 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 30 Jun 2021 17:16:30 -0600 Subject: [PATCH 240/245] WIP/Finish result implementation --- lib/babe/errors.go | 160 ++++++++++++++++++++++++++++++--------------- 1 file changed, 106 insertions(+), 54 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 98aac2bd47..90934ec119 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -199,28 +199,7 @@ type MandatoryDispatch struct{} // Index Returns VDT index func (err MandatoryDispatch) Index() uint { return 9 } -//func determineDispatchErr(res []byte) error { -// var e Other -// vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) -// err := scale.Unmarshal(res, &vdt) -// if err != nil { -// return &UnmarshalError{err.Error()} -// } -// -// switch val := vdt.Value().(type) { -// case Other: -// return &DispatchOutcomeError{fmt.Sprintf("unknown error: %s", val)} -// case CannotLookup: -// return &DispatchOutcomeError{"failed lookup"} -// case BadOrigin: -// return &DispatchOutcomeError{"bad origin"} -// case Module: -// return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.string())} -// } -// -// return errInvalidResult -//} - +// Consolidate these into one function func determineDispatchErr(vdt scale.VaryingDataType) error { switch val := vdt.Value().(type) { case Other: @@ -236,15 +215,7 @@ func determineDispatchErr(vdt scale.VaryingDataType) error { return errInvalidResult } -func determineInvalidTxnErr(res []byte) error { - var c InvalidCustom - vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, - ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) - err := scale.Unmarshal(res, &vdt) - if err != nil { - return &UnmarshalError{err.Error()} - } - +func determineInvalidTxnErr(vdt scale.VaryingDataType) error { switch val := vdt.Value().(type) { case Call: return &TransactionValidityError{"call of the transaction is not expected"} @@ -291,38 +262,119 @@ func determineUnknownTxnErr(res []byte) error { return errInvalidResult } + +type ValidityVdt struct{ + vdt scale.VaryingDataType +} +func (err ValidityVdt) Index() uint { return 0 } + +type UnknownVdt struct{ + vdt scale.VaryingDataType +} +func (err UnknownVdt) Index() uint { return 1 } + //Result{ //OK: Result{ OK: nil, Err: VaryingDataType(string, FailedLookup, BadOrigin, Other?) }. //Err: VaryingDataType( VaryingDataType{all the invalid tx errors}, VaryingDataType{TxValidityErros}, //} func determineErr(res []byte) error { - switch res[0] { - case 0: - var e Other - vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) - r := scale.NewResult(nil, vdt) - err := scale.Unmarshal(res[1:], &r) - if err != nil { - return err - } + // Dispatch + var other Other + dispatchVdt := scale.MustNewVaryingDataType(other, CannotLookup{}, BadOrigin{}, Module{}) + // Invalid + var invalidCustom InvalidCustom + var validityVdt ValidityVdt + validityVdt.vdt = scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, + ExhaustsResources{}, invalidCustom, BadMandatory{}, MandatoryDispatch{}) + // Unknown + var unknownCustom UnknownCustom + var unknownVdt UnknownVdt + unknownVdt.vdt = scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, unknownCustom) + // Ok result + okRes := scale.NewResult(nil, dispatchVdt) + + // Big boy result + result := scale.NewResult(okRes, scale.MustNewVaryingDataType(validityVdt, unknownCustom)) + err := scale.Unmarshal(res, &result) + if err != nil { + return err + } - _, err = r.Unwrap() - if err != nil { - switch err := err.(type) { - case scale.WrappedErr: - vdt := err.Err.(scale.VaryingDataType) - return determineDispatchErr(vdt) + ok, err := result.Unwrap() + if err != nil { // Error case is a vdt of 2 vdts + fmt.Printf("err %v\n", err) + switch err := err.(type) { + case scale.WrappedErr: + fmt.Printf("ERROR wrapped error \n") + vdt := err.Err.(scale.VaryingDataType) + fmt.Printf("Error type: %T\n", vdt.Value()) + switch val := vdt.Value().(type) { + case ValidityVdt: + // This part returns invalid error so gotta figure that out + fmt.Printf("ValidityVdt \n") + return determineInvalidTxnErr(val.vdt) + case UnknownVdt: + // Not finding the index here + fmt.Printf("UnknownVdt \n") + default: + fmt.Printf("in here! %T %v\n", val, val) } - } else { - return nil + default: + fmt.Printf("Shouldnt reach here: %v\n", err) } - case 1: - switch res[1] { - case 0: - return determineInvalidTxnErr(res[2:]) - case 1: - return determineUnknownTxnErr(res[2:]) + + } else { // okay case is a result + fmt.Printf("ok type %T\n", ok) + switch o := ok.(type) { + case scale.Result: + fmt.Printf("OK result \n") + ok, err = o.Unwrap() + if err != nil { + fmt.Printf("This should be dispatch error \n") + switch err := err.(type) { + case scale.WrappedErr: + vdt := err.Err.(scale.VaryingDataType) + return determineDispatchErr(vdt) + } + } else { // No error + fmt.Printf("No error \n") + return nil + } + default: + fmt.Printf("Shouldnt reach here: %v\n", o) } } + + //switch res[0] { + //case 0: + // r := scale.NewResult(nil, dispatchVdt) + // err := scale.Unmarshal(res[1:], &r) + // if err != nil { + // return err + // } + // + // _, err = r.Unwrap() + // if err != nil { + // switch err := err.(type) { + // case scale.WrappedErr: + // vdt := err.Err.(scale.VaryingDataType) + // return determineDispatchErr(vdt) + // } + // } else { + // return nil + // } + //case 1: + // switch res[1] { + // case 0: + // //var c InvalidCustom + // //vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, + // // ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) + // return determineInvalidTxnErr(res[2:]) + // case 1: + // //var c UnknownCustom + // //vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) + // return determineUnknownTxnErr(res[2:]) + // } + //} return errInvalidResult } From 7385b116db3cf94eb8c401637de10d9d9a76143c Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Wed, 30 Jun 2021 17:52:10 -0600 Subject: [PATCH 241/245] WIP/Fix decoding custom VDT types --- lib/babe/errors.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 90934ec119..ef258de4d7 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -237,6 +237,8 @@ func determineInvalidTxnErr(vdt scale.VaryingDataType) error { return &TransactionValidityError{"mandatory dispatch error"} case MandatoryDispatch: return &TransactionValidityError{"invalid mandatory dispatch"} + default: + fmt.Printf("Default %T\n", val) } return errInvalidResult @@ -308,11 +310,13 @@ func determineErr(res []byte) error { fmt.Printf("ERROR wrapped error \n") vdt := err.Err.(scale.VaryingDataType) fmt.Printf("Error type: %T\n", vdt.Value()) + // This doesnt seem to be being decoding correctly with custom VDTs switch val := vdt.Value().(type) { case ValidityVdt: // This part returns invalid error so gotta figure that out - fmt.Printf("ValidityVdt \n") - return determineInvalidTxnErr(val.vdt) + fmt.Printf("ValidityVdt %T\n", val.vdt) + fmt.Printf("ValidityVdt %v\n", val.vdt) + //return determineInvalidTxnErr(val.vdt) case UnknownVdt: // Not finding the index here fmt.Printf("UnknownVdt \n") From 64001ee4b26cd260b4c341c49b56b50158b17a81 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 1 Jul 2021 09:42:39 -0600 Subject: [PATCH 242/245] use results for unmarshalling dispatchErrors --- lib/babe/errors.go | 149 ++++++++++----------------------------------- 1 file changed, 32 insertions(+), 117 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index ef258de4d7..fc7696fe63 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -199,7 +199,6 @@ type MandatoryDispatch struct{} // Index Returns VDT index func (err MandatoryDispatch) Index() uint { return 9 } -// Consolidate these into one function func determineDispatchErr(vdt scale.VaryingDataType) error { switch val := vdt.Value().(type) { case Other: @@ -215,7 +214,14 @@ func determineDispatchErr(vdt scale.VaryingDataType) error { return errInvalidResult } -func determineInvalidTxnErr(vdt scale.VaryingDataType) error { +func determineInvalidTxnErr(res []byte) error { + var c InvalidCustom + vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, + ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) + err := scale.Unmarshal(res, &vdt) + if err != nil { + return &UnmarshalError{err.Error()} + } switch val := vdt.Value().(type) { case Call: return &TransactionValidityError{"call of the transaction is not expected"} @@ -237,10 +243,7 @@ func determineInvalidTxnErr(vdt scale.VaryingDataType) error { return &TransactionValidityError{"mandatory dispatch error"} case MandatoryDispatch: return &TransactionValidityError{"invalid mandatory dispatch"} - default: - fmt.Printf("Default %T\n", val) } - return errInvalidResult } @@ -251,7 +254,6 @@ func determineUnknownTxnErr(res []byte) error { if err != nil { return &UnmarshalError{err.Error()} } - switch val := vdt.Value().(type) { case ValidityCannotLookup: return &TransactionValidityError{"lookup failed"} @@ -260,125 +262,38 @@ func determineUnknownTxnErr(res []byte) error { case UnknownCustom: return &TransactionValidityError{fmt.Sprintf("unknown error: %d", val)} } - return errInvalidResult } - -type ValidityVdt struct{ - vdt scale.VaryingDataType -} -func (err ValidityVdt) Index() uint { return 0 } - -type UnknownVdt struct{ - vdt scale.VaryingDataType -} -func (err UnknownVdt) Index() uint { return 1 } - -//Result{ -//OK: Result{ OK: nil, Err: VaryingDataType(string, FailedLookup, BadOrigin, Other?) }. -//Err: VaryingDataType( VaryingDataType{all the invalid tx errors}, VaryingDataType{TxValidityErros}, -//} +// TODO wrap all errors in a Result to unmarshall func determineErr(res []byte) error { - // Dispatch - var other Other - dispatchVdt := scale.MustNewVaryingDataType(other, CannotLookup{}, BadOrigin{}, Module{}) - // Invalid - var invalidCustom InvalidCustom - var validityVdt ValidityVdt - validityVdt.vdt = scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, - ExhaustsResources{}, invalidCustom, BadMandatory{}, MandatoryDispatch{}) - // Unknown - var unknownCustom UnknownCustom - var unknownVdt UnknownVdt - unknownVdt.vdt = scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, unknownCustom) - // Ok result - okRes := scale.NewResult(nil, dispatchVdt) - - // Big boy result - result := scale.NewResult(okRes, scale.MustNewVaryingDataType(validityVdt, unknownCustom)) - err := scale.Unmarshal(res, &result) - if err != nil { - return err - } - - ok, err := result.Unwrap() - if err != nil { // Error case is a vdt of 2 vdts - fmt.Printf("err %v\n", err) - switch err := err.(type) { - case scale.WrappedErr: - fmt.Printf("ERROR wrapped error \n") - vdt := err.Err.(scale.VaryingDataType) - fmt.Printf("Error type: %T\n", vdt.Value()) - // This doesnt seem to be being decoding correctly with custom VDTs - switch val := vdt.Value().(type) { - case ValidityVdt: - // This part returns invalid error so gotta figure that out - fmt.Printf("ValidityVdt %T\n", val.vdt) - fmt.Printf("ValidityVdt %v\n", val.vdt) - //return determineInvalidTxnErr(val.vdt) - case UnknownVdt: - // Not finding the index here - fmt.Printf("UnknownVdt \n") - default: - fmt.Printf("in here! %T %v\n", val, val) - } - default: - fmt.Printf("Shouldnt reach here: %v\n", err) + switch res[0] { + case 0: + var e Other + vdt := scale.MustNewVaryingDataType(e, CannotLookup{}, BadOrigin{}, Module{}) + r := scale.NewResult(nil, vdt) + err := scale.Unmarshal(res[1:], &r) + if err != nil { + return err } - } else { // okay case is a result - fmt.Printf("ok type %T\n", ok) - switch o := ok.(type) { - case scale.Result: - fmt.Printf("OK result \n") - ok, err = o.Unwrap() - if err != nil { - fmt.Printf("This should be dispatch error \n") - switch err := err.(type) { - case scale.WrappedErr: - vdt := err.Err.(scale.VaryingDataType) - return determineDispatchErr(vdt) - } - } else { // No error - fmt.Printf("No error \n") - return nil + _, err = r.Unwrap() + if err != nil { + switch err := err.(type) { + case scale.WrappedErr: + vdt := err.Err.(scale.VaryingDataType) + return determineDispatchErr(vdt) } - default: - fmt.Printf("Shouldnt reach here: %v\n", o) + } else { + return nil + } + case 1: + switch res[1] { + case 0: + return determineInvalidTxnErr(res[2:]) + case 1: + return determineUnknownTxnErr(res[2:]) } } - - //switch res[0] { - //case 0: - // r := scale.NewResult(nil, dispatchVdt) - // err := scale.Unmarshal(res[1:], &r) - // if err != nil { - // return err - // } - // - // _, err = r.Unwrap() - // if err != nil { - // switch err := err.(type) { - // case scale.WrappedErr: - // vdt := err.Err.(scale.VaryingDataType) - // return determineDispatchErr(vdt) - // } - // } else { - // return nil - // } - //case 1: - // switch res[1] { - // case 0: - // //var c InvalidCustom - // //vdt := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{}, - // // ExhaustsResources{}, c, BadMandatory{}, MandatoryDispatch{}) - // return determineInvalidTxnErr(res[2:]) - // case 1: - // //var c UnknownCustom - // //vdt := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, c) - // return determineUnknownTxnErr(res[2:]) - // } - //} return errInvalidResult } From 580a392275957ee5ec88bc3cf207d0e931271fee Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 1 Jul 2021 10:51:22 -0600 Subject: [PATCH 243/245] cleanup --- lib/babe/errors.go | 2 ++ lib/babe/errors_test.go | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index fc7696fe63..b7a44cb96f 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -283,6 +283,8 @@ func determineErr(res []byte) error { case scale.WrappedErr: vdt := err.Err.(scale.VaryingDataType) return determineDispatchErr(vdt) + default: + return &UnmarshalError{"Unexpected result error type"} } } else { return nil diff --git a/lib/babe/errors_test.go b/lib/babe/errors_test.go index 921f30168f..c495cde251 100644 --- a/lib/babe/errors_test.go +++ b/lib/babe/errors_test.go @@ -1,7 +1,6 @@ package babe import ( - "fmt" "testing" "github.com/stretchr/testify/require" @@ -72,7 +71,6 @@ func TestApplyExtrinsicErrors(t *testing.T) { require.NoError(t, err) return } - fmt.Println(err) if c.test[0] == 0 { _, ok := err.(*DispatchOutcomeError) From f286e1592847ef1048c7d52bfb20558db423f252 Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 1 Jul 2021 12:57:50 -0600 Subject: [PATCH 244/245] merge --- pkg/scale/comparison_test.go | 50 +-- pkg/scale/decode.go | 104 +---- pkg/scale/decode_test.go | 375 +---------------- pkg/scale/encode.go | 2 +- pkg/scale/encode_test.go | 86 +--- pkg/scale/result_test.go | 216 +--------- pkg/scale/varying_data_type_test.go | 606 +--------------------------- 7 files changed, 7 insertions(+), 1432 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 52b418556b..1084ac12a2 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -172,52 +172,4 @@ func BenchmarkMarshal(b *testing.B) { } } } -} - -func BenchmarkUnmarshal(b *testing.B) { - for i := 0; i < b.N; i++ { - for _, tt := range allTests { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - } - } -} - -func BenchmarkMarshal(b *testing.B) { - for i := 0; i < b.N; i++ { - for _, tt := range allTests { - // dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if _, err := Marshal(tt.in); (err != nil) != tt.wantErr { - b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - } - } -} - -func BenchmarkUnmarshal(b *testing.B) { - for i := 0; i < b.N; i++ { - for _, tt := range allTests { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - } - } -} - -func BenchmarkMarshal(b *testing.B) { - for i := 0; i < b.N; i++ { - for _, tt := range allTests { - // dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if _, err := Marshal(tt.in); (err != nil) != tt.wantErr { - b.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - } - } -} +} \ No newline at end of file diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 231d865e87..07e4c92e0f 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -230,91 +230,6 @@ func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { return } -func (ds *decodeState) decodeCustomPrimitive(dstv reflect.Value) (err error) { - in := dstv.Interface() - inType := reflect.TypeOf(in) - var temp reflect.Value - switch inType.Kind() { - case reflect.Bool: - temp = reflect.New(reflect.TypeOf(false)) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int: - temp = reflect.New(reflect.TypeOf(int(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int8: - temp = reflect.New(reflect.TypeOf(int8(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int16: - temp = reflect.New(reflect.TypeOf(int16(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int32: - temp = reflect.New(reflect.TypeOf(int32(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Int64: - temp = reflect.New(reflect.TypeOf(int64(1))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.String: - temp = reflect.New(reflect.TypeOf("")) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint: - temp = reflect.New(reflect.TypeOf(uint(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint8: - temp = reflect.New(reflect.TypeOf(uint8(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint16: - temp = reflect.New(reflect.TypeOf(uint16(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint32: - temp = reflect.New(reflect.TypeOf(uint32(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - case reflect.Uint64: - temp = reflect.New(reflect.TypeOf(uint64(0))) - err = ds.unmarshal(temp.Elem()) - if err != nil { - break - } - default: - err = fmt.Errorf("unsupported type for custom primitive: %T", in) - return - } - dstv.Set(temp.Elem().Convert(inType)) - return -} - func (ds *decodeState) decodeResult(dstv reflect.Value) (err error) { res := dstv.Interface().(Result) var rb byte @@ -733,21 +648,4 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { } dstv.Set(reflect.ValueOf(ui128)) return -} - -// decodeUint128 accepts a byte array representing Scale encoded common.Uint128 and performs SCALE decoding of the Uint128 -// if the encoding is valid, it then returns (i interface{}, nil) where i is the decoded common.Uint128 , otherwise -// it returns nil and error -func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { - buf := make([]byte, 16) - err = binary.Read(ds, binary.LittleEndian, buf) - if err != nil { - return - } - ui128, err := NewUint128(buf) - if err != nil { - return - } - dstv.Set(reflect.ValueOf(ui128)) - return -} +} \ No newline at end of file diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index ed0b030d56..f2b5833478 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -237,377 +237,4 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { } }) } -} - -func Test_decodeState_decodeBigInt(t *testing.T) { - for _, tt := range bigIntTests { - t.Run(tt.name, func(t *testing.T) { - var dst *big.Int - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - }) - } -} - -func Test_decodeState_decodeBytes(t *testing.T) { - for _, tt := range stringTests { - t.Run(tt.name, func(t *testing.T) { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - }) - } -} - -func Test_decodeState_decodeBool(t *testing.T) { - for _, tt := range boolTests { - t.Run(tt.name, func(t *testing.T) { - var dst bool - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - }) - } -} - -func Test_decodeState_decodeStruct(t *testing.T) { - for _, tt := range structTests { - t.Run(tt.name, func(t *testing.T) { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - var diff string - if tt.out != nil { - diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(big.Int{}, tt.in, VDTValue2{}, MyStructWithIgnore{})) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - }) - } -} -func Test_decodeState_decodeArray(t *testing.T) { - for _, tt := range arrayTests { - t.Run(tt.name, func(t *testing.T) { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - }) - } -} - -func Test_decodeState_decodeSlice(t *testing.T) { - for _, tt := range sliceTests { - t.Run(tt.name, func(t *testing.T) { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - }) - } -} - -func Test_unmarshal_optionality(t *testing.T) { - var ptrTests tests - for _, t := range allTests { - ptrTest := test{ - name: t.name, - in: t.in, - wantErr: t.wantErr, - want: t.want, - out: t.out, - } - - ptrTest.want = append([]byte{0x01}, t.want...) - ptrTests = append(ptrTests, ptrTest) - } - for _, tt := range ptrTests { - t.Run(tt.name, func(t *testing.T) { - switch in := tt.in.(type) { - case VaryingDataType: - // copy the inputted vdt cause we need the cached values - copy := in - vdt := copy - vdt.value = nil - var dst interface{} = &vdt - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - diff := cmp.Diff(vdt.value, tt.in.(VaryingDataType).value, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - default: - dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - var diff string - if tt.out != nil { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - - } - }) - } -} - -func Test_unmarshal_optionality_nil_case(t *testing.T) { - var ptrTests tests - for _, t := range allTests { - ptrTest := test{ - name: t.name, - in: t.in, - wantErr: t.wantErr, - want: t.want, - // ignore out, since we are testing nil case - // out: t.out, - } - ptrTest.want = []byte{0x00} - - temp := reflect.New(reflect.TypeOf(t.in)) - // create a new pointer to type of temp - tempv := reflect.New(reflect.PtrTo(temp.Type()).Elem()) - // set zero value to elem of **temp so that is nil - tempv.Elem().Set(reflect.Zero(tempv.Elem().Type())) - // set test.in to *temp - ptrTest.in = tempv.Elem().Interface() - - ptrTests = append(ptrTests, ptrTest) - } - for _, tt := range ptrTests { - t.Run(tt.name, func(t *testing.T) { - // this becomes a pointer to a zero value of the underlying value - dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - var diff string - if tt.out != nil { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface()) - } else { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - }) - } -} - -func Test_decodeState_decodeBigInt(t *testing.T) { - for _, tt := range bigIntTests { - t.Run(tt.name, func(t *testing.T) { - var dst *big.Int - // dst := reflect.ValueOf(tt.in).Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - }) - } -} - -func Test_decodeState_decodeBytes(t *testing.T) { - for _, tt := range stringTests { - t.Run(tt.name, func(t *testing.T) { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - }) - } -} - -func Test_decodeState_decodeBool(t *testing.T) { - for _, tt := range boolTests { - t.Run(tt.name, func(t *testing.T) { - var dst bool - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - }) - } -} - -func Test_decodeState_decodeStruct(t *testing.T) { - for _, tt := range structTests { - t.Run(tt.name, func(t *testing.T) { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - var diff string - if tt.out != nil { - diff = cmp.Diff(dst, tt.out, cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(dst, tt.in, cmpopts.IgnoreUnexported(big.Int{}, tt.in, VDTValue2{}, MyStructWithIgnore{})) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - }) - } -} -func Test_decodeState_decodeArray(t *testing.T) { - for _, tt := range arrayTests { - t.Run(tt.name, func(t *testing.T) { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - }) - } -} - -func Test_decodeState_decodeSlice(t *testing.T) { - for _, tt := range sliceTests { - t.Run(tt.name, func(t *testing.T) { - dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(dst, tt.in) { - t.Errorf("decodeState.unmarshal() = %v, want %v", dst, tt.in) - } - }) - } -} - -func Test_unmarshal_optionality(t *testing.T) { - var ptrTests tests - for _, t := range allTests { - ptrTest := test{ - name: t.name, - in: t.in, - wantErr: t.wantErr, - want: t.want, - out: t.out, - } - - ptrTest.want = append([]byte{0x01}, t.want...) - ptrTests = append(ptrTests, ptrTest) - } - for _, tt := range ptrTests { - t.Run(tt.name, func(t *testing.T) { - switch in := tt.in.(type) { - case VaryingDataType: - // copy the inputted vdt cause we need the cached values - copy := in - vdt := copy - vdt.value = nil - var dst interface{} = &vdt - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - diff := cmp.Diff(vdt.value, tt.in.(VaryingDataType).value, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - default: - dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - var diff string - if tt.out != nil { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface(), cmpopts.IgnoreUnexported(tt.in)) - } else { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - - } - }) - } -} - -func Test_unmarshal_optionality_nil_case(t *testing.T) { - var ptrTests tests - for _, t := range allTests { - ptrTest := test{ - name: t.name, - in: t.in, - wantErr: t.wantErr, - want: t.want, - // ignore out, since we are testing nil case - // out: t.out, - } - ptrTest.want = []byte{0x00} - - temp := reflect.New(reflect.TypeOf(t.in)) - // create a new pointer to type of temp - tempv := reflect.New(reflect.PtrTo(temp.Type()).Elem()) - // set zero value to elem of **temp so that is nil - tempv.Elem().Set(reflect.Zero(tempv.Elem().Type())) - // set test.in to *temp - ptrTest.in = tempv.Elem().Interface() - - ptrTests = append(ptrTests, ptrTest) - } - for _, tt := range ptrTests { - t.Run(tt.name, func(t *testing.T) { - // this becomes a pointer to a zero value of the underlying value - dst := reflect.New(reflect.TypeOf(tt.in)).Interface() - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("decodeState.unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - var diff string - if tt.out != nil { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.out).Interface()) - } else { - diff = cmp.Diff(reflect.ValueOf(dst).Elem().Interface(), reflect.ValueOf(tt.in).Interface(), cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}, MyStructWithPrivate{})) - } - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - }) - } -} +} \ No newline at end of file diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 7c62a77bd5..66ead6bcfe 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -353,4 +353,4 @@ func (es *encodeState) encodeUint(i uint) (err error) { func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) return -} +} \ No newline at end of file diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 10724dd6b4..9f2b3b01d1 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -175,20 +175,6 @@ var ( want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, } - int64Tests = tests{ - { - name: "myCustomUint(9223372036854775807)", - in: myCustomUint(9223372036854775807), - want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, - }, - } - int64Tests = tests{ - { - name: "myCustomUint(9223372036854775807)", - in: myCustomUint(9223372036854775807), - want: []byte{0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, - }, - } variableWidthIntegerTests = newTests(intTests, uintTests) int64Tests = tests{ @@ -218,20 +204,6 @@ var ( want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, } - uint64Tests = tests{ - { - name: "myCustomInt64(9223372036854775807)", - in: myCustomInt64(9223372036854775807), - want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, - }, - } - uint64Tests = tests{ - { - name: "myCustomInt64(9223372036854775807)", - in: myCustomInt64(9223372036854775807), - want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, - }, - } uint64Tests = tests{ { name: "uint64(1)", @@ -259,20 +231,6 @@ var ( want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, }, } - int32Tests = tests{ - { - name: "myCustomUint64(9223372036854775807)", - in: myCustomUint64(9223372036854775807), - want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, - }, - } - int32Tests = tests{ - { - name: "myCustomUint64(9223372036854775807)", - in: myCustomUint64(9223372036854775807), - want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, - }, - } int32Tests = tests{ { name: "int32(1)", @@ -317,20 +275,6 @@ var ( want: []byte{0xff, 0xff, 0xff, 0x3f}, }, } - int8Tests = tests{ - { - name: "uint32(1073741823)", - in: myCustomUint32(1073741823), - want: []byte{0xff, 0xff, 0xff, 0x3f}, - }, - } - int8Tests = tests{ - { - name: "uint32(1073741823)", - in: myCustomUint32(1073741823), - want: []byte{0xff, 0xff, 0xff, 0x3f}, - }, - } int8Tests = tests{ { name: "int8(1)", @@ -355,20 +299,6 @@ var ( want: []byte{0x01}, }, } - int16Tests = tests{ - { - name: "myCustomInt8(1)", - in: myCustomUint8(1), - want: []byte{0x01}, - }, - } - int16Tests = tests{ - { - name: "myCustomInt8(1)", - in: myCustomUint8(1), - want: []byte{0x01}, - }, - } int16Tests = tests{ { name: "int16(1)", @@ -386,20 +316,6 @@ var ( want: []byte{0xff, 0x3f}, }, } - uint16Tests = tests{ - { - name: "myCustomInt16(16383)", - in: myCustomInt16(16383), - want: []byte{0xff, 0x3f}, - }, - } - uint16Tests = tests{ - { - name: "myCustomInt16(16383)", - in: myCustomInt16(16383), - want: []byte{0xff, 0x3f}, - }, - } uint16Tests = tests{ { name: "uint16(1)", @@ -1156,4 +1072,4 @@ var byteArray = func(length int) []byte { b[i] = 0xff } return b -} +} \ No newline at end of file diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 38e9f48127..2de0a67285 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -316,218 +316,4 @@ func TestResult_Set(t *testing.T) { } }) } -} - -func TestResult_Set(t *testing.T) { - type args struct { - mode ResultMode - in interface{} - } - tests := []struct { - name string - res Result - args args - wantErr bool - }{ - { - args: args{ - mode: Unset, - }, - res: NewResult(nil, nil), - wantErr: true, - wantResult: Result{ - ok: empty{}, err: empty{}, - }, - }, - { - args: args{ - mode: OK, - in: nil, - }, - res: NewResult(nil, nil), - wantResult: Result{ - ok: empty{}, - err: empty{}, - mode: OK, - }, - }, - { - args: args{ - mode: Err, - in: nil, - }, - res: NewResult(nil, nil), - wantResult: Result{ - ok: empty{}, - err: empty{}, - mode: Err, - }, - }, - { - args: args{ - mode: OK, - in: true, - }, - res: NewResult(true, nil), - wantResult: Result{ - ok: true, - err: empty{}, - mode: OK, - }, - }, - { - args: args{ - mode: Err, - in: true, - }, - res: NewResult(nil, true), - wantResult: Result{ - ok: empty{}, - err: true, - mode: Err, - }, - }, - { - args: args{ - mode: OK, - in: true, - }, - res: NewResult("ok", "err"), - wantErr: true, - wantResult: Result{ - ok: "ok", - err: "err", - }, - }, - { - args: args{ - mode: Err, - in: nil, - }, - res: NewResult(nil, true), - wantErr: true, - wantResult: Result{ - ok: empty{}, - err: true, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - r := tt.res - if err := r.Set(tt.args.mode, tt.args.in); (err != nil) != tt.wantErr { - t.Errorf("Result.Set() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(tt.wantResult, r) { - t.Errorf("Result.Unwrap() = %v, want %v", tt.wantResult, r) - } - }) - } -} - -func TestResult_Set(t *testing.T) { - type args struct { - mode ResultMode - in interface{} - } - tests := []struct { - name string - res Result - args args - wantErr bool - }{ - { - args: args{ - mode: Unset, - }, - res: NewResult(nil, nil), - wantErr: true, - wantResult: Result{ - ok: empty{}, err: empty{}, - }, - }, - { - args: args{ - mode: OK, - in: nil, - }, - res: NewResult(nil, nil), - wantResult: Result{ - ok: empty{}, - err: empty{}, - mode: OK, - }, - }, - { - args: args{ - mode: Err, - in: nil, - }, - res: NewResult(nil, nil), - wantResult: Result{ - ok: empty{}, - err: empty{}, - mode: Err, - }, - }, - { - args: args{ - mode: OK, - in: true, - }, - res: NewResult(true, nil), - wantResult: Result{ - ok: true, - err: empty{}, - mode: OK, - }, - }, - { - args: args{ - mode: Err, - in: true, - }, - res: NewResult(nil, true), - wantResult: Result{ - ok: empty{}, - err: true, - mode: Err, - }, - }, - { - args: args{ - mode: OK, - in: true, - }, - res: NewResult("ok", "err"), - wantErr: true, - wantResult: Result{ - ok: "ok", - err: "err", - }, - }, - { - args: args{ - mode: Err, - in: nil, - }, - res: NewResult(nil, true), - wantErr: true, - wantResult: Result{ - ok: empty{}, - err: true, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - r := tt.res - if err := r.Set(tt.args.mode, tt.args.in); (err != nil) != tt.wantErr { - t.Errorf("Result.Set() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(tt.wantResult, r) { - t.Errorf("Result.Unwrap() = %v, want %v", tt.wantResult, r) - } - }) - } -} +} \ No newline at end of file diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index 62423ba273..1c5a1756ec 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -637,608 +637,4 @@ func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { } }) } -} - -func TestNewVaryingDataType(t *testing.T) { - type args struct { - values []VaryingDataTypeValue - } - tests := []struct { - name string - args args - wantVdt VaryingDataType - wantErr bool - }{ - { - args: args{ - values: []VaryingDataTypeValue{}, - }, - wantErr: true, - }, - { - args: args{ - values: []VaryingDataTypeValue{ - VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), - }, - }, - wantVdt: VaryingDataType{ - cache: map[uint]VaryingDataTypeValue{ - VDTValue{}.Index(): VDTValue{}, - VDTValue1{}.Index(): VDTValue1{}, - VDTValue2{}.Index(): VDTValue2{}, - VDTValue3(0).Index(): VDTValue3(0), - }, - }, - }, - { - args: args{ - values: []VaryingDataTypeValue{ - VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), VDTValue{}, - }, - }, - wantVdt: VaryingDataType{ - cache: map[uint]VaryingDataTypeValue{ - VDTValue{}.Index(): VDTValue{}, - VDTValue1{}.Index(): VDTValue1{}, - VDTValue2{}.Index(): VDTValue2{}, - VDTValue3(0).Index(): VDTValue3(0), - }, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gotVdt, err := NewVaryingDataType(tt.args.values...) - if (err != nil) != tt.wantErr { - t.Errorf("NewVaryingDataType() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(gotVdt, tt.wantVdt) { - t.Errorf("NewVaryingDataType() = %v, want %v", gotVdt, tt.wantVdt) - } - }) - } -} - -func TestVaryingDataType_Set(t *testing.T) { - type args struct { - value VaryingDataTypeValue - } - tests := []struct { - name string - vdt VaryingDataType - args args - wantErr bool - }{ - { - vdt: mustNewVaryingDataType(VDTValue1{}), - args: args{ - value: VDTValue1{}, - }, - }, - { - vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), - args: args{ - value: VDTValue1{}, - }, - }, - { - vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), - args: args{ - value: VDTValue2{}, - }, - }, - { - vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), - args: args{ - value: VDTValue3(0), - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - vdt := tt.vdt - if err := vdt.Set(tt.args.value); (err != nil) != tt.wantErr { - t.Errorf("VaryingDataType.SetValue() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestVaryingDataTypeSlice_Add(t *testing.T) { - type args struct { - values []VaryingDataTypeValue - } - tests := []struct { - name string - vdts VaryingDataTypeSlice - args args - wantErr bool - wantValues []VaryingDataType - }{ - { - name: "happy path", - vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0))), - args: args{ - values: []VaryingDataTypeValue{ - VDTValue{ - B: 1, - }, - }, - }, - wantValues: []VaryingDataType{ - mustNewVaryingDataTypeAndSet( - VDTValue{ - B: 1, - }, - VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), - ), - }, - }, - { - name: "invalid value error case", - vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{})), - args: args{ - values: []VaryingDataTypeValue{ - VDTValue3(0), - }, - }, - wantValues: []VaryingDataType{}, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - vdts := &tt.vdts - if err := vdts.Add(tt.args.values...); (err != nil) != tt.wantErr { - t.Errorf("VaryingDataTypeSlice.Add() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(vdts.Values, tt.wantValues) { - t.Errorf("NewVaryingDataType() = %v, want %v", vdts.Values, tt.wantValues) - } - }) - } -} - -var varyingDataTypeSliceTests = tests{ - { - in: mustNewVaryingDataTypeSliceAndSet( - mustNewVaryingDataType( - VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), - ), - VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, - ), - want: newWant( - []byte{ - // length - 4, - // index - 2, - // value - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - }, - ), - }, - { - in: mustNewVaryingDataTypeSliceAndSet( - mustNewVaryingDataType( - VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), - ), - VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, - VDTValue{ - A: big.NewInt(1073741823), - B: int(1073741823), - C: uint(1073741823), - D: int8(1), - E: uint8(1), - F: int16(16383), - G: uint16(16383), - H: int32(1073741823), - I: uint32(1073741823), - J: int64(9223372036854775807), - K: uint64(9223372036854775807), - L: byteArray(64), - M: testStrings[1], - N: true, - }, - ), - want: newWant( - []byte{ - // length - 8, - }, - []byte{ - // index - 2, - // value - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - }, - []byte{ - // index - 1, - // value - 0xfe, 0xff, 0xff, 0xff, - 0xfe, 0xff, 0xff, 0xff, - 0xfe, 0xff, 0xff, 0xff, - 0x01, - 0x01, - 0xff, 0x3f, - 0xff, 0x3f, - 0xff, 0xff, 0xff, 0x3f, - 0xff, 0xff, 0xff, 0x3f, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, - }, - append([]byte{0x01, 0x01}, byteArray(64)...), - append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), - []byte{0x01}, - ), - }, -} - -func Test_encodeState_encodeVaryingDataTypeSlice(t *testing.T) { - for _, tt := range varyingDataTypeSliceTests { - t.Run(tt.name, func(t *testing.T) { - vdt := tt.in.(VaryingDataTypeSlice) - b, err := Marshal(vdt) - if (err != nil) != tt.wantErr { - t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(b, tt.want) { - t.Errorf("Marshal() = %v, want %v", b, tt.want) - } - }) - } -} - -func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { - opt := cmp.Comparer(func(x, y VaryingDataType) bool { - return reflect.DeepEqual(x.value, y.value) && reflect.DeepEqual(x.cache, y.cache) - }) - - for _, tt := range varyingDataTypeSliceTests { - t.Run(tt.name, func(t *testing.T) { - dst := tt.in.(VaryingDataTypeSlice) - dst.Types = make([]VaryingDataType, 0) - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - vdts := tt.in.(VaryingDataTypeSlice) - diff := cmp.Diff(dst, vdts, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}), opt) - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - }) - } -} - -func TestNewVaryingDataType(t *testing.T) { - type args struct { - values []VaryingDataTypeValue - } - tests := []struct { - name string - args args - wantVdt VaryingDataType - wantErr bool - }{ - { - args: args{ - values: []VaryingDataTypeValue{}, - }, - wantErr: true, - }, - { - args: args{ - values: []VaryingDataTypeValue{ - VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), - }, - }, - wantVdt: VaryingDataType{ - cache: map[uint]VaryingDataTypeValue{ - VDTValue{}.Index(): VDTValue{}, - VDTValue1{}.Index(): VDTValue1{}, - VDTValue2{}.Index(): VDTValue2{}, - VDTValue3(0).Index(): VDTValue3(0), - }, - }, - }, - { - args: args{ - values: []VaryingDataTypeValue{ - VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), VDTValue{}, - }, - }, - wantVdt: VaryingDataType{ - cache: map[uint]VaryingDataTypeValue{ - VDTValue{}.Index(): VDTValue{}, - VDTValue1{}.Index(): VDTValue1{}, - VDTValue2{}.Index(): VDTValue2{}, - VDTValue3(0).Index(): VDTValue3(0), - }, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gotVdt, err := NewVaryingDataType(tt.args.values...) - if (err != nil) != tt.wantErr { - t.Errorf("NewVaryingDataType() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(gotVdt, tt.wantVdt) { - t.Errorf("NewVaryingDataType() = %v, want %v", gotVdt, tt.wantVdt) - } - }) - } -} - -func TestVaryingDataType_Set(t *testing.T) { - type args struct { - value VaryingDataTypeValue - } - tests := []struct { - name string - vdt VaryingDataType - args args - wantErr bool - }{ - { - vdt: mustNewVaryingDataType(VDTValue1{}), - args: args{ - value: VDTValue1{}, - }, - }, - { - vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), - args: args{ - value: VDTValue1{}, - }, - }, - { - vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), - args: args{ - value: VDTValue2{}, - }, - }, - { - vdt: mustNewVaryingDataType(VDTValue1{}, VDTValue2{}), - args: args{ - value: VDTValue3(0), - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - vdt := tt.vdt - if err := vdt.Set(tt.args.value); (err != nil) != tt.wantErr { - t.Errorf("VaryingDataType.SetValue() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestVaryingDataTypeSlice_Add(t *testing.T) { - type args struct { - values []VaryingDataTypeValue - } - tests := []struct { - name string - vdts VaryingDataTypeSlice - args args - wantErr bool - wantValues []VaryingDataType - }{ - { - name: "happy path", - vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0))), - args: args{ - values: []VaryingDataTypeValue{ - VDTValue{ - B: 1, - }, - }, - }, - wantValues: []VaryingDataType{ - mustNewVaryingDataTypeAndSet( - VDTValue{ - B: 1, - }, - VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), - ), - }, - }, - { - name: "invalid value error case", - vdts: NewVaryingDataTypeSlice(MustNewVaryingDataType(VDTValue{}, VDTValue1{}, VDTValue2{})), - args: args{ - values: []VaryingDataTypeValue{ - VDTValue3(0), - }, - }, - wantValues: []VaryingDataType{}, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - vdts := &tt.vdts - if err := vdts.Add(tt.args.values...); (err != nil) != tt.wantErr { - t.Errorf("VaryingDataTypeSlice.Add() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(vdts.Values, tt.wantValues) { - t.Errorf("NewVaryingDataType() = %v, want %v", vdts.Values, tt.wantValues) - } - }) - } -} - -var varyingDataTypeSliceTests = tests{ - { - in: mustNewVaryingDataTypeSliceAndSet( - mustNewVaryingDataType( - VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), - ), - VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, - ), - want: newWant( - []byte{ - // length - 4, - // index - 2, - // value - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - }, - ), - }, - { - in: mustNewVaryingDataTypeSliceAndSet( - mustNewVaryingDataType( - VDTValue{}, VDTValue1{}, VDTValue2{}, VDTValue3(0), - ), - VDTValue1{O: newBigIntPtr(big.NewInt(1073741823))}, - VDTValue{ - A: big.NewInt(1073741823), - B: int(1073741823), - C: uint(1073741823), - D: int8(1), - E: uint8(1), - F: int16(16383), - G: uint16(16383), - H: int32(1073741823), - I: uint32(1073741823), - J: int64(9223372036854775807), - K: uint64(9223372036854775807), - L: byteArray(64), - M: testStrings[1], - N: true, - }, - ), - want: newWant( - []byte{ - // length - 8, - }, - []byte{ - // index - 2, - // value - 0x01, 0xfe, 0xff, 0xff, 0xff, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - }, - []byte{ - // index - 1, - // value - 0xfe, 0xff, 0xff, 0xff, - 0xfe, 0xff, 0xff, 0xff, - 0xfe, 0xff, 0xff, 0xff, - 0x01, - 0x01, - 0xff, 0x3f, - 0xff, 0x3f, - 0xff, 0xff, 0xff, 0x3f, - 0xff, 0xff, 0xff, 0x3f, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, - }, - append([]byte{0x01, 0x01}, byteArray(64)...), - append([]byte{0xC2, 0x02, 0x01, 0x00}, testStrings[1]...), - []byte{0x01}, - ), - }, -} - -func Test_encodeState_encodeVaryingDataTypeSlice(t *testing.T) { - for _, tt := range varyingDataTypeSliceTests { - t.Run(tt.name, func(t *testing.T) { - vdt := tt.in.(VaryingDataTypeSlice) - b, err := Marshal(vdt) - if (err != nil) != tt.wantErr { - t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr) - } - if !reflect.DeepEqual(b, tt.want) { - t.Errorf("Marshal() = %v, want %v", b, tt.want) - } - }) - } -} - -func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { - opt := cmp.Comparer(func(x, y VaryingDataType) bool { - return reflect.DeepEqual(x.value, y.value) && reflect.DeepEqual(x.cache, y.cache) - }) - - for _, tt := range varyingDataTypeSliceTests { - t.Run(tt.name, func(t *testing.T) { - dst := tt.in.(VaryingDataTypeSlice) - dst.Types = make([]VaryingDataType, 0) - if err := Unmarshal(tt.want, &dst); (err != nil) != tt.wantErr { - t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) - return - } - vdts := tt.in.(VaryingDataTypeSlice) - diff := cmp.Diff(dst, vdts, cmpopts.IgnoreUnexported(big.Int{}, VDTValue2{}, MyStructWithIgnore{}), opt) - if diff != "" { - t.Errorf("decodeState.unmarshal() = %s", diff) - } - }) - } -} +} \ No newline at end of file From f1eff85039234f0aada15c9668195f5a49d1c0dd Mon Sep 17 00:00:00 2001 From: jimjbrettj Date: Thu, 1 Jul 2021 13:26:42 -0600 Subject: [PATCH 245/245] lint --- pkg/scale/comparison_test.go | 2 +- pkg/scale/decode.go | 2 +- pkg/scale/decode_test.go | 2 +- pkg/scale/encode.go | 2 +- pkg/scale/encode_test.go | 2 +- pkg/scale/result_test.go | 2 +- pkg/scale/varying_data_type_test.go | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/scale/comparison_test.go b/pkg/scale/comparison_test.go index 1084ac12a2..54f4225da3 100644 --- a/pkg/scale/comparison_test.go +++ b/pkg/scale/comparison_test.go @@ -172,4 +172,4 @@ func BenchmarkMarshal(b *testing.B) { } } } -} \ No newline at end of file +} diff --git a/pkg/scale/decode.go b/pkg/scale/decode.go index 07e4c92e0f..68f43405b6 100644 --- a/pkg/scale/decode.go +++ b/pkg/scale/decode.go @@ -648,4 +648,4 @@ func (ds *decodeState) decodeUint128(dstv reflect.Value) (err error) { } dstv.Set(reflect.ValueOf(ui128)) return -} \ No newline at end of file +} diff --git a/pkg/scale/decode_test.go b/pkg/scale/decode_test.go index f2b5833478..22412b7d8e 100644 --- a/pkg/scale/decode_test.go +++ b/pkg/scale/decode_test.go @@ -237,4 +237,4 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/pkg/scale/encode.go b/pkg/scale/encode.go index 66ead6bcfe..7c62a77bd5 100644 --- a/pkg/scale/encode.go +++ b/pkg/scale/encode.go @@ -353,4 +353,4 @@ func (es *encodeState) encodeUint(i uint) (err error) { func (es *encodeState) encodeUint128(i *Uint128) (err error) { err = binary.Write(es, binary.LittleEndian, i.Bytes()) return -} \ No newline at end of file +} diff --git a/pkg/scale/encode_test.go b/pkg/scale/encode_test.go index 9f2b3b01d1..4f9deedbd4 100644 --- a/pkg/scale/encode_test.go +++ b/pkg/scale/encode_test.go @@ -1072,4 +1072,4 @@ var byteArray = func(length int) []byte { b[i] = 0xff } return b -} \ No newline at end of file +} diff --git a/pkg/scale/result_test.go b/pkg/scale/result_test.go index 2de0a67285..d5b5320974 100644 --- a/pkg/scale/result_test.go +++ b/pkg/scale/result_test.go @@ -316,4 +316,4 @@ func TestResult_Set(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/pkg/scale/varying_data_type_test.go b/pkg/scale/varying_data_type_test.go index 1c5a1756ec..d92dd7c723 100644 --- a/pkg/scale/varying_data_type_test.go +++ b/pkg/scale/varying_data_type_test.go @@ -637,4 +637,4 @@ func Test_decodeState_decodeVaryingDataTypeSlice(t *testing.T) { } }) } -} \ No newline at end of file +}