github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/broadcast/definition.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 "encoding/json" 22 23 "github.com/kaleido-io/firefly/internal/i18n" 24 "github.com/kaleido-io/firefly/pkg/fftypes" 25 ) 26 27 func (bm *broadcastManager) broadcastDefinitionAsNode(ctx context.Context, def fftypes.Definition, tag fftypes.SystemTag) (msg *fftypes.Message, err error) { 28 signingIdentity, err := bm.GetNodeSigningIdentity(ctx) 29 if err != nil { 30 return nil, err 31 } 32 return bm.BroadcastDefinition(ctx, def, signingIdentity, tag) 33 } 34 35 func (bm *broadcastManager) BroadcastDefinition(ctx context.Context, def fftypes.Definition, signingIdentity *fftypes.Identity, tag fftypes.SystemTag) (msg *fftypes.Message, err error) { 36 37 err = bm.blockchain.VerifyIdentitySyntax(ctx, signingIdentity) 38 if err != nil { 39 return nil, err 40 } 41 42 // Ensure the broadcast message is nil on the sending side - only set on receiving side 43 def.SetBroadcastMessage(nil) 44 45 // Serialize it into a data object, as a piece of data we can write to a message 46 data := &fftypes.Data{ 47 Validator: fftypes.ValidatorTypeSystemDefinition, 48 ID: fftypes.NewUUID(), 49 Namespace: fftypes.SystemNamespace, 50 Created: fftypes.Now(), 51 } 52 data.Value, err = json.Marshal(&def) 53 if err == nil { 54 err = data.Seal(ctx) 55 } 56 if err != nil { 57 return nil, i18n.WrapError(ctx, err, i18n.MsgSerializationFailed) 58 } 59 60 // Write as data to the local store 61 if err = bm.database.UpsertData(ctx, data, true, false /* we just generated the ID, so it is new */); err != nil { 62 return nil, err 63 } 64 65 // Create a broadcast message referring to the data 66 msg = &fftypes.Message{ 67 Header: fftypes.MessageHeader{ 68 Namespace: fftypes.SystemNamespace, 69 Type: fftypes.MessageTypeDefinition, 70 Author: signingIdentity.Identifier, 71 Topics: fftypes.FFNameArray{def.Topic()}, 72 Tag: string(tag), 73 TxType: fftypes.TransactionTypeBatchPin, 74 }, 75 Data: fftypes.DataRefs{ 76 {ID: data.ID, Hash: data.Hash}, 77 }, 78 } 79 80 // Broadcast the message 81 if err = bm.broadcastMessageCommon(ctx, msg); err != nil { 82 return nil, err 83 } 84 85 return msg, nil 86 }