-
Notifications
You must be signed in to change notification settings - Fork 1
feat: [E2E] Layer 2 — shim implementations #189
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ed643d0
feat: [E2E] bootstrap writes deploy manifest for test discovery (#177)
sadiq1971 fa8dea2
feat: [E2E] Layer 1 — stack interfaces, types, compose override, Make…
sadiq1971 e8e23c2
used existing types
sadiq1971 2d8fb9c
used existing types
sadiq1971 00cc4a2
added per db dsn
sadiq1971 e79889a
feat: docker discovry added
sadiq1971 b163542
feat: [E2E] Layer 2 — shim implementations
sadiq1971 a9df74c
resolved gemni comments
sadiq1971 214bfef
removed unused db
sadiq1971 9e49dc7
merged main
sadiq1971 14a0e2d
lint fixed
sadiq1971 31e930c
api server to act as ethclient
sadiq1971 0505f87
fix lint
sadiq1971 dc9f255
optimized
sadiq1971 0930433
Merge branch 'main' into feat/e2e-layer2-shim
sadiq1971 91f0e9a
feat: [E2E] Layer 2 — token client, error-safe NewCanton, HTTPError, …
sadiq1971 524a4fe
fix: [E2E] url.PathEscape party IDs in indexer shim, TotalSupply zero…
sadiq1971 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 hidden or 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 hidden or 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,208 @@ | ||
| //go:build e2e | ||
|
|
||
| // Package shim provides concrete implementations of the stack service | ||
| // interfaces. Each shim wraps a real network client (go-ethereum, HTTP, SQL) | ||
| // and is initialized from a ServiceManifest produced by ServiceDiscovery. | ||
| package shim | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/ecdsa" | ||
| "encoding/hex" | ||
| "errors" | ||
| "fmt" | ||
| "math/big" | ||
| "strings" | ||
| "time" | ||
|
|
||
| ethereum "github.com/ethereum/go-ethereum" | ||
| "github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/crypto" | ||
| "github.com/ethereum/go-ethereum/ethclient" | ||
|
|
||
| "github.com/chainsafe/canton-middleware/pkg/auth" | ||
| "github.com/chainsafe/canton-middleware/pkg/ethereum/contracts" | ||
| "github.com/chainsafe/canton-middleware/tests/e2e/devstack/stack" | ||
| ) | ||
|
|
||
| var _ stack.Anvil = (*AnvilShim)(nil) | ||
|
|
||
| // txGasLimit is a fixed gas ceiling for approve and depositToCanton transactions | ||
| // on the local Anvil devnet. Anvil's instant mining makes estimation unnecessary. | ||
| const ( | ||
| txGasLimit = 300_000 | ||
| txWaitTimeout = 30 * time.Second | ||
| bytes32Len = 32 | ||
| ) | ||
|
|
||
| // AnvilShim implements stack.Anvil against a local Anvil node. | ||
| type AnvilShim struct { | ||
| endpoint string | ||
| rpc *ethclient.Client | ||
| chainID *big.Int | ||
| tokenAddr common.Address | ||
| bridgeAddr common.Address | ||
| } | ||
|
|
||
| // NewAnvil dials the Anvil RPC endpoint from the manifest and returns a ready | ||
| // shim. It resolves chainID eagerly so callers do not need a context. | ||
| func NewAnvil(ctx context.Context, manifest *stack.ServiceManifest) (*AnvilShim, error) { | ||
| client, err := ethclient.DialContext(ctx, manifest.AnvilRPC) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("dial anvil: %w", err) | ||
| } | ||
| chainID, err := client.ChainID(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("get anvil chain ID: %w", err) | ||
| } | ||
| return &AnvilShim{ | ||
| endpoint: manifest.AnvilRPC, | ||
| rpc: client, | ||
| chainID: chainID, | ||
| tokenAddr: common.HexToAddress(manifest.PromptTokenAddr), | ||
| bridgeAddr: common.HexToAddress(manifest.BridgeAddr), | ||
| }, nil | ||
| } | ||
|
|
||
| func (a *AnvilShim) Endpoint() string { return a.endpoint } | ||
| func (a *AnvilShim) RPC() *ethclient.Client { return a.rpc } | ||
| func (a *AnvilShim) ChainID() *big.Int { return a.chainID } | ||
| func (a *AnvilShim) Close() { a.rpc.Close() } | ||
|
|
||
| // ERC20Balance returns the on-chain ERC-20 balance of owner for tokenAddr. | ||
| func (a *AnvilShim) ERC20Balance(ctx context.Context, tokenAddr, owner common.Address) (*big.Int, error) { | ||
| token, err := contracts.NewPromptToken(tokenAddr, a.rpc) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("bind erc20: %w", err) | ||
| } | ||
| bal, err := token.BalanceOf(&bind.CallOpts{Context: ctx}, owner) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("balanceOf: %w", err) | ||
| } | ||
| return bal, nil | ||
| } | ||
|
|
||
| // ApproveAndDeposit approves the bridge contract and submits a depositToCanton | ||
| // transaction for account. The canton recipient bytes32 is derived from the | ||
| // account's EVM address fingerprint via auth.ComputeFingerprint. | ||
| func (a *AnvilShim) ApproveAndDeposit(ctx context.Context, account *stack.Account, amount *big.Int) (common.Hash, error) { | ||
| key, err := parseKey(account.PrivateKey) | ||
| if err != nil { | ||
| return common.Hash{}, err | ||
| } | ||
|
|
||
| fingerprint := auth.ComputeFingerprint(account.Address.Hex()) | ||
| recipient, err := fingerprintToBytes32(fingerprint) | ||
| if err != nil { | ||
| return common.Hash{}, err | ||
| } | ||
|
|
||
| token, err := contracts.NewPromptToken(a.tokenAddr, a.rpc) | ||
| if err != nil { | ||
| return common.Hash{}, fmt.Errorf("bind prompt token: %w", err) | ||
| } | ||
| bridge, err := contracts.NewCantonBridge(a.bridgeAddr, a.rpc) | ||
| if err != nil { | ||
| return common.Hash{}, fmt.Errorf("bind canton bridge: %w", err) | ||
| } | ||
|
|
||
| // Step 1: approve. | ||
| auth, err := newTransactor(ctx, a.rpc, key, a.chainID) | ||
| if err != nil { | ||
| return common.Hash{}, err | ||
| } | ||
| approveTx, err := token.Approve(auth, a.bridgeAddr, amount) | ||
| if err != nil { | ||
| return common.Hash{}, fmt.Errorf("approve: %w", err) | ||
| } | ||
| if waitErr := waitForTx(ctx, a.rpc, approveTx.Hash(), txWaitTimeout); waitErr != nil { | ||
| return common.Hash{}, fmt.Errorf("wait approve tx: %w", waitErr) | ||
| } | ||
|
|
||
| // Step 2: deposit. | ||
| auth, err = newTransactor(ctx, a.rpc, key, a.chainID) | ||
| if err != nil { | ||
| return common.Hash{}, err | ||
| } | ||
| depositTx, err := bridge.DepositToCanton(auth, a.tokenAddr, amount, recipient) | ||
| if err != nil { | ||
| return common.Hash{}, fmt.Errorf("depositToCanton: %w", err) | ||
| } | ||
| if waitErr := waitForTx(ctx, a.rpc, depositTx.Hash(), txWaitTimeout); waitErr != nil { | ||
| return common.Hash{}, fmt.Errorf("wait deposit tx: %w", waitErr) | ||
| } | ||
|
|
||
| return depositTx.Hash(), nil | ||
| } | ||
|
|
||
| // newTransactor creates a TransactOpts with current nonce and suggested gas price. | ||
| func newTransactor(ctx context.Context, client *ethclient.Client, key *ecdsa.PrivateKey, chainID *big.Int) (*bind.TransactOpts, error) { | ||
| auth, err := bind.NewKeyedTransactorWithChainID(key, chainID) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("keyed transactor: %w", err) | ||
| } | ||
| nonce, err := client.PendingNonceAt(ctx, crypto.PubkeyToAddress(key.PublicKey)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("pending nonce: %w", err) | ||
| } | ||
| auth.Nonce = new(big.Int).SetUint64(nonce) | ||
| gasPrice, err := client.SuggestGasPrice(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("suggest gas price: %w", err) | ||
| } | ||
| auth.GasPrice = gasPrice | ||
| auth.GasLimit = txGasLimit | ||
| return auth, nil | ||
| } | ||
|
|
||
| // waitForTx polls until the transaction is mined or the timeout is reached. | ||
| // It returns immediately on any RPC error other than ethereum.NotFound (tx not | ||
| // yet visible) to avoid masking genuine node failures. | ||
| func waitForTx(ctx context.Context, client *ethclient.Client, hash common.Hash, timeout time.Duration) error { | ||
| ctx, cancel := context.WithTimeout(ctx, timeout) | ||
| defer cancel() | ||
| for { | ||
| receipt, err := client.TransactionReceipt(ctx, hash) | ||
| if err == nil { | ||
| if receipt.Status == 1 { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("transaction %s reverted", hash.Hex()) | ||
| } | ||
| if !errors.Is(err, ethereum.NotFound) { | ||
| return fmt.Errorf("receipt query for %s: %w", hash.Hex(), err) | ||
| } | ||
| select { | ||
| case <-ctx.Done(): | ||
| return fmt.Errorf("timeout waiting for tx %s: %w", hash.Hex(), ctx.Err()) | ||
| case <-time.After(time.Second): | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // parseKey decodes a hex-encoded ECDSA private key (without 0x prefix). | ||
| func parseKey(hexKey string) (*ecdsa.PrivateKey, error) { | ||
| key, err := crypto.HexToECDSA(hexKey) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("parse private key: %w", err) | ||
| } | ||
| return key, nil | ||
| } | ||
|
|
||
| // fingerprintToBytes32 converts a hex fingerprint string to a [32]byte. | ||
| // auth.ComputeFingerprint always returns a keccak256 hash (exactly 32 bytes), | ||
| // so copy fills the full array with no trailing zeros. | ||
| func fingerprintToBytes32(fingerprint string) ([32]byte, error) { | ||
| var result [32]byte | ||
| fingerprint = strings.TrimPrefix(fingerprint, "0x") | ||
| data, err := hex.DecodeString(fingerprint) | ||
| if err != nil { | ||
| return result, fmt.Errorf("decode fingerprint: %w", err) | ||
| } | ||
| if len(data) > bytes32Len { | ||
| return result, fmt.Errorf("fingerprint too long: %d bytes", len(data)) | ||
| } | ||
| copy(result[:], data) | ||
| return result, nil | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.