Skip to content

Commit 0121483

Browse files
committed
eth, eth/protocols/eth: added metadium message handlers
1 parent 117f254 commit 0121483

File tree

6 files changed

+216
-9
lines changed

6 files changed

+216
-9
lines changed

eth/handler.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package eth
1818

1919
import (
2020
"errors"
21-
"fmt"
2221
"math"
2322
"math/big"
2423
"sync"
@@ -538,7 +537,7 @@ func (h *handler) txBroadcastLoop() {
538537

539538
// RequestMinerStatus sends GetStatusExMsg to the given peer
540539
func (h *handler) RequestMinerStatus(id enode.ID) error {
541-
if p := h.peers.peer(fmt.Sprintf("%x", id[:8])); p != nil {
540+
if p := h.peers.peer(id.String()); p != nil {
542541
return p.RequestStatusEx()
543542
} else {
544543
return ethereum.NotFound
@@ -547,7 +546,7 @@ func (h *handler) RequestMinerStatus(id enode.ID) error {
547546

548547
// RequestEtcdAddMember is an internal protocol level command to add a node to the etcd cluster
549548
func (h *handler) RequestEtcdAddMember(id enode.ID) error {
550-
if p := h.peers.peer(fmt.Sprintf("%x", id[:8])); p != nil {
549+
if p := h.peers.peer(id.String()); p != nil {
551550
return p.RequestEtcdAddMember()
552551
} else {
553552
return ethereum.NotFound

eth/protocols/eth/handler.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ var eth65 = map[uint64]msgHandler{
186186
NewPooledTransactionHashesMsg: handleNewPooledTransactionHashes,
187187
GetPooledTransactionsMsg: handleGetPooledTransactions,
188188
PooledTransactionsMsg: handlePooledTransactions,
189+
// metadium message handlers
190+
GetPendingTxsMsg: handleGetPendingTxs,
191+
GetStatusExMsg: handleGetStatusEx,
192+
StatusExMsg: handleStatusEx,
193+
EtcdAddMemberMsg: handleEtcdAddMember,
194+
EtcdClusterMsg: handleEtcdCluster,
195+
TransactionsExMsg: handleTransactionsEx,
189196
}
190197

191198
var eth66 = map[uint64]msgHandler{
@@ -204,6 +211,13 @@ var eth66 = map[uint64]msgHandler{
204211
ReceiptsMsg: handleReceipts66,
205212
GetPooledTransactionsMsg: handleGetPooledTransactions66,
206213
PooledTransactionsMsg: handlePooledTransactions66,
214+
// metadium message handlers - not eth/66 yet
215+
GetPendingTxsMsg: handleGetPendingTxs,
216+
GetStatusExMsg: handleGetStatusEx,
217+
StatusExMsg: handleStatusEx,
218+
EtcdAddMemberMsg: handleEtcdAddMember,
219+
EtcdClusterMsg: handleEtcdCluster,
220+
TransactionsExMsg: handleTransactionsEx,
207221
}
208222

209223
// handleMessage is invoked whenever an inbound message is received from a remote

eth/protocols/eth/handlers.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/core/types"
2525
"github.com/ethereum/go-ethereum/log"
26+
metaminer "github.com/ethereum/go-ethereum/metadium/miner"
2627
"github.com/ethereum/go-ethereum/rlp"
2728
"github.com/ethereum/go-ethereum/trie"
2829
)
@@ -477,6 +478,33 @@ func handleTransactions(backend Backend, msg Decoder, peer *Peer) error {
477478
return backend.Handle(peer, &txs)
478479
}
479480

481+
func handleTransactionsEx(backend Backend, msg Decoder, peer *Peer) error {
482+
// Transactions arrived, make sure we have a valid and fresh chain to handle them
483+
if !backend.AcceptTxs() {
484+
return nil
485+
}
486+
// Transactions can be processed, parse all of them and deliver to the pool
487+
var txexs TransactionsExPacket
488+
if err := msg.Decode(&txexs); err != nil {
489+
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
490+
}
491+
492+
go func() error {
493+
signer := types.MakeSigner(backend.Chain().Config(), backend.Chain().CurrentBlock().Number())
494+
txs := types.TxExs2Txs(signer, txexs, metaminer.IsPartner(peer.ID()))
495+
for i, tx := range txs {
496+
// Validate and mark the remote transaction
497+
if tx == nil {
498+
return fmt.Errorf("%w: transaction %d is nil", errDecode, i)
499+
}
500+
peer.markTransaction(tx.Hash())
501+
}
502+
txsp := TransactionsPacket(txs)
503+
return backend.Handle(peer, &txsp)
504+
}()
505+
return nil
506+
}
507+
480508
func handlePooledTransactions(backend Backend, msg Decoder, peer *Peer) error {
481509
// Transactions arrived, make sure we have a valid and fresh chain to handle them
482510
if !backend.AcceptTxs() {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package eth
2+
3+
import (
4+
"fmt"
5+
6+
metaapi "github.com/ethereum/go-ethereum/metadium/api"
7+
metaminer "github.com/ethereum/go-ethereum/metadium/miner"
8+
)
9+
10+
func handleGetPendingTxs(backend Backend, msg Decoder, peer *Peer) error {
11+
// not supported, just ignore it.
12+
return nil
13+
}
14+
15+
func handleGetStatusEx(backend Backend, msg Decoder, peer *Peer) error {
16+
if !metaminer.AmPartner() || !metaminer.IsPartner(peer.ID()) {
17+
return nil
18+
}
19+
20+
go func() {
21+
statusEx := metaapi.GetMinerStatus()
22+
statusEx.LatestBlockTd = backend.Chain().GetTd(statusEx.LatestBlockHash,
23+
statusEx.LatestBlockHeight.Uint64())
24+
if err := peer.SendStatusEx(statusEx); err != nil {
25+
// ignore the error
26+
}
27+
}()
28+
29+
return nil
30+
}
31+
32+
func handleStatusEx(backend Backend, msg Decoder, peer *Peer) error {
33+
if !metaminer.AmPartner() || !metaminer.IsPartner(peer.ID()) {
34+
return nil
35+
}
36+
var status metaapi.MetadiumMinerStatus
37+
if err := msg.Decode(&status); err != nil {
38+
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
39+
}
40+
41+
go func() {
42+
if _, td := peer.Head(); status.LatestBlockTd.Cmp(td) > 0 {
43+
peer.SetHead(status.LatestBlockHash, status.LatestBlockTd)
44+
}
45+
metaapi.GotStatusEx(&status)
46+
}()
47+
48+
return nil
49+
}
50+
51+
func handleEtcdAddMember(backend Backend, msg Decoder, peer *Peer) error {
52+
if !metaminer.AmPartner() || !metaminer.IsPartner(peer.ID()) {
53+
return nil
54+
}
55+
56+
go func() {
57+
cluster, _ := metaapi.EtcdAddMember(peer.ID())
58+
if err := peer.SendEtcdCluster(cluster); err != nil {
59+
// ignore the error
60+
}
61+
}()
62+
63+
return nil
64+
}
65+
66+
func handleEtcdCluster(backend Backend, msg Decoder, peer *Peer) error {
67+
if !metaminer.AmPartner() || !metaminer.IsPartner(peer.ID()) {
68+
return nil
69+
}
70+
var cluster string
71+
if err := msg.Decode(&cluster); err != nil {
72+
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
73+
}
74+
75+
go metaapi.GotEtcdCluster(cluster)
76+
77+
return nil
78+
}

eth/protocols/eth/peer.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
mapset "github.com/deckarep/golang-set"
2525
"github.com/ethereum/go-ethereum/common"
2626
"github.com/ethereum/go-ethereum/core/types"
27+
metaapi "github.com/ethereum/go-ethereum/metadium/api"
2728
"github.com/ethereum/go-ethereum/p2p"
2829
"github.com/ethereum/go-ethereum/rlp"
2930
)
@@ -402,6 +403,32 @@ func (p *Peer) ReplyReceiptsRLP(id uint64, receipts []rlp.RawValue) error {
402403
})
403404
}
404405

406+
// SendStatusEx sends this node's miner status
407+
func (p *Peer) SendStatusEx(status *metaapi.MetadiumMinerStatus) error {
408+
return p2p.Send(p.rw, StatusExMsg, status)
409+
}
410+
411+
// ReplyStatusEx is the eth/66 response to GetStatusEx
412+
func (p *Peer) ReplyStatusEx(id uint64, status *metaapi.MetadiumMinerStatus) error {
413+
return p2p.Send(p.rw, StatusExMsg, StatusExPacket66{
414+
RequestId: id,
415+
StatusExPacket: StatusExPacket(*status),
416+
})
417+
}
418+
419+
// SendEtcdCluster sends this node's etcd cluster
420+
func (p *Peer) SendEtcdCluster(cluster string) error {
421+
return p2p.Send(p.rw, EtcdClusterMsg, cluster)
422+
}
423+
424+
// ReplyEtcdCluster is the eth/66 response to EtcdAddMember
425+
func (p *Peer) ReplyEtcdCluster(id uint64, cluster string) error {
426+
return p2p.Send(p.rw, EtcdClusterMsg, EtcdClusterPacket66{
427+
RequestId: id,
428+
EtcdClusterPacket: EtcdClusterPacket(cluster),
429+
})
430+
}
431+
405432
// RequestOneHeader is a wrapper around the header query functions to fetch a
406433
// single header. It is used solely by the fetcher.
407434
func (p *Peer) RequestOneHeader(hash common.Hash) error {
@@ -545,11 +572,17 @@ func (p *Peer) RequestTxs(hashes []common.Hash) error {
545572
// RequestStatusEx fetches extended status of the peer
546573
func (p *Peer) RequestStatusEx() error {
547574
p.Log().Debug("Fetching extended status")
575+
id := rand.Uint64()
576+
577+
requestTracker.Track(p.id, p.version, GetStatusExMsg, StatusExMsg, id)
548578
return p2p.Send(p.rw, GetStatusExMsg, common.Big1)
549579
}
550580

551581
// RequestEtcdAddMember requests the peer to add this node to the cluster
552582
func (p *Peer) RequestEtcdAddMember() error {
553583
p.Log().Debug("Trying to join etcd network")
584+
id := rand.Uint64()
585+
586+
requestTracker.Track(p.id, p.version, EtcdAddMemberMsg, EtcdClusterMsg, id)
554587
return p2p.Send(p.rw, EtcdAddMemberMsg, common.Big1)
555588
}

eth/protocols/eth/protocol.go

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/ethereum/go-ethereum/common"
2626
"github.com/ethereum/go-ethereum/core/forkid"
2727
"github.com/ethereum/go-ethereum/core/types"
28+
metaapi "github.com/ethereum/go-ethereum/metadium/api"
2829
"github.com/ethereum/go-ethereum/rlp"
2930
)
3031

@@ -70,12 +71,12 @@ const (
7071
PooledTransactionsMsg = 0x0a
7172

7273
// Added by Metadium, meta/64
73-
GetPendingTxsMsg = 0x11
74-
GetStatusExMsg = 0x12
75-
StatusExMsg = 0x13
76-
EtcdAddMemberMsg = 0x14
77-
EtcdClusterMsg = 0x15
78-
TxExMsg = 0x16
74+
GetPendingTxsMsg = 0x11
75+
GetStatusExMsg = 0x12
76+
StatusExMsg = 0x13
77+
EtcdAddMemberMsg = 0x14
78+
EtcdClusterMsg = 0x15
79+
TransactionsExMsg = 0x16
7980
)
8081

8182
var (
@@ -128,6 +129,9 @@ func (p *NewBlockHashesPacket) Unpack() ([]common.Hash, []uint64) {
128129
// TransactionsPacket is the network packet for broadcasting new transactions.
129130
type TransactionsPacket []*types.Transaction
130131

132+
// TransactionsExPacket is the network packet for broadcasting new extended transactions.
133+
type TransactionsExPacket []*types.TransactionEx
134+
131135
// GetBlockHeadersPacket represents a block header query.
132136
type GetBlockHeadersPacket struct {
133137
Origin HashOrNumber // Block from which to retrieve headers
@@ -329,6 +333,42 @@ type PooledTransactionsRLPPacket66 struct {
329333
PooledTransactionsRLPPacket
330334
}
331335

336+
// GetStatusExPacket is the network packet for GetStatusEx
337+
type GetStatusExPacket int
338+
339+
// GetStatusExPacket66 is the eth/66 form of GetSTatusExPacket
340+
type GetStatusExPacket66 struct {
341+
RequestId uint64
342+
GetStatusExPacket
343+
}
344+
345+
// StatusExPacket is the network packet for extended status of a node
346+
type StatusExPacket metaapi.MetadiumMinerStatus
347+
348+
// StatusExPacket66 is the eth/66 form of StatusExPacket
349+
type StatusExPacket66 struct {
350+
RequestId uint64
351+
StatusExPacket
352+
}
353+
354+
// EtcdAddMemberPacket is the netowkr packet for EtcdAddMember
355+
type EtcdAddMemberPacket int
356+
357+
// EtcdAddMemberPacket66 is the eth/66 form of EtcdAddMember
358+
type EtcdAddMemberPacket66 struct {
359+
RequestId uint64
360+
EtcdAddMemberPacket
361+
}
362+
363+
// EtcdClusterPacket is the network packet for EtcdAddMember / EtcdCluster exchange
364+
type EtcdClusterPacket string
365+
366+
// EtcdClusterPacket66 is the eth/66 form of EtcdClusterPacket
367+
type EtcdClusterPacket66 struct {
368+
RequestId uint64
369+
EtcdClusterPacket
370+
}
371+
332372
func (*StatusPacket) Name() string { return "Status" }
333373
func (*StatusPacket) Kind() byte { return StatusMsg }
334374

@@ -338,6 +378,9 @@ func (*NewBlockHashesPacket) Kind() byte { return NewBlockHashesMsg }
338378
func (*TransactionsPacket) Name() string { return "Transactions" }
339379
func (*TransactionsPacket) Kind() byte { return TransactionsMsg }
340380

381+
func (*TransactionsExPacket) Name() string { return "TransactionsEx" }
382+
func (*TransactionsExPacket) Kind() byte { return TransactionsExMsg }
383+
341384
func (*GetBlockHeadersPacket) Name() string { return "GetBlockHeaders" }
342385
func (*GetBlockHeadersPacket) Kind() byte { return GetBlockHeadersMsg }
343386

@@ -373,3 +416,15 @@ func (*GetPooledTransactionsPacket) Kind() byte { return GetPooledTransactions
373416

374417
func (*PooledTransactionsPacket) Name() string { return "PooledTransactions" }
375418
func (*PooledTransactionsPacket) Kind() byte { return PooledTransactionsMsg }
419+
420+
func (*GetStatusExPacket) Name() string { return "GetStatusEx" }
421+
func (*GetStatusExPacket) Kind() byte { return GetStatusExMsg }
422+
423+
func (*StatusExPacket) Name() string { return "StatusEx" }
424+
func (*StatusExPacket) Kind() byte { return StatusExMsg }
425+
426+
func (*EtcdAddMemberPacket) Name() string { return "EtcdAddMember" }
427+
func (*EtcdAddMemberPacket) Kind() byte { return EtcdAddMemberMsg }
428+
429+
func (*EtcdClusterPacket) Name() string { return "EtcdCluster" }
430+
func (*EtcdClusterPacket) Kind() byte { return EtcdClusterMsg }

0 commit comments

Comments
 (0)