github.com/braveheart12/just@v0.8.7/core/message/parcel.go (about) 1 /* 2 * Copyright 2019 Insolar Technologies 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package message 18 19 import ( 20 "context" 21 "crypto" 22 23 "github.com/insolar/insolar/core" 24 "github.com/insolar/insolar/instrumentation/inslogger" 25 "github.com/insolar/insolar/instrumentation/instracer" 26 ) 27 28 // ParcelFactory is used for creating parcels 29 type ParcelFactory interface { 30 Create(context.Context, core.Message, core.RecordRef, core.DelegationToken, core.Pulse) (core.Parcel, error) 31 Validate(crypto.PublicKey, core.Parcel) error 32 } 33 34 // Parcel is a message signed by senders private key. 35 type Parcel struct { 36 Sender core.RecordRef 37 Msg core.Message 38 Signature []byte 39 LogTraceID string 40 TraceSpanData []byte 41 Token core.DelegationToken 42 PulseNumber core.PulseNumber 43 } 44 45 // AllowedSenderObjectAndRole implements interface method 46 func (p *Parcel) AllowedSenderObjectAndRole() (*core.RecordRef, core.DynamicRole) { 47 return p.Msg.AllowedSenderObjectAndRole() 48 } 49 50 // DefaultRole returns role for this event 51 func (p *Parcel) DefaultRole() core.DynamicRole { 52 return p.Msg.DefaultRole() 53 } 54 55 // DefaultTarget returns of target of this event. 56 func (p *Parcel) DefaultTarget() *core.RecordRef { 57 return p.Msg.DefaultTarget() 58 } 59 60 // Pulse returns pulse, when parcel was sent 61 func (p *Parcel) Pulse() core.PulseNumber { 62 return p.PulseNumber 63 } 64 65 // Message returns current instance's message 66 func (p *Parcel) Message() core.Message { 67 return p.Msg 68 } 69 70 // Context returns initialized context with propagated data with ctx as parent. 71 func (p *Parcel) Context(ctx context.Context) context.Context { 72 ctx = inslogger.ContextWithTrace(ctx, p.LogTraceID) 73 parentspan := instracer.MustDeserialize(p.TraceSpanData) 74 return instracer.WithParentSpan(ctx, parentspan) 75 } 76 77 func (p *Parcel) DelegationToken() core.DelegationToken { 78 return p.Token 79 } 80 81 // Type returns message type. 82 func (p *Parcel) Type() core.MessageType { 83 return p.Msg.Type() 84 } 85 86 // GetCaller returns initiator of this event. 87 func (p *Parcel) GetCaller() *core.RecordRef { 88 return p.Msg.GetCaller() 89 } 90 91 func (p *Parcel) GetSign() []byte { 92 return p.Signature 93 } 94 95 func (p *Parcel) GetSender() core.RecordRef { 96 return p.Sender 97 } 98 99 func (p *Parcel) AddDelegationToken(token core.DelegationToken) { 100 p.Token = token 101 }