Skip to content

Commit

Permalink
Fix smeshers update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpersaw committed Mar 26, 2024
1 parent b16a134 commit 041dd38
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 34 deletions.
2 changes: 2 additions & 0 deletions model/atx.go
Expand Up @@ -46,6 +46,8 @@ func NewActivation(atx *types.VerifiedActivationTx) *Activation {
func (atx *Activation) GetSmesher(unitSize uint64) *Smesher {
return &Smesher{
Id: atx.SmesherId,
Coinbase: atx.Coinbase,
Timestamp: uint64(atx.Received),
CommitmentSize: uint64(atx.NumUnits) * unitSize,
}
}
3 changes: 0 additions & 3 deletions model/smesher.go
Expand Up @@ -15,9 +15,6 @@ type Smesher struct {
Coinbase string `json:"coinbase" bson:"coinbase"`
AtxCount uint32 `json:"atxcount" bson:"atxcount"`
Timestamp uint64 `json:"timestamp" bson:"timestamp"`
Name string `json:"name" bson:"name"`
Lat float64 `json:"lat" bson:"lat"`
Lon float64 `json:"lon" bson:"lon"`
Rewards int64 `json:"rewards" bson:"-"`
AtxLayer uint32 `json:"atxLayer" bson:"atxLayer"`
Proofs []MalfeasanceProof `json:"proofs,omitempty" bson:"proofs,omitempty"`
Expand Down
39 changes: 16 additions & 23 deletions storage/smesher.go
Expand Up @@ -42,9 +42,6 @@ func (s *Storage) GetSmesher(parent context.Context, query *bson.D) (*model.Smes
doc := cursor.Current
smesher := &model.Smesher{
Id: utils.GetAsString(doc.Lookup("id")),
Name: utils.GetAsString(doc.Lookup("name")),
Lon: doc.Lookup("lon").Double(),
Lat: doc.Lookup("lat").Double(),
CommitmentSize: utils.GetAsUInt64(doc.Lookup("cSize")),
Coinbase: utils.GetAsString(doc.Lookup("coinbase")),
AtxCount: utils.GetAsUInt32(doc.Lookup("atxcount")),
Expand Down Expand Up @@ -102,9 +99,6 @@ func (s *Storage) SaveSmesher(parent context.Context, in *model.Smesher, epoch u
_, err := s.db.Collection("smeshers").UpdateOne(ctx, bson.D{{Key: "id", Value: in.Id}}, bson.D{
{Key: "$set", Value: bson.D{
{Key: "id", Value: in.Id},
{Key: "name", Value: in.Name},
{Key: "lon", Value: in.Lon},
{Key: "lat", Value: in.Lat},
{Key: "cSize", Value: in.CommitmentSize},
{Key: "coinbase", Value: in.Coinbase},
{Key: "atxcount", Value: in.AtxCount},
Expand All @@ -123,9 +117,6 @@ func (s *Storage) SaveSmesherQuery(in *model.Smesher) *mongo.UpdateOneModel {
update := bson.D{
{Key: "$set", Value: bson.D{
{Key: "id", Value: in.Id},
{Key: "name", Value: in.Name},
{Key: "lon", Value: in.Lon},
{Key: "lat", Value: in.Lat},
{Key: "cSize", Value: in.CommitmentSize},
{Key: "coinbase", Value: in.Coinbase},
{Key: "atxcount", Value: in.AtxCount},
Expand All @@ -141,33 +132,35 @@ func (s *Storage) SaveSmesherQuery(in *model.Smesher) *mongo.UpdateOneModel {
return updateModel
}

func (s *Storage) UpdateSmesher(parent context.Context, smesher string, coinbase string, space uint64, timestamp int64, epoch uint32) error {
func (s *Storage) UpdateSmesher(parent context.Context, in *model.Smesher, epoch uint32) error {
ctx, cancel := context.WithTimeout(parent, 5*time.Second)
defer cancel()

opts := options.Update().SetUpsert(true)
filter := bson.D{{Key: "smesherId", Value: smesher}}
_, err := s.db.Collection("coinbases").UpdateOne(ctx, filter, bson.D{
{Key: "$set", Value: bson.D{
{Key: "coinbase", Value: coinbase},
}}}, opts)

filter := bson.D{{Key: "smesherId", Value: in.Id}}
_, err := s.db.Collection("coinbases").UpdateOne(
ctx,
filter,
bson.D{{Key: "$set", Value: bson.D{{Key: "coinbase", Value: in.Coinbase}}}},
options.Update().SetUpsert(true))
if err != nil {
return fmt.Errorf("error insert smesher into `coinbases`: %w", err)
}
atxCount, err := s.db.Collection("activations").CountDocuments(ctx, &bson.D{{Key: "smesher", Value: smesher}})

atxCount, err := s.db.Collection("activations").CountDocuments(ctx, &bson.D{{Key: "smesher", Value: in.Id}})
if err != nil {
log.Info("UpdateSmesher: GetActivationsCount: %v", err)
}
_, err = s.db.Collection("smeshers").UpdateOne(ctx, bson.D{{Key: "id", Value: smesher}}, bson.D{

_, err = s.db.Collection("smeshers").UpdateOne(ctx, bson.D{{Key: "id", Value: in.Id}}, bson.D{
{Key: "$set", Value: bson.D{
{Key: "cSize", Value: space},
{Key: "coinbase", Value: coinbase},
{Key: "id", Value: in.Id},
{Key: "cSize", Value: in.CommitmentSize},
{Key: "coinbase", Value: in.Coinbase},
{Key: "timestamp", Value: in.Timestamp},
{Key: "atxcount", Value: atxCount},
{Key: "timestamp", Value: timestamp},
}},
{Key: "$addToSet", Value: bson.M{"epochs": epoch}},
})
}, options.Update().SetUpsert(true))
if err != nil {
log.Info("UpdateSmesher: %v", err)
}
Expand Down
10 changes: 2 additions & 8 deletions storage/storage.go
Expand Up @@ -74,7 +74,7 @@ type Storage struct {
}

func New(parent context.Context, dbUrl string, dbName string) (*Storage, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(dbUrl))

Expand Down Expand Up @@ -432,13 +432,7 @@ func (s *Storage) OnActivation(atx *types.VerifiedActivationTx) {
log.Err(fmt.Errorf("OnActivation: error %v", err))
}

err = s.SaveSmesher(context.Background(), activation.GetSmesher(s.postUnitSize), activation.TargetEpoch)
if err != nil {
log.Err(fmt.Errorf("OnActivation: save smesher error %v", err))
}

err = s.UpdateSmesher(context.Background(), activation.SmesherId, activation.Coinbase,
uint64(atx.NumUnits)*s.postUnitSize, activation.Received, activation.TargetEpoch)
err = s.UpdateSmesher(context.Background(), activation.GetSmesher(s.postUnitSize), activation.TargetEpoch)
if err != nil {
log.Err(fmt.Errorf("OnActivation: update smesher error %v", err))
}
Expand Down

0 comments on commit 041dd38

Please sign in to comment.