-
Notifications
You must be signed in to change notification settings - Fork 24
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
Add Configurable Worker Pool for Transaction Processing #131
Open
JohnChangUK
wants to merge
4
commits into
aptos-labs:main
Choose a base branch
from
JohnChangUK:worker-pool-tx
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+354
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
51ae856
Add Configurable Worker Pool for Transaction Processing
JohnChangUK c8b06f8
Merge branch 'main' into worker-pool-tx
gregnazario 777d608
Merge branch 'main' into worker-pool-tx
JohnChangUK a4dab69
Merge branch 'main' into worker-pool-tx
gregnazario 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
97 changes: 97 additions & 0 deletions
97
integration_test/transactionSubmission/multi_sender_test.go
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package integration_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aptos-labs/aptos-go-sdk" | ||
"github.com/aptos-labs/aptos-go-sdk/internal/testutil" | ||
) | ||
|
||
func TestBuildSignAndSubmitTransactionsWithSignFnAndWorkerPoolWithMultipleSenders(t *testing.T) { | ||
const ( | ||
numSenders = 3 | ||
txPerSender = 5 | ||
initialFunding = uint64(100_000_000) | ||
transfer_amount = uint64(100) | ||
) | ||
|
||
clients := testutil.SetupTestClients(t) | ||
|
||
// Create and fund senders | ||
senders := make([]testutil.TestAccount, numSenders) | ||
for i := 0; i < numSenders; i++ { | ||
senders[i] = testutil.SetupTestAccount(t, clients.Client, initialFunding) | ||
} | ||
|
||
receiver := testutil.SetupTestAccount(t, clients.Client, 0) | ||
|
||
startTime := time.Now() | ||
|
||
// Process transactions for each sender | ||
doneCh := make(chan struct{}) | ||
|
||
for senderIdx := 0; senderIdx < numSenders; senderIdx++ { | ||
go func(senderIdx int) { | ||
defer func() { | ||
doneCh <- struct{}{} | ||
}() | ||
|
||
sender := senders[senderIdx] | ||
payloads := make(chan aptos.TransactionBuildPayload, txPerSender) | ||
responses := make(chan aptos.TransactionSubmissionResponse, txPerSender) | ||
|
||
go clients.NodeClient.BuildSignAndSubmitTransactionsWithSignFnAndWorkerPool( | ||
sender.Account.Address, | ||
payloads, | ||
responses, | ||
func(rawTxn aptos.RawTransactionImpl) (*aptos.SignedTransaction, error) { | ||
switch txn := rawTxn.(type) { | ||
case *aptos.RawTransaction: | ||
return txn.SignedTransaction(sender.Account) | ||
default: | ||
return nil, fmt.Errorf("unsupported transaction type") | ||
} | ||
}, | ||
aptos.WorkerPoolConfig{NumWorkers: 3}, | ||
) | ||
|
||
workerStartTime := time.Now() | ||
for txNum := 0; txNum < txPerSender; txNum++ { | ||
payload := testutil.CreateTransferPayload(t, receiver.Account.Address, transfer_amount) | ||
payloads <- aptos.TransactionBuildPayload{ | ||
Id: uint64(txNum), | ||
Inner: payload, | ||
Type: aptos.TransactionSubmissionTypeSingle, | ||
} | ||
} | ||
close(payloads) | ||
|
||
for i := 0; i < txPerSender; i++ { | ||
resp := <-responses | ||
if resp.Err != nil { | ||
t.Errorf("Transaction failed: %v", resp.Err) | ||
continue | ||
} | ||
fmt.Printf("[%s] Worker %d → hash: %s\n", | ||
time.Now().Format("15:04:05.000"), | ||
senderIdx, | ||
resp.Response.Hash) | ||
} | ||
|
||
fmt.Printf("[%s] Worker %d completed all transactions (t+%v)\n", | ||
time.Now().Format("15:04:05.000"), | ||
senderIdx, | ||
time.Since(workerStartTime).Round(time.Millisecond)) | ||
}(senderIdx) | ||
} | ||
|
||
// Wait for all senders to complete | ||
for i := 0; i < numSenders; i++ { | ||
<-doneCh | ||
} | ||
|
||
duration := time.Since(startTime) | ||
fmt.Printf("\nTotal Duration: %v\n", duration) | ||
} |
72 changes: 72 additions & 0 deletions
72
integration_test/transactionSubmission/single_sender_test.go
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package integration_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aptos-labs/aptos-go-sdk" | ||
"github.com/aptos-labs/aptos-go-sdk/internal/testutil" | ||
) | ||
|
||
func TestBuildSignAndSubmitTransactionsWithSignFnAndWorkerPoolWithOneSender(t *testing.T) { | ||
const ( | ||
numTransactions = 5 | ||
transferAmount = uint64(100) | ||
numWorkers = 3 | ||
initialFunding = uint64(100_000_000) | ||
) | ||
|
||
clients := testutil.SetupTestClients(t) | ||
sender := testutil.SetupTestAccount(t, clients.Client, initialFunding) | ||
receiver := testutil.SetupTestAccount(t, clients.Client, 0) | ||
|
||
payloads := make(chan aptos.TransactionBuildPayload, numTransactions) | ||
responses := make(chan aptos.TransactionSubmissionResponse, numTransactions) | ||
workerPoolConfig := aptos.WorkerPoolConfig{ | ||
NumWorkers: numWorkers, | ||
BuildResponseBuffer: numTransactions, | ||
SubmissionBuffer: numTransactions, | ||
} | ||
|
||
startTime := time.Now() | ||
|
||
go clients.NodeClient.BuildSignAndSubmitTransactionsWithSignFnAndWorkerPool( | ||
sender.Account.Address, | ||
payloads, | ||
responses, | ||
func(rawTxn aptos.RawTransactionImpl) (*aptos.SignedTransaction, error) { | ||
switch txn := rawTxn.(type) { | ||
case *aptos.RawTransaction: | ||
return txn.SignedTransaction(sender.Account) | ||
default: | ||
return nil, fmt.Errorf("unsupported transaction type") | ||
} | ||
}, | ||
workerPoolConfig, | ||
) | ||
|
||
for txNum := 0; txNum < numTransactions; txNum++ { | ||
payload := testutil.CreateTransferPayload(t, receiver.Account.Address, transferAmount) | ||
payloads <- aptos.TransactionBuildPayload{ | ||
Id: uint64(txNum), | ||
Inner: payload, | ||
Type: aptos.TransactionSubmissionTypeSingle, | ||
} | ||
} | ||
close(payloads) | ||
|
||
for i := 0; i < numTransactions; i++ { | ||
resp := <-responses | ||
if resp.Err != nil { | ||
t.Errorf("Transaction failed: %v", resp.Err) | ||
continue | ||
} | ||
fmt.Printf("[%s] hash: %s\n", | ||
time.Now().Format("15:04:05.000"), | ||
resp.Response.Hash) | ||
} | ||
|
||
duration := time.Since(startTime) | ||
fmt.Printf("\nTotal Duration: %v\n", duration) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package testutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aptos-labs/aptos-go-sdk" | ||
) | ||
|
||
// TestClients holds the clients needed for testing | ||
type TestClients struct { | ||
NodeClient *aptos.NodeClient | ||
Client *aptos.Client | ||
} | ||
|
||
func CreateTestClient() (*aptos.Client, error) { | ||
return aptos.NewClient(aptos.DevnetConfig) | ||
} | ||
|
||
func CreateTestNodeClient() (*aptos.NodeClient, error) { | ||
return aptos.NewNodeClient(aptos.DevnetConfig.NodeUrl, aptos.DevnetConfig.ChainId) | ||
} | ||
|
||
func SetupTestClients(t *testing.T) *TestClients { | ||
nodeClient, err := CreateTestNodeClient() | ||
if err != nil { | ||
t.Fatalf("Failed to create NodeClient: %v", err) | ||
} | ||
|
||
client, err := CreateTestClient() | ||
if err != nil { | ||
t.Fatalf("Failed to create Client: %v", err) | ||
} | ||
|
||
return &TestClients{ | ||
NodeClient: nodeClient, | ||
Client: client, | ||
} | ||
} | ||
|
||
func CreateTransferPayload(t *testing.T, receiver aptos.AccountAddress, amount uint64) aptos.TransactionPayload { | ||
p, err := aptos.CoinTransferPayload(nil, receiver, amount) | ||
if err != nil { | ||
t.Fatalf("Failed to create transfer payload: %v", err) | ||
} | ||
return aptos.TransactionPayload{Payload: p} | ||
} | ||
|
||
// TestAccount represents a funded account for testing | ||
type TestAccount struct { | ||
Account *aptos.Account | ||
InitialBalance uint64 | ||
} | ||
|
||
func SetupTestAccount(t *testing.T, client *aptos.Client, funding uint64) TestAccount { | ||
account, err := aptos.NewEd25519Account() | ||
if err != nil { | ||
t.Fatalf("Failed to create account: %v", err) | ||
} | ||
|
||
err = client.Fund(account.Address, funding) | ||
if err != nil { | ||
t.Fatalf("Failed to fund account: %v", err) | ||
} | ||
|
||
balance, err := client.AccountAPTBalance(account.Address) | ||
if err != nil { | ||
t.Fatalf("Failed to get initial balance: %v", err) | ||
} | ||
|
||
return TestAccount{ | ||
Account: account, | ||
InitialBalance: balance, | ||
} | ||
} |
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
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.
You might be able to put the WorkerPoolConfig in the ...any
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.
Looking at what is passed in
buildOptions
, they are all blockchain TX related(MaxGasAmount, GasUnitPrice, ExpirationSeconds, SequenceNumber, ChainIdOption)
, therefore I think it'll be good to ahve these two as separate configs.