forked from emrhtas/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproof.go
91 lines (75 loc) · 2.29 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
82
83
84
85
86
87
88
89
90
91
package fraud
import (
"context"
"encoding"
"fmt"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric/global"
"github.com/celestiaorg/celestia-node/header"
)
var (
meter = global.MeterProvider().Meter("fraud")
tracer = otel.Tracer("fraud")
)
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)
}
type ProofType string
const (
BadEncoding ProofType = "badencoding"
)
// 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.ExtendedHeader) 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 {
log.Error(err)
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 {
if err != context.Canceled {
log.Errorw("reading next proof failed", "err", err)
}
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)
}