Fix up nwaku module interface

This commit is contained in:
Oskar Thoren 2021-06-10 18:30:51 +08:00
parent fe4d54512e
commit 81422a4536
No known key found for this signature in database
GPG Key ID: B2ECCFD3BC2EF77E
4 changed files with 75 additions and 67 deletions

36
nwaku.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
"context"
"fmt"
"time"
"github.com/ethereum/go-ethereum/rpc"
"github.com/status-im/go-nwaku/nwaku"
)
func main() {
fmt.Println("Starting node...")
nwaku.StartNode()
fmt.Println("JSON RPC request...")
_, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Assumes node started
client, _ := rpc.Dial("http://127.0.0.1:8545")
// Get node info
var wakuInfo = nwaku.GetWakuDebugInfo(client)
fmt.Println("WakuInfo ListenStr", wakuInfo.ListenStr)
// Query messages
var contentTopic = "/toy-chat/2/huilong/proto"
var storeResponse = nwaku.GetWakuStoreMessages(client, contentTopic)
fmt.Println("Fetched", len(storeResponse.Messages), "messages")
// Publish
var message = nwaku.WakuRelayMessage{Payload: "0x1a2b3c4d5e6f", ContentTopic: contentTopic}
var res = nwaku.PostWakuRelayMessage(client, message)
fmt.Println("Publish", res)
}

View File

@ -1,9 +1,6 @@
package nwaku
import (
"context"
"fmt"
"time"
"github.com/ethereum/go-ethereum/rpc"
)
@ -12,38 +9,10 @@ import (
// Later: Generalize with CallContext (not prio)
// TODO Move to types
type WakuInfo struct {
ListenStr string `json:"listenStr"`
}
// NOTE Exposing these methods publicly directly, might be wrapped for higher
// level a la Node API
// XXX in this case Payload isn't string but something else
// panic: json: cannot unmarshal array into Go struct field WakuMessage.messages.payload of type string
// TODO This should be toy-chat protobuf probably
type WakuMessage struct {
// TODO Should be hex encoded string here
Payload []byte `json:"payload"`
ContentTopic string `json:"contentTopic"`
Version int `json:"version"`
Timestamp float64 `json:"timestamp"`
}
type WakuRelayMessage struct {
Payload string `json:"payload"`
ContentTopic string `json:"contentTopic"`
// Version int `json:"version"`
// Timestamp float64 `json:"timestamp"`
}
type StoreResponse struct {
Messages []WakuMessage `json:"messages"`
}
type ContentFilter struct {
ContentTopic string `json:"contentTopic"`
}
func getWakuDebugInfo(client *rpc.Client) WakuInfo {
func GetWakuDebugInfo(client *rpc.Client) WakuInfo {
var wakuInfo WakuInfo
if err := client.Call(&wakuInfo, "get_waku_v2_debug_v1_info"); err != nil {
@ -54,7 +23,7 @@ func getWakuDebugInfo(client *rpc.Client) WakuInfo {
}
// TODO Support more args
func getWakuStoreMessages(client *rpc.Client, contentTopic string) StoreResponse {
func GetWakuStoreMessages(client *rpc.Client, contentTopic string) StoreResponse {
var storeResponse StoreResponse
var contentFilter = ContentFilter{contentTopic}
var contentFilters []ContentFilter
@ -68,7 +37,7 @@ func getWakuStoreMessages(client *rpc.Client, contentTopic string) StoreResponse
}
func postWakuRelayMessage(client *rpc.Client, message WakuRelayMessage) bool {
func PostWakuRelayMessage(client *rpc.Client, message WakuRelayMessage) bool {
var topic = "/waku/2/default-waku/proto"
var res bool
@ -83,27 +52,3 @@ func postWakuRelayMessage(client *rpc.Client, message WakuRelayMessage) bool {
// https://rfc.vac.dev/spec/16/#post_waku_v2_relay_v1_subscriptions
// https://rfc.vac.dev/spec/16/#get_waku_v2_relay_v1_messages
// For now, just do query and publish
func main() {
fmt.Println("JSON RPC request...")
_, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Assumes node started
client, _ := rpc.Dial("http://127.0.0.1:8545")
// Get node info
var wakuInfo = getWakuDebugInfo(client)
fmt.Println("WakuInfo ListenStr", wakuInfo.ListenStr)
// Query messages
var contentTopic = "/toy-chat/2/huilong/proto"
var storeResponse = getWakuStoreMessages(client, contentTopic)
fmt.Println("Fetched", len(storeResponse.Messages), "messages")
// Publish
var message = WakuRelayMessage{Payload: "0x1a2b3c4d5e6f", ContentTopic: contentTopic}
var res = postWakuRelayMessage(client, message)
fmt.Println("Publish", res)
}

31
nwaku/types.go Normal file
View File

@ -0,0 +1,31 @@
package nwaku
type WakuInfo struct {
ListenStr string `json:"listenStr"`
}
// XXX in this case Payload isn't string but something else
// panic: json: cannot unmarshal array into Go struct field WakuMessage.messages.payload of type string
// TODO This should be toy-chat protobuf probably
type WakuMessage struct {
// TODO Should be hex encoded string here
Payload []byte `json:"payload"`
ContentTopic string `json:"contentTopic"`
Version int `json:"version"`
Timestamp float64 `json:"timestamp"`
}
type WakuRelayMessage struct {
Payload string `json:"payload"`
ContentTopic string `json:"contentTopic"`
// Version int `json:"version"`
// Timestamp float64 `json:"timestamp"`
}
type StoreResponse struct {
Messages []WakuMessage `json:"messages"`
}
type ContentFilter struct {
ContentTopic string `json:"contentTopic"`
}

View File

@ -10,7 +10,7 @@ import (
"syscall"
)
func stopNode() {
func StopNode() {
// Since we have reference to same process we can also use cmd.Process.Kill()
strb, _ := ioutil.ReadFile("wakunode2.lock")
command := exec.Command("kill", string(strb))
@ -18,7 +18,7 @@ func stopNode() {
log.Printf("stopping wakunode2 process %s", string(strb))
}
func startNode() {
func StartNode() {
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
@ -28,7 +28,7 @@ func startNode() {
go func() {
sig := <-sigs
log.Printf("received %s", sig)
stopNode()
StopNode()
done <- true
}()
@ -53,7 +53,3 @@ func startNode() {
<-done
log.Printf("exiting")
}
func main() {
startNode()
}