Skip to content

Commit 29c1ba0

Browse files
committed
GH-552: Eliminate extra block increment
1 parent 051c0cd commit 29c1ba0

File tree

2 files changed

+386
-16
lines changed

2 files changed

+386
-16
lines changed

node/src/blockchain/blockchain_bridge.rs

+203-2
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ mod tests {
12901290
let amount = 42;
12911291
let amount2 = 55;
12921292
let expected_transactions = RetrievedBlockchainTransactions {
1293-
new_start_block: BlockNumber::Number(8675309u64.into()),
1293+
new_start_block: BlockNumber::Number(100_006u64.into()),
12941294
transactions: vec![
12951295
BlockchainTransaction {
12961296
block_number: 7,
@@ -1356,7 +1356,7 @@ mod tests {
13561356
&ReceivedPayments {
13571357
timestamp: received_payments.timestamp,
13581358
payments: expected_transactions.transactions,
1359-
new_start_block: 8675309u64,
1359+
new_start_block: 100_006u64,
13601360
response_skeleton_opt: Some(ResponseSkeleton {
13611361
client_id: 1234,
13621362
context_id: 4321
@@ -1703,6 +1703,207 @@ mod tests {
17031703
let _ = subject.handle_retrieve_transactions(retrieve_transactions);
17041704
}
17051705

1706+
#[test]
1707+
fn handle_retrieve_transactions_repeated_calling_does_not_skip_blocks() {
1708+
let retrieve_transactions_params_arc = Arc::new(Mutex::new(vec![]));
1709+
let system =
1710+
System::new("handle_retrieve_transactions_repeated_calling_does_not_skip_blocks");
1711+
let (accountant, _, accountant_recording_arc) = make_recorder();
1712+
let earning_wallet = make_wallet("earningwallet");
1713+
let amount1 = 42;
1714+
let amount2 = 55;
1715+
let amount3 = 75;
1716+
let amount4 = 99;
1717+
let initial_start_block: u64 = 0x45f2f3; // 4_584_179
1718+
let new_start_block_1 = initial_start_block + 1 + DEFAULT_MAX_BLOCK_COUNT;
1719+
let expected_transactions1 = RetrievedBlockchainTransactions {
1720+
new_start_block: BlockNumber::Number(new_start_block_1.into()),
1721+
transactions: vec![BlockchainTransaction {
1722+
block_number: initial_start_block + 0x14382,
1723+
from: earning_wallet.clone(),
1724+
wei_amount: amount1,
1725+
}],
1726+
};
1727+
let new_start_block_2 = initial_start_block + 2 + (2 * DEFAULT_MAX_BLOCK_COUNT);
1728+
let expected_transactions2 = RetrievedBlockchainTransactions {
1729+
new_start_block: BlockNumber::Number(new_start_block_2.into()),
1730+
transactions: vec![BlockchainTransaction {
1731+
block_number: initial_start_block + 0x308b3,
1732+
from: earning_wallet.clone(),
1733+
wei_amount: amount2,
1734+
}],
1735+
};
1736+
let new_start_block_3 = initial_start_block + 3 + (3 * DEFAULT_MAX_BLOCK_COUNT);
1737+
let expected_transactions3 = RetrievedBlockchainTransactions {
1738+
new_start_block: BlockNumber::Number(new_start_block_3.into()),
1739+
transactions: vec![BlockchainTransaction {
1740+
block_number: initial_start_block + 0x34248,
1741+
from: earning_wallet.clone(),
1742+
wei_amount: amount3,
1743+
}],
1744+
};
1745+
let new_start_block_4 = 0x4be667;
1746+
let expected_transactions4 = RetrievedBlockchainTransactions {
1747+
new_start_block: BlockNumber::Number(new_start_block_4.into()),
1748+
transactions: vec![BlockchainTransaction {
1749+
block_number: initial_start_block + 0x49b7e,
1750+
from: earning_wallet.clone(),
1751+
wei_amount: amount4,
1752+
}],
1753+
};
1754+
let lower_interface = LowBlockchainIntMock::default()
1755+
.get_block_number_result(Ok(0x4be663.into()))
1756+
.get_block_number_result(Ok(0x4be664.into()))
1757+
.get_block_number_result(Ok(0x4be665.into()))
1758+
.get_block_number_result(Ok(new_start_block_4.into()));
1759+
let blockchain_interface = BlockchainInterfaceMock::default()
1760+
.lower_interface_results(Box::new(lower_interface))
1761+
.retrieve_transactions_params(&retrieve_transactions_params_arc)
1762+
.retrieve_transactions_result(Ok(expected_transactions1.clone()))
1763+
.retrieve_transactions_result(Ok(expected_transactions2.clone()))
1764+
.retrieve_transactions_result(Ok(expected_transactions3.clone()))
1765+
.retrieve_transactions_result(Ok(expected_transactions4.clone()));
1766+
let persistent_config = PersistentConfigurationMock::new()
1767+
.max_block_count_result(Ok(Some(DEFAULT_MAX_BLOCK_COUNT)))
1768+
.start_block_result(Ok(Some(initial_start_block)))
1769+
.start_block_result(Ok(Some(new_start_block_1)))
1770+
.start_block_result(Ok(Some(new_start_block_2)))
1771+
.start_block_result(Ok(Some(new_start_block_3)))
1772+
.start_block_result(Ok(Some(new_start_block_4)));
1773+
let subject = BlockchainBridge::new(
1774+
Box::new(blockchain_interface),
1775+
Box::new(persistent_config),
1776+
false,
1777+
);
1778+
let addr = subject.start();
1779+
let subject_subs = BlockchainBridge::make_subs_from(&addr);
1780+
let peer_actors = peer_actors_builder().accountant(accountant).build();
1781+
send_bind_message!(subject_subs, peer_actors);
1782+
let retrieve_transactions1 = RetrieveTransactions {
1783+
recipient: earning_wallet.clone(),
1784+
response_skeleton_opt: Some(ResponseSkeleton {
1785+
client_id: 1234,
1786+
context_id: 1,
1787+
}),
1788+
};
1789+
let retrieve_transactions2 = RetrieveTransactions {
1790+
recipient: earning_wallet.clone(),
1791+
response_skeleton_opt: Some(ResponseSkeleton {
1792+
client_id: 1234,
1793+
context_id: 2,
1794+
}),
1795+
};
1796+
let retrieve_transactions3 = RetrieveTransactions {
1797+
recipient: earning_wallet.clone(),
1798+
response_skeleton_opt: Some(ResponseSkeleton {
1799+
client_id: 1234,
1800+
context_id: 3,
1801+
}),
1802+
};
1803+
let retrieve_transactions4 = RetrieveTransactions {
1804+
recipient: earning_wallet.clone(),
1805+
response_skeleton_opt: Some(ResponseSkeleton {
1806+
client_id: 1234,
1807+
context_id: 4,
1808+
}),
1809+
};
1810+
let before = SystemTime::now();
1811+
1812+
let _ = addr.try_send(retrieve_transactions1).unwrap();
1813+
let _ = addr.try_send(retrieve_transactions2).unwrap();
1814+
let _ = addr.try_send(retrieve_transactions3).unwrap();
1815+
let _ = addr.try_send(retrieve_transactions4).unwrap();
1816+
1817+
System::current().stop();
1818+
system.run();
1819+
let after = SystemTime::now();
1820+
1821+
let retrieve_transactions_params = retrieve_transactions_params_arc.lock().unwrap();
1822+
assert_eq!(
1823+
*retrieve_transactions_params,
1824+
vec![
1825+
(
1826+
BlockNumber::Number(initial_start_block.into()),
1827+
BlockNumber::Number((initial_start_block + DEFAULT_MAX_BLOCK_COUNT).into()),
1828+
earning_wallet.clone()
1829+
),
1830+
(
1831+
BlockNumber::Number(new_start_block_1.into()),
1832+
BlockNumber::Number((new_start_block_1 + DEFAULT_MAX_BLOCK_COUNT).into()),
1833+
earning_wallet.clone()
1834+
),
1835+
(
1836+
BlockNumber::Number(new_start_block_2.into()),
1837+
BlockNumber::Number((new_start_block_2 + DEFAULT_MAX_BLOCK_COUNT).into()),
1838+
earning_wallet.clone()
1839+
),
1840+
(
1841+
BlockNumber::Number(new_start_block_3.into()),
1842+
BlockNumber::Number(new_start_block_4.into()),
1843+
earning_wallet.clone()
1844+
),
1845+
]
1846+
);
1847+
let accountant_received_payment = accountant_recording_arc.lock().unwrap();
1848+
assert_eq!(accountant_received_payment.len(), 4);
1849+
let received_payments1 = accountant_received_payment.get_record::<ReceivedPayments>(0);
1850+
check_timestamp(before, received_payments1.timestamp, after);
1851+
assert_eq!(
1852+
received_payments1,
1853+
&ReceivedPayments {
1854+
timestamp: received_payments1.timestamp,
1855+
payments: expected_transactions1.transactions,
1856+
new_start_block: new_start_block_1,
1857+
response_skeleton_opt: Some(ResponseSkeleton {
1858+
client_id: 1234,
1859+
context_id: 1
1860+
}),
1861+
}
1862+
);
1863+
let received_payments2 = accountant_received_payment.get_record::<ReceivedPayments>(1);
1864+
check_timestamp(before, received_payments2.timestamp, after);
1865+
assert_eq!(
1866+
received_payments2,
1867+
&ReceivedPayments {
1868+
timestamp: received_payments2.timestamp,
1869+
payments: expected_transactions2.transactions,
1870+
new_start_block: new_start_block_2,
1871+
response_skeleton_opt: Some(ResponseSkeleton {
1872+
client_id: 1234,
1873+
context_id: 2
1874+
}),
1875+
}
1876+
);
1877+
let received_payments3 = accountant_received_payment.get_record::<ReceivedPayments>(2);
1878+
check_timestamp(before, received_payments3.timestamp, after);
1879+
assert_eq!(
1880+
received_payments3,
1881+
&ReceivedPayments {
1882+
timestamp: received_payments3.timestamp,
1883+
payments: expected_transactions3.transactions,
1884+
new_start_block: new_start_block_3,
1885+
response_skeleton_opt: Some(ResponseSkeleton {
1886+
client_id: 1234,
1887+
context_id: 3
1888+
}),
1889+
}
1890+
);
1891+
let received_payments4 = accountant_received_payment.get_record::<ReceivedPayments>(3);
1892+
check_timestamp(before, received_payments4.timestamp, after);
1893+
assert_eq!(
1894+
received_payments4,
1895+
&ReceivedPayments {
1896+
timestamp: received_payments4.timestamp,
1897+
payments: expected_transactions4.transactions,
1898+
new_start_block: new_start_block_4,
1899+
response_skeleton_opt: Some(ResponseSkeleton {
1900+
client_id: 1234,
1901+
context_id: 4
1902+
}),
1903+
}
1904+
);
1905+
}
1906+
17061907
fn success_handler(
17071908
_bcb: &mut BlockchainBridge,
17081909
_msg: RetrieveTransactions,

0 commit comments

Comments
 (0)