Created
September 26, 2023 05:54
-
-
Save minhd-vu/7be340d8ca4dd862591c843760f27635 to your computer and use it in GitHub Desktop.
Sensor Network Demo
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
package main | |
import ( | |
"math/big" | |
"os" | |
"os/signal" | |
"syscall" | |
"github.com/ethereum/go-ethereum/common" | |
"github.com/ethereum/go-ethereum/core/forkid" | |
"github.com/ethereum/go-ethereum/crypto" | |
"github.com/ethereum/go-ethereum/eth/protocols/eth" | |
"github.com/ethereum/go-ethereum/p2p" | |
"github.com/ethereum/go-ethereum/p2p/enode" | |
"github.com/ethereum/go-ethereum/p2p/nat" | |
"github.com/rs/zerolog" | |
"github.com/rs/zerolog/log" | |
) | |
var protocol = p2p.Protocol{ | |
Name: "eth", | |
Version: 66, | |
Length: 17, | |
Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error { | |
status := eth.StatusPacket{ | |
ProtocolVersion: 66, | |
NetworkID: 137, | |
Genesis: common.HexToHash("0xa9c28ce2141b56c474f1dc504bee9b01eb1bd7d1a507580d5519d4437a97de1b"), | |
ForkID: forkid.ID{Hash: [4]byte{79, 47, 113, 204}}, | |
Head: common.HexToHash("0xfd946f1f1b1fd182ee7a4a0d4ed08ba216d555deb4353052d00a6f088d96b740"), | |
TD: big.NewInt(810023945), | |
} | |
err := p2p.Send(rw, eth.StatusMsg, &status) | |
if err != nil { | |
return err | |
} | |
for { | |
msg, err := rw.ReadMsg() | |
if err != nil { | |
return err | |
} | |
switch msg.Code { | |
case eth.StatusMsg: | |
var packet eth.StatusPacket | |
err = msg.Decode(&packet) | |
if err != nil { | |
return err | |
} | |
if packet.NetworkID != status.NetworkID { | |
return p2p.DiscRequested | |
} | |
log.Info().Any("status", packet).Send() | |
default: | |
log.Info().Any("msg", msg).Send() | |
} | |
if err = msg.Discard(); err != nil { | |
return err | |
} | |
} | |
}, | |
} | |
func main() { | |
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) | |
privateKey, err := crypto.GenerateKey() | |
if err != nil { | |
log.Fatal().Err(err).Send() | |
} | |
server := p2p.Server{ | |
Config: p2p.Config{ | |
PrivateKey: privateKey, | |
BootstrapNodes: []*enode.Node{ | |
enode.MustParseV4("enode://b8f1cc9c5d4403703fbf377116469667d2b1823c0daf16b7250aa576bacf399e42c3930ccfcb02c5df6879565a2b8931335565f0e8d3f8e72385ecf4a4bf160a@3.36.224.80:30303"), | |
enode.MustParseV4("enode://8729e0c825f3d9cad382555f3e46dcff21af323e89025a0e6312df541f4a9e73abfa562d64906f5e59c51fe6f0501b3e61b07979606c56329c020ed739910759@54.194.245.5:30303"), | |
}, | |
MaxPeers: 10, | |
ListenAddr: ":30303", | |
Protocols: []p2p.Protocol{protocol}, | |
NAT: nat.Any(), | |
DiscoveryV4: true, | |
}, | |
} | |
if err := server.Start(); err != nil { | |
log.Fatal().Err(err).Send() | |
} | |
defer server.Stop() | |
events := make(chan *p2p.PeerEvent) | |
server.SubscribeEvents(events) | |
signals := make(chan os.Signal, 1) | |
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) | |
for { | |
select { | |
case event := <-events: | |
log.Debug().Any("event", event).Send() | |
case <-signals: | |
log.Print("Stopping server") | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment