@@ -2,12 +2,14 @@ package localstatequery
22
33import (
44 "fmt"
5+ "sync"
6+
57 "github.com/cloudstruct/go-ouroboros-network/protocol"
68 "github.com/cloudstruct/go-ouroboros-network/protocol/common"
79 "github.com/cloudstruct/go-ouroboros-network/utils"
8- "sync"
910)
1011
12+ // Client implements the LocalStateQuery client
1113type Client struct {
1214 * protocol.Protocol
1315 config * Config
@@ -21,6 +23,7 @@ type Client struct {
2123 currentEra int
2224}
2325
26+ // NewClient returns a new LocalStateQuery client object
2427func NewClient (protoOptions protocol.ProtocolOptions , cfg * Config ) * Client {
2528 if cfg == nil {
2629 tmpCfg := NewConfig ()
@@ -35,26 +38,26 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
3538 }
3639 // Update state map with timeouts
3740 stateMap := StateMap .Copy ()
38- if entry , ok := stateMap [STATE_ACQUIRING ]; ok {
41+ if entry , ok := stateMap [stateAcquiring ]; ok {
3942 entry .Timeout = c .config .AcquireTimeout
40- stateMap [STATE_ACQUIRING ] = entry
43+ stateMap [stateAcquiring ] = entry
4144 }
42- if entry , ok := stateMap [STATE_QUERYING ]; ok {
45+ if entry , ok := stateMap [stateQuerying ]; ok {
4346 entry .Timeout = c .config .QueryTimeout
44- stateMap [STATE_QUERYING ] = entry
47+ stateMap [stateQuerying ] = entry
4548 }
4649 // Configure underlying Protocol
4750 protoConfig := protocol.ProtocolConfig {
48- Name : PROTOCOL_NAME ,
49- ProtocolId : PROTOCOL_ID ,
51+ Name : protocolName ,
52+ ProtocolId : protocolId ,
5053 Muxer : protoOptions .Muxer ,
5154 ErrorChan : protoOptions .ErrorChan ,
5255 Mode : protoOptions .Mode ,
5356 Role : protocol .ProtocolRoleClient ,
5457 MessageHandlerFunc : c .messageHandler ,
5558 MessageFromCborFunc : NewMsgFromCbor ,
5659 StateMap : stateMap ,
57- InitialState : STATE_IDLE ,
60+ InitialState : stateIdle ,
5861 }
5962 // Enable version-dependent features
6063 if protoOptions .Version >= 10 {
@@ -77,14 +80,14 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
7780func (c * Client ) messageHandler (msg protocol.Message , isResponse bool ) error {
7881 var err error
7982 switch msg .Type () {
80- case MESSAGE_TYPE_ACQUIRED :
83+ case MessageTypeAcquired :
8184 err = c .handleAcquired ()
82- case MESSAGE_TYPE_FAILURE :
85+ case MessageTypeFailure :
8386 err = c .handleFailure (msg )
84- case MESSAGE_TYPE_RESULT :
87+ case MessageTypeResult :
8588 err = c .handleResult (msg )
8689 default :
87- err = fmt .Errorf ("%s: received unexpected message type %d" , PROTOCOL_NAME , msg .Type ())
90+ err = fmt .Errorf ("%s: received unexpected message type %d" , protocolName , msg .Type ())
8891 }
8992 return err
9093}
@@ -99,9 +102,9 @@ func (c *Client) handleAcquired() error {
99102func (c * Client ) handleFailure (msg protocol.Message ) error {
100103 msgFailure := msg .(* MsgFailure )
101104 switch msgFailure .Failure {
102- case ACQUIRE_FAILURE_POINT_TOO_OLD :
105+ case AcquireFailurePointTooOld :
103106 c .acquireResultChan <- AcquireFailurePointTooOldError {}
104- case ACQUIRE_FAILURE_POINT_NOT_ON_CHAIN :
107+ case AcquireFailurePointNotOnChain :
105108 c .acquireResultChan <- AcquireFailurePointNotOnChainError {}
106109 default :
107110 return fmt .Errorf ("unknown failure type: %d" , msgFailure .Failure )
@@ -171,37 +174,41 @@ func (c *Client) getCurrentEra() (int, error) {
171174 if c .currentEra > - 1 {
172175 return c .currentEra , nil
173176 }
174- query := buildHardForkQuery (QUERY_TYPE_HARD_FORK_CURRENT_ERA )
177+ query := buildHardForkQuery (QueryTypeHardForkCurrentEra )
175178 var result int
176179 if err := c .runQuery (query , & result ); err != nil {
177180 return - 1 , err
178181 }
179182 return result , nil
180183}
181184
185+ // Acquire starts the acquire process for the specified chain point
182186func (c * Client ) Acquire (point * common.Point ) error {
183187 c .busyMutex .Lock ()
184188 defer c .busyMutex .Unlock ()
185189 return c .acquire (point )
186190}
187191
192+ // Release releases the previously acquired chain point
188193func (c * Client ) Release () error {
189194 c .busyMutex .Lock ()
190195 defer c .busyMutex .Unlock ()
191196 return c .release ()
192197}
193198
199+ // GetCurrentEra returns the current era ID
194200func (c * Client ) GetCurrentEra () (int , error ) {
195201 c .busyMutex .Lock ()
196202 defer c .busyMutex .Unlock ()
197203 return c .getCurrentEra ()
198204}
199205
206+ // GetSystemStart returns the SystemStart value
200207func (c * Client ) GetSystemStart () (* SystemStartResult , error ) {
201208 c .busyMutex .Lock ()
202209 defer c .busyMutex .Unlock ()
203210 query := buildQuery (
204- QUERY_TYPE_SYSTEM_START ,
211+ QueryTypeSystemStart ,
205212 )
206213 var result SystemStartResult
207214 if err := c .runQuery (query , & result ); err != nil {
@@ -210,11 +217,12 @@ func (c *Client) GetSystemStart() (*SystemStartResult, error) {
210217 return & result , nil
211218}
212219
220+ // GetChainBlockNo returns the latest block number
213221func (c * Client ) GetChainBlockNo () (int64 , error ) {
214222 c .busyMutex .Lock ()
215223 defer c .busyMutex .Unlock ()
216224 query := buildQuery (
217- QUERY_TYPE_CHAIN_BLOCK_NO ,
225+ QueryTypeChainBlockNo ,
218226 )
219227 var result []int64
220228 if err := c .runQuery (query , & result ); err != nil {
@@ -223,11 +231,12 @@ func (c *Client) GetChainBlockNo() (int64, error) {
223231 return result [1 ], nil
224232}
225233
234+ // GetChainPoint returns the current chain tip
226235func (c * Client ) GetChainPoint () (* common.Point , error ) {
227236 c .busyMutex .Lock ()
228237 defer c .busyMutex .Unlock ()
229238 query := buildQuery (
230- QUERY_TYPE_CHAIN_POINT ,
239+ QueryTypeChainPoint ,
231240 )
232241 var result common.Point
233242 if err := c .runQuery (query , & result ); err != nil {
@@ -236,17 +245,19 @@ func (c *Client) GetChainPoint() (*common.Point, error) {
236245 return & result , nil
237246}
238247
248+ // GetEraHistory returns the era history
239249func (c * Client ) GetEraHistory () ([]EraHistoryResult , error ) {
240250 c .busyMutex .Lock ()
241251 defer c .busyMutex .Unlock ()
242- query := buildHardForkQuery (QUERY_TYPE_HARD_FORK_ERA_HISTORY )
252+ query := buildHardForkQuery (QueryTypeHardForkEraHistory )
243253 var result []EraHistoryResult
244254 if err := c .runQuery (query , & result ); err != nil {
245255 return []EraHistoryResult {}, err
246256 }
247257 return result , nil
248258}
249259
260+ // GetEpochNo returns the current epoch number
250261func (c * Client ) GetEpochNo () (int , error ) {
251262 c .busyMutex .Lock ()
252263 defer c .busyMutex .Unlock ()
@@ -256,7 +267,7 @@ func (c *Client) GetEpochNo() (int, error) {
256267 }
257268 query := buildShelleyQuery (
258269 currentEra ,
259- QUERY_TYPE_SHELLEY_EPOCH_NO ,
270+ QueryTypeShelleyEpochNo ,
260271 )
261272 var result []int
262273 if err := c .runQuery (query , & result ); err != nil {
@@ -275,7 +286,7 @@ func (c *Client) GetNonMyopicMemberRewards() (*NonMyopicMemberRewardsResult, err
275286 }
276287 query := buildShelleyQuery (
277288 currentEra ,
278- QUERY_TYPE_SHELLEY_NON_MYOPIC_MEMBER_REWARDS ,
289+ QueryTypeShelleyNonMyopicMemberRewards ,
279290 )
280291 var result NonMyopicMemberRewardsResult
281292 if err := c .runQuery (query , & result ); err != nil {
@@ -284,6 +295,7 @@ func (c *Client) GetNonMyopicMemberRewards() (*NonMyopicMemberRewardsResult, err
284295 return & result , nil
285296}
286297
298+ // GetCurrentProtocolParams returns the set of protocol params that are currently in effect
287299func (c * Client ) GetCurrentProtocolParams () (* CurrentProtocolParamsResult , error ) {
288300 c .busyMutex .Lock ()
289301 defer c .busyMutex .Unlock ()
@@ -293,7 +305,7 @@ func (c *Client) GetCurrentProtocolParams() (*CurrentProtocolParamsResult, error
293305 }
294306 query := buildShelleyQuery (
295307 currentEra ,
296- QUERY_TYPE_SHELLEY_CURRENT_PROTOCOL_PARAMS ,
308+ QueryTypeShelleyCurrentProtocolParams ,
297309 )
298310 var result []CurrentProtocolParamsResult
299311 if err := c .runQuery (query , & result ); err != nil {
@@ -313,7 +325,7 @@ func (c *Client) GetProposedProtocolParamsUpdates() (*ProposedProtocolParamsUpda
313325 }
314326 query := buildShelleyQuery (
315327 currentEra ,
316- QUERY_TYPE_SHELLEY_PROPOSED_PROTOCOL_PARAMS_UPDATES ,
328+ QueryTypeShelleyProposedProtocolParamsUpdates ,
317329 )
318330 var result ProposedProtocolParamsUpdatesResult
319331 if err := c .runQuery (query , & result ); err != nil {
@@ -323,6 +335,7 @@ func (c *Client) GetProposedProtocolParamsUpdates() (*ProposedProtocolParamsUpda
323335
324336}
325337
338+ // GetStakeDistribution returns the stake distribution
326339func (c * Client ) GetStakeDistribution () (* StakeDistributionResult , error ) {
327340 c .busyMutex .Lock ()
328341 defer c .busyMutex .Unlock ()
@@ -332,7 +345,7 @@ func (c *Client) GetStakeDistribution() (*StakeDistributionResult, error) {
332345 }
333346 query := buildShelleyQuery (
334347 currentEra ,
335- QUERY_TYPE_SHELLEY_STAKE_DISTRIBUTION ,
348+ QueryTypeShelleyStakeDistribution ,
336349 )
337350 var result StakeDistributionResult
338351 if err := c .runQuery (query , & result ); err != nil {
@@ -351,7 +364,7 @@ func (c *Client) GetUTxOByAddress(addrs []interface{}) (*UTxOByAddressResult, er
351364 }
352365 query := buildShelleyQuery (
353366 currentEra ,
354- QUERY_TYPE_SHELLEY_UTXO_BY_ADDRESS ,
367+ QueryTypeShelleyUtxoByAddress ,
355368 )
356369 var result UTxOByAddressResult
357370 if err := c .runQuery (query , & result ); err != nil {
@@ -370,7 +383,7 @@ func (c *Client) GetUTxOWhole() (*UTxOWholeResult, error) {
370383 }
371384 query := buildShelleyQuery (
372385 currentEra ,
373- QUERY_TYPE_SHELLEY_UTXO_WHOLE ,
386+ QueryTypeShelleyUtxoWhole ,
374387 )
375388 var result UTxOWholeResult
376389 if err := c .runQuery (query , & result ); err != nil {
@@ -389,7 +402,7 @@ func (c *Client) DebugEpochState() (*DebugEpochStateResult, error) {
389402 }
390403 query := buildShelleyQuery (
391404 currentEra ,
392- QUERY_TYPE_SHELLEY_DEBUG_EPOCH_STATE ,
405+ QueryTypeShelleyDebugEpochState ,
393406 )
394407 var result DebugEpochStateResult
395408 if err := c .runQuery (query , & result ); err != nil {
@@ -408,7 +421,7 @@ func (c *Client) GetFilteredDelegationsAndRewardAccounts(creds []interface{}) (*
408421 }
409422 query := buildShelleyQuery (
410423 currentEra ,
411- QUERY_TYPE_SHELLEY_FILTERED_DELEGATION_AND_REWARD_ACCOUNTS ,
424+ QueryTypeShelleyFilteredDelegationAndRewardAccounts ,
412425 )
413426 var result FilteredDelegationsAndRewardAccountsResult
414427 if err := c .runQuery (query , & result ); err != nil {
@@ -427,7 +440,7 @@ func (c *Client) GetGenesisConfig() (*GenesisConfigResult, error) {
427440 }
428441 query := buildShelleyQuery (
429442 currentEra ,
430- QUERY_TYPE_SHELLEY_GENESIS_CONFIG ,
443+ QueryTypeShelleyGenesisConfig ,
431444 )
432445 var result []GenesisConfigResult
433446 if err := c .runQuery (query , & result ); err != nil {
@@ -446,7 +459,7 @@ func (c *Client) DebugNewEpochState() (*DebugNewEpochStateResult, error) {
446459 }
447460 query := buildShelleyQuery (
448461 currentEra ,
449- QUERY_TYPE_SHELLEY_DEBUG_NEW_EPOCH_STATE ,
462+ QueryTypeShelleyDebugNewEpochState ,
450463 )
451464 var result DebugNewEpochStateResult
452465 if err := c .runQuery (query , & result ); err != nil {
@@ -465,7 +478,7 @@ func (c *Client) DebugChainDepState() (*DebugChainDepStateResult, error) {
465478 }
466479 query := buildShelleyQuery (
467480 currentEra ,
468- QUERY_TYPE_SHELLEY_DEBUG_CHAIN_DEP_STATE ,
481+ QueryTypeShelleyDebugChainDepState ,
469482 )
470483 var result DebugChainDepStateResult
471484 if err := c .runQuery (query , & result ); err != nil {
@@ -484,7 +497,7 @@ func (c *Client) GetRewardProvenance() (*RewardProvenanceResult, error) {
484497 }
485498 query := buildShelleyQuery (
486499 currentEra ,
487- QUERY_TYPE_SHELLEY_REWARD_PROVENANCE ,
500+ QueryTypeShelleyRewardProvenance ,
488501 )
489502 var result RewardProvenanceResult
490503 if err := c .runQuery (query , & result ); err != nil {
@@ -503,7 +516,7 @@ func (c *Client) GetUTxOByTxIn(txins []interface{}) (*UTxOByTxInResult, error) {
503516 }
504517 query := buildShelleyQuery (
505518 currentEra ,
506- QUERY_TYPE_SHELLEY_UTXO_BY_TXIN ,
519+ QueryTypeShelleyUtxoByTxin ,
507520 )
508521 var result UTxOByTxInResult
509522 if err := c .runQuery (query , & result ); err != nil {
@@ -522,7 +535,7 @@ func (c *Client) GetStakePools() (*StakePoolsResult, error) {
522535 }
523536 query := buildShelleyQuery (
524537 currentEra ,
525- QUERY_TYPE_SHELLEY_STAKE_POOLS ,
538+ QueryTypeShelleyStakePools ,
526539 )
527540 var result StakePoolsResult
528541 if err := c .runQuery (query , & result ); err != nil {
@@ -541,7 +554,7 @@ func (c *Client) GetStakePoolParams(poolIds []interface{}) (*StakePoolParamsResu
541554 }
542555 query := buildShelleyQuery (
543556 currentEra ,
544- QUERY_TYPE_SHELLEY_STAKE_POOL_PARAMS ,
557+ QueryTypeShelleyStakePoolParams ,
545558 )
546559 var result StakePoolParamsResult
547560 if err := c .runQuery (query , & result ); err != nil {
@@ -560,7 +573,7 @@ func (c *Client) GetRewardInfoPools() (*RewardInfoPoolsResult, error) {
560573 }
561574 query := buildShelleyQuery (
562575 currentEra ,
563- QUERY_TYPE_SHELLEY_REWARD_INFO_POOLS ,
576+ QueryTypeShelleyRewardInfoPools ,
564577 )
565578 var result RewardInfoPoolsResult
566579 if err := c .runQuery (query , & result ); err != nil {
@@ -579,7 +592,7 @@ func (c *Client) GetPoolState(poolIds []interface{}) (*PoolStateResult, error) {
579592 }
580593 query := buildShelleyQuery (
581594 currentEra ,
582- QUERY_TYPE_SHELLEY_POOL_STATE ,
595+ QueryTypeShelleyPoolState ,
583596 )
584597 var result PoolStateResult
585598 if err := c .runQuery (query , & result ); err != nil {
@@ -598,7 +611,7 @@ func (c *Client) GetStakeSnapshots(poolId interface{}) (*StakeSnapshotsResult, e
598611 }
599612 query := buildShelleyQuery (
600613 currentEra ,
601- QUERY_TYPE_SHELLEY_STAKE_SNAPSHOTS ,
614+ QueryTypeShelleyStakeSnapshots ,
602615 )
603616 var result StakeSnapshotsResult
604617 if err := c .runQuery (query , & result ); err != nil {
@@ -617,7 +630,7 @@ func (c *Client) GetPoolDistr(poolIds []interface{}) (*PoolDistrResult, error) {
617630 }
618631 query := buildShelleyQuery (
619632 currentEra ,
620- QUERY_TYPE_SHELLEY_POOL_DISTR ,
633+ QueryTypeShelleyPoolDistr ,
621634 )
622635 var result PoolDistrResult
623636 if err := c .runQuery (query , & result ); err != nil {
0 commit comments