Skip to content

Commit

Permalink
final build
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredomusumeci committed Aug 19, 2023
1 parent 15a7a98 commit 74b594f
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 29 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ include gotools.mk

.PHONY: all
all: check-go-version native docker checks
#all: check-go-version checks

.PHONY: checks
checks: basic-checks
Expand Down
11 changes: 11 additions & 0 deletions common/deliver/deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type Chain interface {

// Forked returns a channel which closes when the backing consenter has forked
Forked() <-chan struct{}

ForkedBlock() <-chan *cb.Block
}

//go:generate counterfeiter -o mock/policy_checker.go -fake-name PolicyChecker . PolicyChecker
Expand Down Expand Up @@ -317,6 +319,15 @@ func (h *Handler) deliverBlocks(ctx context.Context, srv *Server, envelope *cb.E
return cb.Status_INTERNAL_SERVER_ERROR, errors.Wrapf(ctx.Err(), "context finished before block retrieved")
case <-forkedChan:
logger.Warningf("[channel: %s] Aborting deliver request for %s because of fork", chdr.ChannelId, addr)

// TODO: temporarily disabled until figuring out how to send the blocc to the peer from the orderer, i.e. GRPC above
//forkedBlock := <-chain.ForkedBlock()
//if forkedBlock == nil {
// logger.Warningf("[channel: %s] Aborting deliver request for %s because of fork, but no forked block found", chdr.ChannelId, addr)
//} else {
// logger.Debugf("Channel %s forked at block %d", chdr.ChannelId, forkedBlock.String())
//}

return cb.Status_FORKED, nil
case <-erroredChan:
// TODO, today, the only user of the errorChan is the orderer consensus implementations. If the peer ever reports
Expand Down
11 changes: 11 additions & 0 deletions common/deliver/mock/chain.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions core/deliverservice/deliveryclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
"context"
"errors"
"fmt"
"github.com/hyperledger/fabric/gossip/common"
"os"
"sync"
"time"

"github.com/hyperledger/fabric-protos-go/orderer"
errors2 "github.com/hyperledger/fabric/common/errors"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/gossip/common"
"github.com/hyperledger/fabric/internal/pkg/comm"
"github.com/hyperledger/fabric/internal/pkg/identity"
"github.com/hyperledger/fabric/internal/pkg/peer/blocksprovider"
Expand Down Expand Up @@ -166,7 +167,12 @@ func (d *deliverServiceImpl) StartDeliverForChannel(chainID string, ledgerInfo b
if _, ok := err.(*errors2.ForkedTxError); ok {
// If a fork is detected, stop the delivery for the channel
logger.Errorf("Fork occurred for channel %s. "+
"All subsequent blocks may be compromised.", chainID, err)
"All subsequent blocks may be compromised.", chainID)
// If a fork is detected, write the fork information to storage
writeErr := writeForkInfoToFile(chainID)
if writeErr != nil {
logger.Errorf("Failed to write fork information: %s", writeErr)
}
err = d.StopDeliverForChannel(chainID)
if err != nil {
logger.Errorf("Fork occurred but Fabric failed to stop delivery for channel %s: %s. "+
Expand All @@ -186,6 +192,23 @@ func (d *deliverServiceImpl) StartDeliverForChannel(chainID string, ledgerInfo b
return nil
}

func writeForkInfoToFile(channelID string) error {
filename := fmt.Sprintf("/var/hyperledger/production/ledgersData/chains/chains/%s/fork_info.txt", channelID)

// Open the file for writing
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return err
}
defer file.Close()

// Write the fork information to the file
// For now, just writing a simple message.
// TODO: write the entire block or other details.
_, err = file.WriteString(fmt.Sprintf("Fork detected for channel %s", channelID))
return err
}

// StopDeliverForChannel stops blocks delivery for channel by stopping channel block provider
func (d *deliverServiceImpl) StopDeliverForChannel(chainID string) error {
d.lock.Lock()
Expand Down
4 changes: 4 additions & 0 deletions core/peer/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func (c *Channel) Forked() <-chan struct{} {
return nil
}

func (c *Channel) ForkedBlock() <-chan *common.Block {
return nil
}

func capabilitiesSupportedOrPanic(res channelconfig.Resources) {
ac, ok := res.ApplicationConfig()
if !ok {
Expand Down
43 changes: 41 additions & 2 deletions core/scc/bscc/bscc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
/*
BLOCC Project
SPDX-License-Identifier: Apache-2.0
*/

package bscc

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/bccsp"
Expand All @@ -11,8 +20,6 @@ import (
blocc "github.com/hyperledger/fabric/internal/peer/blocc/chaincode"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
"io/ioutil"
"os"
)

func New(peerInstance *peer.Peer) *BSCC {
Expand Down Expand Up @@ -45,6 +52,7 @@ var bloccProtoLogger = flogging.MustGetLogger("bscc")
const (
approveSensoryReading string = "ApproveSensoryReading"
simulateForkAttempt string = "SimulateForkAttempt"
checkForkStatus string = "CheckForkStatus"
)

// ------------------- Error handling ------------------- //
Expand Down Expand Up @@ -120,6 +128,9 @@ func (bscc *BSCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
case simulateForkAttempt:
bloccProtoLogger.Warningf("Adding a fork block!")
return shim.Success(nil)
case checkForkStatus:
bloccProtoLogger.Infof("Checking fork status")
return bscc.CheckForkStatus(string(args[1]))
}

return shim.Error(fmt.Sprintf("Requested function %s not found.", fname))
Expand Down Expand Up @@ -208,3 +219,31 @@ func (bscc *BSCC) approveSensoryReading(address, rootCertFilePath string, event

return err
}

func (bscc *BSCC) CheckForkStatus(channelID string) pb.Response {
if channelID == "" {
return shim.Error("ChannelID not specified")
}

var err error
var isForked bool

// Define the filename based on the channel ID
filename := fmt.Sprintf("/var/hyperledger/production/ledgersData/chains/chains/%s/fork_info.txt", channelID)

// Check if the file exists
if _, err = os.Stat(filename); os.IsNotExist(err) {
isForked = false
} else {
isForked = true
}

jsonResponse, err := json.Marshal(isForked)
if err != nil {
errMsg := fmt.Sprintf("BLOCC: Failed to marshal the result to JSON, error %s", err)
bloccProtoLogger.Error(errMsg)
return shim.Error(errMsg)
}

return shim.Success(jsonResponse)
}
5 changes: 2 additions & 3 deletions core/scc/qscc/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ package qscc
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric/protoutil"
"strconv"

"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/aclmgmt"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/protoutil"
)

// LedgerGetter gets the PeerLedger associated with a channel.
Expand Down Expand Up @@ -251,7 +251,7 @@ func getApprovedTransactions(vledger ledger.PeerLedger, chaincodeName []byte) pb
return shim.Error(errMsg)
}

qscclogger.Debugf("BLOCC: block %d, approvingMspId=%s, approvedTxId=%s, temperature=%d, relativeHumidity=%d, timestamp=%d",
qscclogger.Debugf("BLOCC: block %d, approvingMspId=%s, approvedTxId=%s, temperature=%f, relativeHumidity=%f, timestamp=%d",
blockNum, mspId, approvedTxId, temperature, relativeHumidity, timestamp)

agreements[approvedTxId] = OutputEntry{
Expand Down Expand Up @@ -281,7 +281,6 @@ func getApprovedTransactions(vledger ledger.PeerLedger, chaincodeName []byte) pb
}

return shim.Success(jsonResponse)

}

func getTransactionByID(vledger ledger.PeerLedger, tid []byte) pb.Response {
Expand Down
8 changes: 7 additions & 1 deletion internal/peer/blocc/chaincode/simulatefork.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/*
BLOCC Project
SPDX-License-Identifier: Apache-2.0
*/

package chaincode

import (
"context"
"crypto/tls"
"time"

cb "github.com/hyperledger/fabric-protos-go/common"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/bccsp"
Expand All @@ -12,7 +19,6 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"time"
)

type SimulateForkAttempt struct {
Expand Down
11 changes: 11 additions & 0 deletions internal/pkg/peer/blocksprovider/fake/gossip_service_adapter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions orderer/common/multichannel/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ type mockChainCluster struct {
*mockChain
}

func (c *mockChainCluster) Forked() <-chan struct{} {
// TODO implement me
panic("implement me")
}

func (c *mockChainCluster) ForkedBlock() <-chan *cb.Block {
// TODO implement me
panic("implement me")
}

func (c *mockChainCluster) StatusReport() (types.ConsensusRelation, types.Status) {
return types.ConsensusRelationConsenter, types.StatusActive
}
Expand All @@ -39,6 +49,16 @@ type mockChain struct {
done chan struct{}
}

func (mch *mockChain) Forked() <-chan struct{} {
// TODO implement me
panic("implement me")
}

func (mch *mockChain) ForkedBlock() <-chan *cb.Block {
// TODO implement me
panic("implement me")
}

func (mch *mockChain) Errored() <-chan struct{} {
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions orderer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type Chain interface {
// Forked returns a channel which closes when the backing consenter has forked
Forked() <-chan struct{}

ForkedBlock() <-chan *cb.Block

// Start should allocate whatever resources are needed for staying up to date with the chain.
// Typically, this involves creating a thread which reads from the ordering source, passes those
// messages to a block cutter, and writes the resulting blocks to the ledger.
Expand Down
Loading

0 comments on commit 74b594f

Please sign in to comment.