From ceb55410d64f9b7326be296729b951ab41f97afa Mon Sep 17 00:00:00 2001 From: Adrian-Stefan Mares Date: Thu, 14 Jul 2022 19:30:58 +0300 Subject: [PATCH] ns: Use minimalistic recent downlink representation --- pkg/networkserver/downlink.go | 26 ++++-- pkg/networkserver/downlink_internal_test.go | 82 ++++++++++++------- pkg/networkserver/downlink_test.go | 76 ++++++++--------- pkg/networkserver/internal/utils.go | 2 +- .../networkserver_util_internal_test.go | 24 +----- 5 files changed, 114 insertions(+), 96 deletions(-) diff --git a/pkg/networkserver/downlink.go b/pkg/networkserver/downlink.go index da6be3ef1a9..dbeb70a83e6 100644 --- a/pkg/networkserver/downlink.go +++ b/pkg/networkserver/downlink.go @@ -1065,11 +1065,27 @@ func loggerWithDownlinkSchedulingErrorFields(logger log.Interface, errs downlink return logger.WithFields(log.Fields(pairs...)) } -func appendRecentDownlink(recent []*ttnpb.DownlinkMessage, down *ttnpb.DownlinkMessage, window int) []*ttnpb.DownlinkMessage { +func toMACStateDownlinkMessages(downs ...*ttnpb.DownlinkMessage) []*ttnpb.MACState_DownlinkMessage { + if len(downs) == 0 { + return nil + } + recentDowns := make([]*ttnpb.MACState_DownlinkMessage, 0, len(downs)) + for _, down := range downs { + recentDowns = append(recentDowns, &ttnpb.MACState_DownlinkMessage{ + Payload: down.Payload, + CorrelationIds: down.CorrelationIds, + }) + } + return recentDowns +} + +func appendRecentDownlink( + recent []*ttnpb.MACState_DownlinkMessage, down *ttnpb.DownlinkMessage, window int, +) []*ttnpb.MACState_DownlinkMessage { if n := len(recent); n > 0 { recent[n-1].CorrelationIds = nil } - recent = append(recent, down) + recent = append(recent, toMACStateDownlinkMessages(down)...) if extra := len(recent) - window; extra > 0 { recent = recent[extra:] } @@ -1195,11 +1211,7 @@ func recordDataDownlink(dev *ttnpb.EndDevice, genState generateDownlinkState, ne dev.MacState.PendingApplicationDownlink = genState.ApplicationDownlink dev.Session.LastConfFCntDown = macPayload.FullFCnt } - dev.MacState.RecentDownlinks = appendRecentDownlink(dev.MacState.RecentDownlinks, &ttnpb.DownlinkMessage{ - Payload: down.Message.Payload, - Settings: down.Message.Settings, - CorrelationIds: down.Message.CorrelationIds, - }, recentDownlinkCount) + dev.MacState.RecentDownlinks = appendRecentDownlink(dev.MacState.RecentDownlinks, down.Message, recentDownlinkCount) dev.MacState.RxWindowsAvailable = false } diff --git a/pkg/networkserver/downlink_internal_test.go b/pkg/networkserver/downlink_internal_test.go index e1e53a50ec8..c03e0abb5aa 100644 --- a/pkg/networkserver/downlink_internal_test.go +++ b/pkg/networkserver/downlink_internal_test.go @@ -44,49 +44,75 @@ import ( ) func TestAppendRecentDownlink(t *testing.T) { - downs := [...]*ttnpb.DownlinkMessage{ + downs := [...]*ttnpb.MACState_DownlinkMessage{ { - RawPayload: []byte("test1"), + Payload: &ttnpb.Message{ + Mic: []byte{0x01}, + }, }, { - RawPayload: []byte("test2"), + Payload: &ttnpb.Message{ + Mic: []byte{0x02}, + }, }, { - RawPayload: []byte("test3"), + Payload: &ttnpb.Message{ + Mic: []byte{0x03}, + }, }, } for _, tc := range []struct { - Recent []*ttnpb.DownlinkMessage + Recent []*ttnpb.MACState_DownlinkMessage Down *ttnpb.DownlinkMessage Window int - Expected []*ttnpb.DownlinkMessage + Expected []*ttnpb.MACState_DownlinkMessage }{ { - Down: downs[0], + Down: &ttnpb.DownlinkMessage{ + Payload: &ttnpb.Message{ + Mic: []byte{0x01}, + }, + }, Window: 1, Expected: downs[:1], }, { - Recent: downs[:1], - Down: downs[1], + Recent: downs[:1], + Down: &ttnpb.DownlinkMessage{ + Payload: &ttnpb.Message{ + Mic: []byte{0x02}, + }, + }, Window: 1, Expected: downs[1:2], }, { - Recent: downs[:2], - Down: downs[2], + Recent: downs[:2], + Down: &ttnpb.DownlinkMessage{ + Payload: &ttnpb.Message{ + Mic: []byte{0x03}, + }, + }, Window: 1, Expected: downs[2:3], }, { - Recent: downs[:1], - Down: downs[1], + Recent: downs[:1], + Down: &ttnpb.DownlinkMessage{ + Payload: &ttnpb.Message{ + Mic: []byte{0x02}, + }, + }, Window: 2, Expected: downs[:2], }, { - Recent: downs[:2], - Down: downs[2], + Recent: downs[:2], + Down: &ttnpb.DownlinkMessage{ + Payload: &ttnpb.Message{ + Mic: []byte{0x03}, + }, + }, Window: 2, Expected: downs[1:3], }, @@ -96,7 +122,7 @@ func TestAppendRecentDownlink(t *testing.T) { Name: fmt.Sprintf("recent_length:%d,window:%v", len(tc.Recent), tc.Window), Parallel: true, Func: func(ctx context.Context, t *testing.T, a *assertions.Assertion) { - recent := CopyDownlinkMessages(tc.Recent...) + recent := deepcopy.Copy(tc.Recent).([]*ttnpb.MACState_DownlinkMessage) down := CopyDownlinkMessage(tc.Down) ret := appendRecentDownlink(recent, down, tc.Window) a.So(recent, should.Resemble, tc.Recent) @@ -284,12 +310,12 @@ func TestGenerateDataDownlink(t *testing.T) { }, }, }}, - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -353,12 +379,12 @@ func TestGenerateDataDownlink(t *testing.T) { }, }, }}, - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -902,12 +928,12 @@ func TestGenerateDataDownlink(t *testing.T) { }}, }, }}, - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), }, Session: &ttnpb.Session{ DevAddr: devAddr.Bytes(), @@ -984,12 +1010,12 @@ func TestGenerateDataDownlink(t *testing.T) { }}, }, }}, - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), }, Session: &ttnpb.Session{ DevAddr: devAddr.Bytes(), @@ -1036,12 +1062,12 @@ func TestGenerateDataDownlink(t *testing.T) { }}, }, }}, - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), }, Session: &ttnpb.Session{ DevAddr: devAddr.Bytes(), @@ -1116,12 +1142,12 @@ func TestGenerateDataDownlink(t *testing.T) { }}, }, }}, - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), }, Session: &ttnpb.Session{ DevAddr: devAddr.Bytes(), diff --git a/pkg/networkserver/downlink_test.go b/pkg/networkserver/downlink_test.go index ffa6b9efaf3..4e5c49eecf0 100644 --- a/pkg/networkserver/downlink_test.go +++ b/pkg/networkserver/downlink_test.go @@ -391,12 +391,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() / 10), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_0_3, }), - }, + ), }, Session: &ttnpb.Session{ DevAddr: test.DefaultDevAddr.Bytes(), @@ -422,12 +422,12 @@ func TestProcessDownlinkTask(t *testing.T) { DeviceClass: ttnpb.Class_CLASS_A, LorawanVersion: ttnpb.MACVersion_MAC_V1_1, RxWindowsAvailable: true, - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_0_3, }), - }, + ), }, Session: &ttnpb.Session{ DevAddr: test.DefaultDevAddr.Bytes(), @@ -502,12 +502,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() - time.Second - time.Nanosecond), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_0_3, }), - }, + ), }, MacSettings: &ttnpb.MACSettings{ StatusTimePeriodicity: ttnpb.ProtoDurationPtr(0), @@ -550,12 +550,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() / 10), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_0_3, }), - }, + ), RxWindowsAvailable: true, }, MacSettings: &ttnpb.MACSettings{ @@ -599,12 +599,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() / 10), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_0_3, }), - }, + ), RxWindowsAvailable: true, }, MacSettings: &ttnpb.MACSettings{ @@ -684,12 +684,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() / 10), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_0_3, }), - }, + ), RxWindowsAvailable: true, }, MacSettings: &ttnpb.MACSettings{ @@ -769,12 +769,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() / 10), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -920,12 +920,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() / 10), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -1081,12 +1081,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() / 10), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -1236,12 +1236,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() / 10), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -1391,12 +1391,12 @@ func TestProcessDownlinkTask(t *testing.T) { }, }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), }, Session: &ttnpb.Session{ DevAddr: test.DefaultDevAddr.Bytes(), @@ -1542,12 +1542,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() / 10), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -1705,12 +1705,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() - time.Second - time.Nanosecond), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -1834,12 +1834,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() - time.Second), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -1972,12 +1972,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration()), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), }, Session: &ttnpb.Session{ DevAddr: test.DefaultDevAddr.Bytes(), @@ -2132,12 +2132,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-DefaultEU868RX1Delay.Duration() - time.Second), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), }, Session: &ttnpb.Session{ DevAddr: test.DefaultDevAddr.Bytes(), @@ -2274,12 +2274,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-time.Second), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -2336,12 +2336,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-time.Second), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), RxWindowsAvailable: true, }, Session: &ttnpb.Session{ @@ -2420,12 +2420,12 @@ func TestProcessDownlinkTask(t *testing.T) { ReceivedAt: now.Add(-time.Second), }), ), - RecentDownlinks: []*ttnpb.DownlinkMessage{ + RecentDownlinks: ToMACStateDownlinkMessages( MakeDataDownlink(&DataDownlinkConfig{ DecodePayload: true, MACVersion: ttnpb.MACVersion_MAC_V1_1, }), - }, + ), }, Session: &ttnpb.Session{ DevAddr: test.DefaultDevAddr.Bytes(), diff --git a/pkg/networkserver/internal/utils.go b/pkg/networkserver/internal/utils.go index 9714cb595e7..8821fdd51c4 100644 --- a/pkg/networkserver/internal/utils.go +++ b/pkg/networkserver/internal/utils.go @@ -91,7 +91,7 @@ func LastUplink(ups ...*ttnpb.MACState_UplinkMessage) *ttnpb.MACState_UplinkMess return ups[len(ups)-1] } -func LastDownlink(downs ...*ttnpb.DownlinkMessage) *ttnpb.DownlinkMessage { +func LastDownlink(downs ...*ttnpb.MACState_DownlinkMessage) *ttnpb.MACState_DownlinkMessage { return downs[len(downs)-1] } diff --git a/pkg/networkserver/networkserver_util_internal_test.go b/pkg/networkserver/networkserver_util_internal_test.go index 7cedf2b726d..b93597304a0 100644 --- a/pkg/networkserver/networkserver_util_internal_test.go +++ b/pkg/networkserver/networkserver_util_internal_test.go @@ -62,6 +62,7 @@ const ( ) var ( + ToMACStateDownlinkMessages = toMACStateDownlinkMessages AppendRecentDownlink = appendRecentDownlink ToMACStateRxMetadata = toMACStateRxMetadata ToMACStateUplinkMessages = toMACStateUplinkMessages @@ -2184,8 +2185,7 @@ func (o EndDeviceOptionNamespace) SendJoinAccept(priority ttnpb.TxSchedulePriori MACStateOptions.WithPendingJoinRequest(x.PendingMacState.QueuedJoinAccept.Request), MACStateOptions.WithQueuedJoinAccept(nil), MACStateOptions.WithRxWindowsAvailable(false), - MACStateOptions.AppendRecentDownlinks(&ttnpb.DownlinkMessage{ - RawPayload: x.PendingMacState.QueuedJoinAccept.Payload, + MACStateOptions.AppendRecentDownlinks(&ttnpb.MACState_DownlinkMessage{ Payload: &ttnpb.Message{ MHdr: &ttnpb.MHDR{ MType: ttnpb.MType_JOIN_ACCEPT, @@ -2201,26 +2201,6 @@ func (o EndDeviceOptionNamespace) SendJoinAccept(priority ttnpb.TxSchedulePriori }, }, }, - EndDeviceIds: x.Ids, - Settings: &ttnpb.DownlinkMessage_Request{ - Request: &ttnpb.TxRequest{ - Class: ttnpb.Class_CLASS_A, - Priority: priority, - FrequencyPlanId: x.FrequencyPlanId, - Rx1Delay: ttnpb.RxDelay(Band(x.FrequencyPlanId, x.LorawanPhyVersion).JoinAcceptDelay1 / time.Second), - Rx2DataRate: &ttnpb.DataRate{ - Modulation: &ttnpb.DataRate_Lora{ - Lora: &ttnpb.LoRaDataRate{ - Bandwidth: 125000, - SpreadingFactor: 12 - uint32(x.PendingMacState.CurrentParameters.Rx2DataRateIndex), - }, - }, - }, - Rx2Frequency: x.PendingMacState.CurrentParameters.Rx2Frequency, - // TODO: Generate RX1 transmission parameters if necessary. - // https://github.com/TheThingsNetwork/lorawan-stack/issues/3142 - }, - }, CorrelationIds: []string{"join-accept"}, }), ),