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(txpool): calculate consumption when committing a new block #1554

Merged
merged 7 commits into from
Oct 20, 2024

Conversation

Ja7ad
Copy link
Contributor

@Ja7ad Ja7ad commented Oct 17, 2024

Description

Calculate consumption when committing a new block

Related issue(s)

@Ja7ad Ja7ad requested a review from b00f October 17, 2024 19:29
txpool/txpool.go Outdated Show resolved Hide resolved
txpool/txpool_test.go Show resolved Hide resolved
state/state.go Show resolved Hide resolved
@Ja7ad Ja7ad marked this pull request as ready for review October 19, 2024 07:08
state/state.go Outdated Show resolved Hide resolved
txpool/config.go Outdated Show resolved Hide resolved
txpool/config.go Outdated Show resolved Hide resolved
txpool/txpool.go Outdated Show resolved Hide resolved
txpool/txpool.go Outdated Show resolved Hide resolved
- put store reader before message channel
- add comment private for ConsumptionBlocks
- move ConsumptionBlocks to down of struct config
- move handle committed block after write batch
Comment on lines +158 to +160

p.consumptionMap[signer] += uint32(trx.SerializeSize())
}
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 it's better to have atomic increase to prevent race conditions on consumptionMap

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the consumptionMap map[crypto.Address]uint32, atomic operations are typically not necessary unless multiple goroutines concurrently read and modify the same uint32 values in the map. A map in Go is not thread-safe, so accessing or modifying it concurrently requires synchronization (e.g., using sync.Mutex).

However, if the map is safely guarded by a lock or accessed in a single-threaded context, atomic operations for the values aren't required.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean, we can't guarantee scope of use of consumptionMap in long term.
Although we can guarantee that handleIncreaseConsumption is locked but it is likely that another function or goroutine accesses consumptionMap in different scope using different lock, which is not safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

handleIncreaseConsumption and handleDecreaseConsumption have parent lock from HandleCommittedBlock.

In HandleCommittedBlock we don't have concurrent for increase and decrease.

@b00f b00f merged commit 370d882 into pactus-project:main Oct 20, 2024
10 checks passed
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.

Calculate consumption when committing a new block
4 participants