/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.dao.state.blockchain;

import io.bisq.generated.protobuffer.PB;

import lombok.Data;

import javax.annotation.concurrent.Immutable;

/**
 * The base class for RawBlock and Block containing the common immutable bitcoin
 * blockchain specific data.
 */
@Immutable
@Data
public abstract class BaseBlock {
    protected final int height;
    protected final long time; // in ms
    protected final String hash;
    protected final String previousBlockHash;

    BaseBlock(int height, long time, String hash, String previousBlockHash) {
        this.height = height;
        this.time = time;
        this.hash = hash;
        this.previousBlockHash = previousBlockHash;
    }


    ///////////////////////////////////////////////////////////////////////////////////////////
    // PROTO BUFFER
    ///////////////////////////////////////////////////////////////////////////////////////////

    PB.BaseBlock.Builder getBaseBlockBuilder() {
        return PB.BaseBlock.newBuilder()
                .setHeight(height)
                .setTime(time)
                .setHash(hash)
                .setPreviousBlockHash(previousBlockHash);
    }

    @Override
    public String toString() {
        return "BaseBlock{" +
                "\n     height=" + height +
                ",\n     time=" + time +
                ",\n     hash='" + hash + '\'' +
                ",\n     previousBlockHash='" + previousBlockHash + '\'' +
                "\n}";
    }
}

@@ -769,21 +769,83 @@
 ort 
-lombok.Data;%0A
+java.util.Optional;%0A%0Aimport lombok.Data;%0A%0Aimport javax.annotation.Nullable;
 %0Aimp
@@ -1158,24 +1158,82 @@
 tring hash;%0A
+    @Nullable // in case of first block in the blockchain%0A
     protecte
@@ -1757,22 +1757,46 @@
         
-return
+PB.BaseBlock.Builder builder =
  PB.Base
@@ -1910,16 +1910,17 @@
 sh(hash)
+;
 %0A       
@@ -1924,57 +1924,113 @@
     
-        .setPreviousBlockHash(previousBlockHash);
+Optional.ofNullable(previousBlockHash).ifPresent(builder::setPreviousBlockHash);%0A        return builder;%0A
 %0A   

Fix nullpointer