Skip to content

Commit 6b02d95

Browse files
authored
types: Update the dispatch info weight (#323)
1 parent 8b830c5 commit 6b02d95

8 files changed

Lines changed: 54 additions & 149 deletions

types/account_data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type AccountInfo struct {
2222
Consumers U32
2323
Providers U32
2424
Sufficients U32
25-
Data struct {
25+
Data struct {
2626
Free U128
2727
Reserved U128
2828
MiscFrozen U128

types/dispatch_result_with_post_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import "github.com/centrifuge/go-substrate-rpc-client/v4/scale"
2121
// PostDispatchInfo is used in DispatchResultWithPostInfo.
2222
// Weight information that is only available post dispatch.
2323
type PostDispatchInfo struct {
24-
ActualWeight OptionWeight
24+
ActualWeight Option[Weight]
2525
PaysFee Pays
2626
}
2727

types/dispatch_result_with_post_info_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var (
2929
testDispatchResultWithPostInfo1 = DispatchResultWithPostInfo{
3030
IsOk: true,
3131
Ok: PostDispatchInfo{
32-
ActualWeight: NewOptionWeight(123),
32+
ActualWeight: NewOption(testWeight),
3333
PaysFee: Pays{
3434
IsYes: true,
3535
},
@@ -39,7 +39,7 @@ var (
3939
IsError: true,
4040
Error: DispatchErrorWithPostInfo{
4141
PostInfo: PostDispatchInfo{
42-
ActualWeight: NewOptionWeight(456),
42+
ActualWeight: NewOption(testWeight),
4343
PaysFee: Pays{
4444
IsNo: true,
4545
},
@@ -51,7 +51,7 @@ var (
5151
}
5252

5353
dispatchResultWithPostInfoFuzzOpts = CombineFuzzOpts(
54-
optionWeightFuzzOpts,
54+
optionFuzzOpts,
5555
paysFuzzOpts,
5656
dispatchErrorFuzzOpts,
5757
[]FuzzOpt{
@@ -77,14 +77,14 @@ func TestDispatchResultWithPostInfo_EncodeDecode(t *testing.T) {
7777

7878
func TestDispatchResultWithPostInfo_Encode(t *testing.T) {
7979
AssertEncode(t, []EncodingAssert{
80-
{testDispatchResultWithPostInfo1, MustHexDecodeString("0x00017b0000000000000000")},
81-
{testDispatchResultWithPostInfo2, MustHexDecodeString("0x0101c8010000000000000100")},
80+
{testDispatchResultWithPostInfo1, MustHexDecodeString("0x00012ce90900")},
81+
{testDispatchResultWithPostInfo2, MustHexDecodeString("0x01012ce9090100")},
8282
})
8383
}
8484

8585
func TestDispatchResultWithPostInfo_Decode(t *testing.T) {
8686
AssertDecode(t, []DecodingAssert{
87-
{MustHexDecodeString("0x00017b0000000000000000"), testDispatchResultWithPostInfo1},
88-
{MustHexDecodeString("0x0101c8010000000000000100"), testDispatchResultWithPostInfo2},
87+
{MustHexDecodeString("0x00012ce90900"), testDispatchResultWithPostInfo1},
88+
{MustHexDecodeString("0x01012ce9090100"), testDispatchResultWithPostInfo2},
8989
})
9090
}

types/event_record_test.go

Lines changed: 14 additions & 14 deletions
Large diffs are not rendered by default.

types/events_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
func TestDispatchInfo_EncodeDecode(t *testing.T) {
3838
AssertRoundTripFuzz[DispatchInfo](t, 100, dispatchInfoFuzzOpts...)
3939
AssertDecodeNilData[DispatchInfo](t)
40-
AssertEncodeEmptyObj[DispatchInfo](t, 8)
40+
AssertEncodeEmptyObj[DispatchInfo](t, 2)
4141
}
4242

4343
func TestVoteThreshold_Decoder(t *testing.T) {

types/outcome_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import (
2828
var (
2929
testOutcome1 = Outcome{
3030
IsComplete: true,
31-
CompleteWeight: 123,
31+
CompleteWeight: testWeight,
3232
}
3333
testOutcome2 = Outcome{
3434
IsIncomplete: true,
35-
IncompleteWeight: 54,
35+
IncompleteWeight: testWeight,
3636
IncompleteError: XCMError{
3737
IsOverflow: true,
3838
},
@@ -77,16 +77,16 @@ func TestOutcome_EncodeDecode(t *testing.T) {
7777

7878
func TestOutcome_Encode(t *testing.T) {
7979
AssertEncode(t, []EncodingAssert{
80-
{testOutcome1, MustHexDecodeString("0x007b00000000000000")},
81-
{testOutcome2, MustHexDecodeString("0x01360000000000000000")},
80+
{testOutcome1, MustHexDecodeString("0x002ce909")},
81+
{testOutcome2, MustHexDecodeString("0x012ce90900")},
8282
{testOutcome3, MustHexDecodeString("0x0201")},
8383
})
8484
}
8585

8686
func TestOutcome_Decode(t *testing.T) {
8787
AssertDecode(t, []DecodingAssert{
88-
{MustHexDecodeString("0x007b00000000000000"), testOutcome1},
89-
{MustHexDecodeString("0x01360000000000000000"), testOutcome2},
88+
{MustHexDecodeString("0x002ce909"), testOutcome1},
89+
{MustHexDecodeString("0x012ce90900"), testOutcome2},
9090
{MustHexDecodeString("0x0201"), testOutcome3},
9191
})
9292
}

types/weight.go

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,18 @@
1616

1717
package types
1818

19-
import "github.com/centrifuge/go-substrate-rpc-client/v4/scale"
20-
21-
type OptionWeight struct {
22-
option
23-
value Weight
24-
}
25-
2619
// Weight is a numeric range of a transaction weight
27-
type Weight uint64
28-
29-
// NewWeight creates a new Weight type
30-
func NewWeight(u uint64) Weight {
31-
return Weight(u)
20+
type Weight struct {
21+
// The weight of computational time used based on some reference hardware.
22+
RefTime UCompact
23+
// The weight of storage space used by proof of validity.
24+
ProofSize UCompact
3225
}
3326

34-
func NewOptionWeight(value Weight) OptionWeight {
35-
return OptionWeight{
36-
option: option{
37-
hasValue: true,
38-
},
39-
value: value,
40-
}
41-
}
42-
43-
func NewOptionWeightEmpty() OptionWeight {
44-
return OptionWeight{
45-
option: option{
46-
hasValue: false,
47-
},
27+
// NewWeight creates a new Weight type
28+
func NewWeight(refTime UCompact, proofSize UCompact) Weight {
29+
return Weight{
30+
RefTime: refTime,
31+
ProofSize: proofSize,
4832
}
4933
}
50-
51-
func (o OptionWeight) Encode(encoder scale.Encoder) error {
52-
return encoder.EncodeOption(o.hasValue, o.value)
53-
}
54-
55-
func (o *OptionWeight) Decode(decoder scale.Decoder) error {
56-
return decoder.DecodeOption(&o.hasValue, &o.value)
57-
}
58-
59-
// SetSome sets a value
60-
func (o *OptionWeight) SetSome(value Weight) {
61-
o.hasValue = true
62-
o.value = value
63-
}
64-
65-
// SetNone removes a value and marks it as missing
66-
func (o *OptionWeight) SetNone() {
67-
o.hasValue = false
68-
o.value = Weight(0)
69-
}
70-
71-
// Unwrap returns a flag that indicates whether a value is present and the stored value
72-
func (o *OptionWeight) Unwrap() (ok bool, value Weight) {
73-
return o.hasValue, o.value
74-
}

types/weight_test.go

Lines changed: 14 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -22,102 +22,48 @@ import (
2222
. "github.com/centrifuge/go-substrate-rpc-client/v4/types"
2323
. "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
2424
. "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils"
25-
fuzz "github.com/google/gofuzz"
26-
"github.com/stretchr/testify/assert"
2725
)
2826

2927
var (
30-
optionWeightFuzzOpts = []FuzzOpt{
31-
WithFuzzFuncs(func(o *OptionWeight, c fuzz.Continue) {
32-
if c.RandBool() {
33-
*o = NewOptionWeightEmpty()
34-
return
35-
}
36-
37-
var weight Weight
38-
39-
c.Fuzz(&weight)
40-
41-
*o = NewOptionWeight(weight)
42-
}),
43-
}
28+
testWeight = NewWeight(NewUCompactFromUInt(11), NewUCompactFromUInt(634))
4429
)
4530

46-
func TestOptionWeight_EncodeDecode(t *testing.T) {
47-
AssertRoundTripFuzz[OptionWeight](t, 100, optionWeightFuzzOpts...)
48-
AssertEncodeEmptyObj[OptionWeight](t, 1)
49-
}
50-
51-
func TestOptionWeight_Encode(t *testing.T) {
52-
AssertEncode(t, []EncodingAssert{
53-
{NewOptionWeight(NewWeight(0)), MustHexDecodeString("0x010000000000000000")},
54-
{NewOptionWeight(NewWeight(1)), MustHexDecodeString("0x010100000000000000")},
55-
{NewOptionWeight(NewWeight(2)), MustHexDecodeString("0x010200000000000000")},
56-
{NewOptionWeightEmpty(), MustHexDecodeString("0x00")},
57-
})
58-
}
59-
60-
func TestOptionWeight_Decode(t *testing.T) {
61-
AssertDecode(t, []DecodingAssert{
62-
{MustHexDecodeString("0x010000000000000000"), NewOptionWeight(NewWeight(0))},
63-
{MustHexDecodeString("0x010100000000000000"), NewOptionWeight(NewWeight(1))},
64-
{MustHexDecodeString("0x010200000000000000"), NewOptionWeight(NewWeight(2))},
65-
{MustHexDecodeString("0x00"), NewOptionWeightEmpty()},
66-
})
67-
}
68-
69-
func TestOptionWeight_OptionMethods(t *testing.T) {
70-
o := NewOptionWeightEmpty()
71-
o.SetSome(Weight(11))
72-
73-
ok, v := o.Unwrap()
74-
assert.True(t, ok)
75-
assert.NotNil(t, v)
76-
77-
o.SetNone()
78-
79-
ok, v = o.Unwrap()
80-
assert.False(t, ok)
81-
assert.Equal(t, Weight(0), v)
82-
}
83-
8431
func TestWeight_EncodeDecode(t *testing.T) {
8532
AssertRoundTripFuzz[Weight](t, 100)
86-
AssertDecodeNilData[Weight](t)
87-
AssertEncodeEmptyObj[Weight](t, 8)
33+
AssertEncodeEmptyObj[Weight](t, 2)
8834
}
8935

9036
func TestWeight_EncodedLength(t *testing.T) {
91-
AssertEncodedLength(t, []EncodedLengthAssert{{NewWeight(13), 8}})
37+
AssertEncodedLength(t, []EncodedLengthAssert{{testWeight, 3}})
9238
}
9339

9440
func TestWeight_Encode(t *testing.T) {
9541
AssertEncode(t, []EncodingAssert{
96-
{NewWeight(29), MustHexDecodeString("0x1d00000000000000")},
42+
{testWeight, MustHexDecodeString("0x2ce909")},
43+
})
44+
}
45+
46+
func TestWeight_Decode(t *testing.T) {
47+
AssertDecode(t, []DecodingAssert{
48+
{MustHexDecodeString("0x2ce909"), testWeight},
9749
})
9850
}
9951

10052
func TestWeight_Hash(t *testing.T) {
10153
AssertHash(t, []HashAssert{
102-
{NewWeight(29), MustHexDecodeString("0x83e168a13a013e6d47b0778f046aaa05d6c01d6857d044d9e9b658a6d85eb865")},
54+
{testWeight, MustHexDecodeString("0x7daf57922bb9694b4e29da7634e1b0a6af1477a8d13b0544208cda78331ea135")},
10355
})
10456
}
10557

10658
func TestWeight_Hex(t *testing.T) {
10759
AssertEncodeToHex(t, []EncodeToHexAssert{
108-
{NewWeight(29), "0x1d00000000000000"},
109-
})
110-
}
111-
112-
func TestWeight_String(t *testing.T) {
113-
AssertString(t, []StringAssert{
114-
{NewWeight(29), "29"},
60+
{testWeight, "0x2ce909"},
11561
})
11662
}
11763

11864
func TestWeight_Eq(t *testing.T) {
11965
AssertEq(t, []EqAssert{
120-
{NewWeight(23), NewWeight(23), true},
121-
{NewWeight(23), NewBool(false), false},
66+
{testWeight, NewWeight(NewUCompactFromUInt(11), NewUCompactFromUInt(634)), true},
67+
{testWeight, NewBool(false), false},
12268
})
12369
}

0 commit comments

Comments
 (0)