aboutsummaryrefslogtreecommitdiffstats
path: root/core/test/app.go
blob: 0a17f13ae4a69fc3f5724f75fe2996c12ee04c71 (plain) (blame)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus library 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 Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus library. If not, see
// <http://www.gnu.org/licenses/>.

package test

import (
    "fmt"
    "sync"
    "time"

    "github.com/dexon-foundation/dexon-consensus/common"
    "github.com/dexon-foundation/dexon-consensus/core/types"
    "github.com/dexon-foundation/dexon-consensus/core/utils"
)

var (
    // ErrEmptyDeliverSequence means there is no delivery event in this App
    // instance.
    ErrEmptyDeliverSequence = fmt.Errorf("empty deliver sequence")
    // ErrMismatchBlockHashSequence means the delivering sequence between two App
    // instances are different.
    ErrMismatchBlockHashSequence = fmt.Errorf("mismatch block hash sequence")
    // ErrMismatchConsensusTime means the consensus timestamp between two blocks
    // with the same hash from two App instances are different.
    ErrMismatchConsensusTime = fmt.Errorf("mismatch consensus time")
    // ErrApplicationIntegrityFailed means the internal datum in a App instance
    // is not integrated.
    ErrApplicationIntegrityFailed = fmt.Errorf("application integrity failed")
    // ErrConsensusTimestampOutOfOrder means the later delivered block has
    // consensus timestamp older than previous block.
    ErrConsensusTimestampOutOfOrder = fmt.Errorf(
        "consensus timestamp out of order")
    // ErrConsensusHeightOutOfOrder means the later delivered block has
    // consensus height not equal to height of previous block plus one.
    ErrConsensusHeightOutOfOrder = fmt.Errorf(
        "consensus height out of order")
    // ErrDeliveredBlockNotConfirmed means some block delivered (confirmed) but
    // not confirmed.
    ErrDeliveredBlockNotConfirmed = fmt.Errorf("delivered block not confirmed")
    // ErrAckingBlockNotDelivered means the delivered sequence not forming a
    // DAG.
    ErrAckingBlockNotDelivered = fmt.Errorf("acking block not delivered")
    // ErrLowerPendingHeight raised when lastPendingHeight is lower than the
    // height to be prepared.
    ErrLowerPendingHeight = fmt.Errorf(
        "last pending height < consensus height")
    // ErrConfirmedHeightNotIncreasing raise when the height of the confirmed
    // block doesn't follow previous confirmed block on the same chain.
    ErrConfirmedHeightNotIncreasing = fmt.Errorf(
        "confirmed height not increasing")
    // ErrParentBlockNotDelivered raised when the parent block is not seen by
    // this app.
    ErrParentBlockNotDelivered = fmt.Errorf("parent block not delivered")
)

// AppDeliveredRecord caches information when this application received
// a block delivered notification.
type AppDeliveredRecord struct {
    Result types.FinalizationResult
    When   time.Time
    Pos    types.Position
}

// App implements Application interface for testing purpose.
type App struct {
    Confirmed             map[common.Hash]*types.Block
    LastConfirmedHeight   uint64
    confirmedLock         sync.RWMutex
    Delivered             map[common.Hash]*AppDeliveredRecord
    DeliverSequence       common.Hashes
    deliveredLock         sync.RWMutex
    state                 *State
    gov                   *Governance
    rEvt                  *utils.RoundEvent
    hEvt                  *common.Event
    lastPendingHeightLock sync.RWMutex
    LastPendingHeight     uint64
    roundToNotify         uint64
}

// NewApp constructs a TestApp instance.
func NewApp(initRound uint64, gov *Governance, rEvt *utils.RoundEvent) (
    app *App) {
    app = &App{
        Confirmed:       make(map[common.Hash]*types.Block),
        Delivered:       make(map[common.Hash]*AppDeliveredRecord),
        DeliverSequence: common.Hashes{},
        gov:             gov,
        rEvt:            rEvt,
        hEvt:            common.NewEvent(),
        roundToNotify:   initRound,
    }
    if gov != nil {
        app.state = gov.State()
    }
    if rEvt != nil {
        app.hEvt.RegisterHeight(
            utils.GetNextRoundCheckpoint(rEvt.LastPeriod()), func(h uint64) {
                rEvt.ValidateNextRound(h)
            })
        rEvt.Register(func(evts []utils.RoundEventParam) {
            app.hEvt.RegisterHeight(evts[len(evts)-1].NextRoundCheckpoint(),
                func(h uint64) {
                    rEvt.ValidateNextRound(h)
                })
        })
    }
    return app
}

// PreparePayload implements Application interface.
func (app *App) PreparePayload(position types.Position) ([]byte, error) {
    if app.state == nil {
        return []byte{}, nil
    }
    return app.state.PackRequests()
}

// PrepareWitness implements Application interface.
func (app *App) PrepareWitness(height uint64) (types.Witness, error) {
    pendingHeight := app.getLastPendingWitnessHeight()
    if pendingHeight < height {
        return types.Witness{}, ErrLowerPendingHeight
    }
    if pendingHeight == 0 {
        return types.Witness{}, nil
    }
    hash := func() common.Hash {
        app.deliveredLock.RLock()
        defer app.deliveredLock.RUnlock()
        // Our witness height starts from 1.
        h := app.DeliverSequence[pendingHeight-1]
        // Double confirm if the delivered record matches the pending height.
        if app.Delivered[h].Result.Height != pendingHeight {
            app.confirmedLock.RLock()
            defer app.confirmedLock.RUnlock()
            panic(fmt.Errorf("unmatched finalization record: %s, %v, %v",
                app.Confirmed[h], pendingHeight,
                app.Delivered[h].Result.Height))
        }
        return h
    }()
    return types.Witness{
        Height: pendingHeight,
        Data:   hash.Bytes(),
    }, nil
}

// VerifyBlock implements Application interface.
func (app *App) VerifyBlock(block *types.Block) types.BlockVerifyStatus {
    // Make sure we can handle the witness carried by this block.
    pendingHeight := app.getLastPendingWitnessHeight()
    if pendingHeight < block.Witness.Height {
        return types.VerifyRetryLater
    }
    // Confirm if the consensus height matches corresponding block hash.
    var h common.Hash
    copy(h[:], block.Witness.Data)
    app.deliveredLock.RLock()
    defer app.deliveredLock.RUnlock()
    // This is the difference between test.App and fullnode, fullnode has the
    // genesis block at height=0, we don't. Thus our reasonable witness starts
    // from 1.
    if block.Witness.Height > 0 {
        if block.Witness.Height != app.Delivered[h].Result.Height {
            return types.VerifyInvalidBlock
        }
    }
    if block.Position.Height != 0 {
        // This check is copied from fullnode, below is quoted from coresponding
        // comment:
        //
        // Check if target block is the next height to be verified, we can only
        // verify the next block in a given chain.
        app.confirmedLock.RLock()
        defer app.confirmedLock.RUnlock()
        if app.LastConfirmedHeight+1 != block.Position.Height {
            return types.VerifyRetryLater
        }
    }
    return types.VerifyOK
}

// BlockConfirmed implements Application interface.
func (app *App) BlockConfirmed(b types.Block) {
    app.confirmedLock.Lock()
    defer app.confirmedLock.Unlock()
    app.Confirmed[b.Hash] = &b
    if b.Position.Height != 0 {
        if app.LastConfirmedHeight+1 != b.Position.Height {
            panic(ErrConfirmedHeightNotIncreasing)
        }
    }
    app.LastConfirmedHeight = b.Position.Height
}

// BlockDelivered implements Application interface.
func (app *App) BlockDelivered(blockHash common.Hash, pos types.Position,
    result types.FinalizationResult) {
    func() {
        app.deliveredLock.Lock()
        defer app.deliveredLock.Unlock()
        app.Delivered[blockHash] = &AppDeliveredRecord{
            Result: result,
            When:   time.Now().UTC(),
            Pos:    pos,
        }
        app.DeliverSequence = append(app.DeliverSequence, blockHash)
        // Make sure parent block also delivered.
        if !result.ParentHash.Equal(common.Hash{}) {
            d, exists := app.Delivered[result.ParentHash]
            if !exists {
                panic(ErrParentBlockNotDelivered)
            }
            if d.Result.Height+1 != result.Height {
                panic(ErrConsensusHeightOutOfOrder)
            }
        }
        app.lastPendingHeightLock.Lock()
        defer app.lastPendingHeightLock.Unlock()
        app.LastPendingHeight = result.Height
    }()
    // Apply packed state change requests in payload.
    func() {
        if app.state == nil {
            return
        }
        app.confirmedLock.RLock()
        defer app.confirmedLock.RUnlock()
        b := app.Confirmed[blockHash]
        if err := app.state.Apply(b.Payload); err != nil {
            if err != ErrDuplicatedChange {
                panic(err)
            }
        }
        if app.roundToNotify == pos.Round {
            if app.gov != nil {
                app.gov.NotifyRound(app.roundToNotify)
                app.roundToNotify++
            }
        }
    }()
    app.hEvt.NotifyHeight(result.Height)
}

// GetLatestDeliveredPosition would return the latest position of delivered
// block seen by this application instance.
func (app *App) GetLatestDeliveredPosition() types.Position {
    app.deliveredLock.RLock()
    defer app.deliveredLock.RUnlock()
    app.confirmedLock.RLock()
    defer app.confirmedLock.RUnlock()
    if len(app.DeliverSequence) == 0 {
        return types.Position{}
    }
    return app.Confirmed[app.DeliverSequence[len(app.DeliverSequence)-1]].Position
}

// Compare performs these checks against another App instance
// and return erros if not passed:
// - deliver sequence by comparing block hashes.
// - consensus timestamp of each block are equal.
func (app *App) Compare(other *App) (err error) {
    app.WithLock(func(app *App) {
        other.WithLock(func(other *App) {
            minLength := len(app.DeliverSequence)
            if minLength > len(other.DeliverSequence) {
                minLength = len(other.DeliverSequence)
            }
            if minLength == 0 {
                err = ErrEmptyDeliverSequence
                return
            }
            // Here we assumes both Apps begin from the same height.
            for idx, h := range app.DeliverSequence[:minLength] {
                hOther := other.DeliverSequence[idx]
                if hOther != h {
                    err = ErrMismatchBlockHashSequence
                    return
                }
                if app.Delivered[h].Result.Timestamp !=
                    other.Delivered[h].Result.Timestamp {
                    err = ErrMismatchConsensusTime
                    return
                }
            }
        })
    })
    return
}

// Verify checks the integrity of date received by this App instance.
func (app *App) Verify() error {
    // TODO(mission): verify blocks' position when delivered.
    app.confirmedLock.RLock()
    defer app.confirmedLock.RUnlock()
    app.deliveredLock.RLock()
    defer app.deliveredLock.RUnlock()

    if len(app.DeliverSequence) == 0 {
        return ErrEmptyDeliverSequence
    }
    if len(app.DeliverSequence) != len(app.Delivered) {
        return ErrApplicationIntegrityFailed
    }
    expectHeight := uint64(1)
    prevTime := time.Time{}
    for _, h := range app.DeliverSequence {
        _, exists := app.Confirmed[h]
        if !exists {
            return ErrDeliveredBlockNotConfirmed
        }
        rec, exists := app.Delivered[h]
        if !exists {
            return ErrApplicationIntegrityFailed
        }
        // Make sure the consensus time is incremental.
        ok := prevTime.Before(rec.Result.Timestamp) ||
            prevTime.Equal(rec.Result.Timestamp)
        if !ok {
            return ErrConsensusTimestampOutOfOrder
        }
        prevTime = rec.Result.Timestamp
        // Make sure the consensus height is incremental.
        if expectHeight != rec.Result.Height {
            return ErrConsensusHeightOutOfOrder
        }
        expectHeight++
    }
    return nil
}

// BlockReceived implements interface Debug.
func (app *App) BlockReceived(hash common.Hash) {}

// BlockReady implements interface Debug.
func (app *App) BlockReady(hash common.Hash) {}

// WithLock provides a backdoor to check status of App with reader lock.
func (app *App) WithLock(function func(*App)) {
    app.confirmedLock.RLock()
    defer app.confirmedLock.RUnlock()
    app.deliveredLock.RLock()
    defer app.deliveredLock.RUnlock()
    app.lastPendingHeightLock.RLock()
    defer app.lastPendingHeightLock.RUnlock()

    function(app)
}

func (app *App) getLastPendingWitnessHeight() uint64 {
    app.lastPendingHeightLock.RLock()
    defer app.lastPendingHeightLock.RUnlock()
    return app.LastPendingHeight
}