Skip to content

Commit 46cef09

Browse files
benevanoffnahuhh
authored andcommitted
wallet: create set_subaddress_lookahead wallet rpc endpoint
1 parent 351a239 commit 46cef09

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

src/wallet/wallet_rpc_server.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,18 @@ namespace tools
704704
res.index = *index;
705705
return true;
706706
}
707+
bool wallet_rpc_server::on_set_subaddr_lookahead(const wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD::request& req, wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD::response& res, epee::json_rpc::error& er, const connection_context *ctx)
708+
{
709+
if (!m_wallet) return not_open(er);
710+
try {
711+
m_wallet->set_subaddress_lookahead(req.major_idx, req.minor_idx);
712+
}
713+
catch (const std::exception& e) {
714+
handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR);
715+
return false;
716+
}
717+
return true;
718+
}
707719
//------------------------------------------------------------------------------------------------------------------------------
708720
bool wallet_rpc_server::on_create_address(const wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::response& res, epee::json_rpc::error& er, const connection_context *ctx)
709721
{

src/wallet/wallet_rpc_server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ namespace tools
7171
MAP_JON_RPC_WE("get_balance", on_getbalance, wallet_rpc::COMMAND_RPC_GET_BALANCE)
7272
MAP_JON_RPC_WE("get_address", on_getaddress, wallet_rpc::COMMAND_RPC_GET_ADDRESS)
7373
MAP_JON_RPC_WE("get_address_index", on_getaddress_index, wallet_rpc::COMMAND_RPC_GET_ADDRESS_INDEX)
74+
MAP_JON_RPC_WE("set_subaddress_lookahead", on_set_subaddr_lookahead, wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD)
7475
MAP_JON_RPC_WE("getbalance", on_getbalance, wallet_rpc::COMMAND_RPC_GET_BALANCE)
7576
MAP_JON_RPC_WE("getaddress", on_getaddress, wallet_rpc::COMMAND_RPC_GET_ADDRESS)
7677
MAP_JON_RPC_WE("create_address", on_create_address, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS)
@@ -172,6 +173,7 @@ namespace tools
172173
bool on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
173174
bool on_getaddress(const wallet_rpc::COMMAND_RPC_GET_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
174175
bool on_getaddress_index(const wallet_rpc::COMMAND_RPC_GET_ADDRESS_INDEX::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS_INDEX::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
176+
bool on_set_subaddr_lookahead(const wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD::request& req, wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
175177
bool on_create_address(const wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
176178
bool on_label_address(const wallet_rpc::COMMAND_RPC_LABEL_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_LABEL_ADDRESS::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
177179
bool on_get_accounts(const wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::request& req, wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);

src/wallet/wallet_rpc_server_commands_defs.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,27 @@ namespace wallet_rpc
182182
typedef epee::misc_utils::struct_init<response_t> response;
183183
};
184184

185+
struct COMMAND_RPC_SET_SUBADDR_LOOKAHEAD
186+
{
187+
struct request_t
188+
{
189+
uint64_t major_idx;
190+
uint64_t minor_idx;
191+
BEGIN_KV_SERIALIZE_MAP()
192+
KV_SERIALIZE(major_idx)
193+
KV_SERIALIZE(minor_idx)
194+
END_KV_SERIALIZE_MAP()
195+
};
196+
typedef epee::misc_utils::struct_init<request_t> request;
197+
198+
struct response_t
199+
{
200+
BEGIN_KV_SERIALIZE_MAP()
201+
END_KV_SERIALIZE_MAP()
202+
};
203+
typedef epee::misc_utils::struct_init<response_t> response;
204+
};
205+
185206
struct COMMAND_RPC_CREATE_ADDRESS
186207
{
187208
struct request_t

tests/functional_tests/wallet.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def run_test(self):
4646
self.check_keys()
4747
self.create_subaddresses()
4848
self.tags()
49+
self.update_lookahead()
4950
self.attributes()
5051
self.open_close()
5152
self.languages()
@@ -163,6 +164,20 @@ def create_subaddresses(self):
163164

164165
res = wallet.label_account(0, "main")
165166

167+
def update_lookahead(self):
168+
print('Updating subaddress lookahead')
169+
wallet = Wallet()
170+
address_0_999 = '8BQKgTSSqJjP14AKnZUBwnXWj46MuNmLvHfPTpmry52DbfNjjHVvHUk4mczU8nj8yZ57zBhksTJ8kM5xKeJXw55kCMVqyG7' # this is the address for address 999 of the main account in the test wallet
171+
try: # assert address_1_999 is not in the current pubkey table
172+
wallet.get_address_index(address_0_999)
173+
except Exception as e:
174+
assert str(e) == "{'error': {'code': -2, 'message': \"Address doesn't belong to the wallet\"}, 'id': '0', 'jsonrpc': '2.0'}"
175+
# update the lookahead and assert the high index address is now in the table
176+
wallet.set_subaddress_lookahead(50, 1000)
177+
r = wallet.get_address_index(address_0_999)
178+
assert r['index']['major'] == 0
179+
assert r['index']['minor'] == 999
180+
166181
def tags(self):
167182
print('Testing tags')
168183
wallet = Wallet()

utils/python-rpc/framework/wallet.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,18 @@ def generate_from_keys(self, restore_height = 0, filename = "", password = "", a
334334
}
335335
return self.rpc.send_json_rpc_request(generate_from_keys)
336336

337+
def set_subaddress_lookahead(self, major_idx: int, minor_idx: int):
338+
lookahead = {
339+
'method': 'set_subaddress_lookahead',
340+
'jsonrpc': '2.0',
341+
'params' : {
342+
'major_idx': major_idx,
343+
'minor_idx': minor_idx
344+
},
345+
'id': '0'
346+
}
347+
return self.rpc.send_json_rpc_request(lookahead)
348+
337349
def open_wallet(self, filename, password='', autosave_current = True):
338350
open_wallet = {
339351
'method': 'open_wallet',
@@ -1164,6 +1176,7 @@ def frozen(self, key_image):
11641176
}
11651177
return self.rpc.send_json_rpc_request(frozen)
11661178

1179+
<<<<<<< HEAD
11671180
class BackgroundSyncOptions(object):
11681181
def __init__(self):
11691182
self.off = 'off'

0 commit comments

Comments
 (0)