From 9af37d89e9abe38399c8a8ac0e99a96b3ddd4445 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Mon, 17 Jun 2024 18:11:09 +0800 Subject: [PATCH] update bug --- common/model/env.go | 10 ++++- common/network/ethereum_adaptable_executor.go | 45 +++++++++++-------- config/secret_config.go | 2 - rpc_server/api/v1/paymaster.go | 1 - service/operator/operator_test.go | 17 +++++-- service/validator_service/basic_validator.go | 4 +- 6 files changed, 50 insertions(+), 29 deletions(-) diff --git a/common/model/env.go b/common/model/env.go index 83444ec6..f553b372 100644 --- a/common/model/env.go +++ b/common/model/env.go @@ -7,6 +7,7 @@ import ( const EnvKey = "Env" const ProdEnv = "prod" const DevEnv = "dev" +const UnitEnv = "unit" type Env struct { Name string // env Name, like `prod`, `dev` and etc., @@ -14,9 +15,11 @@ type Env struct { } func (env *Env) IsDevelopment() bool { - return strings.EqualFold(DevEnv, env.Name) + return strings.EqualFold(DevEnv, env.Name) || strings.EqualFold(UnitEnv, env.Name) +} +func (env *Env) IsUnit() bool { + return strings.EqualFold(UnitEnv, env.Name) } - func (env *Env) IsProduction() bool { return strings.EqualFold(ProdEnv, env.Name) } @@ -24,3 +27,6 @@ func (env *Env) IsProduction() bool { func (env *Env) GetEnvName() *string { return &env.Name } +func (env *Env) SetUnitEnv() { + env.Name = UnitEnv +} diff --git a/common/network/ethereum_adaptable_executor.go b/common/network/ethereum_adaptable_executor.go index d2d4fffc..56205908 100644 --- a/common/network/ethereum_adaptable_executor.go +++ b/common/network/ethereum_adaptable_executor.go @@ -15,6 +15,7 @@ import ( "AAStarCommunity/EthPaymaster_BackService/common/user_op" "AAStarCommunity/EthPaymaster_BackService/common/utils" "AAStarCommunity/EthPaymaster_BackService/config" + "AAStarCommunity/EthPaymaster_BackService/envirment" "AAStarCommunity/EthPaymaster_BackService/schedulor" "context" "crypto/ecdsa" @@ -96,29 +97,35 @@ func GetEthereumExecutor(network global_const.Network) *EthereumExecutor { if !success { panic(xerrors.Errorf("chainId %s is invalid", config.GetChainId(network))) } - wsUrl := config.GetNewWorkClientURl(network) - wsUrl = strings.Replace(wsUrl, "https", "wss", 1) - logrus.Debugf("wsUrl: %s", wsUrl) - webSocketClient, err := ethclient.Dial(wsUrl) - if err != nil { - panic(err) - } - eventListener, err := schedulor.NewEventListener(webSocketClient, network) - if err != nil { - panic(err) - } - go eventListener.Listen() logrus.Debugf("after Lesten network :[%s]", network) geth := gethclient.New(client.Client()) - executorMap[network] = &EthereumExecutor{ - network: network, - Client: client, - ChainId: chainId, - GethClient: geth, - eventListener: eventListener, - webSocketClient: webSocketClient, + ethExecutor := &EthereumExecutor{ + network: network, + Client: client, + ChainId: chainId, + GethClient: geth, + } + + if !envirment.Environment.IsUnit() { + logrus.Infof("Init EventListener network :[%s]", network) + wsUrl := config.GetNewWorkClientURl(network) + wsUrl = strings.Replace(wsUrl, "https", "wss", 1) + logrus.Debugf("wsUrl: %s", wsUrl) + webSocketClient, err := ethclient.Dial(wsUrl) + if err != nil { + panic(err) + } + + eventListener, err := schedulor.NewEventListener(webSocketClient, network) + if err != nil { + panic(err) + } + go eventListener.Listen() + ethExecutor.eventListener = eventListener + ethExecutor.webSocketClient = webSocketClient } + executorMap[network] = ethExecutor return executorMap[network] } diff --git a/config/secret_config.go b/config/secret_config.go index 15e7533c..ecdde263 100644 --- a/config/secret_config.go +++ b/config/secret_config.go @@ -105,8 +105,6 @@ func secretConfigInit(secretConfigPath string) { } func IsSponsorWhitelist(senderAddress string) bool { - logrus.Debugf("IsSponsorWhitelist [%s]", senderAddress) - logrus.Debugf("IsSponsorWhitelist [%v]", sponsorWhitelist) return sponsorWhitelist.Contains(senderAddress) } func GetNetworkSecretConfig(network global_const.Network) model.NetWorkSecretConfig { diff --git a/rpc_server/api/v1/paymaster.go b/rpc_server/api/v1/paymaster.go index 3a46e24f..0c99a5a4 100644 --- a/rpc_server/api/v1/paymaster.go +++ b/rpc_server/api/v1/paymaster.go @@ -188,6 +188,5 @@ func validateUserOpRequest(request *model.UserOpRequest) error { if request.Network == "" { return xerrors.Errorf("ForceNetwork is empty") } - return nil } diff --git a/service/operator/operator_test.go b/service/operator/operator_test.go index 4d887c73..68311b7a 100644 --- a/service/operator/operator_test.go +++ b/service/operator/operator_test.go @@ -7,12 +7,15 @@ import ( "AAStarCommunity/EthPaymaster_BackService/common/user_op" "AAStarCommunity/EthPaymaster_BackService/common/utils" "AAStarCommunity/EthPaymaster_BackService/config" + "AAStarCommunity/EthPaymaster_BackService/envirment" + "AAStarCommunity/EthPaymaster_BackService/service/dashboard_service" "AAStarCommunity/EthPaymaster_BackService/sponsor_manager" "encoding/json" "fmt" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient" "github.com/sirupsen/logrus" + "os" "testing" ) @@ -26,6 +29,8 @@ func TestOperator(t *testing.T) { mockRequestNotSupport1559 := getMockTryPayUserOpRequest() mockRequestNotSupport1559.UserOp["maxPriorityFeePerGas"] = mockRequestNotSupport1559.UserOp["maxFeePerGas"] sponsor_manager.Init() + dashboard_service.Init() + envirment.Environment.SetUnitEnv() tests := []struct { name string test func(t *testing.T) @@ -129,8 +134,9 @@ func TestOperator(t *testing.T) { "Test_NoSpectCode_TryPayUserOpExecute", func(t *testing.T) { request := model.UserOpRequest{ - Network: global_const.EthereumSepolia, - UserOp: *utils.GenerateMockUservOperation(), + StrategyCode: "3123124__7dtFu", + Network: global_const.EthereumSepolia, + UserOp: *utils.GenerateMockUservOperation(), } testTryPayUserOpExecute(t, &request) }, @@ -169,7 +175,9 @@ func testGetSupportEntrypointExecute(t *testing.T) { t.Log(res) } func testTryPayUserOpExecute(t *testing.T, request *model.UserOpRequest) { - result, err := TryPayUserOpExecute(&model.ApiKeyModel{}, request) + result, err := TryPayUserOpExecute(&model.ApiKeyModel{ + UserId: 5, + }, request) if err != nil { t.Fatal(err) return @@ -224,6 +232,9 @@ func getMockTryPayUserOpRequest() *model.UserOpRequest { } func TestWSclient(t *testing.T) { + os.Setenv("Env", "unit") + + t.Logf("Env: %v", os.Getenv("Env")) //TODO url := "wss://eth-sepolia.g.alchemy.com/v2/wKeLycGxgYRykgf0aGfcpEkUtqyLQg4v" wsClient, err := ethclient.Dial(url) diff --git a/service/validator_service/basic_validator.go b/service/validator_service/basic_validator.go index 62ea339e..d33abd6d 100644 --- a/service/validator_service/basic_validator.go +++ b/service/validator_service/basic_validator.go @@ -39,11 +39,11 @@ func ValidateStrategy(strategy *model.Strategy, request *model.UserOpRequest) er curTime := time.Now().Unix() //check Time if strategy.ExecuteRestriction.EffectiveStartTime != nil { - if curTime < strategy.ExecuteRestriction.EffectiveStartTime.Int64() { + if curTime < strategy.ExecuteRestriction.EffectiveStartTime.Int64() && strategy.ExecuteRestriction.EffectiveEndTime.Sign() > 0 { return xerrors.Errorf("curTime [%s] is OutOff EffectiveStartTime [%s]", curTime, strategy.ExecuteRestriction.EffectiveStartTime.Int64()) } } - if strategy.ExecuteRestriction.EffectiveEndTime != nil { + if strategy.ExecuteRestriction.EffectiveEndTime != nil && strategy.ExecuteRestriction.EffectiveEndTime.Sign() > 0 { if curTime > strategy.ExecuteRestriction.EffectiveEndTime.Int64() { return xerrors.Errorf("curTime [%s] is OutOff EffectiveEndTime [%s]", curTime, strategy.ExecuteRestriction.EffectiveEndTime.Int64()) }