Skip to content

Improve OP_RETURN handling in interpreter #98

Open
@tigh-latte

Description

@tigh-latte

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginterpreterscript interpreter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions