From 99d2281841869780f28e39aaa7a90bfe812fe02e Mon Sep 17 00:00:00 2001 From: esuwu Date: Tue, 24 Dec 2024 12:02:22 +0300 Subject: [PATCH 1/3] Pulled the contract updates from snapshots, removed by block filtering --- pkg/blockchaininfo/blockchaininfo_test.go | 6 ++--- pkg/blockchaininfo/nats_publisher.go | 1 - pkg/blockchaininfo/types.go | 18 +++------------ pkg/state/appender.go | 28 ++++++++++++++++------- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/pkg/blockchaininfo/blockchaininfo_test.go b/pkg/blockchaininfo/blockchaininfo_test.go index c88fd7e3a..a74af8151 100644 --- a/pkg/blockchaininfo/blockchaininfo_test.go +++ b/pkg/blockchaininfo/blockchaininfo_test.go @@ -104,7 +104,7 @@ func TestChangesGenerationNewEntries(t *testing.T) { } equal, changes, err := blockchaininfo.CompareBUpdatesInfo(currentBlockInfo, previousBlockInfo, - proto.TestNetScheme, 10) + proto.TestNetScheme) if err != nil { return } @@ -183,7 +183,7 @@ func TestChangesGenerationContainsPrevious(t *testing.T) { } equal, changes, err := blockchaininfo.CompareBUpdatesInfo(currentBlockInfo, previousBlockInfo, - proto.TestNetScheme, 10) + proto.TestNetScheme) if err != nil { return } @@ -245,7 +245,7 @@ func TestNoChangesGeneration(t *testing.T) { } equal, changes, err := blockchaininfo.CompareBUpdatesInfo(currentBlockInfo, previousBlockInfo, - proto.TestNetScheme, 10) + proto.TestNetScheme) if err != nil { return } diff --git a/pkg/blockchaininfo/nats_publisher.go b/pkg/blockchaininfo/nats_publisher.go index 701dc6ce8..edf36db03 100644 --- a/pkg/blockchaininfo/nats_publisher.go +++ b/pkg/blockchaininfo/nats_publisher.go @@ -159,7 +159,6 @@ func handleBlockchainUpdate(updates BUpdatesInfo, bu *BUpdatesExtensionState, sc bu.currentState = &updates if bu.previousState == nil { // publish initial updates - filteredDataEntries, err := filterDataEntries(updates.BlockUpdatesInfo.Height-bu.Limit, updates.ContractUpdatesInfo.AllDataEntries) if err != nil { diff --git a/pkg/blockchaininfo/types.go b/pkg/blockchaininfo/types.go index 1568a6990..c6e2b92be 100644 --- a/pkg/blockchaininfo/types.go +++ b/pkg/blockchaininfo/types.go @@ -28,14 +28,13 @@ type BUpdatesInfo struct { // TODO wrap errors. func CompareBUpdatesInfo(current, previous BUpdatesInfo, - scheme proto.Scheme, heightLimit uint64) (bool, BUpdatesInfo, error) { + scheme proto.Scheme) (bool, BUpdatesInfo, error) { changes := BUpdatesInfo{ BlockUpdatesInfo: BlockUpdatesInfo{}, ContractUpdatesInfo: L2ContractDataEntries{}, } equal := true - // todo REMOVE POINTERS if current.BlockUpdatesInfo.Height != previous.BlockUpdatesInfo.Height { equal = false changes.BlockUpdatesInfo.Height = current.BlockUpdatesInfo.Height @@ -58,22 +57,11 @@ func CompareBUpdatesInfo(current, previous BUpdatesInfo, changes.BlockUpdatesInfo.BlockHeader = current.BlockUpdatesInfo.BlockHeader } - previousFilteredDataEntries, err := filterDataEntries(previous.BlockUpdatesInfo.Height-heightLimit, + equalEntries, dataEntryChanges, err := compareDataEntries(current.ContractUpdatesInfo.AllDataEntries, previous.ContractUpdatesInfo.AllDataEntries) if err != nil { return false, BUpdatesInfo{}, err } - currentFilteredDataEntries, err := filterDataEntries(current.BlockUpdatesInfo.Height-heightLimit, - current.ContractUpdatesInfo.AllDataEntries) - if err != nil { - return false, BUpdatesInfo{}, err - } - - equalEntries, dataEntryChanges, err := compareDataEntries(currentFilteredDataEntries, - previousFilteredDataEntries) - if err != nil { - return false, BUpdatesInfo{}, err - } if !equalEntries { equal = false changes.ContractUpdatesInfo.AllDataEntries = dataEntryChanges @@ -152,5 +140,5 @@ func compareDataEntries(current, previous proto.DataEntries) (bool, []proto.Data } func statesEqual(state BUpdatesExtensionState, scheme proto.Scheme) (bool, BUpdatesInfo, error) { - return CompareBUpdatesInfo(*state.currentState, *state.previousState, scheme, state.Limit) + return CompareBUpdatesInfo(*state.currentState, *state.previousState, scheme) } diff --git a/pkg/state/appender.go b/pkg/state/appender.go index 2feff4751..c219584d9 100644 --- a/pkg/state/appender.go +++ b/pkg/state/appender.go @@ -848,7 +848,7 @@ func (a *txAppender) appendBlock(params *appendBlockParams) error { // blockSnapshot.TxSnapshots. - updtErr := a.updateBlockchainUpdateInfo(blockInfo, params.block) + updtErr := a.updateBlockchainUpdateInfo(blockInfo, params.block, blockSnapshot) if updtErr != nil { return updtErr } @@ -875,12 +875,8 @@ func (a *txAppender) appendBlock(params *appendBlockParams) error { return a.blockDiffer.saveCurFeeDistr(params.block) } -func (a *txAppender) updateBlockchainUpdateInfo(blockInfo *proto.BlockInfo, blockHeader *proto.BlockHeader) error { - // TODO improve this by using diffs instead grabbing all the records every time - dataEntries, err := a.ia.state.RetrieveEntries(proto.NewRecipientFromAddress(a.bUpdatesExtension.L2ContractAddress())) - if err != nil && !errors.Is(err, proto.ErrNotFound) { - return err - } +func (a *txAppender) updateBlockchainUpdateInfo(blockInfo *proto.BlockInfo, blockHeader *proto.BlockHeader, + blockSnapshot proto.BlockSnapshot) error { blockID := blockHeader.BlockID() bUpdatesInfo := blockchaininfo.BUpdatesInfo{ BlockUpdatesInfo: blockchaininfo.BlockUpdatesInfo{ @@ -890,10 +886,26 @@ func (a *txAppender) updateBlockchainUpdateInfo(blockInfo *proto.BlockInfo, bloc BlockHeader: *blockHeader, }, ContractUpdatesInfo: blockchaininfo.L2ContractDataEntries{ - AllDataEntries: dataEntries, + AllDataEntries: nil, Height: blockInfo.Height, }, } + // Write the L2 contract updates into the structure. + l2ContractCount := 0 + for _, txSnapshots := range blockSnapshot.TxSnapshots { + for _, snapshot := range txSnapshots { + if dataEntriesSnapshot, ok := snapshot.(*proto.DataEntriesSnapshot); ok { + if dataEntriesSnapshot.Address == a.bUpdatesExtension.L2ContractAddress() { + l2ContractCount++ + bUpdatesInfo.ContractUpdatesInfo.AllDataEntries = dataEntriesSnapshot.DataEntries + } + } + } + } + if l2ContractCount > 0 { + return errors.Errorf("unexpected multiple data entries snapshots for l2 contract, %d", l2ContractCount) + } + a.bUpdatesExtension.WriteBUpdates(bUpdatesInfo) return nil } From 2e2d3d82064d2ad1f311289e5c0cdc93daa6f655 Mon Sep 17 00:00:00 2001 From: esuwu Date: Thu, 26 Dec 2024 12:00:39 +0300 Subject: [PATCH 2/3] Fixed a wrong check --- pkg/state/appender.go | 6 +----- pkg/state/state.go | 18 +++++++++--------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/pkg/state/appender.go b/pkg/state/appender.go index c219584d9..351f82deb 100644 --- a/pkg/state/appender.go +++ b/pkg/state/appender.go @@ -897,15 +897,11 @@ func (a *txAppender) updateBlockchainUpdateInfo(blockInfo *proto.BlockInfo, bloc if dataEntriesSnapshot, ok := snapshot.(*proto.DataEntriesSnapshot); ok { if dataEntriesSnapshot.Address == a.bUpdatesExtension.L2ContractAddress() { l2ContractCount++ - bUpdatesInfo.ContractUpdatesInfo.AllDataEntries = dataEntriesSnapshot.DataEntries + bUpdatesInfo.ContractUpdatesInfo.AllDataEntries = append(bUpdatesInfo.ContractUpdatesInfo.AllDataEntries, dataEntriesSnapshot.DataEntries...) } } } } - if l2ContractCount > 0 { - return errors.Errorf("unexpected multiple data entries snapshots for l2 contract, %d", l2ContractCount) - } - a.bUpdatesExtension.WriteBUpdates(bUpdatesInfo) return nil } diff --git a/pkg/state/state.go b/pkg/state/state.go index a65273f7f..7bb6989f5 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -229,15 +229,15 @@ func (s *blockchainEntitiesStorage) flush() error { var ErrIncompatibleStateParams = stderrs.New("incompatible state params") func checkCompatibility(stateDB *stateDB, params StateParams) error { - version, err := stateDB.stateVersion() - if err != nil { - return errors.Wrap(err, "stateVersion") - } - if version != StateVersion { - return errors.Wrapf(ErrIncompatibleStateParams, "incompatible storage version: state has value (%d), want (%d)", - version, StateVersion, - ) - } + //version, err := stateDB.stateVersion() + //if err != nil { + // return errors.Wrap(err, "stateVersion") + //} + //if version != StateVersion { + // return errors.Wrapf(ErrIncompatibleStateParams, "incompatible storage version: state has value (%d), want (%d)", + // version, StateVersion, + // ) + //} hasDataForExtendedApi, err := stateDB.stateStoresApiData() if err != nil { return errors.Wrap(err, "stateStoresApiData") From 8aeff696e27afec39e61dbe8b96c211af66f1567 Mon Sep 17 00:00:00 2001 From: esuwu Date: Thu, 26 Dec 2024 12:14:55 +0300 Subject: [PATCH 3/3] Removed temporary fix --- pkg/state/appender.go | 13 ++++--------- pkg/state/state.go | 18 +++++++++--------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/pkg/state/appender.go b/pkg/state/appender.go index 351f82deb..c1cfa4e98 100644 --- a/pkg/state/appender.go +++ b/pkg/state/appender.go @@ -846,12 +846,7 @@ func (a *txAppender) appendBlock(params *appendBlockParams) error { if a.bUpdatesExtension != nil && a.bUpdatesExtension.EnableBlockchainUpdatesPlugin() { // TODO get info from block snapshot? - // blockSnapshot.TxSnapshots. - - updtErr := a.updateBlockchainUpdateInfo(blockInfo, params.block, blockSnapshot) - if updtErr != nil { - return updtErr - } + a.updateBlockchainUpdateInfo(blockInfo, params.block, blockSnapshot) } // check whether the calculated snapshot state hash equals with the provided one @@ -876,7 +871,7 @@ func (a *txAppender) appendBlock(params *appendBlockParams) error { } func (a *txAppender) updateBlockchainUpdateInfo(blockInfo *proto.BlockInfo, blockHeader *proto.BlockHeader, - blockSnapshot proto.BlockSnapshot) error { + blockSnapshot proto.BlockSnapshot) { blockID := blockHeader.BlockID() bUpdatesInfo := blockchaininfo.BUpdatesInfo{ BlockUpdatesInfo: blockchaininfo.BlockUpdatesInfo{ @@ -897,13 +892,13 @@ func (a *txAppender) updateBlockchainUpdateInfo(blockInfo *proto.BlockInfo, bloc if dataEntriesSnapshot, ok := snapshot.(*proto.DataEntriesSnapshot); ok { if dataEntriesSnapshot.Address == a.bUpdatesExtension.L2ContractAddress() { l2ContractCount++ - bUpdatesInfo.ContractUpdatesInfo.AllDataEntries = append(bUpdatesInfo.ContractUpdatesInfo.AllDataEntries, dataEntriesSnapshot.DataEntries...) + bUpdatesInfo.ContractUpdatesInfo.AllDataEntries = append(bUpdatesInfo.ContractUpdatesInfo.AllDataEntries, + dataEntriesSnapshot.DataEntries...) } } } } a.bUpdatesExtension.WriteBUpdates(bUpdatesInfo) - return nil } func (a *txAppender) createCheckerInfo(params *appendBlockParams) (*checkerInfo, error) { diff --git a/pkg/state/state.go b/pkg/state/state.go index 7bb6989f5..a65273f7f 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -229,15 +229,15 @@ func (s *blockchainEntitiesStorage) flush() error { var ErrIncompatibleStateParams = stderrs.New("incompatible state params") func checkCompatibility(stateDB *stateDB, params StateParams) error { - //version, err := stateDB.stateVersion() - //if err != nil { - // return errors.Wrap(err, "stateVersion") - //} - //if version != StateVersion { - // return errors.Wrapf(ErrIncompatibleStateParams, "incompatible storage version: state has value (%d), want (%d)", - // version, StateVersion, - // ) - //} + version, err := stateDB.stateVersion() + if err != nil { + return errors.Wrap(err, "stateVersion") + } + if version != StateVersion { + return errors.Wrapf(ErrIncompatibleStateParams, "incompatible storage version: state has value (%d), want (%d)", + version, StateVersion, + ) + } hasDataForExtendedApi, err := stateDB.stateStoresApiData() if err != nil { return errors.Wrap(err, "stateStoresApiData")