@@ -501,38 +501,57 @@ angular.module('cesium.wot.services', ['ngResource', 'ngApi', 'cesium.bma.servic
501501 if ( ! res . memberships || ! res . memberships . length ) {
502502 return null ;
503503 }
504- var idtiesByBlock = { } ; // TODO: workaround for Duniter#667 - https://github.com/duniter/duniter/issues/667
505- var idties = res . memberships . reduce ( function ( res , ms ) {
506- if ( ms . membership === 'IN' ) {
504+ var idtiesByBlock = { } ;
505+ var idtiesByPubkey = { } ;
506+ var idties = [ ] ;
507+ _ . forEach ( res . memberships , function ( ms ) {
508+ if ( ms . membership == 'IN' ) {
507509 var idty = {
508510 uid : ms . uid ,
509511 pubkey : ms . pubkey ,
510512 block : ms . blockNumber ,
511513 blockHash : ms . blockHash
512514 } ;
513- if ( ! idtiesByBlock [ ms . blockNumber ] ) {
514- idtiesByBlock [ ms . blockNumber ] = [ idty ] ;
515+ var otherIdtySamePubkey = idtiesByPubkey [ ms . pubkey ] ;
516+ if ( otherIdtySamePubkey && idty . block > otherIdtySamePubkey . block ) {
517+ return ; // skip
518+ }
519+ idtiesByPubkey [ idty . pubkey ] = idty ;
520+ if ( ! idtiesByBlock [ idty . block ] ) {
521+ idtiesByBlock [ idty . block ] = [ idty ] ;
515522 }
516523 else {
517- idtiesByBlock [ ms . blockNumber ] . push ( idty ) ;
524+ idtiesByBlock [ idty . block ] . push ( idty ) ;
525+ }
526+
527+ // Remove previous idty from maps
528+ if ( otherIdtySamePubkey ) {
529+ idtiesByBlock [ otherIdtySamePubkey . block ] = idtiesByBlock [ otherIdtySamePubkey . block ] . reduce ( function ( res , aidty ) {
530+ if ( aidty . pubkey == otherIdtySamePubkey . pubkey ) return res ; // if match idty to remove, to NOT add
531+ return ( res || [ ] ) . concat ( aidty ) ;
532+ } , null ) ;
533+ if ( idtiesByBlock [ otherIdtySamePubkey . block ] === null ) {
534+ delete idtiesByBlock [ otherIdtySamePubkey . block ] ;
535+ }
536+ return ;
537+ }
538+ else {
539+ idties . push ( idty ) ;
518540 }
519- return res . concat ( idty ) ;
520541 }
521- return res ;
522- } , [ ] ) ;
523- idties = _sortAndLimitIdentities ( idties , size ) ;
542+ } ) ;
543+ idties = _sortAndLimitIdentities ( idtiesByPubkey , size ) ;
524544
525545 return BMA . blockchain . blocks ( _ . keys ( idtiesByBlock ) )
526546 . then ( function ( blocks ) {
527547
528548 _ . forEach ( blocks , function ( block ) {
529549 _ . forEach ( idtiesByBlock [ block . number ] , function ( idty ) {
530550 idty . sigDate = block . medianTime ;
531- idty . valid = ( idty . blockHash === block . hash ) ;
532- //if (!idty.valid) {
551+ if ( idty . blockHash !== block . hash ) {
533552 idty . errors = [ 'INVALID_MS_BLOCK_HASH' ] ;
534553 console . debug ( "Invalid membership for uid={0}: block hash not match a real block (block cancelled)" . format ( idty . uid ) ) ;
535- // }
554+ }
536555 } ) ;
537556 } ) ;
538557 return $q ( function ( resolve , reject ) {
0 commit comments