-
Notifications
You must be signed in to change notification settings - Fork 95
/
commitment_roots.go
84 lines (72 loc) · 3.04 KB
/
commitment_roots.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
package iotago
import (
"crypto"
"fmt"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/iota.go/v4/merklehasher"
)
type Roots struct {
TangleRoot Identifier `serix:""`
StateMutationRoot Identifier `serix:""`
StateRoot Identifier `serix:""`
AccountRoot Identifier `serix:""`
AttestationsRoot Identifier `serix:""`
CommitteeRoot Identifier `serix:""`
RewardsRoot Identifier `serix:""`
ProtocolParametersHash Identifier `serix:""`
}
func NewRoots(tangleRoot, stateMutationRoot, attestationsRoot, stateRoot, accountRoot, committeeRoot, rewardsRoot, protocolParametersHash Identifier) *Roots {
return &Roots{
TangleRoot: tangleRoot,
StateMutationRoot: stateMutationRoot,
StateRoot: stateRoot,
AccountRoot: accountRoot,
AttestationsRoot: attestationsRoot,
CommitteeRoot: committeeRoot,
RewardsRoot: rewardsRoot,
ProtocolParametersHash: protocolParametersHash,
}
}
func (r *Roots) values() []Identifier {
return []Identifier{
r.TangleRoot,
r.StateMutationRoot,
r.StateRoot,
r.AccountRoot,
r.AttestationsRoot,
r.CommitteeRoot,
r.RewardsRoot,
r.ProtocolParametersHash,
}
}
func (r *Roots) ID() (id Identifier) {
// We can ignore the error because Identifier.Bytes() will never return an error
return Identifier(
lo.PanicOnErr(
merklehasher.NewHasher[Identifier](crypto.BLAKE2b_256).HashValues(r.values()),
),
)
}
func (r *Roots) AttestationsProof() *merklehasher.Proof[Identifier] {
// We can ignore the error because Identifier.Bytes() will never return an error
return lo.PanicOnErr(merklehasher.NewHasher[Identifier](crypto.BLAKE2b_256).ComputeProofForIndex(r.values(), 4))
}
func (r *Roots) TangleProof() *merklehasher.Proof[Identifier] {
// We can ignore the error because Identifier.Bytes() will never return an error
return lo.PanicOnErr(merklehasher.NewHasher[Identifier](crypto.BLAKE2b_256).ComputeProofForIndex(r.values(), 0))
}
func (r *Roots) MutationProof() *merklehasher.Proof[Identifier] {
// We can ignore the error because Identifier.Bytes() will never return an error
return lo.PanicOnErr(merklehasher.NewHasher[Identifier](crypto.BLAKE2b_256).ComputeProofForIndex(r.values(), 1))
}
func VerifyProof(proof *merklehasher.Proof[Identifier], proofedRoot Identifier, treeRoot Identifier) bool {
// We can ignore the error because Identifier.Bytes() will never return an error
if !lo.PanicOnErr(proof.ContainsValue(proofedRoot, merklehasher.NewHasher[Identifier](crypto.BLAKE2b_256))) {
return false
}
return treeRoot == Identifier(proof.Hash(merklehasher.NewHasher[Identifier](crypto.BLAKE2b_256)))
}
func (r *Roots) String() string {
return fmt.Sprintf(
"Roots(%s): TangleRoot: %s, StateMutationRoot: %s, StateRoot: %s, AccountRoot: %s, AttestationsRoot: %s, CommitteeRoot: %s, RewardsRoot: %s, ProtocolParametersHash: %s", r.ID(), r.TangleRoot, r.StateMutationRoot, r.StateRoot, r.AccountRoot, r.AttestationsRoot, r.CommitteeRoot, r.RewardsRoot, r.ProtocolParametersHash)
}