This repository was archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Modularize Beethoven logic #29
Merged
Merged
Changes from 7 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
78c803d
Refactor code a bit
d6f42d8
Fixing some errors
a5dbbca
Better naming
864abdf
Merge branch 'main' into vcastellm/refactor_rpc
0c436fc
Continue refactor
vcastellm a5ce8a0
Almost working
vcastellm 41328d1
Fixes
vcastellm dd3e2c2
Update interop/verify.go
vcastellm aad375a
Move code around
f31a426
Fix tests
0717692
fix test
6eac949
Merge remote-tracking branch 'origin/remove-validium-dep' into vcaste…
b904c6e
Merge branch 'main' into vcastellm/refactor_rpc
vcastellm bd8c414
Fix merge issues
vcastellm 543599d
Fix lint
vcastellm 813fac1
Fix tests
vcastellm f9ba072
Fix typo
vcastellm 574e018
Fix tests
vcastellm 72afdc4
Refactor and tests
vcastellm 245704e
Add more tests
vcastellm ba5b6ea
More coverage
vcastellm a966245
Add test
vcastellm 795d5ee
More tests
vcastellm 73d9a75
Move mocks to correct place
vcastellm 472b3f3
Tests
vcastellm 9aea6f1
Fix test
vcastellm 0a01173
Cover more
vcastellm 58a5d6a
Apply context in several places
vcastellm c1d05f4
Fix comment
vcastellm b3c19d7
Apply feedback
vcastellm 39dc9bf
Fix signature
vcastellm 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
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
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,20 @@ | ||
package interop | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/0xPolygon/beethoven/tx" | ||
) | ||
|
||
func (e *Executor) CheckTx(ctx context.Context, tx tx.SignedTx) error { | ||
e.logger.Debug("check tx") | ||
|
||
// Check if the RPC is actually registered, if not it won't be possible to assert soundness (in the future once we are stateless won't be needed) | ||
// TODO: The JSON parsing of the contract is incorrect | ||
if _, ok := e.config.FullNodeRPCs[tx.Tx.L1Contract]; !ok { | ||
return fmt.Errorf("there is no RPC registered for %s", tx.Tx.L1Contract) | ||
} | ||
|
||
return nil | ||
} |
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,71 @@ | ||
package interop | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/0xPolygon/beethoven/tx" | ||
"github.com/jackc/pgx/v4" | ||
|
||
"github.com/0xPolygon/cdk-validium-node/jsonrpc/client" | ||
"github.com/0xPolygon/cdk-validium-node/log" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
const ethTxManOwner = "interop" | ||
|
||
func (e *Executor) Execute(signedTx tx.SignedTx) error { | ||
ctx := context.TODO() | ||
|
||
// Check expected root vs root from the managed full node | ||
// TODO: go stateless, depends on https://github.com/0xPolygonHermez/zkevm-prover/issues/581 | ||
// when this happens we should go async from here, since processing all the batches could take a lot of time | ||
zkEVMClient := client.NewClient(e.config.FullNodeRPCs[signedTx.Tx.L1Contract]) | ||
batch, err := zkEVMClient.BatchByNumber( | ||
ctx, | ||
big.NewInt(int64(signedTx.Tx.NewVerifiedBatch)), | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to get batch from our node, error: %s", err) | ||
} | ||
if batch.StateRoot != signedTx.Tx.ZKP.NewStateRoot || batch.LocalExitRoot != signedTx.Tx.ZKP.NewLocalExitRoot { | ||
return fmt.Errorf( | ||
"Missmatch detected, expected local exit root: %s actual: %s. expected state root: %s actual: %s", | ||
signedTx.Tx.ZKP.NewLocalExitRoot.Hex(), | ||
batch.LocalExitRoot.Hex(), | ||
signedTx.Tx.ZKP.NewStateRoot.Hex(), | ||
batch.StateRoot.Hex(), | ||
) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (e *Executor) Settle(signedTx tx.SignedTx, dbTx pgx.Tx) (common.Hash, error) { | ||
// // Send L1 tx | ||
// Verify ZKP using eth_call | ||
l1TxData, err := e.etherman.BuildTrustedVerifyBatchesTxData( | ||
uint64(signedTx.Tx.LastVerifiedBatch), | ||
uint64(signedTx.Tx.NewVerifiedBatch), | ||
signedTx.Tx.ZKP, | ||
) | ||
if err != nil { | ||
return common.Hash{}, fmt.Errorf("failed to build verify ZKP tx: %s", err) | ||
} | ||
|
||
if err := e.ethTxMan.Add( | ||
context.Background(), | ||
ethTxManOwner, | ||
signedTx.Tx.Hash().Hex(), | ||
e.interopAdminAddr, | ||
&signedTx.Tx.L1Contract, | ||
nil, | ||
l1TxData, | ||
dbTx, | ||
); err != nil { | ||
return common.Hash{}, fmt.Errorf("failed to add tx to ethTxMan, error: %s", err) | ||
} | ||
log.Debugf("successfuly added tx %s to ethTxMan", signedTx.Tx.Hash().Hex()) | ||
return signedTx.Tx.Hash(), nil | ||
} |
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,31 @@ | ||
package interop | ||
|
||
import ( | ||
"github.com/0xPolygon/beethoven/config" | ||
|
||
"github.com/0xPolygon/cdk-validium-node/log" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
type Executor struct { | ||
logger *log.Logger | ||
interopAdminAddr common.Address | ||
config *config.Config | ||
ethTxMan EthTxManager | ||
etherman EthermanInterface | ||
} | ||
|
||
func New(logger *log.Logger, cfg *config.Config, | ||
interopAdminAddr common.Address, | ||
db DBInterface, | ||
etherman EthermanInterface, | ||
ethTxManager EthTxManager, | ||
) *Executor { | ||
return &Executor{ | ||
logger: logger, | ||
interopAdminAddr: interopAdminAddr, | ||
config: cfg, | ||
ethTxMan: ethTxManager, | ||
etherman: etherman, | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package rpc | ||
package interop | ||
|
||
import ( | ||
"context" | ||
|
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,24 @@ | ||
package interop | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/0xPolygon/cdk-validium-node/jsonrpc/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/jackc/pgx/v4" | ||
) | ||
|
||
func (e *Executor) GetTxStatus(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (result string, err types.Error) { | ||
res, innerErr := e.ethTxMan.Result(ctx, ethTxManOwner, hash.Hex(), dbTx) | ||
if innerErr != nil { | ||
result = "0x0" | ||
err = types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("failed to get tx, error: %s", innerErr)) | ||
|
||
return | ||
} | ||
|
||
result = res.Status.String() | ||
|
||
return | ||
} |
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,61 @@ | ||
package interop | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/0xPolygon/beethoven/tx" | ||
|
||
"github.com/ethereum/go-ethereum" | ||
) | ||
|
||
func (e *Executor) Verify(tx tx.SignedTx) error { | ||
err := e.VerifyZKP(tx) | ||
if err != nil { | ||
return fmt.Errorf("failed to verify ZKP: %s", err) | ||
} | ||
|
||
return e.VerifySignature(tx) | ||
} | ||
|
||
func (e *Executor) VerifyZKP(stx tx.SignedTx) error { | ||
// Verify ZKP using eth_call | ||
l1TxData, err := e.etherman.BuildTrustedVerifyBatchesTxData( | ||
uint64(stx.Tx.LastVerifiedBatch), | ||
uint64(stx.Tx.NewVerifiedBatch), | ||
stx.Tx.ZKP, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to build verify ZKP tx: %s", err) | ||
} | ||
msg := ethereum.CallMsg{ | ||
From: e.interopAdminAddr, | ||
To: &stx.Tx.L1Contract, | ||
Data: l1TxData, | ||
} | ||
res, err := e.etherman.CallContract(context.Background(), msg, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to call verify ZKP response: %s, error: %s", res, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *Executor) VerifySignature(stx tx.SignedTx) error { | ||
// Auth: check signature vs admin | ||
signer, err := stx.Signer() | ||
if err != nil { | ||
return errors.New("failed to get signer") | ||
} | ||
|
||
sequencer, err := i.etherman.GetSequencerAddr(stx.Tx.L1Contract) | ||
if err != nil { | ||
return errors.New("failed to get admin from L1") | ||
} | ||
if sequencer != signer { | ||
return errors.New("unexpected signer") | ||
} | ||
|
||
return nil | ||
} | ||
vcastellm marked this conversation as resolved.
Show resolved
Hide resolved
|
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.