Skip to content

Commit

Permalink
reduce parsing and getters (hyperledger-archives#2804)
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Lincoln <[email protected]>
  • Loading branch information
nklincoln authored and samjsmith committed Nov 22, 2017
1 parent 87ef8ba commit 72633b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
27 changes: 12 additions & 15 deletions packages/composer-runtime/lib/engine.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,25 @@ class EngineQueries {
const accessController = context.getAccessController();
return context.getCompiledQueryBundle().execute(dataService, identifier, parameters)
.then((objects) => {
return objects.map((object) => {
object = Registry.removeInternalProperties(object);
return serializer.fromJSON(object);
}).reduce((resources, resource) => {
return resources.then((resources) => {
return accessController.check(resource, 'READ')
objects.forEach((object) => {
Registry.removeInternalProperties(object);
});
return objects.reduce((promiseChain, object) => {
return promiseChain.then((objects) => {
return accessController.check(serializer.fromJSON(object), 'READ')
.then(() => {
resources.push(resource);
return resources;
objects.push(object);
return objects;
})
.catch((error) => {
return resources;
return objects;
});
});
}, Promise.resolve([]));
})
.then((resources) => {
resources = resources.map((resource) => {
return serializer.toJSON(resource);
});
LOG.exit(method, resources);
return resources;
.then((objects) => {
LOG.exit(method, objects);
return objects;
});

}
Expand Down
34 changes: 17 additions & 17 deletions packages/composer-runtime/lib/identitymanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,41 @@ class IdentityManager extends TransactionHandler {
// Check to see if the identity exists.
identityRegistry = identityRegistry_;
identifier = this.identityService.getIdentifier();
identityName = this.identityService.getName();
return identityRegistry.exists(identifier);

return identityRegistry.get(identifier)
.catch(() => {
return null;
});
})
.then((exists) => {
.then((identity) => {

// If it doesn't exist, then try again with the temporary identifier, which is hash(name, issuer).
if (!exists) {
if (!identity) {
const sha256 = createHash('sha256');
const name = this.identityService.getName();
const issuer = this.identityService.getIssuer();
sha256.update(name, 'utf8');
sha256.update(issuer, 'utf8');
identifier = sha256.digest('hex');
return identityRegistry.exists(identifier);
return identityRegistry.get(identifier)
.catch(() => {
return null;
});
} else {
return exists;
return identity;
}

})
.then((exists) => {
.then((identity) => {

// If it still doesn't exist, throw!
if (!exists) {
const error = new Error('The current identity has not been registered: '+identityName);
error.identityName=identityName;
if (!identity) {
identityName = this.identityService.getName();
const error = new Error('The current identity has not been registered: ' + identityName);
error.identityName = identityName;
LOG.error(method, error);
throw error;
}

// Get the identity.
return identityRegistry.get(identifier);

})
.then((identity) => {
// Return the identity.
LOG.exit(method, identity);
return identity;
});
Expand Down
8 changes: 3 additions & 5 deletions packages/composer-runtime/test/identitymanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,28 @@ describe('IdentityManager', () => {
it('should look up an identity using the identifier', () => {
mockIdentityService.getIdentifier.returns('7d85a9672abea0dfa45705eed99a536b8470d192e2a17e50c1fb86cb4bccae63');
let identity = factory.newResource('org.hyperledger.composer.system', 'Identity', '7d85a9672abea0dfa45705eed99a536b8470d192e2a17e50c1fb86cb4bccae63');
mockIdentityRegistry.exists.withArgs('7d85a9672abea0dfa45705eed99a536b8470d192e2a17e50c1fb86cb4bccae63').resolves(true);
mockIdentityRegistry.get.withArgs('7d85a9672abea0dfa45705eed99a536b8470d192e2a17e50c1fb86cb4bccae63').resolves(identity);
return identityManager.getIdentity()
.should.eventually.be.equal(identity);
});

it('should look up an issued identity using the name and issuer', () => {
mockIdentityService.getIdentifier.returns('7d85a9672abea0dfa45705eed99a536b8470d192e2a17e50c1fb86cb4bccae63');
mockIdentityRegistry.exists.withArgs('7d85a9672abea0dfa45705eed99a536b8470d192e2a17e50c1fb86cb4bccae63').resolves(false);
mockIdentityRegistry.get.withArgs('7d85a9672abea0dfa45705eed99a536b8470d192e2a17e50c1fb86cb4bccae63').rejects();
mockIdentityService.getName.returns('admin');
mockIdentityService.getIssuer.returns('26341f1fc63f30886c54abeba3aca520601126ae2a57869d1ec1a3f854ebc417');
let identity = factory.newResource('org.hyperledger.composer.system', 'Identity', '9f8b1a1e7280d40f4f14577ee4329473c60f04db3ea0f40c91f9684558c6ab50');
mockIdentityRegistry.exists.withArgs('9f8b1a1e7280d40f4f14577ee4329473c60f04db3ea0f40c91f9684558c6ab50').resolves(true);
mockIdentityRegistry.get.withArgs('9f8b1a1e7280d40f4f14577ee4329473c60f04db3ea0f40c91f9684558c6ab50').resolves(identity);
return identityManager.getIdentity()
.should.eventually.be.equal(identity);
});

it('should throw for an identity that does not exist', () => {
mockIdentityService.getIdentifier.returns('7d85a9672abea0dfa45705eed99a536b8470d192e2a17e50c1fb86cb4bccae63');
mockIdentityRegistry.exists.withArgs('7d85a9672abea0dfa45705eed99a536b8470d192e2a17e50c1fb86cb4bccae63').resolves(false);
mockIdentityRegistry.get.withArgs('7d85a9672abea0dfa45705eed99a536b8470d192e2a17e50c1fb86cb4bccae63').rejects();
mockIdentityService.getName.returns('admin');
mockIdentityService.getIssuer.returns('26341f1fc63f30886c54abeba3aca520601126ae2a57869d1ec1a3f854ebc417');
mockIdentityRegistry.exists.withArgs('9f8b1a1e7280d40f4f14577ee4329473c60f04db3ea0f40c91f9684558c6ab50').resolves(false);
mockIdentityRegistry.get.withArgs('9f8b1a1e7280d40f4f14577ee4329473c60f04db3ea0f40c91f9684558c6ab50').rejects();
return identityManager.getIdentity()
.should.be.rejectedWith(/The current identity has not been registered/);
});
Expand Down

0 comments on commit 72633b1

Please sign in to comment.