|
| 1 | +package handshake |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/cloudstruct/go-ouroboros-network/utils" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + PROTOCOL_ID_SENDER = 0x0000 |
| 10 | + PROTOCOL_ID_RECEIVER = 0x8000 |
| 11 | + MESSAGE_TYPE_REQUEST = 0 |
| 12 | + MESSAGE_TYPE_RESPONSE_ACCEPT = 1 |
| 13 | + MESSAGE_TYPE_RESPONSE_REFUSE = 2 |
| 14 | + REFUSE_REASON_VERSION_MISMATCH = 0 |
| 15 | + REFUSE_REASON_DECODE_ERROR = 1 |
| 16 | + REFUSE_REASON_REFUSED = 2 |
| 17 | +) |
| 18 | + |
| 19 | +type handshakeMessage struct { |
| 20 | + _ struct{} `cbor:",toarray"` |
| 21 | + MessageType uint8 |
| 22 | +} |
| 23 | + |
| 24 | +type handshakeRequest struct { |
| 25 | + handshakeMessage |
| 26 | + VersionMap map[uint16]uint32 |
| 27 | +} |
| 28 | + |
| 29 | +type handshakeResponseAccept struct { |
| 30 | + handshakeMessage |
| 31 | + Version uint16 |
| 32 | + NetworkMagic uint32 |
| 33 | +} |
| 34 | + |
| 35 | +type handshakeResponseRefuse struct { |
| 36 | + handshakeMessage |
| 37 | + Reason []interface{} |
| 38 | +} |
| 39 | + |
| 40 | +type Handshake struct { |
| 41 | + sendChan chan []byte |
| 42 | + recvChan chan []byte |
| 43 | +} |
| 44 | + |
| 45 | +func New(sendChan chan []byte, recvChan chan []byte) *Handshake { |
| 46 | + h := &Handshake{ |
| 47 | + sendChan: sendChan, |
| 48 | + recvChan: recvChan, |
| 49 | + } |
| 50 | + return h |
| 51 | +} |
| 52 | + |
| 53 | +func (h *Handshake) Start(versions []uint16, networkMagic uint32) (uint16, error) { |
| 54 | + // Create our request |
| 55 | + versionMap := make(map[uint16]uint32) |
| 56 | + for _, version := range versions { |
| 57 | + versionMap[version] = networkMagic |
| 58 | + } |
| 59 | + data := handshakeRequest{ |
| 60 | + handshakeMessage: handshakeMessage{ |
| 61 | + MessageType: MESSAGE_TYPE_REQUEST, |
| 62 | + }, |
| 63 | + VersionMap: versionMap, |
| 64 | + } |
| 65 | + dataBytes, err := utils.CborEncode(data) |
| 66 | + if err != nil { |
| 67 | + return 0, err |
| 68 | + } |
| 69 | + // Send request |
| 70 | + h.sendChan <- dataBytes |
| 71 | + // Wait for response |
| 72 | + respBytes := <-h.recvChan |
| 73 | + // Decode response into generic list until we can determine what type of response it is |
| 74 | + var resp []interface{} |
| 75 | + if err := utils.CborDecode(respBytes, &resp); err != nil { |
| 76 | + return 0, err |
| 77 | + } |
| 78 | + switch resp[0].(uint64) { |
| 79 | + case MESSAGE_TYPE_RESPONSE_ACCEPT: |
| 80 | + var respAccept handshakeResponseAccept |
| 81 | + if err := utils.CborDecode(respBytes, &respAccept); err != nil { |
| 82 | + return 0, err |
| 83 | + } |
| 84 | + return respAccept.Version, nil |
| 85 | + case MESSAGE_TYPE_RESPONSE_REFUSE: |
| 86 | + var respRefuse handshakeResponseRefuse |
| 87 | + if err := utils.CborDecode(respBytes, &respRefuse); err != nil { |
| 88 | + return 0, err |
| 89 | + } |
| 90 | + switch respRefuse.Reason[0].(uint64) { |
| 91 | + case REFUSE_REASON_VERSION_MISMATCH: |
| 92 | + return 0, fmt.Errorf("handshake failed: version mismatch") |
| 93 | + case REFUSE_REASON_DECODE_ERROR: |
| 94 | + return 0, fmt.Errorf("handshake failed: decode error: %s", respRefuse.Reason[2].(string)) |
| 95 | + case REFUSE_REASON_REFUSED: |
| 96 | + return 0, fmt.Errorf("handshake failed: refused: %s", respRefuse.Reason[2].(string)) |
| 97 | + } |
| 98 | + } |
| 99 | + return 0, fmt.Errorf("unexpected failure") |
| 100 | +} |
0 commit comments