-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprotocol_transcript_test.go
More file actions
131 lines (116 loc) · 3.48 KB
/
Copy pathprotocol_transcript_test.go
File metadata and controls
131 lines (116 loc) · 3.48 KB
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
package main
import (
"fmt"
"sync"
"time"
)
type entropyRead struct {
Length int `json:"length"`
Bytes []byte `json:"bytes"`
}
type protocolTranscript struct {
UnixMilli []int64 `json:"unix_milli"`
EntropyReads []entropyRead `json:"entropy_reads"`
}
type transcriptRecorder struct {
mu sync.Mutex
deps protocolHostDeps
transcript protocolTranscript
}
func newTranscriptRecorder(deps protocolHostDeps) *transcriptRecorder {
return &transcriptRecorder{deps: deps}
}
func (r *transcriptRecorder) Now() (time.Time, error) {
r.mu.Lock()
defer r.mu.Unlock()
now, err := r.deps.Clock.Now()
if err != nil {
return time.Time{}, err
}
r.transcript.UnixMilli = append(r.transcript.UnixMilli, now.UnixMilli())
return now, nil
}
func (r *transcriptRecorder) Read(dst []byte) error {
r.mu.Lock()
defer r.mu.Unlock()
if err := r.deps.Entropy.Read(dst); err != nil {
return err
}
r.transcript.EntropyReads = append(r.transcript.EntropyReads, entropyRead{
Length: len(dst),
Bytes: append([]byte(nil), dst...),
})
return nil
}
func (r *transcriptRecorder) Transcript() protocolTranscript {
r.mu.Lock()
defer r.mu.Unlock()
return cloneProtocolTranscript(r.transcript)
}
type transcriptReplay struct {
mu sync.Mutex
transcript protocolTranscript
timeCursor int
entropyCursor int
}
func newTranscriptReplay(transcript protocolTranscript) *transcriptReplay {
return &transcriptReplay{transcript: cloneProtocolTranscript(transcript)}
}
func (r *transcriptReplay) Now() (time.Time, error) {
r.mu.Lock()
defer r.mu.Unlock()
if r.timeCursor >= len(r.transcript.UnixMilli) {
return time.Time{}, transcriptReplayError(fmt.Errorf("clock transcript exhausted at read %d", r.timeCursor))
}
unixMilli := r.transcript.UnixMilli[r.timeCursor]
r.timeCursor++
return time.UnixMilli(unixMilli), nil
}
func (r *transcriptReplay) Read(dst []byte) error {
r.mu.Lock()
defer r.mu.Unlock()
if r.entropyCursor >= len(r.transcript.EntropyReads) {
return transcriptReplayError(fmt.Errorf("entropy transcript exhausted at read %d", r.entropyCursor))
}
readIndex := r.entropyCursor
read := r.transcript.EntropyReads[readIndex]
r.entropyCursor++
if read.Length != len(dst) {
return transcriptReplayError(fmt.Errorf("entropy read %d length = %d, want %d", readIndex, read.Length, len(dst)))
}
if len(read.Bytes) != read.Length {
return transcriptReplayError(fmt.Errorf("entropy read %d byte count = %d, want %d", readIndex, len(read.Bytes), read.Length))
}
copy(dst, read.Bytes)
return nil
}
func (r *transcriptReplay) Exhausted() error {
r.mu.Lock()
defer r.mu.Unlock()
if r.timeCursor != len(r.transcript.UnixMilli) || r.entropyCursor != len(r.transcript.EntropyReads) {
return transcriptReplayError(fmt.Errorf(
"unconsumed transcript: clock %d/%d, entropy %d/%d",
r.timeCursor,
len(r.transcript.UnixMilli),
r.entropyCursor,
len(r.transcript.EntropyReads),
))
}
return nil
}
func cloneProtocolTranscript(transcript protocolTranscript) protocolTranscript {
clone := protocolTranscript{
UnixMilli: append([]int64(nil), transcript.UnixMilli...),
EntropyReads: make([]entropyRead, len(transcript.EntropyReads)),
}
for i, read := range transcript.EntropyReads {
clone.EntropyReads[i] = entropyRead{
Length: read.Length,
Bytes: append([]byte(nil), read.Bytes...),
}
}
return clone
}
func transcriptReplayError(internal error) error {
return newProtocolError(protocolBackendIncompatible, "protocol backend is incompatible", internal)
}