Skip to content

Commit

Permalink
Added get_item function to get specific transaction from queue (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
salman01zp authored Aug 11, 2023
1 parent 6b70213 commit d59cab0
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 10 deletions.
198 changes: 198 additions & 0 deletions config/development/local-evm/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
{
"features": {
"governance-relay": true,
"data-query": true,
"private-tx-relay": true
},
"assets": {
"tTNT": {
"name": "Test Tangle Token",
"decimals": 18,
"price": 10
},
"TNT": {
"name": "Tangle Token",
"decimals": 18,
"price": 10
}
},
"evm": {
"athena": {
"name": "athena",
"http-endpoint": "http://localhost:5002",
"ws-endpoint": "ws://localhost:5002",
"explorer": "https://athena-explorer.webb.tools",
"chain-id": 5002,
"block-confirmations": 0,
"private-key": "$ATHENA_PRIVATE_KEY",
"enabled": true,
"contracts": [
{
"contract": "VAnchor",
"address": "0x91eB86019FD8D7c5a9E31143D422850A13F670A3",
"deployed-at": 1,
"events-watcher": {
"enabled": true,
"polling-interval": 1000,
"print-progress-interval": 60000
},
"withdraw-config": {
"withdraw-fee-percentage": 0,
"withdraw-gaslimit": "0x350000"
},
"linked-anchors": [
{
"type": "Evm",
"chain": "hermes",
"chain-id": 5001,
"address": "0x91eB86019FD8D7c5a9E31143D422850A13F670A3"
},
{
"type": "Evm",
"chain": "demeter",
"chain-id": 5003,
"address": "0x91eB86019FD8D7c5a9E31143D422850A13F670A3"
}
],
"smart-anchor-updates": {
"enabled": false,
"max-time-delay": 10,
"min-time-delay": 5
},
"proposal-signing-backend": {
"type": "Mocked",
"private-key": "$GOVERNOR_PRIVATE_KEY"
}
},
{
"contract": "SignatureBridge",
"address": "0x2946259E0334f33A064106302415aD3391BeD384",
"deployed-at": 1,
"events-watcher": {
"enabled": true,
"polling-interval": 1000,
"print-progress-interval": 60000
}
}
]
},
"hermes": {
"name": "hermes",
"http-endpoint": "http://localhost:5001",
"ws-endpoint": "ws://localhost:5001",
"explorer": "https://hermes-explorer.webb.tools",
"chain-id": 5001,
"block-confirmations": 0,
"private-key": "$HERMES_PRIVATE_KEY",
"enabled": true,
"contracts": [
{
"contract": "VAnchor",
"address": "0x91eB86019FD8D7c5a9E31143D422850A13F670A3",
"deployed-at": 1,
"events-watcher": {
"enabled": true,
"polling-interval": 1000,
"print-progress-interval": 60000
},
"withdraw-config": {
"withdraw-fee-percentage": 0,
"withdraw-gaslimit": "0x350000"
},
"linked-anchors": [
{
"type": "Evm",
"chain": "athena",
"chain-id": 5002,
"address": "0x91eB86019FD8D7c5a9E31143D422850A13F670A3"
},
{
"type": "Evm",
"chain": "demeter",
"chain-id": 5003,
"address": "0x91eB86019FD8D7c5a9E31143D422850A13F670A3"
}
],
"smart-anchor-updates": {
"enabled": false,
"max-time-delay": 10,
"min-time-delay": 5
},
"proposal-signing-backend": {
"type": "Mocked",
"private-key": "$GOVERNOR_PRIVATE_KEY"
}
},
{
"contract": "SignatureBridge",
"address": "0x2946259E0334f33A064106302415aD3391BeD384",
"deployed-at": 1,
"events-watcher": {
"enabled": true,
"polling-interval": 1000,
"print-progress-interval": 60000
}
}
]
},
"demeter": {
"name": "demeter",
"http-endpoint": "http://localhost:5003",
"ws-endpoint": "ws://localhost:5003",
"explorer": "https://demeter-explorer.webb.tools",
"chain-id": 5003,
"block-confirmations": 0,
"private-key": "$DEMETER_PRIVATE_KEY",
"enabled": true,
"contracts": [
{
"contract": "VAnchor",
"address": "0x91eB86019FD8D7c5a9E31143D422850A13F670A3",
"deployed-at": 1,
"events-watcher": {
"enabled": true,
"polling-interval": 1000,
"print-progress-interval": 60000
},
"withdraw-config": {
"withdraw-fee-percentage": 0,
"withdraw-gaslimit": "0x350000"
},
"linked-anchors": [
{
"type": "Evm",
"chain": "athena",
"chain-id": 5002,
"address": "0x91eB86019FD8D7c5a9E31143D422850A13F670A3"
},
{
"type": "Evm",
"chain": "hermes",
"chain-id": 5001,
"address": "0x91eB86019FD8D7c5a9E31143D422850A13F670A3"
}
],
"smart-anchor-updates": {
"enabled": false,
"max-time-delay": 10,
"min-time-delay": 5
},
"proposal-signing-backend": {
"type": "Mocked",
"private-key": "$GOVERNOR_PRIVATE_KEY"
}
},
{
"contract": "SignatureBridge",
"address": "0x2946259E0334f33A064106302415aD3391BeD384",
"deployed-at": 1,
"events-watcher": {
"enabled": true,
"polling-interval": 1000,
"print-progress-interval": 60000
}
}
]
}
}
}
39 changes: 39 additions & 0 deletions crates/chains-info/fixtures/chains.json
Original file line number Diff line number Diff line change
Expand Up @@ -14084,6 +14084,45 @@
}
]
},
{
"name": "Athena Local",
"chain": "athena",
"rpc": ["http://localhost:5002"],
"faucets": [],
"nativeCurrency": { "name": "Ether", "symbol": "ETH", "decimals": 18 },
"infoURL": "https://webb.tools",
"shortName": "athena",
"chainId": 5002,
"networkId": 5002,
"status": "testnet",
"explorers": []
},
{
"name": "Hermes Local",
"chain": "hermes",
"rpc": ["http://localhost:5001"],
"faucets": [],
"nativeCurrency": { "name": "Ether", "symbol": "ETH", "decimals": 18 },
"infoURL": "https://webb.tools",
"shortName": "hermes",
"chainId": 5001,
"networkId": 5001,
"status": "testnet",
"explorers": []
},
{
"name": "Demeter Local",
"chain": "demeter",
"rpc": ["http://localhost:5003"],
"faucets": [],
"nativeCurrency": { "name": "Ether", "symbol": "ETH", "decimals": 18 },
"infoURL": "https://webb.tools",
"shortName": "demeter",
"chainId": 5003,
"networkId": 5003,
"status": "testnet",
"explorers": []
},
{
"name": "Tangle testnet",
"chain": "tangle",
Expand Down
3 changes: 3 additions & 0 deletions crates/chains-info/supported_chains.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ chain-ids = [
3884533461, # Athena Testnet
3884533462, # Hermes Testnet
3884533463, # Demeter Testnet
5001,# Athena Local Testnet
5002,# Athena Local Testnet
5003,# Athena Local Testnet
4006, # Tangle EVM Testnet
]

Expand Down
2 changes: 1 addition & 1 deletion crates/relayer-handlers/src/routes/transaction_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub async fn handle_transaction_status_evm(
) -> Result<Json<TransactionStatusResponse>, HandlerError> {
let store = ctx.store();
let maybe_item: Option<QueueItem<TypedTransaction>> = store
.peek_item(SledQueueKey::from_evm_with_custom_key(chain_id, item_key.0))
.get_item(SledQueueKey::from_evm_with_custom_key(chain_id, item_key.0))
.unwrap_or(None);

if let Some(item) = maybe_item {
Expand Down
9 changes: 9 additions & 0 deletions crates/relayer-store/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ where
) -> crate::Result<Option<QueueItem<Item>>>;
/// Check if the item is in the queue.
fn has_item(&self, key: Self::Key) -> crate::Result<bool>;
/// Get item from the queue.
fn get_item(
&self,
key: Self::Key,
) -> crate::Result<Option<QueueItem<Item>>>;
/// Remove an item from the queue.
fn remove_item(
&self,
Expand Down Expand Up @@ -181,6 +186,10 @@ where
S::has_item(self, key)
}

fn get_item(&self, key: Self::Key) -> crate::Result<Option<QueueItem<T>>> {
S::get_item(self, key)
}

fn remove_item(
&self,
key: Self::Key,
Expand Down
94 changes: 94 additions & 0 deletions crates/relayer-store/src/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,23 @@ where
Ok(Some(item))
}

#[tracing::instrument(skip_all, fields(key = %key))]
fn get_item(&self, key: Self::Key) -> crate::Result<Option<QueueItem<T>>> {
let tree = self.db.open_tree(format!("queue_{}", key.queue_name()))?;
let inner_key = match key.item_key() {
Some(k) => k,
None => return Ok(None),
};
if let Some(item_key) = tree.get(&inner_key[..])? {
if let Some(item_bytes) = tree.get(&item_key[..])? {
let item = serde_json::from_slice(&item_bytes)?;
self.db.flush()?;
return Ok(Some(item));
}
}
Ok(None)
}

#[tracing::instrument(skip_all, fields(key = %key))]
fn has_item(&self, key: Self::Key) -> crate::Result<bool> {
let tree = self.db.open_tree(format!("queue_{}", key.queue_name()))?;
Expand Down Expand Up @@ -1207,4 +1224,81 @@ mod tests {
std::thread::sleep(std::time::Duration::from_millis(100));
assert!(!queue_item1.is_expired());
}

#[test]
fn test_get_item_works() {
let tmp = tempfile::tempdir().unwrap();
let store = SledStore::open(tmp.path()).unwrap();
let chain_id = 1u32;
// Step1. Add items to queue
for i in 1..=10 {
let tx1: TypedTransaction = TransactionRequest::pay(
types::Address::from_low_u64_be(i),
types::U256::one(),
)
.from(types::Address::from_low_u64_be(15))
.into();
let queue_item = QueueItem::new(tx1.clone());
store
.enqueue_item(
SledQueueKey::from_evm_with_custom_key(
chain_id,
tx1.item_key(),
),
queue_item,
)
.unwrap();
}
// Step2. update item state to processed.
for _ in 1..=10 {
let item: QueueItem<TypedTransaction> = store
.peek_item(SledQueueKey::from_evm_chain_id(chain_id))
.unwrap()
.unwrap();
// default state of item should be pending.
assert_eq!(item.state(), QueueItemState::Pending);
let tx_hash = item.clone().inner().sighash().0;
// Update state as Processed and shift item to the end of queue.
store
.shift_item_to_end(
SledQueueKey::from_evm_with_custom_key(
chain_id,
item.inner().item_key(),
),
|item1: &mut QueueItem<TypedTransaction>| {
let state = QueueItemState::Processed {
tx_hash: tx_hash.into(),
};
item1.set_state(state);
Ok(())
},
)
.unwrap();
}

// Step3 get specific item from queue.
for i in 1..=10 {
let tx: TypedTransaction = TransactionRequest::pay(
types::Address::from_low_u64_be(i),
types::U256::one(),
)
.from(types::Address::from_low_u64_be(15))
.into();
let tx_item_key = tx.item_key();

let item: QueueItem<TypedTransaction> = store
.get_item(SledQueueKey::from_evm_with_custom_key(
chain_id,
tx.item_key(),
))
.unwrap()
.unwrap();
let item_key_found = item.clone().inner().item_key();
assert_eq!(item_key_found, tx_item_key);
let expect_item_state = QueueItemState::Processed {
tx_hash: tx.sighash().0.into(),
};
assert_eq!(item.state(), expect_item_state);
}
}
}
Loading

0 comments on commit d59cab0

Please sign in to comment.