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

feat(block-producer): multiple inflight account txs #407

Conversation

Mirko-von-Leipzig
Copy link
Contributor

@Mirko-von-Leipzig Mirko-von-Leipzig commented Jul 12, 2024

Support multiple inflight transactions affecting the same account by tracking the latest inflight state of modified accounts. This is then checked against new txns.

This is likely more a progress check than a final PR - I suspect there are more indirect changes that must be made?

Probably missing: tests ensuring that secondary inflight transactions are rejected if they have the wrong initial state.

Closes #186

@Mirko-von-Leipzig Mirko-von-Leipzig marked this pull request as ready for review July 12, 2024 14:06
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Thank you! Looks good! I left a few comments inline.

We also need to make sure that the processes of combining transactions into a batch and combining batches into a block work correctly (e.g., see here and here).

crates/block-producer/src/state_view/mod.rs Outdated Show resolved Hide resolved
crates/block-producer/src/state_view/mod.rs Outdated Show resolved Hide resolved
crates/block-producer/src/state_view/mod.rs Outdated Show resolved Hide resolved
@Mirko-von-Leipzig
Copy link
Contributor Author

I've added the account state tracker; there are still some tests that need updating/fixing.

I'll draft this PR for now.

@Mirko-von-Leipzig Mirko-von-Leipzig marked this pull request as draft July 15, 2024 16:33
@Mirko-von-Leipzig Mirko-von-Leipzig force-pushed the allow-multiple-inflight-txns branch from 2944b92 to 2f8d09c Compare July 15, 2024 16:36
@Mirko-von-Leipzig
Copy link
Contributor Author

I've made the suggested changes, and also added support for multiple transactions in the transaction batcher (missing the detail merging as mentioned here).

I looked into any required BlockWitness changes, but I think those might already be covered by my batcher changes which didn't change the API? Might be wrong - still wrapping my head around things.

@Mirko-von-Leipzig Mirko-von-Leipzig marked this pull request as ready for review July 16, 2024 10:39
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left some more comments inline.

I looked into any required BlockWitness changes, but I think those might already be covered by my batcher changes which didn't change the API?

I think the biggest thing we need to make sure is happening is when we have 2 batches with updates to the same account, we'd need to merge their BlockAccountUpdate's. If this isn't happening already, this could be a fairly involved change, and I'm thinking we can create a separate issue for this and tackle it in a future PR.

crates/block-producer/src/state_view/tests/apply_block.rs Outdated Show resolved Hide resolved
crates/block-producer/src/state_view/account_state.rs Outdated Show resolved Hide resolved
crates/block-producer/src/errors.rs Outdated Show resolved Hide resolved
crates/block-producer/src/batch_builder/batch.rs Outdated Show resolved Hide resolved
crates/block-producer/src/batch_builder/batch.rs Outdated Show resolved Hide resolved
crates/block-producer/src/batch_builder/batch.rs Outdated Show resolved Hide resolved
crates/block-producer/src/batch_builder/batch.rs Outdated Show resolved Hide resolved
crates/block-producer/src/state_view/mod.rs Outdated Show resolved Hide resolved
@Mirko-von-Leipzig
Copy link
Contributor Author

Mirko-von-Leipzig commented Jul 25, 2024

I think this PR could do with another review :)

I also took a stab at merge support in the block witness in the last commit. I think this is done correctly, though of course there are no tests. I'll add those tomorrow -- just wanted input on my implementation first? No point in testing the wrong assumption :D

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left just a couple of small comments inline.

Also agreed - would be good to add some tests.

crates/block-producer/src/errors.rs Outdated Show resolved Hide resolved
@Mirko-von-Leipzig Mirko-von-Leipzig force-pushed the allow-multiple-inflight-txns branch from b88d06f to c8a2890 Compare July 30, 2024 14:10
@Mirko-von-Leipzig
Copy link
Contributor Author

Mirko-von-Leipzig commented Jul 30, 2024

Block witness and producer have been updated to support multiple txs as well. I implemented this in the least intrusive way I could find; however the result is a bit ugly imo.

The witness now outputs the block deltas since it is responsible for merging batches. My initial attempts at moving this out met with resistance from the "code shape" being wrong in several locations. A better flow can probably be found by taking a higher level view but that's a large diff left best left for another PR.

I'm leaving the merge conflict until the changes are accepted so that the reviews don't start from a completely fresh rebase.

Comment on lines +38 to +42
) -> Result<(Self, Vec<BlockAccountUpdate>), BuildBlockError> {
if batches.len() > MAX_BATCHES_PER_BLOCK {
return Err(BuildBlockError::TooManyBatchesInBlock(batches.len()));
}
Self::validate_nullifiers(&block_inputs, batches)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The validate function became a bit of a liability - it can no longer be performed at the start but instead needs to be done in parts since account state can only be checked after we've merged the batch updates.

})
.collect()
};
) -> Result<(Self, Vec<BlockAccountUpdate>), BuildBlockError> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I dislike the return type, but this was the smallest delta I could find.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left just a few small comments inline.

Overall though, I wonder if we should simplify the whole approach somehow. I think things are relatively clean at the batch level, but when we get to the block level, some of the inter-dependencies we need to check worry me a bit.

A good time to think through and potentially refactor this logic would be when we migrate batch/block construction logic to miden-base. Basically, I think miden-base should contain the following logic:

  1. Given a list of proven transactions and some additional data (e.g., inclusion paths for unauthenticated note), output a new TransactionBatch.
  2. Given a list of transaction batches and some additional data (i.e., what is now called BlockInputs), output a new Block.

On my TODO list is to write up issues about these.


Separately, cc @igamigo - we should probably add integration tests in the client to try to test multiple in-flight transactions for the same account.

Support multiple inflight transactions affecting the same account by
tracking the latest inflight state of modified accounts. This is then
checked against new txns.
Previous attempt only tracked the latest state. This didn't
allow for only some of the inflight transactions being published
in a block.
This is now legal behaviour.
They ran into our new batch state assertion
Test was setup to use the incorrect state which is now actually checked.
@Mirko-von-Leipzig Mirko-von-Leipzig force-pushed the allow-multiple-inflight-txns branch from d6d5f89 to 165a3ac Compare July 31, 2024 09:19
@Mirko-von-Leipzig
Copy link
Contributor Author

Mirko-von-Leipzig commented Jul 31, 2024

Overall though, I wonder if we should simplify the whole approach somehow. I think things are relatively clean at the batch level, but when we get to the block level, some of the inter-dependencies we need to check worry me a bit.

A good time to think through and potentially refactor this logic would be when we migrate batch/block construction logic to miden-base. Basically, I think miden-base should contain the following logic:

1. Given a list of proven transactions and some additional data (e.g., inclusion paths for unauthenticated note), output a new `TransactionBatch`.

2. Given a list of transaction batches and some additional data (i.e., what is now called `BlockInputs`), output a new `Block`.

Definitely agree this could be simplified. I like approaching this from the testing point of view. If its difficult to fully test all the variations of the function/type then its probably doing too many things.

If we can pull the store access out then creating tests also becomes easier.

let mut block = BlockBuilder::new();

for (id, batch) in batches.enumerate() {
    block.merge(batch).context("Merging batch {id})?;
}

let store_state = self.store.get_inputs(block.account_list())?;

let block = block
    .create_witness(store_state)?
    .finalize();

@Mirko-von-Leipzig Mirko-von-Leipzig merged commit ec9a4ae into 0xPolygonMiden:next Jul 31, 2024
9 checks passed
@Mirko-von-Leipzig Mirko-von-Leipzig deleted the allow-multiple-inflight-txns branch July 31, 2024 09:33
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.

2 participants