|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "github.com/vechain/networkhub/hayabusa" |
| 10 | + "github.com/vechain/networkhub/preset" |
| 11 | + "github.com/vechain/networkhub/thorbuilder" |
| 12 | + "github.com/vechain/thor/v2/thor" |
| 13 | + "github.com/vechain/thor/v2/thorclient" |
| 14 | +) |
| 15 | + |
| 16 | +// TestClientFourNodesHayabusa tests the client with a 4-node Hayabusa network. |
| 17 | +// This test verifies that the client can: |
| 18 | +// 1. Set up and start a 4-node Hayabusa network with immediate transition |
| 19 | +// 2. Wait for all nodes to connect and sync |
| 20 | +// 3. Deploy and execute smart contracts in post-hayabusa state |
| 21 | +// 4. Verify validator consensus and network health |
| 22 | +func TestClientFourNodesHayabusa(t *testing.T) { |
| 23 | + // Create the four nodes Hayabusa network with immediate transition |
| 24 | + fourNodesHayabusaNetwork := preset.LocalFourNodesHayabusa() |
| 25 | + fourNodesHayabusaNetwork.ThorBuilder.DownloadConfig = &thorbuilder.DownloadConfig{ |
| 26 | + RepoUrl: "https://github.com/vechain/thor", |
| 27 | + Branch: "release/hayabusa", |
| 28 | + IsReusable: false, |
| 29 | + } |
| 30 | + |
| 31 | + // Update ports to avoid collision with other tests |
| 32 | + basePort := 8700 |
| 33 | + for _, node := range fourNodesHayabusaNetwork.Nodes { |
| 34 | + basePort++ |
| 35 | + node.SetAPIAddr(fmt.Sprintf("127.0.0.1:%d", basePort)) |
| 36 | + basePort++ |
| 37 | + node.SetP2PListenPort(basePort) |
| 38 | + } |
| 39 | + |
| 40 | + // Create client with the network |
| 41 | + c, err := New(fourNodesHayabusaNetwork) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + require.NoError(t, c.Start()) |
| 45 | + // Cleanup |
| 46 | + defer func() { |
| 47 | + if err := c.Stop(); err != nil { |
| 48 | + t.Logf("Warning: failed to stop client: %v", err) |
| 49 | + } |
| 50 | + }() |
| 51 | + |
| 52 | + // Wait for all nodes to connect and sync |
| 53 | + t.Log("Waiting for Hayabusa nodes to connect and sync...") |
| 54 | + require.NoError(t, c.network.HealthCheck(4, 2*time.Minute)) |
| 55 | + |
| 56 | + // Test staker contract functionality to verify validators are active |
| 57 | + client := thorclient.New(c.network.Nodes[0].GetHTTPAddr()) |
| 58 | + staker := hayabusa.NewStaker(client) |
| 59 | + |
| 60 | + // Check firstActive to see if validators are now active |
| 61 | + validatorAddr, err := staker.FirstActive() |
| 62 | + require.NoError(t, err) |
| 63 | + t.Logf("FirstActive successful - Validator: %s", validatorAddr) |
| 64 | + |
| 65 | + // Verify that one of our validators is now active |
| 66 | + expectedValidators := []thor.Address{ |
| 67 | + *preset.SixNNAccount1.Address, |
| 68 | + *preset.SixNNAccount1.Address, |
| 69 | + *preset.SixNNAccount1.Address, |
| 70 | + *preset.SixNNAccount1.Address, |
| 71 | + } |
| 72 | + |
| 73 | + validatorFound := false |
| 74 | + for _, expected := range expectedValidators { |
| 75 | + if validatorAddr == expected { |
| 76 | + validatorFound = true |
| 77 | + break |
| 78 | + } |
| 79 | + } |
| 80 | + require.True(t, validatorFound, "Active validator should be one of our registered validators, got %s", validatorAddr) |
| 81 | + |
| 82 | + // Verify all nodes are producing blocks |
| 83 | + t.Log("Verifying all validator nodes are participating in consensus...") |
| 84 | + for i, node := range c.network.Nodes { |
| 85 | + nodeClient := thorclient.New(node.GetHTTPAddr()) |
| 86 | + |
| 87 | + // Check that each node can respond to queries |
| 88 | + block, err := nodeClient.Block("best") |
| 89 | + require.NoError(t, err, "Node %d should respond to block queries", i+1) |
| 90 | + require.Greater(t, block.Number, uint32(0), "Node %d should have produced blocks", i+1) |
| 91 | + |
| 92 | + t.Logf("Node %d: Block %d, Validator nodes operational", i+1, block.Number) |
| 93 | + } |
| 94 | + |
| 95 | + t.Log("Successfully tested Hayabusa network with 4 validator nodes!") |
| 96 | +} |
0 commit comments