Repository: ava-labs/avalanchego (Go)

Issue:
API returns wrong control keys data after subnet ownership transfer
**Describe the bug**
When subnet ownership has been transferred over, `platform.getSubnets()` API call shows the old data unless selecting for the subent specifically using the `ids` param.

**To Reproduce**
Curl-ing with a payload of:
```json
{
    "jsonrpc": "2.0",
    "method": "platform.getSubnets",
    "params": {},
    "id": 1
}
```
gives the list of all the subnets, among others it has:
```json
            {
                "id": "HDHyjxQi3fnMDmuEGZzEx33rbGe6Yz1dz2WGgEit6jEuTbxdj",
                "controlKeys": [
                    "P-fuji1ywzvrftfqexh5g6qa9zyrytj6pqdfetza2hqln"
                ],
                "threshold": "1"
            },
```
But calling with
```json
{
    "jsonrpc": "2.0",
    "method": "platform.getSubnets",
    "params": {"ids":["HDHyjxQi3fnMDmuEGZzEx33rbGe6Yz1dz2WGgEit6jEuTbxdj"]},
    "id": 1
}
```
returns the correct control key:
```json
            {
                "id": "HDHyjxQi3fnMDmuEGZzEx33rbGe6Yz1dz2WGgEit6jEuTbxdj",
                "controlKeys": [
                    "P-fuji1mlhaxu92c5p57ce56trsc7eha6cflh20cfy4gn"
                ],
                "threshold": "1"
            }
```
**Expected behavior**
The API call should return the correct data in both cases.

**Operating System**
Node running on Ubuntu. Version info:
```json
{
    "jsonrpc": "2.0",
    "result": {
        "version": "avalanchego/1.11.4",
        "databaseVersion": "v1.4.5",
        "rpcProtocolVersion": "35",
        "gitCommit": "e8904ae11aa144ae601bb9795682d11c3fb178ee",
        "vmVersions": {
            "avm": "v1.11.4",
            "evm": "v0.13.2",
            "platform": "v1.11.4"
        }
    },
    "id": 1
}
```



Discussion:

Patch:
diff --git a/vms/platformvm/service.go b/vms/platformvm/service.go
index dfe970a88b0a..fe874c2d1d05 100644
--- a/vms/platformvm/service.go
+++ b/vms/platformvm/service.go
@@ -544,15 +544,23 @@ func (s *Service) GetSubnets(_ *http.Request, args *GetSubnetsArgs, response *Ge
 				continue
 			}
 
-			unsignedTx := subnet.Unsigned.(*txs.CreateSubnetTx)
-			owner := unsignedTx.Owner.(*secp256k1fx.OutputOwners)
-			controlAddrs := []string{}
-			for _, controlKeyID := range owner.Addrs {
+			subnetOwner, err := s.vm.state.GetSubnetOwner(subnetID)
+			if err != nil {
+				return err
+			}
+
+			owner, ok := subnetOwner.(*secp256k1fx.OutputOwners)
+			if !ok {
+				return fmt.Errorf("expected *secp256k1fx.OutputOwners but got %T", subnetOwner)
+			}
+
+			controlAddrs := make([]string, len(owner.Addrs))
+			for i, controlKeyID := range owner.Addrs {
 				addr, err := s.addrManager.FormatLocalAddress(controlKeyID)
 				if err != nil {
 					return fmt.Errorf("problem formatting address: %w", err)
 				}
-				controlAddrs = append(controlAddrs, addr)
+				controlAddrs[i] = addr
 			}
 			response.Subnets[i] = APISubnet{
 				ID:          subnetID,


Test patch:
diff --git a/vms/platformvm/service_test.go b/vms/platformvm/service_test.go
index 95e2f98c3228..d72e11f6379c 100644
--- a/vms/platformvm/service_test.go
+++ b/vms/platformvm/service_test.go
@@ -1041,3 +1041,53 @@ func TestServiceGetBlockByHeight(t *testing.T) {
 		})
 	}
 }
+
+func TestServiceGetSubnets(t *testing.T) {
+	require := require.New(t)
+	service, _, _ := defaultService(t)
+
+	testSubnet1ID := testSubnet1.ID()
+
+	var response GetSubnetsResponse
+	require.NoError(service.GetSubnets(nil, &GetSubnetsArgs{}, &response))
+	require.Equal([]APISubnet{
+		{
+			ID: testSubnet1ID,
+			ControlKeys: []string{
+				"P-testing1d6kkj0qh4wcmus3tk59npwt3rluc6en72ngurd",
+				"P-testing17fpqs358de5lgu7a5ftpw2t8axf0pm33983krk",
+				"P-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e",
+			},
+			Threshold: 2,
+		},
+		{
+			ID:          constants.PrimaryNetworkID,
+			ControlKeys: []string{},
+			Threshold:   0,
+		},
+	}, response.Subnets)
+
+	newOwnerIDStr := "P-testing1t73fa4p4dypa4s3kgufuvr6hmprjclw66mgqgm"
+	newOwnerID, err := service.addrManager.ParseLocalAddress(newOwnerIDStr)
+	require.NoError(err)
+	service.vm.state.SetSubnetOwner(testSubnet1ID, &secp256k1fx.OutputOwners{
+		Addrs:     []ids.ShortID{newOwnerID},
+		Threshold: 1,
+	})
+
+	require.NoError(service.GetSubnets(nil, &GetSubnetsArgs{}, &response))
+	require.Equal([]APISubnet{
+		{
+			ID: testSubnet1ID,
+			ControlKeys: []string{
+				newOwnerIDStr,
+			},
+			Threshold: 1,
+		},
+		{
+			ID:          constants.PrimaryNetworkID,
+			ControlKeys: []string{},
+			Threshold:   0,
+		},
+	}, response.Subnets)
+}