Skip to content
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

Improve OP_RETURN handling in interpreter #98

Open
tigh-latte opened this issue Dec 15, 2021 · 0 comments
Open

Improve OP_RETURN handling in interpreter #98

tigh-latte opened this issue Dec 15, 2021 · 0 comments
Labels
bug Something isn't working interpreter script interpreter

Comments

@tigh-latte
Copy link
Contributor

Currently, OP_RETURN after genesis are handled using a special case error code, ErrOK, which results in some pretty funky and unintuitive code.

So for example, on OP_RETURN, we check to see if we're in a post genesis context, and if we are and aren't in a conditional, we return success():

func opcodeReturn(op *ParsedOpcode, t *thread) error {
	if !t.afterGenesis {
		return errs.NewError(errs.ErrEarlyReturn, "script returned early")
	}

	t.earlyReturnAfterGenesis = true
	if len(t.condStack) == 0 {
		// Terminate the execution as successful. The remaining of the script does not affect the validity (even in
		// presence of unbalanced IFs, invalid opcodes etc)
		return success()
	}

	return nil
}

Success is just a wrapping for ErrOK:

func success() errs.Error {
	return errs.NewError(errs.ErrOK, "success")
}

Then, in the thread executor, when we execute the opcode we check for this special case error:

if err := t.executeOpcode(opcode); err != nil {
	if ok := errs.IsErrorCode(err, errs.ErrOK); ok {
		// If returned early, move onto the next script
		t.shiftScript()
		return t.scriptIdx >= len(t.scripts), nil
	}
	return true, err
}

This creates sections in our codebase where an error mightn't actually be an error, and it would be really nice if we could move away from this, even if it was something as simple as a state flag on interpreter.thread.

@tigh-latte tigh-latte added the bug Something isn't working label Dec 15, 2021
@jadwahab jadwahab added the interpreter script interpreter label Dec 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working interpreter script interpreter
Projects
None yet
Development

No branches or pull requests

2 participants