Skip to content

Commit

Permalink
Merge pull request #20 from cloudstruct/feature/handshake-version
Browse files Browse the repository at this point in the history
Properly support protocol versions
  • Loading branch information
agaffney authored Feb 19, 2022
2 parents bae132e + b76d0ea commit 512dd82
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 10 deletions.
22 changes: 14 additions & 8 deletions ouroboros.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ouroboros

import (
"fmt"
"github.com/cloudstruct/go-ouroboros-network/muxer"
"github.com/cloudstruct/go-ouroboros-network/protocol/blockfetch"
"github.com/cloudstruct/go-ouroboros-network/protocol/chainsync"
Expand Down Expand Up @@ -82,21 +81,28 @@ func (o *Ouroboros) setupConnection() error {
}()
// Perform handshake
o.Handshake = handshake.New(o.muxer, o.ErrorChan, o.useNodeToNodeProto)
// TODO: create a proper version map
versionMap := []uint16{1, 32778}
var protoVersions []uint16
if o.useNodeToNodeProto {
versionMap = []uint16{7}
protoVersions = GetProtocolVersionsNtN()
} else {
protoVersions = GetProtocolVersionsNtC()
}
// TODO: figure out better way to signify automatic handshaking and returning the chosen version
if !o.waitForHandshake {
err := o.Handshake.ProposeVersions(versionMap, o.networkMagic)
err := o.Handshake.ProposeVersions(protoVersions, o.networkMagic)
if err != nil {
return err
}
}
o.handshakeComplete = <-o.Handshake.Finished
fmt.Printf("negotiated protocol version %d\n", o.Handshake.Version)
// TODO: register additional mini-protocols
o.ChainSync = chainsync.New(o.muxer, o.ErrorChan, o.useNodeToNodeProto, o.chainSyncCallbackConfig)
o.BlockFetch = blockfetch.New(o.muxer, o.ErrorChan, o.blockFetchCallbackConfig)
if o.useNodeToNodeProto {
//versionNtN := GetProtocolVersionNtN(o.Handshake.Version)
o.ChainSync = chainsync.New(o.muxer, o.ErrorChan, o.useNodeToNodeProto, o.chainSyncCallbackConfig)
o.BlockFetch = blockfetch.New(o.muxer, o.ErrorChan, o.blockFetchCallbackConfig)
} else {
//versionNtC := GetProtocolVersionNtC(o.Handshake.Version)
o.ChainSync = chainsync.New(o.muxer, o.ErrorChan, o.useNodeToNodeProto, o.chainSyncCallbackConfig)
}
return nil
}
10 changes: 8 additions & 2 deletions utils/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import (
)

func CborEncode(data interface{}) ([]byte, error) {
dataBytes, err := cbor.Marshal(data)
return dataBytes, err
buf := bytes.NewBuffer(nil)
em, err := cbor.CoreDetEncOptions().EncMode()
if err != nil {
return nil, err
}
enc := em.NewEncoder(buf)
err = enc.Encode(data)
return buf.Bytes(), err
}

func CborDecode(dataBytes []byte, dest interface{}) (int, error) {
Expand Down
114 changes: 114 additions & 0 deletions versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ouroboros

const (
// The NtC protocol versions have the 15th bit set in the handshake
PROTOCOL_VERSION_NTC_FLAG = 0x8000
)

type ProtocolVersionNtC struct {
// Most of these are enabled in all of the protocol versions that we support, but
// they are here for completeness
EnableLocalQueryProtocol bool
EnableShelleyEra bool
EnableAllegraEra bool
EnableMaryEra bool
EnableAlonzoEra bool
EnableLocalTxMonitorProtocol bool
}

// We don't bother supporting NtC protocol versions before 9 (when Alonzo was enabled)
var ProtocolVersionMapNtC = map[uint16]ProtocolVersionNtC{
9: ProtocolVersionNtC{
EnableLocalQueryProtocol: true,
EnableShelleyEra: true,
EnableAllegraEra: true,
EnableMaryEra: true,
EnableAlonzoEra: true,
},
// This version also adds the GetChainBlockNo and GetChainPoint queries, but we don't
// currently have a flag for this
10: ProtocolVersionNtC{
EnableLocalQueryProtocol: true,
EnableShelleyEra: true,
EnableAllegraEra: true,
EnableMaryEra: true,
EnableAlonzoEra: true,
},
// This version also adds the GetRewardInfoPools Block query, but we don't currently
// have a flag for this
11: ProtocolVersionNtC{
EnableLocalQueryProtocol: true,
EnableShelleyEra: true,
EnableAllegraEra: true,
EnableMaryEra: true,
EnableAlonzoEra: true,
},
12: ProtocolVersionNtC{
EnableLocalQueryProtocol: true,
EnableShelleyEra: true,
EnableAllegraEra: true,
EnableMaryEra: true,
EnableAlonzoEra: true,
EnableLocalTxMonitorProtocol: true,
},
}

type ProtocolVersionNtN struct {
// Most of these are enabled in all of the protocol versions that we support, but
// they are here for completeness
EnableShelleyEra bool
EnableKeepAliveProtocol bool
EnableAllegraEra bool
EnableMaryEra bool
EnableTxSubmission2Protocol bool
EnableAlonzoEra bool
EnableFullDuplex bool
}

// We don't bother supporting NtN protocol versions before 7 (when Alonzo was enabled)
var ProtocolVersionMapNtN = map[uint16]ProtocolVersionNtN{
7: ProtocolVersionNtN{
EnableShelleyEra: true,
EnableKeepAliveProtocol: true,
EnableAllegraEra: true,
EnableMaryEra: true,
EnableTxSubmission2Protocol: true,
EnableAlonzoEra: true,
},
8: ProtocolVersionNtN{
EnableShelleyEra: true,
EnableKeepAliveProtocol: true,
EnableAllegraEra: true,
EnableMaryEra: true,
EnableTxSubmission2Protocol: true,
EnableAlonzoEra: true,
EnableFullDuplex: true,
},
}

func GetProtocolVersionsNtC() []uint16 {
versions := []uint16{}
for key := range ProtocolVersionMapNtC {
versions = append(versions, key+PROTOCOL_VERSION_NTC_FLAG)
}
return versions
}

func GetProtocolVersionNtC(version uint16) ProtocolVersionNtC {
if version > PROTOCOL_VERSION_NTC_FLAG {
version = version - PROTOCOL_VERSION_NTC_FLAG
}
return ProtocolVersionMapNtC[version]
}

func GetProtocolVersionsNtN() []uint16 {
versions := []uint16{}
for key := range ProtocolVersionMapNtN {
versions = append(versions, key)
}
return versions
}

func GetProtocolVersionNtN(version uint16) ProtocolVersionNtN {
return ProtocolVersionMapNtN[version]
}

0 comments on commit 512dd82

Please sign in to comment.