Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Voiculescu committed Aug 2, 2024
1 parent abdc44d commit e3cb158
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 22 deletions.
68 changes: 57 additions & 11 deletions block/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,30 @@ import (
"reflect"
"strings"

// "github.com/streamingfast/substreams-gear/generated/convert100"
// "github.com/streamingfast/substreams-gear/generated/convert1000"
// "github.com/streamingfast/substreams-gear/generated/convert1010"
// "github.com/streamingfast/substreams-gear/generated/convert1020"
// "github.com/streamingfast/substreams-gear/generated/convert1030"
// "github.com/streamingfast/substreams-gear/generated/convert1040"
// "github.com/streamingfast/substreams-gear/generated/convert1050"
// "github.com/streamingfast/substreams-gear/generated/convert1110"
// "github.com/streamingfast/substreams-gear/generated/convert120"
// "github.com/streamingfast/substreams-gear/generated/convert1200"
// "github.com/streamingfast/substreams-gear/generated/convert1210"
// "github.com/streamingfast/substreams-gear/generated/convert130"
// "github.com/streamingfast/substreams-gear/generated/convert1300"
// "github.com/streamingfast/substreams-gear/generated/convert1310"
// "github.com/streamingfast/substreams-gear/generated/convert140"
// "github.com/streamingfast/substreams-gear/generated/convert1400"
// "github.com/streamingfast/substreams-gear/generated/convert1410"
"github.com/streamingfast/substreams-gear/generated/convert1420"

Check failure on line 26 in block/decoder.go

View workflow job for this annotation

GitHub Actions / build (1.22.x)

github.com/streamingfast/[email protected]: replacement directory ../substreams-gear does not exist
// "github.com/streamingfast/substreams-gear/generated/convert210"
// "github.com/streamingfast/substreams-gear/generated/convert310"
// "github.com/streamingfast/substreams-gear/generated/convert320"
// "github.com/streamingfast/substreams-gear/generated/convert330"
// "github.com/streamingfast/substreams-gear/generated/convert340"
// "github.com/streamingfast/substreams-gear/generated/convert350"

"github.com/centrifuge/go-substrate-rpc-client/v4/registry"
"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
Expand All @@ -29,8 +52,7 @@ func NewDecoder(logger *zap.Logger) *Decoder {
}

func (d *Decoder) Decoded(block *pbgear.Block) (*v1.Block, error) {

decodedExtrinsics, err := decodeExtrinsics(d.callRegistry, block.Extrinsics, d.logger)
decodedExtrinsics, err := decodeExtrinsics("", d.callRegistry, block.Extrinsics, d.logger)
if err != nil {
return nil, fmt.Errorf("failed to decode extrinsics: %w", err)
}
Expand All @@ -51,6 +73,29 @@ var versionFuncMap map[string]map[string]reflect.Value
func init() {
versionFuncMap = map[string]map[string]reflect.Value{
"1420": convert1420.FuncMap,
// "1410": convert1410.FuncMap,
// "1400": convert1400.FuncMap,
// "1310": convert1310.FuncMap,
// "1300": convert1300.FuncMap,
// "1210": convert1210.FuncMap,
// "1200": convert1200.FuncMap,
// "1110": convert1110.FuncMap,
// "1050": convert1050.FuncMap,
// "1040": convert1040.FuncMap,
// "1030": convert1030.FuncMap,
// "1020": convert1020.FuncMap,
// "1010": convert1010.FuncMap,
// "1000": convert1000.FuncMap,
// "350": convert350.FuncMap,
// "340": convert340.FuncMap,
// "330": convert330.FuncMap,
// "320": convert320.FuncMap,
// "310": convert310.FuncMap,
// "210": convert210.FuncMap,
// "140": convert140.FuncMap,
// "130": convert130.FuncMap,
// "120": convert120.FuncMap,
// "100": convert100.FuncMap,
}
}

Expand All @@ -62,24 +107,25 @@ func decodeExtrinsics(version string, callRegistry registry.CallRegistry, extrin
if err != nil {
return nil, fmt.Errorf("failed to decode extrinsic: %w", err)
}
_ = decodedFields

parts := strings.Split(callName, ".")
pallet := parts[0]
call := parts[1]
call = stringy.New(call).PascalCase().Get()
structName := pallet + "_" + call + "Call"
funcName := "To_" + structName
_ = funcName
// funcMap := versionFuncMap[]
// if fn, found := funcMap[funcName]; found {
// o := fn.Call([]reflect.Value{reflect.ValueOf(decodedFields)})

funcMap := versionFuncMap[]
if fn, found := funcMap[funcName]; found {
o := fn.Call([]reflect.Value{reflect.ValueOf(decodedFields)})

e := o[0].Interface().(*v1.Extrinsic)
decodedExtrinsics = append(decodedExtrinsics, e)
// e := o[0].Interface().(*v1.Extrinsic)
// decodedExtrinsics = append(decodedExtrinsics, e)

} else {
panic(fmt.Sprintf("unknown extrinsic call: %s", callName))
}
// } else {
// panic(fmt.Sprintf("unknown extrinsic call: %s", callName))
// }
}
return decodedExtrinsics, nil
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/firevara/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ func decodeRunE(logger *zap.Logger, tracer logging.Tracer) func(cmd *cobra.Comma
if err != nil {
return fmt.Errorf("creating call registry: %w", err)
}
decoder.SetCallRegistry(callRegistry)
_ = callRegistry
// decoder.SetCallRegistry(callRegistry)
//todo: save has last metadata seen and reload it at startup
}

Expand Down
1 change: 1 addition & 0 deletions cmd/firevara/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func init() {

rootCmd.AddCommand(NewFetchCmd(logger, tracer))
rootCmd.AddCommand(NewDecoderCmd(logger, tracer))
rootCmd.AddCommand(NewToolsFetchMetadataCmd(logger, tracer))
}

func main() {
Expand Down
24 changes: 24 additions & 0 deletions cmd/firevara/tools-data/specVersions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
1420 0x68a1e81e50b4bd3c7bc35727eca22d31cd68808294d71863bbfac8da38514cf4
1410 0x1555a60f789e322c607f8b8df062cf0441484897b547028c378509d5e56eec48
1400 0x31087a240f444bba41423a348ba0b2b3d6c8709a1fb48c9991ee99db5671faa8
1310 0xeac85b1db33b3cec4881968faeb9cce085d97a2c060bfc2e32a10c49274f79dd
1300 0x6b897f4a6d02c821f828348fc69aa969a0ba227f521150b2dfa21b8e1a21c313
1210 0x78aea47e2ab73220763953dc97a6ab6c10397c56ca2eb1ce924048896f85a584
1200 0x4ac00153fb6791fb93df98350cd62e83d2429e31e65762868240b91e49cb4d1b
1110 0xcf6cb7499ca0377ebf3a87fdc3633543fd9f764ccc600fc277e8c90b750b9bfb
1050 0x7e04851c8c7557997555bab27af956281286298f56a13c97a1487111ce2f9980
1040 0x36e7e2d8445497e35e8107dbe1bea7bb636f79cc2ebd6328ad9c014433d1fad5
1030 0x0c105b5057e05cc7d4d4a58941f28862ba0ed9ea022594144d99caf0218bed08
1020 0x78bbb1728cc716e4cb612d2d14be9ec7542a61b09ebee81ca159497244b6ff70
1010 0xb16e35084b180070209b12128d5126bca6143f43aa297dc3b6e8d51bc03a2ffa
1000 0x4bf0df3d8ee84b880a1faed33d99eeecfa375327b30e64dea79e0316b1919929
350 0x9bb228a076f77c301a54ad9f547e0e7a29c280ad8d99dedf1806f192e69e119f
340 0x357058d8174950bd6e45cf3ee7c056167ff1669b8b5994345f5890b6b3565191
330 0x55963faecfdf0fce94fd07bb3f1d9c0559c4150a931f8eb324fb85ab9dfb7057
320 0x76f76204c862301bae704a9699ff55f69531bc1da0549fd1a012fdee2acc6682
310 0x2edd7b4291877cb5325646a9d0c6b39802455b795e5ca9626101aab7b701676a
210 0xafc8a27faf1ff457d3b5ecc3c1036f6898604288f23b824411a9a9722ff9b42f
140 0x0f7c32bcdfd6baef7235c81a2fbea55da03f78f115ad3a478bc51b5290c9461e
130 0x8f094a48f82634be2251522cc1c51e7a3ec678d2ad127dedd5b3342ec59254ba
120 0x001fc13165338f17a8e573721bda54283a6e8fefe0cd51950ba532cd81add499
100 0x564dbb453b7b15157486eeab4d0f70544f689c7ae19b6ff7e74fd6e0d79c9319
49 changes: 49 additions & 0 deletions cmd/firevara/tools_fetch_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"bufio"
"fmt"
"os"

"github.com/spf13/cobra"
firecore "github.com/streamingfast/firehose-core"
"github.com/streamingfast/logging"
"go.uber.org/zap"
)

func NewToolsFetchMetadataCmd(logger *zap.Logger, tracer logging.Tracer) *cobra.Command {
cmd := &cobra.Command{
Use: "tools-fetch-metadata",
Short: "Fetch all the metadata for Vara blockchain with all the specVersion defined in the file tools-data/specVersions.txt",
RunE: toolsFetchMetadataRunE(logger, tracer),
}

return cmd
}

func toolsFetchMetadataRunE(logger *zap.Logger, tracer logging.Tracer) firecore.CommandExecutor {
return func(cmd *cobra.Command, args []string) (err error) {
// curl --location 'https://vara-mainnet.public.blastapi.io' --header 'Content-Type: application/json' --data '{ "id": 1, "jsonrpc": "2.0", "method": "state_getMetadata", "params": ["0x68a1e81e50b4bd3c7bc35727eca22d31cd68808294d71863bbfac8da38514cf4"] }' | jq .result
filePath := "/Users/eduardvoiculescu/git/streamingfast/firehose-gear/cmd/firebara/tools-data/specVersions.txt"
readFile, err := os.Open(filePath)

if err != nil {
return fmt.Errorf("error opening file: %w", err)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
var fileLines []string

for fileScanner.Scan() {
fileLines = append(fileLines, fileScanner.Text())
}

readFile.Close()

for _, line := range fileLines {
fmt.Println(line)
}

return
}
}
19 changes: 9 additions & 10 deletions rpc/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type FetchedBlockData struct {
}

type LastBlockInfo struct {
blockNum uint64
blockHash types.Hash
specVersion uint32
blockNum uint64
blockHash types.Hash
specVersionHash string
}

type Fetcher struct {
Expand Down Expand Up @@ -72,7 +72,6 @@ func (f *Fetcher) Fetch(ctx context.Context, requestBlockNum uint64) (b *pbbstre
f.logger.Info("gear fetching block", zap.Uint64("block_num", requestBlockNum))

sleepDuration := time.Duration(0)
//TODO: move this logic in the firecore binary, as we do this in all the fetchers
for f.latestBlockNum < requestBlockNum {
time.Sleep(sleepDuration)

Expand All @@ -95,8 +94,8 @@ func (f *Fetcher) Fetch(ctx context.Context, requestBlockNum uint64) (b *pbbstre
return nil, false, fmt.Errorf("fetching block data: %w", err)
}

f.logger.Info("converting block", zap.Uint64("block_num", requestBlockNum), zap.Uint32("spec_version", f.lastBlockInfo.specVersion))
bstreamBlock, err := convertBlock(blockData, f.lastBlockInfo.specVersion)
f.logger.Info("converting block", zap.Uint64("block_num", requestBlockNum), zap.Uint32("spec_version", f.lastBlockInfo.specVersionHash))
bstreamBlock, err := convertBlock(blockData, f.lastBlockInfo.specVersionHash)
if err != nil {
f.logger.Warn("converting block", zap.Uint64("block_num", requestBlockNum), zap.Error(err))
return nil, false, fmt.Errorf("converting block %d from rpc response: %w", requestBlockNum, err)
Expand Down Expand Up @@ -153,7 +152,7 @@ func (f *Fetcher) fetchBlockData(_ context.Context, requestedBlockNum uint64) (*
shouldFetchParentVersion := true
if block.Block.Header.ParentHash == f.lastBlockInfo.blockHash {
shouldFetchParentVersion = false
parentSpecVersion = f.lastBlockInfo.specVersion
parentSpecVersion = f.lastBlockInfo.specVersionHash
}

if requestedBlockNum == 0 {
Expand Down Expand Up @@ -193,9 +192,9 @@ func (f *Fetcher) fetchBlockData(_ context.Context, requestedBlockNum uint64) (*
}

f.lastBlockInfo = &LastBlockInfo{
blockNum: requestedBlockNum,
blockHash: blockHash,
specVersion: requestedBlockSpecVersion,
blockNum: requestedBlockNum,
blockHash: blockHash,
specVersionHash: requestedBlockSpecVersion,
}

return fetchedBlockData, nil
Expand Down

0 comments on commit e3cb158

Please sign in to comment.