forked from celestiaorg/go-fraud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
45 lines (40 loc) · 1.32 KB
/
registry.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
package fraud
import (
"fmt"
"reflect"
"sync"
)
var (
unmarshalersLk = sync.RWMutex{}
defaultUnmarshalers = map[ProofType]ProofUnmarshaler{}
)
// Register adds a string representation and unmarshaller for the provided ProofType.
func Register(p Proof) {
unmarshalersLk.Lock()
defer unmarshalersLk.Unlock()
if _, ok := defaultUnmarshalers[p.Type()]; ok {
panic(fmt.Sprintf("fraud: unmarshaler for %s proof is registered", p.Type()))
}
defaultUnmarshalers[p.Type()] = func(data []byte) (Proof, error) {
// the underlying type of `p` is a pointer to a struct and assigning `p` to a new variable is not
// the case, because it could lead to data races.
// So, there is no easier way to create a hard copy of Proof other than using a reflection.
proof := reflect.New(reflect.ValueOf(p).Elem().Type()).Interface().(Proof)
err := proof.UnmarshalBinary(data)
return proof, err
}
}
// Registered reports a set of registered proof types by Register.
func Registered() []ProofType {
return registeredProofTypes()
}
// registeredProofTypes returns all available proofTypes.
func registeredProofTypes() []ProofType {
unmarshalersLk.Lock()
defer unmarshalersLk.Unlock()
proofs := make([]ProofType, 0, len(defaultUnmarshalers))
for proof := range defaultUnmarshalers {
proofs = append(proofs, proof)
}
return proofs
}