-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unbonding state transitions (#27)
- Loading branch information
Showing
15 changed files
with
336 additions
and
40 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
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,15 @@ | ||
package model | ||
|
||
type TimeLockDocument struct { | ||
StakingTxHashHex string `bson:"_id"` // Primary key | ||
ExpireHeight uint32 `bson:"expire_height"` | ||
TxType string `bson:"tx_type"` | ||
} | ||
|
||
func NewTimeLockDocument(stakingTxHashHex string, expireHeight uint32, txType string) *TimeLockDocument { | ||
return &TimeLockDocument{ | ||
StakingTxHashHex: stakingTxHashHex, | ||
ExpireHeight: expireHeight, | ||
TxType: txType, | ||
} | ||
} |
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,73 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
func (db *Database) SaveNewTimeLockExpire( | ||
ctx context.Context, stakingTxHashHex string, | ||
expireHeight uint32, txType string, | ||
) error { | ||
tlDoc := model.NewTimeLockDocument(stakingTxHashHex, expireHeight, txType) | ||
_, err := db.client.Database(db.dbName). | ||
Collection(model.TimeLockCollection). | ||
InsertOne(ctx, tlDoc) | ||
if err != nil { | ||
var writeErr mongo.WriteException | ||
if errors.As(err, &writeErr) { | ||
for _, e := range writeErr.WriteErrors { | ||
if mongo.IsDuplicateKeyError(e) { | ||
return &DuplicateKeyError{ | ||
Key: tlDoc.StakingTxHashHex, | ||
Message: "timelock already exists", | ||
} | ||
} | ||
} | ||
} | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (db *Database) FindExpiredDelegations(ctx context.Context, btcTipHeight, limit uint64) ([]model.TimeLockDocument, error) { | ||
client := db.client.Database(db.dbName).Collection(model.TimeLockCollection) | ||
filter := bson.M{"expire_height": bson.M{"$lte": btcTipHeight}} | ||
|
||
opts := options.Find().SetLimit(int64(limit)) | ||
cursor, err := client.Find(ctx, filter, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer cursor.Close(ctx) | ||
|
||
var delegations []model.TimeLockDocument | ||
if err = cursor.All(ctx, &delegations); err != nil { | ||
return nil, err | ||
} | ||
|
||
return delegations, nil | ||
} | ||
|
||
func (db *Database) DeleteExpiredDelegation(ctx context.Context, stakingTxHashHex string) error { | ||
client := db.client.Database(db.dbName).Collection(model.TimeLockCollection) | ||
filter := bson.M{"_id": stakingTxHashHex} | ||
|
||
result, err := client.DeleteOne(ctx, filter) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete expired delegation with stakingTxHashHex %v: %w", stakingTxHashHex, err) | ||
} | ||
|
||
// Check if any document was deleted | ||
if result.DeletedCount == 0 { | ||
return fmt.Errorf("no expired delegation found with stakingTxHashHex %v", stakingTxHashHex) | ||
} | ||
|
||
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
Oops, something went wrong.