-
Notifications
You must be signed in to change notification settings - Fork 212
/
poet.go
177 lines (149 loc) · 4.82 KB
/
poet.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package types
import (
"encoding/hex"
"fmt"
"time"
"github.com/spacemeshos/go-scale"
poetShared "github.com/spacemeshos/poet/shared"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/log"
)
//go:generate scalegen -types PoetChallenge,PoetProof,PoetProofMessage,PoetRound,ProcessingError
type PoetServer struct {
Address string `mapstructure:"address" json:"address"`
Pubkey Base64Enc `mapstructure:"pubkey" json:"pubkey"`
}
type PoetChallenge struct {
*NIPostChallenge
InitialPost *Post
InitialPostMetadata *PostMetadata
NumUnits uint32
}
func (c *PoetChallenge) MarshalLogObject(encoder log.ObjectEncoder) error {
if c == nil {
return nil
}
if err := encoder.AddObject("NIPostChallenge", c.NIPostChallenge); err != nil {
return err
}
if err := encoder.AddObject("InitialPost", c.InitialPost); err != nil {
return err
}
if err := encoder.AddObject("InitialPostMetadata", c.InitialPostMetadata); err != nil {
return err
}
encoder.AddUint32("NumUnits", c.NumUnits)
return nil
}
type Member [32]byte
// EncodeScale implements scale codec interface.
func (m *Member) EncodeScale(e *scale.Encoder) (int, error) {
return scale.EncodeByteArray(e, m[:])
}
// DecodeScale implements scale codec interface.
func (m *Member) DecodeScale(d *scale.Decoder) (int, error) {
return scale.DecodeByteArray(d, m[:])
}
type PoetProofRef [32]byte
// EmptyPoetProofRef is an empty PoET proof reference.
var EmptyPoetProofRef = PoetProofRef{}
// PoetProof is the full PoET service proof of elapsed time.
// It includes the number of leaves produced and the actual PoET Merkle proof.
type PoetProof struct {
poetShared.MerkleProof
LeafCount uint64
}
func (p *PoetProof) MarshalLogObject(encoder log.ObjectEncoder) error {
if p == nil {
return nil
}
encoder.AddUint64("LeafCount", p.LeafCount)
encoder.AddString("MerkleProof.Root", hex.EncodeToString(p.Root))
encoder.AddArray("MerkleProof.ProvenLeaves", log.ArrayMarshalerFunc(func(encoder log.ArrayEncoder) error {
for _, v := range p.ProvenLeaves {
encoder.AppendString(hex.EncodeToString(v))
}
return nil
}))
encoder.AddArray("MerkleProof.ProofNodes", log.ArrayMarshalerFunc(func(encoder log.ArrayEncoder) error {
for _, v := range p.ProofNodes {
encoder.AppendString(hex.EncodeToString(v))
}
return nil
}))
return nil
}
// PoetProofMessage is the envelope which includes the PoetProof, service ID, round ID and signature.
type PoetProofMessage struct {
PoetProof
PoetServiceID []byte `scale:"max=32"` // public key of the PoET service
RoundID string `scale:"max=32"` // TODO(mafa): convert to uint64
// The input to Poet's POSW.
// It's the root of a merkle tree built from all of the members
// that are included in the proof.
Statement Hash32
Signature EdSignature
}
func (p *PoetProofMessage) MarshalLogObject(encoder log.ObjectEncoder) error {
if p == nil {
return nil
}
encoder.AddObject("PoetProof", &p.PoetProof)
encoder.AddString("PoetServiceID", hex.EncodeToString(p.PoetServiceID))
encoder.AddString("RoundID", p.RoundID)
encoder.AddString("Statement", hex.EncodeToString(p.Statement[:]))
encoder.AddString("Signature", p.Signature.String())
return nil
}
// Ref returns the reference to the PoET proof message. It's the blake3 sum of the entire proof message.
func (p *PoetProofMessage) Ref() (PoetProofRef, error) {
poetProofBytes, err := codec.Encode(&p.PoetProof)
if err != nil {
return PoetProofRef{}, fmt.Errorf("failed to marshal poet proof for poetId %x round %v: %w",
p.PoetServiceID, p.RoundID, err)
}
h := CalcHash32(poetProofBytes)
return (PoetProofRef)(h), nil
}
type RoundEnd time.Time
func (re RoundEnd) Equal(other RoundEnd) bool {
return (time.Time)(re).Equal((time.Time)(other))
}
func (re *RoundEnd) IntoTime() time.Time {
return (time.Time)(*re)
}
func (p *RoundEnd) EncodeScale(enc *scale.Encoder) (total int, err error) {
t := p.IntoTime()
n, err := scale.EncodeString(enc, t.Format(time.RFC3339Nano))
if err != nil {
return 0, err
}
return n, nil
}
// DecodeScale implements scale codec interface.
func (p *RoundEnd) DecodeScale(dec *scale.Decoder) (total int, err error) {
field, n, err := scale.DecodeString(dec)
if err != nil {
return 0, err
}
t, err := time.Parse(time.RFC3339Nano, field)
if err != nil {
return n, err
}
*p = (RoundEnd)(t)
return n, nil
}
// PoetRound includes the PoET's round ID.
type PoetRound struct {
ID string `scale:"max=32"`
End RoundEnd
}
// ProcessingError is a type of error (implements the error interface) that is used to differentiate processing errors
// from validation errors.
type ProcessingError struct {
Err string `scale:"max=1024"` // TODO(mafa): make error code instead of string
}
// Error returns the processing error as a string. It implements the error interface.
func (s ProcessingError) Error() string {
return s.Err
}