github.com/aergoio/aergo@v1.3.1/consensus/impl/raftv2/cluster_test.go (about) 1 package raftv2 2 3 import ( 4 "encoding/json" 5 "github.com/aergoio/aergo/config" 6 "github.com/aergoio/aergo/consensus" 7 "github.com/aergoio/aergo/types" 8 "github.com/stretchr/testify/assert" 9 "testing" 10 ) 11 12 var ( 13 testMbrs []*consensus.Member 14 testPeerID types.PeerID 15 testEncID string 16 17 testSnapData *consensus.SnapshotData 18 testPeerIDs []types.PeerID 19 testPeerIDStrs = []string{ 20 "16Uiu2HAkvaAMCHkd9hZ6hQkdDLKoXP4eLJSqkMF1YqkSNy5v9SVn", 21 "16Uiu2HAmJqEp9f9WAbzFxkLrnHnW4EuUDM69xkCDPF26HmNCsib6", 22 "16Uiu2HAmA2ysmFxoQ37sk1Zk2sMrPysqTmwYAFrACyf3LtP3gxpJ", 23 "16Uiu2HAmQti7HLHC9rXqkeABtauv2YsCPG3Uo1WLqbXmbuxpbjmF", 24 } 25 ) 26 27 func init() { 28 testEncID = "16Uiu2HAkxVB65cmCWceTu4HsHnz8WkUKknZXwr7PYdg2vy1fjDcU" 29 testPeerIDs = make([]types.PeerID, len(testPeerIDStrs)) 30 testPeerID, _ = types.IDB58Decode(testEncID) 31 32 for i, peerStr := range testPeerIDStrs { 33 peerID, _ := types.IDB58Decode(peerStr) 34 testPeerIDs[i] = peerID 35 } 36 37 testMbrs = []*consensus.Member{ 38 {types.MemberAttr{ 39 ID: 1, 40 Name: "testm1", 41 Address: "/ip4/127.0.0.1/13001", 42 PeerID: []byte(testPeerIDs[0]), 43 }}, 44 {types.MemberAttr{ 45 ID: 2, 46 Name: "testm2", 47 Address: "/ip4/127.0.0.1/tcp/13002", 48 PeerID: []byte(testPeerIDs[1]), 49 }}, 50 {types.MemberAttr{ 51 ID: 3, 52 Name: "testm3", 53 Address: "/ip4/127.0.0.1/tcp/13003", 54 PeerID: []byte(testPeerIDs[2]), 55 }}, 56 } 57 58 testBlock := types.NewBlock(nil, nil, nil, nil, nil, 0) 59 60 testSnapData = consensus.NewSnapshotData(testMbrs, nil, testBlock) 61 } 62 63 func TestMemberJson(t *testing.T) { 64 mbr := testMbrs[0] 65 66 data, err := json.Marshal(mbr) 67 assert.NoError(t, err) 68 69 var newMbr = consensus.Member{} 70 err = json.Unmarshal(data, &newMbr) 71 assert.NoError(t, err) 72 73 assert.NoError(t, err) 74 //t.Logf("peer=%s", types.IDB58Encode(newMbr.GetPeerID())) 75 76 assert.True(t, mbr.Equal(&newMbr)) 77 78 mbrRemove := &consensus.Member{types.MemberAttr{ID: 1}} 79 data, err = json.Marshal(mbrRemove) 80 assert.NoError(t, err) 81 82 newMbr = consensus.Member{} 83 err = json.Unmarshal(data, &newMbr) 84 assert.NoError(t, err) 85 } 86 87 func TestSnapDataJson(t *testing.T) { 88 var snapdata = testSnapData 89 90 data, err := snapdata.Encode() 91 assert.NoError(t, err) 92 93 var newSnapdata = &consensus.SnapshotData{} 94 95 err = newSnapdata.Decode(data) 96 assert.NoError(t, err) 97 98 assert.True(t, snapdata.Equal(newSnapdata)) 99 } 100 101 func TestClusterConfChange(t *testing.T) { 102 // init cluster 103 serverCtx := config.NewServerContext("", "") 104 testCfg := serverCtx.GetDefaultConfig().(*config.Config) 105 testCfg.Consensus.Raft = &config.RaftConfig{ 106 Name: "test1", 107 /* 108 BPs: []config.RaftBPConfig{ 109 {"test1", "/ip4/127.0.0.1/tcp/10001", testPeerIDs[0]}, 110 {"test2", "/ip4/127.0.0.1/tcp/10002", testPeerIDs[1]}, 111 {"test3", "/ip4/127.0.0.1/tcp/10003", testPeerIDs[2]}, 112 },*/ 113 } 114 115 mbrs := []*types.MemberAttr{ 116 {ID: 0, Name: "test1", Address: "/ip4/127.0.0.1/tcp/10001", PeerID: []byte(testPeerIDs[0])}, 117 {ID: 1, Name: "test2", Address: "/ip4/127.0.0.1/tcp/10002", PeerID: []byte(testPeerIDs[1])}, 118 {ID: 2, Name: "test3", Address: "/ip4/127.0.0.1/tcp/10003", PeerID: []byte(testPeerIDs[2])}, 119 } 120 121 cl := NewCluster([]byte("test"), nil, "test1", testPeerIDs[0], 0, nil) 122 123 err := cl.AddInitialMembers(mbrs) 124 assert.NoError(t, err) 125 126 // add applied members 127 for _, m := range cl.Members().ToArray() { 128 err = cl.addMember(m, true) 129 assert.NoError(t, err) 130 } 131 132 // normal case 133 req := &types.MembershipChange{ 134 Type: types.MembershipChangeType_ADD_MEMBER, 135 Attr: &types.MemberAttr{ID: 3, Name: "test4", Address: "/ip4/127.0.0.1/tcp/10004", PeerID: []byte(testPeerIDs[3])}, 136 } 137 _, err = cl.makeProposal(req, true) 138 assert.NoError(t, err) 139 140 id := cl.getNodeID("test3") 141 req = &types.MembershipChange{ 142 Type: types.MembershipChangeType_REMOVE_MEMBER, 143 Attr: &types.MemberAttr{ID: id}, 144 } 145 146 _, err = cl.makeProposal(req, true) 147 assert.NoError(t, err) 148 149 // failed case 150 req = &types.MembershipChange{ 151 Type: types.MembershipChangeType_ADD_MEMBER, 152 Attr: &types.MemberAttr{Address: "/ip4/127.0.0.1/tcp/10004", PeerID: []byte(testPeerIDs[3])}, 153 } 154 _, err = cl.makeProposal(req, true) 155 assert.Error(t, err, "no name") 156 157 req = &types.MembershipChange{ 158 Type: types.MembershipChangeType_ADD_MEMBER, 159 Attr: &types.MemberAttr{Name: "test4", Address: "/ip4/127.0.0.1/tcp/10004", PeerID: []byte(testPeerIDs[0])}, 160 } 161 _, err = cl.makeProposal(req, true) 162 assert.Error(t, err, "duplicate peerid") 163 164 req = &types.MembershipChange{ 165 Type: types.MembershipChangeType_REMOVE_MEMBER, 166 Attr: &types.MemberAttr{Name: "test4", Address: "/ip4/127.0.0.1/tcp/10004", PeerID: []byte(testPeerIDs[3])}, 167 } 168 _, err = cl.makeProposal(req, true) 169 assert.Error(t, err, "no id to remove") 170 171 } 172 173 func TestClusterEqual(t *testing.T) { 174 //isAllMembersEqual 175 cl := NewCluster([]byte("test"), nil, "testm1", testPeerIDs[0], 0, nil) 176 for _, m := range testMbrs { 177 err := cl.addMember(m, true) 178 assert.NoError(t, err) 179 } 180 181 assert.True(t, cl.isAllMembersEqual(testMbrs, nil)) 182 183 rm := testMbrs[2] 184 err := cl.removeMember(rm) 185 assert.NoError(t, err) 186 187 rmMembers := []*consensus.Member{rm} 188 assert.True(t, cl.isAllMembersEqual(testMbrs[0:2], rmMembers)) 189 190 cl = NewCluster([]byte("test"), nil, "testm1", testPeerIDs[0], 0, nil) 191 for _, m := range testMbrs { 192 newM := *m 193 newM.Address = "invalidaddress" 194 err := cl.addMember(&newM, true) 195 assert.NoError(t, err) 196 } 197 198 assert.False(t, cl.isAllMembersEqual(testMbrs, nil)) 199 200 cl = NewCluster([]byte("test"), nil, "testm1", testPeerIDs[0], 0, nil) 201 for i, m := range testMbrs { 202 newM := *m 203 newM.ID = uint64(i) + 100 204 err := cl.addMember(&newM, true) 205 assert.NoError(t, err) 206 } 207 208 assert.False(t, cl.isAllMembersEqual(testMbrs, nil)) 209 }