-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtypes.go
More file actions
148 lines (118 loc) · 3.97 KB
/
Copy pathtypes.go
File metadata and controls
148 lines (118 loc) · 3.97 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package dasmon
import (
"crypto/ecdsa"
"fmt"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/libp2p/go-libp2p/core/peer"
)
// PeerCustody represents peer custody and chain state information.
// This is the abstracted version of Prysm's StatusV2 and MetaDataV2,
// containing only the fields needed by dasmon.
type PeerCustody struct {
// PeerID is the libp2p peer identifier
PeerID peer.ID
// EarliestSlot is the earliest slot the peer has data for
EarliestSlot uint64
// HeadSlot is the peer's current head slot
HeadSlot uint64
// CustodyGroupCount is the number of custody groups the peer is responsible for
CustodyGroupCount uint64
}
func (c PeerCustody) String() string {
return fmt.Sprintf("%d[%d=>%d]", c.CustodyGroupCount, c.EarliestSlot, c.HeadSlot)
}
func (c PeerCustody) Equals(other PeerCustody) bool {
return c.EarliestSlot == other.EarliestSlot &&
c.HeadSlot == other.HeadSlot &&
c.CustodyGroupCount == other.CustodyGroupCount
}
func (c PeerCustody) Diff(other PeerCustody) []any {
ret := []any{}
prev, next := c, other
if prev.EarliestSlot != next.EarliestSlot {
ret = append(ret, "earliest", fmt.Sprintf("%d=>%d", prev.EarliestSlot, next.EarliestSlot))
}
if prev.HeadSlot != next.HeadSlot {
ret = append(ret, "head", fmt.Sprintf("%d=>%d", prev.HeadSlot, next.HeadSlot))
}
if prev.CustodyGroupCount != next.CustodyGroupCount {
ret = append(ret, "cgc", fmt.Sprintf("%d=>%d", prev.CustodyGroupCount, next.CustodyGroupCount))
}
return ret
}
func (c PeerCustody) Range() Range[uint64] {
return Range[uint64]{c.EarliestSlot, c.HeadSlot}
}
// Block represents a beacon chain block with KZG commitments.
// This is the abstracted version of Prysm's SignedBeaconBlock,
// containing only the fields needed by dasmon.
type Block struct {
// Slot is the slot number of this block
Slot uint64
// Root is the hash tree root of the block
Root [32]byte
// BlobCommitments are the KZG commitments for blobs in this block
BlobCommitments [][]byte
}
// ChainEvent represents a chain state change event.
// This abstracts Prysm's state feed events.
type ChainEvent struct {
// Type indicates what kind of chain event occurred
Type ChainEventType
// Slot is the slot number of the event
Slot uint64
// BlockRoot is the root of the block involved in the event
BlockRoot [32]byte
}
// ChainEventType indicates the type of chain state change
type ChainEventType int
const (
// ChainEventNewHead indicates a new head block was received
ChainEventNewHead ChainEventType = iota
// ChainEventReorg indicates a chain reorganization occurred
ChainEventReorg
)
// String returns the string representation of the ChainEventType
func (t ChainEventType) String() string {
switch t {
case ChainEventNewHead:
return "new_head"
case ChainEventReorg:
return "reorg"
default:
return "unknown"
}
}
// BlockQuery specifies a range of slots to query blocks for.
type BlockQuery struct {
// StartSlot is the beginning of the range (inclusive)
StartSlot uint64
// EndSlot is the end of the range (inclusive)
EndSlot uint64
}
// peerIDToNodeID converts a libp2p peer ID to an Ethereum node ID.
func peerIDToNodeID(id peer.ID) (enode.ID, error) {
// Retrieve the public key object of the peer under "crypto" form.
pubkey, err := id.ExtractPublicKey()
if err != nil {
return [32]byte{}, fmt.Errorf("extract public key from peer ID `%s`: %w", id, err)
}
// Extract the bytes representation of the public key.
compressedPubkey, err := pubkey.Raw()
if err != nil {
return [32]byte{}, fmt.Errorf("public key raw: %w", err)
}
// Retrieve the public key object of the peer under "SECP256K1" form.
pubKeyObjSecp256k1, err := btcec.ParsePubKey(compressedPubkey)
if err != nil {
return [32]byte{}, fmt.Errorf("parse public key: %w", err)
}
newPubkey := &ecdsa.PublicKey{
Curve: crypto.S256(),
X: pubKeyObjSecp256k1.X(),
Y: pubKeyObjSecp256k1.Y(),
}
return enode.PubkeyToIDV4(newPubkey), nil
}