github.com/iotexproject/iotex-core@v1.14.1-rc1/blockindex/contractstaking/delta_cache_test.go (about) 1 package contractstaking 2 3 import ( 4 "math/big" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/iotexproject/iotex-core/test/identityset" 10 ) 11 12 func TestContractStakingDelta_BucketInfoDelta(t *testing.T) { 13 require := require.New(t) 14 15 // create a new delta cache 16 cache := newContractStakingDelta() 17 18 // add bucket info 19 bi := &bucketInfo{TypeIndex: 1, CreatedAt: 1, UnlockedAt: maxBlockNumber, UnstakedAt: maxBlockNumber, Delegate: identityset.Address(1), Owner: identityset.Address(2)} 20 require.NoError(cache.AddBucketInfo(1, bi)) 21 22 // modify bucket info 23 bi = &bucketInfo{TypeIndex: 2, CreatedAt: 2, UnlockedAt: maxBlockNumber, UnstakedAt: maxBlockNumber, Delegate: identityset.Address(3), Owner: identityset.Address(4)} 24 require.NoError(cache.UpdateBucketInfo(2, bi)) 25 26 // remove bucket info 27 require.NoError(cache.DeleteBucketInfo(3)) 28 29 // get bucket info delta 30 delta := cache.BucketInfoDelta() 31 32 // check added bucket info 33 require.Len(delta[deltaStateAdded], 1) 34 added, ok := delta[deltaStateAdded][1] 35 require.True(ok) 36 require.NotNil(added) 37 require.EqualValues(1, added.TypeIndex) 38 require.EqualValues(1, added.CreatedAt) 39 require.EqualValues(maxBlockNumber, added.UnlockedAt) 40 require.EqualValues(maxBlockNumber, added.UnstakedAt) 41 require.EqualValues(identityset.Address(1), added.Delegate) 42 require.EqualValues(identityset.Address(2), added.Owner) 43 44 // check modified bucket info 45 require.Len(delta[deltaStateModified], 1) 46 modified, ok := delta[deltaStateModified][2] 47 require.True(ok) 48 require.NotNil(modified) 49 require.EqualValues(2, modified.TypeIndex) 50 require.EqualValues(2, modified.CreatedAt) 51 require.EqualValues(maxBlockNumber, modified.UnlockedAt) 52 require.EqualValues(maxBlockNumber, modified.UnstakedAt) 53 require.EqualValues(identityset.Address(3), modified.Delegate) 54 require.EqualValues(identityset.Address(4), modified.Owner) 55 56 // check removed bucket info 57 require.Len(delta[deltaStateRemoved], 1) 58 removed, ok := delta[deltaStateRemoved][3] 59 require.True(ok) 60 require.Nil(removed) 61 62 } 63 64 func TestContractStakingDelta_BucketTypeDelta(t *testing.T) { 65 require := require.New(t) 66 67 // create a new delta cache 68 cache := newContractStakingDelta() 69 70 // add bucket type 71 require.NoError(cache.AddBucketType(1, &BucketType{Amount: big.NewInt(100), Duration: 100, ActivatedAt: 1})) 72 require.NoError(cache.AddBucketType(2, &BucketType{Amount: big.NewInt(200), Duration: 100, ActivatedAt: 1})) 73 74 // modify bucket type 1 & 3 75 require.NoError(cache.UpdateBucketType(1, &BucketType{Amount: big.NewInt(100), Duration: 100, ActivatedAt: 3})) 76 require.NoError(cache.UpdateBucketType(3, &BucketType{Amount: big.NewInt(100), Duration: 100, ActivatedAt: 4})) 77 78 delta := cache.BucketTypeDelta() 79 // check added bucket type 80 require.Len(delta[deltaStateAdded], 2) 81 added, ok := delta[deltaStateAdded][1] 82 require.True(ok) 83 require.NotNil(added) 84 require.EqualValues(100, added.Amount.Int64()) 85 require.EqualValues(100, added.Duration) 86 require.EqualValues(3, added.ActivatedAt) 87 // check modified bucket type 88 modified, ok := delta[deltaStateModified][3] 89 require.True(ok) 90 require.NotNil(modified) 91 require.EqualValues(100, modified.Amount.Int64()) 92 require.EqualValues(100, modified.Duration) 93 require.EqualValues(4, modified.ActivatedAt) 94 95 } 96 97 func TestContractStakingDelta_MatchBucketType(t *testing.T) { 98 require := require.New(t) 99 100 // create a new delta cache 101 cache := newContractStakingDelta() 102 103 // test with empty bucket type 104 index, bucketType, ok := cache.MatchBucketType(big.NewInt(100), 100) 105 require.False(ok) 106 require.EqualValues(0, index) 107 require.Nil(bucketType) 108 109 // add bucket types 110 require.NoError(cache.AddBucketType(1, &BucketType{Amount: big.NewInt(100), Duration: 100, ActivatedAt: 1})) 111 require.NoError(cache.AddBucketType(2, &BucketType{Amount: big.NewInt(200), Duration: 100, ActivatedAt: 1})) 112 113 // test with amount and duration that match bucket type 1 114 amount := big.NewInt(100) 115 duration := uint64(100) 116 index, bucketType, ok = cache.MatchBucketType(amount, duration) 117 require.True(ok) 118 require.EqualValues(1, index) 119 require.NotNil(bucketType) 120 require.EqualValues(big.NewInt(100), bucketType.Amount) 121 require.EqualValues(uint64(100), bucketType.Duration) 122 require.EqualValues(uint64(1), bucketType.ActivatedAt) 123 124 // test with amount and duration that match bucket type 2 125 amount = big.NewInt(200) 126 duration = uint64(100) 127 index, bucketType, ok = cache.MatchBucketType(amount, duration) 128 require.True(ok) 129 require.EqualValues(2, index) 130 require.NotNil(bucketType) 131 require.EqualValues(big.NewInt(200), bucketType.Amount) 132 require.EqualValues(uint64(100), bucketType.Duration) 133 require.EqualValues(uint64(1), bucketType.ActivatedAt) 134 135 // test with amount and duration that do not match any bucket type 136 amount = big.NewInt(300) 137 duration = uint64(100) 138 index, bucketType, ok = cache.MatchBucketType(amount, duration) 139 require.False(ok) 140 require.EqualValues(0, index) 141 require.Nil(bucketType) 142 } 143 144 func TestContractStakingDelta_GetBucketInfo(t *testing.T) { 145 require := require.New(t) 146 147 // create a new delta cache 148 cache := newContractStakingDelta() 149 150 // add bucket info 151 bi := &bucketInfo{TypeIndex: 1, CreatedAt: 1, UnlockedAt: maxBlockNumber, UnstakedAt: maxBlockNumber, Delegate: identityset.Address(1), Owner: identityset.Address(2)} 152 require.NoError(cache.AddBucketInfo(1, bi)) 153 154 // get added bucket info 155 info, state := cache.GetBucketInfo(1) 156 require.NotNil(info) 157 require.EqualValues(1, info.TypeIndex) 158 require.EqualValues(1, info.CreatedAt) 159 require.EqualValues(maxBlockNumber, info.UnlockedAt) 160 require.EqualValues(maxBlockNumber, info.UnstakedAt) 161 require.EqualValues(identityset.Address(1), info.Delegate) 162 require.EqualValues(identityset.Address(2), info.Owner) 163 require.EqualValues(deltaStateAdded, state) 164 165 // modify bucket info 2 166 bi = &bucketInfo{TypeIndex: 2, CreatedAt: 2, UnlockedAt: maxBlockNumber, UnstakedAt: maxBlockNumber, Delegate: identityset.Address(3), Owner: identityset.Address(4)} 167 require.NoError(cache.UpdateBucketInfo(2, bi)) 168 // get modified bucket info 169 info, state = cache.GetBucketInfo(2) 170 require.NotNil(info) 171 require.EqualValues(2, info.TypeIndex) 172 require.EqualValues(2, info.CreatedAt) 173 require.EqualValues(maxBlockNumber, info.UnlockedAt) 174 require.EqualValues(maxBlockNumber, info.UnstakedAt) 175 require.EqualValues(identityset.Address(3), info.Delegate) 176 require.EqualValues(identityset.Address(4), info.Owner) 177 require.EqualValues(deltaStateModified, state) 178 179 // remove bucket info 2 180 require.NoError(cache.DeleteBucketInfo(2)) 181 // get removed bucket info 182 info, state = cache.GetBucketInfo(2) 183 require.Nil(info) 184 require.EqualValues(deltaStateRemoved, state) 185 } 186 187 func TestContractStakingDelta_GetBucketType(t *testing.T) { 188 require := require.New(t) 189 190 // create a new delta cache 191 cache := newContractStakingDelta() 192 193 // add bucket type 194 bt := &BucketType{Amount: big.NewInt(100), Duration: 100, ActivatedAt: 1} 195 require.NoError(cache.AddBucketType(1, bt)) 196 197 // get added bucket type 198 bucketType, state := cache.GetBucketType(1) 199 require.NotNil(bucketType) 200 require.EqualValues(big.NewInt(100), bucketType.Amount) 201 require.EqualValues(100, bucketType.Duration) 202 require.EqualValues(1, bucketType.ActivatedAt) 203 require.EqualValues(deltaStateAdded, state) 204 205 // modify bucket type 206 bt = &BucketType{Amount: big.NewInt(200), Duration: 200, ActivatedAt: 2} 207 require.NoError(cache.UpdateBucketType(2, bt)) 208 // get modified bucket type 209 bucketType, state = cache.GetBucketType(2) 210 require.NotNil(bucketType) 211 require.EqualValues(big.NewInt(200), bucketType.Amount) 212 require.EqualValues(200, bucketType.Duration) 213 require.EqualValues(2, bucketType.ActivatedAt) 214 require.EqualValues(deltaStateModified, state) 215 216 } 217 218 func TestContractStakingDelta_AddedBucketCnt(t *testing.T) { 219 require := require.New(t) 220 221 // create a new delta cache 222 cache := newContractStakingDelta() 223 224 // test with no added bucket info 225 addedBucketCnt := cache.AddedBucketCnt() 226 require.EqualValues(0, addedBucketCnt) 227 228 // add bucket types 229 require.NoError(cache.AddBucketType(1, &BucketType{Amount: big.NewInt(100), Duration: 100, ActivatedAt: 1})) 230 require.NoError(cache.AddBucketType(2, &BucketType{Amount: big.NewInt(200), Duration: 100, ActivatedAt: 1})) 231 232 // add bucket info 233 bi := &bucketInfo{TypeIndex: 1, CreatedAt: 1, UnlockedAt: maxBlockNumber, UnstakedAt: maxBlockNumber, Delegate: identityset.Address(1), Owner: identityset.Address(2)} 234 require.NoError(cache.AddBucketInfo(1, bi)) 235 // add bucket info 236 bi = &bucketInfo{TypeIndex: 2, CreatedAt: 1, UnlockedAt: maxBlockNumber, UnstakedAt: maxBlockNumber, Delegate: identityset.Address(1), Owner: identityset.Address(2)} 237 require.NoError(cache.AddBucketInfo(2, bi)) 238 239 // test with added bucket info 240 addedBucketCnt = cache.AddedBucketCnt() 241 require.EqualValues(2, addedBucketCnt) 242 243 // remove bucket info 244 require.NoError(cache.DeleteBucketInfo(3)) 245 246 // test with removed bucket info 247 addedBucketCnt = cache.AddedBucketCnt() 248 require.EqualValues(2, addedBucketCnt) 249 } 250 251 func TestContractStakingDelta_AddedBucketTypeCnt(t *testing.T) { 252 require := require.New(t) 253 254 // create a new delta cache 255 cache := newContractStakingDelta() 256 257 // test with no added bucket types 258 addedBucketTypeCnt := cache.AddedBucketTypeCnt() 259 require.EqualValues(0, addedBucketTypeCnt) 260 261 // add bucket types 262 require.NoError(cache.AddBucketType(1, &BucketType{Amount: big.NewInt(100), Duration: 100, ActivatedAt: 1})) 263 require.NoError(cache.AddBucketType(2, &BucketType{Amount: big.NewInt(200), Duration: 100, ActivatedAt: 1})) 264 require.NoError(cache.AddBucketType(3, &BucketType{Amount: big.NewInt(300), Duration: 100, ActivatedAt: 1})) 265 266 // test with added bucket type 267 addedBucketTypeCnt = cache.AddedBucketTypeCnt() 268 require.EqualValues(3, addedBucketTypeCnt) 269 }