-
Notifications
You must be signed in to change notification settings - Fork 107
/
drand_daemon_helper.go
78 lines (65 loc) 路 2.58 KB
/
drand_daemon_helper.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
package core
import (
"fmt"
"github.com/drand/drand/v2/common"
"github.com/drand/drand/v2/protobuf/drand"
)
func (dd *DrandDaemon) readBeaconID(metadata *drand.Metadata) (string, error) {
rcvBeaconID := metadata.GetBeaconID()
if chainHashBytes := metadata.GetChainHash(); len(chainHashBytes) != 0 {
chainHash := fmt.Sprintf("%x", chainHashBytes)
dd.state.RLock()
defer dd.state.RUnlock()
beaconIDByHash, isChainHashFound := dd.chainHashes[chainHash]
if isChainHashFound {
// check if rcv beacon id on request points to a different id obtained from chain hash
// we accept the empty beacon id, since we do a match on the chain hash in that case
if rcvBeaconID != "" && !common.CompareBeaconIDs(rcvBeaconID, beaconIDByHash) {
return "", fmt.Errorf("invalid chain hash: %q != %q", rcvBeaconID, beaconIDByHash)
}
rcvBeaconID = beaconIDByHash
} else {
// for the case where our node is still waiting for the chain hash to be set
rcvBeaconID = common.GetCanonicalBeaconID(rcvBeaconID)
for id, bp := range dd.beaconProcesses {
bp.state.RLock()
group := bp.group
bp.state.RUnlock()
// we only accept to proceed with an unknown chain hash if one beacon process hasn't run DKG yet
if id == rcvBeaconID && group == nil {
// we make sure that the beacon id is not empty, note the metadata is necessarily instantiated
// if the outer IF close is true
metadata.BeaconID = rcvBeaconID
return id, nil
}
}
// if no beacon process is not yet initialized entirely, this is an error
return "", fmt.Errorf("unknown chain hash: %s out of %v", chainHash, dd.chainHashes)
}
}
// if we didn't match on a chain hash, and have the empty string, then it's the default beacon
rcvBeaconID = common.GetCanonicalBeaconID(rcvBeaconID)
// make sure the metadata use a correct beacon id, we might need to instantiate it first
if metadata == nil {
metadata = &drand.Metadata{}
}
// we explicitly set the beacon id on the metadata in case it changed because of GetCanonicalBeaconID
metadata.BeaconID = rcvBeaconID
return rcvBeaconID, nil
}
func (dd *DrandDaemon) getBeaconProcessByID(beaconID string) (*BeaconProcess, error) {
dd.state.Lock()
bp, isBeaconIDFound := dd.beaconProcesses[beaconID]
dd.state.Unlock()
if isBeaconIDFound {
return bp, nil
}
return nil, fmt.Errorf("beacon id [%s] is not running", beaconID)
}
func (dd *DrandDaemon) getBeaconProcessFromRequest(metadata *drand.Metadata) (*BeaconProcess, error) {
beaconID, err := dd.readBeaconID(metadata)
if err != nil {
return nil, err
}
return dd.getBeaconProcessByID(beaconID)
}