diff --git a/integration-tests/go.mod b/integration-tests/go.mod index 7e5ff359a6c..3f1ed933486 100644 --- a/integration-tests/go.mod +++ b/integration-tests/go.mod @@ -26,7 +26,7 @@ require ( github.com/chaos-mesh/chaos-mesh/api v0.0.0-20240821051457-da69c6d9617a github.com/cli/go-gh/v2 v2.0.0 github.com/deckarep/golang-set/v2 v2.6.0 - github.com/ethereum/go-ethereum v1.14.12 + github.com/ethereum/go-ethereum v1.14.11 github.com/fxamacker/cbor/v2 v2.7.0 github.com/go-resty/resty/v2 v2.15.3 github.com/google/go-cmp v0.6.0 diff --git a/test.go b/test.go new file mode 100644 index 00000000000..42978ee2cde --- /dev/null +++ b/test.go @@ -0,0 +1,112 @@ +package main + +import ( + "context" + "fmt" + "log" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rpc" +) + +// Tx represents a transaction structure +type Tx struct { + TxAttempts []TxAttempt +} + +// TxAttempt represents a transaction attempt structure +type TxAttempt struct { + Hash common.Hash +} + +// zircuitResponse represents the response structure from zirc_isQuarantined +type zircuitResponse struct { + IsQuarantined bool `json:"isQuarantined"` +} + +// stuckTxDetector represents the structure for stuck transaction detection +type stuckTxDetector struct { + chainClient *rpc.Client + lggr Logger +} + +// Logger interface for logging (simplified here for demonstration) +type Logger struct{} + +func (l Logger) Debugf(format string, args ...interface{}) { + log.Printf(format, args...) +} + +// detectFraudTransactionsZircuit detects quarantined transactions +func (d *stuckTxDetector) detectFraudTransactionsZircuit(ctx context.Context, txs []Tx) ([]Tx, error) { + txReqs := make([]rpc.BatchElem, len(txs)) + txHashMap := make(map[common.Hash]Tx) + txRes := make([]*zircuitResponse, len(txs)) + + // Build batch request elements + for i, tx := range txs { + latestAttemptHash := tx.TxAttempts[0].Hash + var result zircuitResponse + txReqs[i] = rpc.BatchElem{ + Method: "zirc_isQuarantined", + Args: []interface{}{latestAttemptHash}, + Result: &result, + } + txHashMap[latestAttemptHash] = tx + txRes[i] = &result + } + + // Send batch request using the Ethereum client + err := d.chainClient.BatchCallContext(ctx, txReqs) + if err != nil { + return nil, fmt.Errorf("failed to check Quarantine transactions in batch: %w", err) + } + + // Process the results and check if any transactions are quarantined + var fraudTxs []Tx + for i, req := range txReqs { + txHash := req.Args[0].(common.Hash) + if req.Error != nil { + d.lggr.Debugf("failed to check fraud transaction by hash (%s): %v", txHash.String(), req.Error) + continue + } + + result := txRes[i] + if result != nil && result.IsQuarantined { + tx := txHashMap[txHash] + fraudTxs = append(fraudTxs, tx) + } + } + return fraudTxs, nil +} + +// +//func main() { +// // Create an Ethereum RPC client to communicate with the Zircuit endpoint +// client, err := rpc.Dial("https://zircuit1-mainnet.p2pify.com") +// if err != nil { +// log.Fatalf("Failed to create Ethereum RPC client: %v", err) +// } +// +// // Create the detector with the Ethereum client +// detector := &stuckTxDetector{ +// chainClient: client, +// lggr: Logger{}, +// } +// +// // Example transactions (replace with real data) +// txs := []Tx{ +// {TxAttempts: []TxAttempt{{Hash: common.HexToHash("0xbf7179cbeb760388d972b68dfeef0ee687dd1286d5b4cd534b6b75c42bbdf3c4")}}}, +// {TxAttempts: []TxAttempt{{Hash: common.HexToHash("0x3f9d8133af4ab1ca64c4a6fa572161995eb88fd942d71a61910a04e1639ee51e")}}}, +// } +// +// // Detect fraudulent transactions +// ctx := context.Background() +// fraudTxs, err := detector.detectFraudTransactionsZircuit(ctx, txs) +// if err != nil { +// log.Fatalf("Error detecting fraud transactions: %v", err) +// } +// +// // Print out the fraudulent transactions +// fmt.Printf("Fraudulent transactions: %+v\n", fraudTxs) +//}