diff --git a/core/constants.go b/core/constants.go index bff20dd6..25a1893e 100644 --- a/core/constants.go +++ b/core/constants.go @@ -132,6 +132,9 @@ const BuiltInFunctionUnGuardAccount = "UnGuardAccount" // BuiltInFunctionMigrateDataTrie is the built-in function key for migrating the data trie const BuiltInFunctionMigrateDataTrie = "MigrateDataTrie" +// BuiltInFunctionMigrateCodeLeaf is the built-in function key for migrating code leaf +const BuiltInFunctionMigrateCodeLeaf = "MigrateCodeLeaf" + // ESDTRoleLocalMint is the constant string for the local role of mint for ESDT tokens const ESDTRoleLocalMint = "ESDTRoleLocalMint" diff --git a/core/interface.go b/core/interface.go index 27888e2b..5aa1780a 100644 --- a/core/interface.go +++ b/core/interface.go @@ -68,6 +68,7 @@ type Throttler interface { type KeyValueHolder interface { Key() []byte Value() []byte + Version() TrieNodeVersion } // EpochSubscriberHandler defines the behavior of a component that can be notified if a new epoch was confirmed diff --git a/core/keyValStorage/keyValStorage.go b/core/keyValStorage/keyValStorage.go index 963f6245..06baa4f1 100644 --- a/core/keyValStorage/keyValStorage.go +++ b/core/keyValStorage/keyValStorage.go @@ -1,16 +1,20 @@ package keyValStorage +import "github.com/multiversx/mx-chain-core-go/core" + // KeyValStorage holds a key and an associated value type keyValStorage struct { - key []byte - value []byte + key []byte + value []byte + version core.TrieNodeVersion } // NewKeyValStorage creates a new key-value storage -func NewKeyValStorage(key []byte, val []byte) *keyValStorage { +func NewKeyValStorage(key []byte, val []byte, version core.TrieNodeVersion) *keyValStorage { return &keyValStorage{ - key: key, - value: val, + key: key, + value: val, + version: version, } } @@ -23,3 +27,8 @@ func (k *keyValStorage) Key() []byte { func (k *keyValStorage) Value() []byte { return k.value } + +// Version returns the version in the key-value storage +func (k *keyValStorage) Version() core.TrieNodeVersion { + return k.version +} diff --git a/core/keyValStorage/keyValStorage_test.go b/core/keyValStorage/keyValStorage_test.go index 7c57cb02..4d579285 100644 --- a/core/keyValStorage/keyValStorage_test.go +++ b/core/keyValStorage/keyValStorage_test.go @@ -3,6 +3,7 @@ package keyValStorage_test import ( "testing" + "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/keyValStorage" "github.com/stretchr/testify/assert" ) @@ -13,7 +14,7 @@ func TestNewKeyValStorage_GetKeyAndVal(t *testing.T) { key := []byte("key") value := []byte("value") - keyVal := keyValStorage.NewKeyValStorage(key, value) + keyVal := keyValStorage.NewKeyValStorage(key, value, core.NotSpecified) assert.NotNil(t, keyVal) assert.Equal(t, key, keyVal.Key()) assert.Equal(t, value, keyVal.Value()) diff --git a/core/trie.go b/core/trie.go index 84331e5c..d9a0d2c1 100644 --- a/core/trie.go +++ b/core/trie.go @@ -15,6 +15,10 @@ const ( // AutoBalanceEnabled is used for data tries, and only after the activation of AutoBalanceDataTriesEnableEpoch flag AutoBalanceEnabled + + // WithoutCodeLeaf is used for account with code, it specifies that the trie code leaf has been moved to storage, + // it is enabled only after the activation of MigrateCodeLeafEnableEpoch flag + WithoutCodeLeaf ) const ( @@ -24,7 +28,11 @@ const ( // AutoBalanceEnabledString is the string representation of AutoBalanceEnabled trie node version AutoBalanceEnabledString = "auto balanced" + // WithoutCodeLeafString is the string representation of WithoutCodeLeaf trie node version + WithoutCodeLeafString = "without code leaf" + autoBalanceDataTriesFlag = EnableEpochFlag("AutoBalanceDataTriesFlag") + migrateCodeLeafFlag = EnableEpochFlag("MigrateCodeLeafFlag") ) func (version TrieNodeVersion) String() string { @@ -33,6 +41,8 @@ func (version TrieNodeVersion) String() string { return NotSpecifiedString case AutoBalanceEnabled: return AutoBalanceEnabledString + case WithoutCodeLeaf: + return WithoutCodeLeafString default: return "unknown: " + strconv.Itoa(int(version)) } @@ -59,6 +69,9 @@ func NewTrieNodeVersionVerifier(enableEpochsHandler EnableEpochsHandler) (*trieN // IsValidVersion returns true if the given trie node version is valid func (vv *trieNodeVersionVerifier) IsValidVersion(version TrieNodeVersion) bool { + if vv.enableEpochsHandler.IsFlagEnabled(migrateCodeLeafFlag) { + return version <= WithoutCodeLeaf + } if vv.enableEpochsHandler.IsFlagEnabled(autoBalanceDataTriesFlag) { return version <= AutoBalanceEnabled } @@ -73,6 +86,9 @@ func (vv *trieNodeVersionVerifier) IsInterfaceNil() bool { // GetVersionForNewData returns the trie node version that should be used for new data func GetVersionForNewData(handler EnableEpochsHandler) TrieNodeVersion { + if handler.IsFlagEnabled(migrateCodeLeafFlag) { + return WithoutCodeLeaf + } if handler.IsFlagEnabled(autoBalanceDataTriesFlag) { return AutoBalanceEnabled }