-
Notifications
You must be signed in to change notification settings - Fork 170
/
chunk.go
108 lines (89 loc) · 3.11 KB
/
chunk.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package flow
type ChunkBody struct {
CollectionIndex uint
// execution info
StartState StateCommitment // start state when starting executing this chunk
EventCollection Identifier // Events generated by executing results
BlockID Identifier // Block id of the execution result this chunk belongs to
// Computation consumption info
TotalComputationUsed uint64 // total amount of computation used by running all txs in this chunk
NumberOfTransactions uint64 // number of transactions inside the collection
}
type Chunk struct {
ChunkBody
Index uint64 // chunk index inside the ER (starts from zero)
// EndState inferred from next chunk or from the ER
EndState StateCommitment
}
// ID returns a unique id for this entity
func (ch *Chunk) ID() Identifier {
return MakeID(ch.ChunkBody)
}
// Checksum provides a cryptographic commitment for a chunk content
func (ch *Chunk) Checksum() Identifier {
return MakeID(ch)
}
// ChunkDataPack holds all register touches (any read, or write)
// note that we have to capture a read proof for each write before updating the registers
type ChunkDataPack struct {
ChunkID Identifier
StartState StateCommitment
Proof StorageProof
CollectionID Identifier
}
// ID returns the unique identifier for the concrete view, which is the ID of
// the chunk the view is for.
func (c *ChunkDataPack) ID() Identifier {
return c.ChunkID
}
// Checksum returns the checksum of the chunk data pack.
func (c *ChunkDataPack) Checksum() Identifier {
return MakeID(c)
}
// Note that this is the basic version of the List, we need to substitute it with something like Merkel tree at some point
type ChunkList []*Chunk
func (cl ChunkList) Fingerprint() Identifier {
return MerkleRoot(GetIDs(cl)...)
}
func (cl *ChunkList) Insert(ch *Chunk) {
*cl = append(*cl, ch)
}
func (cl ChunkList) Items() []*Chunk {
return cl
}
// ByChecksum returns an entity from the list by entity fingerprint
func (cl ChunkList) ByChecksum(cs Identifier) (*Chunk, bool) {
for _, ch := range cl {
if ch.Checksum() == cs {
return ch, true
}
}
return nil, false
}
// ByIndex returns an entity from the list by index
// if requested chunk is within range of list, it returns chunk and true
// if requested chunk is out of the range, it returns nil and false
// boolean return value indicates whether requested chunk is within range
func (cl ChunkList) ByIndex(i uint64) (*Chunk, bool) {
if i >= uint64(len(cl)) {
// index out of range
return nil, false
}
return cl[i], true
}
// Len returns the number of Chunks in the list. It is also part of the sort
// interface that makes ChunkList sortable
func (cl ChunkList) Len() int {
return len(cl)
}
// Less returns true if element i in the ChunkList is less than j based on its chunk ID.
// Otherwise it returns true.
// It satisfies the sort.Interface making the ChunkList sortable.
func (cl ChunkList) Less(i, j int) bool {
return cl[i].ID().String() < cl[j].ID().String()
}
// Swap swaps the element i and j in the ChunkList.
// It satisfies the sort.Interface making the ChunkList sortable.
func (cl ChunkList) Swap(i, j int) {
cl[j], cl[i] = cl[i], cl[j]
}