From f6301612ac6ac2470f13081ef4b31e9edf335112 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Thu, 14 May 2020 18:45:35 +0300 Subject: [PATCH] satellite/orders: use serial SerialNumber By ensuring that they have less randomness it means that they can be compressed better. Using a timestamp should be a good improvement here. Change-Id: Ic4dabb53335a744ff1c332dd279f37ae2cd79357 --- satellite/orders/service.go | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/satellite/orders/service.go b/satellite/orders/service.go index a55a890ee..3cd4af413 100644 --- a/satellite/orders/service.go +++ b/satellite/orders/service.go @@ -6,8 +6,10 @@ package orders import ( "bytes" "context" + "crypto/rand" + "encoding/binary" "math" - "math/rand" + mathrand "math/rand" "sync" "time" @@ -48,7 +50,7 @@ type Service struct { repairMaxExcessRateOptimalThreshold float64 nodeStatusLogging bool rngMu sync.Mutex - rng *rand.Rand + rng *mathrand.Rand } // NewService creates new service for creating order limits. @@ -66,7 +68,7 @@ func NewService( orderExpiration: orderExpiration, repairMaxExcessRateOptimalThreshold: repairMaxExcessRateOptimalThreshold, nodeStatusLogging: nodeStatusLogging, - rng: rand.New(rand.NewSource(time.Now().UnixNano())), + rng: mathrand.New(mathrand.NewSource(time.Now().UnixNano())), } } @@ -76,13 +78,18 @@ func (service *Service) VerifyOrderLimitSignature(ctx context.Context, signed *p return signing.VerifyOrderLimitSignature(ctx, service.satellite, signed) } -func (service *Service) createSerial(ctx context.Context) (_ storj.SerialNumber, err error) { +func (service *Service) createSerial(ctx context.Context, orderExpiration time.Time) (_ storj.SerialNumber, err error) { defer mon.Task()(&ctx)(&err) - id, err := uuid.New() + + var serial storj.SerialNumber + + binary.BigEndian.PutUint64(serial[0:8], uint64(orderExpiration.Unix())) + _, err = rand.Read(serial[8:]) if err != nil { return storj.SerialNumber{}, Error.Wrap(err) } - return storj.SerialNumber(id), nil + + return serial, nil } func (service *Service) saveSerial(ctx context.Context, serialNumber storj.SerialNumber, bucketID []byte, expiresAt time.Time) (err error) { @@ -132,7 +139,7 @@ func (service *Service) CreateGetOrderLimitsOld(ctx context.Context, bucketID [] return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } - serialNumber, err := service.createSerial(ctx) + serialNumber, err := service.createSerial(ctx, orderExpiration) if err != nil { return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } @@ -226,7 +233,7 @@ func (service *Service) CreateGetOrderLimits(ctx context.Context, bucketID []byt return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } - serialNumber, err := service.createSerial(ctx) + serialNumber, err := service.createSerial(ctx, orderExpiration) if err != nil { return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } @@ -358,7 +365,7 @@ func (service *Service) CreatePutOrderLimits(ctx context.Context, bucketID []byt return storj.PieceID{}, nil, storj.PiecePrivateKey{}, Error.Wrap(err) } - serialNumber, err := service.createSerial(ctx) + serialNumber, err := service.createSerial(ctx, orderExpiration) if err != nil { return storj.PieceID{}, nil, storj.PiecePrivateKey{}, Error.Wrap(err) } @@ -425,7 +432,7 @@ func (service *Service) CreateDeleteOrderLimits(ctx context.Context, bucketID [] return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } - serialNumber, err := service.createSerial(ctx) + serialNumber, err := service.createSerial(ctx, orderExpiration) if err != nil { return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } @@ -506,7 +513,7 @@ func (service *Service) CreateAuditOrderLimits(ctx context.Context, bucketID []b return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } - serialNumber, err := service.createSerial(ctx) + serialNumber, err := service.createSerial(ctx, orderExpiration) if err != nil { return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } @@ -593,7 +600,7 @@ func (service *Service) CreateAuditOrderLimit(ctx context.Context, bucketID []by return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } - serialNumber, err := service.createSerial(ctx) + serialNumber, err := service.createSerial(ctx, orderExpiration) if err != nil { return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } @@ -673,7 +680,7 @@ func (service *Service) CreateGetRepairOrderLimits(ctx context.Context, bucketID return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } - serialNumber, err := service.createSerial(ctx) + serialNumber, err := service.createSerial(ctx, orderExpiration) if err != nil { return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } @@ -754,7 +761,7 @@ func (service *Service) CreatePutRepairOrderLimits(ctx context.Context, bucketID return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } - serialNumber, err := service.createSerial(ctx) + serialNumber, err := service.createSerial(ctx, orderExpiration) if err != nil { return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } @@ -857,7 +864,7 @@ func (service *Service) CreateGracefulExitPutOrderLimit(ctx context.Context, buc return nil, storj.PiecePrivateKey{}, Error.Wrap(err) } - serialNumber, err := service.createSerial(ctx) + serialNumber, err := service.createSerial(ctx, orderExpiration) if err != nil { return nil, storj.PiecePrivateKey{}, Error.Wrap(err) }