Skip to content

Commit d37dff0

Browse files
committed
complete rollback logic
1 parent 5fac389 commit d37dff0

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pile.rs

+59-20
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,21 @@ pub trait Pile {
8080
>;
8181
type Keep: Aora<Id = Opid, Item = SmallOrdMap<u16, <Self::Seal as RgbSeal>::Definiton>>;
8282
type Index: Index<Opid, <Self::Seal as RgbSeal>::WitnessId>;
83+
type Stand: Index<<Self::Seal as RgbSeal>::WitnessId, Opid>;
8384
type Mine: Cru<<Self::Seal as RgbSeal>::WitnessId, Option<WitnessStatus>>;
8485

8586
fn hoard(&self) -> &Self::Hoard;
8687
fn cache(&self) -> &Self::Cache;
8788
fn keep(&self) -> &Self::Keep;
8889
fn index(&self) -> &Self::Index;
90+
fn stand(&self) -> &Self::Stand;
8991
fn mine(&self) -> &Self::Mine;
9092

9193
fn hoard_mut(&mut self) -> &mut Self::Hoard;
9294
fn cache_mut(&mut self) -> &mut Self::Cache;
9395
fn keep_mut(&mut self) -> &mut Self::Keep;
9496
fn index_mut(&mut self) -> &mut Self::Index;
97+
fn stand_mut(&mut self) -> &mut Self::Stand;
9598
fn mine_mut(&mut self) -> &mut Self::Mine;
9699

97100
fn retrieve(&mut self, opid: Opid) -> impl ExactSizeIterator<Item = SealWitness<Self::Seal>>;
@@ -104,6 +107,7 @@ pub trait Pile {
104107
) {
105108
let pubid = published.pub_id();
106109
self.index_mut().add(opid, pubid);
110+
self.stand_mut().add(pubid, opid);
107111
if self.hoard_mut().has(&pubid) {
108112
let mut prev_anchor = self.hoard_mut().read(pubid);
109113
if prev_anchor != anchor {
@@ -182,15 +186,19 @@ pub mod fs {
182186
}
183187

184188
#[derive(Clone, Debug)]
185-
pub struct FileIndex<Id>
186-
where Id: Copy + Ord + From<[u8; 32]> + Into<[u8; 32]>
189+
pub struct FileIndex<K, V>
190+
where
191+
K: Copy + Ord + From<[u8; 32]> + Into<[u8; 32]>,
192+
V: Copy + Ord + From<[u8; 32]> + Into<[u8; 32]>,
187193
{
188194
path: PathBuf,
189-
cache: BTreeMap<Opid, BTreeSet<Id>>,
195+
cache: BTreeMap<K, BTreeSet<V>>,
190196
}
191197

192-
impl<Id> FileIndex<Id>
193-
where Id: Copy + Ord + From<[u8; 32]> + Into<[u8; 32]>
198+
impl<K, V> FileIndex<K, V>
199+
where
200+
K: Copy + Ord + From<[u8; 32]> + Into<[u8; 32]>,
201+
V: Copy + Ord + From<[u8; 32]> + Into<[u8; 32]>,
194202
{
195203
pub fn create(path: PathBuf) -> io::Result<Self> {
196204
File::create_new(&path)?;
@@ -202,7 +210,7 @@ pub mod fs {
202210
let mut file = File::open(&path)?;
203211
let mut buf = [0u8; 32];
204212
while file.read_exact(&mut buf).is_ok() {
205-
let opid = Opid::from(buf);
213+
let opid = K::from(buf);
206214
let mut ids = BTreeSet::new();
207215
let mut len = [0u8; 4];
208216
file.read_exact(&mut len).expect("cannot read index file");
@@ -220,33 +228,35 @@ pub mod fs {
220228

221229
pub fn save(&self) -> io::Result<()> {
222230
let mut index_file = File::create(&self.path)?;
223-
for (opid, ids) in &self.cache {
224-
index_file.write_all(opid.as_slice())?;
225-
let len = ids.len() as u32;
231+
for (key, values) in &self.cache {
232+
index_file.write_all((*key).into().as_slice())?;
233+
let len = values.len() as u32;
226234
index_file.write_all(&len.to_le_bytes())?;
227-
for id in ids {
235+
for id in values {
228236
index_file.write_all(&(*id).into())?;
229237
}
230238
}
231239
Ok(())
232240
}
233241
}
234242

235-
impl<Id: Copy> Index<Opid, Id> for FileIndex<Id>
236-
where Id: Copy + Ord + From<[u8; 32]> + Into<[u8; 32]>
243+
impl<K, V> Index<K, V> for FileIndex<K, V>
244+
where
245+
K: Copy + Ord + From<[u8; 32]> + Into<[u8; 32]>,
246+
V: Copy + Ord + From<[u8; 32]> + Into<[u8; 32]>,
237247
{
238-
fn keys(&self) -> impl Iterator<Item = Opid> { self.cache.keys().copied() }
248+
fn keys(&self) -> impl Iterator<Item = K> { self.cache.keys().copied() }
239249

240-
fn has(&self, key: Opid) -> bool { self.cache.contains_key(&key) }
250+
fn has(&self, key: K) -> bool { self.cache.contains_key(&key) }
241251

242-
fn get(&self, key: Opid) -> impl ExactSizeIterator<Item = Id> {
252+
fn get(&self, key: K) -> impl ExactSizeIterator<Item = V> {
243253
match self.cache.get(&key) {
244254
Some(ids) => ids.clone().into_iter(),
245255
None => bset![].into_iter(),
246256
}
247257
}
248258

249-
fn add(&mut self, key: Opid, val: Id) {
259+
fn add(&mut self, key: K, val: V) {
250260
self.cache.entry(key).or_default().insert(val);
251261
self.save().expect("Cannot save index file");
252262
}
@@ -258,7 +268,8 @@ pub mod fs {
258268
hoard: FileAora<Seal::WitnessId, Seal::Client>,
259269
cache: FileAora<Seal::WitnessId, Seal::Published>,
260270
keep: FileAora<Opid, SmallOrdMap<u16, Seal::Definiton>>,
261-
index: FileIndex<Seal::WitnessId>,
271+
index: FileIndex<Opid, Seal::WitnessId>,
272+
stand: FileIndex<Seal::WitnessId, Opid>,
262273
mine: FileCru,
263274
_phantom: PhantomData<Seal>,
264275
}
@@ -279,12 +290,24 @@ pub mod fs {
279290
let index = FileIndex::create(index_file.clone()).unwrap_or_else(|_| {
280291
panic!("unable to create index file '{}'", index_file.display())
281292
});
293+
let stand_file = path.join("stand.dat");
294+
let stand = FileIndex::create(stand_file.clone()).unwrap_or_else(|_| {
295+
panic!("unable to create stand file '{}'", stand_file.display())
296+
});
282297
let mine_file = path.join("mine.dat");
283298
let mine = FileCru::create(mine_file.clone()).unwrap_or_else(|_| {
284299
panic!("unable to create mining info file '{}'", mine_file.display())
285300
});
286301

287-
Self { hoard, cache, keep, index, mine, _phantom: PhantomData }
302+
Self {
303+
hoard,
304+
cache,
305+
keep,
306+
index,
307+
stand,
308+
mine,
309+
_phantom: PhantomData,
310+
}
288311
}
289312
}
290313

@@ -300,12 +323,23 @@ pub mod fs {
300323
let index_file = path.join("index.dat");
301324
let index = FileIndex::open(index_file.clone())
302325
.unwrap_or_else(|_| panic!("unable to open index file '{}'", index_file.display()));
326+
let stand_file = path.join("stand.dat");
327+
let stand = FileIndex::open(stand_file.clone())
328+
.unwrap_or_else(|_| panic!("unable to open stand file '{}'", stand_file.display()));
303329
let mine_file = path.join("mine.dat");
304330
let mine = FileCru::open(mine_file.clone()).unwrap_or_else(|_| {
305331
panic!("unable to open mining info file '{}'", mine_file.display())
306332
});
307333

308-
Self { hoard, cache, keep, index, mine, _phantom: PhantomData }
334+
Self {
335+
hoard,
336+
cache,
337+
keep,
338+
index,
339+
stand,
340+
mine,
341+
_phantom: PhantomData,
342+
}
309343
}
310344
}
311345

@@ -320,7 +354,8 @@ pub mod fs {
320354
type Hoard = FileAora<Seal::WitnessId, Seal::Client>;
321355
type Cache = FileAora<Seal::WitnessId, Seal::Published>;
322356
type Keep = FileAora<Opid, SmallOrdMap<u16, Seal::Definiton>>;
323-
type Index = FileIndex<Seal::WitnessId>;
357+
type Index = FileIndex<Opid, Seal::WitnessId>;
358+
type Stand = FileIndex<Seal::WitnessId, Opid>;
324359
type Mine = FileCru;
325360

326361
fn hoard(&self) -> &Self::Hoard { &self.hoard }
@@ -331,6 +366,8 @@ pub mod fs {
331366

332367
fn index(&self) -> &Self::Index { &self.index }
333368

369+
fn stand(&self) -> &Self::Stand { &self.stand }
370+
334371
fn mine(&self) -> &Self::Mine { &self.mine }
335372

336373
fn hoard_mut(&mut self) -> &mut Self::Hoard { &mut self.hoard }
@@ -341,6 +378,8 @@ pub mod fs {
341378

342379
fn index_mut(&mut self) -> &mut Self::Index { &mut self.index }
343380

381+
fn stand_mut(&mut self) -> &mut Self::Stand { &mut self.stand }
382+
344383
fn mine_mut(&mut self) -> &mut Self::Mine { &mut self.mine }
345384

346385
fn retrieve(&mut self, opid: Opid) -> impl ExactSizeIterator<Item = SealWitness<Seal>> {

src/stockpile.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,18 @@ impl<S: Supply, P: Pile> Stockpile<S, P> {
434434
}
435435
}
436436

437-
pub fn witness_update_status(
438-
&self,
437+
pub fn update_witness_status(
438+
&mut self,
439439
pubid: <P::Seal as RgbSeal>::WitnessId,
440440
status: Option<WitnessStatus>,
441441
) {
442-
// TODO: Perform rollback
442+
let opids = self.pile.stand().get(pubid);
443+
if status.is_none() {
444+
self.stock.rollback(opids);
445+
} else {
446+
self.stock.forward(opids);
447+
}
448+
self.pile_mut().mine_mut().update(pubid, status);
443449
}
444450
}
445451

0 commit comments

Comments
 (0)