-
Notifications
You must be signed in to change notification settings - Fork 271
/
share_splitting.go
93 lines (81 loc) · 2.91 KB
/
share_splitting.go
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
package shares
import (
"errors"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
appns "github.com/celestiaorg/celestia-app/pkg/namespace"
coretypes "github.com/tendermint/tendermint/types"
"golang.org/x/exp/maps"
)
var (
ErrIncorrectNumberOfIndexes = errors.New(
"number of indexes is not identical to the number of blobs",
)
ErrUnexpectedFirstBlobShareIndex = errors.New(
"the first blob started at an unexpected index",
)
)
// ExtractShareIndexes iterates over the transactions and extracts the share
// indexes from wrapped transactions. It returns nil if the transactions are
// from an old block that did not have share indexes in the wrapped txs.
func ExtractShareIndexes(txs coretypes.Txs) []uint32 {
var shareIndexes []uint32
for _, rawTx := range txs {
if indexWrappedTxs, isIndexWrapped := coretypes.UnmarshalIndexWrapper(rawTx); isIndexWrapped {
// Since share index == 0 is invalid, it indicates that we are
// attempting to extract share indexes from txs that do not have any
// due to them being old. here we return nil to indicate that we are
// attempting to extract indexes from a block that doesn't support
// it. It checks for 0 because if there is a message in the block,
// then there must also be a tx, which will take up at least one
// share.
if len(indexWrappedTxs.ShareIndexes) == 0 {
return nil
}
shareIndexes = append(shareIndexes, indexWrappedTxs.ShareIndexes...)
}
}
return shareIndexes
}
func SplitTxs(txs coretypes.Txs) (txShares []Share, pfbShares []Share, shareRanges map[coretypes.TxKey]Range, err error) {
txWriter := NewCompactShareSplitter(appns.TxNamespace, appconsts.ShareVersionZero)
pfbTxWriter := NewCompactShareSplitter(appns.PayForBlobNamespace, appconsts.ShareVersionZero)
for _, tx := range txs {
if _, isIndexWrapper := coretypes.UnmarshalIndexWrapper(tx); isIndexWrapper {
err = pfbTxWriter.WriteTx(tx)
} else {
err = txWriter.WriteTx(tx)
}
if err != nil {
return nil, nil, nil, err
}
}
txShares, err = txWriter.Export()
if err != nil {
return nil, nil, nil, err
}
txMap := txWriter.ShareRanges(0)
pfbShares, err = pfbTxWriter.Export()
if err != nil {
return nil, nil, nil, err
}
pfbMap := pfbTxWriter.ShareRanges(len(txShares))
return txShares, pfbShares, mergeMaps(txMap, pfbMap), nil
}
// SplitBlobs splits the provided blobs into shares.
func SplitBlobs(blobs ...coretypes.Blob) ([]Share, error) {
writer := NewSparseShareSplitter()
for _, blob := range blobs {
if err := writer.Write(blob); err != nil {
return nil, err
}
}
return writer.Export(), nil
}
// mergeMaps merges two maps into a new map. If there are any duplicate keys,
// the value in the second map takes precedence.
func mergeMaps(mapOne, mapTwo map[coretypes.TxKey]Range) map[coretypes.TxKey]Range {
merged := make(map[coretypes.TxKey]Range, len(mapOne)+len(mapTwo))
maps.Copy(merged, mapOne)
maps.Copy(merged, mapTwo)
return merged
}