From c8c13cac97460b73046497767ffbbf3e60790247 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Wed, 10 Jul 2024 16:14:46 +0800 Subject: [PATCH 1/5] sponsor Fix Expire Lock Release --- .../sponsor_changelog_repository.go | 17 ++++++++ sponsor_manager/sponsor_service.go | 42 +++++++++++++++++++ sponsor_manager/sponsor_test.go | 6 +++ 3 files changed, 65 insertions(+) diff --git a/sponsor_manager/sponsor_changelog_repository.go b/sponsor_manager/sponsor_changelog_repository.go index 882b6116..c6185765 100644 --- a/sponsor_manager/sponsor_changelog_repository.go +++ b/sponsor_manager/sponsor_changelog_repository.go @@ -5,6 +5,7 @@ import ( "AAStarCommunity/EthPaymaster_BackService/common/model" "gorm.io/datatypes" "math/big" + "time" ) type UserSponsorBalanceUpdateLogDBModel struct { @@ -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) @@ -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 + } +} diff --git a/sponsor_manager/sponsor_service.go b/sponsor_manager/sponsor_service.go index 59f46a01..827416aa 100644 --- a/sponsor_manager/sponsor_service.go +++ b/sponsor_manager/sponsor_service.go @@ -13,6 +13,7 @@ import ( "gorm.io/gorm" "math/big" "sync" + "time" ) type Source string @@ -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---------- diff --git a/sponsor_manager/sponsor_test.go b/sponsor_manager/sponsor_test.go index 05c96aa7..c9001476 100644 --- a/sponsor_manager/sponsor_test.go +++ b/sponsor_manager/sponsor_test.go @@ -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) From dc14bad0f34bd37510acad97915aeb4c60769c13 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Wed, 10 Jul 2024 16:17:38 +0800 Subject: [PATCH 2/5] fix govulncheck --- Dockerfile | 2 +- go.mod | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index e285f123..9c2dc95c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ ## build -FROM golang:1.22.4-alpine3.19 AS build-env +FROM golang:1.22.5-alpine3.19 AS build-env RUN apk add build-base diff --git a/go.mod b/go.mod index e0df251a..2dd0efb8 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module AAStarCommunity/EthPaymaster_BackService -go 1.22.4 +go 1.22.5 require ( github.com/NethermindEth/starknet.go v0.7.0 From c98347173baa7357d680e2758c7b52b25c009aa1 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Wed, 10 Jul 2024 16:22:16 +0800 Subject: [PATCH 3/5] fix github action --- sponsor_manager/sponsor_service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sponsor_manager/sponsor_service.go b/sponsor_manager/sponsor_service.go index 827416aa..f1f8f921 100644 --- a/sponsor_manager/sponsor_service.go +++ b/sponsor_manager/sponsor_service.go @@ -74,7 +74,7 @@ func ReleaseExpireLockBalance() { if err != nil { logrus.Errorf("ReleaseUserOpHashLockWhenFail Error [%v]", err) } - logrus.Info("Release Expire Lock Balance [%s]", userOpHash) + logrus.Infof("Release Expire Lock Balance [%s]", userOpHash) continue } else { continue From 4d7038875bb93042514ea00a0d8199fe7e9ccba1 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Wed, 10 Jul 2024 16:27:25 +0800 Subject: [PATCH 4/5] fix github action --- common/price_compoent/price_util_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/common/price_compoent/price_util_test.go b/common/price_compoent/price_util_test.go index 7211d590..cd8b5467 100644 --- a/common/price_compoent/price_util_test.go +++ b/common/price_compoent/price_util_test.go @@ -40,7 +40,3 @@ func testGetPriceUsd(t *testing.T, tokenType global_const.TokenType) { } t.Logf("price:%v", price) } - -func TestGetCoinGeckoPrice(t *testing.T) { - GetConfigTokenPrice() -} From ee5bfc5d6f3fd3a5d84b16410f81337c0124a846 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Wed, 10 Jul 2024 16:28:40 +0800 Subject: [PATCH 5/5] fix github action --- sponsor_manager/sponsor_service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sponsor_manager/sponsor_service.go b/sponsor_manager/sponsor_service.go index f1f8f921..d5e960bd 100644 --- a/sponsor_manager/sponsor_service.go +++ b/sponsor_manager/sponsor_service.go @@ -48,7 +48,7 @@ func Init() { } func ReleaseExpireLockBalance() { // Get All Lock Balance - logList, err := GetBalanceChangeLogByTimePeriod(time.Now().Add(-15*time.Minute), time.Now().Add(-5*time.Minute)) + logList, err := GetBalanceChangeLogByTimePeriod(time.Now().Add(-20*time.Minute), time.Now().Add(-5*time.Minute)) if err != nil { logrus.Errorf("ReleaseExpireLockBalance Error [%v]", err) }