Skip to content

Commit

Permalink
Merge pull request #5642 from multiversx/last-snapshot-marker-fix
Browse files Browse the repository at this point in the history
lastSnapshot marker fix
  • Loading branch information
BeniaminDrasovean committed Oct 27, 2023
2 parents 46f6022 + 5726d2d commit 9d81e92
Show file tree
Hide file tree
Showing 23 changed files with 479 additions and 238 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
hub release create --draft --message="Release draft from Github Actions" vNext
gh release create --draft --notes="Release draft from Github Actions" vNext
sleep 10
for i in $(find ./assets -name '*.tgz' -type f); do
hub release edit --attach=${i} --message="" vNext
gh release upload vNext ${i}
done
2 changes: 1 addition & 1 deletion cmd/node/config/gasSchedules/gasScheduleV1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SetGuardian = 250000
GuardAccount = 250000
UnGuardAccount = 250000
TrieLoadPerNode = 20000
TrieLoadPerNode = 100000
TrieStorePerNode = 50000

[MetaChainSystemSCsCost]
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/config/gasSchedules/gasScheduleV2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SetGuardian = 250000
GuardAccount = 250000
UnGuardAccount = 250000
TrieLoadPerNode = 20000
TrieLoadPerNode = 100000
TrieStorePerNode = 50000

[MetaChainSystemSCsCost]
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/config/gasSchedules/gasScheduleV3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SetGuardian = 250000
GuardAccount = 250000
UnGuardAccount = 250000
TrieLoadPerNode = 20000
TrieLoadPerNode = 100000
TrieStorePerNode = 50000

[MetaChainSystemSCsCost]
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/config/gasSchedules/gasScheduleV4.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SetGuardian = 250000
GuardAccount = 250000
UnGuardAccount = 250000
TrieLoadPerNode = 20000
TrieLoadPerNode = 100000
TrieStorePerNode = 50000

[MetaChainSystemSCsCost]
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/config/gasSchedules/gasScheduleV5.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SetGuardian = 250000
GuardAccount = 250000
UnGuardAccount = 250000
TrieLoadPerNode = 20000
TrieLoadPerNode = 100000
TrieStorePerNode = 50000

[MetaChainSystemSCsCost]
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/config/gasSchedules/gasScheduleV6.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SetGuardian = 250000
GuardAccount = 250000
UnGuardAccount = 250000
TrieLoadPerNode = 20000
TrieLoadPerNode = 100000
TrieStorePerNode = 50000

[MetaChainSystemSCsCost]
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/config/gasSchedules/gasScheduleV7.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
SetGuardian = 250000
GuardAccount = 250000
UnGuardAccount = 250000
TrieLoadPerNode = 20000
TrieLoadPerNode = 100000
TrieStorePerNode = 50000

[MetaChainSystemSCsCost]
Expand Down
9 changes: 5 additions & 4 deletions examples/construction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,21 @@ func TestConstructTransaction_WithGuardianFields(t *testing.T) {
GasLimit: 150000,
Data: []byte("test data field"),
ChainID: []byte("local-testnet"),
Version: 1,
Version: 2,
Options: 2,
}

tx.GuardianAddr = getPubkeyOfAddress(t, "erd1x23lzn8483xs2su4fak0r0dqx6w38enpmmqf2yrkylwq7mfnvyhsxqw57y")
tx.GuardianSignature = bytes.Repeat([]byte{0}, 64)

tx.Signature = computeTransactionSignature(t, alicePrivateKeyHex, tx)
require.Equal(t, "540ad16e46f379f9adcb7b26c07b16a56f10c624c489103679e488c0a0cb996c71dbc0d765cf925e58cd493d09d8c1d619946618ebd8a2fb924b236b8137c706", hex.EncodeToString(tx.Signature))
require.Equal(t, "e574d78b19e1481a6b9575c162e66f2f906a3178aec537509356385c4f1a5330a9b73a87a456fc6d7041e93b5f8a1231a92fb390174872a104a0929215600c0c", hex.EncodeToString(tx.Signature))

data, _ := contentMarshalizer.Marshal(tx)
require.Equal(t, "085c120e00018ee90ff6181f3761632000001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc0340f093094a0f746573742064617461206669656c64520d6c6f63616c2d746573746e657458016240540ad16e46f379f9adcb7b26c07b16a56f10c624c489103679e488c0a0cb996c71dbc0d765cf925e58cd493d09d8c1d619946618ebd8a2fb924b236b8137c706722032a3f14cf53c4d0543954f6cf1bda0369d13e661dec095107627dc0f6d33612f7a4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(data))
require.Equal(t, "085c120e00018ee90ff6181f3761632000001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc0340f093094a0f746573742064617461206669656c64520d6c6f63616c2d746573746e657458026240e574d78b19e1481a6b9575c162e66f2f906a3178aec537509356385c4f1a5330a9b73a87a456fc6d7041e93b5f8a1231a92fb390174872a104a0929215600c0c6802722032a3f14cf53c4d0543954f6cf1bda0369d13e661dec095107627dc0f6d33612f7a4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(data))

txHash := contentHasher.Compute(string(data))
require.Equal(t, "a5e63b5bf3b7eeb347cad1aa742770a29c7a88e59ac99cdc60dc612ebdc8a7d4", hex.EncodeToString(txHash))
require.Equal(t, "242022e9dcfa0ee1d8199b0043314dbda8601619f70069ebc441b9f03349a35c", hex.EncodeToString(txHash))
}

func TestConstructTransaction_WithNonceZero(t *testing.T) {
Expand Down
9 changes: 4 additions & 5 deletions state/accountsDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/multiversx/mx-chain-go/common/errChan"
"github.com/multiversx/mx-chain-go/common/holders"
"github.com/multiversx/mx-chain-go/state/iteratorChannelsProvider"
"github.com/multiversx/mx-chain-go/state/lastSnapshotMarker"
"github.com/multiversx/mx-chain-go/state/parsers"
"github.com/multiversx/mx-chain-go/state/stateMetrics"
"github.com/multiversx/mx-chain-go/trie/keyBuilder"
Expand All @@ -28,11 +29,8 @@ import (
)

const (
leavesChannelSize = 100
missingNodesChannelSize = 100
lastSnapshot = "lastSnapshot"
waitTimeForSnapshotEpochCheck = time.Millisecond * 100
snapshotWaitTimeout = time.Minute
leavesChannelSize = 100
missingNodesChannelSize = 100
)

type loadingMeasurements struct {
Expand Down Expand Up @@ -138,6 +136,7 @@ func NewAccountsDB(args ArgsAccountsDB) (*AccountsDB, error) {
StateMetrics: sm,
ChannelsProvider: iteratorChannelsProvider.NewUserStateIteratorChannelsProvider(),
AccountFactory: args.AccountFactory,
LastSnapshotMarker: lastSnapshotMarker.NewLastSnapshotMarker(),
}
snapshotManager, err := NewSnapshotsManager(argsSnapshotsManager)
if err != nil {
Expand Down
44 changes: 26 additions & 18 deletions state/accountsDB_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/multiversx/mx-chain-go/state"
"github.com/multiversx/mx-chain-go/state/accounts"
"github.com/multiversx/mx-chain-go/state/factory"
"github.com/multiversx/mx-chain-go/state/lastSnapshotMarker"
"github.com/multiversx/mx-chain-go/state/parsers"
"github.com/multiversx/mx-chain-go/state/storagePruningManager"
"github.com/multiversx/mx-chain-go/state/storagePruningManager/disabled"
Expand Down Expand Up @@ -1011,7 +1012,7 @@ func TestAccountsDB_SnapshotStateOnAClosedStorageManagerShouldNotMarkActiveDB(t
activeDBWasPut = true
}

if string(key) == state.LastSnapshotStarted {
if string(key) == lastSnapshotMarker.LastSnapshot {
lastSnapshotStartedWasPut = true
}

Expand All @@ -1026,7 +1027,7 @@ func TestAccountsDB_SnapshotStateOnAClosedStorageManagerShouldNotMarkActiveDB(t

mut.RLock()
defer mut.RUnlock()
assert.True(t, lastSnapshotStartedWasPut)
assert.False(t, lastSnapshotStartedWasPut)
assert.False(t, activeDBWasPut)
}

Expand Down Expand Up @@ -1059,17 +1060,20 @@ func TestAccountsDB_SnapshotStateWithErrorsShouldNotMarkActiveDB(t *testing.T) {
activeDBWasPut = true
}

if string(key) == state.LastSnapshotStarted {
if string(key) == lastSnapshotMarker.LastSnapshot {
lastSnapshotStartedWasPut = true
}

return nil
},
GetLatestStorageEpochCalled: func() (uint32, error) {
return 1, nil
},
}
},
}
adb := generateAccountDBFromTrie(trieStub)
adb.SnapshotState([]byte("roothash"), 0)
adb.SnapshotState([]byte("roothash"), 1)
time.Sleep(time.Second)

mut.RLock()
Expand Down Expand Up @@ -1106,14 +1110,14 @@ func TestAccountsDB_SnapshotStateSnapshotSameRootHash(t *testing.T) {

rootHash1 := []byte("rootHash1")
rootHash2 := []byte("rootHash2")
latestEpoch := uint32(0)
latestEpoch := atomic.Uint32{}
snapshotMutex := sync.RWMutex{}
takeSnapshotCalled := 0
trieStub := &trieMock.TrieStub{
GetStorageManagerCalled: func() common.StorageManager {
return &storageManager.StorageManagerStub{
GetLatestStorageEpochCalled: func() (uint32, error) {
return latestEpoch, nil
return latestEpoch.Get(), nil
},
TakeSnapshotCalled: func(_ string, _ []byte, _ []byte, iteratorChannels *common.TrieIteratorChannels, _ chan []byte, stats common.SnapshotStatisticsHandler, _ uint32) {
snapshotMutex.Lock()
Expand Down Expand Up @@ -1141,7 +1145,7 @@ func TestAccountsDB_SnapshotStateSnapshotSameRootHash(t *testing.T) {
snapshotMutex.Unlock()

// snapshot rootHash1 and epoch 1
latestEpoch = 1
latestEpoch.Set(1)
adb.SnapshotState(rootHash1, 1)
for adb.IsSnapshotInProgress() {
time.Sleep(waitForOpToFinish)
Expand All @@ -1151,7 +1155,7 @@ func TestAccountsDB_SnapshotStateSnapshotSameRootHash(t *testing.T) {
snapshotMutex.Unlock()

// snapshot rootHash1 and epoch 0 again
latestEpoch = 0
latestEpoch.Set(0)
adb.SnapshotState(rootHash1, 0)
for adb.IsSnapshotInProgress() {
time.Sleep(waitForOpToFinish)
Expand Down Expand Up @@ -1179,7 +1183,7 @@ func TestAccountsDB_SnapshotStateSnapshotSameRootHash(t *testing.T) {
snapshotMutex.Unlock()

// snapshot rootHash2 and epoch 1
latestEpoch = 1
latestEpoch.Set(1)
adb.SnapshotState(rootHash2, 1)
for adb.IsSnapshotInProgress() {
time.Sleep(waitForOpToFinish)
Expand All @@ -1189,7 +1193,7 @@ func TestAccountsDB_SnapshotStateSnapshotSameRootHash(t *testing.T) {
snapshotMutex.Unlock()

// snapshot rootHash2 and epoch 1 again
latestEpoch = 1
latestEpoch.Set(1)
adb.SnapshotState(rootHash2, 1)
for adb.IsSnapshotInProgress() {
time.Sleep(waitForOpToFinish)
Expand All @@ -1205,34 +1209,38 @@ func TestAccountsDB_SnapshotStateSkipSnapshotIfSnapshotInProgress(t *testing.T)
rootHashes := [][]byte{[]byte("rootHash1"), []byte("rootHash2"), []byte("rootHash3"), []byte("rootHash4")}
snapshotMutex := sync.RWMutex{}
takeSnapshotCalled := 0
numPutInEpochCalled := 0
numPutInEpochCalled := atomic.Counter{}

trieStub := &trieMock.TrieStub{
GetStorageManagerCalled: func() common.StorageManager {
return &storageManager.StorageManagerStub{
GetLatestStorageEpochCalled: func() (uint32, error) {
return 0, nil
return uint32(mathRand.Intn(5)), nil
},
TakeSnapshotCalled: func(_ string, _ []byte, _ []byte, iteratorChannels *common.TrieIteratorChannels, _ chan []byte, stats common.SnapshotStatisticsHandler, _ uint32) {
snapshotMutex.Lock()
takeSnapshotCalled++
close(iteratorChannels.LeavesChan)
stats.SnapshotFinished()
for numPutInEpochCalled.Get() != 4 {
time.Sleep(time.Millisecond * 10)
}
snapshotMutex.Unlock()
},
PutInEpochCalled: func(key []byte, val []byte, epoch uint32) error {
assert.Equal(t, []byte(state.LastSnapshotStarted), key)
assert.Equal(t, rootHashes[epoch], val)
assert.Equal(t, []byte(lastSnapshotMarker.LastSnapshot), key)
assert.Equal(t, rootHashes[epoch-1], val)

numPutInEpochCalled++
numPutInEpochCalled.Add(1)
return nil
},
}
},
}
adb := generateAccountDBFromTrie(trieStub)

for epoch, rootHash := range rootHashes {
for i, rootHash := range rootHashes {
epoch := i + 1
adb.SnapshotState(rootHash, uint32(epoch))
}
for adb.IsSnapshotInProgress() {
Expand All @@ -1242,7 +1250,7 @@ func TestAccountsDB_SnapshotStateSkipSnapshotIfSnapshotInProgress(t *testing.T)
snapshotMutex.Lock()
assert.Equal(t, 1, takeSnapshotCalled)
snapshotMutex.Unlock()
assert.Equal(t, len(rootHashes), numPutInEpochCalled)
assert.Equal(t, len(rootHashes), int(numPutInEpochCalled.Get()))
}

func TestAccountsDB_SnapshotStateCallsRemoveFromAllActiveEpochs(t *testing.T) {
Expand All @@ -1263,7 +1271,7 @@ func TestAccountsDB_SnapshotStateCallsRemoveFromAllActiveEpochs(t *testing.T) {
},
RemoveFromAllActiveEpochsCalled: func(hash []byte) error {
removeFromAllActiveEpochsCalled = true
assert.Equal(t, []byte(state.LastSnapshotStarted), hash)
assert.Equal(t, []byte(lastSnapshotMarker.LastSnapshot), hash)
return nil
},
}
Expand Down
3 changes: 3 additions & 0 deletions state/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,6 @@ var ErrNilStateMetrics = errors.New("nil sstate metrics")

// ErrNilChannelsProvider signals that a nil channels provider has been given
var ErrNilChannelsProvider = errors.New("nil channels provider")

// ErrNilLastSnapshotMarker signals that a nil last snapshot marker has been given
var ErrNilLastSnapshotMarker = errors.New("nil last snapshot marker")
21 changes: 0 additions & 21 deletions state/export_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package state

import (
"time"

"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/testscommon/storageManager"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
)

// LastSnapshotStarted -
const LastSnapshotStarted = lastSnapshot

// LoadCode -
func (adb *AccountsDB) LoadCode(accountHandler baseAccountHandler) error {
return adb.loadCode(accountHandler)
Expand Down Expand Up @@ -92,21 +86,6 @@ func (sm *snapshotsManager) GetLastSnapshotInfo() ([]byte, uint32) {
return sm.lastSnapshot.rootHash, sm.lastSnapshot.epoch
}

// GetStorageEpochChangeWaitArgs -
func GetStorageEpochChangeWaitArgs() storageEpochChangeWaitArgs {
return storageEpochChangeWaitArgs{
Epoch: 1,
WaitTimeForSnapshotEpochCheck: time.Millisecond * 100,
SnapshotWaitTimeout: time.Second,
TrieStorageManager: &storageManager.StorageManagerStub{},
}
}

// WaitForStorageEpochChange
func (sm *snapshotsManager) WaitForStorageEpochChange(args storageEpochChangeWaitArgs) error {
return sm.waitForStorageEpochChange(args)
}

// NewNilSnapshotsManager -
func NewNilSnapshotsManager() *snapshotsManager {
return nil
Expand Down
10 changes: 9 additions & 1 deletion state/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ type DataTrie interface {
}

// PeerAccountHandler models a peer state account, which can journalize a normal account's data
// with some extra features like signing statistics or rating information
// with some extra features like signing statistics or rating information
type PeerAccountHandler interface {
SetBLSPublicKey([]byte) error
GetRewardAddress() []byte
Expand Down Expand Up @@ -265,3 +265,11 @@ type SignRate interface {
GetNumSuccess() uint32
GetNumFailure() uint32
}

// LastSnapshotMarker manages the lastSnapshot marker operations
type LastSnapshotMarker interface {
AddMarker(trieStorageManager common.StorageManager, epoch uint32, rootHash []byte)
RemoveMarker(trieStorageManager common.StorageManager, epoch uint32, rootHash []byte)
GetMarkerInfo(trieStorageManager common.StorageManager) ([]byte, error)
IsInterfaceNil() bool
}

0 comments on commit 9d81e92

Please sign in to comment.