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

State changes outport #6429

Merged
merged 30 commits into from
Sep 18, 2024
Merged

Conversation

cristure
Copy link
Contributor

@cristure cristure commented Sep 2, 2024

Reasoning behind the pull request

  • State changes will be also exported through the host driver (and possible other methods)

Proposed changes

  • Add a new method to the Driver interface that will send the state changes collected.

Testing procedure

  • Will need to run a local-testnet along with an observer node that will have the configuration for exporting through host driver or other means.

Pre-requisites

Based on the Contributing Guidelines the PR author and the reviewers must check the following requirements are met:

  • was the PR targeted to the correct branch?
  • if this is a larger feature that probably needs more than one PR, is there a feat branch created?
  • if this is a feat branch merging, do all satellite projects have a proper tag inside go.mod?

@cristure cristure changed the base branch from state-changes-collector to state-changes-read-operations September 2, 2024 09:30
@cristure
Copy link
Contributor Author

cristure commented Sep 4, 2024

There are somethings that need to be addressed. Mainly about coupling stateChangesCollectore in the outportDriver. The other way around is not possible (there is a TODO left, implying the outportDriver should be injected as a dependency in the stateChangesCollector, but doing this will result in a import cycle and therefore the code won't compile. Therefore, I injected the stateChangesCollector in the outportDriver, but that will require some additional changes on the integration between these two.

@miiu96 miiu96 self-requested a review September 4, 2024 11:01
@cristure cristure force-pushed the state-changes-outport branch 3 times, most recently from e95122d to f492061 Compare September 9, 2024 08:17
@cristure cristure changed the base branch from state-changes-read-operations to feat/state-changes September 9, 2024 08:18
… into state-changes-outport

# Conflicts:
#	epochStart/metachain/systemSCs_test.go
#	go.mod
#	go.sum
#	process/transaction/shardProcess.go
#	state/accountsDB.go
#	state/accountsDBApi.go
#	state/interface.go
…nto state-changes-outport

# Conflicts:
#	epochStart/metachain/systemSCs_test.go
#	process/transaction/shardProcess.go
#	state/accountsDB.go
#	state/interface.go
#	state/stateChanges/writeCollector.go
@cristure cristure changed the base branch from feat/state-changes to state-changes-read-operations September 10, 2024 08:02
@cristure
Copy link
Contributor Author

Also moved some structs in proto definition files. PR can be seen here.

Moreover, the mx-chain-go-storage had to be updated with the subsequent change in the proto strucutres. PR here

outport/disabled/disabledOutport.go Outdated Show resolved Hide resolved
outport/factory/hostDriverFactory.go Outdated Show resolved Hide resolved
outport/factory/outportFactory.go Outdated Show resolved Hide resolved
outport/mock/driverStub.go Outdated Show resolved Hide resolved
outport/notifier/eventNotifier.go Outdated Show resolved Hide resolved
scripts/testnet/variables.sh Outdated Show resolved Hide resolved
state/stateChanges/dataAnalysisCollector.go Outdated Show resolved Hide resolved
state/stateChanges/writeCollector.go Show resolved Hide resolved
state/stateChanges/writeCollector.go Outdated Show resolved Hide resolved
@cristure cristure self-assigned this Sep 10, 2024
@AdoAdoAdo AdoAdoAdo self-requested a review September 12, 2024 08:38
go.mod Outdated
github.com/multiversx/mx-chain-crypto-go v1.2.12
github.com/multiversx/mx-chain-es-indexer-go v1.7.5-0.20240807095116-4f2f595e52d9
github.com/multiversx/mx-chain-es-indexer-go v1.4.19-0.20240129150813-a772c480d33a
Copy link
Contributor

Choose a reason for hiding this comment

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

is this indexer downgrade correct? @miiu96

@@ -134,6 +139,9 @@ func (odp *outportDataProvider) PrepareOutportSaveBlockData(arg ArgPrepareOutpor
return nil, err
}

stateChanges := odp.stateChangesCollector.GetStateChangesForTxs()
odp.stateChangesCollector.Reset()
Copy link
Contributor

Choose a reason for hiding this comment

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

I would move the reset on the commit block, after the indexing part.
Otherwise, in case there is no client for the outport block, you would not end up in this method and the reset would not be done.

baseNewAcc, newAccOk := newAcc.(baseAccountHandler)
baseOldAccount, _ := oldAcc.(baseAccountHandler)

if !newAccOk {
return nil, nil
return make([]*stateChange.DataTrieChange, 0), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this required because of a panic?

Copy link
Contributor Author

@cristure cristure Sep 12, 2024

Choose a reason for hiding this comment

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

Frankly, I don't remember. Reverted to the previous implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

@BeniaminDrasovean can you check if this change would be problematic?
do we need to change the test instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's ok both ways.

@@ -918,7 +918,8 @@ func (adb *AccountsDB) commit() ([]byte, error) {
return nil, err
}

adb.stateChangesCollector.Reset()
//TODO: discuss the workflow. If reset here, the outport driver won't be able to pick up the changes.
//adb.stateChangesCollector.Reset()
Copy link
Contributor

Choose a reason for hiding this comment

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

I would move the reset in the processor (shardProcessor & metaProcessor) in the CommitBlock, after calling sp.indexBlockIfNeeded(bodyHandler, headerHash, headerHandler, lastBlockHeader) on shardProcessor, and
after calling mp.indexBlock(header, headerHash, body, lastMetaBlock, notarizedHeadersHashes, rewardsTxs) on metaProcessor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved

Copy link
Contributor

Choose a reason for hiding this comment

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

don't forget to remove also the commented code after we agree on the workflow.

@@ -136,6 +103,47 @@ func (scc *stateChangesCollector) getStateChangesForTxs() ([]StateChangesForTx,
return stateChangesForTxs, nil
}

// GetStateChangesForTxs will retrieve the state changes linked with the tx hash.
func (scc *stateChangesCollector) GetStateChangesForTxs() map[string]*data.StateChanges {
scc.stateChangesMut.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a RLock would be sufficient, as you are just reading the state changes not modifying

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we are actually appending to the map in case the hash is already present. Can be seen at state/stateChanges/writeCollector.go:130

baseNewAcc, newAccOk := newAcc.(baseAccountHandler)
baseOldAccount, _ := oldAcc.(baseAccountHandler)

if !newAccOk {
return nil, nil
return make([]*stateChange.DataTrieChange, 0), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

@BeniaminDrasovean can you check if this change would be problematic?
do we need to change the test instead?

@@ -918,7 +918,8 @@ func (adb *AccountsDB) commit() ([]byte, error) {
return nil, err
}

adb.stateChangesCollector.Reset()
//TODO: discuss the workflow. If reset here, the outport driver won't be able to pick up the changes.
//adb.stateChangesCollector.Reset()
Copy link
Contributor

Choose a reason for hiding this comment

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

don't forget to remove also the commented code after we agree on the workflow.

},
}
} else {
stateChangesForTxs[txHash].StateChanges = append(sc.StateChanges,
Copy link
Contributor

Choose a reason for hiding this comment

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

here we should not append to the sc.StateChanges, but to the stateChangesForTxs[txHash].StateChanges
then we can have the RWMutex as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

can you also add some unit tests for GetStateChangesForTxs ?

Copy link
Contributor Author

@cristure cristure Sep 13, 2024

Choose a reason for hiding this comment

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

sc is assigned that exact value in the if statement above.
if sc, ok := stateChangesForTxs[txHash]; !ok {

Copy link
Contributor Author

@cristure cristure Sep 13, 2024

Choose a reason for hiding this comment

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

but it is very confusing as it is. Will use the same variable name for both.

@@ -7,3 +7,6 @@ var ErrHostIsClosed = errors.New("server is closed")

// ErrNilHost signals that a nil host has been provided
var ErrNilHost = errors.New("nil host provided")

// ErrNilStateChangesCollector signals that a nil state change collector has been provided.
var ErrNilStateChangesCollector = errors.New("nil state changes collector provided")
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not used anywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is used here: state/accountsDB.go:163

baseNewAcc, newAccOk := newAcc.(baseAccountHandler)
baseOldAccount, _ := oldAcc.(baseAccountHandler)

if !newAccOk {
return nil, nil
return make([]*stateChange.DataTrieChange, 0), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

It's ok both ways.

"github.com/multiversx/mx-chain-go/state/stateChanges"
)

type StateChangesCollectorStub struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing comment here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@@ -919,6 +920,9 @@ func TestMetaProcessor_CommitBlockStorageFailsForHeaderShouldNotReturnError(t *t
return &block.Header{}, []byte("hash"), nil
}
arguments.BlockTracker = blockTrackerMock
arguments.StateChangesCollector = &stateMock.StateChangesCollectorStub{
ResetCalled: func() {},
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that the resetCalled is empty, is this any different from the default implementation in StateChangesCollectorStub?

otherwise I think it can be removed here and just have
arguments.StateChangesCollector = &stateMock.StateChangesCollectorStub{}

same for line 1057

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

if check.IfNil(args.StateChangesCollector) {
return ErrNilStateChangesCollector
}
if check.IfNil(args.StateChangesCollector) { return ErrNilStateChangesCollector }
Copy link
Contributor

Choose a reason for hiding this comment

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

can you reformat this line? (the return on the next line, like in previous checks)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

@cristure cristure merged commit bfc6add into state-changes-read-operations Sep 18, 2024
3 checks passed
@cristure cristure deleted the state-changes-outport branch September 18, 2024 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants