Skip to content

Commit d3303c3

Browse files
RuntimeVersion and GenesisHash are now fetched at the beginning instead on every transaction execution.
1 parent e8b4fe6 commit d3303c3

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

documentation/src/installation.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ go get github.com/availproject/[email protected]
3535
```bash
3636
# Creates a new project with name myproject
3737
go mod init myproject
38-
# Fetches Avail-GO SDK v0.2.0. This might not be the newest version so make sure to check out the latest github avail-go-sdk release.
38+
# Fetches Avail-GO SDK v0.2.1. This might not be the newest version so make sure to check out the latest github avail-go-sdk release.
3939
# Link to Github: https://github.com/availproject/avail-go-sdk/releases
40-
go get github.com/availproject/[email protected].0
40+
go get github.com/availproject/[email protected].1
4141
```
4242

4343
#### First Time Running

sdk/client.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import (
1616
)
1717

1818
type Client struct {
19-
client *http.Client
20-
endpoint string
21-
metadata *metadata.Metadata
22-
Rpc RPC
23-
Call RuntimeApi
19+
client *http.Client
20+
endpoint string
21+
metadata *metadata.Metadata
22+
Rpc RPC
23+
Call RuntimeApi
24+
RuntimeVersion *prim.RuntimeVersion
25+
GenesisHash *prim.H256
2426
}
2527

2628
func NewClient(endpoint string) *Client {
@@ -119,6 +121,22 @@ func (this *Client) InitMetadata(at prim.Option[prim.H256]) error {
119121
return nil
120122
}
121123

124+
func (this *Client) InitRuntimeVersion(at prim.Option[prim.H256]) error {
125+
runtimeVersion, err := this.Rpc.State.GetRuntimeVersion(at)
126+
if err != nil {
127+
return err
128+
}
129+
this.RuntimeVersion = &runtimeVersion
130+
131+
genesisHash, err := this.Rpc.ChainSpec.V1GenesisHash()
132+
if err != nil {
133+
return err
134+
}
135+
this.GenesisHash = &genesisHash
136+
137+
return nil
138+
}
139+
122140
func (this *Client) RequestWithRetry(method string, params string) (string, error) {
123141
retryCount := 3
124142
for {

sdk/sdk.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ type SDK struct {
1818
Tx transactions
1919
}
2020

21-
func (this *SDK) UpdateMetadata(blockHash prim.Option[prim.H256]) error {
22-
return this.Client.InitMetadata(blockHash)
23-
}
24-
2521
func EnableLogging() {
2622
// Set log level based on the environment variable
2723
level, err := logrus.ParseLevel(os.Getenv("LOG_LEVEL"))
@@ -52,6 +48,11 @@ func NewSDKWithMetadata(endpoint string, metadataBlockHash prim.Option[prim.H256
5248
if err := client.InitMetadata(metadataBlockHash); err != nil {
5349
return SDK{}, err
5450
}
51+
52+
if err := client.InitRuntimeVersion(metadataBlockHash); err != nil {
53+
return SDK{}, err
54+
}
55+
5556
return SDK{
5657
Client: client,
5758
Tx: newTransactions(client),

sdk/transaction_options.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ func (this TransactionOptions) WithTip(value metadata.Balance) TransactionOption
4444
}
4545

4646
func (this *TransactionOptions) ToPrimitive(client *Client, accountAddress string) (prim.Extra, prim.Additional, error) {
47-
genesisHash, err := client.Rpc.ChainSpec.V1GenesisHash()
48-
if err != nil {
49-
return prim.Extra{}, prim.Additional{}, err
50-
}
5147
forkHash, err := client.Rpc.Chain.GetFinalizedHead()
5248
if err != nil {
5349
return prim.Extra{}, prim.Additional{}, err
@@ -58,15 +54,10 @@ func (this *TransactionOptions) ToPrimitive(client *Client, accountAddress strin
5854
}
5955
forkBlockNumber := header.Number
6056

61-
runtimeVersion, err := client.Rpc.State.GetRuntimeVersion(prim.None[prim.H256]())
62-
if err != nil {
63-
return prim.Extra{}, prim.Additional{}, err
64-
}
65-
6657
additional := prim.Additional{
67-
SpecVersion: runtimeVersion.SpecVersion,
68-
TxVersion: runtimeVersion.TxVersion,
69-
GenesisHash: genesisHash,
58+
SpecVersion: client.RuntimeVersion.SpecVersion,
59+
TxVersion: client.RuntimeVersion.TxVersion,
60+
GenesisHash: *client.GenesisHash,
7061
ForkHash: forkHash,
7162
}
7263

0 commit comments

Comments
 (0)