@@ -49,6 +49,23 @@ public JoinFederationRequestService(ICounterChainSettings counterChainSettings,
4949 this . votingManager = votingManager ;
5050 }
5151
52+ public static byte [ ] GetFederationMemberBytes ( JoinFederationRequest joinRequest , Network network , Network counterChainNetwork )
53+ {
54+ Script collateralScript = PayToPubkeyHashTemplate . Instance . GenerateScriptPubKey ( joinRequest . CollateralMainchainAddress ) ;
55+ BitcoinAddress bitcoinAddress = collateralScript . GetDestinationAddress ( counterChainNetwork ) ;
56+ var collateralFederationMember = new CollateralFederationMember ( joinRequest . PubKey , false , joinRequest . CollateralAmount , bitcoinAddress . ToString ( ) ) ;
57+
58+ return ( network . Consensus . ConsensusFactory as CollateralPoAConsensusFactory ) . SerializeFederationMember ( collateralFederationMember ) ;
59+ }
60+
61+ public static void SetLastRemovalEventId ( JoinFederationRequest joinRequest , byte [ ] federationMemberBytes , VotingManager votingManager )
62+ {
63+ Poll poll = votingManager . GetExecutedPolls ( ) . OrderByDescending ( p => p . PollExecutedBlockData . Height ) . FirstOrDefault ( x =>
64+ x . VotingData . Key == VoteKey . KickFederationMember && x . VotingData . Data . SequenceEqual ( federationMemberBytes ) ) ;
65+
66+ joinRequest . RemovalEventId = ( poll == null ) ? Guid . Empty : new Guid ( poll . PollExecutedBlockData . Hash . ToBytes ( ) . TakeLast ( 16 ) . ToArray ( ) ) ;
67+ }
68+
5269 public async Task < PubKey > JoinFederationAsync ( JoinFederationRequestModel request , CancellationToken cancellationToken )
5370 {
5471 // Wait until the node is synced before joining.
@@ -74,13 +91,7 @@ public async Task<PubKey> JoinFederationAsync(JoinFederationRequestModel request
7491 var joinRequest = new JoinFederationRequest ( minerKey . PubKey , new Money ( expectedCollateralAmount , MoneyUnit . BTC ) , addressKey ) ;
7592
7693 // Populate the RemovalEventId.
77- var collateralFederationMember = new CollateralFederationMember ( minerKey . PubKey , false , joinRequest . CollateralAmount , request . CollateralAddress ) ;
78-
79- byte [ ] federationMemberBytes = ( this . network . Consensus . ConsensusFactory as CollateralPoAConsensusFactory ) . SerializeFederationMember ( collateralFederationMember ) ;
80- Poll poll = this . votingManager . GetApprovedPolls ( ) . FirstOrDefault ( x => x . IsExecuted &&
81- x . VotingData . Key == VoteKey . KickFederationMember && x . VotingData . Data . SequenceEqual ( federationMemberBytes ) ) ;
82-
83- joinRequest . RemovalEventId = ( poll == null ) ? Guid . Empty : new Guid ( poll . PollExecutedBlockData . Hash . ToBytes ( ) . TakeLast ( 16 ) . ToArray ( ) ) ;
94+ SetLastRemovalEventId ( joinRequest , GetFederationMemberBytes ( joinRequest , this . network , this . counterChainSettings . CounterChainNetwork ) , this . votingManager ) ;
8495
8596 // Get the signature by calling the counter-chain "signmessage" API.
8697 var signMessageRequest = new SignMessageRequest ( )
0 commit comments