Skip to content

Commit

Permalink
Merge pull request #1 from thuannguyen2010/feature/ogmios-v6
Browse files Browse the repository at this point in the history
Integrate with ogmios v6
  • Loading branch information
thuannguyen2010 authored Sep 8, 2023
2 parents 1239ccb + 912ffa6 commit 0b70460
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 13 deletions.
47 changes: 44 additions & 3 deletions chain_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type ChainSyncOptions struct {
points chainsync.Points // points to attempt initial intersection
reconnect bool // reconnect to ogmios if connection drops
store Store // store of points
useV6 bool // useV6 decides using v6 or v5 payload
}

func buildChainSyncOptions(opts ...ChainSyncOption) ChainSyncOptions {
Expand Down Expand Up @@ -101,6 +102,13 @@ func WithPoints(points ...chainsync.Point) ChainSyncOption {
}
}

// WithUseV6 decides using ogmios v6 or v5 interface
func WithUseV6(useV6 bool) ChainSyncOption {
return func(opts *ChainSyncOptions) {
opts.useV6 = useV6
}
}

// WithReconnect attempt to reconnect to ogmios if connection drops
func WithReconnect(enabled bool) ChainSyncOption {
return func(opts *ChainSyncOptions) {
Expand Down Expand Up @@ -173,8 +181,14 @@ func (c *Client) doChainSync(ctx context.Context, callback ChainSyncFunc, option
c.logger.Error(err, "failed to connect to ogmios", KV("endpoint", c.options.endpoint))
return fmt.Errorf("failed to connect to ogmios, %v: %w", c.options.endpoint, err)
}

init, err := getInit(ctx, options.store, options.points...)
var init, next []byte
if options.useV6 {
next = []byte(`{"jsonrpc":"2.0","method":"nextBlock"}`)
init, err = getInitV6(ctx, options.store, options.points...)
} else {
next = []byte(`{"type":"jsonwsp/request","version":"1.0","servicename":"ogmios","methodname":"RequestNext","args":{}}`)
init, err = getInit(ctx, options.store, options.points...)
}
if err != nil {
c.logger.Error(err, "failed to create init message")
return fmt.Errorf("failed to create init message: %w", err)
Expand Down Expand Up @@ -221,7 +235,6 @@ func (c *Client) doChainSync(ctx context.Context, callback ChainSyncFunc, option
return fmt.Errorf("failed to write FindIntersect: %w", err)
}

next := []byte(`{"type":"jsonwsp/request","version":"1.0","servicename":"ogmios","methodname":"RequestNext","args":{}}`)
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -355,6 +368,34 @@ func getInit(ctx context.Context, store Store, pp ...chainsync.Point) (data []by
return json.Marshal(init)
}

func getInitV6(ctx context.Context, store Store, pp ...chainsync.Point) (data []byte, err error) {
points, err := store.Load(ctx)
if err != nil {
return nil, fmt.Errorf("failed to retrieve points from store: %w", err)
}
if len(points) == 0 {
points = append(points, pp...)
}
if len(points) == 0 {
points = append(points, chainsync.Origin)
}
sort.Sort(points)
if len(points) > 5 {
points = points[0:5]
}
pointsV6 := points.ConvertToV6()
init := Map{
"jsonrpc": "2.0",
"method": "findIntersection",
"params": Map{
"points": pointsV6,
},
"id": "findIntersect",
}

return json.Marshal(init)
}

// getPoint returns the first point from the list of json encoded chainsync.Responses provided
// multiple Responses allow for the possibility of a Rollback being included in the set
func getPoint(data ...[]byte) (chainsync.Point, bool) {
Expand Down
60 changes: 55 additions & 5 deletions cmd/ogmigo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package main

import (
"context"
"encoding/json"
"fmt"
"github.com/thuannguyen2010/ogmigo"
"github.com/thuannguyen2010/ogmigo/ouroboros/chainsync"
"github.com/urfave/cli/v2"
"log"
"os"
Expand Down Expand Up @@ -65,16 +67,64 @@ func main() {

func action(_ *cli.Context) error {
client := ogmigo.New(
ogmigo.WithEndpoint("ws://localhost:1337"),
ogmigo.WithEndpoint("ws://172.0.0.1:1337"),
ogmigo.WithLogger(ogmigo.DefaultLogger),
)

ctx := context.Background()
redeemer, err := client.EvaluateTx(ctx, "")
var (
ctx = context.Background()
points chainsync.Points
)
points = []chainsync.Point{
chainsync.PointStruct{
BlockNo: 1009189,
Hash: "b95423b8778536cf9a53e8855cef2bd8702f79ffa3c918b8857437cfb238474d",
Slot: 23003752,
}.Point(),
}
//useV6 := false
useV6 := true
var callback ogmigo.ChainSyncFunc = func(ctx context.Context, data []byte) error {
var response chainsync.Response
if useV6 {
var resV6 chainsync.ResponseV6
if err := json.Unmarshal(data, &resV6); err != nil {
return err
}
response = resV6.ConvertToV5()
} else {
if err := json.Unmarshal(data, &response); err != nil {
return err
}
}

if response.Result == nil {
return nil
}
if response.Result.RollForward != nil {
ps := response.Result.RollForward.Block.PointStruct()
fmt.Println("blockNo", ps.BlockNo, "hash", ps.Hash, "slot", ps.Slot)
}
if response.Result.RollBackward != nil {
return nil
}

//ps := response.Result.RollForward.Block.PointStruct()
//fmt.Printf("slot=%v hash=%v block=%v\n", ps.Slot, ps.Hash, ps.BlockNo)

return nil
}
closer, err := client.ChainSync(ctx, callback,
ogmigo.WithPoints(points...),
ogmigo.WithReconnect(true),
ogmigo.WithUseV6(useV6),
)

if err != nil {
panic(err)
return err
}
fmt.Println(redeemer)
defer closer.Close()

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Kill, os.Interrupt)

Expand Down
12 changes: 12 additions & 0 deletions ouroboros/chainsync/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,15 @@ func (a RedeemerKey) Index() int {
}
return 0
}

type EvaluationResult []EvaluationItem

type EvaluationItem struct {
Validator string `json:"validator"`
Budget Budget `json:"budget"`
}

type Budget struct {
Memory int64 `json:"memory"`
Steps int64 `json:"cpu"`
}
Loading

0 comments on commit 0b70460

Please sign in to comment.