app/models/server.rb
# receive verify gifts response from other Gofreerev server
receive verify gifts response from other Gofreerev server
receive verify gifts response from other Gofreerev server
def receive_verify_gifts_response (mid, request_mid, verify_gifts, error)
    logger.debug2 "mid = #{mid}, request_mid = #{request_mid}"
    logger.debug2 "verify_gifts = #{verify_gifts}"
    logger.debug2 "error        = #{error} (#{error.class})"
    logger.debug2 "error.size = #{error.size}" if error.class == Array
    errors = []
    errors << "empty verify_gifts response message was rejected" unless verify_gifts or error
    errors << error if error
    if verify_gifts
      server_seqs = verify_gifts.collect { |hash| hash['seq'] }.uniq
      if server_seqs.size != verify_gifts.size
        errors << "verify_gifts response message was rejected. seq in verify_gifts array must be unique"
        verify_gifts = nil
      end
    end
    if !verify_gifts or verify_gifts.size == 0
      logger.debug2 "errors = #{errors.join(', ')}"
      return errors.size == 0 ? nil : errors.join(', ')
    end
    vgs = {}
    VerifyGift.where(:server_seq => server_seqs).each { |vg| vgs[vg.server_seq] = vg }
    logger.debug2 "vgs = #{vgs.to_json}"
    unknown_seq = []
    invalid_server_id = []
    invalid_gid = []
    invalid_response = []
    identical_response = []
    ok_response = []
    pending_verify_comms_requests = []
    verify_gifts.each do |verify_gift|
      seq = verify_gift['seq']
      gid = verify_gift['gid']
      verified_at_server = verify_gift['verified_at_server']
      error = verify_gift['error']
      direction = verify_gift['direction']
      vg = vgs[seq]
      if !vg
        unknown_seq << verify_gift
      elsif vg.server_id != self.id
        invalid_server_id << verify_gift
      elsif vg.gid != gid
        invalid_gid << verify_gift
      elsif vg.verified_at_server.class != NilClass
        if (verified_at_server != vg.verified_at_server or error != vg.error or direction != vg.direction)
          invalid_response << verify_gift
        else
          identical_response << verify_gift
        end
      else
        ok_response << verify_gift
        vg.verified_at_server = verified_at_server
        vg.error = error
        vg.response_mid = mid
        vg.direction = direction
        logger.debug2 "vg before save = #{vg.to_json}"
        vg.save!
        logger.debug2 "vg after save = #{vg.to_json}"
        logger.debug2 "todo: check for pending verify comments request waiting for this verify gifts response"
        vc = VerifyComment.find_by_server_seq(vg.server_seq)
        if vc
          logger.debug2 "found old verify comments request waiting for verify gifts response. vc = #{vc.to_json}"
          pending_verify_comms_requests << vc
        end
      end
    end 
    logger.debug2 "verify_gifts.size = #{verify_gifts.size}, unknown_seq.size = #{unknown_seq.size}, " +
                      "invalid_server_id.size = #{invalid_server_id.size}, invalid_gid.size = #{invalid_gid.size}, " +
                      "invalid_response.size = #{invalid_response.size}, identical_response.size = #{identical_response.size}, " +
                      "ok_response.size = #{ok_response.size}, old_pending_vc_requests.size = #{pending_verify_comms_requests.size}"
    pending_verify_comms_requests.each do |vc|
      logger.debug2 "repeat old verify comments request. vc = #{vc.to_json}"
      local_verify_comments_request = [JSON.parse(vc.original_client_request)]
      login_user_ids = JSON.parse(vc.login_user_ids)
      local_verify_comments_response = Comment.verify_comments(local_verify_comments_request, login_user_ids, nil, nil) 
      logger.debug2 "local_verify_comments_request = #{local_verify_comments_request}"
      logger.debug2 "local_verify_comments_response = #{local_verify_comments_response}"
      if !local_verify_comments_response
        error = "System error. Empty Comment.verify_comments response while repeating old pending verify comments request"
        logger.debug2 error
        local_verify_comments_response = { :error => error }
      elsif local_verify_comments_response[:error]
        local_verify_comments_response.delete(:comments)
      else
        if local_verify_comments_response[:comments].class != Array or local_verify_comments_response[:comments].size != 1
          error = "System error. Expected one and only one Comment.verify_comments response row while repeating old pending verify comments request"
          local_verify_comments_response = { :error => error }
        end
      end
      message_hash = {
          :msgtype => 'verify_comments',
          :mid => Sequence.next_server_mid,
          :request_mid => mid
      }
      message_hash[:verify_comments] = local_verify_comments_response[:comments] if local_verify_comments_response[:comments]
      message_hash[:error] = local_verify_comments_response[:error] if local_verify_comments_response[:error]
      logger.debug2 "todo: witch mid to use in verify comments response with old verify comments requests?"
      logger.debug2 "message_hash = #{message_hash}"
      logger.debug2 "vc = #{vc.to_json}"
      json_schema = :verify_comments_response
      if !JSON_SCHEMA.has_key? json_schema
        logger.debug2 "todo: send could not validate verify_comments response. JSON schema definition #{json_schema.to_s} was not found error message to other Gofreerev server"
        return ["Could not validate verify_comments response. JSON schema definition #{json_schema.to_s} was not found.", false]
      end
      json_errors = JSON::Validator.fully_validate(JSON_SCHEMA[json_schema], message_hash)
      if json_errors.size > 0
        logger.error2 "Failed to return remote verify comments response. Error in #{json_schema}"
        logger.error2 "message = #{message_hash}"
        logger.error2 "json_schema = #{JSON_SCHEMA[json_schema]}"
        logger.error2 "errors = #{json_errors.join(', ')}"
        logger.debug2 "todo: send Failed to return remote verify comments response. Error in #{json_schema} 1error message to other Gofreerev server"
        return "Failed to return remote verify comments response: #{json_errors.join(', ')}"
      end
      key, message = self.mix_encrypt_message_hash message_hash
      m = Message.new
      m.from_did = SystemParameter.did
      m.to_did = self.new_did
      m.server = true
      m.key = key
      m.message = message
      m.mid = message_hash[:mid]
      m.save!
      logger.debug2 "correct to_did in verify comments response message?"
      logger.debug2 "m = #{m.to_json}"
      logger.debug2 "vc = #{vc.to_json}"
    end 
    if unknown_seq.size + invalid_server_id.size + invalid_gid.size + invalid_response.size > 0
      errors = ["#{unknown_seq.size + invalid_server_id.size + invalid_gid.size + invalid_response.size} rows in verify gift response"]
      errors << "Unknown or deleted seqs: " + unknown_seq.collect { |vg| vg['seq'] }.join(', ') if unknown_seq.size > 0
      errors << "Invalid seqs (other server): " + invalid_server_id.collect { |vg| vg['seq'] }.join(', ') if invalid_server_id.size > 0
      errors << "Invalid gids: " + invalid_gid.collect { |vg| vg['seq'] }.join(', ') if invalid_gid.size > 0
      errors << "Changed response: " + invalid_response.collect { |vg| vg['seq'] }.join(', ') if invalid_gid.size > 0
      message_hash = {
          :msgtype => 'error',
          :mid => Sequence.next_server_mid,
          :request_mid => request_mid,
          :response_mid => mid,
          :error => errors.join(', ')
      }
      logger.debug2 "error message = #{message_hash}"
      key, message = self.mix_encrypt_message_hash message_hash
      m = Message.new
      m.from_did = SystemParameter.did
      m.to_did = self.new_did
      m.server = true
      m.key = key
      m.message = message
      m.mid = message_hash.mid
      m.save!
      return errors.join(', ')
    end
    return nil
  end
def receive_verify_gifts_response ( mid , request_mid , verify_gifts , error ) logger . debug2 "mid = #{mid}, request_mid = #{request_mid}" logger . debug2 "verify_gifts = #{verify_gifts}" logger . debug2 "error        = #{error} (#{error.class})" logger . debug2 "error.size = #{error.size}" if error . class == Array errors = [ ] errors << "empty verify_gifts response message was rejected" unless verify_gifts or error errors << error if error if verify_gifts server_seqs = verify_gifts . collect { | hash | hash [ 'seq' ] } . uniq if server_seqs . size != verify_gifts . size errors << "verify_gifts response message was rejected. seq in verify_gifts array must be unique" verify_gifts = nil end end if ! verify_gifts or verify_gifts . size == 0 logger . debug2 "errors = #{errors.join(', ')}" return errors . size == 0 ? nil : errors . join ( ', ' ) end vgs = { } VerifyGift . where ( :server_seq => server_seqs ) . each { | vg | vgs [ vg . server_seq ] = vg } logger . debug2 "vgs = #{vgs.to_json}" unknown_seq = [ ] invalid_server_id = [ ] invalid_gid = [ ] invalid_response = [ ] identical_response = [ ] ok_response = [ ] pending_verify_comms_requests = [ ] verify_gifts . each do | verify_gift | seq = verify_gift [ 'seq' ] gid = verify_gift [ 'gid' ] verified_at_server = verify_gift [ 'verified_at_server' ] error = verify_gift [ 'error' ] direction = verify_gift [ 'direction' ] vg = vgs [ seq ] if ! vg unknown_seq << verify_gift elsif vg . server_id != self . id invalid_server_id << verify_gift elsif vg . gid != gid invalid_gid << verify_gift elsif vg . verified_at_server . class != NilClass if ( verified_at_server != vg . verified_at_server or error != vg . error or direction != vg . direction ) invalid_response << verify_gift else identical_response << verify_gift end else ok_response << verify_gift vg . verified_at_server = verified_at_server vg . error = error vg . response_mid = mid vg . direction = direction logger . debug2 "vg before save = #{vg.to_json}" vg . save! logger . debug2 "vg after save = #{vg.to_json}" logger . debug2 "todo: check for pending verify comments request waiting for this verify gifts response" vc = VerifyComment . find_by_server_seq ( vg . server_seq ) if vc logger . debug2 "found old verify comments request waiting for verify gifts response. vc = #{vc.to_json}" pending_verify_comms_requests << vc end end end logger . debug2 "verify_gifts.size = #{verify_gifts.size}, unknown_seq.size = #{unknown_seq.size}, " + "invalid_server_id.size = #{invalid_server_id.size}, invalid_gid.size = #{invalid_gid.size}, " + "invalid_response.size = #{invalid_response.size}, identical_response.size = #{identical_response.size}, " + "ok_response.size = #{ok_response.size}, old_pending_vc_requests.size = #{pending_verify_comms_requests.size}" pending_verify_comms_requests . each do | vc | logger . debug2 "repeat old verify comments request. vc = #{vc.to_json}" local_verify_comments_request = [ JSON . parse ( vc . original_client_request ) ] login_user_ids = JSON . parse ( vc . login_user_ids ) local_verify_comments_response = Comment . verify_comments ( local_verify_comments_request , login_user_ids , nil , nil ) logger . debug2 "local_verify_comments_request = #{local_verify_comments_request}" logger . debug2 "local_verify_comments_response = #{local_verify_comments_response}" if ! local_verify_comments_response error = "System error. Empty Comment.verify_comments response while repeating old pending verify comments request" logger . debug2 error local_verify_comments_response = { :error => error } elsif local_verify_comments_response [ :error ] local_verify_comments_response . delete ( :comments ) else if local_verify_comments_response [ :comments ] . class != Array or local_verify_comments_response [ :comments ] . size != 1 error = "System error. Expected one and only one Comment.verify_comments response row while repeating old pending verify comments request" local_verify_comments_response = { :error => error } end end message_hash = { :msgtype => 'verify_comments' , :mid => Sequence . next_server_mid , :request_mid => mid } message_hash [ :verify_comments ] = local_verify_comments_response [ :comments ] if local_verify_comments_response [ :comments ] message_hash [ :error ] = local_verify_comments_response [ :error ] if local_verify_comments_response [ :error ] logger . debug2 "todo: witch mid to use in verify comments response with old verify comments requests?" logger . debug2 "message_hash = #{message_hash}" logger . debug2 "vc = #{vc.to_json}" json_schema = :verify_comments_response if ! JSON_SCHEMA . has_key? json_schema logger . debug2 "todo: send could not validate verify_comments response. JSON schema definition #{json_schema.to_s} was not found error message to other Gofreerev server" return [ "Could not validate verify_comments response. JSON schema definition #{json_schema.to_s} was not found." , false ] end json_errors = JSON :: Validator . fully_validate ( JSON_SCHEMA [ json_schema ] , message_hash ) if json_errors . size > 0 logger . error2 "Failed to return remote verify comments response. Error in #{json_schema}" logger . error2 "message = #{message_hash}" logger . error2 "json_schema = #{JSON_SCHEMA[json_schema]}" logger . error2 "errors = #{json_errors.join(', ')}" logger . debug2 "todo: send Failed to return remote verify comments response. Error in #{json_schema} 1error message to other Gofreerev server" return "Failed to return remote verify comments response: #{json_errors.join(', ')}" end key , message = self . mix_encrypt_message_hash message_hash m = Message . new m . from_did = SystemParameter . did m . to_did = self . new_did m . server = true m . key = key m . message = message m . mid = message_hash [ :mid ] m . save! logger . debug2 "correct to_did in verify comments response message?" logger . debug2 "m = #{m.to_json}" logger . debug2 "vc = #{vc.to_json}" end if unknown_seq . size + invalid_server_id . size + invalid_gid . size + invalid_response . size > 0 errors = [ "#{unknown_seq.size + invalid_server_id.size + invalid_gid.size + invalid_response.size} rows in verify gift response" ] errors << "Unknown or deleted seqs: " + unknown_seq . collect { | vg | vg [ 'seq' ] } . join ( ', ' ) if unknown_seq . size > 0 errors << "Invalid seqs (other server): " + invalid_server_id . collect { | vg | vg [ 'seq' ] } . join ( ', ' ) if invalid_server_id . size > 0 errors << "Invalid gids: " + invalid_gid . collect { | vg | vg [ 'seq' ] } . join ( ', ' ) if invalid_gid . size > 0 errors << "Changed response: " + invalid_response . collect { | vg | vg [ 'seq' ] } . join ( ', ' ) if invalid_gid . size > 0 message_hash = { :msgtype => 'error' , :mid => Sequence . next_server_mid , :request_mid => request_mid , :response_mid => mid , :error => errors . join ( ', ' ) } logger . debug2 "error message = #{message_hash}" key , message = self . mix_encrypt_message_hash message_hash m = Message . new m . from_did = SystemParameter . did m . to_did = self . new_did m . server = true m . key = key m . message = message m . mid = message_hash . mid m . save! return errors . join ( ', ' ) end return nil end
receive verify gifts response from other Gofreerev server
receive verify gifts response from other Gofreerev server
# seq in verify_gifts must be unique # seq must be in verify_gifts table (see Gift.verify_gifts) # has already received a different response for this request # save response for remote gift verification. Response will be returned to client in next ping # - row in verify_comments table with identical server_seq, request_mid and with login_user_ids # must repeat original verify comments request # original verify comments request can by a client ping and must be returned in /util/ping response # original verify comments request can be a server to server request and must be returned in a verify gifts server to server response # each verify_gift # loop for each pending verify_comments requests. execute verify_comments request with correct login_users and set response to correct server # comment verify requests ready for local verification # client_sid = client_sha256 = nil (local verification) # empty response! # error response with fatal error. Ignore any verify comments responses # ok response # format response - note - no login users array in verify comments response # validate json message before "sending" # save server to server message - will be procesed in next ping # pending_verify_comms_requests loop # send server to server error message
mid
<not_specific>
request_mid
<not_specific>
verify_gifts
<not_specific>
error
<not_specific>