Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Measure gas usage by signature disputes (#285)
Browse files Browse the repository at this point in the history
* Measure transfers dispute signature gas usage

* Measure create2Transfers dispute signature gas usage

* Restore E2E build tag

Co-authored-by: Michał Sieczkowski <[email protected]>
  • Loading branch information
b-tarczynski and Michał Sieczkowski authored Aug 2, 2021
1 parent 45a0a71 commit 6acb45c
Showing 1 changed file with 95 additions and 11 deletions.
106 changes: 95 additions & 11 deletions e2e/dispute_gas_measure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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
}

0 comments on commit 6acb45c

Please sign in to comment.