github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/broadcast/syshandler_network_node.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package broadcast
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/kaleido-io/firefly/internal/log"
    23  	"github.com/kaleido-io/firefly/pkg/fftypes"
    24  )
    25  
    26  func (bm *broadcastManager) handleNodeBroadcast(ctx context.Context, msg *fftypes.Message, data []*fftypes.Data) (valid bool, err error) {
    27  	l := log.L(ctx)
    28  
    29  	var node fftypes.Node
    30  	valid = bm.getSystemBroadcastPayload(ctx, msg, data, &node)
    31  	if !valid {
    32  		return false, nil
    33  	}
    34  
    35  	if err = node.Validate(ctx, true); err != nil {
    36  		l.Warnf("Unable to process node broadcast %s - validate failed: %s", msg.Header.ID, err)
    37  		return false, nil
    38  	}
    39  
    40  	owner, err := bm.database.GetOrganizationByIdentity(ctx, node.Owner)
    41  	if err != nil {
    42  		return false, err // We only return database errors
    43  	}
    44  	if owner == nil {
    45  		l.Warnf("Unable to process node broadcast %s - parent identity not found: %s", msg.Header.ID, node.Owner)
    46  		return false, nil
    47  	}
    48  
    49  	id, err := bm.identity.Resolve(ctx, node.Owner)
    50  	if err != nil {
    51  		l.Warnf("Unable to process node broadcast %s - resolve owner identity failed: %s", msg.Header.ID, err)
    52  		return false, nil
    53  	}
    54  
    55  	if msg.Header.Author != id.OnChain {
    56  		l.Warnf("Unable to process node broadcast %s - incorrect signature. Expected=%s Received=%s", msg.Header.ID, id.OnChain, msg.Header.Author)
    57  		return false, nil
    58  	}
    59  
    60  	existing, err := bm.database.GetNode(ctx, node.Owner, node.Name)
    61  	if err == nil && existing == nil {
    62  		existing, err = bm.database.GetNodeByID(ctx, node.ID)
    63  	}
    64  	if err != nil {
    65  		return false, err // We only return database errors
    66  	}
    67  	if existing != nil {
    68  		if existing.Owner != node.Owner {
    69  			l.Warnf("Unable to process node broadcast %s - mismatch with existing %v", msg.Header.ID, existing.ID)
    70  			return false, nil
    71  		}
    72  		node.ID = nil // we keep the existing ID
    73  	}
    74  
    75  	if err = bm.database.UpsertNode(ctx, &node, true); err != nil {
    76  		return false, err
    77  	}
    78  
    79  	// Tell the data exchange about this node. Treat these errors like database errors - and return for retry processing
    80  	if err = bm.exchange.AddPeer(ctx, &node); err != nil {
    81  		return false, err
    82  	}
    83  
    84  	return true, nil
    85  }