forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate.go
74 lines (59 loc) · 2.3 KB
/
certificate.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
package p2pcommon
import (
"errors"
"github.com/aergoio/aergo/types"
"github.com/btcsuite/btcd/btcec"
"time"
)
var (
ErrInvalidCertVersion = errors.New("invalid certificate version")
ErrInvalidRole = errors.New("invalid peer role") // receiver is not bp or requester is not registered agent
ErrInvalidKey = errors.New("invalid key in certificate ")
ErrInvalidPeerID = errors.New("invalid peer id in certificate ")
ErrVerificationFailed = errors.New("signature verification failed")
ErrMalformedCert = errors.New("malformed certificate data")
ErrInvalidCertField = errors.New("invalid field in certificate ")
)
const (
CertVersion0001 uint32 = 0x01
)
const (
timeErrorTolerance = time.Minute
)
// AgentCertificateV1 is a certificate issued by a block producer to guarantee that it is a trustworthy agent.
type AgentCertificateV1 struct {
Version uint32
BPID types.PeerID
BPPubKey *btcec.PublicKey
CreateTime time.Time
ExpireTime time.Time
AgentID types.PeerID
AgentAddress []string
Signature *btcec.Signature
}
func (c *AgentCertificateV1) IsValidInTime(t time.Time, errTolerance time.Duration) bool {
// TODO consider the case is time error between peers
return (c.CreateTime.Sub(t) < errTolerance ) && t.Before(c.ExpireTime)
}
func (c *AgentCertificateV1) IsNeedUpdate(t time.Time, bufTerm time.Duration) bool {
// TODO consider the case is time error between peers
return c.ExpireTime.Sub(t) < bufTerm
}
// CertificateManager manages local peer's certificates and related information
type CertificateManager interface {
PeerEventListener
Start()
Stop()
// methods for bp
// CreateCertificate create certificate for the agent. It will return ErrInvalidRole error if local peer is not block producer
CreateCertificate(remoteMeta PeerMeta) (*AgentCertificateV1, error)
// methods for agents
// GetProducers return list of peer id of which this agent is charge.
GetProducers() []types.PeerID
// GetCertificates returns my certificates
GetCertificates() []*AgentCertificateV1
// AddCertificate add to my certificate list
AddCertificate(cert *AgentCertificateV1)
CanHandle(bpID types.PeerID) bool
}
//go:generate sh -c "mockgen github.com/aergoio/aergo/p2p/p2pcommon CertificateManager | sed -e 's/^package mock_p2pcommon/package p2pmock/g' > ../p2pmock/mock_certificate.go"