github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/testing/path.go (about)

     1  package ibctesting
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types"
     8  )
     9  
    10  // Path contains two endpoints representing two chains connected over IBC
    11  type Path struct {
    12  	EndpointA *Endpoint
    13  	EndpointB *Endpoint
    14  }
    15  
    16  // NewPath constructs an endpoint for each chain using the default values
    17  // for the endpoints. Each endpoint is updated to have a pointer to the
    18  // counterparty endpoint.
    19  func NewPath(chainA, chainB TestChainI) *Path {
    20  	endpointA := NewDefaultEndpoint(chainA)
    21  	endpointB := NewDefaultEndpoint(chainB)
    22  
    23  	endpointA.Counterparty = endpointB
    24  	endpointB.Counterparty = endpointA
    25  
    26  	return &Path{
    27  		EndpointA: endpointA,
    28  		EndpointB: endpointB,
    29  	}
    30  }
    31  
    32  // SetChannelOrdered sets the channel order for both endpoints to ORDERED.
    33  func (path *Path) SetChannelOrdered() {
    34  	path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED
    35  	path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED
    36  }
    37  
    38  // RelayPacket attempts to relay the packet first on EndpointA and then on EndpointB
    39  // if EndpointA does not contain a packet commitment for that packet. An error is returned
    40  // if a relay step fails or the packet commitment does not exist on either endpoint.
    41  func (path *Path) RelayPacket(packet channeltypes.Packet, ack []byte) error {
    42  	pc := path.EndpointA.Chain.App().GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
    43  	if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointA.Chain.App().AppCodec(), packet)) {
    44  
    45  		// packet found, relay from A to B
    46  		path.EndpointB.UpdateClient()
    47  
    48  		if err := path.EndpointB.RecvPacket(packet); err != nil {
    49  			return err
    50  		}
    51  		if ack == nil {
    52  			res, err := path.EndpointB.RecvPacketWithResult(packet)
    53  			if err != nil {
    54  				return err
    55  			}
    56  
    57  			ack2, err := ParseAckFromEvents(res.Events)
    58  			if err != nil {
    59  				return err
    60  			}
    61  			ack = ack2
    62  		}
    63  		if err := path.EndpointA.AcknowledgePacket(packet, ack); err != nil {
    64  			return err
    65  		}
    66  		return nil
    67  
    68  	}
    69  
    70  	pc = path.EndpointB.Chain.App().GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointB.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
    71  	if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointB.Chain.App().AppCodec(), packet)) {
    72  
    73  		// packet found, relay B to A
    74  		path.EndpointA.UpdateClient()
    75  
    76  		if err := path.EndpointA.RecvPacket(packet); err != nil {
    77  			return err
    78  		}
    79  		if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil {
    80  			return err
    81  		}
    82  		return nil
    83  	}
    84  
    85  	return fmt.Errorf("packet commitment does not exist on either endpoint for provided packet")
    86  }
    87  
    88  func (path *Path) RelayPacketV4(packet channeltypes.Packet) error {
    89  	pc := path.EndpointA.Chain.GetSimApp().GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
    90  	if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointA.Chain.GetSimApp().AppCodec(), packet)) {
    91  
    92  		// packet found, relay from A to B
    93  		if err := path.EndpointB.UpdateClient(); err != nil {
    94  			return err
    95  		}
    96  
    97  		res, err := path.EndpointB.RecvPacketWithResult(packet)
    98  		if err != nil {
    99  			return err
   100  		}
   101  
   102  		ack, err := ParseAckFromEvents(res.Events)
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		if err := path.EndpointA.AcknowledgePacket(packet, ack); err != nil {
   108  			return err
   109  		}
   110  
   111  		return nil
   112  	}
   113  
   114  	pc = path.EndpointB.Chain.GetSimApp().GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointB.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
   115  	if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointB.Chain.GetSimApp().AppCodec(), packet)) {
   116  
   117  		// packet found, relay B to A
   118  		if err := path.EndpointA.UpdateClient(); err != nil {
   119  			return err
   120  		}
   121  
   122  		res, err := path.EndpointA.RecvPacketWithResult(packet)
   123  		if err != nil {
   124  			return err
   125  		}
   126  
   127  		ack, err := ParseAckFromEvents(res.Events)
   128  		if err != nil {
   129  			return err
   130  		}
   131  
   132  		if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil {
   133  			return err
   134  		}
   135  		return nil
   136  	}
   137  
   138  	return fmt.Errorf("packet commitment does not exist on either endpoint for provided packet")
   139  }