Skip to content

Commit

Permalink
Sui/watcher (#1791)
Browse files Browse the repository at this point in the history
* sui/watcher: updates to python example

* sui/watcher: simple go example
  • Loading branch information
jumpsiegel committed Oct 28, 2022
1 parent 0aedc11 commit 16dfd94
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 76 deletions.
2 changes: 2 additions & 0 deletions sui/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ sui client publish --gas-budget 10000 | tee publish.log
wa="`grep "The newly published package" publish.log | sed -e 's/^.*: //'`"
sed -i -e "s/wormhole = .*/wormhole = \"$wa\"/" Move.toml
state="`grep "Move Object" publish.log | sed -e 's/^.*(\(.*\)\[.*$/\1/'`"
owner="`grep '^Owner: Account Address' publish.log | sed -e 's/^.* ( \(.*\) )/\1/'`"
echo "WORM_PACKAGE=\"$wa\"" > ../env.sh
echo "WORM_STATE=\"$state\"" >> ../env.sh
echo "WORM_OWNER=\"$owner\"" >> ../env.sh

2 changes: 1 addition & 1 deletion sui/tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module watcher

go 1.17

require github.com/gorilla/websocket v1.5.0 // indirect
require golang.org/x/net v0.1.0 // indirect
4 changes: 2 additions & 2 deletions sui/tests/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
88 changes: 19 additions & 69 deletions sui/tests/watcher.go
Original file line number Diff line number Diff line change
@@ -1,85 +1,35 @@
// Copyright 2015 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build ignore
// +build ignore

// go get github.com/gorilla/websocket

package main

import (
"flag"
"fmt"
"golang.org/x/net/websocket"
"log"
"net/url"
"os"
"os/signal"
"time"

"github.com/gorilla/websocket"
"os"
)

var addr = flag.String("addr", "localhost:5001", "http service address")

func main() {
flag.Parse()
log.SetFlags(0)

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

u := url.URL{Scheme: "ws", Host: *addr, Path: "/sui_subscribeEvent"}
log.Printf("connecting to %s", u.String())

c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
origin := "http://localhost/"
url := "ws://localhost:9001"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal("dial:", err)
log.Fatal(err)
}
defer c.Close()

done := make(chan struct{})

go func() {
defer close(done)
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
log.Printf("recv: %s", message)
}
}()

ticker := time.NewTicker(time.Second)
defer ticker.Stop()
s := fmt.Sprintf(`{"jsonrpc":"2.0", "id": 1, "method": "sui_subscribeEvent", "params": [{"SenderAddress": "%s"}]}`, os.Getenv("WORM_OWNER"))
fmt.Printf("Sending: %s.\n", s)

if _, err := ws.Write([]byte(s)); err != nil {
log.Fatal(err)
}
for {
select {
case <-done:
return
case t := <-ticker.C:
err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
if err != nil {
log.Println("write:", err)
return
}
case <-interrupt:
log.Println("interrupt")

// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-done:
case <-time.After(time.Second):
}
return
var msg = make([]byte, 512)
var n int
ws.SetReadDeadline(time.Now().Local().Add(1_000_000_000));
if n, err = ws.Read(msg); err != nil {
fmt.Printf("err");
} else {
fmt.Printf("Received: %s.\n", msg[:n])
}
}
}
67 changes: 64 additions & 3 deletions sui/tests/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,71 @@
import _thread
import time
import rel
import os
import pprint
import json
import base64

# https://github.com/MystenLabs/sui/pull/5113

# {
# "jsonrpc": "2.0",
# "method": "sui_subscribeEvent",
# "params": {
# "subscription": 1805876586195140,
# "result": {
# "timestamp": 1666704112752,
# "txDigest": "ckB13AaG+OHrO0Ha3I8IK3ERanYHmHAI0jSXnqk9R+I=",
# "event": {
# "moveEvent": {
# "packageId": "0xbd99019f3c8f9d08b5498fedcc97e1c24cddff88",
# "transactionModule": "wormhole",
# "sender": "0xdc2f7334400a353c6a9303235b578477202809c6",
# "type": "0xbd99019f3c8f9d08b5498fedcc97e1c24cddff88::state::WormholeMessage",
# "fields": {
# "consistency_level": 0,
# "nonce": 400,
# "payload": "Ag==",
# "sender": "0xdc2f7334400a353c6a9303235b578477202809c6",
# "sequence": 19,
# "timestamp": 0
# },
# "bcs": "3C9zNEAKNTxqkwMjW1eEdyAoCcYTAAAAAAAAAJABAAAAAAAAAQIAAAAAAAAAAAA="
# }
# }
# }
# }
# }

# curl -s -X POST -d '{"jsonrpc":"2.0", "id": 1, "method": "sui_getEventsByTransaction", "params": ["KgsiF8pCF61N02zX2oMFYLWQdrbxkOD1ypBxND752No=", 2]}' -H 'Content-Type: application/json' http://127.0.0.1:9000 | jq

# {
# "jsonrpc": "2.0",
# "result": [
# {
# "timestamp": 1666704112752,
# "txDigest": "ckB13AaG+OHrO0Ha3I8IK3ERanYHmHAI0jSXnqk9R+I=",
# "event": {
# "moveEvent": {
# "packageId": "0xbd99019f3c8f9d08b5498fedcc97e1c24cddff88",
# "transactionModule": "wormhole",
# "sender": "0xdc2f7334400a353c6a9303235b578477202809c6",
# "type": "0xbd99019f3c8f9d08b5498fedcc97e1c24cddff88::state::WormholeMessage",
# "bcs": "3C9zNEAKNTxqkwMjW1eEdyAoCcYTAAAAAAAAAJABAAAAAAAAAQIAAAAAAAAAAAA="
# }
# }
# }
# ],
# "id": 1
# }

def on_message(ws, message):
print(message)
v = json.loads(message)
if "params" in v:
tx = v["params"]["result"]["txDigest"]
tx = len(base64.standard_b64decode(tx))
print(tx.hex())
print(json.dumps(v, indent=4))

def on_error(ws, error):
print(error)
Expand All @@ -14,10 +76,9 @@ def on_close(ws, close_status_code, close_msg):

def on_open(ws):
print("Opened connection")
ws.send("{\"jsonrpc\":\"2.0\", \"id\": 1, \"method\": \"sui_subscribeEvent\", \"params\": [{\"SenderAddress\": \"0xf4e306649ca37370e61c27b1fc684dae62234b78\"}]}")
ws.send("{\"jsonrpc\":\"2.0\", \"id\": 1, \"method\": \"sui_subscribeEvent\", \"params\": [{\"SenderAddress\": \"" + os.getenv("WORM_OWNER") + "\"}]}")

if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:9001",
on_open=on_open,
on_message=on_message,
Expand Down
2 changes: 1 addition & 1 deletion sui/wormhole/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ version = "0.0.1"
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework", rev = "devnet" }

[addresses]
wormhole = "0xbbd7f613afc47d3f8b991208f6444107fec1a52d"
wormhole = "0xbd99019f3c8f9d08b5498fedcc97e1c24cddff88"

0 comments on commit 16dfd94

Please sign in to comment.