-
Notifications
You must be signed in to change notification settings - Fork 10
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(ARCO-299): merge register and request transactions in blocktx #687
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 | ||||
---|---|---|---|---|---|---|
|
@@ -26,20 +26,19 @@ import ( | |||||
) | ||||||
|
||||||
var ( | ||||||
ErrFailedToSubscribeToTopic = errors.New("failed to subscribe to register topic") | ||||||
ErrFailedToSubscribeToTopic = errors.New("failed to subscribe to request-tx topic") | ||||||
ErrFailedToCreateBUMP = errors.New("failed to create new bump for tx hash from merkle tree and index") | ||||||
ErrFailedToGetStringFromBUMPHex = errors.New("failed to get string from bump for tx hash") | ||||||
ErrFailedToInsertBlockTransactions = errors.New("failed to insert block transactions") | ||||||
ErrFailedToPublishMinedTransaction = errors.New("failed to publish mined transactions") | ||||||
) | ||||||
|
||||||
const ( | ||||||
transactionStoringBatchsizeDefault = 8192 // power of 2 for easier memory allocation | ||||||
maxRequestBlocks = 10 | ||||||
maxBlocksInProgress = 1 | ||||||
registerTxsIntervalDefault = time.Second * 10 | ||||||
registerRequestTxsIntervalDefault = time.Second * 5 | ||||||
registerTxsBatchSizeDefault = 100 | ||||||
registerRequestTxBatchSizeDefault = 100 | ||||||
requestTxsIntervalDefault = time.Second * 5 | ||||||
requestTxBatchSizeDefault = 100 | ||||||
waitForBlockProcessing = 5 * time.Minute | ||||||
) | ||||||
|
||||||
|
@@ -52,12 +51,9 @@ type Processor struct { | |||||
transactionStorageBatchSize int | ||||||
dataRetentionDays int | ||||||
mqClient MessageQueueClient | ||||||
registerTxsChan chan []byte | ||||||
requestTxChannel chan []byte | ||||||
registerTxsInterval time.Duration | ||||||
registerRequestTxsInterval time.Duration | ||||||
registerTxsBatchSize int | ||||||
registerRequestTxsBatchSize int | ||||||
requestTxsInterval time.Duration | ||||||
requestTxsBatchSize int | ||||||
tracingEnabled bool | ||||||
tracingAttributes []attribute.KeyValue | ||||||
processGuardsMap sync.Map | ||||||
|
@@ -89,10 +85,8 @@ func NewProcessor( | |||||
blockRequestCh: blockRequestCh, | ||||||
blockProcessCh: blockProcessCh, | ||||||
transactionStorageBatchSize: transactionStoringBatchsizeDefault, | ||||||
registerTxsInterval: registerTxsIntervalDefault, | ||||||
registerRequestTxsInterval: registerRequestTxsIntervalDefault, | ||||||
registerTxsBatchSize: registerTxsBatchSizeDefault, | ||||||
registerRequestTxsBatchSize: registerRequestTxBatchSizeDefault, | ||||||
requestTxsInterval: requestTxsIntervalDefault, | ||||||
requestTxsBatchSize: requestTxBatchSizeDefault, | ||||||
hostname: hostname, | ||||||
stats: newProcessorStats(), | ||||||
statCollectionInterval: statCollectionIntervalDefault, | ||||||
|
@@ -112,15 +106,7 @@ func NewProcessor( | |||||
} | ||||||
|
||||||
func (p *Processor) Start() error { | ||||||
err := p.mqClient.Subscribe(RegisterTxTopic, func(msg []byte) error { | ||||||
p.registerTxsChan <- msg | ||||||
return nil | ||||||
}) | ||||||
if err != nil { | ||||||
return errors.Join(ErrFailedToSubscribeToTopic, fmt.Errorf("topic: %s", RegisterTxTopic), err) | ||||||
} | ||||||
|
||||||
err = p.mqClient.Subscribe(RequestTxTopic, func(msg []byte) error { | ||||||
err := p.mqClient.Subscribe(RequestTxTopic, func(msg []byte) error { | ||||||
p.requestTxChannel <- msg | ||||||
return nil | ||||||
}) | ||||||
|
@@ -130,7 +116,6 @@ func (p *Processor) Start() error { | |||||
|
||||||
p.StartBlockRequesting() | ||||||
p.StartBlockProcessing() | ||||||
p.StartProcessRegisterTxs() | ||||||
p.StartProcessRequestTxs() | ||||||
|
||||||
return nil | ||||||
|
@@ -287,47 +272,12 @@ func (p *Processor) unlockBlock(ctx context.Context, hash *chainhash.Hash) { | |||||
} | ||||||
} | ||||||
|
||||||
func (p *Processor) StartProcessRegisterTxs() { | ||||||
p.waitGroup.Add(1) | ||||||
txHashes := make([][]byte, 0, p.registerTxsBatchSize) | ||||||
|
||||||
ticker := time.NewTicker(p.registerTxsInterval) | ||||||
go func() { | ||||||
defer p.waitGroup.Done() | ||||||
for { | ||||||
select { | ||||||
case <-p.ctx.Done(): | ||||||
return | ||||||
case txHash := <-p.registerTxsChan: | ||||||
txHashes = append(txHashes, txHash) | ||||||
|
||||||
if len(txHashes) < p.registerTxsBatchSize { | ||||||
continue | ||||||
} | ||||||
|
||||||
p.registerTransactions(txHashes[:]) | ||||||
txHashes = txHashes[:0] | ||||||
ticker.Reset(p.registerTxsInterval) | ||||||
|
||||||
case <-ticker.C: | ||||||
if len(txHashes) == 0 { | ||||||
continue | ||||||
} | ||||||
|
||||||
p.registerTransactions(txHashes[:]) | ||||||
txHashes = txHashes[:0] | ||||||
ticker.Reset(p.registerTxsInterval) | ||||||
} | ||||||
} | ||||||
}() | ||||||
} | ||||||
|
||||||
func (p *Processor) StartProcessRequestTxs() { | ||||||
p.waitGroup.Add(1) | ||||||
|
||||||
txHashes := make([]*chainhash.Hash, 0, p.registerRequestTxsBatchSize) | ||||||
txHashes := make([][]byte, 0, p.requestTxsBatchSize) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||
|
||||||
ticker := time.NewTicker(p.registerRequestTxsInterval) | ||||||
ticker := time.NewTicker(p.requestTxsInterval) | ||||||
|
||||||
go func() { | ||||||
defer p.waitGroup.Done() | ||||||
|
@@ -337,15 +287,15 @@ func (p *Processor) StartProcessRequestTxs() { | |||||
case <-p.ctx.Done(): | ||||||
return | ||||||
case txHash := <-p.requestTxChannel: | ||||||
tx, err := chainhash.NewHash(txHash) | ||||||
_, err := chainhash.NewHash(txHash) | ||||||
if err != nil { | ||||||
p.logger.Error("Failed to create hash from byte array", slog.String("err", err.Error())) | ||||||
continue | ||||||
} | ||||||
|
||||||
txHashes = append(txHashes, tx) | ||||||
txHashes = append(txHashes, txHash) | ||||||
|
||||||
if len(txHashes) < p.registerRequestTxsBatchSize || len(txHashes) == 0 { | ||||||
if len(txHashes) < p.requestTxsBatchSize { | ||||||
continue | ||||||
} | ||||||
|
||||||
|
@@ -355,8 +305,8 @@ func (p *Processor) StartProcessRequestTxs() { | |||||
continue // retry, don't clear the txHashes slice | ||||||
} | ||||||
|
||||||
txHashes = make([]*chainhash.Hash, 0, p.registerRequestTxsBatchSize) | ||||||
ticker.Reset(p.registerRequestTxsInterval) | ||||||
txHashes = make([][]byte, 0, p.requestTxsBatchSize) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
ticker.Reset(p.requestTxsInterval) | ||||||
|
||||||
case <-ticker.C: | ||||||
if len(txHashes) == 0 { | ||||||
|
@@ -366,54 +316,47 @@ func (p *Processor) StartProcessRequestTxs() { | |||||
err := p.publishMinedTxs(txHashes) | ||||||
if err != nil { | ||||||
p.logger.Error("failed to publish mined txs", slog.String("err", err.Error())) | ||||||
ticker.Reset(p.registerRequestTxsInterval) | ||||||
ticker.Reset(p.requestTxsInterval) | ||||||
continue // retry, don't clear the txHashes slice | ||||||
} | ||||||
|
||||||
txHashes = make([]*chainhash.Hash, 0, p.registerRequestTxsBatchSize) | ||||||
ticker.Reset(p.registerRequestTxsInterval) | ||||||
txHashes = make([][]byte, 0, p.requestTxsBatchSize) | ||||||
ticker.Reset(p.requestTxsInterval) | ||||||
} | ||||||
} | ||||||
}() | ||||||
} | ||||||
|
||||||
func (p *Processor) publishMinedTxs(txHashes []*chainhash.Hash) error { | ||||||
minedTxs, err := p.store.GetMinedTransactions(p.ctx, txHashes) | ||||||
func (p *Processor) publishMinedTxs(txHashes [][]byte) error { | ||||||
minedTxs, err := p.store.UpsertAndGetMinedTransactions(p.ctx, txHashes) | ||||||
if err != nil { | ||||||
return fmt.Errorf("failed to get mined transactions: %v", err) | ||||||
} | ||||||
|
||||||
var publishErr error | ||||||
|
||||||
for _, minedTx := range minedTxs { | ||||||
txBlock := &blocktx_api.TransactionBlock{ | ||||||
TransactionHash: minedTx.TxHash, | ||||||
BlockHash: minedTx.BlockHash, | ||||||
BlockHeight: minedTx.BlockHeight, | ||||||
MerklePath: minedTx.MerklePath, | ||||||
} | ||||||
|
||||||
err = p.mqClient.PublishMarshal(p.ctx, MinedTxsTopic, txBlock) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if err != nil { | ||||||
p.logger.Error("failed to publish mined transaction", slog.String("err", err.Error())) | ||||||
publishErr = err | ||||||
} | ||||||
} | ||||||
|
||||||
if err != nil { | ||||||
return fmt.Errorf("failed to publish mined transactions: %v", err) | ||||||
if publishErr != nil { | ||||||
return ErrFailedToPublishMinedTransaction | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
func (p *Processor) registerTransactions(txHashes [][]byte) { | ||||||
updatedTxs, err := p.store.RegisterTransactions(p.ctx, txHashes) | ||||||
if err != nil { | ||||||
p.logger.Error("failed to register transactions", slog.String("err", err.Error())) | ||||||
} | ||||||
|
||||||
if len(updatedTxs) > 0 { | ||||||
err = p.publishMinedTxs(updatedTxs) | ||||||
if err != nil { | ||||||
p.logger.Error("failed to publish mined txs", slog.String("err", err.Error())) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
func (p *Processor) buildMerkleTreeStoreChainHash(ctx context.Context, txids []*chainhash.Hash) []*chainhash.Hash { | ||||||
_, span := tracing.StartTracing(ctx, "buildMerkleTreeStoreChainHash", p.tracingEnabled, p.tracingAttributes...) | ||||||
defer tracing.EndTracing(span, nil) | ||||||
|
This file contains 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.
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.
I think we should keep here the name
registerTxsInterval
and also topicRegisterTxTopic = "register-tx"
and droprequestTxsInterval
instead because a new tx is always registered. So that is kind a core functionality.The functionality for requesting a tx because it wasn't mined for too long is rather an exception in case that e.g.
blocktx
was failing for some reason. So that is like an auxiliary functionality.