Skip to content

Commit

Permalink
Merge pull request #2514 from b-zee/feat-networking-transaction-with-…
Browse files Browse the repository at this point in the history
…payment

feat(networking): add TransactionWithPayment
  • Loading branch information
b-zee authored Dec 10, 2024
2 parents 1716b04 + a0162f4 commit 111b635
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions ant-networking/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ impl SwarmDriver {
}
RecordKind::ChunkWithPayment
| RecordKind::RegisterWithPayment
| RecordKind::TransactionWithPayment
| RecordKind::ScratchpadWithPayment => {
error!("Record {record_key:?} with payment shall not be stored locally.");
return Err(NetworkError::InCorrectRecordHeader);
Expand Down
1 change: 1 addition & 0 deletions ant-networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ impl Network {
match kind {
RecordKind::Chunk
| RecordKind::ChunkWithPayment
| RecordKind::TransactionWithPayment
| RecordKind::RegisterWithPayment
| RecordKind::ScratchpadWithPayment => {
error!("Encountered a split record for {pretty_key:?} with unexpected RecordKind {kind:?}, skipping.");
Expand Down
52 changes: 51 additions & 1 deletion ant-node/src/put_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,55 @@ impl Node {
}
result
}
RecordKind::TransactionWithPayment => {
let (payment, transaction) =
try_deserialize_record::<(ProofOfPayment, Transaction)>(&record)?;

// check if the deserialized value's TransactionAddress matches the record's key
let net_addr = NetworkAddress::from_transaction_address(transaction.address());
let key = net_addr.to_record_key();
let pretty_key = PrettyPrintRecordKey::from(&key);
if record.key != key {
warn!(
"Record's key {pretty_key:?} does not match with the value's TransactionAddress, ignoring PUT."
);
return Err(Error::RecordKeyMismatch);
}

let already_exists = self.validate_key_and_existence(&net_addr, &key).await?;

// The transaction may already exist during the replication.
// The payment shall get deposit to self even the transaction already presents.
// However, if the transaction is already present, the incoming one shall be
// appended with the existing one, if content is different.
if let Err(err) = self
.payment_for_us_exists_and_is_still_valid(&net_addr, payment)
.await
{
if already_exists {
debug!("Payment of the incoming exists transaction {pretty_key:?} having error {err:?}");
} else {
error!("Payment of the incoming non-exist transaction {pretty_key:?} having error {err:?}");
return Err(err);
}
}

let res = self
.validate_merge_and_store_transactions(vec![transaction], &key)
.await;
if res.is_ok() {
let content_hash = XorName::from_content(&record.value);

// Notify replication_fetcher to mark the attempt as completed.
// Send the notification earlier to avoid it got skipped due to:
// the record becomes stored during the fetch because of other interleaved process.
self.network().notify_fetch_completed(
record.key.clone(),
RecordType::NonChunk(content_hash),
);
}
res
}
RecordKind::Register => {
let register = try_deserialize_record::<SignedRegister>(&record)?;

Expand Down Expand Up @@ -282,6 +331,7 @@ impl Node {
match record_header.kind {
// A separate flow handles payment for chunks and registers
RecordKind::ChunkWithPayment
| RecordKind::TransactionWithPayment
| RecordKind::RegisterWithPayment
| RecordKind::ScratchpadWithPayment => {
warn!("Prepaid record came with Payment, which should be handled in another flow");
Expand Down Expand Up @@ -515,7 +565,7 @@ impl Node {
Ok(())
}

/// Validate and store `Vec<AAATransaction>` to the RecordStore
/// Validate and store `Vec<Transaction>` to the RecordStore
/// If we already have a transaction at this address, the Vec is extended and stored.
pub(crate) async fn validate_merge_and_store_transactions(
&self,
Expand Down
3 changes: 3 additions & 0 deletions ant-protocol/src/storage/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum RecordKind {
Chunk,
ChunkWithPayment,
Transaction,
TransactionWithPayment,
Register,
RegisterWithPayment,
Scratchpad,
Expand All @@ -54,6 +55,7 @@ impl Serialize for RecordKind {
Self::RegisterWithPayment => serializer.serialize_u32(4),
Self::Scratchpad => serializer.serialize_u32(5),
Self::ScratchpadWithPayment => serializer.serialize_u32(6),
Self::TransactionWithPayment => serializer.serialize_u32(7),
}
}
}
Expand All @@ -72,6 +74,7 @@ impl<'de> Deserialize<'de> for RecordKind {
4 => Ok(Self::RegisterWithPayment),
5 => Ok(Self::Scratchpad),
6 => Ok(Self::ScratchpadWithPayment),
7 => Ok(Self::TransactionWithPayment),
_ => Err(serde::de::Error::custom(
"Unexpected integer for RecordKind variant",
)),
Expand Down

0 comments on commit 111b635

Please sign in to comment.