|
| 1 | +package chainsync |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "github.com/cloudstruct/go-ouroboros-network/muxer" |
| 8 | + "github.com/cloudstruct/go-ouroboros-network/protocol/common" |
| 9 | + "github.com/cloudstruct/go-ouroboros-network/utils" |
| 10 | + "io" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + PROTOCOL_ID_NTN uint16 = 2 |
| 15 | + PROTOCOL_ID_NTC uint16 = 5 |
| 16 | + |
| 17 | + STATE_IDLE = iota |
| 18 | + STATE_CAN_AWAIT |
| 19 | + STATE_MUST_REPLY |
| 20 | + STATE_INTERSECT |
| 21 | + |
| 22 | + BLOCK_TYPE_BYRON_EBB = 0 |
| 23 | + BLOCK_TYPE_BYRON_MAIN = 1 |
| 24 | + // TODO: more block types |
| 25 | +) |
| 26 | + |
| 27 | +type ChainSync struct { |
| 28 | + errorChan chan error |
| 29 | + sendChan chan *muxer.Message |
| 30 | + recvChan chan *muxer.Message |
| 31 | + state uint8 |
| 32 | + nodeToNode bool |
| 33 | + protocolId uint16 |
| 34 | + requestNextChan chan *RequestNextResult |
| 35 | + recvBuffer bytes.Buffer |
| 36 | +} |
| 37 | + |
| 38 | +func New(m *muxer.Muxer, errorChan chan error, nodeToNode bool) *ChainSync { |
| 39 | + // Use node-to-client protocol ID |
| 40 | + protocolId := PROTOCOL_ID_NTC |
| 41 | + if nodeToNode { |
| 42 | + // Use node-to-node protocol ID |
| 43 | + protocolId = PROTOCOL_ID_NTN |
| 44 | + } |
| 45 | + sendChan, recvChan := m.RegisterProtocol(protocolId) |
| 46 | + c := &ChainSync{ |
| 47 | + errorChan: errorChan, |
| 48 | + sendChan: sendChan, |
| 49 | + recvChan: recvChan, |
| 50 | + state: STATE_IDLE, |
| 51 | + nodeToNode: nodeToNode, |
| 52 | + protocolId: protocolId, |
| 53 | + requestNextChan: make(chan *RequestNextResult, 10), |
| 54 | + } |
| 55 | + go c.recvLoop() |
| 56 | + return c |
| 57 | +} |
| 58 | + |
| 59 | +func (c *ChainSync) recvLoop() { |
| 60 | + for { |
| 61 | + var err error |
| 62 | + // Wait for response |
| 63 | + msg := <-c.recvChan |
| 64 | + c.recvBuffer.Write(msg.Payload) |
| 65 | + // Decode response into generic list until we can determine what type of response it is |
| 66 | + var resp []interface{} |
| 67 | + if err := utils.CborDecode(c.recvBuffer.Bytes(), &resp); err != nil { |
| 68 | + if errors.Is(err, io.ErrUnexpectedEOF) { |
| 69 | + continue |
| 70 | + } |
| 71 | + c.errorChan <- fmt.Errorf("chain-sync: decode error: %s", err) |
| 72 | + } |
| 73 | + switch resp[0].(uint64) { |
| 74 | + case MESSAGE_TYPE_AWAIT_REPLY: |
| 75 | + //err = c.handleRequest(msg) |
| 76 | + fmt.Printf("MESSAGE_TYPE_AWAIT_REPLY\n") |
| 77 | + case MESSAGE_TYPE_ROLL_FORWARD: |
| 78 | + err = c.handleRollForward(c.recvBuffer.Bytes()) |
| 79 | + case MESSAGE_TYPE_ROLL_BACKWARD: |
| 80 | + err = c.handleRollBackward(c.recvBuffer.Bytes()) |
| 81 | + case MESSAGE_TYPE_INTERSECT_FOUND: |
| 82 | + // TODO |
| 83 | + fmt.Printf("MESSAGE_TYPE_INTERSECT_FOUND\n") |
| 84 | + case MESSAGE_TYPE_INTERSECT_NOT_FOUND: |
| 85 | + // TODO |
| 86 | + fmt.Printf("MESSAGE_TYPE_INTERSECT_NOT_FOUND\n") |
| 87 | + case MESSAGE_TYPE_DONE: |
| 88 | + // TODO |
| 89 | + fmt.Printf("MESSAGE_TYPE_DONE\n") |
| 90 | + default: |
| 91 | + err = fmt.Errorf("chain-sync: received unexpected message: %#v", resp) |
| 92 | + } |
| 93 | + c.recvBuffer.Reset() |
| 94 | + if err != nil { |
| 95 | + c.errorChan <- err |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func (c *ChainSync) RequestNext() (*RequestNextResult, error) { |
| 101 | + // TODO |
| 102 | + // if c.state != STATE_IDLE { |
| 103 | + // return nil, fmt.Errorf("protocol not in expected state") |
| 104 | + // } |
| 105 | + // Create our request |
| 106 | + data := newMsgRequestNext() |
| 107 | + dataBytes, err := utils.CborEncode(data) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + msg := muxer.NewMessage(c.protocolId, dataBytes, false) |
| 112 | + // Send request |
| 113 | + c.sendChan <- msg |
| 114 | + // Set the new state |
| 115 | + c.state = STATE_CAN_AWAIT |
| 116 | + resp := <-c.requestNextChan |
| 117 | + return resp, nil |
| 118 | +} |
| 119 | + |
| 120 | +func (c *ChainSync) FindIntersect(points []string) { |
| 121 | + return |
| 122 | +} |
| 123 | + |
| 124 | +func (c *ChainSync) handleAwaitReply(msg *muxer.Message) error { |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func (c *ChainSync) handleRollForward(data []byte) error { |
| 129 | + // TODO |
| 130 | + // if c.state != STATE_CONFIRM { |
| 131 | + // return fmt.Errorf("received handshake accept response when protocol is in wrong state") |
| 132 | + // } |
| 133 | + var msg msgRollForward |
| 134 | + if err := utils.CborDecode(data, &msg); err != nil { |
| 135 | + return fmt.Errorf("chain-sync: decode error: %s", err) |
| 136 | + } |
| 137 | + /* |
| 138 | + if len(msg.WrappedData) < 8000 { |
| 139 | + for _, x := range msg.WrappedData { |
| 140 | + fmt.Printf("%02x ", x) |
| 141 | + } |
| 142 | + fmt.Printf("\n") |
| 143 | + } |
| 144 | + */ |
| 145 | + var block wrappedBlock |
| 146 | + if err := utils.CborDecode(msg.WrappedData, &block); err != nil { |
| 147 | + return fmt.Errorf("chain-sync: decode error: %s", err) |
| 148 | + //fmt.Printf("ignoring wrapped data decode error for now...%s\n", err) |
| 149 | + } |
| 150 | + resp := &RequestNextResult{ |
| 151 | + BlockType: block.Type, |
| 152 | + } |
| 153 | + switch block.Type { |
| 154 | + case BLOCK_TYPE_BYRON_EBB: |
| 155 | + var block2 common.ByronEpochBoundaryBlock |
| 156 | + if err := utils.CborDecode(block.RawBlock, &block2); err != nil { |
| 157 | + return fmt.Errorf("chain-sync: decode error: %s", err) |
| 158 | + } |
| 159 | + resp.Block = block2 |
| 160 | + case BLOCK_TYPE_BYRON_MAIN: |
| 161 | + var block2 common.ByronMainBlock |
| 162 | + if err := utils.CborDecode(block.RawBlock, &block2); err != nil { |
| 163 | + return fmt.Errorf("chain-sync: decode error: %s", err) |
| 164 | + } |
| 165 | + //fmt.Printf("epoch = %d, slot = %d, prevBlock = %s\n", block2.Header.ConsensusData.SlotId.Epoch, block2.Header.ConsensusData.SlotId.Slot, block2.Header.PrevBlock) |
| 166 | + //fmt.Printf("block2 = %#v\n", block2) |
| 167 | + resp.Block = block2 |
| 168 | + // TODO: support more block types |
| 169 | + default: |
| 170 | + var payload interface{} |
| 171 | + if err := utils.CborDecode(msg.WrappedData, &payload); err != nil { |
| 172 | + //return fmt.Errorf("chain-sync: decode error: %s", err) |
| 173 | + //fmt.Printf("ignoring generic payload decode error for now...%s\n", err) |
| 174 | + } |
| 175 | + //fmt.Printf("payload = %s\n", utils.DumpCborStructure(payload, "")) |
| 176 | + resp.Block = payload |
| 177 | + } |
| 178 | + c.requestNextChan <- resp |
| 179 | + // TODO |
| 180 | + // c.state = STATE_DONE |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +func (c *ChainSync) handleRollBackward(data []byte) error { |
| 185 | + // TODO |
| 186 | + // if c.state != STATE_CONFIRM { |
| 187 | + // return fmt.Errorf("received handshake accept response when protocol is in wrong state") |
| 188 | + // } |
| 189 | + var msg msgRollBackward |
| 190 | + if err := utils.CborDecode(data, &msg); err != nil { |
| 191 | + return fmt.Errorf("chain-sync: decode error: %s", err) |
| 192 | + } |
| 193 | + fmt.Printf("handleRollBackward: msg = %#v\n", msg) |
| 194 | + resp := &RequestNextResult{ |
| 195 | + Rollback: true, |
| 196 | + } |
| 197 | + c.requestNextChan <- resp |
| 198 | + // TODO |
| 199 | + // c.state = STATE_DONE |
| 200 | + return nil |
| 201 | +} |
| 202 | + |
| 203 | +func (c *ChainSync) handleIntersectFound(msg *muxer.Message) error { |
| 204 | + return nil |
| 205 | +} |
| 206 | + |
| 207 | +func (c *ChainSync) handleIntersectNotFound(msg *muxer.Message) error { |
| 208 | + return nil |
| 209 | +} |
| 210 | + |
| 211 | +func (c *ChainSync) handleDone(msg *muxer.Message) error { |
| 212 | + return nil |
| 213 | +} |
0 commit comments