diff --git a/protocol/localstatequery/client.go b/protocol/localstatequery/client.go index 1faa0ff8..50b19f35 100644 --- a/protocol/localstatequery/client.go +++ b/protocol/localstatequery/client.go @@ -2,12 +2,14 @@ package localstatequery import ( "fmt" + "sync" + "github.com/cloudstruct/go-ouroboros-network/protocol" "github.com/cloudstruct/go-ouroboros-network/protocol/common" "github.com/cloudstruct/go-ouroboros-network/utils" - "sync" ) +// Client implements the LocalStateQuery client type Client struct { *protocol.Protocol config *Config @@ -21,6 +23,7 @@ type Client struct { currentEra int } +// NewClient returns a new LocalStateQuery client object func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client { if cfg == nil { tmpCfg := NewConfig() @@ -35,18 +38,18 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client { } // Update state map with timeouts stateMap := StateMap.Copy() - if entry, ok := stateMap[STATE_ACQUIRING]; ok { + if entry, ok := stateMap[stateAcquiring]; ok { entry.Timeout = c.config.AcquireTimeout - stateMap[STATE_ACQUIRING] = entry + stateMap[stateAcquiring] = entry } - if entry, ok := stateMap[STATE_QUERYING]; ok { + if entry, ok := stateMap[stateQuerying]; ok { entry.Timeout = c.config.QueryTimeout - stateMap[STATE_QUERYING] = entry + stateMap[stateQuerying] = entry } // Configure underlying Protocol protoConfig := protocol.ProtocolConfig{ - Name: PROTOCOL_NAME, - ProtocolId: PROTOCOL_ID, + Name: protocolName, + ProtocolId: protocolId, Muxer: protoOptions.Muxer, ErrorChan: protoOptions.ErrorChan, Mode: protoOptions.Mode, @@ -54,7 +57,7 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client { MessageHandlerFunc: c.messageHandler, MessageFromCborFunc: NewMsgFromCbor, StateMap: stateMap, - InitialState: STATE_IDLE, + InitialState: stateIdle, } // Enable version-dependent features if protoOptions.Version >= 10 { @@ -77,14 +80,14 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client { func (c *Client) messageHandler(msg protocol.Message, isResponse bool) error { var err error switch msg.Type() { - case MESSAGE_TYPE_ACQUIRED: + case MessageTypeAcquired: err = c.handleAcquired() - case MESSAGE_TYPE_FAILURE: + case MessageTypeFailure: err = c.handleFailure(msg) - case MESSAGE_TYPE_RESULT: + case MessageTypeResult: err = c.handleResult(msg) default: - err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type()) + err = fmt.Errorf("%s: received unexpected message type %d", protocolName, msg.Type()) } return err } @@ -99,9 +102,9 @@ func (c *Client) handleAcquired() error { func (c *Client) handleFailure(msg protocol.Message) error { msgFailure := msg.(*MsgFailure) switch msgFailure.Failure { - case ACQUIRE_FAILURE_POINT_TOO_OLD: + case AcquireFailurePointTooOld: c.acquireResultChan <- AcquireFailurePointTooOldError{} - case ACQUIRE_FAILURE_POINT_NOT_ON_CHAIN: + case AcquireFailurePointNotOnChain: c.acquireResultChan <- AcquireFailurePointNotOnChainError{} default: return fmt.Errorf("unknown failure type: %d", msgFailure.Failure) @@ -171,7 +174,7 @@ func (c *Client) getCurrentEra() (int, error) { if c.currentEra > -1 { return c.currentEra, nil } - query := buildHardForkQuery(QUERY_TYPE_HARD_FORK_CURRENT_ERA) + query := buildHardForkQuery(QueryTypeHardForkCurrentEra) var result int if err := c.runQuery(query, &result); err != nil { return -1, err @@ -179,29 +182,33 @@ func (c *Client) getCurrentEra() (int, error) { return result, nil } +// Acquire starts the acquire process for the specified chain point func (c *Client) Acquire(point *common.Point) error { c.busyMutex.Lock() defer c.busyMutex.Unlock() return c.acquire(point) } +// Release releases the previously acquired chain point func (c *Client) Release() error { c.busyMutex.Lock() defer c.busyMutex.Unlock() return c.release() } +// GetCurrentEra returns the current era ID func (c *Client) GetCurrentEra() (int, error) { c.busyMutex.Lock() defer c.busyMutex.Unlock() return c.getCurrentEra() } +// GetSystemStart returns the SystemStart value func (c *Client) GetSystemStart() (*SystemStartResult, error) { c.busyMutex.Lock() defer c.busyMutex.Unlock() query := buildQuery( - QUERY_TYPE_SYSTEM_START, + QueryTypeSystemStart, ) var result SystemStartResult if err := c.runQuery(query, &result); err != nil { @@ -210,11 +217,12 @@ func (c *Client) GetSystemStart() (*SystemStartResult, error) { return &result, nil } +// GetChainBlockNo returns the latest block number func (c *Client) GetChainBlockNo() (int64, error) { c.busyMutex.Lock() defer c.busyMutex.Unlock() query := buildQuery( - QUERY_TYPE_CHAIN_BLOCK_NO, + QueryTypeChainBlockNo, ) var result []int64 if err := c.runQuery(query, &result); err != nil { @@ -223,11 +231,12 @@ func (c *Client) GetChainBlockNo() (int64, error) { return result[1], nil } +// GetChainPoint returns the current chain tip func (c *Client) GetChainPoint() (*common.Point, error) { c.busyMutex.Lock() defer c.busyMutex.Unlock() query := buildQuery( - QUERY_TYPE_CHAIN_POINT, + QueryTypeChainPoint, ) var result common.Point if err := c.runQuery(query, &result); err != nil { @@ -236,10 +245,11 @@ func (c *Client) GetChainPoint() (*common.Point, error) { return &result, nil } +// GetEraHistory returns the era history func (c *Client) GetEraHistory() ([]EraHistoryResult, error) { c.busyMutex.Lock() defer c.busyMutex.Unlock() - query := buildHardForkQuery(QUERY_TYPE_HARD_FORK_ERA_HISTORY) + query := buildHardForkQuery(QueryTypeHardForkEraHistory) var result []EraHistoryResult if err := c.runQuery(query, &result); err != nil { return []EraHistoryResult{}, err @@ -247,6 +257,7 @@ func (c *Client) GetEraHistory() ([]EraHistoryResult, error) { return result, nil } +// GetEpochNo returns the current epoch number func (c *Client) GetEpochNo() (int, error) { c.busyMutex.Lock() defer c.busyMutex.Unlock() @@ -256,7 +267,7 @@ func (c *Client) GetEpochNo() (int, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_EPOCH_NO, + QueryTypeShelleyEpochNo, ) var result []int if err := c.runQuery(query, &result); err != nil { @@ -275,7 +286,7 @@ func (c *Client) GetNonMyopicMemberRewards() (*NonMyopicMemberRewardsResult, err } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_NON_MYOPIC_MEMBER_REWARDS, + QueryTypeShelleyNonMyopicMemberRewards, ) var result NonMyopicMemberRewardsResult if err := c.runQuery(query, &result); err != nil { @@ -284,6 +295,7 @@ func (c *Client) GetNonMyopicMemberRewards() (*NonMyopicMemberRewardsResult, err return &result, nil } +// GetCurrentProtocolParams returns the set of protocol params that are currently in effect func (c *Client) GetCurrentProtocolParams() (*CurrentProtocolParamsResult, error) { c.busyMutex.Lock() defer c.busyMutex.Unlock() @@ -293,7 +305,7 @@ func (c *Client) GetCurrentProtocolParams() (*CurrentProtocolParamsResult, error } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_CURRENT_PROTOCOL_PARAMS, + QueryTypeShelleyCurrentProtocolParams, ) var result []CurrentProtocolParamsResult if err := c.runQuery(query, &result); err != nil { @@ -313,7 +325,7 @@ func (c *Client) GetProposedProtocolParamsUpdates() (*ProposedProtocolParamsUpda } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_PROPOSED_PROTOCOL_PARAMS_UPDATES, + QueryTypeShelleyProposedProtocolParamsUpdates, ) var result ProposedProtocolParamsUpdatesResult if err := c.runQuery(query, &result); err != nil { @@ -323,6 +335,7 @@ func (c *Client) GetProposedProtocolParamsUpdates() (*ProposedProtocolParamsUpda } +// GetStakeDistribution returns the stake distribution func (c *Client) GetStakeDistribution() (*StakeDistributionResult, error) { c.busyMutex.Lock() defer c.busyMutex.Unlock() @@ -332,7 +345,7 @@ func (c *Client) GetStakeDistribution() (*StakeDistributionResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_STAKE_DISTRIBUTION, + QueryTypeShelleyStakeDistribution, ) var result StakeDistributionResult if err := c.runQuery(query, &result); err != nil { @@ -351,7 +364,7 @@ func (c *Client) GetUTxOByAddress(addrs []interface{}) (*UTxOByAddressResult, er } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_UTXO_BY_ADDRESS, + QueryTypeShelleyUtxoByAddress, ) var result UTxOByAddressResult if err := c.runQuery(query, &result); err != nil { @@ -370,7 +383,7 @@ func (c *Client) GetUTxOWhole() (*UTxOWholeResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_UTXO_WHOLE, + QueryTypeShelleyUtxoWhole, ) var result UTxOWholeResult if err := c.runQuery(query, &result); err != nil { @@ -389,7 +402,7 @@ func (c *Client) DebugEpochState() (*DebugEpochStateResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_DEBUG_EPOCH_STATE, + QueryTypeShelleyDebugEpochState, ) var result DebugEpochStateResult if err := c.runQuery(query, &result); err != nil { @@ -408,7 +421,7 @@ func (c *Client) GetFilteredDelegationsAndRewardAccounts(creds []interface{}) (* } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_FILTERED_DELEGATION_AND_REWARD_ACCOUNTS, + QueryTypeShelleyFilteredDelegationAndRewardAccounts, ) var result FilteredDelegationsAndRewardAccountsResult if err := c.runQuery(query, &result); err != nil { @@ -427,7 +440,7 @@ func (c *Client) GetGenesisConfig() (*GenesisConfigResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_GENESIS_CONFIG, + QueryTypeShelleyGenesisConfig, ) var result []GenesisConfigResult if err := c.runQuery(query, &result); err != nil { @@ -446,7 +459,7 @@ func (c *Client) DebugNewEpochState() (*DebugNewEpochStateResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_DEBUG_NEW_EPOCH_STATE, + QueryTypeShelleyDebugNewEpochState, ) var result DebugNewEpochStateResult if err := c.runQuery(query, &result); err != nil { @@ -465,7 +478,7 @@ func (c *Client) DebugChainDepState() (*DebugChainDepStateResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_DEBUG_CHAIN_DEP_STATE, + QueryTypeShelleyDebugChainDepState, ) var result DebugChainDepStateResult if err := c.runQuery(query, &result); err != nil { @@ -484,7 +497,7 @@ func (c *Client) GetRewardProvenance() (*RewardProvenanceResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_REWARD_PROVENANCE, + QueryTypeShelleyRewardProvenance, ) var result RewardProvenanceResult if err := c.runQuery(query, &result); err != nil { @@ -503,7 +516,7 @@ func (c *Client) GetUTxOByTxIn(txins []interface{}) (*UTxOByTxInResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_UTXO_BY_TXIN, + QueryTypeShelleyUtxoByTxin, ) var result UTxOByTxInResult if err := c.runQuery(query, &result); err != nil { @@ -522,7 +535,7 @@ func (c *Client) GetStakePools() (*StakePoolsResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_STAKE_POOLS, + QueryTypeShelleyStakePools, ) var result StakePoolsResult if err := c.runQuery(query, &result); err != nil { @@ -541,7 +554,7 @@ func (c *Client) GetStakePoolParams(poolIds []interface{}) (*StakePoolParamsResu } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_STAKE_POOL_PARAMS, + QueryTypeShelleyStakePoolParams, ) var result StakePoolParamsResult if err := c.runQuery(query, &result); err != nil { @@ -560,7 +573,7 @@ func (c *Client) GetRewardInfoPools() (*RewardInfoPoolsResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_REWARD_INFO_POOLS, + QueryTypeShelleyRewardInfoPools, ) var result RewardInfoPoolsResult if err := c.runQuery(query, &result); err != nil { @@ -579,7 +592,7 @@ func (c *Client) GetPoolState(poolIds []interface{}) (*PoolStateResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_POOL_STATE, + QueryTypeShelleyPoolState, ) var result PoolStateResult if err := c.runQuery(query, &result); err != nil { @@ -598,7 +611,7 @@ func (c *Client) GetStakeSnapshots(poolId interface{}) (*StakeSnapshotsResult, e } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_STAKE_SNAPSHOTS, + QueryTypeShelleyStakeSnapshots, ) var result StakeSnapshotsResult if err := c.runQuery(query, &result); err != nil { @@ -617,7 +630,7 @@ func (c *Client) GetPoolDistr(poolIds []interface{}) (*PoolDistrResult, error) { } query := buildShelleyQuery( currentEra, - QUERY_TYPE_SHELLEY_POOL_DISTR, + QueryTypeShelleyPoolDistr, ) var result PoolDistrResult if err := c.runQuery(query, &result); err != nil { diff --git a/protocol/localstatequery/error.go b/protocol/localstatequery/error.go index c7c27796..c8f37d9d 100644 --- a/protocol/localstatequery/error.go +++ b/protocol/localstatequery/error.go @@ -1,5 +1,6 @@ package localstatequery +// AcquireFailurePointTooOldError indicates a failure to acquire a point due to it being too old type AcquireFailurePointTooOldError struct { } @@ -7,6 +8,7 @@ func (e AcquireFailurePointTooOldError) Error() string { return "acquire failure: point too old" } +// AcquireFailurePointNotOnChainError indicates a failure to acquire a point due to it not being present on the chain type AcquireFailurePointNotOnChainError struct { } diff --git a/protocol/localstatequery/localstatequery.go b/protocol/localstatequery/localstatequery.go index 21dc4d10..42ea2c6e 100644 --- a/protocol/localstatequery/localstatequery.go +++ b/protocol/localstatequery/localstatequery.go @@ -1,3 +1,4 @@ +// Package localstatequery implements the Ouroboros local-state-query protocol package localstatequery import ( @@ -6,90 +7,94 @@ import ( "github.com/cloudstruct/go-ouroboros-network/protocol" ) +// Protocol identifiers const ( - PROTOCOL_NAME = "local-state-query" - PROTOCOL_ID uint16 = 7 + protocolName = "local-state-query" + protocolId uint16 = 7 ) var ( - STATE_IDLE = protocol.NewState(1, "Idle") - STATE_ACQUIRING = protocol.NewState(2, "Acquiring") - STATE_ACQUIRED = protocol.NewState(3, "Acquired") - STATE_QUERYING = protocol.NewState(4, "Querying") - STATE_DONE = protocol.NewState(5, "Done") + stateIdle = protocol.NewState(1, "Idle") + stateAcquiring = protocol.NewState(2, "Acquiring") + stateAcquired = protocol.NewState(3, "Acquired") + stateQuerying = protocol.NewState(4, "Querying") + stateDone = protocol.NewState(5, "Done") ) +// LocalStateQuery protocol state machine var StateMap = protocol.StateMap{ - STATE_IDLE: protocol.StateMapEntry{ + stateIdle: protocol.StateMapEntry{ Agency: protocol.AgencyClient, Transitions: []protocol.StateTransition{ { - MsgType: MESSAGE_TYPE_ACQUIRE, - NewState: STATE_ACQUIRING, + MsgType: MessageTypeAcquire, + NewState: stateAcquiring, }, { - MsgType: MESSAGE_TYPE_ACQUIRE_NO_POINT, - NewState: STATE_ACQUIRING, + MsgType: MessageTypeAcquireNoPoint, + NewState: stateAcquiring, }, { - MsgType: MESSAGE_TYPE_DONE, - NewState: STATE_DONE, + MsgType: MessageTypeDone, + NewState: stateDone, }, }, }, - STATE_ACQUIRING: protocol.StateMapEntry{ + stateAcquiring: protocol.StateMapEntry{ Agency: protocol.AgencyServer, Transitions: []protocol.StateTransition{ { - MsgType: MESSAGE_TYPE_FAILURE, - NewState: STATE_IDLE, + MsgType: MessageTypeFailure, + NewState: stateIdle, }, { - MsgType: MESSAGE_TYPE_ACQUIRED, - NewState: STATE_ACQUIRED, + MsgType: MessageTypeAcquired, + NewState: stateAcquired, }, }, }, - STATE_ACQUIRED: protocol.StateMapEntry{ + stateAcquired: protocol.StateMapEntry{ Agency: protocol.AgencyClient, Transitions: []protocol.StateTransition{ { - MsgType: MESSAGE_TYPE_QUERY, - NewState: STATE_QUERYING, + MsgType: MessageTypeQuery, + NewState: stateQuerying, }, { - MsgType: MESSAGE_TYPE_REACQUIRE, - NewState: STATE_ACQUIRING, + MsgType: MessageTypeReacquire, + NewState: stateAcquiring, }, { - MsgType: MESSAGE_TYPE_REACQUIRE_NO_POINT, - NewState: STATE_ACQUIRING, + MsgType: MessageTypeReacquireNoPoint, + NewState: stateAcquiring, }, { - MsgType: MESSAGE_TYPE_RELEASE, - NewState: STATE_IDLE, + MsgType: MessageTypeRelease, + NewState: stateIdle, }, }, }, - STATE_QUERYING: protocol.StateMapEntry{ + stateQuerying: protocol.StateMapEntry{ Agency: protocol.AgencyServer, Transitions: []protocol.StateTransition{ { - MsgType: MESSAGE_TYPE_RESULT, - NewState: STATE_ACQUIRED, + MsgType: MessageTypeResult, + NewState: stateAcquired, }, }, }, - STATE_DONE: protocol.StateMapEntry{ + stateDone: protocol.StateMapEntry{ Agency: protocol.AgencyNone, }, } +// LocalStateQuery is a wrapper object that holds the client and server instances type LocalStateQuery struct { Client *Client Server *Server } +// Config is used to configure the LocalStateQuery protocol instance type Config struct { AcquireFunc AcquireFunc QueryFunc QueryFunc @@ -108,6 +113,7 @@ type ReleaseFunc func() error type ReAcquireFunc func(interface{}) error type DoneFunc func() error +// New returns a new LocalStateQuery object func New(protoOptions protocol.ProtocolOptions, cfg *Config) *LocalStateQuery { l := &LocalStateQuery{ Client: NewClient(protoOptions, cfg), @@ -116,8 +122,10 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *LocalStateQuery { return l } +// LocalStateQueryOptionFunc represents a function used to modify the LocalStateQuery protocol config type LocalStateQueryOptionFunc func(*Config) +// NewConfig returns a new LocalStateQuery config object with the provided options func NewConfig(options ...LocalStateQueryOptionFunc) Config { c := Config{ AcquireTimeout: 5 * time.Second, @@ -130,42 +138,49 @@ func NewConfig(options ...LocalStateQueryOptionFunc) Config { return c } +// WithAcquireFunc specifies the Acquire callback function when acting as a server func WithAcquireFunc(acquireFunc AcquireFunc) LocalStateQueryOptionFunc { return func(c *Config) { c.AcquireFunc = acquireFunc } } +// WithQueryFunc specifies the Query callback function when acting as a server func WithQueryFunc(queryFunc QueryFunc) LocalStateQueryOptionFunc { return func(c *Config) { c.QueryFunc = queryFunc } } +// WithReleaseFunc specifies the Release callback function when acting as a server func WithReleaseFunc(releaseFunc ReleaseFunc) LocalStateQueryOptionFunc { return func(c *Config) { c.ReleaseFunc = releaseFunc } } +// WithReAcquireFunc specifies the ReAcquire callback function when acting as a server func WithReAcquireFunc(reAcquireFunc ReAcquireFunc) LocalStateQueryOptionFunc { return func(c *Config) { c.ReAcquireFunc = reAcquireFunc } } +// WithDoneFunc specifies the Done callback function when acting as a server func WithDoneFunc(doneFunc DoneFunc) LocalStateQueryOptionFunc { return func(c *Config) { c.DoneFunc = doneFunc } } +// WithAcquireTimeout specifies the timeout for the Acquire operation when acting as a client func WithAcquireTimeout(timeout time.Duration) LocalStateQueryOptionFunc { return func(c *Config) { c.AcquireTimeout = timeout } } +// WithQueryTimeout specifies the timeout for the Query operation when acting as a client func WithQueryTimeout(timeout time.Duration) LocalStateQueryOptionFunc { return func(c *Config) { c.QueryTimeout = timeout diff --git a/protocol/localstatequery/messages.go b/protocol/localstatequery/messages.go index 06715a2a..14b56256 100644 --- a/protocol/localstatequery/messages.go +++ b/protocol/localstatequery/messages.go @@ -2,54 +2,61 @@ package localstatequery import ( "fmt" + "github.com/cloudstruct/go-ouroboros-network/protocol" "github.com/cloudstruct/go-ouroboros-network/protocol/common" "github.com/cloudstruct/go-ouroboros-network/utils" + "github.com/fxamacker/cbor/v2" ) +// Message types +const ( + MessageTypeAcquire = 0 + MessageTypeAcquired = 1 + MessageTypeFailure = 2 + MessageTypeQuery = 3 + MessageTypeResult = 4 + MessageTypeRelease = 5 + MessageTypeReacquire = 6 + MessageTypeDone = 7 + MessageTypeAcquireNoPoint = 8 + MessageTypeReacquireNoPoint = 9 +) + +// Acquire failure reasons const ( - MESSAGE_TYPE_ACQUIRE = 0 - MESSAGE_TYPE_ACQUIRED = 1 - MESSAGE_TYPE_FAILURE = 2 - MESSAGE_TYPE_QUERY = 3 - MESSAGE_TYPE_RESULT = 4 - MESSAGE_TYPE_RELEASE = 5 - MESSAGE_TYPE_REACQUIRE = 6 - MESSAGE_TYPE_DONE = 7 - MESSAGE_TYPE_ACQUIRE_NO_POINT = 8 - MESSAGE_TYPE_REACQUIRE_NO_POINT = 9 - - ACQUIRE_FAILURE_POINT_TOO_OLD = 0 - ACQUIRE_FAILURE_POINT_NOT_ON_CHAIN = 1 + AcquireFailurePointTooOld = 0 + AcquireFailurePointNotOnChain = 1 ) +// NewMsgFromCbor parses a LocalStateQuery message from CBOR func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error) { var ret protocol.Message switch msgType { - case MESSAGE_TYPE_ACQUIRE: + case MessageTypeAcquire: ret = &MsgAcquire{} - case MESSAGE_TYPE_ACQUIRED: + case MessageTypeAcquired: ret = &MsgAcquired{} - case MESSAGE_TYPE_FAILURE: + case MessageTypeFailure: ret = &MsgFailure{} - case MESSAGE_TYPE_QUERY: + case MessageTypeQuery: ret = &MsgQuery{} - case MESSAGE_TYPE_RESULT: + case MessageTypeResult: ret = &MsgResult{} - case MESSAGE_TYPE_RELEASE: + case MessageTypeRelease: ret = &MsgRelease{} - case MESSAGE_TYPE_REACQUIRE: + case MessageTypeReacquire: ret = &MsgReAcquire{} - case MESSAGE_TYPE_ACQUIRE_NO_POINT: + case MessageTypeAcquireNoPoint: ret = &MsgAcquireNoPoint{} - case MESSAGE_TYPE_REACQUIRE_NO_POINT: + case MessageTypeReacquireNoPoint: ret = &MsgReAcquireNoPoint{} - case MESSAGE_TYPE_DONE: + case MessageTypeDone: ret = &MsgDone{} } if _, err := utils.CborDecode(data, ret); err != nil { - return nil, fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err) + return nil, fmt.Errorf("%s: decode error: %s", protocolName, err) } if ret != nil { // Store the raw message CBOR @@ -66,7 +73,7 @@ type MsgAcquire struct { func NewMsgAcquire(point common.Point) *MsgAcquire { m := &MsgAcquire{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_ACQUIRE, + MessageType: MessageTypeAcquire, }, Point: point, } @@ -80,7 +87,7 @@ type MsgAcquireNoPoint struct { func NewMsgAcquireNoPoint() *MsgAcquireNoPoint { m := &MsgAcquireNoPoint{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_ACQUIRE_NO_POINT, + MessageType: MessageTypeAcquireNoPoint, }, } return m @@ -93,7 +100,7 @@ type MsgAcquired struct { func NewMsgAcquired() *MsgAcquired { m := &MsgAcquired{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_ACQUIRED, + MessageType: MessageTypeAcquired, }, } return m @@ -107,7 +114,7 @@ type MsgFailure struct { func NewMsgFailure(failure uint8) *MsgFailure { m := &MsgFailure{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_FAILURE, + MessageType: MessageTypeFailure, }, Failure: failure, } @@ -122,7 +129,7 @@ type MsgQuery struct { func NewMsgQuery(query interface{}) *MsgQuery { m := &MsgQuery{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_QUERY, + MessageType: MessageTypeQuery, }, Query: query, } @@ -137,7 +144,7 @@ type MsgResult struct { func NewMsgResult(resultCbor []byte) *MsgResult { m := &MsgResult{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_RESULT, + MessageType: MessageTypeResult, }, Result: cbor.RawMessage(resultCbor), } @@ -151,7 +158,7 @@ type MsgRelease struct { func NewMsgRelease() *MsgRelease { m := &MsgRelease{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_RELEASE, + MessageType: MessageTypeRelease, }, } return m @@ -165,7 +172,7 @@ type MsgReAcquire struct { func NewMsgReAcquire(point common.Point) *MsgReAcquire { m := &MsgReAcquire{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_REACQUIRE, + MessageType: MessageTypeReacquire, }, Point: point, } @@ -179,7 +186,7 @@ type MsgReAcquireNoPoint struct { func NewMsgReAcquireNoPoint() *MsgReAcquireNoPoint { m := &MsgReAcquireNoPoint{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_REACQUIRE_NO_POINT, + MessageType: MessageTypeReacquireNoPoint, }, } return m @@ -192,7 +199,7 @@ type MsgDone struct { func NewMsgDone() *MsgDone { m := &MsgDone{ MessageBase: protocol.MessageBase{ - MessageType: MESSAGE_TYPE_DONE, + MessageType: MessageTypeDone, }, } return m diff --git a/protocol/localstatequery/messages_test.go b/protocol/localstatequery/messages_test.go index 4ebc13b8..e29a9cbb 100644 --- a/protocol/localstatequery/messages_test.go +++ b/protocol/localstatequery/messages_test.go @@ -19,17 +19,17 @@ var tests = []testDefinition{ { CborHex: "820080", Message: NewMsgAcquire(common.Point{}), - MessageType: MESSAGE_TYPE_ACQUIRE, + MessageType: MessageTypeAcquire, }, { CborHex: "8101", Message: NewMsgAcquired(), - MessageType: MESSAGE_TYPE_ACQUIRED, + MessageType: MessageTypeAcquired, }, { CborHex: "820201", - Message: NewMsgFailure(ACQUIRE_FAILURE_POINT_NOT_ON_CHAIN), - MessageType: MESSAGE_TYPE_FAILURE, + Message: NewMsgFailure(AcquireFailurePointNotOnChain), + MessageType: MessageTypeFailure, }, { CborHex: "8203820082028101", @@ -45,37 +45,37 @@ var tests = []testDefinition{ }, }, ), - MessageType: MESSAGE_TYPE_QUERY, + MessageType: MessageTypeQuery, }, { CborHex: "820405", Message: NewMsgResult([]byte{5}), - MessageType: MESSAGE_TYPE_RESULT, + MessageType: MessageTypeResult, }, { CborHex: "8105", Message: NewMsgRelease(), - MessageType: MESSAGE_TYPE_RELEASE, + MessageType: MessageTypeRelease, }, { CborHex: "820680", Message: NewMsgReAcquire(common.Point{}), - MessageType: MESSAGE_TYPE_REACQUIRE, + MessageType: MessageTypeReacquire, }, { CborHex: "8107", Message: NewMsgDone(), - MessageType: MESSAGE_TYPE_DONE, + MessageType: MessageTypeDone, }, { CborHex: "8108", Message: NewMsgAcquireNoPoint(), - MessageType: MESSAGE_TYPE_ACQUIRE_NO_POINT, + MessageType: MessageTypeAcquireNoPoint, }, { CborHex: "8109", Message: NewMsgReAcquireNoPoint(), - MessageType: MESSAGE_TYPE_REACQUIRE_NO_POINT, + MessageType: MessageTypeReacquireNoPoint, }, } diff --git a/protocol/localstatequery/queries.go b/protocol/localstatequery/queries.go index 8b88ca90..ab2af3ef 100644 --- a/protocol/localstatequery/queries.go +++ b/protocol/localstatequery/queries.go @@ -4,43 +4,44 @@ import ( "github.com/fxamacker/cbor/v2" ) +// Query types const ( - QUERY_TYPE_BLOCK = 0 - QUERY_TYPE_SYSTEM_START = 1 - QUERY_TYPE_CHAIN_BLOCK_NO = 2 - QUERY_TYPE_CHAIN_POINT = 3 + QueryTypeBlock = 0 + QueryTypeSystemStart = 1 + QueryTypeChainBlockNo = 2 + QueryTypeChainPoint = 3 // Block query sub-types - QUERY_TYPE_SHELLEY = 0 - QUERY_TYPE_HARD_FORK = 2 + QueryTypeShelley = 0 + QueryTypeHardFork = 2 // Hard fork query sub-types - QUERY_TYPE_HARD_FORK_ERA_HISTORY = 0 - QUERY_TYPE_HARD_FORK_CURRENT_ERA = 1 + QueryTypeHardForkEraHistory = 0 + QueryTypeHardForkCurrentEra = 1 // Shelley query sub-types - QUERY_TYPE_SHELLEY_LEDGER_TIP = 0 - QUERY_TYPE_SHELLEY_EPOCH_NO = 1 - QUERY_TYPE_SHELLEY_NON_MYOPIC_MEMBER_REWARDS = 2 - QUERY_TYPE_SHELLEY_CURRENT_PROTOCOL_PARAMS = 3 - QUERY_TYPE_SHELLEY_PROPOSED_PROTOCOL_PARAMS_UPDATES = 4 - QUERY_TYPE_SHELLEY_STAKE_DISTRIBUTION = 5 - QUERY_TYPE_SHELLEY_UTXO_BY_ADDRESS = 6 - QUERY_TYPE_SHELLEY_UTXO_WHOLE = 7 - QUERY_TYPE_SHELLEY_DEBUG_EPOCH_STATE = 8 - QUERY_TYPE_SHELLEY_CBOR = 9 - QUERY_TYPE_SHELLEY_FILTERED_DELEGATION_AND_REWARD_ACCOUNTS = 10 - QUERY_TYPE_SHELLEY_GENESIS_CONFIG = 11 - QUERY_TYPE_SHELLEY_DEBUG_NEW_EPOCH_STATE = 12 - QUERY_TYPE_SHELLEY_DEBUG_CHAIN_DEP_STATE = 13 - QUERY_TYPE_SHELLEY_REWARD_PROVENANCE = 14 - QUERY_TYPE_SHELLEY_UTXO_BY_TXIN = 15 - QUERY_TYPE_SHELLEY_STAKE_POOLS = 16 - QUERY_TYPE_SHELLEY_STAKE_POOL_PARAMS = 17 - QUERY_TYPE_SHELLEY_REWARD_INFO_POOLS = 18 - QUERY_TYPE_SHELLEY_POOL_STATE = 19 - QUERY_TYPE_SHELLEY_STAKE_SNAPSHOTS = 20 - QUERY_TYPE_SHELLEY_POOL_DISTR = 21 + QueryTypeShelleyLedgerTip = 0 + QueryTypeShelleyEpochNo = 1 + QueryTypeShelleyNonMyopicMemberRewards = 2 + QueryTypeShelleyCurrentProtocolParams = 3 + QueryTypeShelleyProposedProtocolParamsUpdates = 4 + QueryTypeShelleyStakeDistribution = 5 + QueryTypeShelleyUtxoByAddress = 6 + QueryTypeShelleyUtxoWhole = 7 + QueryTypeShelleyDebugEpochState = 8 + QueryTypeShelleyCbor = 9 + QueryTypeShelleyFilteredDelegationAndRewardAccounts = 10 + QueryTypeShelleyGenesisConfig = 11 + QueryTypeShelleyDebugNewEpochState = 12 + QueryTypeShelleyDebugChainDepState = 13 + QueryTypeShelleyRewardProvenance = 14 + QueryTypeShelleyUtxoByTxin = 15 + QueryTypeShelleyStakePools = 16 + QueryTypeShelleyStakePoolParams = 17 + QueryTypeShelleyRewardInfoPools = 18 + QueryTypeShelleyPoolState = 19 + QueryTypeShelleyStakeSnapshots = 20 + QueryTypeShelleyPoolDistr = 21 ) func buildQuery(queryType int, params ...interface{}) []interface{} { @@ -53,9 +54,9 @@ func buildQuery(queryType int, params ...interface{}) []interface{} { func buildHardForkQuery(queryType int, params ...interface{}) []interface{} { ret := buildQuery( - QUERY_TYPE_BLOCK, + QueryTypeBlock, buildQuery( - QUERY_TYPE_HARD_FORK, + QueryTypeHardFork, buildQuery( queryType, params..., @@ -67,9 +68,9 @@ func buildHardForkQuery(queryType int, params ...interface{}) []interface{} { func buildShelleyQuery(era int, queryType int, params ...interface{}) []interface{} { ret := buildQuery( - QUERY_TYPE_BLOCK, + QueryTypeBlock, buildQuery( - QUERY_TYPE_SHELLEY, + QueryTypeShelley, buildQuery( era, buildQuery( diff --git a/protocol/localstatequery/server.go b/protocol/localstatequery/server.go index e418e18b..c78e2b29 100644 --- a/protocol/localstatequery/server.go +++ b/protocol/localstatequery/server.go @@ -2,9 +2,11 @@ package localstatequery import ( "fmt" + "github.com/cloudstruct/go-ouroboros-network/protocol" ) +// Server implements the LocalStateQuery server type Server struct { *protocol.Protocol config *Config @@ -13,13 +15,14 @@ type Server struct { enableGetRewardInfoPoolsBlock bool } +// NewServer returns a new LocalStateQuery server object func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server { s := &Server{ config: cfg, } protoConfig := protocol.ProtocolConfig{ - Name: PROTOCOL_NAME, - ProtocolId: PROTOCOL_ID, + Name: protocolName, + ProtocolId: protocolId, Muxer: protoOptions.Muxer, ErrorChan: protoOptions.ErrorChan, Mode: protoOptions.Mode, @@ -27,7 +30,7 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server { MessageHandlerFunc: s.messageHandler, MessageFromCborFunc: NewMsgFromCbor, StateMap: StateMap, - InitialState: STATE_IDLE, + InitialState: stateIdle, } // Enable version-dependent features if protoOptions.Version >= 10 { @@ -44,22 +47,22 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server { func (s *Server) messageHandler(msg protocol.Message, isResponse bool) error { var err error switch msg.Type() { - case MESSAGE_TYPE_ACQUIRE: + case MessageTypeAcquire: err = s.handleAcquire(msg) - case MESSAGE_TYPE_QUERY: + case MessageTypeQuery: err = s.handleQuery(msg) - case MESSAGE_TYPE_RELEASE: + case MessageTypeRelease: err = s.handleRelease() - case MESSAGE_TYPE_REACQUIRE: + case MessageTypeReacquire: err = s.handleReAcquire(msg) - case MESSAGE_TYPE_ACQUIRE_NO_POINT: + case MessageTypeAcquireNoPoint: err = s.handleAcquire(msg) - case MESSAGE_TYPE_REACQUIRE_NO_POINT: + case MessageTypeReacquireNoPoint: err = s.handleReAcquire(msg) - case MESSAGE_TYPE_DONE: + case MessageTypeDone: err = s.handleDone() default: - err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type()) + err = fmt.Errorf("%s: received unexpected message type %d", protocolName, msg.Type()) } return err }