Skip to content

Commit 512dd82

Browse files
authored
Merge pull request #20 from cloudstruct/feature/handshake-version
Properly support protocol versions
2 parents bae132e + b76d0ea commit 512dd82

File tree

3 files changed

+136
-10
lines changed

3 files changed

+136
-10
lines changed

ouroboros.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ouroboros
22

33
import (
4-
"fmt"
54
"github.com/cloudstruct/go-ouroboros-network/muxer"
65
"github.com/cloudstruct/go-ouroboros-network/protocol/blockfetch"
76
"github.com/cloudstruct/go-ouroboros-network/protocol/chainsync"
@@ -82,21 +81,28 @@ func (o *Ouroboros) setupConnection() error {
8281
}()
8382
// Perform handshake
8483
o.Handshake = handshake.New(o.muxer, o.ErrorChan, o.useNodeToNodeProto)
85-
// TODO: create a proper version map
86-
versionMap := []uint16{1, 32778}
84+
var protoVersions []uint16
8785
if o.useNodeToNodeProto {
88-
versionMap = []uint16{7}
86+
protoVersions = GetProtocolVersionsNtN()
87+
} else {
88+
protoVersions = GetProtocolVersionsNtC()
8989
}
90+
// TODO: figure out better way to signify automatic handshaking and returning the chosen version
9091
if !o.waitForHandshake {
91-
err := o.Handshake.ProposeVersions(versionMap, o.networkMagic)
92+
err := o.Handshake.ProposeVersions(protoVersions, o.networkMagic)
9293
if err != nil {
9394
return err
9495
}
9596
}
9697
o.handshakeComplete = <-o.Handshake.Finished
97-
fmt.Printf("negotiated protocol version %d\n", o.Handshake.Version)
9898
// TODO: register additional mini-protocols
99-
o.ChainSync = chainsync.New(o.muxer, o.ErrorChan, o.useNodeToNodeProto, o.chainSyncCallbackConfig)
100-
o.BlockFetch = blockfetch.New(o.muxer, o.ErrorChan, o.blockFetchCallbackConfig)
99+
if o.useNodeToNodeProto {
100+
//versionNtN := GetProtocolVersionNtN(o.Handshake.Version)
101+
o.ChainSync = chainsync.New(o.muxer, o.ErrorChan, o.useNodeToNodeProto, o.chainSyncCallbackConfig)
102+
o.BlockFetch = blockfetch.New(o.muxer, o.ErrorChan, o.blockFetchCallbackConfig)
103+
} else {
104+
//versionNtC := GetProtocolVersionNtC(o.Handshake.Version)
105+
o.ChainSync = chainsync.New(o.muxer, o.ErrorChan, o.useNodeToNodeProto, o.chainSyncCallbackConfig)
106+
}
101107
return nil
102108
}

utils/cbor.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import (
66
)
77

88
func CborEncode(data interface{}) ([]byte, error) {
9-
dataBytes, err := cbor.Marshal(data)
10-
return dataBytes, err
9+
buf := bytes.NewBuffer(nil)
10+
em, err := cbor.CoreDetEncOptions().EncMode()
11+
if err != nil {
12+
return nil, err
13+
}
14+
enc := em.NewEncoder(buf)
15+
err = enc.Encode(data)
16+
return buf.Bytes(), err
1117
}
1218

1319
func CborDecode(dataBytes []byte, dest interface{}) (int, error) {

versions.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package ouroboros
2+
3+
const (
4+
// The NtC protocol versions have the 15th bit set in the handshake
5+
PROTOCOL_VERSION_NTC_FLAG = 0x8000
6+
)
7+
8+
type ProtocolVersionNtC struct {
9+
// Most of these are enabled in all of the protocol versions that we support, but
10+
// they are here for completeness
11+
EnableLocalQueryProtocol bool
12+
EnableShelleyEra bool
13+
EnableAllegraEra bool
14+
EnableMaryEra bool
15+
EnableAlonzoEra bool
16+
EnableLocalTxMonitorProtocol bool
17+
}
18+
19+
// We don't bother supporting NtC protocol versions before 9 (when Alonzo was enabled)
20+
var ProtocolVersionMapNtC = map[uint16]ProtocolVersionNtC{
21+
9: ProtocolVersionNtC{
22+
EnableLocalQueryProtocol: true,
23+
EnableShelleyEra: true,
24+
EnableAllegraEra: true,
25+
EnableMaryEra: true,
26+
EnableAlonzoEra: true,
27+
},
28+
// This version also adds the GetChainBlockNo and GetChainPoint queries, but we don't
29+
// currently have a flag for this
30+
10: ProtocolVersionNtC{
31+
EnableLocalQueryProtocol: true,
32+
EnableShelleyEra: true,
33+
EnableAllegraEra: true,
34+
EnableMaryEra: true,
35+
EnableAlonzoEra: true,
36+
},
37+
// This version also adds the GetRewardInfoPools Block query, but we don't currently
38+
// have a flag for this
39+
11: ProtocolVersionNtC{
40+
EnableLocalQueryProtocol: true,
41+
EnableShelleyEra: true,
42+
EnableAllegraEra: true,
43+
EnableMaryEra: true,
44+
EnableAlonzoEra: true,
45+
},
46+
12: ProtocolVersionNtC{
47+
EnableLocalQueryProtocol: true,
48+
EnableShelleyEra: true,
49+
EnableAllegraEra: true,
50+
EnableMaryEra: true,
51+
EnableAlonzoEra: true,
52+
EnableLocalTxMonitorProtocol: true,
53+
},
54+
}
55+
56+
type ProtocolVersionNtN struct {
57+
// Most of these are enabled in all of the protocol versions that we support, but
58+
// they are here for completeness
59+
EnableShelleyEra bool
60+
EnableKeepAliveProtocol bool
61+
EnableAllegraEra bool
62+
EnableMaryEra bool
63+
EnableTxSubmission2Protocol bool
64+
EnableAlonzoEra bool
65+
EnableFullDuplex bool
66+
}
67+
68+
// We don't bother supporting NtN protocol versions before 7 (when Alonzo was enabled)
69+
var ProtocolVersionMapNtN = map[uint16]ProtocolVersionNtN{
70+
7: ProtocolVersionNtN{
71+
EnableShelleyEra: true,
72+
EnableKeepAliveProtocol: true,
73+
EnableAllegraEra: true,
74+
EnableMaryEra: true,
75+
EnableTxSubmission2Protocol: true,
76+
EnableAlonzoEra: true,
77+
},
78+
8: ProtocolVersionNtN{
79+
EnableShelleyEra: true,
80+
EnableKeepAliveProtocol: true,
81+
EnableAllegraEra: true,
82+
EnableMaryEra: true,
83+
EnableTxSubmission2Protocol: true,
84+
EnableAlonzoEra: true,
85+
EnableFullDuplex: true,
86+
},
87+
}
88+
89+
func GetProtocolVersionsNtC() []uint16 {
90+
versions := []uint16{}
91+
for key := range ProtocolVersionMapNtC {
92+
versions = append(versions, key+PROTOCOL_VERSION_NTC_FLAG)
93+
}
94+
return versions
95+
}
96+
97+
func GetProtocolVersionNtC(version uint16) ProtocolVersionNtC {
98+
if version > PROTOCOL_VERSION_NTC_FLAG {
99+
version = version - PROTOCOL_VERSION_NTC_FLAG
100+
}
101+
return ProtocolVersionMapNtC[version]
102+
}
103+
104+
func GetProtocolVersionsNtN() []uint16 {
105+
versions := []uint16{}
106+
for key := range ProtocolVersionMapNtN {
107+
versions = append(versions, key)
108+
}
109+
return versions
110+
}
111+
112+
func GetProtocolVersionNtN(version uint16) ProtocolVersionNtN {
113+
return ProtocolVersionMapNtN[version]
114+
}

0 commit comments

Comments
 (0)