github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/abci/client/client.go (about) 1 package abcicli 2 3 import ( 4 "fmt" 5 "sync" 6 7 "github.com/badrootd/celestia-core/abci/types" 8 "github.com/badrootd/celestia-core/libs/service" 9 cmtsync "github.com/badrootd/celestia-core/libs/sync" 10 ) 11 12 const ( 13 dialRetryIntervalSeconds = 3 14 echoRetryIntervalSeconds = 1 15 ) 16 17 //go:generate ../../scripts/mockery_generate.sh Client 18 19 // Client defines an interface for an ABCI client. 20 // All `Async` methods return a `ReqRes` object. 21 // All `Sync` methods return the appropriate protobuf ResponseXxx struct and an error. 22 // Note these are client errors, eg. ABCI socket connectivity issues. 23 // Application-related errors are reflected in response via ABCI error codes and logs. 24 type Client interface { 25 service.Service 26 27 SetResponseCallback(Callback) 28 Error() error 29 30 FlushAsync() *ReqRes 31 EchoAsync(msg string) *ReqRes 32 InfoAsync(types.RequestInfo) *ReqRes 33 DeliverTxAsync(types.RequestDeliverTx) *ReqRes 34 CheckTxAsync(types.RequestCheckTx) *ReqRes 35 QueryAsync(types.RequestQuery) *ReqRes 36 CommitAsync() *ReqRes 37 InitChainAsync(types.RequestInitChain) *ReqRes 38 BeginBlockAsync(types.RequestBeginBlock) *ReqRes 39 EndBlockAsync(types.RequestEndBlock) *ReqRes 40 ListSnapshotsAsync(types.RequestListSnapshots) *ReqRes 41 OfferSnapshotAsync(types.RequestOfferSnapshot) *ReqRes 42 LoadSnapshotChunkAsync(types.RequestLoadSnapshotChunk) *ReqRes 43 ApplySnapshotChunkAsync(types.RequestApplySnapshotChunk) *ReqRes 44 45 FlushSync() error 46 EchoSync(msg string) (*types.ResponseEcho, error) 47 InfoSync(types.RequestInfo) (*types.ResponseInfo, error) 48 DeliverTxSync(types.RequestDeliverTx) (*types.ResponseDeliverTx, error) 49 CheckTxSync(types.RequestCheckTx) (*types.ResponseCheckTx, error) 50 QuerySync(types.RequestQuery) (*types.ResponseQuery, error) 51 CommitSync() (*types.ResponseCommit, error) 52 InitChainSync(types.RequestInitChain) (*types.ResponseInitChain, error) 53 BeginBlockSync(types.RequestBeginBlock) (*types.ResponseBeginBlock, error) 54 EndBlockSync(types.RequestEndBlock) (*types.ResponseEndBlock, error) 55 ListSnapshotsSync(types.RequestListSnapshots) (*types.ResponseListSnapshots, error) 56 OfferSnapshotSync(types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error) 57 LoadSnapshotChunkSync(types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) 58 ApplySnapshotChunkSync(types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) 59 PrepareProposalSync(types.RequestPrepareProposal) (*types.ResponsePrepareProposal, error) 60 ProcessProposalSync(types.RequestProcessProposal) (*types.ResponseProcessProposal, error) 61 } 62 63 //---------------------------------------- 64 65 // NewClient returns a new ABCI client of the specified transport type. 66 // It returns an error if the transport is not "socket" or "grpc" 67 func NewClient(addr, transport string, mustConnect bool) (client Client, err error) { 68 switch transport { 69 case "socket": 70 client = NewSocketClient(addr, mustConnect) 71 case "grpc": 72 client = NewGRPCClient(addr, mustConnect) 73 default: 74 err = fmt.Errorf("unknown abci transport %s", transport) 75 } 76 return 77 } 78 79 type Callback func(*types.Request, *types.Response) 80 81 type ReqRes struct { 82 *types.Request 83 *sync.WaitGroup 84 *types.Response // Not set atomically, so be sure to use WaitGroup. 85 86 mtx cmtsync.Mutex 87 88 // callbackInvoked as a variable to track if the callback was already 89 // invoked during the regular execution of the request. This variable 90 // allows clients to set the callback simultaneously without potentially 91 // invoking the callback twice by accident, once when 'SetCallback' is 92 // called and once during the normal request. 93 callbackInvoked bool 94 cb func(*types.Response) // A single callback that may be set. 95 } 96 97 func NewReqRes(req *types.Request) *ReqRes { 98 return &ReqRes{ 99 Request: req, 100 WaitGroup: waitGroup1(), 101 Response: nil, 102 103 callbackInvoked: false, 104 cb: nil, 105 } 106 } 107 108 // Sets sets the callback. If reqRes is already done, it will call the cb 109 // immediately. Note, reqRes.cb should not change if reqRes.done and only one 110 // callback is supported. 111 func (r *ReqRes) SetCallback(cb func(res *types.Response)) { 112 r.mtx.Lock() 113 114 if r.callbackInvoked { 115 r.mtx.Unlock() 116 cb(r.Response) 117 return 118 } 119 120 r.cb = cb 121 r.mtx.Unlock() 122 } 123 124 // InvokeCallback invokes a thread-safe execution of the configured callback 125 // if non-nil. 126 func (r *ReqRes) InvokeCallback() { 127 r.mtx.Lock() 128 defer r.mtx.Unlock() 129 130 if r.cb != nil { 131 r.cb(r.Response) 132 } 133 r.callbackInvoked = true 134 } 135 136 // GetCallback returns the configured callback of the ReqRes object which may be 137 // nil. Note, it is not safe to concurrently call this in cases where it is 138 // marked done and SetCallback is called before calling GetCallback as that 139 // will invoke the callback twice and create a potential race condition. 140 // 141 // ref: https://github.com/cometbft/cometbft/issues/5439 142 func (r *ReqRes) GetCallback() func(*types.Response) { 143 r.mtx.Lock() 144 defer r.mtx.Unlock() 145 return r.cb 146 } 147 148 func waitGroup1() (wg *sync.WaitGroup) { 149 wg = &sync.WaitGroup{} 150 wg.Add(1) 151 return 152 }