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

Compare chain id env variable with rpc on startup #733

Merged
merged 6 commits into from
Mar 30, 2020

Conversation

kimpers
Copy link
Contributor

@kimpers kimpers commented Mar 2, 2020

Implements #171

core/core.go Outdated Show resolved Hide resolved
core/core.go Outdated
@@ -711,6 +745,14 @@ func (app *App) Start(ctx context.Context) error {
}
}

// Need to wait for the async ETH RPC call to finish before we know if the chain id is correctly configured
chainIdCheckerWg.Wait()
if err := <-chainIdMismatchErrChan; err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this into a case statement in the above select block.

core/core.go Outdated
go func() {
defer wg.Done()
defer chainIdCheckerWg.Done()
ctx, cancel := context.WithTimeout(context.Background(), ethereumRPCRequestTimeout)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move the actual logic in this go routine into a helper method with a good name? Having it in-lined here feels like the wrong level of abstraction when compared with the rest of it's parent's function logic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use innerCtx instead of context.Background() here.

@kimpers kimpers force-pushed the feature/compare-chain-id-variable-with-rpc branch 2 times, most recently from 8c7b292 to 0eed7bb Compare March 3, 2020 17:54
@albrow
Copy link
Contributor

albrow commented Mar 24, 2020

@kimpers @fabioberger do you mind if I take over this PR to push it over the finish line? This check would actually be really useful and solve a common problem, especially in the browser.

@kimpers
Copy link
Contributor Author

kimpers commented Mar 25, 2020

@kimpers @fabioberger do you mind if I take over this PR to push it over the finish line? This check would actually be really useful and solve a common problem, especially in the browser.

Feel free 👍 Beware that there seems to be a strange race condition with the test, locally on my machine it passes some times and fails other times.

When you figure out what the underlying issue is I'd be very interested to know what causes it.

@albrow albrow force-pushed the feature/compare-chain-id-variable-with-rpc branch from 688fcc3 to a913f61 Compare March 26, 2020 00:34
@albrow albrow marked this pull request as ready for review March 26, 2020 20:40
@albrow
Copy link
Contributor

albrow commented Mar 26, 2020

@kimpers the race condition here was definitely a tricky one. It doesn't directly stem from the code you added, moreso this new code made an existing bug more apparent.

Inside of core.App.Start there is a select statement where we wait for several different goroutines to send an error through their respective error channels (e.g. p2pErrChan, orderWatcherErrChan, or the new chainIDMismatchErrChan which you added in this PR):

select {
		case err := <-p2pErrChan:
			if err != nil {
				log.WithError(err).Error("p2p node exited with error")
				cancel()
				return err
			}
		case err := <-orderWatcherErrChan:
			if err != nil {
				log.WithError(err).Error("order watcher exited with error")
				cancel()
				return err
			}

		// etc...
}

There was an implicit assumption here that if one of the goroutines sent an error through their respective channels, it was time to shut the app down. But that assumption was wrong. There are some cases where a goroutine may send nil through the channel to indicate that it has exited without an error, and in this case we don't shut down the application. The most straightforward example of this is that the Ethereum RPC rate limiter sends nil through ethRPCRateLimiterErrChan immediately if Ethereum RPC rate limiting has been disabled. In the old code this would cause us to exit the select statement, and therefore not receive errors from any of the other (still running) goroutines.

I fixed this by wrapping the select statement in a for loop. This way if one of the goroutines exits and sends nil through its respective error channel, we keep waiting to receive errors on the other channels and then handle them appropriately.

@albrow
Copy link
Contributor

albrow commented Mar 26, 2020

@fabio This PR is now ready for review. I changed it to no longer be a draft.

@albrow albrow self-assigned this Mar 26, 2020
@albrow albrow changed the title Compare chain id env variable with rpc on startup WIP Compare chain id env variable with rpc on startup Mar 30, 2020
@albrow albrow force-pushed the feature/compare-chain-id-variable-with-rpc branch from a47cd81 to 2f04a4f Compare March 30, 2020 20:45
@albrow albrow merged commit 9ac6b5e into development Mar 30, 2020
@albrow albrow deleted the feature/compare-chain-id-variable-with-rpc branch March 30, 2020 20:57
albrow added a commit that referenced this pull request Apr 20, 2020
* Periodically rerun ordersync instead of running it only once on startup (#764)

* Periodically rerun ordersync instead of running it only once on startup

* Add a random jitter to ordersync delay

* Change ordersyncApproxDelay to 1 hour

* Add log message when starting ordersync service

* Improve comment clarity

* Update ganache addresses (#765)

* Update Ganache DevUtils address

* Update Ganache snapshot version

* Update Dummy ERC1155 contract address

* Fix typo

* Add a polyfill for WebAssembly.instantateStreaming (#770)

* Add a polyfill for WebAssembly.instantateStreaming

* Fix linter error

* Update changelog

* Compare chain id env variable with rpc on startup (#733)

* ADDS check that configured chain id matches RPC WIP

* FIXES async error channel race condition and clean up code

* FIXES remove unnecessary wait group, move chain id fetch into helper

* FIXES use ParseBig256 for parsing chain id response from eth_chainId

* Fix race condition in core.App.Start

* Update changelog

Co-authored-by: Alex Browne <stephenalexbrowne@gmail.com>

* Add custom order filters documentation (#768)

* Add custom order filters docs

* Fix nits

* Add doc to menu

* Improve limitations description

* Add example filters

* Merge the two example sections

* Fully Automate Document Generation (#771)

* Automated the documentation update step

* Ignored exported fields that shouldn't be documented

* Addressed @albrow's review feedback

* Revert "Addressed @albrow's review feedback"

This reverts commit 75f48c0.

* [expirationwatch] Fixes consistency bug (#773)

* Fix small bug in expirationwatcher

* Add a test to ensure that a "barely expired" order will be pruned

* Update CHANGELOG with PR number

* Fixes race condition in orderwatcher test (#774)

* Improve scenario package (#779)

* WIP Improve scenario package

* Create orderopts package. Update scenario and ordervalidator to use it

* Fix remaining ordervalidator tests

* Update most orderwatcher tests

* Support setting taker state and enable remaining orderwatcher tests

* Fix remaining tests (only MultiAsset disabled)

* Uncomment test that depends on on-chain state for a StaticCall order

* Fix browser integration test

* Remove unneeded ethClient parameter in a few places

* Move sleep statement in order_watcher_test.go

* Address Alex Towle's feedback

* Remove old comment

* Revalidate affected orders regardless of ERC721 approval operator (#782)

* Increase sleep duration in TestPingPong (#785)

* scenario: Add ability to create multiple orders in a single batch (#784)

* scenario: Add ability to create multiple orders in a single batch

* Fix some typos

* Update changelog for PR #782 (#786)

* p2p: Don't call HandleMessages if there are no messages to handle (#787)

* Add missing regression test for ordersync pagination subprotocol (#788)

* Add missing regression test for ordersync pagination subprotocol

Also fixes a minor bug where the exponential backoff for ordersync
was too long in some circumstances.

* Remove some duplicated code in core_test.go

* Set versions to 9.3.0

Co-authored-by: Fabio B <me@fabioberger.com>
Co-authored-by: Kim Persson <kimpers@users.noreply.github.com>
Co-authored-by: Alex Towle <jalextowle@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants