forked from celestiaorg/go-fraud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproof.go
81 lines (67 loc) · 2.13 KB
/
proof.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
package fraud
import (
"context"
"encoding"
"fmt"
"github.com/celestiaorg/go-header"
)
// ProofType type defines a unique proof type string.
type ProofType string
// String returns string representation of ProofType.
func (pt ProofType) String() string {
return string(pt)
}
// Proof is a generic interface that will be used for all types of fraud proofs in the network.
type Proof interface {
// Type returns the exact type of fraud proof.
Type() ProofType
// HeaderHash returns the block hash.
HeaderHash() []byte
// Height returns the block height corresponding to the Proof.
Height() uint64
// Validate check the validity of fraud proof.
// Validate throws an error if some conditions don't pass and thus fraud proof is not valid.
// NOTE: header.ExtendedHeader should pass basic validation otherwise it will panic if it's
// malformed.
Validate(header.Header) error
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}
// OnProof subscribes to the given Fraud Proof topic via the given Subscriber.
// In case a Fraud Proof is received, then the given handle function will be invoked.
func OnProof(ctx context.Context, subscriber Subscriber, p ProofType, handle func(proof Proof)) {
subscription, err := subscriber.Subscribe(p)
if err != nil {
return
}
defer subscription.Cancel()
// At this point we receive already verified fraud proof,
// so there is no need to call Validate.
proof, err := subscription.Proof(ctx)
if err != nil {
return
}
handle(proof)
}
// Unmarshal converts raw bytes into respective Proof type.
func Unmarshal(proofType ProofType, msg []byte) (Proof, error) {
unmarshalersLk.RLock()
defer unmarshalersLk.RUnlock()
unmarshaler, ok := defaultUnmarshalers[proofType]
if !ok {
return nil, &ErrNoUnmarshaler{proofType: proofType}
}
return unmarshaler(msg)
}
type ErrFraudExists struct {
Proof []Proof
}
func (e *ErrFraudExists) Error() string {
return fmt.Sprintf("fraud: %s proof exists\n", e.Proof[0].Type())
}
type ErrNoUnmarshaler struct {
proofType ProofType
}
func (e *ErrNoUnmarshaler) Error() string {
return fmt.Sprintf("fraud: unmarshaler for %s type is not registered", e.proofType)
}