From 6acb45c12e982b259f7354c9370841e3be5e7290 Mon Sep 17 00:00:00 2001 From: Bartlomiej Tarczynski <51199571+b-tarczynski@users.noreply.github.com> Date: Mon, 2 Aug 2021 18:18:37 +0200 Subject: [PATCH] Measure gas usage by signature disputes (#285) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Measure transfers dispute signature gas usage * Measure create2Transfers dispute signature gas usage * Restore E2E build tag Co-authored-by: MichaƂ Sieczkowski --- e2e/dispute_gas_measure_test.go | 106 ++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 11 deletions(-) diff --git a/e2e/dispute_gas_measure_test.go b/e2e/dispute_gas_measure_test.go index 09c8dd20e..29263a545 100644 --- a/e2e/dispute_gas_measure_test.go +++ b/e2e/dispute_gas_measure_test.go @@ -14,6 +14,7 @@ import ( "github.com/Worldcoin/hubble-commander/models" "github.com/Worldcoin/hubble-commander/utils/ref" "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" "github.com/ybbus/jsonrpc/v2" ) @@ -35,12 +36,39 @@ func TestMeasureDisputeGasUsage(t *testing.T) { ethClient := newEthClient(t, cmd.Client()) + measureDisputeSignatureTransfer(t, cmd.Client(), ethClient) + measureDisputeSignatureC2T(t, cmd.Client(), ethClient, wallets) + testSendTransferBatch(t, cmd.Client(), senderWallet, 0) measureDisputeTransitionTransferInvalidStateRoot(t, cmd.Client(), ethClient) measureDisputeTransitionC2TInvalidStateRoot(t, cmd.Client(), ethClient, wallets) } +func measureDisputeSignatureTransfer(t *testing.T, client jsonrpc.RPCClient, ethClient *eth.Client) { + sink := make(chan *rollup.RollupRollbackStatus) + subscription, err := ethClient.Rollup.WatchRollbackStatus(&bind.WatchOpts{}, sink) + require.NoError(t, err) + defer subscription.Unsubscribe() + + send32TransfersBatchWithInvalidSignature(t, ethClient) + testRollbackCompletion(t, ethClient, sink, subscription) + + testBatchesAfterDispute(t, client, 0) +} + +func measureDisputeSignatureC2T(t *testing.T, client jsonrpc.RPCClient, ethClient *eth.Client, wallets []bls.Wallet) { + sink := make(chan *rollup.RollupRollbackStatus) + subscription, err := ethClient.Rollup.WatchRollbackStatus(&bind.WatchOpts{}, sink) + require.NoError(t, err) + defer subscription.Unsubscribe() + + send32C2TBatchWithInvalidSignature(t, ethClient, wallets) + testRollbackCompletion(t, ethClient, sink, subscription) + + testBatchesAfterDispute(t, client, 0) +} + func measureDisputeTransitionTransferInvalidStateRoot(t *testing.T, client jsonrpc.RPCClient, ethClient *eth.Client) { sink := make(chan *rollup.RollupRollbackStatus) subscription, err := ethClient.Rollup.WatchRollbackStatus(&bind.WatchOpts{}, sink) @@ -91,6 +119,72 @@ func send32C2TBatchWithInvalidStateRoot(t *testing.T, ethClient *eth.Client, wal ToStateID: ref.Uint32(38), } + registeredPubKeyIDs := register32Accounts(t, ethClient, wallets) + encodedTransfers := encodeCreate2Transfers(t, &transfer, registeredPubKeyIDs, 38) + + sendC2TCommitment(t, ethClient, encodedTransfers, 2) +} + +func send32TransfersBatchWithInvalidSignature(t *testing.T, ethClient *eth.Client) { + transfer := models.Transfer{ + TransactionBase: models.TransactionBase{ + FromStateID: 1, + Amount: models.MakeUint256(90), + Fee: models.MakeUint256(10), + }, + ToStateID: 2, + } + + encodedTransfer, err := encoder.EncodeTransferForCommitment(&transfer) + require.NoError(t, err) + postStateRoot := common.Hash{45, 76, 35, 230, 155, 178, 7, 67, 241, 86, 195, 114, 225, 244, 169, 166, 182, 213, 46, 60, 106, 107, 252, 125, 107, 78, 157, 106, 126, 38, 160, 137} + + commitment := models.Commitment{ + Transactions: bytes.Repeat(encodedTransfer, 32), + FeeReceiver: 0, + CombinedSignature: models.Signature{}, + PostStateRoot: postStateRoot, + } + submitTransfersBatch(t, ethClient, []models.Commitment{commitment}, 1) +} + +func send32C2TBatchWithInvalidSignature(t *testing.T, ethClient *eth.Client, wallets []bls.Wallet) { + transfer := models.Create2Transfer{ + TransactionBase: models.TransactionBase{ + FromStateID: 1, + Amount: models.MakeUint256(100), + Fee: models.MakeUint256(10), + }, + ToStateID: ref.Uint32(6), + } + + registeredPubKeyIDs := register32Accounts(t, ethClient, wallets) + encodedTransfers := encodeCreate2Transfers(t, &transfer, registeredPubKeyIDs, 6) + + postStateRoot := common.Hash{9, 165, 135, 45, 162, 158, 64, 129, 26, 232, 17, 209, 169, 198, 175, 189, 42, 40, 119, 15, 11, 78, 238, 158, 35, 163, 205, 164, 23, 120, 249, 253} + + commitment := models.Commitment{ + Transactions: encodedTransfers, + FeeReceiver: 0, + CombinedSignature: models.Signature{}, + PostStateRoot: postStateRoot, + } + submitC2TBatch(t, ethClient, []models.Commitment{commitment}, 1) +} + +func encodeCreate2Transfers(t *testing.T, transfer *models.Create2Transfer, registeredPubKeyIDs []uint32, startStateID uint32) []byte { + encodedTransfers := make([]byte, 0, encoder.Create2TransferLength*len(registeredPubKeyIDs)) + for i := range registeredPubKeyIDs { + transfer.ToStateID = ref.Uint32(startStateID + uint32(i)) + encodedTx, err := encoder.EncodeCreate2TransferForCommitment(transfer, registeredPubKeyIDs[i]) + require.NoError(t, err) + + encodedTransfers = append(encodedTransfers, encodedTx...) + } + return encodedTransfers +} + +func register32Accounts(t *testing.T, ethClient *eth.Client, wallets []bls.Wallet) []uint32 { registrations, unsubscribe, err := ethClient.WatchBatchAccountRegistrations(&bind.WatchOpts{}) require.NoError(t, err) defer unsubscribe() @@ -107,15 +201,5 @@ func send32C2TBatchWithInvalidStateRoot(t *testing.T, ethClient *eth.Client, wal require.NoError(t, err) registeredPubKeyIDs = append(registeredPubKeyIDs, pubKeyIDs...) } - - encodedTransfers := make([]byte, 0, encoder.Create2TransferLength*32) - for i := range registeredPubKeyIDs { - transfer.ToStateID = ref.Uint32(uint32(38 + i)) - encodedTx, err := encoder.EncodeCreate2TransferForCommitment(&transfer, registeredPubKeyIDs[i]) - require.NoError(t, err) - - encodedTransfers = append(encodedTransfers, encodedTx...) - } - - sendC2TCommitment(t, ethClient, encodedTransfers, 2) + return registeredPubKeyIDs }