Skip to content

Commit

Permalink
docs: type/function docs for TxSubmission protocol (#546)
Browse files Browse the repository at this point in the history
Fixes #162
  • Loading branch information
agaffney authored Mar 17, 2024
1 parent 620d0c6 commit 4f41b55
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion protocol/peersharing/peersharing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package handshake implements the Ouroboros handshake protocol
// Package peersharing implements the Ouroboros PeerSharing protocol
package peersharing

import (
Expand Down
2 changes: 2 additions & 0 deletions protocol/txsubmission/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
"github.com/blinklabs-io/gouroboros/protocol"
)

// Client implements the TxSubmission client
type Client struct {
*protocol.Protocol
config *Config
onceInit sync.Once
}

// NewClient returns a new TxSubmission client object
func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
if cfg == nil {
tmpCfg := NewConfig()
Expand Down
2 changes: 2 additions & 0 deletions protocol/txsubmission/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/blinklabs-io/gouroboros/protocol"
)

// Message types
const (
MessageTypeRequestTxIds = 0
MessageTypeReplyTxIds = 1
Expand All @@ -30,6 +31,7 @@ const (
MessageTypeInit = 6
)

// NewMsgFromCbor parses a TxSubmission message from CBOR
func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error) {
var ret protocol.Message
switch msgType {
Expand Down
4 changes: 4 additions & 0 deletions protocol/txsubmission/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/blinklabs-io/gouroboros/protocol"
)

// Server implements the TxSubmission server
type Server struct {
*protocol.Protocol
config *Config
Expand All @@ -31,6 +32,7 @@ type Server struct {
onceStart sync.Once
}

// NewServer returns a new TxSubmission server object
func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server {
s := &Server{
config: cfg,
Expand Down Expand Up @@ -86,6 +88,7 @@ func (s *Server) messageHandler(msg protocol.Message) error {
return err
}

// RequestTxIds requests the next set of TX identifiers from the remote node's mempool
func (s *Server) RequestTxIds(
blocking bool,
reqCount int,
Expand All @@ -107,6 +110,7 @@ func (s *Server) RequestTxIds(
return txIds, nil
}

// RequestTxs requests the content of the requested TX identifiers from the remote node's mempool
func (s *Server) RequestTxs(txIds []TxId) ([]TxBody, error) {
if s.stateDone {
return nil, protocol.ProtocolShuttingDownError
Expand Down
12 changes: 12 additions & 0 deletions protocol/txsubmission/txsubmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package txsubmission implements the Ouroboros TxSubmission protocol
package txsubmission

import (
Expand All @@ -20,6 +21,7 @@ import (
"github.com/blinklabs-io/gouroboros/protocol"
)

// Protocol identifiers
const (
ProtocolName = "tx-submission"
ProtocolId uint16 = 4
Expand All @@ -34,6 +36,7 @@ var (
stateDone = protocol.NewState(6, "Done")
)

// TxSubmission protocol state machine
var StateMap = protocol.StateMap{
stateInit: protocol.StateMapEntry{
Agency: protocol.AgencyClient,
Expand Down Expand Up @@ -107,11 +110,13 @@ var StateMap = protocol.StateMap{
},
}

// TxSubmission is a wrapper object that holds the client and server instances
type TxSubmission struct {
Client *Client
Server *Server
}

// Config is used to configure the TxSubmission protocol instance
type Config struct {
RequestTxIdsFunc RequestTxIdsFunc
RequestTxsFunc RequestTxsFunc
Expand All @@ -124,6 +129,7 @@ type RequestTxIdsFunc func(bool, uint16, uint16) ([]TxIdAndSize, error)
type RequestTxsFunc func([]TxId) ([]TxBody, error)
type InitFunc func() error

// New returns a new TxSubmission object
func New(protoOptions protocol.ProtocolOptions, cfg *Config) *TxSubmission {
t := &TxSubmission{
Client: NewClient(protoOptions, cfg),
Expand All @@ -132,8 +138,10 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *TxSubmission {
return t
}

// TxSubmissionOptionFunc represents a function used to modify the TxSubmission protocol config
type TxSubmissionOptionFunc func(*Config)

// NewConfig returns a new TxSubmission config object with the provided options
func NewConfig(options ...TxSubmissionOptionFunc) Config {
c := Config{
IdleTimeout: 300 * time.Second,
Expand All @@ -145,6 +153,7 @@ func NewConfig(options ...TxSubmissionOptionFunc) Config {
return c
}

// WithRequestTxIdsFunc specifies the RequestTxIds callback function
func WithRequestTxIdsFunc(
requestTxIdsFunc RequestTxIdsFunc,
) TxSubmissionOptionFunc {
Expand All @@ -153,18 +162,21 @@ func WithRequestTxIdsFunc(
}
}

// WithRequestTxsFunc specifies the RequestTxs callback function
func WithRequestTxsFunc(requestTxsFunc RequestTxsFunc) TxSubmissionOptionFunc {
return func(c *Config) {
c.RequestTxsFunc = requestTxsFunc
}
}

// WithInitFunc specifies the Init callback function
func WithInitFunc(initFunc InitFunc) TxSubmissionOptionFunc {
return func(c *Config) {
c.InitFunc = initFunc
}
}

// WithIdleTimeout specifies the timeout for waiting for new transactions from the remote node's mempool
func WithIdleTimeout(timeout time.Duration) TxSubmissionOptionFunc {
return func(c *Config) {
c.IdleTimeout = timeout
Expand Down

0 comments on commit 4f41b55

Please sign in to comment.