Skip to content

Commit

Permalink
Only use a single context.Background()
Browse files Browse the repository at this point in the history
If multiple context.Background() objects are used there is a chance that transactions with multiple nonces will be created.  To prevent this from happening, ensure only a single context.Background() is passed throughout the program.
  • Loading branch information
joshuacolvin0 committed Nov 19, 2020
1 parent 51566b2 commit f213dc4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {
// Enable line numbers in logging
log.SetFlags(log.LstdFlags | log.Lshortfile)

ctx := context.Background()
fs := flag.NewFlagSet("", flag.ContinueOnError)
walletArgs := utils.AddWalletFlags(fs)
rpcVars := utils2.AddRPCFlags(fs)
Expand Down Expand Up @@ -89,7 +90,7 @@ func main() {
log.Println("Aggregator submitting batches from address", auth.From)

if err := arbbridge.WaitForBalance(
context.Background(),
ctx,
ethbridge.NewEthClient(ethclint),
common.Address{},
common.NewAddressFromEth(auth.From),
Expand All @@ -108,7 +109,7 @@ func main() {
dbPath := filepath.Join(rollupArgs.ValidatorFolder, "checkpoint_db")

if err := rpc.LaunchAggregator(
context.Background(),
ctx,
ethclint,
rollupArgs.Address,
contractFile,
Expand Down
2 changes: 1 addition & 1 deletion packages/arb-tx-aggregator/rpc/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func LaunchAggregator(
if err != nil {
return err
}
inboxAddress, err := rollupContract.InboxAddress(context.Background())
inboxAddress, err := rollupContract.InboxAddress(ctx)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions packages/arb-validator/cmd/arb-validator/arb-validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func main() {
}

func createRollupChain() error {
ctx := context.Background()
createCmd := flag.NewFlagSet("validate", flag.ExitOnError)
walletVars := utils.AddWalletFlags(createCmd)
tokenAddressString := createCmd.String("staketoken", "", "staketoken=TokenAddress")
Expand Down Expand Up @@ -102,7 +103,7 @@ func createRollupChain() error {
// Rollup creation
client := ethbridge.NewEthAuthClient(ethclint, auth)

if err := arbbridge.WaitForBalance(context.Background(), client, common.Address{}, common.NewAddressFromEth(auth.From)); err != nil {
if err := arbbridge.WaitForBalance(ctx, client, common.Address{}, common.NewAddressFromEth(auth.From)); err != nil {
return err
}

Expand All @@ -126,7 +127,7 @@ func createRollupChain() error {
}

address, _, err := factory.CreateRollup(
context.Background(),
ctx,
mach.Hash(),
params,
common.Address{},
Expand Down
14 changes: 8 additions & 6 deletions packages/arb-validator/cmdhelper/cmdhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func ValidateRollupChain(
contractFile string, dbPath string,
) (*rollupmanager.Manager, error),
) error {
ctx := context.Background()
// Check number of args

validateCmd := flag.NewFlagSet("validate", flag.ExitOnError)
Expand Down Expand Up @@ -94,17 +95,17 @@ func ValidateRollupChain(
return err
}

params, err := rollup.GetParams(context.Background())
params, err := rollup.GetParams(ctx)
if err != nil {
return err
}

if err := arbbridge.WaitForBalance(context.Background(), client, params.StakeToken, common.NewAddressFromEth(auth.From)); err != nil {
if err := arbbridge.WaitForBalance(ctx, client, params.StakeToken, common.NewAddressFromEth(auth.From)); err != nil {
return err
}

validatorListener := chainlistener.NewValidatorChainListener(
context.Background(),
ctx,
rollupArgs.Address,
rollup,
)
Expand All @@ -126,8 +127,8 @@ func ValidateRollupChain(
if err != nil {
return err
}
manager.AddListener(&chainlistener.AnnouncerListener{})
manager.AddListener(validatorListener)
manager.AddListener(ctx, &chainlistener.AnnouncerListener{})
manager.AddListener(ctx, validatorListener)

wait := make(chan bool)
<-wait
Expand All @@ -145,6 +146,7 @@ func ObserveRollupChain(
contractFile string, dbPath string,
) (*rollupmanager.Manager, error),
) error {
ctx := context.Background()
// Check number of args
validateCmd := flag.NewFlagSet("observe", flag.ExitOnError)
quietFlag := validateCmd.Bool(
Expand Down Expand Up @@ -188,7 +190,7 @@ func ObserveRollupChain(
return err
}
if !*quietFlag {
manager.AddListener(&chainlistener.AnnouncerListener{})
manager.AddListener(ctx, &chainlistener.AnnouncerListener{})
}

wait := make(chan bool)
Expand Down
4 changes: 2 additions & 2 deletions packages/arb-validator/rollupmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,12 @@ func CreateManagerAdvanced(
return man, nil
}

func (man *Manager) AddListener(listener chainlistener.ChainListener) {
func (man *Manager) AddListener(ctx context.Context, listener chainlistener.ChainListener) {
man.Lock()
defer man.Unlock()
man.listeners = append(man.listeners, listener)
if man.activeChain != nil {
man.activeChain.AddListener(context.Background(), listener)
man.activeChain.AddListener(ctx, listener)
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/fibgo/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func setupValidators(
return err
}

manager.AddListener(&chainlistener.AnnouncerListener{Prefix: "validator " + client.Address().String() + ": "})
manager.AddListener(ctx, &chainlistener.AnnouncerListener{Prefix: "validator " + client.Address().String() + ": "})

validatorListener := chainlistener.NewValidatorChainListener(
context.Background(),
Expand All @@ -122,7 +122,7 @@ func setupValidators(
if err != nil {
return err
}
manager.AddListener(validatorListener)
manager.AddListener(ctx, validatorListener)
managers = append(managers, manager)
}

Expand Down

0 comments on commit f213dc4

Please sign in to comment.