From 0658a5d89b3a3491229c4c099b30baeafaf779e0 Mon Sep 17 00:00:00 2001 From: qima Date: Mon, 6 Jan 2025 23:51:10 +0800 Subject: [PATCH] fix: shall only have Chunk and NonChunk for RecordType --- ant-networking/src/cmd.rs | 7 ++++--- ant-networking/src/record_store.rs | 19 ++++++++++++++----- ant-node/src/put_validation.rs | 10 ++++++---- ant-node/src/python.rs | 14 ++------------ ant-protocol/src/storage/header.rs | 7 ++----- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/ant-networking/src/cmd.rs b/ant-networking/src/cmd.rs index 66c91b612b..17e4e3cfc9 100644 --- a/ant-networking/src/cmd.rs +++ b/ant-networking/src/cmd.rs @@ -662,9 +662,10 @@ impl SwarmDriver { Ok(record_header) => { match record_header.kind { RecordKind::Chunk => RecordType::Chunk, - RecordKind::Scratchpad => RecordType::Scratchpad, - RecordKind::Pointer => RecordType::Pointer, - RecordKind::GraphEntry | RecordKind::Register => { + RecordKind::GraphEntry + | RecordKind::Pointer + | RecordKind::Register + | RecordKind::Scratchpad => { let content_hash = XorName::from_content(&record.value); RecordType::NonChunk(content_hash) } diff --git a/ant-networking/src/record_store.rs b/ant-networking/src/record_store.rs index 5cee127a61..cabdb6611c 100644 --- a/ant-networking/src/record_store.rs +++ b/ant-networking/src/record_store.rs @@ -820,14 +820,23 @@ impl RecordStore for NodeRecordStore { match RecordHeader::from_record(&record) { Ok(record_header) => { match record_header.kind { - RecordKind::ChunkWithPayment | RecordKind::RegisterWithPayment => { + RecordKind::ChunkWithPayment + | RecordKind::GraphEntryWithPayment + | RecordKind::PointerWithPayment + | RecordKind::RegisterWithPayment + | RecordKind::ScratchpadWithPayment => { debug!("Record {record_key:?} with payment shall always be processed."); } - _ => { + // Shall not use wildcard, to avoid mis-match during enum update. + RecordKind::Chunk + | RecordKind::GraphEntry + | RecordKind::Pointer + | RecordKind::Register + | RecordKind::Scratchpad => { // Chunk with existing key do not to be stored again. - // `Spend` or `Register` with same content_hash do not to be stored again, - // otherwise shall be passed further to allow - // double transaction to be detected or register op update. + // Others with same content_hash do not to be stored again, + // otherwise shall be passed further to allow different version of nonchunk + // to be detected or updated. match self.records.get(&record.key) { Some((_addr, RecordType::Chunk)) => { debug!("Chunk {record_key:?} already exists."); diff --git a/ant-node/src/put_validation.rs b/ant-node/src/put_validation.rs index 23d458211d..946b9168e9 100644 --- a/ant-node/src/put_validation.rs +++ b/ant-node/src/put_validation.rs @@ -125,20 +125,21 @@ impl Node { // we eagerly retry replicaiton as it seems like other nodes are having trouble // did not manage to get this scratchpad as yet. Ok(_) | Err(Error::IgnoringOutdatedScratchpadPut) => { + let content_hash = XorName::from_content(&record.value); Marker::ValidScratchpadRecordPutFromClient(&PrettyPrintRecordKey::from( &record_key, )) .log(); self.replicate_valid_fresh_record( record_key.clone(), - RecordType::Scratchpad, + RecordType::NonChunk(content_hash), ); // 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, RecordType::Scratchpad); + .notify_fetch_completed(record_key, RecordType::NonChunk(content_hash)); } Err(_) => {} } @@ -552,14 +553,15 @@ impl Node { publisher: None, expires: None, }; - self.network().put_local_record(record); + self.network().put_local_record(record.clone()); let pretty_key = PrettyPrintRecordKey::from(&scratchpad_key); self.record_metrics(Marker::ValidScratchpadRecordPutFromNetwork(&pretty_key)); if is_client_put { - self.replicate_valid_fresh_record(scratchpad_key, RecordType::Scratchpad); + let content_hash = XorName::from_content(&record.value); + self.replicate_valid_fresh_record(scratchpad_key, RecordType::NonChunk(content_hash)); } Ok(()) diff --git a/ant-node/src/python.rs b/ant-node/src/python.rs index 3d50520940..2571b0d13f 100644 --- a/ant-node/src/python.rs +++ b/ant-node/src/python.rs @@ -4,11 +4,7 @@ use crate::{NodeBuilder, RunningNode}; use ant_evm::{EvmNetwork, RewardsAddress}; use ant_networking::PutRecordCfg; -use ant_protocol::{ - node::get_antnode_root_dir, - storage::{ChunkAddress, RecordType}, - NetworkAddress, -}; +use ant_protocol::{node::get_antnode_root_dir, storage::ChunkAddress, NetworkAddress}; use const_hex::FromHex; use libp2p::{ identity::{Keypair, PeerId}, @@ -239,7 +235,7 @@ impl AntNode { self_: PyRef, key: String, value: Vec, - record_type: String, + _data_type: String, ) -> PyResult<()> { let node_guard = self_ .node @@ -250,12 +246,6 @@ impl AntNode { .try_lock() .map_err(|_| PyRuntimeError::new_err("Failed to acquire runtime lock"))?; - let _record_type = match record_type.to_lowercase().as_str() { - "chunk" => RecordType::Chunk, - "scratchpad" => RecordType::Scratchpad, - _ => return Err(PyValueError::new_err("Invalid record type. Must be one of: 'chunk', 'register', 'scratchpad', 'transaction'")), - }; - match (&*node_guard, &*rt_guard) { (Some(node), Some(rt)) => { let xorname = XorName::from_content( diff --git a/ant-protocol/src/storage/header.rs b/ant-protocol/src/storage/header.rs index 74a13ee6a7..00a4c13003 100644 --- a/ant-protocol/src/storage/header.rs +++ b/ant-protocol/src/storage/header.rs @@ -16,14 +16,11 @@ use std::fmt::Display; use xor_name::XorName; /// Indicates the type of the record content. -/// Note for `Spend` and `Register`, using its content_hash (in `XorName` format) -/// to indicate different content body. +/// This is to be only used within the node instance to reflect different content version. +/// Hence, only need to have two entries: Chunk and NonChunk. #[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)] pub enum RecordType { Chunk, - Scratchpad, - Pointer, - GraphEntry, NonChunk(XorName), }