Skip to content

Commit

Permalink
Wait endorsers for finality when we run FSC based endorsement
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandros Filios <alexandros.filios@ibm.com>
  • Loading branch information
alexandrosfilios committed Sep 9, 2024
1 parent 64c6d3c commit 56a8126
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
34 changes: 34 additions & 0 deletions integration/token/common/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package common
import (
"github.com/hyperledger-labs/fabric-smart-client/integration"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common"
topology2 "github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric/topology"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/services/endorser"
token2 "github.com/hyperledger-labs/fabric-token-sdk/integration/token"
"github.com/hyperledger-labs/fabric-token-sdk/integration/token/common/views"
"github.com/hyperledger-labs/fabric-token-sdk/token"
Expand All @@ -29,3 +31,35 @@ func CheckFinality(network *integration.Infrastructure, id *token2.NodeReference
Expect(err).NotTo(HaveOccurred())
}
}

func CheckEndorserFinality(network *integration.Infrastructure, id *token2.NodeReference, txID string, tmsID *token.TMSID, fail bool) {
if id == nil || len(id.Id()) == 0 {
return
}
var nw, channel string
if tmsID != nil {
nw, channel = tmsID.Network, tmsID.Channel
} else {
t := getFabricTopology(network)
nw, channel = t.Name(), t.Channels[0].Name
}
_, err := network.Client(id.ReplicaName()).CallView("EndorserFinality", common.JSONMarshall(&endorser.Finality{
TxID: txID,
Network: nw,
Channel: channel,
}))
if fail {
Expect(err).To(HaveOccurred())
} else {
Expect(err).NotTo(HaveOccurred())
}
}

func getFabricTopology(network *integration.Infrastructure) *topology2.Topology {
for _, t := range network.Topologies {
if t.Type() == "fabric" {
return t.(*topology2.Topology)
}
}
panic("no fabric topology found")
}
27 changes: 25 additions & 2 deletions integration/token/fungible/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ func IssueCash(network *integration.Infrastructure, wallet string, typ string, a
return IssueCashForTMSID(network, wallet, typ, amount, receiver, auditor, anonymous, issuer, nil, expectedErrorMsgs...)
}

func IssueSuccessfulCash(network *integration.Infrastructure, wallet string, typ string, amount uint64, receiver *token3.NodeReference, auditor *token3.NodeReference, anonymous bool, issuer *token3.NodeReference, finalities ...*token3.NodeReference) string {
return issueCashForTMSID(network, wallet, typ, amount, receiver, auditor, anonymous, issuer, nil, finalities, []string{})
}

func IssueCashForTMSID(network *integration.Infrastructure, wallet string, typ string, amount uint64, receiver *token3.NodeReference, auditor *token3.NodeReference, anonymous bool, issuer *token3.NodeReference, tmsId *token2.TMSID, expectedErrorMsgs ...string) string {
return issueCashForTMSID(network, wallet, typ, amount, receiver, auditor, anonymous, issuer, tmsId, []*token3.NodeReference{}, expectedErrorMsgs)
}

func issueCashForTMSID(network *integration.Infrastructure, wallet string, typ string, amount uint64, receiver *token3.NodeReference, auditor *token3.NodeReference, anonymous bool, issuer *token3.NodeReference, tmsId *token2.TMSID, endorsers []*token3.NodeReference, expectedErrorMsgs []string) string {
targetAuditor := auditor.Id()
if auditor.Id() == "issuer" || auditor.Id() == "newIssuer" {
// the issuer is the auditor, choose default identity
Expand All @@ -97,11 +105,16 @@ func IssueCashForTMSID(network *integration.Infrastructure, wallet string, typ s
TMSID: tmsId,
}))

topology.ToOptions(network.FscPlatform.Peers[0].Options).Endorser()
if len(expectedErrorMsgs) == 0 {
Expect(err).NotTo(HaveOccurred())
txID := common.JSONUnmarshalString(txIDBoxed)
common2.CheckFinality(network, receiver, txID, tmsId, false)
common2.CheckFinality(network, auditor, txID, tmsId, false)
for _, n := range []*token3.NodeReference{receiver, auditor} {
common2.CheckFinality(network, n, txID, tmsId, false)
}
for _, n := range endorsers {
common2.CheckEndorserFinality(network, n, txID, tmsId, false)
}
return common.JSONUnmarshalString(txIDBoxed)
}

Expand All @@ -112,6 +125,16 @@ func IssueCashForTMSID(network *integration.Infrastructure, wallet string, typ s
return ""
}

func GetEndorsers(network *integration.Infrastructure, sel *token3.ReplicaSelector) []*token3.NodeReference {
endorsers := make([]*token3.NodeReference, 0)
for _, p := range network.FscPlatform.Peers {
if topology.ToOptions(p.Options).Endorser() {
endorsers = append(endorsers, sel.Get(p.Name))
}
}
return endorsers
}

func CheckAuditedTransactions(network *integration.Infrastructure, auditor *token3.NodeReference, expected []*ttxdb.TransactionRecord, start *time.Time, end *time.Time) {
txsBoxed, err := network.Client(auditor.ReplicaName()).CallView("historyAuditing", common.JSONMarshall(&views.ListAuditedTransactions{
From: start,
Expand Down
5 changes: 3 additions & 2 deletions integration/token/fungible/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func TestAll(network *integration.Infrastructure, auditorId string, onAuditorRes
bob := sel.Get("bob")
charlie := sel.Get("charlie")
manager := sel.Get("manager")
endorsers := GetEndorsers(network, sel)
RegisterAuditor(network, auditor, nil)

// give some time to the nodes to get the public parameters
Expand All @@ -286,7 +287,7 @@ func TestAll(network *integration.Infrastructure, auditorId string, onAuditorRes
Eventually(DoesWalletExist).WithArguments(network, issuer, "pineapple", views.IssuerWallet).WithTimeout(1 * time.Minute).WithPolling(15 * time.Second).Should(Equal(false))
Eventually(DoesWalletExist).WithArguments(network, alice, "", views.OwnerWallet).WithTimeout(1 * time.Minute).WithPolling(15 * time.Second).Should(Equal(true))
Eventually(DoesWalletExist).WithArguments(network, alice, "mango", views.OwnerWallet).WithTimeout(1 * time.Minute).WithPolling(15 * time.Second).Should(Equal(false))
IssueCash(network, "", "USD", 110, alice, auditor, true, issuer)
IssueSuccessfulCash(network, "", "USD", 110, alice, auditor, true, issuer, endorsers...)
t1 := time.Now()
CheckBalanceAndHolding(network, alice, "", "USD", 110, auditor)
CheckAuditedTransactions(network, auditor, AuditedTransactions[:1], nil, nil)
Expand Down Expand Up @@ -681,7 +682,7 @@ func TestAll(network *integration.Infrastructure, auditorId string, onAuditorRes
CheckBalanceAndHolding(network, bob, "", "EUR", 2820-sum, auditor)

// Transfer With TokenSelector
IssueCash(network, "", "YUAN", 17, alice, auditor, true, issuer)
IssueSuccessfulCash(network, "", "YUAN", 17, alice, auditor, true, issuer, endorsers...)
TransferCashWithSelector(network, alice, "", "YUAN", 10, bob, auditor)
CheckBalanceAndHolding(network, alice, "", "YUAN", 7, auditor)
CheckBalanceAndHolding(network, bob, "", "YUAN", 10, auditor)
Expand Down
2 changes: 2 additions & 0 deletions integration/token/fungible/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc/node"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/monitoring"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/orion"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/services/endorser"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/sdk/tracing"
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token"
common2 "github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/common"
Expand Down Expand Up @@ -310,6 +311,7 @@ func Topology(opts common.Opts) []api.Topology {
if opts.FSCBasedEndorsement {
endorserTemplate := fscTopology.NewTemplate("endorser")
endorserTemplate.RegisterViewFactory("GetPublicParams", &views.GetPublicParamsViewFactory{})
endorserTemplate.RegisterViewFactory("EndorserFinality", &endorser.FinalityViewFactory{})
endorserTemplate.AddOptions(
fabric.WithOrganization("Org1"),
fabric2.WithEndorserRole(),
Expand Down

0 comments on commit 56a8126

Please sign in to comment.