Skip to content

Commit

Permalink
[FAB-3231] Renamed user accessor methods
Browse files Browse the repository at this point in the history
Change-Id: I4064ab31d99c97549608ab0623a00329888296a4
Signed-off-by: biljana lukovic <biljana.lukovic@securekey.com>
  • Loading branch information
biljanaLukovic committed Apr 19, 2017
1 parent b920301 commit 6d3307f
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 59 deletions.
32 changes: 16 additions & 16 deletions fabric-client/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,9 @@ func (c *chain) JoinChannel(request *JoinChannelRequest) error {
return fmt.Errorf("Error unmarshalling block: %s", err)
}
// Get user enrolment info and serialize for signing requests
user, err := c.clientContext.GetUserContext("")
user, err := c.clientContext.LoadUserFromStateStore("")
if err != nil {
return fmt.Errorf("GetUserContext returned error: %s", err)
return fmt.Errorf("LoadUserFromStateStore returned error: %s", err)
}
creatorID, err := getSerializedIdentity(user.GetEnrollmentCertificate())
if err != nil {
Expand Down Expand Up @@ -867,9 +867,9 @@ func (c *chain) CreateTransactionProposal(chaincodeName string, chainID string,
Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: chaincodeName},
Input: &pb.ChaincodeInput{Args: argsArray}}}

user, err := c.clientContext.GetUserContext("")
user, err := c.clientContext.LoadUserFromStateStore("")
if err != nil {
return nil, fmt.Errorf("GetUserContext return error: %s", err)
return nil, fmt.Errorf("LoadUserFromStateStore return error: %s", err)
}

creatorID, err := getSerializedIdentity(user.GetEnrollmentCertificate())
Expand Down Expand Up @@ -1136,9 +1136,9 @@ func (c *chain) SendInstallProposal(chaincodeName string, chaincodePath string,
Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: chaincodeName, Path: chaincodePath, Version: chaincodeVersion}},
CodePackage: chaincodePackage, EffectiveDate: &google_protobuf.Timestamp{Seconds: int64(now.Second()), Nanos: int32(now.Nanosecond())}}

user, err := c.clientContext.GetUserContext("")
user, err := c.clientContext.LoadUserFromStateStore("")
if err != nil {
return nil, "", fmt.Errorf("GetUserContext return error: %s", err)
return nil, "", fmt.Errorf("LoadUserFromStateStore return error: %s", err)
}

creatorID, err := getSerializedIdentity(user.GetEnrollmentCertificate())
Expand Down Expand Up @@ -1208,9 +1208,9 @@ func (c *chain) SendInstantiateProposal(chaincodeName string, chainID string,
Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: chaincodeName, Path: chaincodePath, Version: chaincodeVersion},
Input: &pb.ChaincodeInput{Args: argsArray}}}

user, err := c.clientContext.GetUserContext("")
user, err := c.clientContext.LoadUserFromStateStore("")
if err != nil {
return nil, "", fmt.Errorf("GetUserContext return error: %s", err)
return nil, "", fmt.Errorf("LoadUserFromStateStore return error: %s", err)
}

creatorID, err := getSerializedIdentity(user.GetEnrollmentCertificate())
Expand Down Expand Up @@ -1247,9 +1247,9 @@ func (c *chain) SendInstantiateProposal(chaincodeName string, chainID string,

func (c *chain) SignPayload(payload []byte) (*SignedEnvelope, error) {
//Get user info
user, err := c.clientContext.GetUserContext("")
user, err := c.clientContext.LoadUserFromStateStore("")
if err != nil {
return nil, fmt.Errorf("GetUserContext returned error: %s", err)
return nil, fmt.Errorf("LoadUserFromStateStore returned error: %s", err)
}

signature, err := c.signObjectWithKey(payload, user.GetPrivateKey(),
Expand Down Expand Up @@ -1381,9 +1381,9 @@ func (c *chain) signObjectWithKey(object []byte, key bccsp.Key,
}

func (c *chain) signProposal(proposal *pb.Proposal) (*pb.SignedProposal, error) {
user, err := c.clientContext.GetUserContext("")
user, err := c.clientContext.LoadUserFromStateStore("")
if err != nil {
return nil, fmt.Errorf("GetUserContext return error: %s", err)
return nil, fmt.Errorf("LoadUserFromStateStore return error: %s", err)
}

proposalBytes, err := protos_utils.GetBytesProposal(proposal)
Expand All @@ -1402,9 +1402,9 @@ func (c *chain) signProposal(proposal *pb.Proposal) (*pb.SignedProposal, error)
// fetchGenesisBlock fetches the configuration block for this channel
func (c *chain) fetchGenesisBlock() (*common.Block, error) {
// Get user enrolment info and serialize for signing requests
user, err := c.clientContext.GetUserContext("")
user, err := c.clientContext.LoadUserFromStateStore("")
if err != nil {
return nil, fmt.Errorf("GetUserContext returned error: %s", err)
return nil, fmt.Errorf("LoadUserFromStateStore returned error: %s", err)
}
creatorID, err := getSerializedIdentity(user.GetEnrollmentCertificate())
if err != nil {
Expand Down Expand Up @@ -1848,9 +1848,9 @@ func (c *chain) getBlock(pos *ab.SeekPosition) (*common.Block, error) {
return nil, fmt.Errorf("error when generating nonce: %v", err)
}

user, err := c.clientContext.GetUserContext("")
user, err := c.clientContext.LoadUserFromStateStore("")
if err != nil {
return nil, fmt.Errorf("GetUserContext return error: %s", err)
return nil, fmt.Errorf("LoadUserFromStateStore return error: %s", err)
}

creator, err := getSerializedIdentity(user.GetEnrollmentCertificate())
Expand Down
2 changes: 1 addition & 1 deletion fabric-client/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func setupTestChain() (Chain, error) {
client := NewClient()
user := NewUser("test")
cryptoSuite := &mocks.MockCryptoSuite{}
client.SetUserContext(user, true)
client.SaveUserToStateStore(user, true)
client.SetCryptoSuite(cryptoSuite)
return NewChain("testChain", client)
}
Expand Down
14 changes: 7 additions & 7 deletions fabric-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type Client interface {
GetStateStore() kvs.KeyValueStore
SetCryptoSuite(cryptoSuite bccsp.BCCSP)
GetCryptoSuite() bccsp.BCCSP
SetUserContext(user User, skipPersistence bool) error
GetUserContext(name string) (User, error)
SaveUserToStateStore(user User, skipPersistence bool) error
LoadUserFromStateStore(name string) (User, error)
}

type client struct {
Expand Down Expand Up @@ -169,15 +169,15 @@ func (c *client) GetCryptoSuite() bccsp.BCCSP {
return c.cryptoSuite
}

// SetUserContext ...
// SaveUserToStateStore ...
/*
* Sets an instance of the User class as the security context of this client instance. This user’s credentials (ECert) will be
* used to conduct transactions and queries with the blockchain network. Upon setting the user context, the SDK saves the object
* in a persistence cache if the “state store” has been set on the Client instance. If no state store has been set,
* this cache will not be established and the application is responsible for setting the user context again when the application
* crashed and is recovered.
*/
func (c *client) SetUserContext(user User, skipPersistence bool) error {
func (c *client) SaveUserToStateStore(user User, skipPersistence bool) error {
if user == nil {
return fmt.Errorf("user is nil")
}
Expand All @@ -197,22 +197,22 @@ func (c *client) SetUserContext(user User, skipPersistence bool) error {
}
err = c.stateStore.SetValue(user.GetName(), data)
if err != nil {
return fmt.Errorf("stateStore SetValue return error: %v", err)
return fmt.Errorf("stateStore SaveUserToStateStore return error: %v", err)
}
}
return nil

}

// GetUserContext ...
// LoadUserFromStateStore ...
/*
* The client instance can have an optional state store. The SDK saves enrolled users in the storage which can be accessed by
* authorized users of the application (authentication is done by the application outside of the SDK).
* This function attempts to load the user by name from the local storage (via the KeyValueStore interface).
* The loaded user object must represent an enrolled user with a valid enrollment certificate signed by a trusted CA
* (such as the COP server).
*/
func (c *client) GetUserContext(name string) (User, error) {
func (c *client) LoadUserFromStateStore(name string) (User, error) {
if c.userContext != nil {
return c.userContext, nil
}
Expand Down
42 changes: 21 additions & 21 deletions fabric-client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,57 +45,57 @@ func TestClientMethods(t *testing.T) {
t.Fatalf("Client getCryptoSuite should not be nil after setCryptoSuite")
}

//Client tests: getUserContext successful nill user
user, err := client.GetUserContext("")
//Client tests: LoadUserFromStateStore successful nill user
user, err := client.LoadUserFromStateStore("")
if err != nil {
t.Fatalf("client.GetUserContext return error[%s]", err)
t.Fatalf("client.LoadUserFromStateStore return error[%s]", err)
}
if user != nil {
t.Fatalf("client.GetUserContext should return nil user")
t.Fatalf("client.LoadUserFromStateStore should return nil user")
}

//Client tests: Should return error "user is nil"
err = client.SetUserContext(nil, false)
err = client.SaveUserToStateStore(nil, false)
if err == nil {
t.Fatalf("client.SetUserContext didn't return error")
t.Fatalf("client.SaveUserToStateStore didn't return error")
}
if err.Error() != "user is nil" {
t.Fatalf("client.SetUserContext didn't return right error")
t.Fatalf("client.SaveUserToStateStore didn't return right error")
}

//Client tests: getUserContext with no context in memory or persisted returns nil
user, err = client.GetUserContext("someUser")
//Client tests: LoadUserFromStateStore with no context in memory or persisted returns nil
user, err = client.LoadUserFromStateStore("someUser")
if err != nil {
t.Fatalf("client.GetUserContext return error[%s]", err)
t.Fatalf("client.LoadUserFromStateStore return error[%s]", err)
}
if user != nil {
t.Fatalf("client.GetUserContext should return nil user")
t.Fatalf("client.LoadUserFromStateStore should return nil user")
}

//Client tests: successfully setUserContext with skipPersistence true
//Client tests: successfully SaveUserToStateStore with skipPersistence true
user = NewUser("someUser")
err = client.SetUserContext(user, true)
err = client.SaveUserToStateStore(user, true)
if err != nil {
t.Fatalf("client.SetUserContext return error[%s]", err)
t.Fatalf("client.SaveUserToStateStore return error[%s]", err)
}
user, err = client.GetUserContext("someUser")
user, err = client.LoadUserFromStateStore("someUser")
if err != nil {
t.Fatalf("client.GetUserContext return error[%s]", err)
t.Fatalf("client.LoadUserFromStateStore return error[%s]", err)
}
if user == nil {
t.Fatalf("client.GetUserContext return nil user")
t.Fatalf("client.LoadUserFromStateStore return nil user")
}
if user.GetName() != "someUser" {
t.Fatalf("client.GetUserContext didn't return the right user")
t.Fatalf("client.LoadUserFromStateStore didn't return the right user")
}

//Client tests: Should throw "stateStore is nil"
err = client.SetUserContext(user, false)
err = client.SaveUserToStateStore(user, false)
if err == nil {
t.Fatalf("client.SetUserContext didn't return error")
t.Fatalf("client.SaveUserToStateStore didn't return error")
}
if err.Error() != "stateStore is nil" {
t.Fatalf("client.SetUserContext didn't return right error")
t.Fatalf("client.SaveUserToStateStore didn't return right error")
}

//Client tests: Create new chain
Expand Down
12 changes: 6 additions & 6 deletions fabric-client/helpers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func GetClient(name string, pwd string, stateStorePath string) (fabricClient.Cli
return nil, fmt.Errorf("CreateNewFileKeyValueStore return error[%s]", err)
}
client.SetStateStore(stateStore)
user, err := client.GetUserContext(name)
user, err := client.LoadUserFromStateStore(name)
if err != nil {
return nil, fmt.Errorf("client.GetUserContext return error: %v", err)
return nil, fmt.Errorf("client.LoadUserFromStateStore return error: %v", err)
}
if user == nil {
fabricCAClient, err1 := fabricCAClient.NewFabricCAClient()
Expand All @@ -115,9 +115,9 @@ func GetClient(name string, pwd string, stateStorePath string) (fabricClient.Cli
}
user.SetPrivateKey(k)
user.SetEnrollmentCertificate(cert)
err = client.SetUserContext(user, false)
err = client.SaveUserToStateStore(user, false)
if err != nil {
return nil, fmt.Errorf("client.SetUserContext return error: %v", err)
return nil, fmt.Errorf("client.SaveUserToStateStore return error: %v", err)
}
}

Expand All @@ -128,9 +128,9 @@ func GetClient(name string, pwd string, stateStorePath string) (fabricClient.Cli
// Utility method gets serialized enrollment certificate
func getCreatorID(client fabricClient.Client) ([]byte, error) {

user, err := client.GetUserContext("")
user, err := client.LoadUserFromStateStore("")
if err != nil {
return nil, fmt.Errorf("GetUserContext returned error: %s", err)
return nil, fmt.Errorf("LoadUserFromStateStore returned error: %s", err)
}
serializedIdentity := &msp.SerializedIdentity{Mspid: config.GetFabricCAID(),
IdBytes: user.GetEnrollmentCertificate()}
Expand Down
2 changes: 1 addition & 1 deletion fabric-client/keyvaluestore/filekeyvaluestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestFKVSMethods(t *testing.T) {
stateStore.SetValue("testvalue", []byte("data"))
value, err := stateStore.GetValue("testvalue")
if err != nil {
t.Fatalf("stateStore.SetValue return error[%s]", err)
t.Fatalf("stateStore.GetValue return error[%s]", err)
}
if string(value) != "data" {
t.Fatalf("stateStore.GetValue didn't return the right value")
Expand Down
14 changes: 7 additions & 7 deletions test/integration/fabric_ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func TestRegisterEnrollRevoke(t *testing.T) {
}

// Admin user is used to register, enrol and revoke a test user
adminUser, err := client.GetUserContext("admin")
adminUser, err := client.LoadUserFromStateStore("admin")

if err != nil {
t.Fatalf("client.GetUserContext return error: %v", err)
t.Fatalf("client.LoadUserFromStateStore return error: %v", err)
}
if adminUser == nil {
key, cert, err := caClient.Enroll("admin", "adminpw")
Expand Down Expand Up @@ -118,16 +118,16 @@ func TestRegisterEnrollRevoke(t *testing.T) {
}
adminUser.SetPrivateKey(k)
adminUser.SetEnrollmentCertificate(cert)
err = client.SetUserContext(adminUser, false)
err = client.SaveUserToStateStore(adminUser, false)
if err != nil {
t.Fatalf("client.SetUserContext return error: %v", err)
t.Fatalf("client.SaveUserToStateStore return error: %v", err)
}
adminUser, err = client.GetUserContext("admin")
adminUser, err = client.LoadUserFromStateStore("admin")
if err != nil {
t.Fatalf("client.GetUserContext return error: %v", err)
t.Fatalf("client.LoadUserFromStateStore return error: %v", err)
}
if adminUser == nil {
t.Fatalf("client.GetUserContext return nil")
t.Fatalf("client.LoadUserFromStateStore return nil")
}
}

Expand Down

0 comments on commit 6d3307f

Please sign in to comment.