From 08038173a8baeb77c8b3922474ad16b228b31b17 Mon Sep 17 00:00:00 2001 From: pk910 Date: Thu, 11 Jan 2024 04:00:18 +0100 Subject: [PATCH] more flexibility for chained `generate_transaction` tasks (eg. deploy contract and callmethods on it) --- .../tasks/generate_transaction/config.go | 13 ++- .../tasks/generate_transaction/task.go | 80 +++++++++++-------- 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/pkg/coordinator/tasks/generate_transaction/config.go b/pkg/coordinator/tasks/generate_transaction/config.go index 8714e7b..4657de2 100644 --- a/pkg/coordinator/tasks/generate_transaction/config.go +++ b/pkg/coordinator/tasks/generate_transaction/config.go @@ -21,6 +21,7 @@ type Config struct { ClientPattern string `yaml:"clientPattern" json:"clientPattern"` + AwaitReceipt bool `yaml:"awaitReceipt" json:"awaitReceipt"` FailOnReject bool `yaml:"failOnReject" json:"failOnReject"` FailOnSuccess bool `yaml:"failOnSuccess" json:"failOnSuccess"` ExpectEvents []struct { @@ -29,14 +30,18 @@ type Config struct { Topic2 string `yaml:"topic2" json:"topic2"` Data string `yaml:"data" json:"data"` } `yaml:"expectEvents" json:"expectEvents"` + + TransactionHashResultVar string `yaml:"transactionHashResultVar" json:"transactionHashResultVar"` + ContractAddressResultVar string `yaml:"contractAddressResultVar" json:"contractAddressResultVar"` } func DefaultConfig() Config { return Config{ - FeeCap: big.NewInt(100000000000), // 100 Gwei - TipCap: big.NewInt(1000000000), // 1 Gwei - GasLimit: 50000, - Amount: big.NewInt(0), + FeeCap: big.NewInt(100000000000), // 100 Gwei + TipCap: big.NewInt(1000000000), // 1 Gwei + GasLimit: 50000, + Amount: big.NewInt(0), + AwaitReceipt: true, } } diff --git a/pkg/coordinator/tasks/generate_transaction/task.go b/pkg/coordinator/tasks/generate_transaction/task.go index 0073737..3941367 100644 --- a/pkg/coordinator/tasks/generate_transaction/task.go +++ b/pkg/coordinator/tasks/generate_transaction/task.go @@ -156,54 +156,64 @@ func (t *Task) Execute(ctx context.Context) error { return err } - receipt, err := t.wallet.AwaitTransaction(ctx, tx) - if err != nil { - t.logger.Warnf("failed waiting for tx receipt: %v", err) - return fmt.Errorf("failed waiting for tx receipt: %v", err) + if t.config.TransactionHashResultVar != "" { + t.ctx.Vars.SetVar(t.config.TransactionHashResultVar, tx.Hash().Hex()) } - if receipt == nil { - return fmt.Errorf("tx receipt not found") - } + if t.config.AwaitReceipt { + receipt, err := t.wallet.AwaitTransaction(ctx, tx) + if err != nil { + t.logger.Warnf("failed waiting for tx receipt: %v", err) + return fmt.Errorf("failed waiting for tx receipt: %v", err) + } - t.logger.Infof("transaction %v confirmed (nonce: %v, status: %v)", tx.Hash().Hex(), tx.Nonce(), receipt.Status) + if receipt == nil { + return fmt.Errorf("tx receipt not found") + } - if t.config.FailOnSuccess && receipt.Status > 0 { - return fmt.Errorf("transaction succeeded, but expected rejection") - } + t.logger.Infof("transaction %v confirmed (nonce: %v, status: %v)", tx.Hash().Hex(), tx.Nonce(), receipt.Status) - if t.config.FailOnReject && receipt.Status == 0 { - return fmt.Errorf("transaction rejected, but expected success") - } + if t.config.FailOnSuccess && receipt.Status > 0 { + return fmt.Errorf("transaction succeeded, but expected rejection") + } - if len(t.config.ExpectEvents) > 0 { - for _, expectedEvent := range t.config.ExpectEvents { - foundEvent := false + if t.config.FailOnReject && receipt.Status == 0 { + return fmt.Errorf("transaction rejected, but expected success") + } - for _, log := range receipt.Logs { - if expectedEvent.Topic0 != "" && (len(log.Topics) < 1 || !bytes.Equal(common.FromHex(expectedEvent.Topic0), log.Topics[0][:])) { - continue - } + if t.config.ContractAddressResultVar != "" { + t.ctx.Vars.SetVar(t.config.ContractAddressResultVar, receipt.ContractAddress.Hex()) + } - if expectedEvent.Topic1 != "" && (len(log.Topics) < 2 || !bytes.Equal(common.FromHex(expectedEvent.Topic1), log.Topics[1][:])) { - continue - } + if len(t.config.ExpectEvents) > 0 { + for _, expectedEvent := range t.config.ExpectEvents { + foundEvent := false - if expectedEvent.Topic2 != "" && (len(log.Topics) < 3 || !bytes.Equal(common.FromHex(expectedEvent.Topic2), log.Topics[2][:])) { - continue - } + for _, log := range receipt.Logs { + if expectedEvent.Topic0 != "" && (len(log.Topics) < 1 || !bytes.Equal(common.FromHex(expectedEvent.Topic0), log.Topics[0][:])) { + continue + } - if expectedEvent.Data != "" && !bytes.Equal(common.FromHex(expectedEvent.Data), log.Data) { - continue - } + if expectedEvent.Topic1 != "" && (len(log.Topics) < 2 || !bytes.Equal(common.FromHex(expectedEvent.Topic1), log.Topics[1][:])) { + continue + } - foundEvent = true + if expectedEvent.Topic2 != "" && (len(log.Topics) < 3 || !bytes.Equal(common.FromHex(expectedEvent.Topic2), log.Topics[2][:])) { + continue + } - break - } + if expectedEvent.Data != "" && !bytes.Equal(common.FromHex(expectedEvent.Data), log.Data) { + continue + } - if !foundEvent { - return fmt.Errorf("expected event not fired: %v", expectedEvent) + foundEvent = true + + break + } + + if !foundEvent { + return fmt.Errorf("expected event not fired: %v", expectedEvent) + } } } }