Skip to content

Commit

Permalink
Merge pull request #255 from multiversx/feat/refactor_enable_epochs_h…
Browse files Browse the repository at this point in the history
…andler

Feat/refactor enable epochs handler
  • Loading branch information
sstanculeanu authored Oct 2, 2023
2 parents 5bdc450 + ea59ee0 commit 059ccae
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 83 deletions.
25 changes: 25 additions & 0 deletions core/epochFlags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core

import (
"fmt"

"github.com/multiversx/mx-chain-core-go/core/check"
)

// EnableEpochFlag defines a flag specific to the enableEpochs.toml
type EnableEpochFlag string

// CheckHandlerCompatibility checks if the provided handler is compatible with this mx-chain-core-go version
func CheckHandlerCompatibility(handler EnableEpochsHandler, requiredFlags []EnableEpochFlag) error {
if check.IfNil(handler) {
return ErrNilEnableEpochsHandler
}

for _, flag := range requiredFlags {
if !handler.IsFlagDefined(flag) {
return fmt.Errorf("%w for flag %s", ErrInvalidEnableEpochsHandler, flag)
}
}

return nil
}
45 changes: 45 additions & 0 deletions core/epochFlags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package core_test

import (
"errors"
"strings"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/mock"
"github.com/stretchr/testify/require"
)

func TestCheckHandlerCompatibility(t *testing.T) {
t.Parallel()

err := core.CheckHandlerCompatibility(nil, []core.EnableEpochFlag{})
require.Equal(t, core.ErrNilEnableEpochsHandler, err)

testFlags := []core.EnableEpochFlag{"f0", "f1", "f2"}
allFlagsDefinedHandler := &mock.EnableEpochsHandlerStub{
IsFlagDefinedCalled: func(flag core.EnableEpochFlag) bool {
return true
},
}
err = core.CheckHandlerCompatibility(allFlagsDefinedHandler, testFlags)
require.Nil(t, err)

allFlagsUndefinedHandler := &mock.EnableEpochsHandlerStub{
IsFlagDefinedCalled: func(flag core.EnableEpochFlag) bool {
return false
},
}
err = core.CheckHandlerCompatibility(allFlagsUndefinedHandler, testFlags)
require.True(t, errors.Is(err, core.ErrInvalidEnableEpochsHandler))

missingFlag := testFlags[1]
oneFlagUndefinedHandler := &mock.EnableEpochsHandlerStub{
IsFlagDefinedCalled: func(flag core.EnableEpochFlag) bool {
return flag != missingFlag
},
}
err = core.CheckHandlerCompatibility(oneFlagUndefinedHandler, testFlags)
require.True(t, errors.Is(err, core.ErrInvalidEnableEpochsHandler))
require.True(t, strings.Contains(err.Error(), string(missingFlag)))
}
5 changes: 4 additions & 1 deletion core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var ErrInvalidValue = errors.New("invalid value provided")
// ErrNilInputData signals that a nil data has been provided
var ErrNilInputData = errors.New("nil input data")

//ErrNilUrl signals that the provided url is empty
// ErrNilUrl signals that the provided url is empty
var ErrNilUrl = errors.New("url is empty")

// ErrPemFileIsInvalid signals that a pem file is invalid
Expand Down Expand Up @@ -118,3 +118,6 @@ var ErrDBIsClosed = errors.New("DB is closed")

// ErrNilEnableEpochsHandler signals that a nil enable epochs handler has been provided
var ErrNilEnableEpochsHandler = errors.New("nil enable epochs handler")

// ErrInvalidEnableEpochsHandler signals that an invalid enable epochs handler has been provided
var ErrInvalidEnableEpochsHandler = errors.New("invalid enable epochs handler")
32 changes: 32 additions & 0 deletions core/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,42 @@ package core

import "time"

// GetContainingDuration -
func (sw *StopWatch) GetContainingDuration() (map[string]time.Duration, []string) {
return sw.getContainingDuration()
}

// GetIdentifiers -
func (sw *StopWatch) GetIdentifiers() []string {
return sw.identifiers
}

// SetIdentifiers -
func (sw *StopWatch) SetIdentifiers(identifiers []string) {
sw.identifiers = identifiers
}

// GetStarted -
func (sw *StopWatch) GetStarted(identifier string) (time.Time, bool) {
s, has := sw.started[identifier]
return s, has
}

// GetElapsed -
func (sw *StopWatch) GetElapsed(identifier string) (time.Duration, bool) {
e, has := sw.elapsed[identifier]
return e, has
}

// SetElapsed -
func (sw *StopWatch) SetElapsed(identifier string, duration time.Duration) {
sw.elapsed[identifier] = duration
}

// SplitExponentFraction -
func SplitExponentFraction(val string) (string, string) {
return splitExponentFraction(val)
}

// TestAutoBalanceDataTriesFlag -
const TestAutoBalanceDataTriesFlag = autoBalanceDataTriesFlag
6 changes: 3 additions & 3 deletions core/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -152,7 +152,7 @@ func LoadSkPkFromPemFile(relativePath string, skIndex int) ([]byte, string, erro
_ = file.Close()
}()

buff, err := ioutil.ReadAll(file)
buff, err := io.ReadAll(file)
if err != nil {
return nil, "", fmt.Errorf("%w while reading %s file", err, relativePath)
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func LoadAllKeysFromPemFile(relativePath string) ([][]byte, []string, error) {
_ = file.Close()
}()

buff, err := ioutil.ReadAll(file)
buff, err := io.ReadAll(file)
if err != nil {
return nil, nil, fmt.Errorf("%w while reading %s file", err, relativePath)
}
Expand Down
3 changes: 1 addition & 2 deletions core/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core_test
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -146,7 +145,7 @@ func TestLoadJSonFile_FileExitsShouldPass(t *testing.T) {

data, _ := json.MarshalIndent(TestStruct{A: 0, B: 0}, "", " ")

_ = ioutil.WriteFile(fileName, data, 0644)
_ = os.WriteFile(fileName, data, 0644)

err = file.Close()
assert.Nil(t, err)
Expand Down
5 changes: 4 additions & 1 deletion core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ type TrieNodeVersionVerifier interface {

// EnableEpochsHandler defines the behavior of a component that can return if a feature is enabled or not
type EnableEpochsHandler interface {
IsAutoBalanceDataTriesEnabled() bool
IsFlagDefined(flag EnableEpochFlag) bool
IsFlagEnabled(flag EnableEpochFlag) bool
IsFlagEnabledInEpoch(flag EnableEpochFlag, epoch uint32) bool
GetActivationEpoch(flag EnableEpochFlag) uint32
IsInterfaceNil() bool
}
7 changes: 4 additions & 3 deletions core/loggingFunctions_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package core
package core_test

import (
"fmt"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/mock"
"github.com/stretchr/testify/require"
)
Expand All @@ -18,12 +19,12 @@ func TestDumpGoRoutinesToLogShouldNotPanic(t *testing.T) {
}
}()

DumpGoRoutinesToLog(0, &mock.LoggerMock{})
core.DumpGoRoutinesToLog(0, &mock.LoggerMock{})
}

func TestGetRunningGoRoutines(t *testing.T) {
t.Parallel()

res := GetRunningGoRoutines(&mock.LoggerMock{})
res := core.GetRunningGoRoutines(&mock.LoggerMock{})
require.NotNil(t, res)
}
20 changes: 0 additions & 20 deletions core/mock/enableEpochsHandlerMock.go

This file was deleted.

48 changes: 48 additions & 0 deletions core/mock/enableEpochsHandlerStub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package mock

import "github.com/multiversx/mx-chain-core-go/core"

// EnableEpochsHandlerStub -
type EnableEpochsHandlerStub struct {
IsFlagDefinedCalled func(flag core.EnableEpochFlag) bool
IsFlagEnabledCalled func(flag core.EnableEpochFlag) bool
IsFlagEnabledInEpochCalled func(flag core.EnableEpochFlag, epoch uint32) bool
GetActivationEpochCalled func(flag core.EnableEpochFlag) uint32
}

// IsFlagDefined -
func (stub *EnableEpochsHandlerStub) IsFlagDefined(flag core.EnableEpochFlag) bool {
if stub.IsFlagDefinedCalled != nil {
return stub.IsFlagDefinedCalled(flag)
}
return false
}

// IsFlagEnabled -
func (stub *EnableEpochsHandlerStub) IsFlagEnabled(flag core.EnableEpochFlag) bool {
if stub.IsFlagEnabledCalled != nil {
return stub.IsFlagEnabledCalled(flag)
}
return false
}

// IsFlagEnabledInEpoch -
func (stub *EnableEpochsHandlerStub) IsFlagEnabledInEpoch(flag core.EnableEpochFlag, epoch uint32) bool {
if stub.IsFlagEnabledInEpochCalled != nil {
return stub.IsFlagEnabledInEpochCalled(flag, epoch)
}
return false
}

// GetActivationEpoch -
func (stub *EnableEpochsHandlerStub) GetActivationEpoch(flag core.EnableEpochFlag) uint32 {
if stub.GetActivationEpochCalled != nil {
return stub.GetActivationEpochCalled(flag)
}
return 0
}

// IsInterfaceNil -
func (stub *EnableEpochsHandlerStub) IsInterfaceNil() bool {
return stub == nil
}
Loading

0 comments on commit 059ccae

Please sign in to comment.