diff --git a/collector/http.go b/collector/http.go index 8ee2d18..84013c1 100644 --- a/collector/http.go +++ b/collector/http.go @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/collector/sql/atxs.go b/collector/sql/atxs.go index 4620827..9ce07d9 100644 --- a/collector/sql/atxs.go +++ b/collector/sql/atxs.go @@ -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" ) @@ -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 +} diff --git a/collector/sql/sql.go b/collector/sql/sql.go index 63adab5..56a6e1e 100644 --- a/collector/sql/sql.go +++ b/collector/sql/sql.go @@ -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{} diff --git a/model/network_info.go b/model/network_info.go index 82f1621..c5adaee 100644 --- a/model/network_info.go +++ b/model/network_info.go @@ -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"` diff --git a/storage/network_info.go b/storage/network_info.go index 036fdc9..bad8ac7 100644 --- a/storage/network_info.go +++ b/storage/network_info.go @@ -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}, diff --git a/storage/storage.go b/storage/storage.go index 42f918a..3a644f2 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -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) diff --git a/test/testseed/db.go b/test/testseed/db.go index 9d439d8..f741764 100644 --- a/test/testseed/db.go +++ b/test/testseed/db.go @@ -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 {