|
| 1 | +package epochkghandler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/jackc/pgx/v4/pgxpool" |
| 9 | + pubsub "github.com/libp2p/go-libp2p-pubsub" |
| 10 | + "gotest.tools/assert" |
| 11 | + |
| 12 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/database" |
| 13 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/identitypreimage" |
| 14 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/testkeygen" |
| 15 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/testsetup" |
| 16 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/p2p" |
| 17 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg" |
| 18 | +) |
| 19 | + |
| 20 | +// The number of identity preimages to generate for each of the benchmark runs. Note that this must |
| 21 | +// be smaller than MaxNumKeysPerMessage, otherwise the benchmarks will fail. |
| 22 | +const numIdentityPreimages = 100 |
| 23 | + |
| 24 | +func prepareBenchmark(ctx context.Context, b *testing.B, dbpool *pgxpool.Pool) (*testkeygen.EonKeys, []identitypreimage.IdentityPreimage) { |
| 25 | + b.Helper() |
| 26 | + keyperIndex := uint64(1) |
| 27 | + identityPreimages := []identitypreimage.IdentityPreimage{} |
| 28 | + for i := 0; i < numIdentityPreimages; i++ { |
| 29 | + b := make([]byte, 52) |
| 30 | + big.NewInt(int64(i)).FillBytes(b) |
| 31 | + identityPreimage := identitypreimage.IdentityPreimage(b) |
| 32 | + identityPreimages = append(identityPreimages, identityPreimage) |
| 33 | + } |
| 34 | + |
| 35 | + keys := testsetup.InitializeEon(ctx, b, dbpool, config, keyperIndex) |
| 36 | + return keys, identityPreimages |
| 37 | +} |
| 38 | + |
| 39 | +func prepareKeysBenchmark(ctx context.Context, b *testing.B, dbpool *pgxpool.Pool) (p2p.MessageHandler, *p2pmsg.DecryptionKeys) { |
| 40 | + b.Helper() |
| 41 | + keys, identityPreimages := prepareBenchmark(ctx, b, dbpool) |
| 42 | + |
| 43 | + encodedDecryptionKeys := [][]byte{} |
| 44 | + for _, identityPreimage := range identityPreimages { |
| 45 | + decryptionKey, err := keys.EpochSecretKey(identityPreimage) |
| 46 | + assert.NilError(b, err) |
| 47 | + encodedDecryptionKey := decryptionKey.Marshal() |
| 48 | + encodedDecryptionKeys = append(encodedDecryptionKeys, encodedDecryptionKey) |
| 49 | + } |
| 50 | + decryptionKeys := []*p2pmsg.Key{} |
| 51 | + for i, identityPreimage := range identityPreimages { |
| 52 | + key := &p2pmsg.Key{ |
| 53 | + Identity: identityPreimage.Bytes(), |
| 54 | + Key: encodedDecryptionKeys[i], |
| 55 | + } |
| 56 | + decryptionKeys = append(decryptionKeys, key) |
| 57 | + } |
| 58 | + msg := &p2pmsg.DecryptionKeys{ |
| 59 | + InstanceID: config.GetInstanceID(), |
| 60 | + Eon: 1, |
| 61 | + Keys: decryptionKeys, |
| 62 | + } |
| 63 | + |
| 64 | + var handler p2p.MessageHandler = &DecryptionKeyHandler{config: config, dbpool: dbpool} |
| 65 | + |
| 66 | + return handler, msg |
| 67 | +} |
| 68 | + |
| 69 | +func prepareKeySharesBenchmark( |
| 70 | + ctx context.Context, |
| 71 | + b *testing.B, |
| 72 | + dbpool *pgxpool.Pool, |
| 73 | + isSecond bool, |
| 74 | +) (p2p.MessageHandler, *p2pmsg.DecryptionKeyShares) { |
| 75 | + b.Helper() |
| 76 | + keys, identityPreimages := prepareBenchmark(ctx, b, dbpool) |
| 77 | + var handler p2p.MessageHandler = &DecryptionKeyShareHandler{config: config, dbpool: dbpool} |
| 78 | + |
| 79 | + if isSecond { |
| 80 | + shares := []*p2pmsg.KeyShare{} |
| 81 | + keyperIndex := 0 |
| 82 | + for _, identityPreimage := range identityPreimages { |
| 83 | + share := &p2pmsg.KeyShare{ |
| 84 | + EpochID: identityPreimage.Bytes(), |
| 85 | + Share: keys.EpochSecretKeyShare(identityPreimage, keyperIndex).Marshal(), |
| 86 | + } |
| 87 | + shares = append(shares, share) |
| 88 | + } |
| 89 | + msg := &p2pmsg.DecryptionKeyShares{ |
| 90 | + InstanceID: config.GetInstanceID(), |
| 91 | + Eon: 1, |
| 92 | + KeyperIndex: uint64(keyperIndex), |
| 93 | + Shares: shares, |
| 94 | + } |
| 95 | + validationResult, err := handler.ValidateMessage(ctx, msg) |
| 96 | + assert.NilError(b, err) |
| 97 | + assert.Check(b, validationResult == pubsub.ValidationAccept) |
| 98 | + _, err = handler.HandleMessage(ctx, msg) |
| 99 | + assert.NilError(b, err) |
| 100 | + } |
| 101 | + |
| 102 | + keyperIndex := 2 |
| 103 | + shares := []*p2pmsg.KeyShare{} |
| 104 | + for _, identityPreimage := range identityPreimages { |
| 105 | + share := &p2pmsg.KeyShare{ |
| 106 | + EpochID: identityPreimage.Bytes(), |
| 107 | + Share: keys.EpochSecretKeyShare(identityPreimage, keyperIndex).Marshal(), |
| 108 | + } |
| 109 | + shares = append(shares, share) |
| 110 | + } |
| 111 | + msg := &p2pmsg.DecryptionKeyShares{ |
| 112 | + InstanceID: config.GetInstanceID(), |
| 113 | + Eon: 1, |
| 114 | + KeyperIndex: uint64(keyperIndex), |
| 115 | + Shares: shares, |
| 116 | + } |
| 117 | + |
| 118 | + return handler, msg |
| 119 | +} |
| 120 | + |
| 121 | +func BenchmarkValidateKeysIntegration(b *testing.B) { |
| 122 | + ctx := context.Background() |
| 123 | + dbpool, dbclose := testsetup.NewTestDBPool(ctx, b, database.Definition) |
| 124 | + b.Cleanup(dbclose) |
| 125 | + handler, msg := prepareKeysBenchmark(ctx, b, dbpool) |
| 126 | + |
| 127 | + b.ResetTimer() |
| 128 | + validationResult, err := handler.ValidateMessage(ctx, msg) |
| 129 | + b.StopTimer() |
| 130 | + assert.NilError(b, err) |
| 131 | + assert.Check(b, validationResult == pubsub.ValidationAccept) |
| 132 | +} |
| 133 | + |
| 134 | +func BenchmarkHandleKeysIntegration(b *testing.B) { |
| 135 | + ctx := context.Background() |
| 136 | + dbpool, dbclose := testsetup.NewTestDBPool(ctx, b, database.Definition) |
| 137 | + b.Cleanup(dbclose) |
| 138 | + handler, msg := prepareKeysBenchmark(ctx, b, dbpool) |
| 139 | + |
| 140 | + validationResult, err := handler.ValidateMessage(ctx, msg) |
| 141 | + assert.NilError(b, err) |
| 142 | + assert.Check(b, validationResult == pubsub.ValidationAccept) |
| 143 | + |
| 144 | + b.ResetTimer() |
| 145 | + _, err = handler.HandleMessage(ctx, msg) |
| 146 | + b.StopTimer() |
| 147 | + assert.NilError(b, err) |
| 148 | +} |
| 149 | + |
| 150 | +func BenchmarkValidateFirstKeySharesIntegration(b *testing.B) { |
| 151 | + ctx := context.Background() |
| 152 | + dbpool, dbclose := testsetup.NewTestDBPool(ctx, b, database.Definition) |
| 153 | + b.Cleanup(dbclose) |
| 154 | + handler, msg := prepareKeySharesBenchmark(ctx, b, dbpool, false) |
| 155 | + |
| 156 | + b.ResetTimer() |
| 157 | + validationResult, err := handler.ValidateMessage(ctx, msg) |
| 158 | + b.StopTimer() |
| 159 | + assert.NilError(b, err) |
| 160 | + assert.Check(b, validationResult == pubsub.ValidationAccept) |
| 161 | +} |
| 162 | + |
| 163 | +func BenchmarkHandleFirstKeySharesIntegration(b *testing.B) { |
| 164 | + ctx := context.Background() |
| 165 | + dbpool, dbclose := testsetup.NewTestDBPool(ctx, b, database.Definition) |
| 166 | + b.Cleanup(dbclose) |
| 167 | + handler, msg := prepareKeySharesBenchmark(ctx, b, dbpool, false) |
| 168 | + |
| 169 | + validationResult, err := handler.ValidateMessage(ctx, msg) |
| 170 | + assert.NilError(b, err) |
| 171 | + assert.Check(b, validationResult == pubsub.ValidationAccept) |
| 172 | + |
| 173 | + b.ResetTimer() |
| 174 | + _, err = handler.HandleMessage(ctx, msg) |
| 175 | + b.StopTimer() |
| 176 | + assert.NilError(b, err) |
| 177 | +} |
| 178 | + |
| 179 | +func BenchmarkValidateSecondKeySharesIntegration(b *testing.B) { |
| 180 | + ctx := context.Background() |
| 181 | + dbpool, dbclose := testsetup.NewTestDBPool(ctx, b, database.Definition) |
| 182 | + b.Cleanup(dbclose) |
| 183 | + handler, msg := prepareKeySharesBenchmark(ctx, b, dbpool, true) |
| 184 | + |
| 185 | + b.ResetTimer() |
| 186 | + validationResult, err := handler.ValidateMessage(ctx, msg) |
| 187 | + b.StopTimer() |
| 188 | + assert.NilError(b, err) |
| 189 | + assert.Check(b, validationResult == pubsub.ValidationAccept) |
| 190 | +} |
| 191 | + |
| 192 | +func BenchmarkHandleSecondKeySharesIntegration(b *testing.B) { |
| 193 | + ctx := context.Background() |
| 194 | + dbpool, dbclose := testsetup.NewTestDBPool(ctx, b, database.Definition) |
| 195 | + b.Cleanup(dbclose) |
| 196 | + handler, msg := prepareKeySharesBenchmark(ctx, b, dbpool, true) |
| 197 | + |
| 198 | + validationResult, err := handler.ValidateMessage(ctx, msg) |
| 199 | + assert.NilError(b, err) |
| 200 | + assert.Check(b, validationResult == pubsub.ValidationAccept) |
| 201 | + |
| 202 | + b.ResetTimer() |
| 203 | + _, err = handler.HandleMessage(ctx, msg) |
| 204 | + b.StopTimer() |
| 205 | + assert.NilError(b, err) |
| 206 | +} |
0 commit comments