-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various Utilities in Support of the Token-SDK (#487)
Signed-off-by: Angelo De Caro <[email protected]> Signed-off-by: Alexandros Filios <[email protected]>
- Loading branch information
1 parent
34da6a1
commit a925afa
Showing
63 changed files
with
1,657 additions
and
287 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
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
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,25 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package mock | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/assert" | ||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view" | ||
) | ||
|
||
// InitiatorViewFactory is the factory of Initiator views | ||
type InitiatorViewFactory struct{} | ||
|
||
// NewView returns a new instance of the Initiator view | ||
func (i *InitiatorViewFactory) NewView(in []byte) (view.View, error) { | ||
f := &Initiator{Params: &Params{}} | ||
err := json.Unmarshal(in, f.Params) | ||
assert.NoError(err, "failed unmarshalling input") | ||
return f, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package mock | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view" | ||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/assert" | ||
view3 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/view" | ||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type Params struct { | ||
Mock bool | ||
} | ||
|
||
type Initiator struct { | ||
*Params | ||
} | ||
|
||
func (p *Initiator) Call(ctx view.Context) (interface{}, error) { | ||
// Retrieve responder identity | ||
responder := view2.GetIdentityProvider(ctx).Identity("responder") | ||
var context view.Context | ||
if p.Mock { | ||
c := &view3.MockContext{Ctx: ctx} | ||
c.RespondToAs(ctx.Initiator(), responder, &Responder{}) | ||
context = c | ||
} else { | ||
context = ctx | ||
} | ||
|
||
// Open a session to the responder | ||
session, err := context.GetSession(context.Initiator(), responder) | ||
assert.NoError(err) // Send a ping | ||
|
||
err = session.Send([]byte("ping")) | ||
assert.NoError(err) // Wait for the pong | ||
ch := session.Receive() | ||
select { | ||
case msg := <-ch: | ||
if msg.Status == view.ERROR { | ||
return nil, errors.New(string(msg.Payload)) | ||
} | ||
m := string(msg.Payload) | ||
if m != "mock pong" { | ||
return nil, fmt.Errorf("expected mock pong, got %s", m) | ||
} | ||
case <-time.After(1 * time.Minute): | ||
return nil, errors.New("responder didn't pong in time") | ||
} | ||
|
||
// Return | ||
return "OK", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package mock | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/assert" | ||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view" | ||
) | ||
|
||
type Responder struct{} | ||
|
||
func (p *Responder) Call(context view.Context) (interface{}, error) { | ||
// Retrieve the session opened by the initiator | ||
session := context.Session() | ||
|
||
// Read the message from the initiator | ||
ch := session.Receive() | ||
var payload []byte | ||
select { | ||
case msg := <-ch: | ||
payload = msg.Payload | ||
case <-time.After(5 * time.Second): | ||
return nil, errors.New("time out reached") | ||
} | ||
// Respond with a pong if a ping is received, an error otherwise | ||
m := string(payload) | ||
switch { | ||
case m != "ping": | ||
// reply with an error | ||
err := session.SendError([]byte(fmt.Sprintf("expected ping, got %s", m))) | ||
assert.NoError(err) | ||
return nil, fmt.Errorf("expected ping, got %s", m) | ||
default: | ||
// reply with pong | ||
err := session.Send([]byte("mock pong")) | ||
assert.NoError(err) | ||
} | ||
|
||
// Return | ||
return "OK", 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
Oops, something went wrong.