Skip to content

Commit 1a8ff1e

Browse files
Hotfix: Event record raw decoding naming conflict, decoding failed
1 parent 6fb88e3 commit 1a8ff1e

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

rpc/state/state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ var mockSrv = MockSrv{
151151
storageDataHex: "0xb82d895d00000000",
152152
storageSize: 926778,
153153
storageHashHex: "0xdf0e877ee1cb973b9a566f53707d365b269d7131b55e65b9790994e4e63b95e1",
154-
childStorageKeyHex: "0x3a6368696c645f73746f726167653a64656661756c743a05470000", //nolint:lll beginning with hex encoded `:child_storage:` as per https://github.com/paritytech/substrate/blob/master/core/primitives/storage/src/lib.rs#L71
154+
childStorageKeyHex: "0x3a6368696c645f73746f726167653a64656661756c743a05470000", //nolint:lll // beginning with hex encoded `:child_storage:` as per https://github.com/paritytech/substrate/blob/master/core/primitives/storage/src/lib.rs#L71
155155
childStorageTrieKeyHex: "0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d0",
156156
childStorageTrieValueHex: "0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d09c1705d98d059a2d7f5faa89277ee5d0a38cc455f8b5fdf38fda471e988cb8a921000000", //nolint:lll
157157
childStorageTrieValue: ChildStorageTrieTestVal{

types/event_record.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"errors"
2222
"fmt"
23+
"io"
2324
"reflect"
2425

2526
"github.com/centrifuge/go-substrate-rpc-client/scale"
@@ -28,8 +29,31 @@ import (
2829
// EventRecordsRaw is a raw record for a set of events, represented as the raw bytes. It exists since
2930
// decoding of events can only be done with metadata, so events can't follow the static way of decoding
3031
// other types do. It exposes functions to decode events using metadata and targets.
32+
// Be careful using this in your own structs – it only works as the last value in a struct since it will consume the
33+
// remainder of the encoded data. The reason for this is that it does not contain any length encoding, so it would
34+
// not know where to stop.
3135
type EventRecordsRaw []byte
3236

37+
// Encode implements encoding for Data, which just unwraps the bytes of Data
38+
func (e EventRecordsRaw) Encode(encoder scale.Encoder) error {
39+
return encoder.Write(e)
40+
}
41+
42+
// Decode implements decoding for Data, which just reads all the remaining bytes into Data
43+
func (e *EventRecordsRaw) Decode(decoder scale.Decoder) error {
44+
for i := 0; true; i++ {
45+
b, err := decoder.ReadOneByte()
46+
if err == io.EOF {
47+
break
48+
}
49+
if err != nil {
50+
return err
51+
}
52+
*e = append((*e)[:i], b)
53+
}
54+
return nil
55+
}
56+
3357
type EventSystemExtrinsicSuccess struct {
3458
Phase Phase
3559
Topics []Hash
@@ -83,8 +107,8 @@ type EventRecords struct {
83107
Session_NewSession []EventSessionNewSession //nolint:stylecheck,golint
84108
}
85109

86-
// Decode decodes the events from an EventRecordRaw into a target t using the given Metadata m
87-
func (e EventRecordsRaw) Decode(m *Metadata, t interface{}) error {
110+
// DecodeEventRecords decodes the events records from an EventRecordRaw into a target t using the given Metadata m
111+
func (e EventRecordsRaw) DecodeEventRecords(m *Metadata, t interface{}) error {
88112
// ensure t is a pointer
89113
ttyp := reflect.TypeOf(t)
90114
if ttyp.Kind() != reflect.Ptr {

types/event_record_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func ExampleEventRecordsRaw_Decode() {
7676
e := EventRecordsRaw(MustHexDecodeString("0x100000000000000000000100000000000000020000000302d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48266d00000000000000000000000000000010a5d4e80000000000000000000000000002000000000000")) //nolint:lll
7777

7878
events := EventRecords{}
79-
err := e.Decode(ExamplaryMetadataV8, &events)
79+
err := e.DecodeEventRecords(ExamplaryMetadataV8, &events)
8080
if err != nil {
8181
panic(err)
8282
}

0 commit comments

Comments
 (0)