Skip to content

Commit

Permalink
add UT tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrie-yl authored and cryyl committed Apr 29, 2022
1 parent e9be0d4 commit 0132f13
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions core/blockchain_notries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ type requestRoot struct {
diffHash common.Hash
}

type verifFailedStatus struct {
status types.VerifyStatus
blockNumber uint64
}

// mockVerifyPeer is a mocking struct that simulates p2p signals for verification tasks.
type mockVerifyPeer struct {
callback func(*requestRoot)
Expand Down Expand Up @@ -75,7 +80,7 @@ func newMockRemoteVerifyPeer(peers []VerifyPeer) *mockVerifyPeers {
return &mockVerifyPeers{peers}
}

func makeTestBackendWithRemoteValidator(blocks int) (*testBackend, *testBackend, []*types.Block, error) {
func makeTestBackendWithRemoteValidator(blocks int, mode VerifyMode, failed *verifFailedStatus) (*testBackend, *testBackend, []*types.Block, error) {
signer := types.HomesteadSigner{}

// Create a database pre-initialize with a genesis block
Expand Down Expand Up @@ -105,7 +110,7 @@ func makeTestBackendWithRemoteValidator(blocks int) (*testBackend, *testBackend,
}

fastnode, err := NewBlockChain(db2, nil, params.TestChainConfig, engine2, vm.Config{},
nil, nil, EnableBlockValidator(params.TestChainConfig, engine2, FullVerify, newMockRemoteVerifyPeer(peers)))
nil, nil, EnableBlockValidator(params.TestChainConfig, engine2, mode, newMockRemoteVerifyPeer(peers)))
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -156,9 +161,15 @@ func makeTestBackendWithRemoteValidator(blocks int) (*testBackend, *testBackend,

peer.setCallBack(func(req *requestRoot) {
if fastnode.validator != nil && fastnode.validator.RemoteVerifyManager() != nil {
resp := verifier.GetRootByDiffHash(req.blockNumber, req.blockHash, req.diffHash)
if failed != nil && req.blockNumber == failed.blockNumber {
resp.Status = failed.status
} else {
resp.Status = types.StatusFullVerified
}
fastnode.validator.RemoteVerifyManager().
HandleRootResponse(
verifier.GetRootByDiffHash(req.blockNumber, req.blockHash, req.diffHash), "mock")
resp, peer.ID())
}
})
if _, err := verifier.InsertChain(bs); err != nil {
Expand All @@ -176,12 +187,42 @@ func makeTestBackendWithRemoteValidator(blocks int) (*testBackend, *testBackend,
}

func TestFastNode(t *testing.T) {
_, fastnode, blocks, err := makeTestBackendWithRemoteValidator(10240)
// test full mode and succeed
_, fastnode, blocks, err := makeTestBackendWithRemoteValidator(10240, FullVerify, nil)
if err != nil {
t.Fatalf(err.Error())
}
_, err = fastnode.chain.InsertChain(blocks)
if err != nil {
t.Fatalf(err.Error())
}
// test full mode and failed
failed := &verifFailedStatus{status: types.StatusDiffHashMismatch, blockNumber: 2048}
_, fastnode, blocks, err = makeTestBackendWithRemoteValidator(10240, FullVerify, failed)
if err != nil {
t.Fatalf(err.Error())
}
_, err = fastnode.chain.InsertChain(blocks)
if err == nil || fastnode.chain.CurrentBlock().NumberU64() != failed.blockNumber+10 {
t.Fatalf("blocks insert should be failed at height %d", failed.blockNumber+11)
}
// test insecure mode and succeed
_, fastnode, blocks, err = makeTestBackendWithRemoteValidator(10240, InsecureVerify, nil)
if err != nil {
t.Fatalf(err.Error())
}
_, err = fastnode.chain.InsertChain(blocks)
if err != nil {
t.Fatalf(err.Error())
}
// test insecure mode and failed
failed = &verifFailedStatus{status: types.StatusImpossibleFork, blockNumber: 2048}
_, fastnode, blocks, err = makeTestBackendWithRemoteValidator(10240, FullVerify, failed)
if err != nil {
t.Fatalf(err.Error())
}
_, err = fastnode.chain.InsertChain(blocks)
if err == nil || fastnode.chain.CurrentBlock().NumberU64() != failed.blockNumber+10 {
t.Fatalf("blocks insert should be failed at height %d", failed.blockNumber+11)
}
}

0 comments on commit 0132f13

Please sign in to comment.