-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
simulation: Write logs on panic #2285
Merged
+68
−21
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,13 @@ package simulation | |
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"math" | ||
"math/rand" | ||
"os" | ||
"os/signal" | ||
"runtime/debug" | ||
"sort" | ||
"strings" | ||
"syscall" | ||
|
@@ -26,9 +28,9 @@ import ( | |
func Simulate( | ||
t *testing.T, app *baseapp.BaseApp, appStateFn func(r *rand.Rand, keys []crypto.PrivKey, accs []sdk.AccAddress) json.RawMessage, ops []Operation, setups []RandSetup, | ||
invariants []Invariant, numBlocks int, blockSize int, commit bool, | ||
) { | ||
) error { | ||
time := time.Now().UnixNano() | ||
SimulateFromSeed(t, app, appStateFn, time, ops, setups, invariants, numBlocks, blockSize, commit) | ||
return SimulateFromSeed(t, app, appStateFn, time, ops, setups, invariants, numBlocks, blockSize, commit) | ||
} | ||
|
||
func initChain(r *rand.Rand, keys []crypto.PrivKey, accs []sdk.AccAddress, setups []RandSetup, app *baseapp.BaseApp, | ||
|
@@ -56,7 +58,9 @@ func randTimestamp(r *rand.Rand) time.Time { | |
func SimulateFromSeed( | ||
tb testing.TB, app *baseapp.BaseApp, appStateFn func(r *rand.Rand, keys []crypto.PrivKey, accs []sdk.AccAddress) json.RawMessage, seed int64, ops []Operation, setups []RandSetup, | ||
invariants []Invariant, numBlocks int, blockSize int, commit bool, | ||
) { | ||
) (simError error) { | ||
// in case we have to end early, don't os.Exit so that we can run cleanup code. | ||
stopEarly := false | ||
testingMode, t, b := getTestingMode(tb) | ||
fmt.Printf("Starting SimulateFromSeed with randomness created with seed %d\n", int(seed)) | ||
r := rand.New(rand.NewSource(seed)) | ||
|
@@ -79,12 +83,12 @@ func SimulateFromSeed( | |
|
||
// Setup code to catch SIGTERM's | ||
c := make(chan os.Signal) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) | ||
go func() { | ||
<-c | ||
fmt.Printf("Exiting early due to SIGTERM, on block %d, operation %d\n", header.Height, opCount) | ||
DisplayEvents(events) | ||
os.Exit(128 + int(syscall.SIGTERM)) | ||
fmt.Printf("Exiting early due to SIGTERM/SIGINT, on block %d, operation %d\n", header.Height, opCount) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not: sig := <-c
fmt.Printf("Exiting early due to %s, on block %d, operation %d\n", sig, header.Height, opCount) |
||
simError = errors.New("Exited due to SIGTERM/SIGINT") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
stopEarly = true | ||
}() | ||
|
||
var pastTimes []time.Time | ||
|
@@ -95,15 +99,27 @@ func SimulateFromSeed( | |
operationQueue := make(map[int][]Operation) | ||
var blockLogBuilders []*strings.Builder | ||
|
||
if !testingMode { | ||
b.ResetTimer() | ||
} else { | ||
if testingMode { | ||
blockLogBuilders = make([]*strings.Builder, numBlocks) | ||
} | ||
displayLogs := logPrinter(testingMode, blockLogBuilders) | ||
blockSimulator := createBlockSimulator(testingMode, tb, t, event, invariants, ops, operationQueue, numBlocks, displayLogs) | ||
if !testingMode { | ||
b.ResetTimer() | ||
} else { | ||
// Recover logs in case of panic | ||
defer func() { | ||
if r := recover(); r != nil { | ||
fmt.Println("Panic with err\n", r) | ||
stackTrace := string(debug.Stack()) | ||
fmt.Println(stackTrace) | ||
displayLogs() | ||
simError = fmt.Errorf("Simulation halted due to panic on block %d", header.Height) | ||
} | ||
}() | ||
} | ||
|
||
for i := 0; i < numBlocks; i++ { | ||
for i := 0; i < numBlocks && !stopEarly; i++ { | ||
// Log the header time for future lookup | ||
pastTimes = append(pastTimes, header.Time) | ||
pastSigningValidators = append(pastSigningValidators, request.LastCommitInfo.Validators) | ||
|
@@ -122,10 +138,9 @@ func SimulateFromSeed( | |
|
||
// Run queued operations. Ignores blocksize if blocksize is too small | ||
numQueuedOpsRan := runQueuedOperations(operationQueue, int(header.Height), tb, r, app, ctx, keys, logWriter, displayLogs, event) | ||
opCount += numQueuedOpsRan | ||
thisBlockSize -= numQueuedOpsRan | ||
operations := blockSimulator(thisBlockSize, r, app, ctx, keys, header, logWriter) | ||
opCount += operations | ||
opCount += operations + numQueuedOpsRan | ||
|
||
res := app.EndBlock(abci.RequestEndBlock{}) | ||
header.Height++ | ||
|
@@ -146,9 +161,13 @@ func SimulateFromSeed( | |
// Update the validator set | ||
validators = updateValidators(tb, r, validators, res.ValidatorUpdates, event) | ||
} | ||
|
||
if stopEarly { | ||
DisplayEvents(events) | ||
return | ||
} | ||
fmt.Printf("\nSimulation complete. Final height (blocks): %d, final time (seconds), : %v, operations ran %d\n", header.Height, header.Time, opCount) | ||
DisplayEvents(events) | ||
return nil | ||
} | ||
|
||
// Returns a function to simulate blocks. Written like this to avoid constant parameters being passed everytime, to minimize | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linting was failing here, so I just ran make format.