diff --git a/api/params.go b/api/params.go index 6430eb60c5860be420c11b0715da0c960ddbc5e5..39f9d8db3768ac00c454bf89d2ba8b5326c9fb4e 100644 --- a/api/params.go +++ b/api/params.go @@ -8,6 +8,7 @@ package api import ( + "encoding/json" "gitlab.com/elixxir/client/cmix" "gitlab.com/elixxir/client/e2e/ratchet/partner/session" ) @@ -23,3 +24,16 @@ func GetDefaultParams() Params { Session: session.GetDefaultParams(), } } + +// GetParameters returns the default Params, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := GetDefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} diff --git a/api/params_test.go b/api/params_test.go new file mode 100644 index 0000000000000000000000000000000000000000..35bf61d8f10e5b494a8459f64d239d1569df36b4 --- /dev/null +++ b/api/params_test.go @@ -0,0 +1,51 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package api + +import ( + "bytes" + "encoding/json" + "testing" +) + +// Tests that no data is lost when marshaling and +// unmarshaling the Params object. +func TestParams_MarshalUnmarshal(t *testing.T) { + // Construct a set of params + p := GetDefaultParams() + + // Marshal the params + data, err := json.Marshal(&p) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + t.Logf("%s", string(data)) + + // Unmarshal the params object + received := Params{} + err = json.Unmarshal(data, &received) + if err != nil { + t.Fatalf("Unmarshal error: %v", err) + } + + // Re-marshal this params object + data2, err := json.Marshal(received) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + t.Logf("%s", string(data2)) + + // Check that they match (it is done this way to avoid + // false failures with the reflect.DeepEqual function and + // pointers) + if !bytes.Equal(data, data2) { + t.Fatalf("Data was lost in marshal/unmarshal.") + } +} diff --git a/auth/params.go b/auth/params.go index 92cb6e57de8f1e39efafccfc8d8d93ce5f30b917..907fa4ad825c3c6ef068d9dc7e6cd8b92d70a773 100644 --- a/auth/params.go +++ b/auth/params.go @@ -1,18 +1,44 @@ package auth -import "gitlab.com/elixxir/client/catalog" +import ( + "encoding/json" + "gitlab.com/elixxir/client/catalog" +) -type Param struct { - ReplayRequests bool +// Params is are the parameters for the auth package. +type Params struct { + ReplayRequests bool + RequestTag string + ConfirmTag string + ResetRequestTag string + ResetConfirmTag string +} +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + ReplayRequests bool RequestTag string ConfirmTag string ResetRequestTag string ResetConfirmTag string } -func GetDefaultParams() Param { - return Param{ +// GetParameters Obtain default Params, or override with +// given parameters if set. +func GetParameters(params string) (Params, error) { + p := GetDefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + +// GetDefaultParams returns a default set of Params. +func GetDefaultParams() Params { + return Params{ ReplayRequests: false, RequestTag: catalog.Request, ConfirmTag: catalog.Confirm, @@ -21,7 +47,7 @@ func GetDefaultParams() Param { } } -func GetDefaultTemporaryParams() Param { +func GetDefaultTemporaryParams() Params { p := GetDefaultParams() p.RequestTag = catalog.RequestEphemeral p.ConfirmTag = catalog.ConfirmEphemeral @@ -30,7 +56,38 @@ func GetDefaultTemporaryParams() Param { return p } -func (p Param) getConfirmTag(reset bool) string { +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{ + ReplayRequests: p.ReplayRequests, + RequestTag: p.ResetRequestTag, + ConfirmTag: p.ConfirmTag, + ResetRequestTag: p.RequestTag, + ResetConfirmTag: p.ResetConfirmTag, + } + return json.Marshal(&pDisk) +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{ + ReplayRequests: pDisk.ReplayRequests, + RequestTag: pDisk.ResetRequestTag, + ConfirmTag: pDisk.ConfirmTag, + ResetRequestTag: pDisk.RequestTag, + ResetConfirmTag: pDisk.ResetConfirmTag, + } + + return nil +} + +func (p Params) getConfirmTag(reset bool) string { if reset { return p.ResetConfirmTag } else { diff --git a/auth/state.go b/auth/state.go index 2be3d4d6b3a63f0b9706a1bcccbf862828cc06d5..859b4015e3bc4beda1d5fe80dc78c70ea9d0af02 100644 --- a/auth/state.go +++ b/auth/state.go @@ -41,7 +41,7 @@ type state struct { store *store.Store event event.Reporter - params Param + params Params backupTrigger func(reason string) } @@ -101,7 +101,7 @@ type Callbacks interface { // with a memory only versioned.KV) as well as a memory only versioned.KV for // NewState and use GetDefaultTemporaryParams() for the parameters func NewState(kv *versioned.KV, net cmix.Client, e2e e2e.Handler, - rng *fastRNG.StreamGenerator, event event.Reporter, params Param, + rng *fastRNG.StreamGenerator, event event.Reporter, params Params, callbacks Callbacks, backupTrigger func(reason string)) (State, error) { kv = kv.Prefix(makeStorePrefix(e2e.GetReceptionID())) return NewStateLegacy( @@ -114,7 +114,7 @@ func NewState(kv *versioned.KV, net cmix.Client, e2e e2e.Handler, // Does not modify the kv prefix for backwards compatibility // Otherwise, acts the same as NewState func NewStateLegacy(kv *versioned.KV, net cmix.Client, e2e e2e.Handler, - rng *fastRNG.StreamGenerator, event event.Reporter, params Param, + rng *fastRNG.StreamGenerator, event event.Reporter, params Params, callbacks Callbacks, backupTrigger func(reason string)) (State, error) { s := &state{ diff --git a/auth/state_test.go b/auth/state_test.go index 8b7e7305c4a198194ef0444ed59013c8dac4f3dc..10ebb2d3ed4d9d99c654da78dfacc260a877a771 100644 --- a/auth/state_test.go +++ b/auth/state_test.go @@ -146,7 +146,7 @@ func TestManager_ReplayRequests(t *testing.T) { rng: fastRNG.NewStreamGenerator(1000, 10, csprng.NewSystemRNG), store: s, event: &mockEventManager{}, - params: Param{ + params: Params{ ReplayRequests: true, }, } diff --git a/cmix/gateway/hostPool.go b/cmix/gateway/hostPool.go index 25dc7192ed9933dcf323190a526c772a1548e8a3..09f52d5bd30eaae4d768e58eb0ae3a5120aa8cce 100644 --- a/cmix/gateway/hostPool.go +++ b/cmix/gateway/hostPool.go @@ -14,6 +14,7 @@ package gateway import ( "encoding/binary" + "encoding/json" "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/storage" @@ -108,7 +109,18 @@ type PoolParams struct { ForceConnection bool // HostParams is the parameters for the creation of new Host objects. - HostParams connect.HostParams + //fixme params: have this adhere to json.Marshaler. + // This will allow the PoolParams object to have full adherence. + HostParams connect.HostParams `json:"-"` +} + +// poolParamsDisk will be the marshal-able and umarshal-able object. +type poolParamsDisk struct { + MaxPoolSize uint32 + PoolSize uint32 + ProxyAttempts uint32 + MaxPings uint32 + ForceConnection bool } // DefaultPoolParams returns a default set of PoolParams. @@ -132,6 +144,54 @@ func DefaultPoolParams() PoolParams { return p } +// GetParameters returns the default PoolParams, or +// override with given parameters, if set. +func GetParameters(params string) (PoolParams, error) { + p := DefaultPoolParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return PoolParams{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (pp PoolParams) MarshalJSON() ([]byte, error) { + ppd := poolParamsDisk{ + MaxPoolSize: pp.MaxPoolSize, + PoolSize: pp.PoolSize, + ProxyAttempts: pp.ProxyAttempts, + MaxPings: pp.MaxPings, + ForceConnection: pp.ForceConnection, + } + + return json.Marshal(&ppd) +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (pp *PoolParams) UnmarshalJSON(data []byte) error { + ppDisk := poolParamsDisk{} + err := json.Unmarshal(data, &ppDisk) + if err != nil { + return err + } + + *pp = PoolParams{ + MaxPoolSize: ppDisk.MaxPoolSize, + PoolSize: ppDisk.PoolSize, + ProxyAttempts: ppDisk.ProxyAttempts, + MaxPings: ppDisk.MaxPings, + ForceConnection: ppDisk.ForceConnection, + // Since this does not adhere to json.Marshaler yet, + // file it in manually assuming default values + HostParams: connect.GetDefaultHostParams(), + } + + return nil +} + // newHostPool builds and returns a new HostPool object. func newHostPool(poolParams PoolParams, rng *fastRNG.StreamGenerator, netDef *ndf.NetworkDefinition, getter HostManager, storage storage.Session, diff --git a/cmix/identity/receptionID/store/unknownRounds.go b/cmix/identity/receptionID/store/unknownRounds.go index 022fd252dba2d7dbaaec14a072e5dbac6623ff25..b14c939abdd27e72a284bf37ba71e763b6bd0536 100644 --- a/cmix/identity/receptionID/store/unknownRounds.go +++ b/cmix/identity/receptionID/store/unknownRounds.go @@ -24,21 +24,6 @@ const ( defaultMaxCheck = 3 ) -// UnknownRounds tracks data for unknown rounds. Should adhere to UnknownRounds -// interface. -type UnknownRounds struct { - // Maps an unknown round to how many times the round has been checked - rounds map[id.Round]*uint64 - - // Configurations of UnknownRounds - params UnknownRoundsParams - - // Key Value store to save data to disk - kv *versioned.KV - - mux sync.Mutex -} - // UnknownRoundsParams allows configuration of UnknownRounds parameters. type UnknownRoundsParams struct { // MaxChecks is the maximum amount of checks of a round before that round @@ -49,6 +34,12 @@ type UnknownRoundsParams struct { Stored bool } +// unknownRoundsParamsDisk will be the marshal-able and umarshal-able object. +type unknownRoundsParamsDisk struct { + MaxChecks uint64 + Stored bool +} + // DefaultUnknownRoundsParams returns a default set of UnknownRoundsParams. func DefaultUnknownRoundsParams() UnknownRoundsParams { return UnknownRoundsParams{ @@ -57,6 +48,60 @@ func DefaultUnknownRoundsParams() UnknownRoundsParams { } } +// GetParameters returns the default UnknownRoundsParams, +// or override with given parameters, if set. +func GetParameters(params string) (UnknownRoundsParams, error) { + p := DefaultUnknownRoundsParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return UnknownRoundsParams{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (urp UnknownRoundsParams) MarshalJSON() ([]byte, error) { + urpDisk := unknownRoundsParamsDisk{ + MaxChecks: urp.MaxChecks, + Stored: urp.Stored, + } + + return json.Marshal(&urpDisk) +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (urp *UnknownRoundsParams) UnmarshalJSON(data []byte) error { + urpDisk := unknownRoundsParamsDisk{} + err := json.Unmarshal(data, &urpDisk) + if err != nil { + return err + } + + *urp = UnknownRoundsParams{ + MaxChecks: urpDisk.MaxChecks, + Stored: urpDisk.Stored, + } + + return nil +} + +// UnknownRounds tracks data for unknown rounds. Should adhere to UnknownRounds +// interface. +type UnknownRounds struct { + // Maps an unknown round to how many times the round has been checked + rounds map[id.Round]*uint64 + + // Configurations of UnknownRounds + params UnknownRoundsParams + + // Key Value store to save data to disk + kv *versioned.KV + + mux sync.Mutex +} + // NewUnknownRounds builds and returns a new UnknownRounds object. func NewUnknownRounds(kv *versioned.KV, params UnknownRoundsParams) *UnknownRounds { diff --git a/cmix/message/params.go b/cmix/message/params.go index 9a3a56cd599860416ebe2951048c2c5ac7767987..cd2e6675bff9ad8eb6303b40c32a01f80ba152ea 100644 --- a/cmix/message/params.go +++ b/cmix/message/params.go @@ -1,7 +1,18 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + package message -import "time" +import ( + "encoding/json" + "time" +) +// Params contains the parameters for the message package. type Params struct { MessageReceptionBuffLen uint MessageReceptionWorkerPoolSize uint @@ -10,6 +21,17 @@ type Params struct { RealtimeOnly bool } +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + MessageReceptionBuffLen uint + MessageReceptionWorkerPoolSize uint + MaxChecksInProcessMessage uint + InProcessMessageWait time.Duration + RealtimeOnly bool +} + +// GetDefaultParams returns a Params object containing the +// default parameters. func GetDefaultParams() Params { return Params{ MessageReceptionBuffLen: 500, @@ -19,3 +41,49 @@ func GetDefaultParams() Params { RealtimeOnly: false, } } + +// GetParameters returns the default Params, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := GetDefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{ + MessageReceptionBuffLen: p.MessageReceptionBuffLen, + MessageReceptionWorkerPoolSize: p.MessageReceptionWorkerPoolSize, + MaxChecksInProcessMessage: p.MaxChecksInProcessMessage, + InProcessMessageWait: p.InProcessMessageWait, + RealtimeOnly: p.RealtimeOnly, + } + + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{ + MessageReceptionBuffLen: pDisk.MessageReceptionBuffLen, + MessageReceptionWorkerPoolSize: pDisk.MessageReceptionWorkerPoolSize, + MaxChecksInProcessMessage: pDisk.MaxChecksInProcessMessage, + InProcessMessageWait: pDisk.InProcessMessageWait, + RealtimeOnly: pDisk.RealtimeOnly, + } + + return nil +} diff --git a/cmix/params.go b/cmix/params.go index d823632e199e021606a3414bc2403babcc3d17b9..d8aa778b104dadbdfa0e28215e00f0a10f5b6761 100644 --- a/cmix/params.go +++ b/cmix/params.go @@ -1,3 +1,10 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + package cmix import ( @@ -57,6 +64,26 @@ type Params struct { Historical rounds.Params } +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + TrackNetworkPeriod time.Duration + MaxCheckedRounds uint + RegNodesBufferLen uint + NetworkHealthTimeout time.Duration + ParallelNodeRegistrations uint + KnownRoundsThreshold uint + FastPolling bool + VerboseRoundTracking bool + RealtimeOnly bool + ReplayRequests bool + Rounds rounds.Params + Pickup pickup.Params + Message message.Params + Historical rounds.Params +} + +// GetDefaultParams returns a Params object containing the +// default parameters. func GetDefaultParams() Params { n := Params{ TrackNetworkPeriod: 100 * time.Millisecond, @@ -78,18 +105,7 @@ func GetDefaultParams() Params { return n } -func (n Params) Marshal() ([]byte, error) { - return json.Marshal(n) -} - -func (n Params) SetRealtimeOnlyAll() Params { - n.RealtimeOnly = true - n.Pickup.RealtimeOnly = true - n.Message.RealtimeOnly = true - return n -} - -// GetParameters returns the default network parameters, or override with given +// GetParameters returns the default Params, or override with given // parameters, if set. func GetParameters(params string) (Params, error) { p := GetDefaultParams() @@ -102,7 +118,62 @@ func GetParameters(params string) (Params, error) { return p, nil } -type NodeMap map[id.ID]bool +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{ + TrackNetworkPeriod: p.TrackNetworkPeriod, + MaxCheckedRounds: p.MaxCheckedRounds, + RegNodesBufferLen: p.RegNodesBufferLen, + NetworkHealthTimeout: p.NetworkHealthTimeout, + ParallelNodeRegistrations: p.ParallelNodeRegistrations, + KnownRoundsThreshold: p.KnownRoundsThreshold, + FastPolling: p.FastPolling, + VerboseRoundTracking: p.VerboseRoundTracking, + RealtimeOnly: p.RealtimeOnly, + ReplayRequests: p.ReplayRequests, + Rounds: p.Rounds, + Pickup: p.Pickup, + Message: p.Message, + Historical: p.Historical, + } + + return json.Marshal(&pDisk) +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{ + TrackNetworkPeriod: pDisk.TrackNetworkPeriod, + MaxCheckedRounds: pDisk.MaxCheckedRounds, + RegNodesBufferLen: pDisk.RegNodesBufferLen, + NetworkHealthTimeout: pDisk.NetworkHealthTimeout, + ParallelNodeRegistrations: pDisk.ParallelNodeRegistrations, + KnownRoundsThreshold: pDisk.KnownRoundsThreshold, + FastPolling: pDisk.FastPolling, + VerboseRoundTracking: pDisk.VerboseRoundTracking, + RealtimeOnly: pDisk.RealtimeOnly, + ReplayRequests: pDisk.ReplayRequests, + Rounds: pDisk.Rounds, + Pickup: pDisk.Pickup, + Message: pDisk.Message, + Historical: pDisk.Historical, + } + + return nil +} + +func (p Params) SetRealtimeOnlyAll() Params { + p.RealtimeOnly = true + p.Pickup.RealtimeOnly = true + p.Message.RealtimeOnly = true + return p +} const DefaultDebugTag = "External" @@ -137,6 +208,17 @@ type CMIXParams struct { Critical bool } +// cMixParamsDisk will be the marshal-able and umarshal-able object. +type cMixParamsDisk struct { + RoundTries uint + Timeout time.Duration + RetryDelay time.Duration + SendTimeout time.Duration + DebugTag string + BlacklistedNodes NodeMap + Critical bool +} + func GetDefaultCMIXParams() CMIXParams { return CMIXParams{ RoundTries: 10, @@ -163,6 +245,47 @@ func GetCMIXParameters(params string) (CMIXParams, error) { return p, nil } +// MarshalJSON adheres to the json.Marshaler interface. +func (p CMIXParams) MarshalJSON() ([]byte, error) { + pDisk := cMixParamsDisk{ + RoundTries: p.RoundTries, + Timeout: p.Timeout, + RetryDelay: p.RetryDelay, + SendTimeout: p.SendTimeout, + DebugTag: p.DebugTag, + Critical: p.Critical, + BlacklistedNodes: p.BlacklistedNodes, + } + + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *CMIXParams) UnmarshalJSON(data []byte) error { + pDisk := cMixParamsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = CMIXParams{ + RoundTries: pDisk.RoundTries, + Timeout: pDisk.Timeout, + RetryDelay: pDisk.RetryDelay, + SendTimeout: pDisk.SendTimeout, + DebugTag: pDisk.DebugTag, + Critical: pDisk.Critical, + BlacklistedNodes: pDisk.BlacklistedNodes, + } + + return nil +} + +// NodeMap represents a map of nodes and whether they have been +// blacklisted. This is designed for use with CMIXParams.BlacklistedNodes +type NodeMap map[id.ID]bool + // MarshalJSON adheres to the json.Marshaler interface. func (nm NodeMap) MarshalJSON() ([]byte, error) { stringMap := make(map[string]bool, len(nm)) diff --git a/cmix/params_test.go b/cmix/params_test.go index bb19499b3640c5234ece0b9a14d2cd7789649872..90ece9d5f6ff0dbf23a528832a0adc297425818e 100644 --- a/cmix/params_test.go +++ b/cmix/params_test.go @@ -8,6 +8,7 @@ package cmix import ( + "bytes" "encoding/json" "gitlab.com/xx_network/primitives/id" "reflect" @@ -15,6 +16,43 @@ import ( "time" ) +// Tests that no data is lost when marshaling and +// unmarshaling the Params object. +func TestParams_MarshalUnmarshal(t *testing.T) { + // Construct a set of params + p := GetDefaultParams() + + // Marshal the params + data, err := json.Marshal(&p) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + t.Logf("%s", string(data)) + + // Unmarshal the params object + received := Params{} + err = json.Unmarshal(data, &received) + if err != nil { + t.Fatalf("Unmarshal error: %v", err) + } + + // Re-marshal this params object + data2, err := json.Marshal(received) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + t.Logf("%s", string(data2)) + + // Check that they match (it is done this way to avoid + // false failures with the reflect.DeepEqual function and + // pointers) + if !bytes.Equal(data, data2) { + t.Fatalf("Data was lost in marshal/unmarshal.") + } +} + func TestCMIXParams_JSON_Marshal_Unmarshal(t *testing.T) { p := CMIXParams{ RoundTries: 5, diff --git a/cmix/pickup/params.go b/cmix/pickup/params.go index 95200674d0e0e8a791a0236e1a5dc019b4b59225..99ec4cb835557f411d3d88cdf07acc23e40dba14 100644 --- a/cmix/pickup/params.go +++ b/cmix/pickup/params.go @@ -1,7 +1,18 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + package pickup -import "time" +import ( + "encoding/json" + "time" +) +// Params contains the parameters for the pickup package. type Params struct { // Number of worker threads for retrieving messages from gateways NumMessageRetrievalWorkers uint @@ -31,6 +42,19 @@ type Params struct { ForceHistoricalRounds bool } +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + NumMessageRetrievalWorkers uint + LookupRoundsBufferLen uint + MaxHistoricalRoundsRetries uint + UncheckRoundPeriod time.Duration + ForceMessagePickupRetry bool + SendTimeout time.Duration + RealtimeOnly bool + ForceHistoricalRounds bool +} + +// GetDefaultParams returns a default set of Params. func GetDefaultParams() Params { return Params{ NumMessageRetrievalWorkers: 8, @@ -42,3 +66,55 @@ func GetDefaultParams() Params { RealtimeOnly: false, } } + +// GetParameters returns the default Params, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := GetDefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{ + NumMessageRetrievalWorkers: p.NumMessageRetrievalWorkers, + LookupRoundsBufferLen: p.LookupRoundsBufferLen, + MaxHistoricalRoundsRetries: p.MaxHistoricalRoundsRetries, + UncheckRoundPeriod: p.UncheckRoundPeriod, + ForceMessagePickupRetry: p.ForceMessagePickupRetry, + SendTimeout: p.SendTimeout, + RealtimeOnly: p.RealtimeOnly, + ForceHistoricalRounds: p.ForceHistoricalRounds, + } + + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{ + NumMessageRetrievalWorkers: pDisk.NumMessageRetrievalWorkers, + LookupRoundsBufferLen: pDisk.LookupRoundsBufferLen, + MaxHistoricalRoundsRetries: pDisk.MaxHistoricalRoundsRetries, + UncheckRoundPeriod: pDisk.UncheckRoundPeriod, + ForceMessagePickupRetry: pDisk.ForceMessagePickupRetry, + SendTimeout: pDisk.SendTimeout, + RealtimeOnly: pDisk.RealtimeOnly, + ForceHistoricalRounds: pDisk.ForceHistoricalRounds, + } + + return nil +} diff --git a/cmix/rounds/params.go b/cmix/rounds/params.go index 1023f6022acce31a971747c5f31bfebdec724c03..dd9596c2fcf6809010ceb9fbeda609f2ef3b6645 100644 --- a/cmix/rounds/params.go +++ b/cmix/rounds/params.go @@ -1,7 +1,18 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + package rounds -import "time" +import ( + "encoding/json" + "time" +) +// Params contains the parameters for the rounds package. type Params struct { // MaxHistoricalRounds is the number of historical rounds required to // automatically send a historical rounds query. @@ -20,6 +31,15 @@ type Params struct { MaxHistoricalRoundsRetries uint } +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + MaxHistoricalRounds uint + HistoricalRoundsPeriod time.Duration + HistoricalRoundsBufferLen uint + MaxHistoricalRoundsRetries uint +} + +// GetDefaultParams returns a default set of Params. func GetDefaultParams() Params { return Params{ MaxHistoricalRounds: 100, @@ -28,3 +48,47 @@ func GetDefaultParams() Params { MaxHistoricalRoundsRetries: 3, } } + +// GetParameters returns the default Params, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := GetDefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (r Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{ + MaxHistoricalRounds: r.MaxHistoricalRounds, + HistoricalRoundsPeriod: r.HistoricalRoundsPeriod, + HistoricalRoundsBufferLen: r.HistoricalRoundsBufferLen, + MaxHistoricalRoundsRetries: r.MaxHistoricalRoundsRetries, + } + + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (r *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *r = Params{ + MaxHistoricalRounds: pDisk.MaxHistoricalRounds, + HistoricalRoundsPeriod: pDisk.HistoricalRoundsPeriod, + HistoricalRoundsBufferLen: pDisk.HistoricalRoundsBufferLen, + MaxHistoricalRoundsRetries: pDisk.MaxHistoricalRoundsRetries, + } + + return nil +} diff --git a/connect/connect.go b/connect/connect.go index e5c544672de6358aac88a5fbe331880c50f74a43..310ef304a868f769068ac1d8d95b82ceb28b2945 100644 --- a/connect/connect.go +++ b/connect/connect.go @@ -7,6 +7,7 @@ package connect import ( + "encoding/json" "io" "time" @@ -84,9 +85,9 @@ type Callback func(connection Connection) // Params for managing Connection objects. type Params struct { - Auth auth.Param + Auth auth.Params Rekey rekey.Params - Event event.Reporter + Event event.Reporter `json:"-"` Timeout time.Duration } @@ -100,6 +101,19 @@ func GetDefaultParams() Params { } } +// GetParameters returns the default Params, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := GetDefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + // Connect performs auth key negotiation with the given recipient, // and returns a Connection object for the newly-created partner.Manager // This function is to be used sender-side and will block until the diff --git a/connect/params_test.go b/connect/params_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5501ad55153af983a72df2b0ab4d1e8b64358628 --- /dev/null +++ b/connect/params_test.go @@ -0,0 +1,49 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +package connect + +import ( + "bytes" + "encoding/json" + "testing" +) + +func TestParams_MarshalUnmarshal(t *testing.T) { + // Construct a set of params + p := GetDefaultParams() + + // Marshal the params + data, err := json.Marshal(&p) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + t.Logf("%s", string(data)) + + // Unmarshal the params object + received := Params{} + err = json.Unmarshal(data, &received) + if err != nil { + t.Fatalf("Unmarshal error: %v", err) + } + + // Re-marshal this params object + data2, err := json.Marshal(received) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + t.Logf("%s", string(data2)) + + // Check that they match (it is done this way to avoid + // false failures with the reflect.DeepEqual function and + // pointers) + if !bytes.Equal(data, data2) { + t.Fatalf("Data was lost in marshal/unmarshal.") + } +} diff --git a/e2e/params.go b/e2e/params.go index 23052727ffcc103f62e14c5d41fad2fbcabfe244..2702d490656342e975883177b115f63dbf2df961 100644 --- a/e2e/params.go +++ b/e2e/params.go @@ -29,6 +29,17 @@ type Params struct { cmix.CMIXParams } +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + ServiceTag string + LastServiceTag string + KeyGetRetryCount uint + KeyGeRetryDelay time.Duration + Rekey bool + cmix.CMIXParams +} + +// GetDefaultParams returns a default set of Params. func GetDefaultParams() Params { return Params{ ServiceTag: catalog.Silent, @@ -41,12 +52,9 @@ func GetDefaultParams() Params { CMIXParams: cmix.GetDefaultCMIXParams(), } } -func (e Params) Marshal() ([]byte, error) { - return json.Marshal(e) -} -// GetParameters Obtain default E2E parameters, or override with -// given parameters if set +// GetParameters Obtain default Params, or override with +// given parameters if set. func GetParameters(params string) (Params, error) { p := GetDefaultParams() if len(params) > 0 { @@ -57,3 +65,38 @@ func GetParameters(params string) (Params, error) { } return p, nil } + +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{ + ServiceTag: p.ServiceTag, + LastServiceTag: p.LastServiceTag, + KeyGetRetryCount: p.KeyGetRetryCount, + KeyGeRetryDelay: p.KeyGeRetryDelay, + Rekey: p.Rekey, + CMIXParams: p.CMIXParams, + } + + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{ + ServiceTag: pDisk.ServiceTag, + LastServiceTag: pDisk.LastServiceTag, + KeyGetRetryCount: pDisk.KeyGetRetryCount, + KeyGeRetryDelay: pDisk.KeyGeRetryDelay, + Rekey: pDisk.Rekey, + CMIXParams: pDisk.CMIXParams, + } + + return nil +} diff --git a/e2e/ratchet/partner/session/params.go b/e2e/ratchet/partner/session/params.go index 8ccd454947ff281471cd77d79fa15ea826efec9d..2782578e9e605d117d2bb872f40822816c0fde2f 100644 --- a/e2e/ratchet/partner/session/params.go +++ b/e2e/ratchet/partner/session/params.go @@ -1,6 +1,20 @@ package session -import "fmt" +import ( + "encoding/json" + "fmt" +) + +// DEFAULT KEY GENERATION PARAMETERS +// Hardcoded limits for keys +// sets the number of keys very high, but with a low rekey threshold. In this case, if the other party is online, you will read +const ( + minKeys uint16 = 1000 + maxKeys uint16 = 2000 + rekeyThrshold float64 = 0.05 + numReKeys uint16 = 16 + rekeyRatio float64 = 1 / 10 +) type Params struct { // using the DH as a seed, both sides finalizeKeyNegotation a number @@ -20,17 +34,16 @@ type Params struct { UnconfirmedRetryRatio float64 } -// DEFAULT KEY GENERATION PARAMETERS -// Hardcoded limits for keys -// sets the number of keys very high, but with a low rekey threshold. In this case, if the other party is online, you will read -const ( - minKeys uint16 = 1000 - maxKeys uint16 = 2000 - rekeyThrshold float64 = 0.05 - numReKeys uint16 = 16 - rekeyRatio float64 = 1 / 10 -) +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + MinKeys uint16 + MaxKeys uint16 + RekeyThreshold float64 + NumRekeys uint16 + UnconfirmedRetryRatio float64 +} +// GetDefaultParams returns a default set of Params. func GetDefaultParams() Params { return Params{ MinKeys: minKeys, @@ -41,6 +54,50 @@ func GetDefaultParams() Params { } } +// GetParameters returns the default Params, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := GetDefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{ + MinKeys: p.MinKeys, + MaxKeys: p.MaxKeys, + RekeyThreshold: p.RekeyThreshold, + NumRekeys: p.NumRekeys, + UnconfirmedRetryRatio: p.UnconfirmedRetryRatio, + } + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{ + MinKeys: pDisk.MinKeys, + MaxKeys: pDisk.MaxKeys, + RekeyThreshold: pDisk.RekeyThreshold, + NumRekeys: pDisk.NumRekeys, + UnconfirmedRetryRatio: pDisk.UnconfirmedRetryRatio, + } + return nil +} + func (p Params) String() string { return fmt.Sprintf("SessionParams{ MinKeys: %d, MaxKeys: %d, NumRekeys: %d }", p.MinKeys, p.MaxKeys, p.NumRekeys) diff --git a/e2e/rekey/params.go b/e2e/rekey/params.go index 0611e67e84b4875bae35f29855fbddc0e007fe83..274770c112329cb552a85df55c6abc6d273cf67c 100644 --- a/e2e/rekey/params.go +++ b/e2e/rekey/params.go @@ -1,6 +1,7 @@ package rekey import ( + "encoding/json" "gitlab.com/elixxir/client/catalog" "time" ) @@ -22,6 +23,17 @@ type Params struct { StoppableName string } +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + RoundTimeout time.Duration + TriggerName string + Trigger catalog.MessageType + ConfirmName string + Confirm catalog.MessageType + StoppableName string +} + +// GetDefaultParams returns a default set of Params. func GetDefaultParams() Params { return Params{ RoundTimeout: time.Minute, @@ -33,6 +45,8 @@ func GetDefaultParams() Params { } } +// GetDefaultEphemeralParams returns a default set of Params for +// ephemeral re-keying. func GetDefaultEphemeralParams() Params { p := GetDefaultParams() p.TriggerName = keyExchangeTriggerEphemeralName @@ -42,3 +56,50 @@ func GetDefaultEphemeralParams() Params { p.StoppableName = keyExchangeEphemeralMulti return p } + +// GetParameters returns the default network parameters, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := GetDefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{ + RoundTimeout: p.RoundTimeout, + TriggerName: p.TriggerName, + Trigger: p.Trigger, + ConfirmName: p.ConfirmName, + Confirm: p.Confirm, + StoppableName: p.StoppableName, + } + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{ + RoundTimeout: pDisk.RoundTimeout, + TriggerName: pDisk.TriggerName, + Trigger: pDisk.Trigger, + ConfirmName: pDisk.ConfirmName, + Confirm: pDisk.Confirm, + StoppableName: pDisk.StoppableName, + } + + return nil +} diff --git a/fileTransfer/params.go b/fileTransfer/params.go index 754bb88db3d45c75f3e47454f45bf4cf6170e830..8e54d5d02c9430224f526008d9138cb35e08e1ce 100644 --- a/fileTransfer/params.go +++ b/fileTransfer/params.go @@ -7,7 +7,10 @@ package fileTransfer -import "time" +import ( + "encoding/json" + "time" +) const ( defaultMaxThroughput = 150_000 // 150 kB per second @@ -26,6 +29,12 @@ type Params struct { SendTimeout time.Duration } +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + MaxThroughput int + SendTimeout time.Duration +} + // DefaultParams returns a Params object filled with the default values. func DefaultParams() Params { return Params{ @@ -33,3 +42,42 @@ func DefaultParams() Params { SendTimeout: defaultSendTimeout, } } + +// GetParameters returns the default network parameters, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := DefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{ + MaxThroughput: p.MaxThroughput, + SendTimeout: p.SendTimeout, + } + + return json.Marshal(&pDisk) +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{ + MaxThroughput: pDisk.MaxThroughput, + SendTimeout: pDisk.SendTimeout, + } + + return nil +} diff --git a/fileTransfer2/connect/params.go b/fileTransfer2/connect/params.go index 3b502637e895a852b8ab11c0118d0806659c073a..93a3a44a1f6a6d001724875bd85ba5fd13374a54 100644 --- a/fileTransfer2/connect/params.go +++ b/fileTransfer2/connect/params.go @@ -7,6 +7,10 @@ package connect +import ( + "encoding/json" +) + const ( defaultNotifyUponCompletion = true ) @@ -18,9 +22,47 @@ type Params struct { NotifyUponCompletion bool } +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + NotifyUponCompletion bool +} + // DefaultParams returns a Params object filled with the default values. func DefaultParams() Params { return Params{ NotifyUponCompletion: defaultNotifyUponCompletion, } } + +// GetParameters returns the default Params, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := DefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{NotifyUponCompletion: p.NotifyUponCompletion} + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{NotifyUponCompletion: pDisk.NotifyUponCompletion} + + return nil +} diff --git a/fileTransfer2/e2e/params.go b/fileTransfer2/e2e/params.go index 254e7d3a40777ec48c8ad33c89b95ccacaf5d2d9..819e963f5fd9efc3d058b5a953e87c7aa8874f1f 100644 --- a/fileTransfer2/e2e/params.go +++ b/fileTransfer2/e2e/params.go @@ -7,6 +7,8 @@ package e2e +import "encoding/json" + const ( defaultNotifyUponCompletion = true ) @@ -18,9 +20,47 @@ type Params struct { NotifyUponCompletion bool } +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + NotifyUponCompletion bool +} + // DefaultParams returns a Params object filled with the default values. func DefaultParams() Params { return Params{ NotifyUponCompletion: defaultNotifyUponCompletion, } } + +// GetParameters returns the default Params, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := DefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{NotifyUponCompletion: p.NotifyUponCompletion} + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{NotifyUponCompletion: pDisk.NotifyUponCompletion} + + return nil +} diff --git a/fileTransfer2/params.go b/fileTransfer2/params.go index b4f1587bec6584e143b65d70d572cf740921bbb4..4a70046560b0e2793620f8bf29d5c1c62078b6f1 100644 --- a/fileTransfer2/params.go +++ b/fileTransfer2/params.go @@ -8,6 +8,7 @@ package fileTransfer2 import ( + "encoding/json" "gitlab.com/elixxir/client/cmix" "time" ) @@ -32,6 +33,13 @@ type Params struct { Cmix cmix.CMIXParams } +// paramsDisk will be the marshal-able and umarshal-able object. +type paramsDisk struct { + MaxThroughput int + SendTimeout time.Duration + Cmix cmix.CMIXParams +} + // DefaultParams returns a Params object filled with the default values. func DefaultParams() Params { return Params{ @@ -40,3 +48,45 @@ func DefaultParams() Params { Cmix: cmix.GetDefaultCMIXParams(), } } + +// GetParameters returns the default network parameters, or override with given +// parameters, if set. +func GetParameters(params string) (Params, error) { + p := DefaultParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return Params{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (p Params) MarshalJSON() ([]byte, error) { + pDisk := paramsDisk{ + MaxThroughput: p.MaxThroughput, + SendTimeout: p.SendTimeout, + Cmix: p.Cmix, + } + + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (p *Params) UnmarshalJSON(data []byte) error { + pDisk := paramsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *p = Params{ + MaxThroughput: pDisk.MaxThroughput, + SendTimeout: pDisk.SendTimeout, + Cmix: pDisk.Cmix, + } + + return nil +} diff --git a/fileTransfer2/params_test.go b/fileTransfer2/params_test.go index 849db501a81c9639e12890f8337a55ef85165263..8bbeb942b4f1c5f1d70e418ebf1c5f796dd84d4a 100644 --- a/fileTransfer2/params_test.go +++ b/fileTransfer2/params_test.go @@ -8,11 +8,51 @@ package fileTransfer2 import ( + "bytes" + "encoding/json" "gitlab.com/elixxir/client/cmix" "reflect" "testing" ) +// Tests that no data is lost when marshaling and +// unmarshaling the Params object. +func TestParams_MarshalUnmarshal(t *testing.T) { + // Construct a set of params + p := DefaultParams() + + // Marshal the params + data, err := json.Marshal(&p) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + t.Logf("%s", string(data)) + + // Unmarshal the params object + received := Params{} + err = json.Unmarshal(data, &received) + if err != nil { + t.Fatalf("Unmarshal error: %v", err) + } + + // Re-marshal this params object + data2, err := json.Marshal(received) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + t.Logf("%s", string(data2)) + + // Check that they match (it is done this way to avoid + // false failures with the reflect.DeepEqual function and + // pointers) + if !bytes.Equal(data, data2) { + t.Fatalf("Data was lost in marshal/unmarshal.") + } + +} + // Tests that DefaultParams returns a Params object with the expected defaults. func TestDefaultParams(t *testing.T) { expected := Params{ diff --git a/single/params.go b/single/params.go index 10d60a908022f6d7c0e4857133a8e7a7b8444306..8895818449ba626ea69a112226a8afd182cdc5f3 100644 --- a/single/params.go +++ b/single/params.go @@ -8,6 +8,7 @@ package single import ( + "encoding/json" "gitlab.com/elixxir/client/cmix" "time" ) @@ -32,6 +33,13 @@ type RequestParams struct { CmixParams cmix.CMIXParams } +// requestParamsDisk will be the marshal-able and umarshal-able object. +type requestParamsDisk struct { + Timeout time.Duration + MaxResponseMessages uint8 + CmixParams cmix.CMIXParams +} + // GetDefaultRequestParams returns a RequestParams with the default // configuration. func GetDefaultRequestParams() RequestParams { @@ -41,3 +49,45 @@ func GetDefaultRequestParams() RequestParams { CmixParams: cmix.GetDefaultCMIXParams(), } } + +// GetParameters returns the default network parameters, or override with given +// parameters, if set. +func GetParameters(params string) (RequestParams, error) { + p := GetDefaultRequestParams() + if len(params) > 0 { + err := json.Unmarshal([]byte(params), &p) + if err != nil { + return RequestParams{}, err + } + } + return p, nil +} + +// MarshalJSON adheres to the json.Marshaler interface. +func (rp RequestParams) MarshalJSON() ([]byte, error) { + pDisk := requestParamsDisk{ + Timeout: rp.Timeout, + MaxResponseMessages: rp.MaxResponseMessages, + CmixParams: rp.CmixParams, + } + + return json.Marshal(&pDisk) + +} + +// UnmarshalJSON adheres to the json.Unmarshaler interface. +func (rp *RequestParams) UnmarshalJSON(data []byte) error { + pDisk := requestParamsDisk{} + err := json.Unmarshal(data, &pDisk) + if err != nil { + return err + } + + *rp = RequestParams{ + Timeout: pDisk.Timeout, + MaxResponseMessages: pDisk.MaxResponseMessages, + CmixParams: pDisk.CmixParams, + } + + return nil +} diff --git a/single/params_test.go b/single/params_test.go index 39f56c80c888e1496dffebb2b9f60d8951cd055e..7d8224c6127366af542baecb2e755475311967a0 100644 --- a/single/params_test.go +++ b/single/params_test.go @@ -8,11 +8,42 @@ package single import ( + "bytes" + "encoding/json" "gitlab.com/elixxir/client/cmix" "reflect" "testing" ) +// Tests that no data is lost when marshaling and +// unmarshaling the RequestParams object. +func TestParams_MarshalUnmarshal(t *testing.T) { + p := GetDefaultRequestParams() + data, err := json.Marshal(p) + if err != nil { + t.Fatalf("Marshal error: %+v", err) + } + + t.Logf("%s", string(data)) + + received := RequestParams{} + err = json.Unmarshal(data, &received) + if err != nil { + t.Fatalf("Unmarshal error: %+v", err) + } + + data2, err := json.Marshal(received) + if err != nil { + t.Fatalf("Marshal error: %+v", err) + } + + t.Logf("%s", string(data2)) + + if !bytes.Equal(data, data2) { + t.Fatalf("Data was lost in marshal/unmarshal.") + } +} + // Tests that GetDefaultRequestParams returns a RequestParams with the expected // default values. func TestGetDefaultRequestParams(t *testing.T) {