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

Commit

Permalink
Specify gas limit for dispute transactions (#305)
Browse files Browse the repository at this point in the history
* Set gas limit for signature disputes

* Set gas limit for transition disputes

* Add signature and transition dispute gas limits options to config

* Use config values to set signature and transition gas limits

* Read postgres user and password from env variables in test config

* Make Postgres Host, Port and Name configurable in test config as well

* Fix getUint64 fallback param type

* Update config.example.yaml

* Add TransitionDisputeGasLimit and SignatureDisputeGasLimit to Client

* Make ClientConfig gas limit fields optional for consistency

Also, gas limit = 0 means: use estimated gas

* Fix ClientConfig comments

Co-authored-by: Michał Sieczkowski <[email protected]>
  • Loading branch information
duckception and Michał Sieczkowski authored Aug 25, 2021
1 parent 9cc4d63 commit 4fd9733
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 54 deletions.
22 changes: 16 additions & 6 deletions commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func bootstrapFromChainState(
importedChainState := newChainStateFromChainSpec(chainSpec)

if dbChainState == nil {
return bootstrapChainStateAndCommander(chain, storage, importedChainState)
return bootstrapChainStateAndCommander(chain, storage, importedChainState, cfg.Rollup)
}

err = compareChainStates(importedChainState, dbChainState)
Expand All @@ -212,7 +212,7 @@ func bootstrapFromChainState(
}

log.Printf("Continuing from saved state on ChainID = %s", importedChainState.ChainID.String())
return createClientFromChainState(chain, importedChainState)
return createClientFromChainState(chain, importedChainState, cfg.Rollup)
}

func compareChainStates(chainStateA, chainStateB *models.ChainState) error {
Expand Down Expand Up @@ -241,14 +241,15 @@ func bootstrapChainStateAndCommander(
chain deployer.ChainConnection,
storage *st.Storage,
importedChainState *models.ChainState,
cfg *config.RollupConfig,
) (*eth.Client, error) {
chainID := chain.GetChainID()
if chainID != importedChainState.ChainID {
return nil, inconsistentFileChainIDErr
}

log.Printf("Bootstrapping genesis state from chain spec file")
return setGenesisStateAndCreateClient(chain, storage, importedChainState)
return setGenesisStateAndCreateClient(chain, storage, importedChainState, cfg)
}

func bootstrapFromRemoteState(
Expand All @@ -265,13 +266,14 @@ func bootstrapFromRemoteState(
return nil, inconsistentRemoteChainIDErr
}

return setGenesisStateAndCreateClient(chain, storage, fetchedChainState)
return setGenesisStateAndCreateClient(chain, storage, fetchedChainState, cfg.Rollup)
}

func setGenesisStateAndCreateClient(
chain deployer.ChainConnection,
storage *st.Storage,
chainState *models.ChainState,
cfg *config.RollupConfig,
) (*eth.Client, error) {
err := PopulateGenesisAccounts(storage, chainState.GenesisAccounts)
if err != nil {
Expand All @@ -283,7 +285,7 @@ func setGenesisStateAndCreateClient(
return nil, err
}

client, err := createClientFromChainState(chain, chainState)
client, err := createClientFromChainState(chain, chainState, cfg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -321,7 +323,11 @@ func fetchChainStateFromRemoteNode(url string) (*models.ChainState, error) {
}, nil
}

func createClientFromChainState(chain deployer.ChainConnection, chainState *models.ChainState) (*eth.Client, error) {
func createClientFromChainState(
chain deployer.ChainConnection,
chainState *models.ChainState,
cfg *config.RollupConfig,
) (*eth.Client, error) {
err := logChainState(chainState)
if err != nil {
return nil, err
Expand All @@ -340,6 +346,10 @@ func createClientFromChainState(chain deployer.ChainConnection, chainState *mode
ChainState: *chainState,
Rollup: rollupContract,
AccountRegistry: accountRegistry,
ClientConfig: eth.ClientConfig{
TransitionDisputeGasLimit: ref.Uint64(cfg.TransitionDisputeGasLimit),
SignatureDisputeGasLimit: ref.Uint64(cfg.SignatureDisputeGasLimit),
},
})
if err != nil {
return nil, err
Expand Down
14 changes: 12 additions & 2 deletions commander/executor/dispute_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,19 @@ func (t *TransactionExecutor) DisputeTransition(
}

if batch.Type == txtype.Transfer {
err = t.client.DisputeTransitionTransfer(&batch.ID, previousCommitmentProof, targetCommitmentProof, merkleProofs)
err = t.client.DisputeTransitionTransfer(
&batch.ID,
previousCommitmentProof,
targetCommitmentProof,
merkleProofs,
)
} else {
err = t.client.DisputeTransitionCreate2Transfer(&batch.ID, previousCommitmentProof, targetCommitmentProof, merkleProofs)
err = t.client.DisputeTransitionCreate2Transfer(
&batch.ID,
previousCommitmentProof,
targetCommitmentProof,
merkleProofs,
)
}
return err
}
2 changes: 2 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# max_txs_per_commitment: 32
# min_commitments_per_batch: 1
# max_commitments_per_batch: 32
# transition_dispute_gas_limit: 5000000
# signature_dispute_gas_limit: 7500000
# commitment_loop_interval: 500ms
# batch_loop_interval: 500ms
#
Expand Down
59 changes: 34 additions & 25 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (

"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/utils"
"github.com/Worldcoin/hubble-commander/utils/ref"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

const SimulatorChainID = 1337
const (
SimulatorChainID = 1337
DefaultTransitionDisputeGasLimit = uint64(5_000_000)
DefaultSignatureDisputeGasLimit = uint64(7_500_000)
)

func setupViper() {
viper.SetConfigFile(getConfigPath())
Expand Down Expand Up @@ -42,15 +45,17 @@ func GetConfig() *Config {
ChainSpecPath: getStringOrNil("bootstrap.chain_spec_path"),
},
Rollup: &RollupConfig{
SyncSize: getUint32("rollup.sync_size", 50),
FeeReceiverPubKeyID: getUint32("rollup.fee_receiver_pub_key_id", 0),
MinTxsPerCommitment: getUint32("rollup.min_txs_per_commitment", 1),
MaxTxsPerCommitment: getUint32("rollup.max_txs_per_commitment", 32),
MinCommitmentsPerBatch: getUint32("rollup.min_commitments_per_batch", 1),
MaxCommitmentsPerBatch: getUint32("rollup.max_commitments_per_batch", 32),
CommitmentLoopInterval: getDuration("rollup.commitment_loop_interval", 500*time.Millisecond),
BatchLoopInterval: getDuration("rollup.batch_loop_interval", 500*time.Millisecond),
DisableSignatures: getBool("rollup.disable_signatures", false),
SyncSize: getUint32("rollup.sync_size", 50),
FeeReceiverPubKeyID: getUint32("rollup.fee_receiver_pub_key_id", 0),
MinTxsPerCommitment: getUint32("rollup.min_txs_per_commitment", 1),
MaxTxsPerCommitment: getUint32("rollup.max_txs_per_commitment", 32),
MinCommitmentsPerBatch: getUint32("rollup.min_commitments_per_batch", 1),
MaxCommitmentsPerBatch: getUint32("rollup.max_commitments_per_batch", 32),
TransitionDisputeGasLimit: getUint64("rollup.transition_dispute_gas_limit", DefaultTransitionDisputeGasLimit),
SignatureDisputeGasLimit: getUint64("rollup.signature_dispute_gas_limit", DefaultSignatureDisputeGasLimit),
CommitmentLoopInterval: getDuration("rollup.commitment_loop_interval", 500*time.Millisecond),
BatchLoopInterval: getDuration("rollup.batch_loop_interval", 500*time.Millisecond),
DisableSignatures: getBool("rollup.disable_signatures", false),
},
API: &APIConfig{
Version: "0.0.1",
Expand All @@ -72,6 +77,8 @@ func GetConfig() *Config {
}

func GetTestConfig() *Config {
setupViper()

return &Config{
Log: &LogConfig{
Level: log.InfoLevel,
Expand All @@ -84,26 +91,28 @@ func GetTestConfig() *Config {
ChainSpecPath: nil,
},
Rollup: &RollupConfig{
SyncSize: 50,
FeeReceiverPubKeyID: 0,
MinTxsPerCommitment: 2,
MaxTxsPerCommitment: 2,
MinCommitmentsPerBatch: 1,
MaxCommitmentsPerBatch: 32,
CommitmentLoopInterval: 500 * time.Millisecond,
BatchLoopInterval: 500 * time.Millisecond,
DisableSignatures: true,
SyncSize: 50,
FeeReceiverPubKeyID: 0,
MinTxsPerCommitment: 2,
MaxTxsPerCommitment: 2,
MinCommitmentsPerBatch: 1,
MaxCommitmentsPerBatch: 32,
TransitionDisputeGasLimit: DefaultTransitionDisputeGasLimit,
SignatureDisputeGasLimit: DefaultSignatureDisputeGasLimit,
CommitmentLoopInterval: 500 * time.Millisecond,
BatchLoopInterval: 500 * time.Millisecond,
DisableSignatures: true,
},
API: &APIConfig{
Version: "dev-0.0.1",
Port: "8080",
},
Postgres: &PostgresConfig{
Host: nil,
Port: nil,
Name: "hubble_test",
User: ref.String("hubble"),
Password: ref.String("root"),
Host: getStringOrNil("postgres.host"),
Port: getStringOrNil("postgres.port"),
Name: getString("postgres.name", "hubble_test"),
User: getStringOrNil("postgres.user"),
Password: getStringOrNil("postgres.password"),
MigrationsPath: getMigrationsPath(),
},
Badger: &BadgerConfig{
Expand Down
5 changes: 5 additions & 0 deletions config/get_viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func getUint32(key string, fallback uint32) uint32 {
return viper.GetUint32(key)
}

func getUint64(key string, fallback uint64) uint64 {
viper.SetDefault(key, fallback)
return viper.GetUint64(key)
}

func getBool(key string, fallback bool) bool {
viper.SetDefault(key, fallback)
return viper.GetBool(key)
Expand Down
20 changes: 11 additions & 9 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ type BootstrapConfig struct {
}

type RollupConfig struct {
SyncSize uint32
FeeReceiverPubKeyID uint32
MinTxsPerCommitment uint32
MaxTxsPerCommitment uint32
MinCommitmentsPerBatch uint32
MaxCommitmentsPerBatch uint32
CommitmentLoopInterval time.Duration
BatchLoopInterval time.Duration
DisableSignatures bool
SyncSize uint32
FeeReceiverPubKeyID uint32
MinTxsPerCommitment uint32
MaxTxsPerCommitment uint32
MinCommitmentsPerBatch uint32
MaxCommitmentsPerBatch uint32
TransitionDisputeGasLimit uint64
SignatureDisputeGasLimit uint64
CommitmentLoopInterval time.Duration
BatchLoopInterval time.Duration
DisableSignatures bool
}

type APIConfig struct {
Expand Down
13 changes: 11 additions & 2 deletions eth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/Worldcoin/hubble-commander/bls"
"github.com/Worldcoin/hubble-commander/config"
"github.com/Worldcoin/hubble-commander/contracts/accountregistry"
"github.com/Worldcoin/hubble-commander/contracts/rollup"
"github.com/Worldcoin/hubble-commander/eth/deployer"
Expand All @@ -23,8 +24,10 @@ type NewClientParams struct {
}

type ClientConfig struct {
TxTimeout *time.Duration // default 60s
StakeAmount *models.Uint256 // default 0.1 ether
TxTimeout *time.Duration // default 60s
StakeAmount *models.Uint256 // default 0.1 ether
TransitionDisputeGasLimit *uint64 // default 5_000_000 gas
SignatureDisputeGasLimit *uint64 // default 7_500_000 gas
}

type Client struct {
Expand Down Expand Up @@ -72,4 +75,10 @@ func fillWithDefaults(c *ClientConfig) {
if c.StakeAmount == nil {
c.StakeAmount = models.NewUint256(1e17)
}
if c.TransitionDisputeGasLimit == nil {
c.TransitionDisputeGasLimit = ref.Uint64(config.DefaultTransitionDisputeGasLimit)
}
if c.SignatureDisputeGasLimit == nil {
c.SignatureDisputeGasLimit = ref.Uint64(config.DefaultSignatureDisputeGasLimit)
}
}
24 changes: 14 additions & 10 deletions eth/dispute_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ func (c *Client) DisputeSignatureTransfer(
targetProof *models.TransferCommitmentInclusionProof,
signatureProof *models.SignatureProof,
) error {
transaction, err := c.rollup().DisputeSignatureTransfer(
batchID.ToBig(),
*TransferProofToCalldata(targetProof),
*signatureProofToCalldata(signatureProof),
)
transaction, err := c.rollup().
WithGasLimit(*c.config.SignatureDisputeGasLimit).
DisputeSignatureTransfer(
batchID.ToBig(),
*TransferProofToCalldata(targetProof),
*signatureProofToCalldata(signatureProof),
)
if err != nil {
return handleDisputeSignatureError(err)
}
Expand All @@ -35,11 +37,13 @@ func (c *Client) DisputeSignatureCreate2Transfer(
targetProof *models.TransferCommitmentInclusionProof,
signatureProof *models.SignatureProofWithReceiver,
) error {
transaction, err := c.rollup().DisputeSignatureCreate2Transfer(
batchID.ToBig(),
*TransferProofToCalldata(targetProof),
*signatureProofWithReceiverToCalldata(signatureProof),
)
transaction, err := c.rollup().
WithGasLimit(*c.config.SignatureDisputeGasLimit).
DisputeSignatureCreate2Transfer(
batchID.ToBig(),
*TransferProofToCalldata(targetProof),
*signatureProofWithReceiverToCalldata(signatureProof),
)
if err != nil {
return handleDisputeSignatureError(err)
}
Expand Down
2 changes: 2 additions & 0 deletions eth/dispute_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (c *Client) DisputeTransitionTransfer(
proofs []models.StateMerkleProof,
) error {
transaction, err := c.rollup().
WithGasLimit(*c.config.TransitionDisputeGasLimit).
DisputeTransitionTransfer(
batchID.ToBig(),
*CommitmentProofToCalldata(previous),
Expand All @@ -54,6 +55,7 @@ func (c *Client) DisputeTransitionCreate2Transfer(
proofs []models.StateMerkleProof,
) error {
transaction, err := c.rollup().
WithGasLimit(*c.config.TransitionDisputeGasLimit).
DisputeTransitionCreate2Transfer(
batchID.ToBig(),
*CommitmentProofToCalldata(previous),
Expand Down

0 comments on commit 4fd9733

Please sign in to comment.