Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push notifications #100

Closed
wants to merge 75 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
b54653b
update localsecret and secret-contract-optimizer versions
darwinzer0 May 13, 2024
5b80e60
remove decoy code
darwinzer0 May 14, 2024
822a306
update authors
darwinzer0 May 14, 2024
ad97a47
update cosmwasm-std, cosmwasm-storage, and secret toolkit versions
darwinzer0 May 14, 2024
32bd10c
fix tx history store as Addr bug
darwinzer0 May 15, 2024
22b2ff5
remove legacy transfer
darwinzer0 May 16, 2024
fba3895
dev: start dwb and remove denom from history storage
darwinzer0 May 18, 2024
c0a034e
adding rng
darwinzer0 May 18, 2024
3108d7a
balances key -> canonical addr
darwinzer0 May 18, 2024
a24d205
dev: settling owner in dwb
darwinzer0 May 19, 2024
3b538af
dwb array development
darwinzer0 May 22, 2024
e84160c
remove condition in constant_time_release
darwinzer0 May 23, 2024
d396dcd
fix constant time operations for saturated dwb
darwinzer0 May 25, 2024
7fd21bb
cleanup
darwinzer0 May 25, 2024
18a0540
fix random range
darwinzer0 May 25, 2024
448a626
fix random range for modulo bias
darwinzer0 May 25, 2024
9b56d0a
make list len u16 and handle full list
darwinzer0 May 25, 2024
338fc1a
dev: unsaturated buffer handling
darwinzer0 May 25, 2024
100dd72
query balance include dwb
darwinzer0 May 25, 2024
293f8af
start on txs query
darwinzer0 May 25, 2024
3e273f9
dev: handle undersaturated dwb
blake-regalia May 26, 2024
be5d6e9
query_transactions dev
darwinzer0 May 26, 2024
e556e98
Merge branch 'dwbs' into undersaturated
darwinzer0 May 26, 2024
f091807
Merge pull request #1 from SolarRepublic/undersaturated
darwinzer0 May 26, 2024
ecee6c5
query transactions w/ binary search
darwinzer0 Jun 4, 2024
069259e
cleanup
darwinzer0 Jun 4, 2024
32a42fd
tx history, plus transfer testing
darwinzer0 Jun 8, 2024
925f06c
add query balance to test
darwinzer0 Jun 8, 2024
554de79
move add recipient to dwb impl and perform mint
darwinzer0 Jun 11, 2024
d907276
dwb for additional tx types
darwinzer0 Jun 14, 2024
090463f
update transfer test for new mint settling
darwinzer0 Jun 14, 2024
fbdafa3
deposit and cleanup
darwinzer0 Jun 15, 2024
8cae7b3
fix wasm compilation, tests
darwinzer0 Jun 15, 2024
aa75d22
add TESTING dwb query
darwinzer0 Jun 15, 2024
b5093f1
update secretdev to localsecret
darwinzer0 Jun 15, 2024
8212dbf
add gas logging for testing
darwinzer0 Jun 16, 2024
e06150b
fix: R5a. and reuse released addr
blake-regalia Jun 27, 2024
c2d6ef9
fix: use existing entry address
blake-regalia Jun 27, 2024
5fddb99
Merge pull request #6 from SolarRepublic/dwbs-anti-random
darwinzer0 Jun 27, 2024
6a3a032
dev: bitwise-trie of stored balances
blake-regalia Jun 30, 2024
3d7de8b
dev: stored balances todos
darwinzer0 Jul 9, 2024
8d2d83e
update query_transactions
darwinzer0 Jul 10, 2024
5bccca2
refactor stored_balance to use stored_entry
darwinzer0 Jul 10, 2024
86cd753
dev: add child loop when inserting dwb entry
darwinzer0 Jul 10, 2024
c199827
comments
darwinzer0 Jul 10, 2024
06e82d7
dev: inc bit_pos
darwinzer0 Jul 10, 2024
bcd8938
dev: init btsb
darwinzer0 Jul 10, 2024
6ecd16b
dev: all unit tests pass with stored balances
darwinzer0 Jul 13, 2024
239bcc3
Merge pull request #8 from SolarRepublic/btsb-darwinzer0
blake-regalia Jul 14, 2024
a630732
dev: btsb -> btbe
blake-regalia Jul 14, 2024
e9986fc
dev: integration tests
blake-regalia Jul 31, 2024
f7e1d0c
fix: dwb_entry recipient and hkdf size
blake-regalia Aug 2, 2024
8e2fc97
style: x cfg macro
blake-regalia Aug 2, 2024
df34e04
all tracker code using cfg(gas_tracking)
darwinzer0 Aug 2, 2024
ceb74bf
Merge pull request #9 from SolarRepublic/btsb
darwinzer0 Aug 2, 2024
9a9c98c
chore: upgrade neutrino
blake-regalia Aug 2, 2024
b72211a
Merge pull request #10 from SolarRepublic/final-integration
blake-regalia Aug 2, 2024
3bbaa01
style: cleanup
blake-regalia Aug 2, 2024
d9abb00
fix: gas tracking response
blake-regalia Aug 2, 2024
bae3fd8
dev: snip50
darwinzer0 Aug 2, 2024
5d8d4b6
unit tests btbe
darwinzer0 Aug 3, 2024
2291658
cargo fmt
darwinzer0 Aug 3, 2024
4a91f5c
dev: transfer from
blake-regalia Aug 3, 2024
79e898e
Merge remote-tracking branch 'origin/dwb-s50' into final-integration
blake-regalia Aug 3, 2024
3ad29b9
dev: gas evaporation
blake-regalia Aug 3, 2024
45399d0
fix: tweak gas evaporation params
blake-regalia Aug 3, 2024
1f20825
fix: tweak gas evaporation params
blake-regalia Aug 3, 2024
44af4fc
style: code cleanup
blake-regalia Aug 3, 2024
cf0ef65
fix: inspectability
blake-regalia Aug 3, 2024
cb85125
fix: make evaporation optional
blake-regalia Aug 5, 2024
54fe07c
Merge pull request #12 from SolarRepublic/final-integration
blake-regalia Aug 5, 2024
4bdea0c
remove prng store, update create viewing key to make entropy optional
darwinzer0 Aug 8, 2024
4cd963c
Merge pull request #13 from SolarRepublic/no-prng-2
darwinzer0 Aug 8, 2024
9e62c8e
dev: snip52+53 implementation for mint, transfer, batch transfer, sen…
darwinzer0 Oct 15, 2024
055805c
dev: add remainder of notifications
darwinzer0 Oct 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
dev: handle undersaturated dwb
blake-regalia committed May 26, 2024
commit 3e273f9b2dc66ca1c8c100ca52dd43c54ec548aa
89 changes: 50 additions & 39 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ use secret_toolkit::viewing_key::{ViewingKey, ViewingKeyStore};
use secret_toolkit_crypto::{sha_256, ContractPrng};

use crate::batch;
use crate::dwb::{random_in_range, DelayedWriteBuffer, DWB, DWB_LEN, TX_NODES};
use crate::dwb::{random_in_range, DelayedWriteBuffer, DWB, DWB_LEN, DWB_MAX_TX_EVENTS, TX_NODES};
use crate::msg::{
AllowanceGivenResult, AllowanceReceivedResult, ContractStatusLevel, ExecuteAnswer,
ExecuteMsg, InstantiateMsg, QueryAnswer, QueryMsg, QueryWithPermit, ResponseStatus::Success,
@@ -1904,56 +1904,57 @@ fn perform_transfer(
new_entry.add_tx_node(store, tx_id)?;
new_entry.add_amount(amount)?;

// if recipient is in the buffer (non-zero index), set this value to 1, otherwise 0, in constant-time
// casting to isize will never overflow, so long as dwb length is limited to a u16 value
let recipient_in_buffer = (((recipient_index as isize | -(recipient_index as isize)) >> 31) & 1) as usize;

if dwb.saturated() {
// randomly pick an entry to exclude in case the recipient is not in the buffer
let random_exclude_index = random_in_range(rng, 1, DWB_LEN as u32)? as usize;

// index of entry to exclude from selection
let exclude_index = (recipient_index as usize * recipient_in_buffer) + (random_exclude_index * (1 - recipient_in_buffer));
// whether or not recipient is in the buffer (non-zero index)
// casting to i32 will never overflow, so long as dwb length is limited to a u16 value
let if_recipient_in_buffer = constant_time_is_not_zero(recipient_index as i32);

// randomly select any other entry to settle in constant-time (avoiding the reserved 0th position)
let random_settle_index = (((random_in_range(rng, 0, DWB_LEN as u32 - 2)? + exclude_index as u32) % (DWB_LEN as u32 - 1)) + 1) as usize;
// randomly pick an entry to exclude in case the recipient is not in the buffer
let random_exclude_index = random_in_range(rng, 1, DWB_LEN as u32)? as usize;

// check if we have any open slots in the linked list
let open_slots = (u16::MAX - dwb.entries[recipient_index].list_len()?) as i32;
let list_can_grow = (((open_slots | -open_slots) >> 31) & 1) as usize;
// index of entry to exclude from selection
let exclude_index = constant_time_if_else(if_recipient_in_buffer, recipient_index, random_exclude_index);

// if we would overflow the list, just settle recipient
// TODO: see docs for attack analysis
let actual_settle_index = (random_settle_index as usize * list_can_grow) + (recipient_index * (1 - list_can_grow));
// randomly select any other entry to settle in constant-time (avoiding the reserved 0th position)
let random_settle_index = (((random_in_range(rng, 0, DWB_LEN as u32 - 2)? + exclude_index as u32) % (DWB_LEN as u32 - 1)) + 1) as usize;

// settle the entry
dwb.settle_entry(store, actual_settle_index)?;

// replace it with a randomly generated address (that is not currently in the buffer) and 0 amount and nil events pointer
let replacement_entry = dwb.unique_random_entry(rng)?;
dwb.entries[actual_settle_index] = replacement_entry;
// whether or not the buffer is fully saturated yet
let if_undersaturated = constant_time_is_not_zero(dwb.empty_space_counter as i32);

// pick the index to where the recipient's entry should be written
let write_index = (recipient_index * recipient_in_buffer) + (actual_settle_index * (1 - recipient_in_buffer));
// find the next empty entry in the buffer
let next_empty_index = (DWB_LEN - dwb.empty_space_counter) as usize;

// either updates the existing recipient entry, or overwrites the random replacement entry in the settled index
dwb.entries[write_index] = new_entry;
} else {
// TODO: revisit contract warm up with other options, e.g saturating with random address from beginning
// if buffer is not yet saturated, settle the address at the next empty index
let bounded_settle_index = constant_time_if_else(if_undersaturated, next_empty_index, random_settle_index);

// find the next empty entry in the buffer
let next_index = (DWB_LEN - dwb.empty_space_counter) as usize;

// pick the index to where the recipient's entry should be written
let write_index = (recipient_index * recipient_in_buffer) + (next_index * (1 - recipient_in_buffer));
// check if we have any open slots in the linked list
let if_list_can_grow = constant_time_is_not_zero((DWB_MAX_TX_EVENTS - dwb.entries[recipient_index].list_len()?) as i32);

// either updates the existing recipient entry, or write the entry to the next index value
dwb.entries[write_index] = new_entry;
// if we would overflow the list, just settle recipient
// TODO: see docs for attack analysis
let actual_settle_index = constant_time_if_else(if_list_can_grow, bounded_settle_index, recipient_index);

// decrement empty space counter if receipient is not already in buffer
let empty_space_counter_delta = (1 - recipient_in_buffer) as u16;
dwb.empty_space_counter -= empty_space_counter_delta;
}
// settle the entry
dwb.settle_entry(store, actual_settle_index)?;

// replace it with a randomly generated address (that is not currently in the buffer) and 0 amount and nil events pointer
let replacement_entry = dwb.unique_random_entry(rng)?;
dwb.entries[actual_settle_index] = replacement_entry;

// pick the index to where the recipient's entry should be written
let write_index = constant_time_if_else(if_recipient_in_buffer, recipient_index, actual_settle_index);

// either updates the existing recipient entry, or overwrites the random replacement entry in the settled index
dwb.entries[write_index] = new_entry;

// decrement empty space counter if it is undersaturated and the recipient was not already in the buffer
dwb.empty_space_counter -= constant_time_if_else(
if_undersaturated,
constant_time_if_else(if_recipient_in_buffer, 0usize, 1usize),
0usize
) as u16;

DWB.save(store, &dwb)?;

@@ -1993,6 +1994,16 @@ fn is_valid_symbol(symbol: &str) -> bool {
len_is_valid && symbol.bytes().all(|byte| byte.is_ascii_alphabetic())
}

#[inline]
fn constant_time_is_not_zero(value: i32) -> u32 {
return (((value | -value) >> 31) & 1) as u32;
}

#[inline]
fn constant_time_if_else(condition: u32, then: usize, els: usize) -> usize {
return (then * condition as usize) | (els * (1 - condition as usize));
}

// pub fn migrate(
// _deps: DepsMut,
// _env: Env,
3 changes: 3 additions & 0 deletions src/dwb.rs
Original file line number Diff line number Diff line change
@@ -38,6 +38,9 @@ pub const IMPOSSIBLE_ADDR: [u8; 20] = [
// minimum allowable size: 3
pub const DWB_LEN: u16 = 65;

// maximum number of tx events allowed in an entry's linked list
pub const DWB_MAX_TX_EVENTS: u16 = u16::MAX;

#[derive(Serialize, Deserialize, Debug)]
pub struct DelayedWriteBuffer {
pub empty_space_counter: u16,