Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework network protocol & other fixes #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"log"
"os"

"github.com/o3labs/neo-transaction-watcher/neotx"
"github.com/o3labs/neo-transaction-watcher/neotx/network"
"github.com/corollari/neo-transaction-watcher/neotx"
"github.com/corollari/neo-transaction-watcher/neotx/network"
)

type Handler struct {
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"log"
"os"

"github.com/o3labs/neo-transaction-watcher/neotx"
"github.com/o3labs/neo-transaction-watcher/neotx/network"
"github.com/corollari/neo-transaction-watcher/neotx"
"github.com/corollari/neo-transaction-watcher/neotx/network"
)

type Handler struct {
Expand All @@ -28,7 +28,7 @@ func main() {
config := neotx.Config{
Network: neotx.NEOMainNet,
Port: 10333,
IPAddress: "52.193.202.2",
IPAddress: "207.180.217.134",
}
client := neotx.NewClient(config)
handler := &Handler{}
Expand Down
54 changes: 27 additions & 27 deletions neotx/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"io"
"log"
"net"
"time"

"github.com/o3labs/neo-transaction-watcher/neotx/network"
"github.com/corollari/neo-transaction-watcher/neotx/network"
)

//Network
Expand Down Expand Up @@ -51,6 +52,17 @@ type Interface interface {
SetDelegate(MessageDelegate)
}

func startPingLoop(c *Client) {
conn := c.connection
for {
time.Sleep(1000 * time.Millisecond)
nonce, _ := network.RandomUint32()
payload := network.NewPingPayload(nonce)
pingCommand := network.NewMessage(c.Config.Network, network.CommandPing, payload)
conn.Write(pingCommand)
}
}

var _ Interface = (*Client)(nil)

func (c *Client) handleConnection() {
Expand All @@ -73,17 +85,18 @@ func (c *Client) handleConnection() {
return
}

payloadByte := make([]byte, msg.Length)
_, err = io.ReadFull(conn, payloadByte)
if err != nil {
if c.delegate != nil {
go c.delegate.OnError(err)
}
continue
}

//receive version from remote node
if msg.Command == string(network.CommandVersion) {
out := &network.Version{}
payloadByte := make([]byte, msg.Length)
_, err = io.ReadFull(conn, payloadByte)
if err != nil {
if c.delegate != nil {
go c.delegate.OnError(err)
}
continue
}
pr := bytes.NewBuffer(payloadByte)
out.Decode(pr, 0)
//reply with verack
Expand All @@ -94,29 +107,15 @@ func (c *Client) handleConnection() {
go c.delegate.OnConnected(*out)
}
} else if msg.Command == string(network.CommandVerack) {

go startPingLoop(c)
} else if msg.Command == string(network.CommandPong) {
log.Println("Pong!")
} else if msg.Command == string(network.CommandAddr) {
out := &network.Addr{}
payloadByte := make([]byte, msg.Length)
_, err = io.ReadFull(conn, payloadByte)
if err != nil {
if c.delegate != nil {
go c.delegate.OnError(err)
}
continue
}
pr := bytes.NewBuffer(payloadByte)
out.Decode(pr, 0)
} else if msg.Command == string(network.CommandInv) {
out := &network.Inv{}
payloadByte := make([]byte, msg.Length)
_, err = io.ReadFull(conn, payloadByte)
if err != nil {
if c.delegate != nil {
go c.delegate.OnError(err)
}
continue
}
log.Printf("msg = %+v\n", msg)
pr := bytes.NewBuffer(payloadByte)
out.Decode(pr, 0)
Expand All @@ -132,7 +131,8 @@ func (c *Client) handleConnection() {
go c.delegate.OnReceive(tx)
}
}

} else {
log.Printf("Unhandled message: %s\n", msg.Command)
}
}
}
Expand Down
58 changes: 58 additions & 0 deletions neotx/network/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package network

import (
"bytes"
"fmt"
"io"
"time"
)

type Ping struct {
Timestamp uint32 //4 bytes
Nonce uint32 //4 bytes
BlockHeight uint32 //4 bytes
}

func NewPingPayload(nonce uint32) *Ping {

v := Ping{}
v.Timestamp = uint32(time.Unix(time.Now().Unix(), 0).Unix())
v.Nonce = nonce
v.BlockHeight = 0 //TODO get this from a chain data
return &v
}

func (v *Ping) Decode(r io.Reader, protocolVersion uint32) error {

buf, ok := r.(*bytes.Buffer)

if !ok {
return fmt.Errorf("Ping decode reader is not a *byte.Buffer")
}

err := ReadElements(buf, &v.BlockHeight, (*uint32)(&v.Timestamp), &v.Nonce)
if err != nil {
return err
}

return nil
}

func (v *Ping) Encode(w io.Writer, protocolVersion uint32) error {
err := WriteElement(w, v.BlockHeight)
if err != nil {
return err
}

err = WriteElement(w, v.Timestamp)
if err != nil {
return err
}

err = WriteElement(w, v.Nonce)
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion neotx/network/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ServiceFlag uint64

const (
MaxUserAgentLength = 1024
DefaultUserAgent = "NEOxO3:0.0.1;" //test
DefaultUserAgent = "/Neo:2.10.2/" //test
ProtocolVersion = uint32(0)
DefaultServiceFlag = ServiceFlag(1)
)
Expand Down