Skip to content

Commit

Permalink
Merge pull request #63 from KenshiTech/v0.11.8
Browse files Browse the repository at this point in the history
v0.11.8
  • Loading branch information
pouya-eghbali authored Feb 21, 2024
2 parents 68039ba + 70614c6 commit 198903f
Show file tree
Hide file tree
Showing 21 changed files with 386 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ docker/pgadmin
.clinic
node_trace.*
src/bin
conf.*.private
14 changes: 7 additions & 7 deletions conf.broker.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ name: <name>
database:
url: postgres://<user>:<pass>@<host>:<port>/<db>

rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

plugins:
uniswap:
rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

tokens:
- name: ethereum
pair: "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640"
Expand Down
14 changes: 7 additions & 7 deletions conf.remote.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ name: <name>
database:
url: postgres://<user>:<pass>@<host>:<port>/<db>

rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

plugins:
uniswap:
rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

tokens:
- name: ethereum
pair: "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640"
Expand Down
14 changes: 7 additions & 7 deletions conf.worker.yaml.template
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
log: info
name: <name>

rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

plugins:
uniswap:
rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

tokens:
- name: ethereum
pair: "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640"
Expand Down
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfE
github.com/ianlancetaylor/demangle v0.0.0-20230524184225-eabc099b10ab/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb-client-go/v2 v2.4.0/go.mod h1:vLNHdxTJkIf2mSLvGrpj8TCcISApPoXkaxP8g9uRlW8=
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c h1:qSHzRbhzK8RdXOsAdfDgO49TtqC1oZ+acxPrkfTxcCs=
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo=
github.com/jedisct1/go-minisign v0.0.0-20230811132847-661be99b8267/go.mod h1:h1nSAbGFqGVzn6Jyl1R/iCcBUHN4g+gW1u9CoBTrb9E=
Expand Down Expand Up @@ -126,4 +127,5 @@ google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCID
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:J7XzRzVy1+IPwWHZUzoD0IccYZIrXILAQpc+Qy9CMhY=
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:0xJLfVdJqpAPl8tDg1ujOCGzx6LFLttXT5NhllGOXY4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f/go.mod h1:L9KNLi232K1/xB6f7AlSX692koaRnKaWSR0stBki0Yc=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
16 changes: 8 additions & 8 deletions quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ config:
log: info
name: <name>

rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

plugins:
uniswap:
rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

tokens:
- name: ethereum
pair: "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640"
Expand Down Expand Up @@ -236,7 +236,7 @@ the following modifications if required:
messages.
- `name`: This name will be associated with your validator node, and is published to
all peers.
- `plugins.uniswap.rpc.ethereum`: Unchained testnet has automatic RPC rotation and renewal when
- `rpc.ethereum`: Unchained testnet has automatic RPC rotation and renewal when
issues are detected with the RPC connection. You can find a list of Ethereum
RPC nodes on [Chainlist](https://chainlist.org/chain/1).
- `plugins.uniswap.tokens`: UniSwap V3 pool information for fetching and indexing token prices.
Expand Down
60 changes: 60 additions & 0 deletions src/cmd/consumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/KenshiTech/unchained/bls"
"github.com/KenshiTech/unchained/config"
"github.com/KenshiTech/unchained/constants"
"github.com/KenshiTech/unchained/log"
"github.com/KenshiTech/unchained/net/client"

"github.com/spf13/cobra"
)

// consumerCmd represents the consumer command
var consumerCmd = &cobra.Command{
Use: "consumer",
Short: "Run the Unchained client in consumer mode",
Long: `Run the Unchained client in consumer mode`,

PreRun: func(cmd *cobra.Command, args []string) {
config.Config.BindPFlag("broker.uri", cmd.Flags().Lookup("broker"))
},

Run: func(cmd *cobra.Command, args []string) {

log.Logger.
With("Version", constants.Version).
With("Protocol", constants.ProtocolVersion).
Info("Running Unchained")

config.LoadConfig(configPath, secretsPath)
bls.InitClientIdentity()
client.StartClient()
client.StartConsumer()
client.ClientBlock()
},
}

func init() {
rootCmd.AddCommand(consumerCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// consumerCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// consumerCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

consumerCmd.Flags().StringP(
"broker",
"b",
"wss://shinobi.brokers.kenshi.io",
"Unchained broker to connect to",
)
}
1 change: 0 additions & 1 deletion src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func init() {

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.Flags().BoolVarP(&printVersion, "version", "v", false, "Print the Unchained version number and die")

rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./conf.yaml", "Config file")
Expand Down
9 changes: 7 additions & 2 deletions src/cmd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/KenshiTech/unchained/ethereum"
"github.com/KenshiTech/unchained/log"
"github.com/KenshiTech/unchained/net/client"
"github.com/KenshiTech/unchained/plugins/logs"
"github.com/KenshiTech/unchained/plugins/uniswap"

"github.com/spf13/cobra"
Expand All @@ -20,6 +21,11 @@ var workerCmd = &cobra.Command{
Use: "worker",
Short: "Run the Unchained client in worker mode",
Long: `Run the Unchained client in worker mode`,

PreRun: func(cmd *cobra.Command, args []string) {
config.Config.BindPFlag("broker.uri", cmd.Flags().Lookup("broker"))
},

Run: func(cmd *cobra.Command, args []string) {

log.Logger.
Expand All @@ -33,6 +39,7 @@ var workerCmd = &cobra.Command{
ethereum.Start()
uniswap.Setup()
uniswap.Start()
logs.Start()
client.ClientBlock()
},
}
Expand All @@ -56,6 +63,4 @@ func init() {
"wss://shinobi.brokers.kenshi.io",
"Unchained broker to connect to",
)

config.Config.BindPFlag("broker", workerCmd.Flags().Lookup("broker"))
}
5 changes: 3 additions & 2 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
func defaults() {
Config.SetDefault("name", petname.Generate(3, "-"))
Config.SetDefault("log", "info")
Config.SetDefault("plugins.uniswap.rpc.ethereum", "https://ethereum.publicnode.com")
Config.SetDefault("broker", "wss://shinobi.brokers.kenshi.io")
Config.SetDefault("rpc.ethereum", "https://ethereum.publicnode.com")
Config.SetDefault("broker.uri", "wss://shinobi.brokers.kenshi.io")
Config.SetDefault("broker.bind", "0.0.0.0:9123")
}

var Config *viper.Viper
Expand Down
4 changes: 2 additions & 2 deletions src/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package constants

var Version = "0.11.7"
var ProtocolVersion = "0.11.7"
var Version = "0.11.8"
var ProtocolVersion = "0.11.8"
6 changes: 6 additions & 0 deletions src/datasets/uniswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ type PriceReport struct {
PriceInfo PriceInfo
Signature [48]byte
}

type BroadcastPacket struct {
Info PriceInfo
Signature [48]byte
Signers [][]byte
}
10 changes: 5 additions & 5 deletions src/ethereum/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (

var rpcList []string
var rpcIndex int
var client *ethclient.Client
var Client *ethclient.Client

func Start() {
rpcConfig := config.Config.Get("plugins.uniswap.rpc.ethereum")
rpcConfig := config.Config.Get("rpc.ethereum")
rpcIndex = 0

switch reflect.TypeOf(rpcConfig).Kind() {
Expand Down Expand Up @@ -48,7 +48,7 @@ func refreshRPCWithRetries(retries int) bool {

var err error

client, err = ethclient.Dial(rpcList[rpcIndex])
Client, err = ethclient.Dial(rpcList[rpcIndex])

if err != nil {
return refreshRPCWithRetries(retries - 1)
Expand All @@ -66,9 +66,9 @@ func GetNewUniV3Contract(address string, refresh bool) (*contracts.UniV3, error)
RefreshRPC()
}

return contracts.NewUniV3(common.HexToAddress(address), client)
return contracts.NewUniV3(common.HexToAddress(address), Client)
}

func GetBlockNumber() (uint64, error) {
return client.BlockNumber(context.Background())
return Client.BlockNumber(context.Background())
}
2 changes: 1 addition & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/spf13/viper v1.18.2
github.com/vmihailenco/msgpack/v5 v5.4.1
golang.org/x/crypto v0.19.0
golang.org/x/text v0.14.0
)

require (
Expand Down Expand Up @@ -71,7 +72,6 @@ require (
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.18.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
7 changes: 5 additions & 2 deletions src/net/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func StartClient() {

brokerUrl := fmt.Sprintf(
"%s/%s",
config.Config.GetString("broker"),
config.Config.GetString("broker.uri"),
constants.ProtocolVersion,
)

Expand Down Expand Up @@ -63,7 +63,7 @@ func StartClient() {
Error("Read error")
} else {
log.Logger.
With("Error", payload[1:]).
With("Error", string(payload[1:])).
Error("Broker error")
}

Expand Down Expand Up @@ -119,6 +119,9 @@ func StartClient() {
Error("Write error")
}

case 7:
Consume(payload[1:])

default:
log.Logger.
With("Code", payload[0]).
Expand Down
26 changes: 26 additions & 0 deletions src/net/client/consumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package client

import (
"github.com/KenshiTech/unchained/datasets"
"github.com/KenshiTech/unchained/log"
"github.com/gorilla/websocket"
"github.com/vmihailenco/msgpack/v5"
)

func Consume(message []byte) {
var packet datasets.BroadcastPacket
err := msgpack.Unmarshal(message[1:], &packet)
if err != nil {
panic(err)
}
log.Logger.
With("Validators", len(packet.Signers)).
With("Asset", packet.Info.Asset).
With("Block", packet.Info.Block).
With("Price", packet.Info.Price.Text(10)).
Info("Attestation")
}

func StartConsumer() {
Client.WriteMessage(websocket.BinaryMessage, []byte{6})
}
13 changes: 13 additions & 0 deletions src/net/consumer/consumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package consumer

import (
"github.com/KenshiTech/unchained/net/repository"
"github.com/gorilla/websocket"
)

func Broadcast(message []byte) {
repository.Consumers.Range(func(consumer *websocket.Conn, _ bool) bool {
consumer.WriteMessage(websocket.BinaryMessage, message)
return true
})
}
13 changes: 13 additions & 0 deletions src/net/repository/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package repository

import (
"github.com/gorilla/websocket"
"github.com/puzpuzpuz/xsync/v3"
)

// TODO: Do consumers need KOSK?
var Consumers *xsync.MapOf[*websocket.Conn, bool]

func init() {
Consumers = xsync.NewMapOf[*websocket.Conn, bool]()
}
Loading

0 comments on commit 198903f

Please sign in to comment.