Skip to content

Commit

Permalink
create substream package in wasm wasi
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Dec 22, 2023
1 parent 01a2995 commit d4d8ff4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
42 changes: 3 additions & 39 deletions wasm/bench/substreams_wasi_go/main.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
//go:build wasip1

package main

import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"os"
"strings"
"time"

"github.com/streamingfast/substreams/wasm/bench/substreams_wasi_go/pb"
"github.com/streamingfast/substreams/wasm/wasi/substream"
)

func main() {
start := time.Now()
log.Print("let's do it")
log.Print("start: ", start)

input, err := readInput()
input, err := substream.ReadInput()
if err != nil {
panic(fmt.Errorf("reading input: %w", err))
}
Expand Down Expand Up @@ -69,28 +67,6 @@ func main() {
log.Print("total duration: ", time.Since(start))
}

func readAll(r io.Reader) ([]byte, error) {
b := make([]byte, 0, 1024*1024)
count := 0
for {
count++
n, err := r.Read(b[len(b):cap(b)])
log.Print("Read count: ", count)
b = b[:len(b)+n]
if err != nil {
if err == io.EOF {
err = nil
}
return b, err
}

if len(b) == cap(b) {
// Add more capacity (let append pick how much).
b = append(b, 0)[:len(b)]
}
}
}

type blockStat struct {
TrxCount int
TransferCount int
Expand Down Expand Up @@ -141,21 +117,9 @@ func mapBlock(block *pb.Block) error {
if err != nil {
return fmt.Errorf("marshalling stats: %w", err)
}
_, err = writeOutput(data)
_, err = substream.WriteOutput(data)
if err != nil {
return fmt.Errorf("writing output: %w", err)
}
return nil
}

func writeOutput(data []byte) (int, error) {
return os.Stdout.Write(data)
}

func readInput() ([]byte, error) {
return readAll(os.Stdin)
}

func readFile(path string) ([]byte, error) {
return os.ReadFile(path)
}
Binary file modified wasm/bench/substreams_wasi_go/main.wasm
Binary file not shown.
41 changes: 41 additions & 0 deletions wasm/wasi/substream/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package substream

import (
"io"
"log"
"os"
)

func ReadAll(r io.Reader) ([]byte, error) {
b := make([]byte, 0, 1024*1024)
count := 0
for {
count++
n, err := r.Read(b[len(b):cap(b)])
log.Print("Read count: ", count)
b = b[:len(b)+n]
if err != nil {
if err == io.EOF {
err = nil
}
return b, err
}

if len(b) == cap(b) {
// Add more capacity (let append pick how much).
b = append(b, 0)[:len(b)]
}
}
}

func WriteOutput(data []byte) (int, error) {
return os.Stdout.Write(data)
}

func ReadInput() ([]byte, error) {
return ReadAll(os.Stdin)
}

func ReadFile(path string) ([]byte, error) {
return os.ReadFile(path)
}

0 comments on commit d4d8ff4

Please sign in to comment.