github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/qln/msgtypes.go (about)

     1  package qln
     2  
     3  import (
     4  	"github.com/mit-dci/lit/lnp2p"
     5  	"github.com/mit-dci/lit/lnutil"
     6  	"github.com/mit-dci/lit/logging"
     7  	"sync"
     8  )
     9  
    10  // LitMsgWrapperMessage is a wrapper type for adapting things to other things.
    11  type LitMsgWrapperMessage struct {
    12  	mtype  uint8
    13  	rawbuf []byte
    14  }
    15  
    16  // Type .
    17  func (wm LitMsgWrapperMessage) Type() uint8 {
    18  	return wm.mtype
    19  }
    20  
    21  // Bytes .
    22  func (wm LitMsgWrapperMessage) Bytes() []byte {
    23  	return wm.rawbuf
    24  }
    25  
    26  func makeNeoOmniParser(mtype uint8) lnp2p.ParseFuncType {
    27  	return func(buf []byte) (lnp2p.Message, error) {
    28  		fullbuf := make([]byte, len(buf)+1)
    29  		fullbuf[0] = mtype
    30  		copy(fullbuf[1:], buf)
    31  		return LitMsgWrapperMessage{mtype, fullbuf}, nil
    32  	}
    33  }
    34  
    35  // Mostly taken from LNDCReader in msghandler.go, then horribly changed.
    36  func makeNeoOmniHandler(nd *LitNode) lnp2p.HandleFuncType {
    37  
    38  	inited := make(map[*RemotePeer]struct{})
    39  	mtx := &sync.Mutex{}
    40  
    41  	return func(p *lnp2p.Peer, m lnp2p.Message) error {
    42  
    43  		// Idk how much locking I need to do here, but doing it across the whole
    44  		// function probably wouldn't hurt.
    45  		mtx.Lock()
    46  		defer mtx.Unlock()
    47  
    48  		var err error
    49  
    50  		wm := m.(LitMsgWrapperMessage)
    51  		rawbuf := wm.rawbuf
    52  		msg, err := lnutil.LitMsgFromBytes(rawbuf, p.GetIdx())
    53  		if err != nil {
    54  			return err
    55  		}
    56  
    57  		peer := nd.PeerMap[p]
    58  
    59  		if _, ok := inited[peer]; !ok {
    60  
    61  			// init the qchan map thingy, this is quite inefficient
    62  			err = nd.PopulateQchanMap(peer)
    63  			if err != nil {
    64  				logging.Errorf("error initing peer: %s", err.Error())
    65  				return err
    66  			}
    67  
    68  			inited[peer] = struct{}{} // :thinking:
    69  
    70  		}
    71  
    72  		// TODO Fix this.  Also it's quite inefficient the way it's written at the moment.
    73  		var chanIdx uint32
    74  		chanIdx = 0
    75  		if len(rawbuf) > 38 {
    76  			var opArr [36]byte
    77  			for _, q := range peer.QCs {
    78  				b := lnutil.OutPointToBytes(q.Op)
    79  				peer.OpMap[b] = q.Idx()
    80  			}
    81  			copy(opArr[:], rawbuf[1:37]) // yay for magic numbers /s
    82  			chanCheck, ok := peer.OpMap[opArr]
    83  			if ok {
    84  				chanIdx = chanCheck
    85  			}
    86  		}
    87  
    88  		logging.Infof("chanIdx is %d, InProg is %d\n", chanIdx, nd.InProg.ChanIdx)
    89  
    90  		if chanIdx != 0 {
    91  			err = nd.PeerHandler(msg, peer.QCs[chanIdx], peer)
    92  		} else {
    93  			err = nd.PeerHandler(msg, nil, peer)
    94  		}
    95  
    96  		if err != nil {
    97  			logging.Errorf("PeerHandler error with %d: %s\n", p.GetIdx(), err.Error())
    98  		}
    99  
   100  		return nil
   101  
   102  	}
   103  }