Skip to content

Commit

Permalink
Add post unit size to network info & add http endpoint to sync specif…
Browse files Browse the repository at this point in the history
…ic atx
  • Loading branch information
kacpersaw committed Apr 22, 2024
1 parent b38e51b commit 7e6639f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
27 changes: 23 additions & 4 deletions collector/http.go
Expand Up @@ -14,7 +14,26 @@ import (
func (c *Collector) StartHttpServer(apiHost string, apiPort int) {
e := echo.New()

e.GET("/sync/atx/ts/:ts", func(ctx echo.Context) error {
e.GET("/sync/atx/:id", func(ctx echo.Context) error {
id := ctx.Param("id")

log.Info("http syncing atx %s", id)
go func() {
atx, err := c.dbClient.GetAtxById(c.db, id)
if err != nil {
log.Warning("syncing atx %s failed with error %d", id, err)
return
}
if atx != nil {
c.listener.OnActivation(atx)
c.listener.RecalculateEpochStats()
}
}()

return ctx.NoContent(http.StatusOK)
})

e.GET("/sync/atxs/ts/:ts", func(ctx echo.Context) error {
ts := ctx.Param("ts")
timestamp, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
Expand All @@ -37,7 +56,7 @@ func (c *Collector) StartHttpServer(apiHost string, apiPort int) {
return ctx.NoContent(http.StatusOK)
})

e.GET("/sync/bulk/atx/ts/:ts", func(ctx echo.Context) error {
e.GET("/sync/bulk/atxs/ts/:ts", func(ctx echo.Context) error {
ts := ctx.Param("ts")
timestamp, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
Expand All @@ -62,7 +81,7 @@ func (c *Collector) StartHttpServer(apiHost string, apiPort int) {
return ctx.NoContent(http.StatusOK)
})

e.GET("/sync/atx/:epoch", func(ctx echo.Context) error {
e.GET("/sync/atxs/:epoch", func(ctx echo.Context) error {
epoch := ctx.Param("epoch")
epochId, err := strconv.ParseInt(epoch, 10, 64)
if err != nil {
Expand All @@ -85,7 +104,7 @@ func (c *Collector) StartHttpServer(apiHost string, apiPort int) {
return ctx.NoContent(http.StatusOK)
})

e.GET("/sync/bulk/atx/:epoch", func(ctx echo.Context) error {
e.GET("/sync/bulk/atxs/:epoch", func(ctx echo.Context) error {
epoch := ctx.Param("epoch")
epochId, err := strconv.ParseInt(epoch, 10, 64)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions collector/sql/atxs.go
Expand Up @@ -2,9 +2,11 @@ package sql

import (
"fmt"
"github.com/spacemeshos/explorer-backend/utils"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"time"
)

Expand Down Expand Up @@ -130,3 +132,20 @@ func (c *Client) GetAtxsByEpochPaginated(db *sql.Database, epoch, limit, offset
}
return derr
}

func (c *Client) GetAtxById(db *sql.Database, id string) (*types.VerifiedActivationTx, error) {
idBytes, err := utils.StringToBytes(id)
if err != nil {
return nil, err
}

var atxId types.ATXID
copy(atxId[:], idBytes)

atx, err := atxs.Get(db, atxId)
if err != nil {
return nil, err
}

return atx, nil
}
1 change: 1 addition & 0 deletions collector/sql/sql.go
Expand Up @@ -16,6 +16,7 @@ type DatabaseClient interface {
GetAtxsByEpoch(db *sql.Database, epoch int64, fn func(tx *types.VerifiedActivationTx) bool) error
CountAtxsByEpoch(db *sql.Database, epoch int64) (int, error)
GetAtxsByEpochPaginated(db *sql.Database, epoch, limit, offset int64, fn func(tx *types.VerifiedActivationTx) bool) error
GetAtxById(db *sql.Database, id string) (*types.VerifiedActivationTx, error)
}

type Client struct{}
Expand Down
1 change: 1 addition & 0 deletions model/network_info.go
Expand Up @@ -6,6 +6,7 @@ type NetworkInfo struct {
EpochNumLayers uint32 `json:"layers" bson:"layers"`
MaxTransactionsPerSecond uint32 `json:"maxtx" bson:"maxtx"`
LayerDuration uint32 `json:"duration" bson:"duration"`
PostUnitSize uint64 `json:"postUnitSize" bson:"postUnitSize"`

LastLayer uint32 `json:"lastlayer" bson:"lastlayer"`
LastLayerTimestamp uint32 `json:"lastlayerts" bson:"lastlayerts"`
Expand Down
1 change: 1 addition & 0 deletions storage/network_info.go
Expand Up @@ -57,6 +57,7 @@ func (s *Storage) SaveOrUpdateNetworkInfo(parent context.Context, in *model.Netw
{Key: "layers", Value: in.EpochNumLayers},
{Key: "maxtx", Value: in.MaxTransactionsPerSecond},
{Key: "duration", Value: in.LayerDuration},
{Key: "postUnitSize", Value: in.PostUnitSize},
{Key: "lastlayer", Value: in.LastLayer},
{Key: "lastlayerts", Value: in.LastLayerTimestamp},
{Key: "lastapprovedlayer", Value: in.LastApprovedLayer},
Expand Down
1 change: 1 addition & 0 deletions storage/storage.go
Expand Up @@ -155,6 +155,7 @@ func (s *Storage) OnNetworkInfo(genesisId string, genesisTime uint64, epochNumLa
s.NetworkInfo.EpochNumLayers = epochNumLayers
s.NetworkInfo.MaxTransactionsPerSecond = uint32(maxTransactionsPerSecond)
s.NetworkInfo.LayerDuration = uint32(layerDuration)
s.NetworkInfo.PostUnitSize = postUnitSize
s.postUnitSize = postUnitSize

err := s.SaveOrUpdateNetworkInfo(context.Background(), &s.NetworkInfo)
Expand Down
4 changes: 4 additions & 0 deletions test/testseed/db.go
Expand Up @@ -214,6 +214,10 @@ func (c *Client) GetAtxsByEpochPaginated(db *sql.Database, epoch, limit, offset
return nil
}

func (c *Client) GetAtxById(db *sql.Database, id string) (*types.VerifiedActivationTx, error) {
return nil, nil
}

func mustParse(str string) []byte {
res, err := utils.StringToBytes(str)
if err != nil {
Expand Down

0 comments on commit 7e6639f

Please sign in to comment.