Skip to content

Commit 49d0854

Browse files
committed
block-listener: identify invalid transactions
Added check for invalid transactions in block. Removed rejection event processing as that is no longer needed. Change-Id: I24aeef3cd18d788af8eb0ea1e2674228ec782947 Signed-off-by: Patrick Mullaney <[email protected]>
1 parent bc93489 commit 49d0854

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

examples/events/block-listener/block-listener.go

+44-32
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,20 @@ import (
2121
"fmt"
2222
"os"
2323

24+
"github.com/hyperledger/fabric/core/ledger/util"
2425
"github.com/hyperledger/fabric/events/consumer"
2526
"github.com/hyperledger/fabric/protos/common"
2627
pb "github.com/hyperledger/fabric/protos/peer"
2728
"github.com/hyperledger/fabric/protos/utils"
2829
)
2930

3031
type adapter struct {
31-
notfy chan *pb.Event_Block
32-
rejected chan *pb.Event_Rejection
33-
listenToRejections bool
32+
notfy chan *pb.Event_Block
3433
}
3534

3635
//GetInterestedEvents implements consumer.EventAdapter interface for registering interested events
3736
func (a *adapter) GetInterestedEvents() ([]*pb.Interest, error) {
38-
return []*pb.Interest{{EventType: pb.EventType_BLOCK}, {EventType: pb.EventType_REJECTION}}, nil
37+
return []*pb.Interest{{EventType: pb.EventType_BLOCK}}, nil
3938
}
4039

4140
//Recv implements consumer.EventAdapter interface for receiving events
@@ -44,12 +43,6 @@ func (a *adapter) Recv(msg *pb.Event) (bool, error) {
4443
a.notfy <- o
4544
return true, nil
4645
}
47-
if o, e := msg.Event.(*pb.Event_Rejection); e {
48-
if a.listenToRejections {
49-
a.rejected <- o
50-
}
51-
return true, nil
52-
}
5346
return false, fmt.Errorf("Receive unkown type event: %v", msg)
5447
}
5548

@@ -59,12 +52,11 @@ func (a *adapter) Disconnected(err error) {
5952
os.Exit(1)
6053
}
6154

62-
func createEventClient(eventAddress string, listenToRejections bool, cid string) *adapter {
55+
func createEventClient(eventAddress string, cid string) *adapter {
6356
var obcEHClient *consumer.EventsClient
6457

6558
done := make(chan *pb.Event_Block)
66-
reject := make(chan *pb.Event_Rejection)
67-
adapter := &adapter{notfy: done, rejected: reject, listenToRejections: listenToRejections}
59+
adapter := &adapter{notfy: done}
6860
obcEHClient, _ = consumer.NewEventsClient(eventAddress, 5, adapter)
6961
if err := obcEHClient.Start(); err != nil {
7062
fmt.Printf("could not start chat %s\n", err)
@@ -74,6 +66,23 @@ func createEventClient(eventAddress string, listenToRejections bool, cid string)
7466

7567
return adapter
7668
}
69+
func getTxPayload(tdata []byte) (*common.Payload, error) {
70+
if tdata == nil {
71+
return nil, fmt.Errorf("Cannot extract payload from nil transaction")
72+
}
73+
74+
if env, err := utils.GetEnvelopeFromBlock(tdata); err != nil {
75+
return nil, fmt.Errorf("Error getting tx from block(%s)", err)
76+
} else if env != nil {
77+
// get the payload from the envelope
78+
payload, err := utils.GetPayload(env)
79+
if err != nil {
80+
return nil, fmt.Errorf("Could not extract payload from envelope, err %s", err)
81+
}
82+
return payload, nil
83+
}
84+
return nil, nil
85+
}
7786

7887
// getChainCodeEvents parses block events for chaincode events associated with individual transactions
7988
func getChainCodeEvents(tdata []byte) (*pb.ChaincodeEvent, error) {
@@ -82,7 +91,7 @@ func getChainCodeEvents(tdata []byte) (*pb.ChaincodeEvent, error) {
8291
}
8392

8493
if env, err := utils.GetEnvelopeFromBlock(tdata); err != nil {
85-
return nil, fmt.Errorf("Error getting tx from block(%s)\n", err)
94+
return nil, fmt.Errorf("Error getting tx from block(%s)", err)
8695
} else if env != nil {
8796
// get the payload from the envelope
8897
payload, err := utils.GetPayload(env)
@@ -119,16 +128,14 @@ func getChainCodeEvents(tdata []byte) (*pb.ChaincodeEvent, error) {
119128

120129
func main() {
121130
var eventAddress string
122-
var listenToRejections bool
123131
var chaincodeID string
124132
flag.StringVar(&eventAddress, "events-address", "0.0.0.0:7053", "address of events server")
125-
flag.BoolVar(&listenToRejections, "listen-to-rejections", false, "whether to listen to rejection events")
126133
flag.StringVar(&chaincodeID, "events-from-chaincode", "", "listen to events from given chaincode")
127134
flag.Parse()
128135

129136
fmt.Printf("Event Address: %s\n", eventAddress)
130137

131-
a := createEventClient(eventAddress, listenToRejections, chaincodeID)
138+
a := createEventClient(eventAddress, chaincodeID)
132139
if a == nil {
133140
fmt.Printf("Error creating event client\n")
134141
return
@@ -141,24 +148,29 @@ func main() {
141148
fmt.Printf("\n")
142149
fmt.Printf("Received block\n")
143150
fmt.Printf("--------------\n")
144-
for _, r := range b.Block.Data.Data {
145-
fmt.Printf("Transaction:\n\t[%v]\n", r)
146-
if event, err := getChainCodeEvents(r); err == nil {
147-
if event.ChaincodeID == chaincodeID {
148-
fmt.Printf("Received chaincode event\n")
149-
fmt.Printf("------------------------\n")
150-
fmt.Printf("Chaincode Event:%+v\n", event)
151+
txsFltr := util.NewFilterBitArrayFromBytes(b.Block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
152+
153+
for i, r := range b.Block.Data.Data {
154+
if txsFltr.IsSet(uint(i)) {
155+
tx, _ := getTxPayload(r)
156+
if tx != nil {
157+
fmt.Printf("\n")
158+
fmt.Printf("\n")
159+
fmt.Printf("Received invalid transaction\n")
160+
fmt.Printf("--------------\n")
161+
fmt.Printf("Transaction invalid: TxID: %s\n", tx.Header.ChainHeader.TxID)
162+
}
163+
} else {
164+
fmt.Printf("Transaction:\n\t[%v]\n", r)
165+
if event, err := getChainCodeEvents(r); err == nil {
166+
if len(chaincodeID) != 0 && event.ChaincodeID == chaincodeID {
167+
fmt.Printf("Received chaincode event\n")
168+
fmt.Printf("------------------------\n")
169+
fmt.Printf("Chaincode Event:%+v\n", event)
170+
}
151171
}
152172
}
153173
}
154-
case r := <-a.rejected:
155-
fmt.Printf("\n")
156-
fmt.Printf("\n")
157-
fmt.Printf("Received rejected transaction\n")
158-
fmt.Printf("--------------\n")
159-
//TODO get TxID from pb.ChaincodeHeader from TransactionAction's Header
160-
//fmt.Printf("Transaction error:\n%s\t%s\n", r.Rejection.Tx.Txid, r.Rejection.ErrorMsg)
161-
fmt.Printf("Transaction error:\n%s\n", r.Rejection.ErrorMsg)
162174
}
163175
}
164176
}

0 commit comments

Comments
 (0)