This repository has been archived by the owner on May 24, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d99ba3d
commit f5f0bdb
Showing
229 changed files
with
14,201 additions
and
3,552 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package messages | ||
|
||
// Base ... | ||
type Base interface{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package messages | ||
|
||
import "math/big" | ||
|
||
// BlockImport ... | ||
type BlockImport struct { | ||
BestHash []uint8 | ||
BestNumber *big.Int | ||
} | ||
|
||
// NewBlockImport ... | ||
func NewBlockImport(bestHash []uint8, bestNumber *big.Int) *BlockImport { | ||
return &BlockImport{ | ||
BestHash: bestHash, | ||
BestNumber: bestNumber, | ||
} | ||
} | ||
|
||
// ToJSON ... | ||
func (b *BlockImport) ToJSON() { | ||
/* | ||
return { | ||
...super.toJSON(), | ||
// NOTE the endpoint expects non-prefixed values, as much as I hate doing it... | ||
best: u8aToHex(this.bestHash).slice(2), | ||
height: this.bestNumber.toNumber() | ||
}; | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package messages | ||
|
||
// BlockMessage ... | ||
type BlockMessage struct{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package messages | ||
|
||
// Connected ... | ||
type Connected struct { | ||
Chain string | ||
Name string | ||
} | ||
|
||
// NewConnected ... | ||
func NewConnected(chain, name string) *Connected { | ||
return &Connected{ | ||
Chain: chain, | ||
Name: name, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package messages | ||
|
||
import ( | ||
"math/big" | ||
|
||
synctypes "github.com/opennetsys/golkadot/client/p2p/sync/types" | ||
) | ||
|
||
// Interval ... | ||
type Interval struct { | ||
BestHash []uint8 | ||
BestNumber *big.Int | ||
Peers int | ||
Status synctypes.StatusEnum | ||
} | ||
|
||
// NewInterval ... | ||
func NewInterval(bestHash []uint8, bestNumber *big.Int, peers int, status synctypes.StatusEnum) *Interval { | ||
return &Interval{ | ||
BestHash: bestHash, | ||
BestNumber: bestNumber, | ||
Peers: peers, | ||
Status: status, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package messages | ||
|
||
import "math/big" | ||
|
||
// Started ... | ||
type Started struct { | ||
BestHash []uint8 | ||
BestNumber *big.Int | ||
} | ||
|
||
// NewStarted ... | ||
func NewStarted(bestHash []uint8, bestNumber *big.Int) *Started { | ||
return &Started{ | ||
BestHash: bestHash, | ||
BestNumber: bestNumber, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package messages | ||
|
||
// Level ... | ||
type Level string | ||
|
||
//type Level = "INFO" | ||
|
||
// Message ... | ||
type Message string | ||
|
||
// type Message = 'system.connected' | 'system.interval' | 'node.start' | 'block.import'; | ||
|
||
// SyncStatus ... | ||
type SyncStatus int | ||
|
||
// BaseJSON ... | ||
type BaseJSON struct { | ||
level Level | ||
msg Message | ||
ts string | ||
} | ||
|
||
// BlockJSON ... | ||
type BlockJSON struct { | ||
level Level | ||
msg Message | ||
ts string | ||
best string | ||
height int | ||
} | ||
|
||
// ConnectedJSON ... | ||
type ConnectedJSON struct { | ||
level Level | ||
msg Message | ||
ts string | ||
chain string | ||
config string | ||
implementation string | ||
name string | ||
version string | ||
} | ||
|
||
// IntervalJSON ... | ||
type IntervalJSON struct { | ||
peers int | ||
status SyncStatus | ||
txcount int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,113 @@ | ||
package telemetry | ||
|
||
// TODO | ||
import ( | ||
"encoding/json" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
ws "github.com/gorilla/websocket" | ||
chains "github.com/opennetsys/golkadot/client/chain" | ||
clientdb "github.com/opennetsys/golkadot/client/db" | ||
synctypes "github.com/opennetsys/golkadot/client/p2p/sync/types" | ||
messages "github.com/opennetsys/golkadot/client/telemetry/messages" | ||
clienttypes "github.com/opennetsys/golkadot/client/types" | ||
) | ||
|
||
// Telemetry ... | ||
type Telemetry struct { | ||
Blocks *clientdb.BlockDB | ||
IsActive bool | ||
Chain string | ||
Name string | ||
URL string | ||
Websocket *ws.Conn | ||
} | ||
|
||
// NewTelemetry ... | ||
func NewTelemetry(config *clienttypes.ConfigClient, chain chains.Chain) *Telemetry { | ||
tel := config.Telemetry | ||
name := strings.TrimSpace(tel.Name) | ||
|
||
isActive := len(name) > 0 && len(tel.URL) > 0 | ||
|
||
return &Telemetry{ | ||
Blocks: chain.Blocks, | ||
IsActive: isActive, | ||
Chain: chain.Chain.Name, | ||
Name: name, | ||
URL: tel.URL, | ||
} | ||
} | ||
|
||
// Start ... | ||
func (t *Telemetry) Start() { | ||
if !t.IsActive { | ||
return | ||
} | ||
|
||
t.Connect() | ||
} | ||
|
||
// Connect ... | ||
func (t *Telemetry) Connect() { | ||
log.Printf("Connecting to telemtry, url=%s, name=%s\n", t.URL, t.Name) | ||
|
||
wsconn, _, err := ws.DefaultDialer.Dial(t.URL, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
wsconn.SetCloseHandler(func(code int, text string) error { | ||
log.Println("Disconnected from telemetry") | ||
t.Websocket = nil | ||
|
||
time.AfterFunc(5*time.Second, t.Connect) | ||
|
||
return nil | ||
}) | ||
|
||
log.Println("Connected to telemetry") | ||
t.Websocket = wsconn | ||
t.SendInitial() | ||
} | ||
|
||
// BlockImported ... | ||
func (t *Telemetry) BlockImported() { | ||
bestHash := t.Blocks.BestHash.Get(nil) | ||
bestNumber := t.Blocks.BestNumber.Get(nil) | ||
t.Send(messages.NewBlockImport(bestHash, bestNumber)) | ||
} | ||
|
||
// IntervalInfo ... | ||
func (t *Telemetry) IntervalInfo(peers int, status synctypes.StatusEnum) { | ||
bestHash := t.Blocks.BestHash.Get(nil) | ||
bestNumber := t.Blocks.BestNumber.Get(nil) | ||
t.Send(messages.NewInterval(bestHash, bestNumber, peers, status)) | ||
} | ||
|
||
// SendInitial ... | ||
func (t *Telemetry) SendInitial() { | ||
bestHash := t.Blocks.BestHash.Get(nil) | ||
bestNumber := t.Blocks.BestNumber.Get(nil) | ||
t.Send(messages.NewStarted(bestHash, bestNumber)) | ||
t.Send(messages.NewConnected(t.Chain, t.Name)) | ||
} | ||
|
||
// Send ... | ||
func (t *Telemetry) Send(message messages.Base) { | ||
if t.Websocket == nil { | ||
return | ||
} | ||
|
||
b, err := json.Marshal(message) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Printf("Sending %s\n", string(b)) | ||
err = t.Websocket.WriteMessage(ws.TextMessage, b) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package telemetry | ||
|
||
import "testing" | ||
|
||
func TestTelemetry(t *testing.T) { | ||
// TODO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package telemetry | ||
|
||
import ( | ||
synctypes "github.com/opennetsys/golkadot/client/p2p/sync/types" | ||
) | ||
|
||
// Config ... | ||
type Config struct { | ||
Name string | ||
URL string | ||
} | ||
|
||
// InterfaceTelemetry ... | ||
type InterfaceTelemetry interface { | ||
BlockImported() | ||
IntervalInfo(peers int, status synctypes.StatusEnum) | ||
Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.