|
6 | 6 | "github.com/NethermindEth/juno/core/felt" |
7 | 7 | "github.com/NethermindEth/juno/db" |
8 | 8 | "github.com/NethermindEth/juno/encoder" |
9 | | - "github.com/NethermindEth/juno/utils" |
10 | 9 | ) |
11 | 10 |
|
12 | 11 | /** |
@@ -247,66 +246,46 @@ func DeleteBlockHeaderByNumber(w db.KeyValueWriter, number uint64) error { |
247 | 246 | return w.Delete(db.BlockHeaderByNumberKey(number)) |
248 | 247 | } |
249 | 248 |
|
250 | | -func GetReceiptByBlockNumIndexBytes(r db.KeyValueReader, bnIndex []byte) (*TransactionReceipt, error) { |
251 | | - var ( |
252 | | - receipt *TransactionReceipt |
253 | | - val []byte |
254 | | - ) |
255 | | - err := r.Get(db.ReceiptByBlockNumIndexKeyBytes(bnIndex), func(data []byte) error { |
256 | | - val = data |
257 | | - return nil |
258 | | - }) |
| 249 | +func GetReceiptByHash( |
| 250 | + r db.KeyValueReader, |
| 251 | + hash *felt.TransactionHash, |
| 252 | +) (*TransactionReceipt, error) { |
| 253 | + val, err := TransactionBlockNumbersAndIndicesByHashBucket.RawValue().Get(r, hash) |
259 | 254 | if err != nil { |
260 | 255 | return nil, err |
261 | 256 | } |
262 | | - if err = encoder.Unmarshal(val, &receipt); err != nil { |
263 | | - return nil, err |
264 | | - } |
265 | | - return receipt, nil |
266 | | -} |
267 | 257 |
|
268 | | -func WriteReceiptByBlockNumIndex(w db.KeyValueWriter, num, index uint64, receipt *TransactionReceipt) error { |
269 | | - data, err := encoder.Marshal(receipt) |
270 | | - if err != nil { |
271 | | - return err |
272 | | - } |
273 | | - return w.Put(db.ReceiptByBlockNumIndexKey(num, index), data) |
274 | | -} |
275 | | - |
276 | | -func DeleteReceiptByBlockNumIndex(w db.KeyValueWriter, num, index uint64) error { |
277 | | - return w.Delete(db.ReceiptByBlockNumIndexKey(num, index)) |
278 | | -} |
279 | | - |
280 | | -func GetReceiptByHash(r db.KeyValueReader, hash *felt.Felt) (*TransactionReceipt, error) { |
281 | | - var val []byte |
282 | | - err := r.Get(db.TxBlockNumIndexByHashKey(hash), func(data []byte) error { |
283 | | - val = data |
284 | | - return nil |
285 | | - }) |
| 258 | + receipt, err := ReceiptsByBlockNumberAndIndexBucket.RawKey().Get(r, val) |
286 | 259 | if err != nil { |
287 | 260 | return nil, err |
288 | 261 | } |
289 | | - |
290 | | - return GetReceiptByBlockNumIndexBytes(r, val) |
| 262 | + return &receipt, nil |
291 | 263 | } |
292 | 264 |
|
293 | 265 | func DeleteTxsAndReceipts(batch db.IndexedBatch, blockNum, numTxs uint64) error { |
294 | 266 | // remove txs and receipts |
295 | 267 | for i := range numTxs { |
296 | | - txn, err := GetTxByBlockNumIndex(batch, blockNum, i) |
| 268 | + key := db.BlockNumIndexKey{ |
| 269 | + Number: blockNum, |
| 270 | + Index: i, |
| 271 | + } |
| 272 | + txn, err := TransactionsByBlockNumberAndIndexBucket.Get(batch, key) |
297 | 273 | if err != nil { |
298 | 274 | return err |
299 | 275 | } |
300 | 276 |
|
301 | | - if err := DeleteTxByBlockNumIndex(batch, blockNum, i); err != nil { |
| 277 | + if err := TransactionsByBlockNumberAndIndexBucket.Delete(batch, key); err != nil { |
302 | 278 | return err |
303 | 279 | } |
304 | | - if err := DeleteReceiptByBlockNumIndex(batch, blockNum, i); err != nil { |
| 280 | + if err := ReceiptsByBlockNumberAndIndexBucket.Delete(batch, key); err != nil { |
305 | 281 | return err |
306 | 282 | } |
307 | | - if err := DeleteTxBlockNumIndexByHash(batch, txn.Hash()); err != nil { |
| 283 | + |
| 284 | + txHash := (*felt.TransactionHash)(txn.Hash()) |
| 285 | + if err := TransactionBlockNumbersAndIndicesByHashBucket.Delete(batch, txHash); err != nil { |
308 | 286 | return err |
309 | 287 | } |
| 288 | + |
310 | 289 | if l1handler, ok := txn.(*L1HandlerTransaction); ok { |
311 | 290 | if err := DeleteL1HandlerTxnHashByMsgHash(batch, l1handler.MessageHash()); err != nil { |
312 | 291 | return err |
@@ -411,61 +390,29 @@ func WriteBlockHeader(w db.KeyValueWriter, header *Header) error { |
411 | 390 |
|
412 | 391 | // Returns all transactions in a given block |
413 | 392 | func GetTxsByBlockNum(r db.KeyValueReader, blockNum uint64) ([]Transaction, error) { |
414 | | - prefix := db.TransactionsByBlockNumberAndIndex.Key(MarshalBlockNumber(blockNum)) |
415 | | - |
416 | | - it, err := r.NewIterator(prefix, true) |
417 | | - if err != nil { |
418 | | - return nil, err |
419 | | - } |
420 | | - |
421 | | - var txs []Transaction |
422 | | - for it.First(); it.Valid(); it.Next() { |
423 | | - val, vErr := it.Value() |
424 | | - if vErr != nil { |
425 | | - return nil, utils.RunAndWrapOnError(it.Close, vErr) |
426 | | - } |
| 393 | + txs := []Transaction{} |
| 394 | + txSeq := TransactionsByBlockNumberAndIndexBucket.Prefix().Add(blockNum).Scan(r) |
427 | 395 |
|
428 | | - var tx Transaction |
429 | | - if err = encoder.Unmarshal(val, &tx); err != nil { |
430 | | - return nil, utils.RunAndWrapOnError(it.Close, err) |
| 396 | + for tx, err := range txSeq { |
| 397 | + if err != nil { |
| 398 | + return nil, err |
431 | 399 | } |
432 | | - |
433 | | - txs = append(txs, tx) |
434 | | - } |
435 | | - |
436 | | - if err = it.Close(); err != nil { |
437 | | - return nil, err |
| 400 | + txs = append(txs, tx.Value) |
438 | 401 | } |
439 | 402 |
|
440 | 403 | return txs, nil |
441 | 404 | } |
442 | 405 |
|
443 | 406 | // Returns all receipts in a given block |
444 | 407 | func GetReceiptsByBlockNum(r db.KeyValueReader, blockNum uint64) ([]*TransactionReceipt, error) { |
445 | | - prefix := db.ReceiptsByBlockNumberAndIndex.Key(MarshalBlockNumber(blockNum)) |
446 | | - |
447 | | - it, err := r.NewIterator(prefix, true) |
448 | | - if err != nil { |
449 | | - return nil, err |
450 | | - } |
| 408 | + receipts := []*TransactionReceipt{} |
| 409 | + receiptSeq := ReceiptsByBlockNumberAndIndexBucket.Prefix().Add(blockNum).Scan(r) |
451 | 410 |
|
452 | | - var receipts []*TransactionReceipt |
453 | | - for it.First(); it.Valid(); it.Next() { |
454 | | - val, vErr := it.Value() |
455 | | - if vErr != nil { |
456 | | - return nil, utils.RunAndWrapOnError(it.Close, vErr) |
457 | | - } |
458 | | - |
459 | | - var receipt *TransactionReceipt |
460 | | - if err = encoder.Unmarshal(val, &receipt); err != nil { |
461 | | - return nil, utils.RunAndWrapOnError(it.Close, err) |
| 411 | + for receipt, err := range receiptSeq { |
| 412 | + if err != nil { |
| 413 | + return nil, err |
462 | 414 | } |
463 | | - |
464 | | - receipts = append(receipts, receipt) |
465 | | - } |
466 | | - |
467 | | - if err = it.Close(); err != nil { |
468 | | - return nil, err |
| 415 | + receipts = append(receipts, &receipt.Value) |
469 | 416 | } |
470 | 417 |
|
471 | 418 | return receipts, nil |
@@ -494,85 +441,40 @@ func GetBlockByNumber(r db.KeyValueReader, blockNum uint64) (*Block, error) { |
494 | 441 | }, nil |
495 | 442 | } |
496 | 443 |
|
497 | | -func GetTxBlockNumIndexByHash(r db.KeyValueReader, hash *felt.Felt) (db.BlockNumIndexKey, error) { |
498 | | - bnIndex := db.BlockNumIndexKey{} |
499 | | - err := r.Get(db.TxBlockNumIndexByHashKey(hash), bnIndex.UnmarshalBinary) |
500 | | - return bnIndex, err |
501 | | -} |
502 | | - |
503 | | -func WriteTxBlockNumIndexByHash(w db.KeyValueWriter, num, index uint64, hash *felt.Felt) error { |
504 | | - val := db.BlockNumIndexKey{ |
| 444 | +func WriteTxAndReceipt( |
| 445 | + w db.KeyValueWriter, |
| 446 | + num, index uint64, |
| 447 | + tx Transaction, |
| 448 | + receipt *TransactionReceipt, |
| 449 | +) error { |
| 450 | + txHash := (*felt.TransactionHash)(tx.Hash()) |
| 451 | + key := db.BlockNumIndexKey{ |
505 | 452 | Number: num, |
506 | 453 | Index: index, |
507 | 454 | } |
508 | | - return w.Put(db.TxBlockNumIndexByHashKey(hash), val.Marshal()) |
509 | | -} |
510 | 455 |
|
511 | | -func DeleteTxBlockNumIndexByHash(w db.KeyValueWriter, hash *felt.Felt) error { |
512 | | - return w.Delete(db.TxBlockNumIndexByHashKey(hash)) |
513 | | -} |
514 | | - |
515 | | -func GetTxByBlockNumIndex(r db.KeyValueReader, blockNum, index uint64) (Transaction, error) { |
516 | | - var tx Transaction |
517 | | - err := r.Get(db.TxByBlockNumIndexKey(blockNum, index), func(data []byte) error { |
518 | | - return encoder.Unmarshal(data, &tx) |
519 | | - }) |
520 | | - if err != nil { |
521 | | - return nil, err |
522 | | - } |
523 | | - return tx, nil |
524 | | -} |
525 | | - |
526 | | -func GetTxByBlockNumIndexBytes(r db.KeyValueReader, val []byte) (Transaction, error) { |
527 | | - var tx Transaction |
528 | | - err := r.Get(db.TxByBlockNumIndexKeyBytes(val), func(data []byte) error { |
529 | | - return encoder.Unmarshal(data, &tx) |
530 | | - }) |
531 | | - if err != nil { |
532 | | - return nil, err |
533 | | - } |
534 | | - return tx, nil |
535 | | -} |
536 | | - |
537 | | -func WriteTxByBlockNumIndex(w db.KeyValueWriter, num, index uint64, tx Transaction) error { |
538 | | - enc, err := encoder.Marshal(tx) |
539 | | - if err != nil { |
540 | | - return err |
541 | | - } |
542 | | - return w.Put(db.TxByBlockNumIndexKey(num, index), enc) |
543 | | -} |
544 | | - |
545 | | -func DeleteTxByBlockNumIndex(w db.KeyValueWriter, num, index uint64) error { |
546 | | - return w.Delete(db.TxByBlockNumIndexKey(num, index)) |
547 | | -} |
548 | | - |
549 | | -func WriteTxAndReceipt(w db.KeyValueWriter, num, index uint64, tx Transaction, receipt *TransactionReceipt) error { |
550 | | - if err := WriteTxBlockNumIndexByHash(w, num, index, receipt.TransactionHash); err != nil { |
| 456 | + if err := TransactionBlockNumbersAndIndicesByHashBucket.Put(w, txHash, &key); err != nil { |
551 | 457 | return err |
552 | 458 | } |
553 | 459 |
|
554 | | - if err := WriteTxByBlockNumIndex(w, num, index, tx); err != nil { |
| 460 | + if err := TransactionsByBlockNumberAndIndexBucket.Put(w, key, &tx); err != nil { |
555 | 461 | return err |
556 | 462 | } |
557 | 463 |
|
558 | | - if err := WriteReceiptByBlockNumIndex(w, num, index, receipt); err != nil { |
| 464 | + if err := ReceiptsByBlockNumberAndIndexBucket.Put(w, key, receipt); err != nil { |
559 | 465 | return err |
560 | 466 | } |
561 | 467 |
|
562 | 468 | return nil |
563 | 469 | } |
564 | 470 |
|
565 | | -func GetTxByHash(r db.KeyValueReader, hash *felt.Felt) (Transaction, error) { |
566 | | - var val []byte |
567 | | - err := r.Get(db.TxBlockNumIndexByHashKey(hash), func(data []byte) error { |
568 | | - val = data |
569 | | - return nil |
570 | | - }) |
| 471 | +func GetTxByHash(r db.KeyValueReader, hash *felt.TransactionHash) (Transaction, error) { |
| 472 | + val, err := TransactionBlockNumbersAndIndicesByHashBucket.RawValue().Get(r, hash) |
571 | 473 | if err != nil { |
572 | 474 | return nil, err |
573 | 475 | } |
574 | 476 |
|
575 | | - return GetTxByBlockNumIndexBytes(r, val) |
| 477 | + return TransactionsByBlockNumberAndIndexBucket.RawKey().Get(r, val) |
576 | 478 | } |
577 | 479 |
|
578 | 480 | func GetAggregatedBloomFilter(r db.KeyValueReader, fromBlock, toBLock uint64) (AggregatedBloomFilter, error) { |
|
0 commit comments