-
Notifications
You must be signed in to change notification settings - Fork 19
/
replay.go
198 lines (168 loc) · 5.7 KB
/
replay.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package nullchain
import (
"bufio"
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strconv"
"time"
"code.vegaprotocol.io/vega/core/blockchain"
vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/logging"
abci "github.com/cometbft/cometbft/abci/types"
)
var ErrReplayFileIsRequired = errors.New("replay-file is required when replay/record is enabled")
type blockData struct {
Height int64 `json:"height"`
Time int64 `json:"time"`
Txs [][]byte `json:"txns"`
AppHash []byte `json:"appHash"`
}
type Replayer struct {
log *logging.Logger
app ApplicationService
rFile *os.File
current *blockData
stop chan struct{}
}
func NewNullChainReplayer(app ApplicationService, cfg blockchain.ReplayConfig, log *logging.Logger) (*Replayer, error) {
if cfg.ReplayFile == "" {
return nil, ErrReplayFileIsRequired
}
flags := os.O_RDWR | os.O_CREATE
if !cfg.Replay {
// not replaying so make sure the file is empty before we start recording
flags |= os.O_TRUNC
}
f, err := os.OpenFile(cfg.ReplayFile, flags, 0o600)
if err != nil {
return nil, fmt.Errorf("failed to open replay file %s: %w", cfg.ReplayFile, err)
}
return &Replayer{
app: app,
rFile: f,
log: log,
stop: make(chan struct{}, 1),
}, nil
}
func (r *Replayer) InitChain(req abci.RequestInitChain) (*abci.ResponseInitChain, error) {
return r.app.InitChain(context.Background(), &req)
}
func (r *Replayer) Stop() error {
r.stop <- struct{}{}
close(r.stop)
return r.rFile.Close()
}
// startBlock saves in memory all the transactions in the block, we do not write until saveBlock us called
// with a potential appHash.
func (r *Replayer) startBlock(height, now int64, txs [][]byte) {
r.current = &blockData{
Height: height,
Time: now,
}
r.current.Txs = append(r.current.Txs, txs...)
}
// saveBlock writes to the replay file the details of the current block adding the appHash to it.
// If a panic occurred appHash may be empty.
func (r *Replayer) saveBlock(appHash []byte) {
r.current.AppHash = appHash
if err := r.write(); err != nil {
r.log.Panic("unable to write block to file", logging.Int64("block-height", r.current.Height))
}
r.current = nil
}
func readLine(r *bufio.Reader) ([]byte, error) {
line := []byte{}
for {
l, more, err := r.ReadLine()
if err != nil {
return nil, err
}
line = append(line, l...)
if !more {
return line, nil
}
}
}
// replayChain sends all the recorded per-block transactions into the protocol returning the block-height and block-time it reached
// appHeight is the block-height the application will process next, any blocks less than this will not be replayed.
func (r *Replayer) replayChain(appHeight int64) (int64, time.Time, error) {
var replayedHeight int64
var replayedTime time.Time
s := bufio.NewReader(r.rFile)
for {
line, err := readLine(s)
if err == io.EOF {
break
}
if err != nil {
return replayedHeight, replayedTime, err
}
select {
case <-r.stop:
r.log.Info("core is shutting down, nullchain replaying stopped", logging.Int64("block-height", replayedHeight))
return replayedHeight, replayedTime, nil
default:
}
var data blockData
if err := json.Unmarshal(line, &data); err != nil {
return replayedHeight, replayedTime, err
}
replayedHeight = data.Height
replayedTime = time.Unix(0, data.Time)
if data.Height < appHeight {
// skip because we've loaded from a snapshot at a block higher than this
continue
}
r.log.Info("replaying block", logging.Int64("height", data.Height), logging.Int("ntxns", len(data.Txs)))
resp, _ := r.app.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{
Height: data.Height,
Time: time.Unix(0, data.Time),
Hash: vgcrypto.Hash([]byte(strconv.FormatInt(data.Height+data.Time, 10))),
Txs: data.Txs,
})
r.app.Commit(context.Background(), &abci.RequestCommit{})
if len(data.AppHash) == 0 {
// we've replayed a block which when recorded must have panicked so we do not have a apphash
// somehow we've made it through this time, maybe someone is testing a fix so we skip the hash check and log it as strange
r.log.Error("app-hash missing from block data -- a block with a panic is working now?")
continue
}
if !bytes.Equal(data.AppHash, resp.AppHash) {
return replayedHeight, replayedTime, fmt.Errorf("appHash mismatch on replay, expected %s got %s", hex.EncodeToString(data.AppHash), hex.EncodeToString(resp.AppHash))
}
}
if replayedHeight < appHeight-1 {
return replayedHeight, replayedTime, fmt.Errorf("replay data missing, replay store up to height %d, but app-height is %d", replayedHeight, appHeight)
}
return replayedHeight, replayedTime, nil
}
func (r *Replayer) write() error {
b, err := json.Marshal(r.current)
if err != nil {
return fmt.Errorf("unable to record block %d: %w", r.current.Height, err)
}
// write each marshalled json block on a new line, its crude, but lets worry about perf if perf becomes a problem.
r.rFile.Write(b)
r.rFile.Write([]byte("\n"))
return nil
}