Exemple #1
0
   void WalletDb::store_account( const AccountData& account )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");
       FC_ASSERT( account.name != string() );
       FC_ASSERT( account.owner_key != PublicKeyType() );

       oWalletAccountEntry account_entry = lookup_account( account.owner_address() );
       if( !account_entry.valid() )
           account_entry = WalletAccountEntry();

       AccountData& temp = *account_entry;
       temp = account;

       store_and_reload_entry( *account_entry );

       set<PublicKeyType> account_public_keys;
       account_public_keys.insert( account_entry->owner_key );
       for( const auto& active_key_item : account_entry->active_key_history )
       {
           const PublicKeyType& active_key = active_key_item.second;
           if( active_key == PublicKeyType() ) continue;
           account_public_keys.insert( active_key );
       }
       if( account.is_delegate() )
       {
           for( const auto& item : account.delegate_info->signing_key_history )
           {
               const PublicKeyType& signing_key = item.second;
               if( signing_key == PublicKeyType() ) continue;
               account_public_keys.insert( signing_key );
           }
       }

       for( const PublicKeyType& account_public_key : account_public_keys )
       {
           const Address account_address = Address( account_public_key );
           oWalletKeyEntry key_entry = lookup_key( account_address );
           if( !key_entry.valid() )
           {
               KeyData key;
               key.account_address = account_address;
               key.public_key = account_public_key;
               store_key( key );
           }
           else if( key_entry->has_private_key() )
           {
               if( !account_entry->is_my_account )
               {
                   account_entry->is_my_account = true;
                   store_and_reload_entry( *account_entry );
               }

               if( key_entry->account_address != account_entry->owner_address() )
               {
                   key_entry->account_address = account_entry->owner_address();
                   store_key( *key_entry );
               }
           }
       }
   } FC_CAPTURE_AND_RETHROW( (account) ) }
   void wallet_db::store_account( const account_data& account )
   { try {
       FC_ASSERT( is_open() );
       FC_ASSERT( account.name != string() );
       FC_ASSERT( account.owner_key != public_key_type() );

       owallet_account_record account_record = lookup_account( account.owner_address() );
       if( !account_record.valid() )
           account_record = wallet_account_record();

       account_data& temp = *account_record;
       temp = account;

       store_and_reload_record( *account_record );

       set<public_key_type> account_public_keys;
       account_public_keys.insert( account_record->owner_key );
       for( const auto& active_key_item : account_record->active_key_history )
       {
           const public_key_type& active_key = active_key_item.second;
           if( active_key == public_key_type() ) continue;
           account_public_keys.insert( active_key );
       }
       if( account.is_delegate() )
       {
           for( const auto& item : account.delegate_info->signing_key_history )
           {
               const public_key_type& signing_key = item.second;
               if( signing_key == public_key_type() ) continue;
               account_public_keys.insert( signing_key );
           }
       }

       for( const public_key_type& account_public_key : account_public_keys )
       {
           const address account_address = address( account_public_key );
           owallet_key_record key_record = lookup_key( account_address );
           if( !key_record.valid() )
           {
               key_data key;
               key.account_address = account_address;
               key.public_key = account_public_key;
               store_key( key );
           }
           else if( key_record->has_private_key() )
           {
               if( !account_record->is_my_account )
               {
                   account_record->is_my_account = true;
                   store_and_reload_record( *account_record );
               }

               if( key_record->account_address != account_record->owner_address() )
               {
                   key_record->account_address = account_record->owner_address();
                   store_key( *key_record );
               }
           }
       }
   } FC_CAPTURE_AND_RETHROW( (account) ) }
   public_key_type wallet_db::generate_new_account( const fc::sha512& password, const string& account_name,
                                                    const private_key_type& owner_private_key )
   { try {
       FC_ASSERT( is_open() );

       owallet_account_record account_record = lookup_account( account_name );
       FC_ASSERT( !account_record.valid(), "Wallet already contains an account with that name!" );

       const public_key_type owner_public_key = owner_private_key.get_public_key();
       const address owner_address = address( owner_public_key );

       const private_key_type active_private_key = get_account_child_key( owner_private_key, 0 );
       const public_key_type active_public_key = active_private_key.get_public_key();
       const address active_address = address( active_public_key );

       account_record = lookup_account( owner_address );
       FC_ASSERT( !account_record.valid(), "Wallet already contains an account with that key!" );

       account_record = lookup_account( active_address );
       FC_ASSERT( !account_record.valid(), "Wallet already contains an account with the new derived active key!" );

       owallet_key_record key_record = lookup_key( owner_address );
       FC_ASSERT( !key_record.valid() || !key_record->has_private_key(), "Wallet already contains that key!" );

       key_record = lookup_key( active_address );
       FC_ASSERT( !key_record.valid() || !key_record->has_private_key(), "Wallet already contains the new derived active key!" );

       key_data active_key;
       active_key.account_address = owner_address;
       active_key.public_key = active_public_key;
       active_key.encrypt_private_key( password, active_private_key );

       key_data owner_key;
       owner_key.account_address = owner_address;
       owner_key.public_key = owner_public_key;
       owner_key.encrypt_private_key( password, owner_private_key );

       account_data account;
       account.name = account_name;
       account.owner_key = owner_public_key;
       account.set_active_key( blockchain::now(), active_public_key );
       account.last_update = blockchain::now();

       store_key( active_key );
       store_key( owner_key );
       store_account( account );

       return owner_public_key;
   } FC_CAPTURE_AND_RETHROW( (account_name) ) }
Exemple #4
0
btree_status_t
btree_range_query_start_fast(btree_raw_t            *btree,
                             btree_indexid_t         indexid,
                             btree_range_cursor_t    *c,
                             btree_range_meta_t      *rmeta)
{
	int pathcnt;
	btree_raw_mem_node_t node;

	c->dir = IS_ASCENDING_QUERY(rmeta) ? 1 : -1;

	c->ts_key = malloc(btree->max_key_size);
	if (c->ts_key == NULL) {
		assert(0);
		c->ts_key = NULL;
		return BTREE_FAILURE;
	}
	c->ts_keylen = 0;
	c->ts_data = NULL;
	c->ts_datalen = 0;
	c->key_last_seq_unknown = false;
	c->last_logical_id = 0;
	c->is_tombstone = false;

	if(!store_key(&c->query_meta.key_start, &c->query_meta.keylen_start,
			rmeta->key_start, rmeta->keylen_start))
		return BTREE_FAILURE;

	return BTREE_SUCCESS;
}
   void wallet_db::add_account( const account_record& blockchain_account,
                                const variant& private_data  )
   { try {
      wallet_account_record war;
      account_record& tmp  = war;
      tmp = blockchain_account;
      war.private_data = private_data;
      war.account_address = address(blockchain_account.owner_key);

      war.wallet_record_index = new_wallet_record_index();
      if (has_private_key(war.account_address))
          war.is_my_account = true;
      store_record( war );

      auto current_key = lookup_key( blockchain_account.owner_key );
      if( current_key.valid() )
      {  
         current_key->account_address = address(blockchain_account.owner_key);
         store_record( *current_key );
      }
      else
      {
         wallet_key_record new_key;
         new_key.wallet_record_index = new_wallet_record_index();
         new_key.account_address = address(blockchain_account.owner_key);
         new_key.public_key = blockchain_account.active_key();
         my->load_key_record( new_key, false );
         store_key( new_key );
      }
   } FC_CAPTURE_AND_RETHROW( (blockchain_account) ) }
Exemple #6
0
static int do_whole_file(const char *filename)
{
  errno = 0;
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    error("can't open `%1': %2", filename, strerror(errno));
    return 0;
  }
  int count = 0;
  int key_len = 0;
  int c;
  while ((c = getc(fp)) != EOF) {
    if (csalnum(c)) {
      key_len = 1;
      key_buffer[0] = c;
      while ((c = getc(fp)) != EOF) {
	if (!csalnum(c))
	  break;
	if (key_len < truncate_len)
	  key_buffer[key_len++] = c;
      }
      if (store_key(key_buffer, key_len)) {
	if (++count >= max_keys_per_item)
	  break;
      }
      if (c == EOF)
	break;
    }
  }
  store_reference(filenames.length(), 0, 0);
  store_filename(filename);
  fclose(fp);
  return 1;
}
Exemple #7
0
Fichier : ht.c Projet : taysom/tau
STATIC void rec_copy(Node_s *dst, int a, Node_s *src, int b)
{
	Hrec_s rec = get_rec(src, b);

	store_lump(dst, rec.val);
	store_key(dst, rec.key);
	store_end(dst, a);
}
   void wallet_db::cache_memo( const memo_status& memo,
                               const fc::ecc::private_key& account_key,
                               const fc::sha512& password )
   {
      key_data data;
      data.account_address = address(account_key.get_public_key());
      //data.memo = memo_data(memo);
      data.valid_from_signature = memo.has_valid_signature;
      data.encrypt_private_key( password, memo.owner_private_key );

      store_key( data );
   }
Exemple #9
0
 void WalletDb::cache_memo( const MemoStatus& memo,
                             const PrivateKeyType& account_key,
                             const fc::sha512& password )
 {
    KeyData data;
    data.account_address = Address( account_key.get_public_key() );
    data.public_key = memo.owner_private_key.get_public_key();
    data.encrypt_private_key( password, memo.owner_private_key );
    data.valid_from_signature = memo.has_valid_signature;
    //data.memo = memo_data( memo );
    store_key( data );
 }
Exemple #10
0
   wallet_account_record wallet_db::store_account( const account_data& account )
   { try {
       FC_ASSERT( is_open() );
       FC_ASSERT( account.name != string() );
       FC_ASSERT( account.owner_key != public_key_type() );

       owallet_account_record account_record = lookup_account( account.owner_address() );
       if( !account_record.valid() )
           account_record = wallet_account_record();

       account_data& temp = *account_record;
       temp = account;

       store_and_reload_record( *account_record );

       const auto index_key = [ & ]( const public_key_type& account_public_key )
       {
           const address account_address = address( account_public_key );
           owallet_key_record key_record = lookup_key( account_address );
           if( !key_record.valid() )
           {
               key_data key;
               key.account_address = account_address;
               key.public_key = account_public_key;
               store_key( key );
           }
           else if( key_record->has_private_key() )
           {
               if( key_record->account_address != account_record->owner_address() )
               {
                   key_record->account_address = account_record->owner_address();
                   store_key( *key_record );
               }
           }
       };
       account_record->scan_public_keys( index_key );

       return *account_record;
   } FC_CAPTURE_AND_RETHROW( (account) ) }
Exemple #11
0
static void possibly_store_key(char *s, int len)
{
  static int last_tagno = -1;
  static int key_count;
  if (last_tagno != ntags) {
    last_tagno = ntags;
    key_count = 0;
  }
  if (key_count < max_keys_per_item) {
    if (store_key(s, len))
      key_count++;
  }
}
Exemple #12
0
   public_key_type wallet_db::generate_new_account( const fc::sha512& password, const string& account_name, const variant& private_data )
   { try {
       FC_ASSERT( is_open() );

       owallet_account_record account_record = lookup_account( account_name );
       FC_ASSERT( !account_record.valid(), "Wallet already contains an account with that name!" );

       uint32_t key_index = get_last_wallet_child_key_index();
       private_key_type account_private_key;
       public_key_type account_public_key;
       address account_address;
       while( true )
       {
           ++key_index;
           FC_ASSERT( key_index != 0, "Overflow!" );

           account_private_key = get_wallet_child_key( password, key_index );
           account_public_key = account_private_key.get_public_key();
           account_address = address( account_public_key );

           account_record = lookup_account( account_address );
           if( account_record.valid() ) continue;

           owallet_key_record key_record = lookup_key( account_address );
           if( key_record.valid() && key_record->has_private_key() ) continue;

           break;
       }

       key_data key;
       key.account_address = account_address;
       key.public_key = account_public_key;
       key.encrypt_private_key( password, account_private_key );
       key.gen_seq_number = key_index;

       account_data account;
       account.name = account_name;
       account.owner_key = account_public_key;
       account.set_active_key( blockchain::now(), account_public_key );
       account.private_data = private_data;
       account.is_my_account = true;

       set_last_wallet_child_key_index( key_index );
       store_key( key );
       store_account( account );

       return account_public_key;
   } FC_CAPTURE_AND_RETHROW( (account_name) ) }
Exemple #13
0
   PrivateKeyType WalletDb::generate_new_one_time_key( const fc::sha512& password )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");
       const PrivateKeyType one_time_private_key = PrivateKeyType::generate();

       const Address one_time_address = Address( one_time_private_key.get_public_key() );
       const oWalletKeyEntry key_entry = lookup_key( one_time_address );
       FC_ASSERT( !key_entry.valid() );

       KeyData key;
       key.public_key = one_time_private_key.get_public_key();
       key.encrypt_private_key( password, one_time_private_key );
       store_key( key );

       return one_time_private_key;
   } FC_CAPTURE_AND_RETHROW() }
Exemple #14
0
   private_key_type wallet_db::generate_new_one_time_key( const fc::sha512& password )
   { try {
       FC_ASSERT( is_open() );
       const private_key_type one_time_private_key = private_key_type::generate();

       const address one_time_address = address( one_time_private_key.get_public_key() );
       const owallet_key_record key_record = lookup_key( one_time_address );
       FC_ASSERT( !key_record.valid() );

       key_data key;
       key.public_key = one_time_private_key.get_public_key();
       key.encrypt_private_key( password, one_time_private_key );
       store_key( key );

       return one_time_private_key;
   } FC_CAPTURE_AND_RETHROW() }
Exemple #15
0
   PrivateKeyType WalletDb::generate_new_account_child_key( const fc::sha512& password, const string& account_name )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");

       oWalletAccountEntry account_entry = lookup_account( account_name );
       FC_ASSERT( account_entry.valid(), "Account not found!" );
       FC_ASSERT( !account_entry->is_retracted(), "Account has been retracted!" );
       FC_ASSERT( account_entry->is_my_account, "Not my account!" );

       const oWalletKeyEntry key_entry = lookup_key( Address( account_entry->active_key() ) );
       FC_ASSERT( key_entry.valid(), "Active key not found!" );
       FC_ASSERT( key_entry->has_private_key(), "Active private key not found!" );

       const PrivateKeyType active_private_key = key_entry->decrypt_private_key( password );
       uint32_t seq_num = account_entry->last_used_gen_sequence;
       PrivateKeyType account_child_private_key;
       PublicKeyType account_child_public_key;
       Address account_child_address;
       while( true )
       {
           ++seq_num;
           FC_ASSERT( seq_num != 0, "Overflow!" );

           account_child_private_key = get_account_child_key( active_private_key, seq_num );
           account_child_public_key = account_child_private_key.get_public_key();
           account_child_address = Address( account_child_public_key );

           oWalletKeyEntry key_entry = lookup_key( account_child_address );
           if( key_entry.valid() && key_entry->has_private_key() ) continue;

           break;
       }

       account_entry->last_used_gen_sequence = seq_num;

       KeyData key;
       key.account_address = account_entry->owner_address();
       key.public_key = account_child_public_key;
       key.encrypt_private_key( password, account_child_private_key );
       key.gen_seq_number = seq_num;

       store_account( *account_entry );
       store_key( key );

       return account_child_private_key;
   } FC_CAPTURE_AND_RETHROW( (account_name) ) }
   void wallet_db::add_account( const string& new_account_name,
                                const public_key_type& new_account_key,
                                const variant& private_data )
   {
      auto current_account_itr = name_to_account_wallet_record_index.find( new_account_name );
      FC_ASSERT( current_account_itr == name_to_account_wallet_record_index.end(),
                 "Account with name ${name} already exists",
                 ("name",new_account_name) );
      auto current_address_itr = address_to_account_wallet_record_index.find( new_account_key );
      FC_ASSERT( current_address_itr == address_to_account_wallet_record_index.end(),
                 "Account with address ${address} already exists",
                 ("name",new_account_key) );

      wallet_account_record war;
      war.name = new_account_name;
      war.id = 0;
      war.account_address = address( new_account_key );
      war.owner_key = new_account_key;
      war.set_active_key( blockchain::now(), new_account_key );
      war.private_data = private_data;

      war.wallet_record_index = new_wallet_record_index();
      if (has_private_key(war.account_address))
          war.is_my_account = true;
      store_record( war );

      auto current_key = lookup_key( new_account_key );
      if( current_key )
      {
         current_key->account_address = address(new_account_key);
         store_record( *current_key, true );
      }
      else
      {
         wallet_key_record new_key;
         new_key.wallet_record_index = new_wallet_record_index();
         new_key.account_address = address(new_account_key);
         new_key.public_key = new_account_key;
         my->load_key_record( new_key, false );
         store_key( new_key );
      }
   }
   fc::ecc::private_key wallet_db::new_private_key( const fc::sha512& password, 
                                                    const address& parent_account_address )
   {
      FC_ASSERT( wallet_master_key.valid() );

      auto master_ext_priv_key  = wallet_master_key->decrypt_key( password );
      auto new_priv_key = master_ext_priv_key.child( new_key_child_index() );

      key_data new_key;
      new_key.account_address = parent_account_address;
      new_key.encrypt_private_key( password, new_priv_key );
      // if there is no parent account address, then the account_address of this key is itself
      if (parent_account_address == address())
      {
         new_key.account_address = address(new_key.public_key);
      }

      store_key( new_key );
      return new_priv_key;
   }
Exemple #18
0
   void WalletDb::import_key( const fc::sha512& password, const string& account_name, const PrivateKeyType& private_key,
                               bool move_existing )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");

       oWalletAccountEntry account_entry = lookup_account( account_name );
       FC_ASSERT( account_entry.valid(), "Account name not found!" );

       const PublicKeyType public_key = private_key.get_public_key();

       oWalletKeyEntry key_entry = lookup_key( Address( public_key ) );
       if( !key_entry.valid() )
           key_entry = WalletKeyEntry();
       else if( !move_existing )
           FC_ASSERT( key_entry->account_address == account_entry->owner_address() );

       key_entry->account_address = account_entry->owner_address();
       key_entry->public_key = public_key;
       key_entry->encrypt_private_key( password, private_key );

       store_key( *key_entry );
   } FC_CAPTURE_AND_RETHROW( (account_name)(move_existing) ) }
Exemple #19
0
   void wallet_db::import_key( const fc::sha512& password, const string& account_name, const private_key_type& private_key,
                               bool move_existing )
   { try {
       FC_ASSERT( is_open() );

       owallet_account_record account_record = lookup_account( account_name );
       FC_ASSERT( account_record.valid(), "Account name not found!" );

       const public_key_type public_key = private_key.get_public_key();

       owallet_key_record key_record = lookup_key( address( public_key ) );
       if( !key_record.valid() )
           key_record = wallet_key_record();
       else if( !move_existing )
           FC_ASSERT( key_record->account_address == account_record->owner_address() );

       key_record->account_address = account_record->owner_address();
       key_record->public_key = public_key;
       key_record->encrypt_private_key( password, private_key );

       store_key( *key_record );
   } FC_CAPTURE_AND_RETHROW( (account_name)(move_existing) ) }
Exemple #20
0
int goonindex(int argc, char *argv[])
{
    struct option opts[] = {
        {"seqkey", required_argument, NULL, 's'},
        {"startkey", required_argument, NULL, 'b'},
        {"endkey", required_argument, NULL, 'e'},
        {"zerobased", no_argument, NULL, '0'},
        {"rightopen", no_argument, NULL, 'r'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };
    struct stat f_stat;
    int c;
    ti_conf_t conf;

    if (argc == 1) {
        USAGE;
        return -1;
    }

    INIT_CONF(&conf);

    while ((c = getopt_long(argc,
                            argv,
                            "s:b:e:0rh",
                            opts,
                            NULL)) != -1) {
        switch (c) {
            case 'h': USAGE;
                      return -1;
            case 's': if (store_key(optarg, conf.sk) != 0) {
                          fprintf(stderr, "could not store key\n");
                          return -1;
                      }
                      break;
            case 'b': if (store_key(optarg, conf.bk) != 0) {
                          fprintf(stderr, "could not store key\n");
                          return -1;
                      }
                      break;
            case 'e': if (store_key(optarg, conf.ek) != 0) {
                          fprintf(stderr, "could not store key\n");
                          return -1;
                      }
                      break;
            case '0': conf.zerobased = 1;
                      break;
            case 'r': conf.rightopen = 1;
                      break;
            default: return -1;
        }
    }

    if (conf.sk[0] == '\0' || conf.bk[0] == '\0') {
        fprintf(stderr, "error: missing mandatory argument\n");
        return -1;
    }

    if (conf.ek[0] == '\0' && conf.rightopen) {
        fprintf(stderr, "error: options `rightopen` not allowed with single "
                        "position\n");
        return -1;
    }

    if (optind == argc) {
        fprintf(stderr, "error: missing file name\n");
        return -1;
    }

    if (stat(argv[optind], &f_stat) != 0) {
        fprintf(stderr, "error: input file does not exist\n");
        return -1;
    }

    if (bgzf_is_bgzf(argv[optind]) != 1) {
        fprintf(stderr, "error: input file is not in bgzip format\n");
        return -1;
    }

    return build_gn_index(argv[optind], &conf);
}
Exemple #21
0
static
btree_status_t
btree_range_get_next_fast(btree_range_cursor_t *c,
                     int                   n_in,
                     int                  *n_out,
                     btree_range_data_t   *values)
{
#define START_EDGE 1
#define END_EDGE 2
#define TREE_DEPTH_MAX 32
	struct {
		btree_raw_mem_node_t* node;
		int16_t start_idx, end_idx, cur_idx;
		uint8_t edge;
	} stack[TREE_DEPTH_MAX], *cur, *child;

	key_stuff_info_t ks;
	btree_range_meta_t *meta = &c->query_meta;
	btree_status_t ret = BTREE_SUCCESS, r = BTREE_SUCCESS;
	int sp = 0;

	*n_out = 0;

	/* Right now it is not required to keep track of tombstone
	 * across two chunks of range queries */
	c->prior_version_tombstoned = false;

	if (!btree_in_rescue_mode(c->btree)) {
		plat_rwlock_rdlock(&c->btree->lock);
	}

	ret = btree_range_find_diversion(c);
	if (ret != BTREE_SUCCESS) {
		sp = -1;
		goto cleanup;
	}

	stack[0].node = c->node;
	stack[0].cur_idx = c->start_idx;
	stack[0].start_idx = c->start_idx;
	stack[0].end_idx = c->end_idx;
	stack[0].edge = START_EDGE | END_EDGE;

	while(sp >= 0 && !fatal(ret)) {
		cur = stack + sp;
		child = cur + 1;

		int isleaf = is_leaf(c->btree, cur->node->pnode);

		dbg_print("sp=%d cur_idx %d start_idx %d end_idx %d is_leaf %d logical_id %ld\n", sp, cur->cur_idx, cur->start_idx, cur->end_idx, isleaf, cur->node->pnode->logical_id);

		if(isleaf) {
			if(*n_out >= n_in) {
//				fprintf(stderr, "Exit with all keys done.\n",);
				break;
			}

			if(cur->cur_idx * c->dir > cur->end_idx * c->dir)
				ret = BTREE_FAILURE;
			else
				populate_output_array(c, cur->node, &cur->cur_idx, cur->end_idx, n_in, n_out, values, &ret);
		} else if(cur->cur_idx != cur->end_idx) {
			uint64_t logical_id = cur->node->pnode->rightmost;
			if(cur->cur_idx < cur->node->pnode->nkeys) {
				ks.key = tmp_key_buf;
				get_key_stuff_info2(c->btree, cur->node->pnode, cur->cur_idx, &ks);
				logical_id = ks.ptr;
			}

			getnode_flags_t nflags = NODE_CACHE_VALIDATE;
			if (is_snapshot_query(meta)) nflags |= NODE_CACHE_DEREF_DELETE;

			child->node = get_existing_node(&r, c->btree, logical_id, nflags, LOCKTYPE_READ);
			if (child->node == NULL) {
				if (storage_error(r) && btree_in_rescue_mode(c->btree)) {
					add_to_rescue(c->btree, cur->node->pnode, logical_id, cur->cur_idx);
				}
				ret = r;
				goto cleanup;
			}

			ks.key = NULL;
			ks.keylen = 0;

			child->cur_idx = child->start_idx = c->dir > 0 ? 0 : child->node->pnode->nkeys;
			if(cur->cur_idx == cur->start_idx && (cur->edge & START_EDGE)) {
				child->edge |= START_EDGE;
				child->cur_idx = child->start_idx = bsearch_start(c, child->node);
			}

			child->end_idx = c->dir > 0 ? child->node->pnode->nkeys : 0;
			if(cur->cur_idx == cur->end_idx - c->dir && (cur->edge & END_EDGE)) {
				child->edge |= END_EDGE;
				child->end_idx = bsearch_end(c, child->node);
			}

			if(!is_leaf(c->btree, child->node->pnode))
				child->end_idx += c->dir;
			else if(c->dir < 0) {
				child->end_idx += c->dir;
				child->start_idx += c->dir;
				child->cur_idx += c->dir;
			}
			sp++; //call
		}
		if(cur->cur_idx == cur->end_idx) {
			plat_rwlock_unlock(&cur->node->lock);
			deref_l1cache_node(c->btree, cur->node);
			sp--; //return
		}
		if(!isleaf)
			cur->cur_idx += c->dir;
	}

cleanup:
	if (!btree_in_rescue_mode(c->btree)) {
		plat_rwlock_unlock(&c->btree->lock);

		if (storage_error(ret)) {
			set_lasterror_rquery(c->btree, meta);
		}
	}

	if ((sp < 0) && (!fatal(ret)) && (c->dir < 0)) {
		if (c->key_last_seq_unknown) {
			int16_t idx = 0;
			populate_output_array(c, NULL, &idx, idx, n_in, n_out, values, &ret);
		}
	}

	if(!fatal(ret) && !*n_out)
		ret = BTREE_QUERY_DONE;

	if(ret != BTREE_FAILURE && ret != BTREE_QUERY_DONE && !storage_error(ret)) {
		assert(*n_out);
		store_key(&meta->key_start, &meta->keylen_start, values[*n_out - 1].key, values[*n_out - 1].keylen);
		dbg_print_key(values[*n_out - 1].key, values[*n_out - 1].keylen, "last_key");
		meta->flags &= ~(RANGE_START_LE | RANGE_START_LT | RANGE_START_GE | RANGE_START_GT);
		if(ret == BTREE_QUERY_PAUSED)
			meta->flags |= c->dir > 0 ? RANGE_START_GE : RANGE_START_LE;
		else
			meta->flags |= c->dir > 0 ? RANGE_START_GT : RANGE_START_LT;
	}

//	fprintf(stderr, "loop exit: sp=%d fatal=%d\n", sp, fatal(ret));

	while(sp >= 0) {
		plat_rwlock_unlock(&stack[sp].node->lock);
		deref_l1cache_node(c->btree, stack[sp].node);
		sp--; //return
	}

	dbg_print("ret=%d sp=%d n_in=%d n_out=%d\n", ret, sp, n_in, *n_out);

	return ret;
}
Exemple #22
0
/* Start an index query.
 * 
 * Returns: BTREE_SUCCESS if successful
 *          BTREE_FAILURE if unsuccessful
 */
btree_status_t
btree_raw_range_query_start(btree_raw_t             *btree, 
                            btree_indexid_t         indexid,
                            btree_range_cursor_t    **cursor,
                            btree_range_meta_t      *rmeta)
{
	btree_metadata_t  meta;
	btree_status_t       status;
	btree_range_cursor_t *c;

#ifdef DEBUG_STUFF
	if (indexid != BTREE_RANGE_PRIMARY_INDEX) {
		fprintf(stderr, "Index other than primary index is not "
		                "supported yet\n");
		return (BTREE_FAILURE);
	}

	if (rmeta->flags & RANGE_PRIMARY_KEY) {
		fprintf(stderr, "Primary key retrival for secondary index not "
		                "supported yet\n");
		return (BTREE_FAILURE);
	}
#endif

	status = validate_range_query(rmeta);
	if (status != BTREE_SUCCESS) {
		return status;
	}

	int i = 0;
	while(i < N_CURSOR_MAX && __thread_cursor[i].btree)
		i++;

	if(i >= N_CURSOR_MAX) {
		fprintf(stderr, "Limit(%d) on number of concurrent range queries exceeded\n", N_CURSOR_MAX);
		return BTREE_FAILURE;
	}

	c = &__thread_cursor[i];

	/* Initialize the cursor, to accomplish further queries */
	c->btree = btree;
	c->cguid = c->btree->cguid;

	memcpy(&c->query_meta, rmeta, sizeof(btree_range_meta_t));

	c->query_meta.key_end = NULL;
	c->query_meta.key_start = NULL;
	c->dir = 1;

	store_key(&c->query_meta.key_end, &c->query_meta.keylen_end,
			rmeta->key_end, rmeta->keylen_end);

	*cursor = c;

	dbg_print_key(rmeta->key_start, rmeta->keylen_start, "start");
	dbg_print_key(rmeta->key_end, rmeta->keylen_end, "end");

	if(rmeta->flags & RANGE_INPLACE_POINTERS)
	{
		assert(IS_ASCENDING_QUERY(rmeta));
		return btree_range_query_start_inplace(btree, indexid, c, rmeta);
	}

	return  btree_range_query_start_fast(btree, indexid, c, rmeta);
}
Exemple #23
0
LONG EXPENTRY standardEvt(MIDIFILE * mf)
{
    register UCHAR chr;
    register USHORT val;

    /* Store the Time of this event in MIDIFILE's Time field */
    mf->Time = TrkPtr->Time;

    /* Get the Status for this event */
    chr = mf->Status = TrkPtr->Status;

    /* Is this a Meta-Event? (ie, we use meta's Type for the status, and these values are always
	less than 0x80) */
    if ( !(chr & 0x80) )
    {
	 /* Set Status to 0xFF */
	 mf->Status = 0xFF;
	 /* Set Data[0] = Type */
	 mf->Data[0] = chr;
	 switch (chr)
	 {
	      /* NOTE: MIDIFILE.DLL sets Data[1] to the proper length for the fixed length
		  meta types (ie, 0x00, 0x2F, 0x51, 0x54, 0x58, and 0x59). */

	      /* ------- Sequence # ------- */
	      /* NOTE: If we use a MetaSeqNum callback, we wouldn't write out a Meta-Event
		 here, and so this case wouldn't be needed. */
	      case 0x00:
		   /* Note the recasting of the EVENT and the MIDIFILE structures to the versions
		       appropriate for a SEQUENCE NUMBER Meta-Event. */
		   store_seq( (SEQEVENT *)TrkPtr, (METASEQ *)mf );
		   break;

	      /* ------- Set Tempo -------- */
	      case 0x51:
		   store_tempo( (TEMPOEVENT *)TrkPtr, (METATEMPO *)mf );
		   break;

	      /* --------- SMPTE --------- */
	      case 0x54:
		   /* Right now, let's ignore SMPTE events. This was too much of a hassle
		       since there are too many data bytes to fit into the 8 bytes that I use to
		       express events internally in this program. */
		   break;

	      /* ------- End of Track ------ */
	      case 0x2F:
		   printf("Closing track #%ld...\r\n", mf->TrackNum);
		   break;

	      /* ------ Time Signature ----- */
	      case 0x58:
		   store_time( (TIMEEVENT *)TrkPtr, (METATIME *)mf );
		   break;

	      /* ------ Key Signature ------ */
	      case 0x59:
		   store_key( (KEYEVENT *)TrkPtr, (METAKEY *)mf );
		   break;

	      /* Must be a variable length Meta-Event (ie, type is 0x01 to 0x0F, or 0x7F) */
	      default:
		   store_meta( (TXTEVENT *)TrkPtr, (METATXT *)mf );
	 }
    }

    /* Must be a real MIDI event (as opposed to a MetaEvent) */
    else switch ( chr )
    {
	 /* SYSEX (0xF0) or SYSEX CONTINUATION (0xF7) */
	 case 0xF0:
	 case 0xF7:
	      store_sysex( (XEVENT *)TrkPtr, (METATXT *)mf );
	      break;

	 /* For other MIDI messages, they're all fixed length, and will fit into the MIDIFILE
	     structure, so we copy 2 more data bytes to the MIDIFILE (whether those data bytes
	     are valid or not -- the DLL takes care of writing the message out properly). */
	 default:
	      mf->Data[0] = TrkPtr->Data1;
	      mf->Data[1] = TrkPtr->Data2;
    }

    /* Advance the ptr to the next event (ie, for the next call to this function) */
    TrkPtr++;

    return(0);
}
Exemple #24
0
Fichier : ht.c Projet : taysom/tau
STATIC void store_rec(Node_s *node, Key_t key, Lump_s val, unint i)
{
	store_lump(node, val);
	store_key(node, key);
	store_end(node, i);
}
Exemple #25
0
struct super_block * reiserfs_read_super (struct super_block * s, void * data, int silent)
{
  int size;
  struct inode *root_inode;
  kdev_t dev = s->s_dev;
  int j;
  extern int *blksize_size[];
  struct reiserfs_transaction_handle th ;
  int old_format = 0;
  unsigned long blocks;
  int jinit_done = 0 ;

  memset (&s->u.reiserfs_sb, 0, sizeof (struct reiserfs_sb_info));

  if (parse_options ((char *) data, &(s->u.reiserfs_sb.s_mount_opt), &blocks) == 0) {
    s->s_dev = 0;
    return NULL;
  }

  if (blocks) {
  	printk("reserfs: resize option for remount only\n");
	return NULL;
  }	

  MOD_INC_USE_COUNT;
  lock_super (s);

  if (blksize_size[MAJOR(dev)] && blksize_size[MAJOR(dev)][MINOR(dev)] != 0) {
    /* as blocksize is set for partition we use it */
    size = blksize_size[MAJOR(dev)][MINOR(dev)];
  } else {
    size = BLOCK_SIZE;
    set_blocksize (s->s_dev, BLOCK_SIZE);
  }

  /* read block, containing reiserfs super block (it is stored at REISERFS_FIRST_BLOCK-th 1K block) */
  if (read_super_block (s, size)) {
	  if(read_old_super_block(s,size)) 
		  goto error;
	  else
		  old_format = 1;
  }

  s->u.reiserfs_sb.s_mount_state = le16_to_cpu (SB_DISK_SUPER_BLOCK (s)->s_state); /* journal victim */
  s->u.reiserfs_sb.s_mount_state = REISERFS_VALID_FS ;

  /* reiserfs can not be mounted when it propably contains errors */
#if 0 /* journal victim */
  if (le16_to_cpu (SB_DISK_SUPER_BLOCK (s)->s_state) != REISERFS_VALID_FS) {
    printk ("reiserfs_read_super:  mounting unchecked fs, run reiserfsck first\n");
    goto error;
  }
#endif
  if (old_format ? read_old_bitmaps(s) : read_bitmaps(s)) { 
	  printk ("reiserfs_read_super: unable to read bitmap\n");
	  goto error;
  }

  if (journal_init(s)) {
    printk("reiserfs_read_super: unable to initialize journal space\n") ;
    goto error ;
  } else {
    jinit_done = 1 ; /* once this is set, journal_release must be called
                     ** if we error out of the mount 
		     */
  }
  if (reread_meta_blocks(s)) {
    printk("reiserfs_read_super: unable to reread meta blocks after journal init\n") ;
    goto error ;
  }

  if (replay_only (s))
    goto error;

  /*s->s_op = &reiserfs_sops;*/
   
  /* get root directory inode */
  store_key (s, &root_key);
  root_inode = iget (s, root_key.k_objectid);
  forget_key (s, &root_key);
  if (!root_inode) {
    printk ("reiserfs_read_super: get root inode failed\n");
    goto error;
  }

  s->s_root = d_alloc_root(root_inode, NULL);  
  if (!s->s_root) {
    iput(root_inode);
    goto error;
  }

  if (!(s->s_flags & MS_RDONLY)) {
    SB_DISK_SUPER_BLOCK (s)->s_state = cpu_to_le16 (REISERFS_ERROR_FS);
    /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
    journal_begin(&th, s, 1) ;
    journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
    journal_end(&th, s, 1) ;
    s->s_dirt = 0;
  }

  /*s->u.reiserfs_sb.unpreserve = dont_preserve (s) ? 0 : unpreserve;*/
  /* we have to do this to make journal writes work correctly */
  SB_BUFFER_WITH_SB(s)->b_end_io = reiserfs_end_buffer_io_sync ;

  unlock_super (s);
  print_credits() ;
  printk("%s\n", reiserfs_get_version_string()) ;
  return s;

 error:
  if (jinit_done) { /* kill the commit thread, free journal ram */
    journal_release_error(NULL, s) ;
  }
  if (SB_DISK_SUPER_BLOCK (s)) {
    for (j = 0; j < le16_to_cpu (SB_DISK_SUPER_BLOCK (s)->s_bmap_nr); j ++) {
      if (SB_AP_BITMAP (s))
	brelse (SB_AP_BITMAP (s)[j]);
      /* if (SB_AP_CAUTIOUS_BITMAP (s))
	brelse (SB_AP_CAUTIOUS_BITMAP (s)[j]); */
    }
    if (SB_AP_BITMAP (s))
      reiserfs_kfree (SB_AP_BITMAP (s), sizeof (struct buffer_head *) * SB_BMAP_NR (s), s);
    /* if (SB_AP_CAUTIOUS_BITMAP (s))
      reiserfs_kfree (SB_AP_CAUTIOUS_BITMAP (s), sizeof (struct buffer_head *) * SB_BMAP_NR (s), s); */
  }
  if (SB_BUFFER_WITH_SB (s))
    brelse(SB_BUFFER_WITH_SB (s));
  s->s_dev = 0;
  unlock_super(s);
  MOD_DEC_USE_COUNT;

  return NULL;
}
Exemple #26
0
   void WalletDb::repair_entrys( const fc::sha512& password )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");

       vector<GenericWalletEntry> entrys;
       for( auto iter = my->_entrys.begin(); iter.valid(); ++iter )
           entrys.push_back( iter.value() );


       // Repair account_data.is_my_account and key_data.account_address
       uint32_t count = 0;
       for( GenericWalletEntry& entry : entrys )
       {
           try
           {
               if( WalletEntryTypeEnum( entry.type ) == account_entry_type )
               {
                   std::cout << "\rRepairing account entry " << std::to_string( ++count ) << std::flush;
                   WalletAccountEntry account_entry = entry.as<WalletAccountEntry>();
                   store_account( account_entry );
               }
           }
           catch( ... )
           {
           }
       }

       // Repair key_data.public_key when I have the private key and
       // repair key_data.account_address and account_data.is_my_account
       count = 0;
       for( GenericWalletEntry& entry : entrys )
       {
           try
           {
               if( WalletEntryTypeEnum( entry.type ) == key_entry_type )
               {
                   std::cout << "\rRepairing key entry     " << std::to_string( ++count ) << std::flush;
                   WalletKeyEntry key_entry = entry.as<WalletKeyEntry>();
                   if( key_entry.has_private_key() )
                   {
                       const PrivateKeyType private_key = key_entry.decrypt_private_key( password );
                       const PublicKeyType public_key = private_key.get_public_key();
                       if( key_entry.public_key != public_key )
                       {
                           const Address key_address = key_entry.get_address();
                           keys.erase( key_address );
                           btc_to_gop_address.erase( key_address );
                           btc_to_gop_address.erase( Address( PtsAddress( key_entry.public_key, false, 0  ) ) );
                           btc_to_gop_address.erase( Address( PtsAddress( key_entry.public_key, true,  0  ) ) );
                           btc_to_gop_address.erase( Address( PtsAddress( key_entry.public_key, false, 56 ) ) );
                           btc_to_gop_address.erase( Address( PtsAddress( key_entry.public_key, true,  56 ) ) );

                           key_entry.public_key = public_key;
                           my->load_key_entry( key_entry );
                       }
                   }
                   store_key( key_entry );
               }
           }
           catch( ... )
           {
           }
       }

       // Repair transaction_data.entry_id
       count = 0;
       for( GenericWalletEntry& entry : entrys )
       {
           try
           {
               if( WalletEntryTypeEnum( entry.type ) == transaction_entry_type )
               {
                   std::cout << "\rRepairing transaction entry     " << std::to_string( ++count ) << std::flush;
                   WalletTransactionEntry transaction_entry = entry.as<WalletTransactionEntry>();
                   if( transaction_entry.trx.id() != SignedTransaction().id()  )
                   {
                       const TransactionIdType entry_id = transaction_entry.trx.id();
                       if( transaction_entry.entry_id != entry_id )
                       {
                           transactions.erase( transaction_entry.entry_id );
                           id_to_transaction_entry_index.erase( transaction_entry.entry_id );

                           transaction_entry.entry_id = entry_id;
                           my->load_transaction_entry( transaction_entry );
                       }
                   }
                   store_transaction( transaction_entry );
               }
           }
           catch( ... )
           {
           }
       }
       std::cout << "\rWallet entrys repaired.                                  " << std::flush << "\n";
   } FC_CAPTURE_AND_RETHROW() }
Exemple #27
0
   private_key_type wallet_db::generate_new_account_child_key( const fc::sha512& password, const string& account_name,
                                                               const account_key_type parent_key_type )
   { try {
       FC_ASSERT( is_open() );

       owallet_account_record account_record = lookup_account( account_name );
       FC_ASSERT( account_record.valid(), "Account not found!" );
       FC_ASSERT( !account_record->is_retracted(), "Account has been retracted!" );

       public_key_type parent_public_key;
       switch( parent_key_type )
       {
           case account_key_type::owner_key:
               parent_public_key = account_record->owner_key;
               break;
           case account_key_type::active_key:
               parent_public_key = account_record->active_key();
               break;
           case account_key_type::signing_key:
               FC_ASSERT( account_record->is_delegate() );
               parent_public_key = account_record->signing_key();
               break;
           // No default to force compiler warning
       }

       const owallet_key_record parent_key_record = lookup_key( address( parent_public_key ) );
       FC_ASSERT( parent_key_record.valid(), "Parent key not found!" );
       FC_ASSERT( parent_key_record->has_private_key(), "Parent private key not found!" );

       const private_key_type parent_private_key = parent_key_record->decrypt_private_key( password );
       uint32_t child_key_index = account_record->last_child_key_index;
       private_key_type account_child_private_key;
       public_key_type account_child_public_key;
       address account_child_address;
       while( true )
       {
           ++child_key_index;
           FC_ASSERT( child_key_index != 0, "Overflow!" );

           account_child_private_key = get_account_child_key( parent_private_key, child_key_index );
           account_child_public_key = account_child_private_key.get_public_key();
           account_child_address = address( account_child_public_key );

           owallet_key_record child_key_record = lookup_key( account_child_address );
           if( child_key_record.valid() && child_key_record->has_private_key() ) continue;

           break;
       }

       account_record->last_child_key_index = child_key_index;

       key_data child_key;
       child_key.account_address = account_record->owner_address();
       child_key.child_key_index = child_key_index;
       child_key.public_key = account_child_public_key;
       child_key.encrypt_private_key( password, account_child_private_key );

       store_account( *account_record );
       store_key( child_key );

       return account_child_private_key;
   } FC_CAPTURE_AND_RETHROW( (account_name) ) }
Exemple #28
0
   void wallet_db::repair_records( const fc::sha512& password )
   { try {
       FC_ASSERT( is_open() );

       vector<generic_wallet_record> records;
       for( auto iter = my->_records.begin(); iter.valid(); ++iter )
           records.push_back( iter.value() );

       // Repair account_data.is_my_account and key_data.account_address
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == account_record_type )
               {
                   wallet_account_record account_record = record.as<wallet_account_record>();
                   store_account( account_record );
               }
           }
           catch( ... )
           {
           }
       }

       // Repair key_data.public_key when I have the private key and
       // repair key_data.account_address and account_data.is_my_account
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == key_record_type )
               {
                   wallet_key_record key_record = record.as<wallet_key_record>();
                   if( key_record.has_private_key() )
                   {
                       const private_key_type private_key = key_record.decrypt_private_key( password );
                       const public_key_type public_key = private_key.get_public_key();
                       if( key_record.public_key != public_key )
                       {
                           const address key_address = key_record.get_address();
                           keys.erase( key_address );
                           btc_to_bts_address.erase( key_address );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 0  ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  0  ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 56 ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  56 ) ) );

                           key_record.public_key = public_key;
                           my->load_key_record( key_record );
                       }
                   }
                   store_key( key_record );
               }
           }
           catch( ... )
           {
           }
       }

       // Repair transaction_data.record_id
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == transaction_record_type )
               {
                   wallet_transaction_record transaction_record = record.as<wallet_transaction_record>();
                   if( transaction_record.trx.id() != signed_transaction().id()  )
                   {
                       const transaction_id_type record_id = transaction_record.trx.permanent_id();
                       if( transaction_record.record_id != record_id )
                       {
                           transactions.erase( transaction_record.record_id );
                           id_to_transaction_record_index.erase( transaction_record.record_id );

                           transaction_record.record_id = record_id;
                           my->load_transaction_record( transaction_record );
                       }
                   }
                   store_transaction( transaction_record );
               }
           }
           catch( ... )
           {
           }
       }
   } FC_CAPTURE_AND_RETHROW() }
Exemple #29
0
   PublicKeyType WalletDb::generate_new_account( const fc::sha512& password, const string& account_name, const variant& private_data )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");

       oWalletAccountEntry account_entry = lookup_account( account_name );
       FC_ASSERT( !account_entry.valid(), "Wallet already contains an account with that name!" );

       uint32_t key_index = get_last_wallet_child_key_index();
       PrivateKeyType owner_private_key, active_private_key;
       PublicKeyType owner_public_key, active_public_key;
       Address owner_address, active_address;
       while( true )
       {
           ++key_index;
           FC_ASSERT( key_index != 0, "Overflow!" );

           owner_private_key = get_wallet_child_key( password, key_index );
           owner_public_key = owner_private_key.get_public_key();
           owner_address = Address( owner_public_key );

           account_entry = lookup_account( owner_address );
           if( account_entry.valid() ) continue;

           oWalletKeyEntry key_entry = lookup_key( owner_address );
           if( key_entry.valid() && key_entry->has_private_key() ) continue;

           active_private_key = get_account_child_key( owner_private_key, 0 );
           active_public_key = active_private_key.get_public_key();
           active_address = Address( active_public_key );

           account_entry = lookup_account( active_address );
           if( account_entry.valid() ) continue;

           key_entry = lookup_key( active_address );
           if( key_entry.valid() && key_entry->has_private_key() ) continue;

           break;
       }

       KeyData active_key;
       active_key.account_address = owner_address;
       active_key.public_key = active_public_key;
       active_key.encrypt_private_key( password, active_private_key );

       KeyData owner_key;
       owner_key.account_address = owner_address;
       owner_key.public_key = owner_public_key;
       owner_key.encrypt_private_key( password, owner_private_key );
       owner_key.gen_seq_number = key_index;

       AccountData account;
       account.name = account_name;
       account.owner_key = owner_public_key;
       account.set_active_key( blockchain::now(), active_public_key );
       account.last_update = blockchain::now();
       account.is_my_account = true;
       account.private_data = private_data;

       store_key( active_key );
       set_last_wallet_child_key_index( key_index );
       store_key( owner_key );
       store_account( account );

       return owner_public_key;
   } FC_CAPTURE_AND_RETHROW( (account_name) ) }
Exemple #30
0
   void wallet_db::repair_records( const fc::sha512& password )
   { try {
       FC_ASSERT( is_open() );

       vector<generic_wallet_record> records;
       for( auto iter = my->_records.begin(); iter.valid(); ++iter )
           records.push_back( iter.value() );

       // Repair key_data.account_address when possible
       uint32_t count = 0;
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == account_record_type )
               {
                   std::cout << "\rRepairing account record " << std::to_string( ++count ) << std::flush;
                   wallet_account_record account_record = record.as<wallet_account_record>();
                   store_account( account_record );
               }
           }
           catch( const fc::exception& )
           {
           }
       }

       // Repair key_data.public_key when I have the private key and remove if I don't have the private key
       count = 0;
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == key_record_type )
               {
                   std::cout << "\rRepairing key record     " << std::to_string( ++count ) << std::flush;
                   wallet_key_record key_record = record.as<wallet_key_record>();
                   const address key_address = key_record.get_address();
                   if( key_record.has_private_key() )
                   {
                       const private_key_type private_key = key_record.decrypt_private_key( password );
                       const public_key_type public_key = private_key.get_public_key();
                       if( key_record.public_key != public_key )
                       {
                           keys.erase( key_address );
                           btc_to_bts_address.erase( key_address );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 0  ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  0  ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 56 ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  56 ) ) );

                           key_record.public_key = public_key;
                           my->load_key_record( key_record );
                       }
                   }
                   else
                   {
                       keys.erase( key_address );
                       btc_to_bts_address.erase( key_address );
                       btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 0  ) ) );
                       btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  0  ) ) );
                       btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 56 ) ) );
                       btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  56 ) ) );
                       remove_item( key_record.wallet_record_index );
                       continue;
                   }
                   store_key( key_record );
               }
           }
           catch( const fc::exception& )
           {
           }
       }

       // Repair transaction_data.record_id
       count = 0;
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == transaction_record_type )
               {
                   std::cout << "\rRepairing transaction record     " << std::to_string( ++count ) << std::flush;
                   wallet_transaction_record transaction_record = record.as<wallet_transaction_record>();
                   if( transaction_record.trx.id() != signed_transaction().id()  )
                   {
                       const transaction_id_type record_id = transaction_record.trx.id();
                       if( transaction_record.record_id != record_id )
                       {
                           transactions.erase( transaction_record.record_id );
                           id_to_transaction_record_index.erase( transaction_record.record_id );

                           transaction_record.record_id = record_id;
                           my->load_transaction_record( transaction_record );
                       }
                   }
                   store_transaction( transaction_record );
               }
           }
           catch( const fc::exception& )
           {
           }
       }
       std::cout << "\rWallet records repaired.                                  " << std::flush << "\n";
   } FC_CAPTURE_AND_RETHROW() }