-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
974f088
commit 5390d58
Showing
23 changed files
with
961 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package conns | ||
|
||
import ( | ||
"github.com/olahol/melody" | ||
) | ||
|
||
// ConnectionManager defines a connection manager interface | ||
// for active WS connections | ||
type ConnectionManager interface { | ||
// AddWSConnection registers a new WS connection | ||
AddWSConnection(id string, session *melody.Session) | ||
|
||
// RemoveWSConnection Removes the WS connection with the supplied ID | ||
RemoveWSConnection(id string) | ||
|
||
// GetWSConnection fetches a WS connection, if any, using the supplied ID | ||
GetWSConnection(id string) WSConnection | ||
} | ||
|
||
// WSConnection represents a single WS connection | ||
type WSConnection interface { | ||
// WriteData pushes out data to the WS connection. | ||
// Returns an error if the write failed (ex. connection closed) | ||
WriteData(data any) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package wsconn | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/gnolang/tx-indexer/serve/conns" | ||
"github.com/gnolang/tx-indexer/serve/writer" | ||
"github.com/gnolang/tx-indexer/serve/writer/ws" | ||
"github.com/olahol/melody" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Conns manages active WS connections | ||
type Conns struct { | ||
logger *zap.Logger | ||
conns map[string]Conn // ws connection ID -> conn | ||
|
||
mux sync.RWMutex | ||
} | ||
|
||
// NewConns creates a new instance of the WS connection manager | ||
func NewConns(logger *zap.Logger) *Conns { | ||
return &Conns{ | ||
logger: logger, | ||
conns: make(map[string]Conn), | ||
} | ||
} | ||
|
||
// AddWSConnection registers a new WS connection | ||
func (pw *Conns) AddWSConnection(id string, session *melody.Session) { | ||
pw.mux.Lock() | ||
defer pw.mux.Unlock() | ||
|
||
ctx, cancelFn := context.WithCancel(context.Background()) | ||
|
||
pw.conns[id] = Conn{ | ||
ctx: ctx, | ||
cancelFn: cancelFn, | ||
writer: ws.New( | ||
pw.logger.Named( | ||
fmt.Sprintf("ws-%s", id), | ||
), | ||
session, | ||
), | ||
} | ||
} | ||
|
||
// RemoveWSConnection removes an existing WS connection | ||
func (pw *Conns) RemoveWSConnection(id string) { | ||
pw.mux.Lock() | ||
defer pw.mux.Unlock() | ||
|
||
conn, found := pw.conns[id] | ||
if !found { | ||
return | ||
} | ||
|
||
// Cancel the connection context | ||
conn.cancelFn() | ||
|
||
delete(pw.conns, id) | ||
} | ||
|
||
// GetWSConnection fetches a WS connection, if any | ||
func (pw *Conns) GetWSConnection(id string) conns.WSConnection { | ||
pw.mux.RLock() | ||
defer pw.mux.RUnlock() | ||
|
||
conn, found := pw.conns[id] | ||
if !found { | ||
return nil | ||
} | ||
|
||
return &conn | ||
} | ||
|
||
// Conn is a single WS connection | ||
type Conn struct { | ||
ctx context.Context | ||
cancelFn context.CancelFunc | ||
|
||
writer writer.ResponseWriter | ||
} | ||
|
||
// WriteData writes arbitrary data to the WS connection | ||
func (c *Conn) WriteData(data any) error { | ||
if c.ctx.Err() != nil { | ||
return c.ctx.Err() | ||
} | ||
|
||
c.writer.WriteResponse(data) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package serve | ||
|
||
import ( | ||
"github.com/gnolang/tx-indexer/serve/metadata" | ||
"github.com/gnolang/tx-indexer/serve/spec" | ||
) | ||
|
||
// Handler executes a method with accompanying | ||
// data such as metadata and params | ||
type Handler func(metadata *metadata.Metadata, params []any) (any, *spec.BaseJSONError) | ||
|
||
type handlers map[string]Handler | ||
|
||
// newHandlers creates a new map of method handlers | ||
func newHandlers() handlers { | ||
return make(handlers) | ||
} | ||
|
||
// addHandler adds a new method handler for the specified method name | ||
func (h handlers) addHandler(method string, handler Handler) { | ||
h[method] = handler | ||
} | ||
|
||
// removeHandler removes the method handler for the specified method, if any | ||
func (h handlers) removeHandler(method string) { | ||
delete(h, method) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package block | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/gnolang/gno/tm2/pkg/bft/types" | ||
"github.com/gnolang/tx-indexer/serve/metadata" | ||
"github.com/gnolang/tx-indexer/serve/spec" | ||
) | ||
|
||
type Handler struct { | ||
storage Storage | ||
} | ||
|
||
func NewHandler(storage Storage) *Handler { | ||
return &Handler{ | ||
storage: storage, | ||
} | ||
} | ||
|
||
func (h *Handler) GetBlockHandler( | ||
_ *metadata.Metadata, | ||
params []any, | ||
) (any, *spec.BaseJSONError) { | ||
// Check the params | ||
if len(params) < 1 { | ||
return nil, spec.GenerateInvalidParamCountError() | ||
} | ||
|
||
// Extract the params | ||
requestedBlock, ok := params[0].(string) | ||
if !ok { | ||
return nil, spec.GenerateInvalidParamError(1) | ||
} | ||
|
||
blockNum, err := strconv.ParseInt(requestedBlock, 10, 64) | ||
if err != nil { | ||
return nil, spec.GenerateInvalidParamError(1) | ||
} | ||
|
||
// Run the handler | ||
response, err := h.getBlock(blockNum) | ||
if err != nil { | ||
return nil, spec.GenerateResponseError(err) | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
// getBlock fetches the block from storage, if any | ||
func (h *Handler) getBlock(blockNum int64) (*types.Block, error) { | ||
block, err := h.storage.GetBlock(blockNum) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return block, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package block | ||
|
||
import ( | ||
"github.com/gnolang/gno/tm2/pkg/bft/types" | ||
) | ||
|
||
type Storage interface { | ||
// GetBlock returns specified block from permanent storage | ||
GetBlock(int64) (*types.Block, error) | ||
} |
Oops, something went wrong.