github.com/516108736/tendermint@v0.36.0/proxy/client.go (about)

     1  package proxy
     2  
     3  import (
     4  	"fmt"
     5  
     6  	abcicli "github.com/tendermint/tendermint/abci/client"
     7  	"github.com/tendermint/tendermint/abci/example/counter"
     8  	"github.com/tendermint/tendermint/abci/example/kvstore"
     9  	"github.com/tendermint/tendermint/abci/types"
    10  	tmsync "github.com/tendermint/tendermint/libs/sync"
    11  )
    12  
    13  // ClientCreator creates new ABCI clients.
    14  type ClientCreator interface {
    15  	// NewABCIClient returns a new ABCI client.
    16  	NewABCIClient() (abcicli.Client, error)
    17  }
    18  
    19  //----------------------------------------------------
    20  // local proxy uses a mutex on an in-proc app
    21  
    22  type localClientCreator struct {
    23  	mtx *tmsync.Mutex
    24  	app types.Application
    25  }
    26  
    27  // NewLocalClientCreator returns a ClientCreator for the given app,
    28  // which will be running locally.
    29  func NewLocalClientCreator(app types.Application) ClientCreator {
    30  	return &localClientCreator{
    31  		mtx: new(tmsync.Mutex),
    32  		app: app,
    33  	}
    34  }
    35  
    36  func (l *localClientCreator) NewABCIClient() (abcicli.Client, error) {
    37  	return abcicli.NewLocalClient(l.mtx, l.app), nil
    38  }
    39  
    40  //---------------------------------------------------------------
    41  // remote proxy opens new connections to an external app process
    42  
    43  type remoteClientCreator struct {
    44  	addr        string
    45  	transport   string
    46  	mustConnect bool
    47  }
    48  
    49  // NewRemoteClientCreator returns a ClientCreator for the given address (e.g.
    50  // "192.168.0.1") and transport (e.g. "tcp"). Set mustConnect to true if you
    51  // want the client to connect before reporting success.
    52  func NewRemoteClientCreator(addr, transport string, mustConnect bool) ClientCreator {
    53  	return &remoteClientCreator{
    54  		addr:        addr,
    55  		transport:   transport,
    56  		mustConnect: mustConnect,
    57  	}
    58  }
    59  
    60  func (r *remoteClientCreator) NewABCIClient() (abcicli.Client, error) {
    61  	remoteApp, err := abcicli.NewClient(r.addr, r.transport, r.mustConnect)
    62  	if err != nil {
    63  		return nil, fmt.Errorf("failed to connect to proxy: %w", err)
    64  	}
    65  
    66  	return remoteApp, nil
    67  }
    68  
    69  // DefaultClientCreator returns a default ClientCreator, which will create a
    70  // local client if addr is one of: 'counter', 'counter_serial', 'kvstore',
    71  // 'persistent_kvstore' or 'noop', otherwise - a remote client.
    72  func DefaultClientCreator(addr, transport, dbDir string) ClientCreator {
    73  	switch addr {
    74  	case "counter":
    75  		return NewLocalClientCreator(counter.NewApplication(false))
    76  	case "counter_serial":
    77  		return NewLocalClientCreator(counter.NewApplication(true))
    78  	case "kvstore":
    79  		return NewLocalClientCreator(kvstore.NewApplication())
    80  	case "persistent_kvstore":
    81  		return NewLocalClientCreator(kvstore.NewPersistentKVStoreApplication(dbDir))
    82  	case "noop":
    83  		return NewLocalClientCreator(types.NewBaseApplication())
    84  	default:
    85  		mustConnect := false // loop retrying
    86  		return NewRemoteClientCreator(addr, transport, mustConnect)
    87  	}
    88  }