diff --git a/eth/backend.go b/eth/backend.go index c48a862ae046..a0b64a131c7f 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -318,7 +318,7 @@ func (s *Ethereum) APIs() []rpc.API { Service: backends.NewEthBackendServer(s.APIBackend), }) - sessionManager := suave_builder.NewSessionManager(s.blockchain, s.txPool, &suave_builder.Config{}) + sessionManager := suave_builder.NewSessionManager(s.blockchain, s.txPool, suave_builder.NewConfig()) apis = append(apis, rpc.API{ Namespace: "suavex", diff --git a/suave/builder/session_manager.go b/suave/builder/session_manager.go index f99d076d76fc..351bfbdd4668 100644 --- a/suave/builder/session_manager.go +++ b/suave/builder/session_manager.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "math/big" + "os" + "strconv" "sync" "time" @@ -25,6 +27,36 @@ type Config struct { MaxConcurrentSessions int } +func NewConfig() *Config { + cfg := &Config{ + GasCeil: 1000000000000000000, + SessionIdleTimeout: 5 * time.Second, + MaxConcurrentSessions: 16, + } + + // apply env if exists + var err error + if valStr := os.Getenv("GAS_CEIL"); valStr != "" { + if cfg.GasCeil, err = strconv.ParseUint(valStr, 10, 64); err != nil { + panic(fmt.Sprintf("failed to parse GAS_CEIL flag as uint: %v", err)) + } + } + + if valStr := os.Getenv("SESSION_IDLE_TIMEOUT"); valStr != "" { + if cfg.SessionIdleTimeout, err = time.ParseDuration(valStr); err != nil { + panic(fmt.Sprintf("failed to parse SESSION_IDLE_TIMEOUT flag as duration: %v", err)) + } + } + + if valStr := os.Getenv("MAX_CONCURRENT_SESSIONS"); valStr != "" { + if cfg.MaxConcurrentSessions, err = strconv.Atoi(valStr); err != nil { + panic(fmt.Sprintf("failed to parse MAX_CONCURRENT_SESSIONS flag as int: %v", err)) + } + } + + return cfg +} + type SessionManager struct { sem chan struct{} sessions map[string]*miner.Builder @@ -36,14 +68,8 @@ type SessionManager struct { } func NewSessionManager(blockchain *core.BlockChain, pool *txpool.TxPool, config *Config) *SessionManager { - if config.GasCeil == 0 { - config.GasCeil = 1000000000000000000 - } - if config.SessionIdleTimeout == 0 { - config.SessionIdleTimeout = 5 * time.Second - } - if config.MaxConcurrentSessions <= 0 { - config.MaxConcurrentSessions = 16 // chosen arbitrarily + if config == nil { + panic("empty session manager config") } sem := make(chan struct{}, config.MaxConcurrentSessions) @@ -197,7 +223,7 @@ func (s *SessionManager) Bid(sessionId string, blsPubKey phase0.BLSPubKey) (*api } func (s *SessionManager) GetBalance(sessionId string, addr common.Address) (*big.Int, error) { - builder, err := s.getSession(sessionId) + builder, err := s.getSession(sessionId, false) if err != nil { return nil, err } diff --git a/suave/builder/session_manager_test.go b/suave/builder/session_manager_test.go index 0cfbfa0aa426..1607922c60de 100644 --- a/suave/builder/session_manager_test.go +++ b/suave/builder/session_manager_test.go @@ -22,9 +22,9 @@ import ( ) func TestSessionManager_SessionTimeout(t *testing.T) { - mngr, _ := newSessionManager(t, &Config{ - SessionIdleTimeout: 500 * time.Millisecond, - }) + config := NewConfig() + config.SessionIdleTimeout = 500 * time.Millisecond + mngr, _ := newSessionManager(t, config) args := &api.BuildBlockArgs{} @@ -43,10 +43,10 @@ func TestSessionManager_MaxConcurrentSessions(t *testing.T) { const d = time.Millisecond * 100 args := &api.BuildBlockArgs{} - mngr, _ := newSessionManager(t, &Config{ - MaxConcurrentSessions: 1, - SessionIdleTimeout: d, - }) + config := NewConfig() + config.MaxConcurrentSessions = 1 + config.SessionIdleTimeout = d + mngr, _ := newSessionManager(t, config) t.Run("SessionAvailable", func(t *testing.T) { sess, err := mngr.NewSession(context.TODO(), args) @@ -76,9 +76,9 @@ func TestSessionManager_MaxConcurrentSessions(t *testing.T) { } func TestSessionManager_SessionRefresh(t *testing.T) { - mngr, _ := newSessionManager(t, &Config{ - SessionIdleTimeout: 500 * time.Millisecond, - }) + config := NewConfig() + config.SessionIdleTimeout = 500 * time.Millisecond + mngr, _ := newSessionManager(t, config) args := &api.BuildBlockArgs{} id, err := mngr.NewSession(context.TODO(), args) @@ -103,8 +103,7 @@ func TestSessionManager_SessionRefresh(t *testing.T) { } func TestSessionManager_StartSession(t *testing.T) { - // test that the session starts and it can simulate transactions - mngr, bMock := newSessionManager(t, &Config{}) + mngr, bMock := newSessionManager(t, NewConfig()) args := &api.BuildBlockArgs{} id, err := mngr.NewSession(context.TODO(), args)