go-substrate-rpc-client icon indicating copy to clipboard operation
go-substrate-rpc-client copied to clipboard

Failed to decode System_ExtrinsicFailed event

Open ntchjb opened this issue 3 years ago • 2 comments
trafficstars

I was trying to decode events in a block at the number 10834783 and it failed. I found that it is because ModuleError type has Error field with U8 type. However, the metadata specifies that it has type [4]U8. So, is it safe now to change ModuleError.Error to the new type [4]U8?

Reproducible Code

package main

import (
	"fmt"

	gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
	"github.com/centrifuge/go-substrate-rpc-client/v4/types"
)

func main() {
	// Connect to Polkadot/Kasuma. Can be WebSocket (wss://) or HTTP(S) (https://)
	api, err := gsrpc.NewSubstrateAPI("wss://rpc.polkadot.io")
	if err != nil {
		panic(fmt.Errorf("unable to connect to RPC node: %w", err))
	}
	fmt.Println("Connected to RPC node.")

	// Get latest metadata
	meta, err := api.RPC.State.GetMetadataLatest()
	if err != nil {
		panic(fmt.Errorf("unable to get latest metadata: %w", err))
	}
	fmt.Println("Got metadata.")

	// Create storage key to query events from system pallet
	eventKey, err := types.CreateStorageKey(meta, "System", "Events")
	if err != nil {
		panic(fmt.Errorf("unable to create storage key: %w", err))
	}
	fmt.Printf("Generated event key: %#x\n", eventKey)

	blockHash, err := api.RPC.Chain.GetBlockHash(uint64(10834784))
	if err != nil {
		panic(fmt.Errorf("unable to get block hash by given block number: %w", err))
	}

	raw, err := api.RPC.State.GetStorageRaw(eventKey, blockHash)
	if err != nil {
		panic(fmt.Errorf("unable to query storage: %w", err))
	}

	var events types.EventRecords
	err = (*types.EventRecordsRaw)(raw).DecodeEventRecords(meta, &events)
	if err != nil {
		panic(fmt.Errorf("unable to decode event records: %w", err))
	}

	if len(events.Balances_Transfer) == 0 {
		fmt.Printf("No transfer at this block.\n")
	}
	for _, e := range events.Balances_Transfer {
		fmt.Printf("Transfer: %#x -> %#x: %v Planck\n", e.From, e.To, e.Value)
	}
}

Expected Result

Connected to RPC node.
Got metadata.
Generated event key: 0x26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7
Transfer: 0xe92b082f259d2f3f6ddd9a7d7a2feddd797017ebe5b0cb31a0c74955ea789c5b -> 0xeccde1e7551abd70a491ee0268f2ae351ec2c9af0c9600d25dc1df6cf3a85f31: 139051324200 Planck
Transfer: 0x68cfce325dd5035632ee2a1e664371b3b313ce1c9255eefa5de23eb39201704c -> 0x57680e93f9d60b9be427bd9f7c5b6afe6d3ad3d09372bde3103a6c2595a0685c: 199684999900 Planck
Transfer: 0x2f42611a3f142f2693d53062d78b5ef4c5334fe4feabf3cff4fc0f7183447dac -> 0xbd2918e36cd5a61729672b9704e376cb48f77ccf4cae6f4b59382014dce9264d: 251566574610 Planck
Transfer: 0x7168e82591356e1fdafb7887feec9c431466ab161fea276e97cd7ad098830755 -> 0xbd2918e36cd5a61729672b9704e376cb48f77ccf4cae6f4b59382014dce9264d: 204273199984 Planck
Transfer: 0x4b1b250917bea8fd29c8c03aa54b3bd6ac86ff4ed383ed72d5bcc03defd417c0 -> 0xbd2918e36cd5a61729672b9704e376cb48f77ccf4cae6f4b59382014dce9264d: 263793839984 Planck
Transfer: 0x487dfcf551799f68881c816a865481c263a6c21443118acb1cc6d6122288260e -> 0xd1009b8b6bf3d74140b12ea8f3b70ee97451352588bec22ca8644e9b20a32bd2: 1724900000000 Planck
Transfer: 0x8c86b7f9efd3b16317a7ff3c57f50a15ea4d99df2922c638764bac82023b2a53 -> 0x70617261d4070000000000000000000000000000000000000000000000000000: 2725000000000 Planck

Actual Result

Connected to RPC node.
Got metadata.
Generated event key: 0x26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7
panic: unable to decode event records: unable to decode field 2 event #56 with EventID [0 0], field System_ExtrinsicSuccess: expected more bytes, but could not decode any more

goroutine 1 [running]:
main.main()
        [redacted]/test-polkadot-rpc/main2.go:45 +0x404
exit status 2

ntchjb avatar Jun 21 '22 14:06 ntchjb

We're seeing a similar issue when using the client. Appears to be because we are on polkadot-v0.9.23, which has the 4 byte error type.

tjsharp1 avatar Aug 10 '22 17:08 tjsharp1

We encountered the same issue, we fixed it by forking and adding this: https://github.com/threefoldtech/go-substrate-rpc-client/commit/0abbabef7232b6af10b02d89f289a7b410b7d25b

DylanVerstraete avatar Aug 18 '22 14:08 DylanVerstraete

@ntchjb Can we somehow fix this on this upstream? @DylanVerstraete PR?

penumbra23 avatar Dec 07 '22 15:12 penumbra23

@penumbra23 I believe we fixed it already as part of this PR

I verified the code above and it works as expected. Can you pull the latest and try again?

mikiquantum avatar Dec 07 '22 22:12 mikiquantum

Ah, okay, it gives me errors for a custom event from our parachain. How can I extend the package with custom events?

penumbra23 avatar Dec 08 '22 10:12 penumbra23

Any progress on custom events? I got the same error.

yooml avatar Apr 03 '23 03:04 yooml

@yooml - we do have a WIP PR for decoding events based on metadata information here - https://github.com/centrifuge/go-substrate-rpc-client/pull/338

Also, did you get the same error while decoding the same event? Is it due to the module error that we defined statically?

cdamian avatar Apr 12 '23 11:04 cdamian

Closing this for now, please let us know if you still need help here.

cdamian avatar Dec 05 '23 12:12 cdamian