-
Notifications
You must be signed in to change notification settings - Fork 0
/
substrate.go
82 lines (69 loc) · 1.79 KB
/
substrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package substrate
import (
"strconv"
"time"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/metrics"
)
type Client struct {
// w *wallet.Key
api *gsrpc.SubstrateAPI
vu modules.VU
metrics ethMetrics
}
func (c *Client) Exports() modules.Exports {
return modules.Exports{}
}
// GetBlockHashLatests returns the current block number.
func (c *Client) GetBlockHashLatest() (types.Hash, error) {
return c.api.RPC.Chain.GetBlockHashLatest()
}
// GetBlock returns the block for the given block hash.
func (c *Client) GetBlock(hash types.Hash) (*types.SignedBlock, error) {
return c.api.RPC.Chain.GetBlock(hash)
}
// PollBlocks polls for new blocks and emits a "block" metric.
func (c *Client) subscribeNewHeads() {
sub, err := c.api.RPC.Chain.SubscribeNewHeads()
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
for {
now := time.Now()
head := <-sub.Chan()
d := time.Since(now)
bh, err := c.api.RPC.Chain.GetBlockHash(uint64(head.Number))
if err != nil {
panic(err)
}
block, err := c.api.RPC.Chain.GetBlock(bh)
if err != nil {
panic(err)
}
// Compute TPS
tps := float64(len(block.Block.Extrinsics)) / d.Seconds()
if c.vu.Context() != nil {
metrics.PushIfNotDone(c.vu.Context(), c.vu.State().Samples, metrics.ConnectedSamples{
Samples: []metrics.Sample{
{
Metric: c.metrics.Block,
Tags: metrics.NewSampleTags(map[string]string{
"extrinsics": strconv.Itoa(len(block.Block.Extrinsics)),
}),
Value: float64(head.Number),
Time: time.Now(),
},
{
Metric: c.metrics.TPS,
// Tags: metrics.NewSampleTags(map[string]string{}),
Value: tps,
Time: time.Now(),
},
},
})
}
}
}