Skip to content

Commit

Permalink
more flexibility for chained generate_transaction tasks (eg. deploy…
Browse files Browse the repository at this point in the history
… contract and callmethods on it)
  • Loading branch information
pk910 committed Jan 11, 2024
1 parent a17ea75 commit 0803817
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
13 changes: 9 additions & 4 deletions pkg/coordinator/tasks/generate_transaction/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}

Expand Down
80 changes: 45 additions & 35 deletions pkg/coordinator/tasks/generate_transaction/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down

0 comments on commit 0803817

Please sign in to comment.