Skip to content

Commit

Permalink
Add dynamic extrinsic signing support (#389)
Browse files Browse the repository at this point in the history
* extrinsics: Add dynamic types for extrinsics, add new rpc endpoint

* extrinsic: Add errors to validation funcs

* extrinsic: Generate extrinsic payload and signature based on metadata

* rpc: Extract submit and watch func

* extrinsic: Add more comments

* extrinsic: Add support for AssetID signed field.

* extrinsic: Add support for PrevalidateAttests signed extension

* extrinsic: Add support for CheckNetworkMembership signed extra

* chore: Update encode/decode tests for types, fix imports format

* test: Adjust APIs, registry types, and tests

* rpc: Re-add pending extrinsics retrieval

* lib: Add extrinsic decoder, adjust tests, types, and rpc

* registry: Add extra check for generic extrinsic type, add test

* extrinsic: Add more tests

* lib: Drop old metadata examples, fix linter

* CI: Install docker-compose

* tests: Fix state tests

* registry: Remove unused Acala signature struct
  • Loading branch information
cdamian authored Sep 19, 2024
1 parent 9d7be34 commit e3b9385
Show file tree
Hide file tree
Showing 93 changed files with 3,825 additions and 7,039 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ jobs:
uses: actions/[email protected]
with:
fetch-depth: 0
- name: Install compose
run: |
sudo curl -SL https://github.com/docker/compose/releases/download/v2.29.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
sudo chmod +x /usr/bin/docker-compose
docker compose version
- name: Run tests
run: make test-dockerized
- name: Upload coverage
Expand Down
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,10 @@ test-dockerized: run-substrate-docker
run-substrate-docker: ## starts the Substrate Docker image
@docker-compose up -d substrate

generate-test-data: ## generate data for types decode test
@go generate -tags=types_test ./types/test/...

test-types-decode: ## run tests for types decode
@go test -tags=types_test ./types/test/...

generate-mocks: ## generate mocks
@docker run -v `pwd`:/app -w /app --entrypoint /bin/sh vektra/mockery:v2.13.0-beta.1 -c 'go generate ./...'

help: ## shows this help
@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST)

.PHONY: install-deps lint lint-fix test test-cover test-dockerized run-substrate-docker clean generate-test-data
.PHONY: clean lint lint-fix test test-cover test-dockerized run-substrate-docker generate-mocks
42 changes: 19 additions & 23 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package gsrpc_test

import (
"fmt"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/extrinsic"
"math/big"
"time"

Expand Down Expand Up @@ -215,7 +216,7 @@ func Example_makeASimpleTransfer() {
}

// Create the extrinsic
ext := types.NewExtrinsic(c)
ext := extrinsic.NewExtrinsic(c)

genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
Expand All @@ -239,18 +240,16 @@ func Example_makeASimpleTransfer() {
}

nonce := uint32(accountInfo.Nonce)
o := types.SignatureOptions{
BlockHash: genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
SpecVersion: rv.SpecVersion,
Tip: types.NewUCompactFromUInt(100),
TransactionVersion: rv.TransactionVersion,
}

// Sign the transaction using Alice's default account
err = ext.Sign(signature.TestKeyringPairAlice, o)
err = ext.Sign(signature.TestKeyringPairAlice, meta, extrinsic.WithEra(types.ExtrinsicEra{IsImmortalEra: true}, genesisHash),
extrinsic.WithNonce(types.NewUCompactFromUInt(uint64(nonce))),
extrinsic.WithTip(types.NewUCompactFromUInt(0)),
extrinsic.WithSpecVersion(rv.SpecVersion),
extrinsic.WithTransactionVersion(rv.TransactionVersion),
extrinsic.WithGenesisHash(genesisHash),
)

if err != nil {
panic(err)
}
Expand Down Expand Up @@ -428,7 +427,7 @@ func Example_transactionWithEvents() {
}

// Create the extrinsic
ext := types.NewExtrinsic(c)
ext := extrinsic.NewExtrinsic(c)

genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
Expand All @@ -454,20 +453,17 @@ func Example_transactionWithEvents() {

nonce := uint32(accountInfo.Nonce)

o := types.SignatureOptions{
BlockHash: genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
SpecVersion: rv.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: rv.TransactionVersion,
}

fmt.Printf("Sending %v from %#x to %#x with nonce %v", amount, signature.TestKeyringPairAlice.PublicKey, bob.AsAccountID, nonce)

// Sign the transaction using Alice's default account
err = ext.Sign(signature.TestKeyringPairAlice, o)
err = ext.Sign(signature.TestKeyringPairAlice, meta, extrinsic.WithEra(types.ExtrinsicEra{IsImmortalEra: true}, genesisHash),
extrinsic.WithNonce(types.NewUCompactFromUInt(uint64(nonce))),
extrinsic.WithTip(types.NewUCompactFromUInt(0)),
extrinsic.WithSpecVersion(rv.SpecVersion),
extrinsic.WithTransactionVersion(rv.TransactionVersion),
extrinsic.WithGenesisHash(genesisHash),
)

if err != nil {
panic(err)
}
Expand Down
14 changes: 5 additions & 9 deletions registry/REGISTRY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ By leveraging the on-chain metadata, GSRPC is more robust to changes on types, a

This registry can be used afterwards to decode data read from live chains (events & extrinsics).

## How to parse events and its types
## How to parse events and their fields
First we instantiate the API with the client node and open a connection:
```go
testURL := "wss://fullnode.parachain.centrifuge.io" // Your endpoint
Expand Down Expand Up @@ -68,16 +68,12 @@ for _, event := range events {

## Extended Usage
Since docs get outdated fairly quick, here are links to tests that will always be up-to-date.
### Populate Call, Error & Events Registries
[Browse me](registry_test.go)
### Populate Call, Error & Events Registries, Extrinsic Decoder
[Factory tests](factory_test.go)
[Decoder tests](decoder_test.go)

### Event retriever
[TestLive_EventRetriever_GetEvents](retriever/event_retriever_live_test.go)
### Extrinsic retriever
Since chain runtimes can be customized, modifying core types such as Accounts, Signature payloads or Payment payloads, the code supports a customizable way of passing those custom types to the extrinsic retriever.

On the other hand, since a great majority of chains do not need to change these types, the tool provides a default for the most common used ones.
#### Using Chain Defaults
[TestExtrinsicRetriever_NewDefault](retriever/extrinsic_retriever_test.go#L179)
#### Using Custom core types
### Extrinsic retriever
[TestLive_ExtrinsicRetriever_GetExtrinsics](retriever/extrinsic_retriever_live_test.go)
Loading

0 comments on commit e3b9385

Please sign in to comment.