Skip to content

Commit

Permalink
types: Update the dispatch info weight (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian authored Jan 10, 2023
1 parent 8b830c5 commit 6b02d95
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 149 deletions.
2 changes: 1 addition & 1 deletion types/account_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type AccountInfo struct {
Consumers U32
Providers U32
Sufficients U32
Data struct {
Data struct {
Free U128
Reserved U128
MiscFrozen U128
Expand Down
2 changes: 1 addition & 1 deletion types/dispatch_result_with_post_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import "github.com/centrifuge/go-substrate-rpc-client/v4/scale"
// PostDispatchInfo is used in DispatchResultWithPostInfo.
// Weight information that is only available post dispatch.
type PostDispatchInfo struct {
ActualWeight OptionWeight
ActualWeight Option[Weight]
PaysFee Pays
}

Expand Down
14 changes: 7 additions & 7 deletions types/dispatch_result_with_post_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
testDispatchResultWithPostInfo1 = DispatchResultWithPostInfo{
IsOk: true,
Ok: PostDispatchInfo{
ActualWeight: NewOptionWeight(123),
ActualWeight: NewOption(testWeight),
PaysFee: Pays{
IsYes: true,
},
Expand All @@ -39,7 +39,7 @@ var (
IsError: true,
Error: DispatchErrorWithPostInfo{
PostInfo: PostDispatchInfo{
ActualWeight: NewOptionWeight(456),
ActualWeight: NewOption(testWeight),
PaysFee: Pays{
IsNo: true,
},
Expand All @@ -51,7 +51,7 @@ var (
}

dispatchResultWithPostInfoFuzzOpts = CombineFuzzOpts(
optionWeightFuzzOpts,
optionFuzzOpts,
paysFuzzOpts,
dispatchErrorFuzzOpts,
[]FuzzOpt{
Expand All @@ -77,14 +77,14 @@ func TestDispatchResultWithPostInfo_EncodeDecode(t *testing.T) {

func TestDispatchResultWithPostInfo_Encode(t *testing.T) {
AssertEncode(t, []EncodingAssert{
{testDispatchResultWithPostInfo1, MustHexDecodeString("0x00017b0000000000000000")},
{testDispatchResultWithPostInfo2, MustHexDecodeString("0x0101c8010000000000000100")},
{testDispatchResultWithPostInfo1, MustHexDecodeString("0x00012ce90900")},
{testDispatchResultWithPostInfo2, MustHexDecodeString("0x01012ce9090100")},
})
}

func TestDispatchResultWithPostInfo_Decode(t *testing.T) {
AssertDecode(t, []DecodingAssert{
{MustHexDecodeString("0x00017b0000000000000000"), testDispatchResultWithPostInfo1},
{MustHexDecodeString("0x0101c8010000000000000100"), testDispatchResultWithPostInfo2},
{MustHexDecodeString("0x00012ce90900"), testDispatchResultWithPostInfo1},
{MustHexDecodeString("0x01012ce9090100"), testDispatchResultWithPostInfo2},
})
}
28 changes: 14 additions & 14 deletions types/event_record_test.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion types/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (
func TestDispatchInfo_EncodeDecode(t *testing.T) {
AssertRoundTripFuzz[DispatchInfo](t, 100, dispatchInfoFuzzOpts...)
AssertDecodeNilData[DispatchInfo](t)
AssertEncodeEmptyObj[DispatchInfo](t, 8)
AssertEncodeEmptyObj[DispatchInfo](t, 2)
}

func TestVoteThreshold_Decoder(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions types/outcome_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
var (
testOutcome1 = Outcome{
IsComplete: true,
CompleteWeight: 123,
CompleteWeight: testWeight,
}
testOutcome2 = Outcome{
IsIncomplete: true,
IncompleteWeight: 54,
IncompleteWeight: testWeight,
IncompleteError: XCMError{
IsOverflow: true,
},
Expand Down Expand Up @@ -77,16 +77,16 @@ func TestOutcome_EncodeDecode(t *testing.T) {

func TestOutcome_Encode(t *testing.T) {
AssertEncode(t, []EncodingAssert{
{testOutcome1, MustHexDecodeString("0x007b00000000000000")},
{testOutcome2, MustHexDecodeString("0x01360000000000000000")},
{testOutcome1, MustHexDecodeString("0x002ce909")},
{testOutcome2, MustHexDecodeString("0x012ce90900")},
{testOutcome3, MustHexDecodeString("0x0201")},
})
}

func TestOutcome_Decode(t *testing.T) {
AssertDecode(t, []DecodingAssert{
{MustHexDecodeString("0x007b00000000000000"), testOutcome1},
{MustHexDecodeString("0x01360000000000000000"), testOutcome2},
{MustHexDecodeString("0x002ce909"), testOutcome1},
{MustHexDecodeString("0x012ce90900"), testOutcome2},
{MustHexDecodeString("0x0201"), testOutcome3},
})
}
61 changes: 10 additions & 51 deletions types/weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,18 @@

package types

import "github.com/centrifuge/go-substrate-rpc-client/v4/scale"

type OptionWeight struct {
option
value Weight
}

// Weight is a numeric range of a transaction weight
type Weight uint64

// NewWeight creates a new Weight type
func NewWeight(u uint64) Weight {
return Weight(u)
type Weight struct {
// The weight of computational time used based on some reference hardware.
RefTime UCompact
// The weight of storage space used by proof of validity.
ProofSize UCompact
}

func NewOptionWeight(value Weight) OptionWeight {
return OptionWeight{
option: option{
hasValue: true,
},
value: value,
}
}

func NewOptionWeightEmpty() OptionWeight {
return OptionWeight{
option: option{
hasValue: false,
},
// NewWeight creates a new Weight type
func NewWeight(refTime UCompact, proofSize UCompact) Weight {
return Weight{
RefTime: refTime,
ProofSize: proofSize,
}
}

func (o OptionWeight) Encode(encoder scale.Encoder) error {
return encoder.EncodeOption(o.hasValue, o.value)
}

func (o *OptionWeight) Decode(decoder scale.Decoder) error {
return decoder.DecodeOption(&o.hasValue, &o.value)
}

// SetSome sets a value
func (o *OptionWeight) SetSome(value Weight) {
o.hasValue = true
o.value = value
}

// SetNone removes a value and marks it as missing
func (o *OptionWeight) SetNone() {
o.hasValue = false
o.value = Weight(0)
}

// Unwrap returns a flag that indicates whether a value is present and the stored value
func (o *OptionWeight) Unwrap() (ok bool, value Weight) {
return o.hasValue, o.value
}
82 changes: 14 additions & 68 deletions types/weight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,102 +22,48 @@ import (
. "github.com/centrifuge/go-substrate-rpc-client/v4/types"
. "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
. "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
)

var (
optionWeightFuzzOpts = []FuzzOpt{
WithFuzzFuncs(func(o *OptionWeight, c fuzz.Continue) {
if c.RandBool() {
*o = NewOptionWeightEmpty()
return
}

var weight Weight

c.Fuzz(&weight)

*o = NewOptionWeight(weight)
}),
}
testWeight = NewWeight(NewUCompactFromUInt(11), NewUCompactFromUInt(634))
)

func TestOptionWeight_EncodeDecode(t *testing.T) {
AssertRoundTripFuzz[OptionWeight](t, 100, optionWeightFuzzOpts...)
AssertEncodeEmptyObj[OptionWeight](t, 1)
}

func TestOptionWeight_Encode(t *testing.T) {
AssertEncode(t, []EncodingAssert{
{NewOptionWeight(NewWeight(0)), MustHexDecodeString("0x010000000000000000")},
{NewOptionWeight(NewWeight(1)), MustHexDecodeString("0x010100000000000000")},
{NewOptionWeight(NewWeight(2)), MustHexDecodeString("0x010200000000000000")},
{NewOptionWeightEmpty(), MustHexDecodeString("0x00")},
})
}

func TestOptionWeight_Decode(t *testing.T) {
AssertDecode(t, []DecodingAssert{
{MustHexDecodeString("0x010000000000000000"), NewOptionWeight(NewWeight(0))},
{MustHexDecodeString("0x010100000000000000"), NewOptionWeight(NewWeight(1))},
{MustHexDecodeString("0x010200000000000000"), NewOptionWeight(NewWeight(2))},
{MustHexDecodeString("0x00"), NewOptionWeightEmpty()},
})
}

func TestOptionWeight_OptionMethods(t *testing.T) {
o := NewOptionWeightEmpty()
o.SetSome(Weight(11))

ok, v := o.Unwrap()
assert.True(t, ok)
assert.NotNil(t, v)

o.SetNone()

ok, v = o.Unwrap()
assert.False(t, ok)
assert.Equal(t, Weight(0), v)
}

func TestWeight_EncodeDecode(t *testing.T) {
AssertRoundTripFuzz[Weight](t, 100)
AssertDecodeNilData[Weight](t)
AssertEncodeEmptyObj[Weight](t, 8)
AssertEncodeEmptyObj[Weight](t, 2)
}

func TestWeight_EncodedLength(t *testing.T) {
AssertEncodedLength(t, []EncodedLengthAssert{{NewWeight(13), 8}})
AssertEncodedLength(t, []EncodedLengthAssert{{testWeight, 3}})
}

func TestWeight_Encode(t *testing.T) {
AssertEncode(t, []EncodingAssert{
{NewWeight(29), MustHexDecodeString("0x1d00000000000000")},
{testWeight, MustHexDecodeString("0x2ce909")},
})
}

func TestWeight_Decode(t *testing.T) {
AssertDecode(t, []DecodingAssert{
{MustHexDecodeString("0x2ce909"), testWeight},
})
}

func TestWeight_Hash(t *testing.T) {
AssertHash(t, []HashAssert{
{NewWeight(29), MustHexDecodeString("0x83e168a13a013e6d47b0778f046aaa05d6c01d6857d044d9e9b658a6d85eb865")},
{testWeight, MustHexDecodeString("0x7daf57922bb9694b4e29da7634e1b0a6af1477a8d13b0544208cda78331ea135")},
})
}

func TestWeight_Hex(t *testing.T) {
AssertEncodeToHex(t, []EncodeToHexAssert{
{NewWeight(29), "0x1d00000000000000"},
})
}

func TestWeight_String(t *testing.T) {
AssertString(t, []StringAssert{
{NewWeight(29), "29"},
{testWeight, "0x2ce909"},
})
}

func TestWeight_Eq(t *testing.T) {
AssertEq(t, []EqAssert{
{NewWeight(23), NewWeight(23), true},
{NewWeight(23), NewBool(false), false},
{testWeight, NewWeight(NewUCompactFromUInt(11), NewUCompactFromUInt(634)), true},
{testWeight, NewBool(false), false},
})
}

0 comments on commit 6b02d95

Please sign in to comment.