|
| 1 | +// Copyright 2026 The Accumulate Authors |
| 2 | +// |
| 3 | +// Use of this source code is governed by an MIT-style |
| 4 | +// license that can be found in the LICENSE file or at |
| 5 | +// https://opensource.org/licenses/MIT. |
| 6 | + |
| 7 | +package bpt |
| 8 | + |
| 9 | +import ( |
| 10 | + "crypto/sha256" |
| 11 | + "fmt" |
| 12 | + "sync" |
| 13 | + |
| 14 | + "gitlab.com/accumulatenetwork/accumulate/pkg/database" |
| 15 | + "gitlab.com/accumulatenetwork/accumulate/pkg/errors" |
| 16 | + "gitlab.com/accumulatenetwork/accumulate/pkg/types/record" |
| 17 | +) |
| 18 | + |
| 19 | +// ShardedBPT is a BPT that partitions the tree at a configurable depth into |
| 20 | +// independent shards for parallel updates. Each shard is a standard BPT |
| 21 | +// instance with its own locking, providing embarrassingly parallel operations |
| 22 | +// with zero contention between shards. |
| 23 | +// |
| 24 | +// The sharding is based on the natural binary structure of the tree. At depth |
| 25 | +// N, the tree has 2^N branches, and each branch becomes an independent shard. |
| 26 | +// Keys are routed to shards using the high-order bits of the key hash. |
| 27 | +// |
| 28 | +// Storage format is identical to non-sharded BPT - no database changes needed. |
| 29 | +// The tree structure itself provides natural partitioning. |
| 30 | +type ShardedBPT struct { |
| 31 | + shardDepth int // Number of bits for routing (4, 5, or 6) |
| 32 | + numShards int // Number of shards (2^shardDepth) |
| 33 | + shards []*BPT // Array of standard BPT instances |
| 34 | + shardMu []sync.Mutex // Per-shard locks for thread safety |
| 35 | + store database.Store |
| 36 | + key *record.Key |
| 37 | +} |
| 38 | + |
| 39 | +// NewShardedBPT creates a new ShardedBPT with the specified shard depth. |
| 40 | +// The depth determines how many shards are created (2^depth shards). |
| 41 | +// |
| 42 | +// Recommended depths: |
| 43 | +// - 4 bits = 16 shards (optimal for 16-core systems) |
| 44 | +// - 5 bits = 32 shards (for 32-core systems) |
| 45 | +// - 6 bits = 64 shards (diminishing returns beyond this) |
| 46 | +// |
| 47 | +// The storage key is used as the base for all shard BPT instances. |
| 48 | +func NewShardedBPT(store database.Store, key *record.Key, depth int) (*ShardedBPT, error) { |
| 49 | + if depth < 1 || depth > 8 { |
| 50 | + return nil, errors.BadRequest.WithFormat("shard depth must be between 1 and 8, got %d", depth) |
| 51 | + } |
| 52 | + |
| 53 | + numShards := 1 << depth |
| 54 | + s := &ShardedBPT{ |
| 55 | + shardDepth: depth, |
| 56 | + numShards: numShards, |
| 57 | + shards: make([]*BPT, numShards), |
| 58 | + shardMu: make([]sync.Mutex, numShards), |
| 59 | + store: store, |
| 60 | + key: key, |
| 61 | + } |
| 62 | + |
| 63 | + // Create a BPT instance for each shard |
| 64 | + // Each shard gets its own key prefix to avoid storage collisions |
| 65 | + for i := 0; i < numShards; i++ { |
| 66 | + shardKey := key.Append(fmt.Sprintf("shard-%d", i)) |
| 67 | + s.shards[i] = New(nil, nil, store, shardKey) |
| 68 | + } |
| 69 | + |
| 70 | + return s, nil |
| 71 | +} |
| 72 | + |
| 73 | +// routeToShard determines which shard a key belongs to based on the high-order |
| 74 | +// bits of the key hash. This uses the same routing logic as the BPT's internal |
| 75 | +// tree structure. Returns both the shard and its index for locking. |
| 76 | +func (s *ShardedBPT) routeToShard(keyHash [32]byte) (int, *BPT) { |
| 77 | + // Extract the high-order bits from the first byte of the key hash |
| 78 | + // For depth=4: shifts right by 4, giving us bits 7-4 (0-15) |
| 79 | + // For depth=5: shifts right by 3, giving us bits 7-3 (0-31) |
| 80 | + // For depth=6: shifts right by 2, giving us bits 7-2 (0-63) |
| 81 | + shardID := int(keyHash[0] >> (8 - s.shardDepth)) |
| 82 | + return shardID, s.shards[shardID] |
| 83 | +} |
| 84 | + |
| 85 | +// Insert updates or inserts a value for the given key. The operation is |
| 86 | +// routed to the appropriate shard based on the key hash. Thread-safe with |
| 87 | +// per-shard locking. |
| 88 | +func (s *ShardedBPT) Insert(key *record.Key, value []byte) error { |
| 89 | + shardID, shard := s.routeToShard(key.Hash()) |
| 90 | + s.shardMu[shardID].Lock() |
| 91 | + defer s.shardMu[shardID].Unlock() |
| 92 | + return shard.Insert(key, value) |
| 93 | +} |
| 94 | + |
| 95 | +// Get retrieves the value associated with the given key. The operation is |
| 96 | +// routed to the appropriate shard based on the key hash. Thread-safe with |
| 97 | +// per-shard locking. |
| 98 | +func (s *ShardedBPT) Get(key *record.Key) ([]byte, error) { |
| 99 | + shardID, shard := s.routeToShard(key.Hash()) |
| 100 | + s.shardMu[shardID].Lock() |
| 101 | + defer s.shardMu[shardID].Unlock() |
| 102 | + return shard.Get(key) |
| 103 | +} |
| 104 | + |
| 105 | +// Delete removes the entry for the given key, if present. The operation is |
| 106 | +// routed to the appropriate shard based on the key hash. Thread-safe with |
| 107 | +// per-shard locking. |
| 108 | +func (s *ShardedBPT) Delete(key *record.Key) error { |
| 109 | + shardID, shard := s.routeToShard(key.Hash()) |
| 110 | + s.shardMu[shardID].Lock() |
| 111 | + defer s.shardMu[shardID].Unlock() |
| 112 | + return shard.Delete(key) |
| 113 | +} |
| 114 | + |
| 115 | +// GetRootHash computes the root hash by combining all shard root hashes |
| 116 | +// hierarchically. This is the only coordination point in the sharded BPT. |
| 117 | +// |
| 118 | +// The algorithm: |
| 119 | +// 1. Read root hash from each shard (with per-shard locking) |
| 120 | +// 2. Combine the shard roots bottom-up in a virtual binary tree |
| 121 | +// 3. Return the final root hash |
| 122 | +// |
| 123 | +// This produces the same root hash as a non-sharded BPT with the same data. |
| 124 | +func (s *ShardedBPT) GetRootHash() ([32]byte, error) { |
| 125 | + // Read all shard root hashes with per-shard locking |
| 126 | + shardRoots := make([][32]byte, s.numShards) |
| 127 | + for i, shard := range s.shards { |
| 128 | + s.shardMu[i].Lock() |
| 129 | + rootHash, err := shard.GetRootHash() |
| 130 | + s.shardMu[i].Unlock() |
| 131 | + |
| 132 | + if err != nil { |
| 133 | + return [32]byte{}, errors.UnknownError.WithFormat("get shard %d root: %w", i, err) |
| 134 | + } |
| 135 | + shardRoots[i] = rootHash |
| 136 | + } |
| 137 | + |
| 138 | + // Combine the shard roots hierarchically |
| 139 | + return s.combineShardRoots(shardRoots), nil |
| 140 | +} |
| 141 | + |
| 142 | +// combineShardRoots combines an array of shard root hashes into a single |
| 143 | +// root hash by building a virtual binary tree bottom-up. |
| 144 | +// |
| 145 | +// The algorithm pairs adjacent roots and hashes them together, repeating |
| 146 | +// until only one root remains. For odd numbers of roots, the last root |
| 147 | +// is carried forward to the next level. |
| 148 | +// |
| 149 | +// CRITICAL: BPT routing is inverted - bit=1 goes LEFT, bit=0 goes RIGHT. |
| 150 | +// So when combining shard i (even) and i+1 (odd), we hash (i+1, i) not (i, i+1). |
| 151 | +// |
| 152 | +// This follows the same hash semantics as BPT's branch.getHash() to ensure |
| 153 | +// the final root hash is identical to a non-sharded BPT. |
| 154 | +func (s *ShardedBPT) combineShardRoots(roots [][32]byte) [32]byte { |
| 155 | + current := roots |
| 156 | + |
| 157 | + // Build virtual tree bottom-up |
| 158 | + for len(current) > 1 { |
| 159 | + next := make([][32]byte, (len(current)+1)/2) |
| 160 | + for i := 0; i < len(current); i += 2 { |
| 161 | + if i+1 < len(current) { |
| 162 | + // Pair exists - hash (odd, even) because BPT routing is inverted |
| 163 | + // Shard i (even, bit=0) goes RIGHT |
| 164 | + // Shard i+1 (odd, bit=1) goes LEFT |
| 165 | + // So hash(LEFT, RIGHT) = hash(i+1, i) |
| 166 | + next[i/2] = hashBranch(current[i+1], current[i]) |
| 167 | + } else { |
| 168 | + // Odd number - carry forward the last root |
| 169 | + next[i/2] = current[i] |
| 170 | + } |
| 171 | + } |
| 172 | + current = next |
| 173 | + } |
| 174 | + |
| 175 | + return current[0] |
| 176 | +} |
| 177 | + |
| 178 | +// hashBranch combines two branch hashes following BPT's branch.getHash() |
| 179 | +// semantics exactly. This is critical for ensuring root hash equivalence |
| 180 | +// between sharded and non-sharded BPTs. |
| 181 | +// |
| 182 | +// The logic: |
| 183 | +// - Both non-empty: SHA256(left || right) |
| 184 | +// - Only left non-empty: return left |
| 185 | +// - Only right non-empty: return right |
| 186 | +// - Both empty: return empty hash |
| 187 | +func hashBranch(left, right [32]byte) [32]byte { |
| 188 | + leftEmpty := left == [32]byte{} |
| 189 | + rightEmpty := right == [32]byte{} |
| 190 | + |
| 191 | + switch { |
| 192 | + case !leftEmpty && !rightEmpty: |
| 193 | + // Both branches present - concatenate and hash |
| 194 | + var b [64]byte |
| 195 | + copy(b[:32], left[:]) |
| 196 | + copy(b[32:], right[:]) |
| 197 | + return sha256.Sum256(b[:]) |
| 198 | + case !leftEmpty: |
| 199 | + // Only left branch - return it directly |
| 200 | + return left |
| 201 | + case !rightEmpty: |
| 202 | + // Only right branch - return it directly |
| 203 | + return right |
| 204 | + default: |
| 205 | + // Both empty - return empty hash |
| 206 | + return [32]byte{} |
| 207 | + } |
| 208 | +} |
0 commit comments