This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
forked from ethpandaops/ethcore
/
message_transactions.go
65 lines (48 loc) · 1.63 KB
/
message_transactions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// eth protocol transactions https://github.com/ethereum/devp2p/blob/master/caps/eth.md#transactions-0x02
package mimicry
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/eth/protocols/eth"
"github.com/ethereum/go-ethereum/rlp"
"github.com/sirupsen/logrus"
)
const (
TransactionsCode = 0x12
)
type Transactions eth.TransactionsPacket
func (msg *Transactions) Code() int { return TransactionsCode }
func (msg *Transactions) ReqID() uint64 { return 0 }
func (c *Client) receiveTransactions(ctx context.Context, data []byte) (*Transactions, error) {
s := new(Transactions)
if err := rlp.DecodeBytes(data, &s); err != nil {
return nil, fmt.Errorf("error decoding transactions: %w", err)
}
return s, nil
}
func (c *Client) handleTransactions(ctx context.Context, code uint64, data []byte) error {
c.log.WithField("code", code).Debug("received Transactions")
txs, err := c.receiveTransactions(ctx, data)
if err != nil {
return err
}
c.publishTransactions(ctx, txs)
return err
}
func (c *Client) sendTransactions(ctx context.Context, transactions *Transactions) error {
c.log.WithFields(logrus.Fields{
"code": TransactionsCode,
"transactions": transactions,
}).Debug("sending Transactions")
encodedData, err := rlp.EncodeToBytes(transactions)
if err != nil {
return fmt.Errorf("error encoding transactions: %w", err)
}
if _, err := c.rlpxConn.Write(TransactionsCode, encodedData); err != nil {
return fmt.Errorf("error sending transactions: %w", err)
}
return nil
}
func (c *Client) Transactions(ctx context.Context, transactions *Transactions) error {
return c.sendTransactions(ctx, transactions)
}