-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathincorporated_result.go
90 lines (76 loc) · 3.02 KB
/
incorporated_result.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
package flow
import (
"github.com/onflow/flow-go/crypto"
)
// IncorporatedResult is a wrapper around an ExecutionResult which contains the
// ID of the first block on its fork in which it was incorporated.
type IncorporatedResult struct {
// IncorporatedBlockID is the ID of the first block on its fork where a
// receipt for this result was incorporated. Within a fork, multiple blocks
// may contain receipts for the same result; only the first one is used to
// compute the random beacon of the result's chunk assignment.
IncorporatedBlockID Identifier
// Result is the ExecutionResult contained in the ExecutionReceipt that was
// incorporated in the payload of IncorporatedBlockID.
Result *ExecutionResult
// aggregatedSignature is a placeholder for attestation signatures collected
// for each chunk. It gets populated by the consensus matching engine when
// approvals are matched to execution results.
// This field is not exported (name doesn't start with a capital letter), so
// it is not used in calculating the ID and Checksum of the Incorporated
// Result (RLP encoding ignores private fields).
aggregatedSignatures map[uint64]*AggregatedSignature
}
func NewIncorporatedResult(incorporatedBlockID Identifier, result *ExecutionResult) *IncorporatedResult {
return &IncorporatedResult{
IncorporatedBlockID: incorporatedBlockID,
Result: result,
aggregatedSignatures: make(map[uint64]*AggregatedSignature),
}
}
// ID implements flow.Entity.ID for IncorporatedResult to make it capable of
// being stored directly in mempools and storage.
func (ir *IncorporatedResult) ID() Identifier {
body := struct {
IncorporatedBlockID Identifier
Result *ExecutionResult
}{
IncorporatedBlockID: ir.IncorporatedBlockID,
Result: ir.Result,
}
return MakeID(body)
}
// CheckSum implements flow.Entity.CheckSum for IncorporatedResult to make it
// capable of being stored directly in mempools and storage.
func (ir *IncorporatedResult) Checksum() Identifier {
return MakeID(ir)
}
// GetSignature returns a signature by chunk index and signer ID.
func (ir *IncorporatedResult) GetSignature(chunkIndex uint64, signerID Identifier) (*crypto.Signature, bool) {
as, ok := ir.aggregatedSignatures[chunkIndex]
if !ok {
return nil, false
}
return as.BySigner(signerID)
}
// AddSignature adds a signature to the collection of AggregatedSignatures
func (ir *IncorporatedResult) AddSignature(chunkIndex uint64, signerID Identifier, signature crypto.Signature) {
as, ok := ir.aggregatedSignatures[chunkIndex]
if !ok {
as = &AggregatedSignature{}
}
as.Add(signerID, signature)
ir.aggregatedSignatures[chunkIndex] = as
}
// GetAggregatedSignatures returns all the aggregated signatures orderd by chunk
// index
func (ir *IncorporatedResult) GetAggregatedSignatures() []AggregatedSignature {
result := make([]AggregatedSignature, 0, len(ir.Result.Chunks))
for _, chunk := range ir.Result.Chunks {
as, ok := ir.aggregatedSignatures[chunk.Index]
if ok {
result = append(result, *as)
}
}
return result
}