-
Notifications
You must be signed in to change notification settings - Fork 872
Background Transaction Generation #3021
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
086caeb
Show time spent per block.
1386543
Handle block creation on background goroutine.
8ecfba5
added missing files
6fc55ba
Merge branch 'main' into cody-littley/time-per-block
8938c6d
adjust executor queue size
a7d0c54
lint
dc531f4
Merge branch 'main' into cody-littley/time-per-block
8674330
made suggested changes
fc4ac1f
Merge branch 'main' into cody-littley/time-per-block
cody-littley 6605990
Merge branch 'main' into cody-littley/time-per-block
cody-littley e13369a
Merge branch 'main' into cody-littley/time-per-block
cody-littley a6d3126
Merge branch 'main' into cody-littley/time-per-block
cody-littley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package cryptosim | ||
|
|
||
| import "iter" | ||
|
|
||
| // A simulated block of transactions. | ||
| type block struct { | ||
| config *CryptoSimConfig | ||
|
|
||
| // The transactions in the block. | ||
| transactions []*transaction | ||
|
|
||
| // The block number. This is not currently preserved across benchmark restarts, but otherwise monotonically | ||
| // increases as you'd expect. | ||
| blockNumber int64 | ||
|
|
||
| // The next account ID to be used when creating a new account, as of the end of this block. | ||
| nextAccountID int64 | ||
|
|
||
| // The number of cold accounts, as of the end of this block. | ||
| numberOfColdAccounts int64 | ||
|
|
||
| // The next ERC20 contract ID to be used when creating a new ERC20 contract, as of the end of this block. | ||
| nextErc20ContractID int64 | ||
|
|
||
| metrics *CryptosimMetrics | ||
| } | ||
|
|
||
| // Creates a new block with the given capacity. | ||
| func NewBlock( | ||
| config *CryptoSimConfig, | ||
| metrics *CryptosimMetrics, | ||
| blockNumber int64, | ||
| capacity int, | ||
| ) *block { | ||
| return &block{ | ||
| config: config, | ||
| blockNumber: blockNumber, | ||
| transactions: make([]*transaction, 0, capacity), | ||
| metrics: metrics, | ||
| } | ||
| } | ||
|
|
||
| // Returns an iterator over the transactions in the block. | ||
| func (b *block) Iterator() iter.Seq[*transaction] { | ||
| return func(yield func(*transaction) bool) { | ||
| for _, txn := range b.transactions { | ||
| if !yield(txn) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Adds a transaction to the block. | ||
| func (b *block) AddTransaction(txn *transaction) { | ||
| b.transactions = append(b.transactions, txn) | ||
| } | ||
|
|
||
| // Returns the block number. | ||
| func (b *block) BlockNumber() int64 { | ||
| return b.blockNumber | ||
| } | ||
|
|
||
| // Sets information about account state as of the end of this block. | ||
| func (b *block) SetBlockAccountStats( | ||
| nextAccountID int64, | ||
| numberOfColdAccounts int64, | ||
| nextErc20ContractID int64, | ||
| ) { | ||
| b.nextAccountID = nextAccountID | ||
| b.numberOfColdAccounts = numberOfColdAccounts | ||
| b.nextErc20ContractID = nextErc20ContractID | ||
| } | ||
|
|
||
| // This method should be called after a block is finished executing and finalized. | ||
| // Reports metrics about the block. | ||
| func (b *block) ReportBlockMetrics() { | ||
| b.metrics.SetTotalNumberOfAccounts(b.nextAccountID, int64(b.config.NumberOfHotAccounts), b.numberOfColdAccounts) | ||
| } | ||
|
|
||
| // Returns the next account ID to be used when creating a new account, as of the end of this block. | ||
| func (b *block) NextAccountID() int64 { | ||
| return b.nextAccountID | ||
| } | ||
|
|
||
| // Returns the next ERC20 contract ID to be used when creating a new ERC20 contract, as of the end of this block. | ||
| func (b *block) NextErc20ContractID() int64 { | ||
| return b.nextErc20ContractID | ||
| } | ||
|
|
||
| // Returns the number of transactions in the block. | ||
| func (b *block) TransactionCount() int64 { | ||
| return int64(len(b.transactions)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package cryptosim | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // A builder for blocks of transactions. | ||
| type blockBuilder struct { | ||
| ctx context.Context | ||
|
|
||
| config *CryptoSimConfig | ||
|
|
||
| // Metrics for the benchmark. | ||
| metrics *CryptosimMetrics | ||
|
|
||
| // Produces random data. | ||
| dataGenerator *DataGenerator | ||
|
|
||
| // Blocks are sent to this channel. | ||
| blocksChan chan *block | ||
|
|
||
| // The next block number to be used. | ||
| nextBlockNumber int64 | ||
| } | ||
|
|
||
| // Asyncronously produces blocks of transactions. | ||
| func NewBlockBuilder( | ||
| ctx context.Context, | ||
| config *CryptoSimConfig, | ||
| metrics *CryptosimMetrics, | ||
| dataGenerator *DataGenerator, | ||
| ) *blockBuilder { | ||
| return &blockBuilder{ | ||
| ctx: ctx, | ||
| config: config, | ||
| metrics: metrics, | ||
| dataGenerator: dataGenerator, | ||
| blocksChan: make(chan *block, config.BlockChannelCapacity), | ||
| } | ||
| } | ||
|
|
||
| // Starts the block builder. This should not be called until all other threads are done using the data generator, | ||
| // as the data generator is not thread-safe. | ||
| func (b *blockBuilder) Start() { | ||
| go b.mainLoop() | ||
|
||
| } | ||
|
|
||
| // Builds blocks and sends them to the blocks channel. | ||
| func (b *blockBuilder) mainLoop() { | ||
| for { | ||
| block := b.buildBlock() | ||
| select { | ||
| case <-b.ctx.Done(): | ||
| return | ||
| case b.blocksChan <- block: | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (b *blockBuilder) buildBlock() *block { | ||
| blk := NewBlock(b.config, b.metrics, b.nextBlockNumber, b.config.TransactionsPerBlock) | ||
| b.nextBlockNumber++ | ||
|
|
||
| for i := 0; i < b.config.TransactionsPerBlock; i++ { | ||
| txn, err := BuildTransaction(b.dataGenerator) | ||
| if err != nil { | ||
| fmt.Printf("failed to build transaction: %v\n", err) | ||
| continue | ||
| } | ||
| blk.AddTransaction(txn) | ||
| } | ||
|
|
||
| blk.SetBlockAccountStats( | ||
| b.dataGenerator.NextAccountID(), | ||
| b.dataGenerator.NumberOfColdAccounts(), | ||
| b.dataGenerator.NextErc20ContractID()) | ||
|
|
||
| b.dataGenerator.ReportEndOfBlock() | ||
|
|
||
| return blk | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this function get called?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to avoid direct access to the
block.transactionsmember variable, but forgot to actually use it. It is now used.