-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from GoLedgerDev/develop
Feature: Events
- Loading branch information
Showing
18 changed files
with
680 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package assets | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/goledgerdev/cc-tools/errors" | ||
sw "github.com/goledgerdev/cc-tools/stubwrapper" | ||
"github.com/hyperledger/fabric-chaincode-go/shim" | ||
pb "github.com/hyperledger/fabric-protos-go/peer" | ||
) | ||
|
||
type HistoryResponse struct { | ||
Result []map[string]interface{} `json:"result"` | ||
Metadata *pb.QueryResponseMetadata `json:"metadata"` | ||
} | ||
|
||
func History(stub *sw.StubWrapper, key string, resolve bool) (*HistoryResponse, errors.ICCError) { | ||
var resultsIterator shim.HistoryQueryIteratorInterface | ||
|
||
resultsIterator, err := stub.GetHistoryForKey(key) | ||
if err != nil { | ||
return nil, errors.WrapErrorWithStatus(err, "failed to get history for key", http.StatusInternalServerError) | ||
} | ||
defer resultsIterator.Close() | ||
|
||
historyResult := make([]map[string]interface{}, 0) | ||
|
||
for resultsIterator.HasNext() { | ||
queryResponse, err := resultsIterator.Next() | ||
if err != nil { | ||
return nil, errors.WrapErrorWithStatus(err, "error iterating response", 500) | ||
} | ||
|
||
var data map[string]interface{} | ||
|
||
err = json.Unmarshal(queryResponse.Value, &data) | ||
if err != nil { | ||
return nil, errors.WrapErrorWithStatus(err, "failed to unmarshal queryResponse values", 500) | ||
} | ||
|
||
if resolve { | ||
key, err := NewKey(data) | ||
if err != nil { | ||
return nil, errors.WrapError(err, "failed to create key object to resolve result") | ||
} | ||
asset, err := key.GetRecursive(stub) | ||
if err != nil { | ||
return nil, errors.WrapError(err, "failed to resolve result") | ||
} | ||
data = asset | ||
} | ||
|
||
historyResult = append(historyResult, data) | ||
} | ||
|
||
response := HistoryResponse{ | ||
Result: historyResult, | ||
} | ||
|
||
return &response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package events | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/goledgerdev/cc-tools/errors" | ||
sw "github.com/goledgerdev/cc-tools/stubwrapper" | ||
) | ||
|
||
type EventType float64 | ||
|
||
const ( | ||
EventLog EventType = iota | ||
EventTransaction | ||
EventCustom | ||
) | ||
|
||
// Event is the struct defining a primitive event. | ||
type Event struct { | ||
// Tag is how the event will be referenced | ||
Tag string `json:"tag"` | ||
|
||
// Label is the pretty event name for logs | ||
Label string `json:"label"` | ||
|
||
// Description is a simple explanation describing the meaning of the event. | ||
Description string `json:"description"` | ||
|
||
// BaseLog is the basisc log message for the event | ||
BaseLog string `json:"baseLog"` | ||
|
||
// Type is the type of event | ||
Type EventType `json:"type"` | ||
|
||
// Receivers is an array that specifies which organizations will receive the event. | ||
// Accepts either basic strings for exact matches | ||
// eg. []string{'org1MSP', 'org2MSP'} | ||
// or regular expressions | ||
// eg. []string{`$org\dMSP`} and cc-tools will | ||
// check for a match with regular expression `org\dMSP` | ||
Receivers []string `json:"receivers,omitempty"` | ||
|
||
// Transaction is the transaction that the event triggers (if of type EventTransaction) | ||
Transaction string `json:"transaction"` | ||
|
||
// Channel is the channel of the transaction that the event triggers (if of type EventTransaction) | ||
// If empty, the event will trigger on the same channel as the transaction that calls the event | ||
Channel string `json:"channel"` | ||
|
||
// Chaincode is the chaincode of the transaction that the event triggers (if of type EventTransaction) | ||
// If empty, the event will trigger on the same chaincode as the transaction that calls the event | ||
Chaincode string `json:"chaincode"` | ||
|
||
// CustomFunction is used an event of type "EventCustom" is called. | ||
// It is a function that receives a stub and a payload and returns an error. | ||
CustomFunction func(*sw.StubWrapper, []byte) error `json:"-"` | ||
|
||
// ReadOnly indicates if the CustomFunction has the ability to alter the world state (if of type EventCustom). | ||
ReadOnly bool `json:"readOnly"` | ||
} | ||
|
||
func (event Event) CallEvent(stub *sw.StubWrapper, payload []byte) errors.ICCError { | ||
err := stub.SetEvent(event.Tag, payload) | ||
if err != nil { | ||
return errors.WrapError(err, "stub.SetEvent call error") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func CallEvent(stub *sw.StubWrapper, eventTag string, payload []byte) errors.ICCError { | ||
event := FetchEvent(eventTag) | ||
if event == nil { | ||
return errors.NewCCError(fmt.Sprintf("event named %s does not exist", eventTag), http.StatusBadRequest) | ||
} | ||
|
||
return event.CallEvent(stub, payload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package events | ||
|
||
// eventList is the list which should contain all defined events | ||
var eventList = []Event{} | ||
|
||
// EventList returns a copy of the eventList variable. | ||
func EventList() []Event { | ||
listCopy := make([]Event, len(eventList)) | ||
copy(listCopy, eventList) | ||
return listCopy | ||
} | ||
|
||
// InitEventList appends custom events to eventList to avoid initialization loop. | ||
func InitEventList(l []Event) { | ||
eventList = l | ||
} | ||
|
||
// FetchEvent returns a pointer to the event object or nil if event is not found. | ||
func FetchEvent(eventTag string) *Event { | ||
for _, event := range eventList { | ||
if event.Tag == eventTag { | ||
return &event | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package test | ||
|
||
import ( | ||
"log" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/goledgerdev/cc-tools/events" | ||
) | ||
|
||
func TestFetchEvent(t *testing.T) { | ||
event := *events.FetchEvent("createLibraryLog") | ||
expectedEvent := testEventTypeList[0] | ||
|
||
if !reflect.DeepEqual(event, expectedEvent) { | ||
log.Println("these should be deeply equal") | ||
log.Println(event) | ||
log.Println(expectedEvent) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestFetchEventList(t *testing.T) { | ||
eventList := events.EventList() | ||
expectedEventList := testEventTypeList | ||
|
||
if !reflect.DeepEqual(eventList, expectedEventList) { | ||
log.Println("these should be deeply equal") | ||
log.Println(eventList) | ||
log.Println(expectedEventList) | ||
t.FailNow() | ||
} | ||
} |
Oops, something went wrong.