Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non critical fixes for Sirius (patch 2) #5805

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 20 additions & 8 deletions storage/factory/dbConfigHandler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package factory

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -14,6 +15,10 @@ const (
defaultType = "LvlDBSerial"
)

var (
errInvalidConfiguration = errors.New("invalid configuration")
)

type dbConfigHandler struct {
dbType string
batchDelaySeconds int
Expand All @@ -38,7 +43,7 @@ func NewDBConfigHandler(config config.DBConfig) *dbConfigHandler {
// GetDBConfig will get the db config based on path
func (dh *dbConfigHandler) GetDBConfig(path string) (*config.DBConfig, error) {
dbConfigFromFile := &config.DBConfig{}
err := core.LoadTomlFile(dbConfigFromFile, getPersisterConfigFilePath(path))
err := readCorrectConfigurationFromToml(dbConfigFromFile, getPersisterConfigFilePath(path))
if err == nil {
log.Debug("GetDBConfig: loaded db config from toml config file",
"config path", path,
Expand Down Expand Up @@ -79,6 +84,20 @@ func (dh *dbConfigHandler) GetDBConfig(path string) (*config.DBConfig, error) {
return dbConfig, nil
}

func readCorrectConfigurationFromToml(dbConfig *config.DBConfig, filePath string) error {
err := core.LoadTomlFile(dbConfig, filePath)
if err != nil {
return err
}

isInvalidConfig := len(dbConfig.Type) == 0 || dbConfig.MaxBatchSize <= 0 || dbConfig.BatchDelaySeconds <= 0 || dbConfig.MaxOpenFiles <= 0
if isInvalidConfig {
return errInvalidConfiguration
}

return nil
}

// SaveDBConfigToFilePath will save the provided db config to specified path
func (dh *dbConfigHandler) SaveDBConfigToFilePath(path string, dbConfig *config.DBConfig) error {
pathExists, err := checkIfDirExists(path)
Expand All @@ -92,13 +111,6 @@ func (dh *dbConfigHandler) SaveDBConfigToFilePath(path string, dbConfig *config.

configFilePath := getPersisterConfigFilePath(path)

loadedDBConfig := &config.DBConfig{}
err = core.LoadTomlFile(loadedDBConfig, configFilePath)
if err == nil {
// config file already exists, no need to save config
return nil
}

err = core.SaveTomlFile(dbConfig, configFilePath)
if err != nil {
return err
Expand Down
58 changes: 51 additions & 7 deletions storage/factory/dbConfigHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package factory_test

import (
"os"
"path"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/storage/factory"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -88,6 +90,37 @@ func TestDBConfigHandler_GetDBConfig(t *testing.T) {
require.Nil(t, err)
require.Equal(t, expectedDBConfig, conf)
})
t.Run("empty config.toml file, load default db config", func(t *testing.T) {
t.Parallel()

testConfig := createDefaultDBConfig()
testConfig.BatchDelaySeconds = 37
testConfig.MaxBatchSize = 38
testConfig.MaxOpenFiles = 39
testConfig.ShardIDProviderType = "BinarySplit"
testConfig.NumShards = 4
pf := factory.NewDBConfigHandler(testConfig)

dirPath := t.TempDir()

f, _ := os.Create(path.Join(dirPath, factory.DBConfigFileName))
_ = f.Close()

expectedDBConfig := &config.DBConfig{
FilePath: "",
Type: factory.DefaultType,
BatchDelaySeconds: testConfig.BatchDelaySeconds,
MaxBatchSize: testConfig.MaxBatchSize,
MaxOpenFiles: testConfig.MaxOpenFiles,
UseTmpAsFilePath: false,
ShardIDProviderType: "",
NumShards: 0,
}

conf, err := pf.GetDBConfig(dirPath)
require.Nil(t, err)
require.Equal(t, expectedDBConfig, conf)
})
t.Run("empty dir, load db config from main config", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -146,22 +179,33 @@ func TestDBConfigHandler_SaveDBConfigToFilePath(t *testing.T) {
err := pf.SaveDBConfigToFilePath("no/valid/path", &dbConfig)
require.Nil(t, err)
})

t.Run("config file already present, should not fail", func(t *testing.T) {
t.Run("config file already present, should not fail and should rewrite", func(t *testing.T) {
t.Parallel()

dbConfig := createDefaultDBConfig()
dbConfig1 := createDefaultDBConfig()
dbConfig1.MaxOpenFiles = 37
dbConfig1.Type = "dbconfig1"
dirPath := t.TempDir()
configPath := factory.GetPersisterConfigFilePath(dirPath)

err := core.SaveTomlFile(dbConfig, configPath)
err := core.SaveTomlFile(dbConfig1, configPath)
require.Nil(t, err)

pf := factory.NewDBConfigHandler(dbConfig)
err = pf.SaveDBConfigToFilePath(dirPath, &dbConfig)
pf := factory.NewDBConfigHandler(dbConfig1)

dbConfig2 := createDefaultDBConfig()
dbConfig2.MaxOpenFiles = 38
dbConfig2.Type = "dbconfig2"

err = pf.SaveDBConfigToFilePath(dirPath, &dbConfig2)
require.Nil(t, err)

loadedDBConfig := &config.DBConfig{}
err = core.LoadTomlFile(loadedDBConfig, path.Join(dirPath, "config.toml"))
require.Nil(t, err)
})

assert.Equal(t, dbConfig2, *loadedDBConfig)
})
t.Run("should work", func(t *testing.T) {
t.Parallel()

Expand Down
3 changes: 3 additions & 0 deletions storage/factory/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
// DefaultType exports the defaultType const to be used in tests
const DefaultType = defaultType

// DBConfigFileName exports the dbConfigFileName const to be used in tests
const DBConfigFileName = dbConfigFileName

// GetPersisterConfigFilePath -
func GetPersisterConfigFilePath(path string) string {
return getPersisterConfigFilePath(path)
Expand Down
26 changes: 24 additions & 2 deletions vm/systemSmartContracts/esdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/vm"
logger "github.com/multiversx/mx-chain-logger-go"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
)

Expand Down Expand Up @@ -1343,16 +1344,37 @@ func (e *esdt) getSpecialRoles(args *vmcommon.ContractCallInput) vmcommon.Return
rolesAsString = append(rolesAsString, string(role))
}

specialRoleAddress := e.addressPubKeyConverter.SilentEncode(specialRole.Address, log)

roles := strings.Join(rolesAsString, ",")

specialRoleAddress, errEncode := e.addressPubKeyConverter.Encode(specialRole.Address)
e.treatErrorForGetSpecialRoles(errEncode, rolesAsString, specialRole.Address)

message := fmt.Sprintf("%s:%s", specialRoleAddress, roles)
e.eei.Finish([]byte(message))
}

return vmcommon.Ok
}

func (e *esdt) treatErrorForGetSpecialRoles(err error, roles []string, address []byte) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

treatEncodeError...?

if err == nil {
return
}

logLevel := logger.LogTrace
for _, role := range roles {
if role != vmcommon.ESDTRoleBurnForAll {
logLevel = logger.LogWarning
break
}
}

log.Log(logLevel, "esdt.treatErrorForGetSpecialRoles",
"hex specialRole.Address", hex.EncodeToString(address),
"roles", strings.Join(roles, ", "),
"error", err)
}

func (e *esdt) basicOwnershipChecks(args *vmcommon.ContractCallInput) (*ESDTDataV2, vmcommon.ReturnCode) {
if args.CallValue.Cmp(zero) != 0 {
e.eei.AddReturnMessage("callValue must be 0")
Expand Down
57 changes: 57 additions & 0 deletions vm/systemSmartContracts/esdt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,63 @@ func TestEsdt_GetSpecialRolesShouldWork(t *testing.T) {
assert.Equal(t, []byte("erd1e7n8rzxdtl2n2fl6mrsg4l7stp2elxhfy6l9p7eeafspjhhrjq7qk05usw:ESDTRoleNFTAddQuantity,ESDTRoleNFTCreate,ESDTRoleNFTBurn"), eei.output[1])
}

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

tokenName := []byte("esdtToken")
args := createMockArgumentsForESDT()
eei := createDefaultEei()
args.Eei = eei

addr := ""
addrBytes, _ := testscommon.RealWorldBech32PubkeyConverter.Decode(addr)

specialRoles := []*ESDTRoles{
{
Address: addrBytes,
Roles: [][]byte{
[]byte(core.ESDTRoleLocalMint),
[]byte(core.ESDTRoleLocalBurn),
},
},
{
Address: addrBytes,
Roles: [][]byte{
[]byte(core.ESDTRoleNFTAddQuantity),
[]byte(core.ESDTRoleNFTCreate),
[]byte(core.ESDTRoleNFTBurn),
},
},
{
Address: addrBytes,
Roles: [][]byte{
[]byte(vmcommon.ESDTRoleBurnForAll),
},
},
}
tokensMap := map[string][]byte{}
marshalizedData, _ := args.Marshalizer.Marshal(ESDTDataV2{
SpecialRoles: specialRoles,
})
tokensMap[string(tokenName)] = marshalizedData
eei.storageUpdate[string(eei.scAddress)] = tokensMap
args.Eei = eei

args.AddressPubKeyConverter = testscommon.RealWorldBech32PubkeyConverter

e, _ := NewESDTSmartContract(args)

eei.output = make([][]byte, 0)
vmInput := getDefaultVmInputForFunc("getSpecialRoles", [][]byte{[]byte("esdtToken")})
output := e.Execute(vmInput)
assert.Equal(t, vmcommon.Ok, output)

assert.Equal(t, 3, len(eei.output))
assert.Equal(t, []byte(":ESDTRoleLocalMint,ESDTRoleLocalBurn"), eei.output[0])
assert.Equal(t, []byte(":ESDTRoleNFTAddQuantity,ESDTRoleNFTCreate,ESDTRoleNFTBurn"), eei.output[1])
assert.Equal(t, []byte(":ESDTRoleBurnForAll"), eei.output[2])
}

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

Expand Down