|
| 1 | +// Copyright (C) 2019, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package cchain |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/ava-labs/avalanchego/database/memdb" |
| 13 | + "github.com/ava-labs/avalanchego/snow" |
| 14 | + "github.com/ava-labs/avalanchego/vms/saevm/blocks" |
| 15 | + "github.com/ava-labs/avalanchego/vms/saevm/cchain/tx/txtest" |
| 16 | +) |
| 17 | + |
| 18 | +func TestConsensusGettersAfterRestart(t *testing.T) { |
| 19 | + key := txtest.NewKey(t) |
| 20 | + alloc := withMaxAllocFor(key.EthAddress()) |
| 21 | + db := memdb.New() |
| 22 | + |
| 23 | + ctx, node := newSUT(t, alloc, withDB(db)) |
| 24 | + w := newWallet(key, node.ctx, node.Client) |
| 25 | + |
| 26 | + genesis, err := node.GetBlock(ctx, node.lastAccepted(ctx, t)) |
| 27 | + require.NoErrorf(t, err, "%T.GetBlock()", node.VM) |
| 28 | + const numBlocks = 3 |
| 29 | + want := make([]*blocks.Block, 0, numBlocks+1) |
| 30 | + want = append(want, genesis) |
| 31 | + for range numBlocks { |
| 32 | + blk := node.issueAndExecute(ctx, t, w.newMinimalTx(t)) |
| 33 | + want = append(want, blk) |
| 34 | + } |
| 35 | + require.NoErrorf(t, node.Shutdown(ctx), "%T.Shutdown()", node.VM) |
| 36 | + |
| 37 | + for _, state := range []snow.State{ |
| 38 | + snow.StateSyncing, |
| 39 | + snow.Bootstrapping, |
| 40 | + snow.NormalOp, |
| 41 | + } { |
| 42 | + t.Run(state.String(), func(t *testing.T) { |
| 43 | + ctx, s := newSUT(t, alloc, withDB(db), withState(state)) |
| 44 | + |
| 45 | + wantLastAccepted := want[len(want)-1].ID() |
| 46 | + gotLastAccepted, err := s.LastAccepted(ctx) |
| 47 | + require.NoErrorf(t, err, "%T.LastAccepted()", s) |
| 48 | + assert.Equalf(t, wantLastAccepted, gotLastAccepted, "%T.LastAccepted()", s) |
| 49 | + |
| 50 | + for _, b := range want { |
| 51 | + gotID, err := s.GetBlockIDAtHeight(ctx, b.Height()) |
| 52 | + require.NoErrorf(t, err, "%T.GetBlockIDAtHeight(%d)", s, b.Height()) |
| 53 | + assert.Equalf(t, b.ID(), gotID, "%T.GetBlockIDAtHeight(%d)", s, b.Height()) |
| 54 | + |
| 55 | + gotBlock, err := s.GetBlock(ctx, b.ID()) |
| 56 | + require.NoErrorf(t, err, "%T.GetBlock(%s)", s, b.ID()) |
| 57 | + assert.Equalf(t, b.ID(), gotBlock.ID(), "%T.GetBlock(%s).ID()", s, b.ID()) |
| 58 | + assert.Equalf(t, b.Height(), gotBlock.Height(), "%T.GetBlock(%s).Height()", s, b.ID()) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestGetGenesisNoState(t *testing.T) { |
| 65 | + ctx, sut := newSUT(t, withState(snow.StateSyncing)) |
| 66 | + hash, err := sut.LastAccepted(ctx) |
| 67 | + require.NoErrorf(t, err, "%T.LastAccepted()", sut) |
| 68 | + require.NotZero(t, hash) |
| 69 | + |
| 70 | + gotID, err := sut.GetBlockIDAtHeight(ctx, 0) |
| 71 | + require.NoErrorf(t, err, "%T.GetBlockIDAtHeight(%d)", sut, 0) |
| 72 | + assert.Equalf(t, hash, gotID, "%T.GetBlockIDAtHeight(%d)", sut, 0) |
| 73 | + |
| 74 | + gotBlock, err := sut.GetBlock(ctx, hash) |
| 75 | + require.NoErrorf(t, err, "%T.GetBlock(%s)", sut, hash) |
| 76 | + assert.Equalf(t, hash, gotBlock.ID(), "%T.GetBlock(%s).ID()", sut, hash) |
| 77 | + assert.Equalf(t, uint64(0), gotBlock.Height(), "%T.GetBlock(%s).Height()", sut, hash) |
| 78 | +} |
0 commit comments