-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathbatch.go
More file actions
115 lines (96 loc) · 3.36 KB
/
batch.go
File metadata and controls
115 lines (96 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package store
import (
"context"
"crypto/sha256"
"fmt"
ds "github.com/ipfs/go-datastore"
"github.com/evstack/ev-node/types"
)
// DefaultBatch provides a batched write interface for atomic store operations
type DefaultBatch struct {
store *DefaultStore
batch ds.Batch
ctx context.Context
}
// NewBatch creates a new batch for atomic operations
func (s *DefaultStore) NewBatch(ctx context.Context) (Batch, error) {
batch, err := s.db.Batch(ctx)
if err != nil {
return nil, fmt.Errorf("failed to create a new batch: %w", err)
}
return &DefaultBatch{
store: s,
batch: batch,
ctx: ctx,
}, nil
}
// SetHeight sets the height in the batch
func (b *DefaultBatch) SetHeight(height uint64) error {
currentHeight, err := b.store.Height(b.ctx)
if err != nil {
return err
}
if height <= currentHeight {
return nil
}
heightBytes := encodeHeight(height)
return b.batch.Put(b.ctx, ds.RawKey(getHeightKey()), heightBytes)
}
// SaveBlockData saves block data to the batch
func (b *DefaultBatch) SaveBlockData(header *types.SignedHeader, data *types.Data, signature *types.Signature) error {
headerBlob, err := header.MarshalBinary()
if err != nil {
return fmt.Errorf("failed to marshal Header to binary: %w", err)
}
dataBlob, err := data.MarshalBinary()
if err != nil {
return fmt.Errorf("failed to marshal Data to binary: %w", err)
}
return b.SaveBlockDataFromBytes(header, headerBlob, dataBlob, signature)
}
// SaveBlockDataFromBytes saves pre-serialized block data to the batch.
// This avoids re-marshalling header and data when the caller already has the binary blobs.
func (b *DefaultBatch) SaveBlockDataFromBytes(header *types.SignedHeader, headerBlob, dataBlob []byte, signature *types.Signature) error {
height := header.Height()
signatureHash := *signature
if err := b.batch.Put(b.ctx, ds.RawKey(getHeaderKey(height)), headerBlob); err != nil {
return fmt.Errorf("failed to put header blob in batch: %w", err)
}
if err := b.batch.Put(b.ctx, ds.RawKey(getDataKey(height)), dataBlob); err != nil {
return fmt.Errorf("failed to put data blob in batch: %w", err)
}
if err := b.batch.Put(b.ctx, ds.RawKey(getSignatureKey(height)), signatureHash[:]); err != nil {
return fmt.Errorf("failed to put signature blob in batch: %w", err)
}
headerHash := sha256.Sum256(headerBlob)
heightBytes := encodeHeight(height)
if err := b.batch.Put(b.ctx, ds.RawKey(getIndexKey(headerHash[:])), heightBytes); err != nil {
return fmt.Errorf("failed to put index key in batch: %w", err)
}
return nil
}
// UpdateState updates the state in the batch.
// Uses pooled State.MarshalBinary to reduce per-block allocations.
func (b *DefaultBatch) UpdateState(state types.State) error {
height := state.LastBlockHeight
data, err := state.MarshalBinary()
if err != nil {
return fmt.Errorf("failed to marshal state: %w", err)
}
return b.batch.Put(b.ctx, ds.RawKey(getStateAtHeightKey(height)), data)
}
// Commit commits all batched operations atomically
func (b *DefaultBatch) Commit() error {
if err := b.batch.Commit(b.ctx); err != nil {
return fmt.Errorf("failed to commit batch: %w", err)
}
return nil
}
// Delete adds a delete operation to the batch
func (b *DefaultBatch) Delete(key ds.Key) error {
return b.batch.Delete(b.ctx, key)
}
// Put adds a put operation to the batch
func (b *DefaultBatch) Put(key ds.Key, value []byte) error {
return b.batch.Put(b.ctx, key, value)
}