@@ -11,10 +11,13 @@ import (
1111 "github.com/ethereum/go-ethereum/common"
1212 "github.com/ethereum/go-ethereum/core/types"
1313 "github.com/libp2p/go-libp2p/core/host"
14+ "github.com/libp2p/go-libp2p/core/peer"
15+ "github.com/rs/zerolog/log"
1416 "github.com/sprintertech/sprinter-signing/chains/evm/calls/events"
1517 "github.com/sprintertech/sprinter-signing/comm"
1618 "github.com/sprintertech/sprinter-signing/tss"
1719 "github.com/sprintertech/sprinter-signing/tss/ecdsa/signing"
20+ tssMessage "github.com/sprintertech/sprinter-signing/tss/message"
1821 "github.com/sygmaprotocol/sygma-core/relayer/message"
1922 "github.com/sygmaprotocol/sygma-core/relayer/proposal"
2023)
@@ -28,7 +31,8 @@ type EventFilterer interface {
2831}
2932
3033type AcrossData struct {
31- depositId * big.Int
34+ depositId * big.Int
35+ coordinator peer.ID
3236}
3337
3438func NewAcrossMessage (source uint8 , destination uint8 , acrossData AcrossData ) * message.Message {
@@ -47,19 +51,56 @@ type AcrossMessageHandler struct {
4751 across common.Address
4852 abi abi.ABI
4953
50- coordinator * tss.Coordinator
51- host host.Host
52- communication comm.Communication
53- fetcher signing.SaveDataFetcher
54+ coordinator * tss.Coordinator
55+ host host.Host
56+ comm comm.Communication
57+ fetcher signing.SaveDataFetcher
5458
55- resultChn chan interface {}
59+ sigChn chan interface {}
60+ }
61+
62+ func (h * AcrossMessageHandler ) Listen (ctx context.Context ) {
63+ msgChn := make (chan * comm.WrappedMessage )
64+ subID := h .comm .Subscribe (comm .AcrossSessionID , comm .AcrossMsg , msgChn )
65+
66+ for {
67+ select {
68+ case wMsg := <- msgChn :
69+ {
70+ acrossMsg , err := tssMessage .UnmarshalAcrossMessage (wMsg .Payload )
71+ if err != nil {
72+ log .Warn ().Msgf ("Failed unmarshaling across message: %s" , err )
73+ continue
74+ }
75+
76+ msg := NewAcrossMessage (acrossMsg .Source , acrossMsg .Destination , AcrossData {
77+ depositId : acrossMsg .DepositId ,
78+ coordinator : wMsg .From ,
79+ })
80+ _ , err = h .HandleMessage (msg )
81+ if err != nil {
82+ log .Err (err ).Msgf ("Failed handling across message %+v because of: %s" , acrossMsg , err )
83+ }
84+ }
85+ case <- ctx .Done ():
86+ {
87+ h .comm .UnSubscribe (subID )
88+ return
89+ }
90+ }
91+ }
5692}
5793
5894// HandleMessage finds the Across deposit with the according deposit ID and starts
5995// the MPC signature process for it. The result will be saved into the signature
6096// cache through the result channel.
6197func (h * AcrossMessageHandler ) HandleMessage (m * message.Message ) (* proposal.Proposal , error ) {
6298 data := m .Data .(AcrossData )
99+ err := h .notify (m , data )
100+ if err != nil {
101+ return nil , err
102+ }
103+
63104 d , err := h .deposit (data .depositId )
64105 if err != nil {
65106 return nil , err
@@ -70,19 +111,28 @@ func (h *AcrossMessageHandler) HandleMessage(m *message.Message) (*proposal.Prop
70111 data .depositId .Text (16 ),
71112 data .depositId .Text (16 ),
72113 h .host ,
73- h .communication ,
114+ h .comm ,
74115 h .fetcher )
75116 if err != nil {
76117 return nil , err
77118 }
78119
79- err = h .coordinator .Execute (context .Background (), []tss.TssProcess {signing }, h .resultChn , h . host . ID () )
120+ err = h .coordinator .Execute (context .Background (), []tss.TssProcess {signing }, h .sigChn , data . coordinator )
80121 if err != nil {
81122 return nil , err
82123 }
83124 return nil , nil
84125}
85126
127+ func (h * AcrossMessageHandler ) notify (m * message.Message , data AcrossData ) error {
128+ msgBytes , err := tssMessage .MarshalAcrossMessage (data .depositId , m .Source , m .Destination )
129+ if err != nil {
130+ return err
131+ }
132+
133+ return h .comm .Broadcast (h .host .Peerstore ().Peers (), msgBytes , comm .AcrossMsg , comm .AcrossSessionID )
134+ }
135+
86136func (h * AcrossMessageHandler ) deposit (depositId * big.Int ) (* events.AcrossDeposit , error ) {
87137 q := ethereum.FilterQuery {
88138 Addresses : []common.Address {
0 commit comments