Fix up nwaku module interface
This commit is contained in:
parent
fe4d54512e
commit
81422a4536
|
@ -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)
|
||||||
|
}
|
|
@ -1,9 +1,6 @@
|
||||||
package nwaku
|
package nwaku
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,38 +9,10 @@ import (
|
||||||
|
|
||||||
// Later: Generalize with CallContext (not prio)
|
// Later: Generalize with CallContext (not prio)
|
||||||
|
|
||||||
// TODO Move to types
|
// NOTE Exposing these methods publicly directly, might be wrapped for higher
|
||||||
type WakuInfo struct {
|
// level a la Node API
|
||||||
ListenStr string `json:"listenStr"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// XXX in this case Payload isn't string but something else
|
func GetWakuDebugInfo(client *rpc.Client) WakuInfo {
|
||||||
// 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 {
|
|
||||||
var wakuInfo WakuInfo
|
var wakuInfo WakuInfo
|
||||||
|
|
||||||
if err := client.Call(&wakuInfo, "get_waku_v2_debug_v1_info"); err != nil {
|
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
|
// TODO Support more args
|
||||||
func getWakuStoreMessages(client *rpc.Client, contentTopic string) StoreResponse {
|
func GetWakuStoreMessages(client *rpc.Client, contentTopic string) StoreResponse {
|
||||||
var storeResponse StoreResponse
|
var storeResponse StoreResponse
|
||||||
var contentFilter = ContentFilter{contentTopic}
|
var contentFilter = ContentFilter{contentTopic}
|
||||||
var contentFilters []ContentFilter
|
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 topic = "/waku/2/default-waku/proto"
|
||||||
var res bool
|
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/#post_waku_v2_relay_v1_subscriptions
|
||||||
// https://rfc.vac.dev/spec/16/#get_waku_v2_relay_v1_messages
|
// https://rfc.vac.dev/spec/16/#get_waku_v2_relay_v1_messages
|
||||||
// For now, just do query and publish
|
// 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)
|
|
||||||
}
|
|
||||||
|
|
|
@ -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"`
|
||||||
|
}
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func stopNode() {
|
func StopNode() {
|
||||||
// Since we have reference to same process we can also use cmd.Process.Kill()
|
// Since we have reference to same process we can also use cmd.Process.Kill()
|
||||||
strb, _ := ioutil.ReadFile("wakunode2.lock")
|
strb, _ := ioutil.ReadFile("wakunode2.lock")
|
||||||
command := exec.Command("kill", string(strb))
|
command := exec.Command("kill", string(strb))
|
||||||
|
@ -18,7 +18,7 @@ func stopNode() {
|
||||||
log.Printf("stopping wakunode2 process %s", string(strb))
|
log.Printf("stopping wakunode2 process %s", string(strb))
|
||||||
}
|
}
|
||||||
|
|
||||||
func startNode() {
|
func StartNode() {
|
||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
done := make(chan bool, 1)
|
done := make(chan bool, 1)
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ func startNode() {
|
||||||
go func() {
|
go func() {
|
||||||
sig := <-sigs
|
sig := <-sigs
|
||||||
log.Printf("received %s", sig)
|
log.Printf("received %s", sig)
|
||||||
stopNode()
|
StopNode()
|
||||||
done <- true
|
done <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -53,7 +53,3 @@ func startNode() {
|
||||||
<-done
|
<-done
|
||||||
log.Printf("exiting")
|
log.Printf("exiting")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
startNode()
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue