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

toString method called by the debugger should not change the state of the BtcBlock object #6

Open
SergioDemianLerner opened this issue Nov 27, 2017 · 0 comments

Comments

@SergioDemianLerner
Copy link

SergioDemianLerner commented Nov 27, 2017

The method toString is called by the debugger many times during a debug session, even if the debug window is not open.
The BtcBlock.java class implements the following code:

    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append(" block: \n");
        s.append("   hash: ").append(getHashAsString()).append('\n');

But the method getHashAsString modifies the internal state:

    public String getHashAsString() {
        return getHash().toString();
    }
  @Override
    public Sha256Hash getHash() {
        if (hash == null)
            hash = calculateHash();
        return hash;
    }

Therefore the method getHashAsString / getHash methods should be replaced by:

    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append(" block: \n");
        s.append("   hash: ").append(getHashAsStringDebug()).append('\n');

.....
    public String getHashAsStringDebug() {
        return getHash().toString();
    }
....
    public Sha256Hash getHashDebug() {
        Sha256Hash  localHash = calculateHash();
        return localHash;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant