github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/bft/appconn/multi_app_conn.go (about)

     1  package appconn
     2  
     3  import (
     4  	abcicli "github.com/gnolang/gno/tm2/pkg/bft/abci/client"
     5  	"github.com/gnolang/gno/tm2/pkg/errors"
     6  	"github.com/gnolang/gno/tm2/pkg/service"
     7  )
     8  
     9  //-----------------------------
    10  
    11  // Tendermint's interface to the application consists of multiple connections
    12  type AppConns interface {
    13  	service.Service
    14  
    15  	Mempool() Mempool
    16  	Consensus() Consensus
    17  	Query() Query
    18  }
    19  
    20  // NewABCIClient returns newly connected client
    21  type ClientCreator interface {
    22  	NewABCIClient() (abcicli.Client, error)
    23  }
    24  
    25  func NewAppConns(clientCreator ClientCreator) AppConns {
    26  	return NewMulti(clientCreator)
    27  }
    28  
    29  //-----------------------------
    30  // multi implements AppConns
    31  
    32  // a multi is made of a few appConns (mempool, consensus, query)
    33  // and manages their underlying abci clients
    34  // TODO: on app restart, clients must reboot together
    35  type multi struct {
    36  	service.BaseService
    37  
    38  	mempoolConn   *mempool
    39  	consensusConn *consensus
    40  	queryConn     *query
    41  
    42  	clientCreator ClientCreator
    43  }
    44  
    45  // Make all necessary abci connections to the application
    46  func NewMulti(clientCreator ClientCreator) *multi {
    47  	multi := &multi{
    48  		clientCreator: clientCreator,
    49  	}
    50  	multi.BaseService = *service.NewBaseService(nil, "multi", multi)
    51  	return multi
    52  }
    53  
    54  // Returns the mempool connection
    55  func (app *multi) Mempool() Mempool {
    56  	return app.mempoolConn
    57  }
    58  
    59  // Returns the consensus Connection
    60  func (app *multi) Consensus() Consensus {
    61  	return app.consensusConn
    62  }
    63  
    64  // Returns the query Connection
    65  func (app *multi) Query() Query {
    66  	return app.queryConn
    67  }
    68  
    69  func (app *multi) OnStart() error {
    70  	// query connection
    71  	querycli, err := app.clientCreator.NewABCIClient()
    72  	if err != nil {
    73  		return errors.Wrap(err, "Error creating ABCI client (query connection)")
    74  	}
    75  	querycli.SetLogger(app.Logger.With("module", "abci-client", "connection", "query"))
    76  	if err := querycli.Start(); err != nil {
    77  		return errors.Wrap(err, "Error starting ABCI client (query connection)")
    78  	}
    79  	app.queryConn = NewQuery(querycli)
    80  
    81  	// mempool connection
    82  	memcli, err := app.clientCreator.NewABCIClient()
    83  	if err != nil {
    84  		return errors.Wrap(err, "Error creating ABCI client (mempool connection)")
    85  	}
    86  	memcli.SetLogger(app.Logger.With("module", "abci-client", "connection", "mempool"))
    87  	if err := memcli.Start(); err != nil {
    88  		return errors.Wrap(err, "Error starting ABCI client (mempool connection)")
    89  	}
    90  	app.mempoolConn = NewMempool(memcli)
    91  
    92  	// consensus connection
    93  	concli, err := app.clientCreator.NewABCIClient()
    94  	if err != nil {
    95  		return errors.Wrap(err, "Error creating ABCI client (consensus connection)")
    96  	}
    97  	concli.SetLogger(app.Logger.With("module", "abci-client", "connection", "consensus"))
    98  	if err := concli.Start(); err != nil {
    99  		return errors.Wrap(err, "Error starting ABCI client (consensus connection)")
   100  	}
   101  	app.consensusConn = NewConsensus(concli)
   102  
   103  	return nil
   104  }