diff --git a/model/atx.go b/model/atx.go index 291cb17..d99e2a6 100644 --- a/model/atx.go +++ b/model/atx.go @@ -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, } } diff --git a/model/smesher.go b/model/smesher.go index 37ab729..0ef66d5 100644 --- a/model/smesher.go +++ b/model/smesher.go @@ -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"` diff --git a/storage/smesher.go b/storage/smesher.go index 51e0eb7..7675ca6 100644 --- a/storage/smesher.go +++ b/storage/smesher.go @@ -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")), @@ -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}, @@ -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}, @@ -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) } diff --git a/storage/storage.go b/storage/storage.go index 777be27..35135f2 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -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)) @@ -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)) }