Skip to content

Commit

Permalink
sponsor Fix Expire Lock Release
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed Jul 10, 2024
1 parent 9f2bc02 commit c8c13ca
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
17 changes: 17 additions & 0 deletions sponsor_manager/sponsor_changelog_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"AAStarCommunity/EthPaymaster_BackService/common/model"
"gorm.io/datatypes"
"math/big"
"time"
)

type UserSponsorBalanceUpdateLogDBModel struct {
Expand All @@ -26,6 +27,13 @@ func (UserSponsorBalanceUpdateLogDBModel) TableName() string {
func AddBalanceChangeLog(changeDbModel *UserSponsorBalanceUpdateLogDBModel) error {
return relayDB.Create(changeDbModel).Error
}
func GetBalanceChangeLogByTimePeriod(startTime, endTime time.Time) (models []*UserSponsorBalanceUpdateLogDBModel, err error) {
tx := relayDB.Model(&UserSponsorBalanceUpdateLogDBModel{}).Where("created_at >= ?", startTime).Where("created_at <= ?", endTime).Where("update_type in (?)", []global_const.UpdateType{global_const.UpdateTypeLock, global_const.UpdateTypeRelease}).Find(&models)
if tx.Error != nil {
return nil, tx.Error
}
return models, nil
}

func GetDepositAndWithDrawLog(userId string, IsTestNet bool) (models []*UserSponsorBalanceUpdateLogDBModel, err error) {
tx := relayDB.Model(&UserSponsorBalanceUpdateLogDBModel{}).Where("pay_user_id = ?", userId).Where("is_test_net = ?", IsTestNet).Where("update_type in (?)", []global_const.UpdateType{global_const.UpdateTypeDeposit, global_const.UpdateTypeWithdraw}).Find(&models)
Expand Down Expand Up @@ -65,3 +73,12 @@ func GetChangeModel(updateType global_const.UpdateType, payUserId string, txHash
return nil, nil
}
}

func GetChangeModelByUserOpHash(userOpHash string, isTestNet bool) (ChangeModel *UserSponsorBalanceUpdateLogDBModel, err error) {
tx := relayDB.Model(ChangeModel).Where("user_op_hash = ?", userOpHash).Where("is_test_net", isTestNet).First(&ChangeModel)
if tx.Error != nil {
return nil, tx.Error
} else {
return ChangeModel, nil
}
}
42 changes: 42 additions & 0 deletions sponsor_manager/sponsor_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"gorm.io/gorm"
"math/big"
"sync"
"time"
)

type Source string
Expand All @@ -38,6 +39,47 @@ func Init() {
}
relayDB = relayDBVar
})
go func() {
for {
time.Sleep(10 * time.Minute)
ReleaseExpireLockBalance()
}
}()
}
func ReleaseExpireLockBalance() {
// Get All Lock Balance
logList, err := GetBalanceChangeLogByTimePeriod(time.Now().Add(-15*time.Minute), time.Now().Add(-5*time.Minute))
if err != nil {
logrus.Errorf("ReleaseExpireLockBalance Error [%v]", err)
}
if len(logList) == 0 {
logrus.Info("[ReleaseExpireLockBalance] No Lock Balance")
return
}
lockMap := make(map[string]*UserSponsorBalanceUpdateLogDBModel)
releaseMap := make(map[string]*UserSponsorBalanceUpdateLogDBModel)
for _, changModel := range logList {
if changModel.UpdateType == global_const.UpdateTypeLock {
lockMap[changModel.UserOpHash] = changModel
} else if changModel.UpdateType == global_const.UpdateTypeRelease {
releaseMap[changModel.UserOpHash] = changModel
}
}

for userOpHash, lockModel := range lockMap {
_, ok := releaseMap[userOpHash]
if !ok {
userOpHashByte, _ := utils.DecodeStringWithPrefix(userOpHash)
_, err := ReleaseUserOpHashLockWhenFail(userOpHashByte, lockModel.IsTestNet)
if err != nil {
logrus.Errorf("ReleaseUserOpHashLockWhenFail Error [%v]", err)
}
logrus.Info("Release Expire Lock Balance [%s]", userOpHash)
continue
} else {
continue
}
}
}

//----------Functions----------
Expand Down
6 changes: 6 additions & 0 deletions sponsor_manager/sponsor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func TestSponsor(t *testing.T) {
testReleaseBalanceWithActualCost(t, "test", mockUserOpHash2, true, big.NewFloat(0.5))
},
},
{
"TestLockBalanceRelease",
func(t *testing.T) {
ReleaseExpireLockBalance()
},
},
}
for _, tt := range tests {
t.Run(tt.name, tt.test)
Expand Down

0 comments on commit c8c13ca

Please sign in to comment.