##
##  quorum.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         quorum
Summary:      Quorum Blockchain Service
URL:          https://www.jpmorgan.com/quorum
Vendor:       JP Morgan Chase
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        P2P
License:      LGPL
Version:      2.5.0
Release:      20200304

#   package options
%option       with_private   yes
%option       with_bootnode  yes

#   list of sources
Source0:      https://github.com/jpmorganchase/quorum/archive/v%{version}.tar.gz
Source1:      quorum-genesis.json
Source2:      quorum-geth.conf
Source3:      rc.quorum

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go
PreReq:       OpenPKG, openpkg >= 20160101, apg
%if "%{with_private}" == "yes" && "%{with_bootnode}" == "yes"
PreReq:       quorum-bootnode
%endif

%description
    Quorum is a decentralized platform that runs smart contracts,
    applications that run exactly as programmed without possibility of
    downtime, censorship, fraud or third party interference. Quorum is
    a fork of the Ethereum code-base and adds: Privacy: Quorum supports
    private transactions and private contracts through public/private
    state separation and utilising Constellation, a peer-to-peer
    encrypted message exchange for directed transfer of private data
    to network participants; Alternative Consensus Mechanisms: with
    no need for POW/POS in a permissioned network, Quorum instead
    offers multiple consensus mechanisms that are more appropriate
    for consortium chains: Raft-based Consensus, a consensus model
    for faster blocktimes, transaction finality, and on-demand block
    creation and Istanbul BFT, a PBFT-inspired consensus algorithm with
    transaction finality; Peer Permissioning: node/peer permissioning
    using smart contracts, ensuring only known parties can join the
    network; and Higher Performance: Quorum offers significantly higher
    performance than public Ethereum.

%track
    prog quorum = {
        version   = %{version}
        url       = https://github.com/jpmorganchase/quorum/releases
        regex     = v(__VER__)\.tar\.gz
    }

%prep
    %setup -q

%build
    #   build program
    %{l_make} %{l_mflags} all

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/quorum \
        $RPM_BUILD_ROOT%{l_prefix}/var/quorum/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/quorum/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/quorum/data/ethash

    #   install program
    for prog in build/bin/*; do
        name=`echo $prog | sed -e 's;build/bin/;quorum-;'`
        %{l_shtool} install -c -s -m 775 \
            $prog $RPM_BUILD_ROOT%{l_prefix}/bin/$name
    done
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/quorum-bootnode

    #   install default configuration files
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE quorum-geth.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/quorum/
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE quorum-genesis.json} $RPM_BUILD_ROOT%{l_prefix}/etc/quorum/

    #   install run-command script
    flags=""
%if "%{with_private}" == "yes"
        flags="--mine --etherbase=0x0000000000000000000000000000000000000000"
%endif
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@flags@;$flags;" \
        %{SOURCE rc.quorum} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/quorum' \
        '%config %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/quorum/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/quorum/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/quorum/*/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   determine bootnodes
%if "%{with_private}" == "yes"
%if "%{with_bootnode}" != "yes"
        bootnodes=""
%else
        bootnodes=`$RPM_INSTALL_PREFIX/bin/quorum-bootnode \
            -nodekey $RPM_INSTALL_PREFIX/etc/quorum-bootnode/quorum-bootnode.key -writeaddress`
        bootnodes="\"enode://$bootnodes@127.0.0.1:30301\""
%endif
%else
        bootnodes=`$RPM_INSTALL_PREFIX/bin/quorum-geth dumpconfig | \
            grep '^BootstrapNodes' | sed -e 's;^.*\\[;;' -e 's;\\].*;;'`
%endif
        %{l_shtool} subst -e "s;@bootnodes@;$bootnodes;" \
            $RPM_INSTALL_PREFIX/etc/quorum/quorum-geth.conf

%if "%{with_private}" == "yes"
        #   generate local account
        echo "Generating Private Ethereum Blockchain Local Account" | \
            %{l_rpmtool} msg -b -t notice
        su - %{l_rusr} -c \
            "umask 0077; $RPM_INSTALL_PREFIX/bin/apg -n1 -a1 -m32 -x32 -M NCL \
                >$RPM_INSTALL_PREFIX/etc/quorum/quorum-account.passwd"
        account=`su - %{l_rusr} -c \
            "$RPM_INSTALL_PREFIX/bin/quorum-geth \
                --config $RPM_INSTALL_PREFIX/etc/quorum/quorum-geth.conf \
                account new \
                --password $RPM_INSTALL_PREFIX/etc/quorum/quorum-account.passwd" 2>&1 | \
            egrep "^Address:" | sed -e 's;^Address: {\(.*\)}.*;\1;'`

        #   generate genesis block
        echo "Generating Private Ethereum Blockchain Genesis Block" | \
            %{l_rpmtool} msg -b -t notice
        %{l_shtool} subst -e "s;@account@;$account;" \
            $RPM_INSTALL_PREFIX/etc/quorum/quorum-genesis.json
        su - %{l_rusr} -c \
            "$RPM_INSTALL_PREFIX/bin/quorum-geth \
                --config $RPM_INSTALL_PREFIX/etc/quorum/quorum-geth.conf \
                init $RPM_INSTALL_PREFIX/etc/quorum/quorum-genesis.json \
                >>$RPM_INSTALL_PREFIX/var/quorum/log/quorum-geth.log 2>&1"
%endif
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} quorum status 2>/dev/null`
        [ ".$quorum_active" = .yes ] && %{l_rc} quorum restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        %{l_rc} quorum stop 2>/dev/null
        rm -rf $RPM_INSTALL_PREFIX/var/quorum/log/*  >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/quorum/run/*  >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/quorum/data/* >/dev/null 2>&1 || true
    fi
    exit 0

