MCPcopy Create free account
hub / github.com/chain/Core / checkValidSrc

Function checkValidSrc

protocol/validation/validation.go:323–386  ·  view source on GitHub ↗
(vstate *validationState, vs *bc.ValueSource)

Source from the content-addressed store, hash-verified

321}
322
323func checkValidSrc(vstate *validationState, vs *bc.ValueSource) error {
324 if vs == nil {
325 return errors.Wrap(errMissingField, "empty value source")
326 }
327 if vs.Ref == nil {
328 return errors.Wrap(errMissingField, "missing ref on value source")
329 }
330 if vs.Value == nil || vs.Value.AssetId == nil {
331 return errors.Wrap(errMissingField, "missing value on value source")
332 }
333
334 e, ok := vstate.tx.Entries[*vs.Ref]
335 if !ok {
336 return errors.Wrapf(bc.ErrMissingEntry, "entry for value source %x not found", vs.Ref.Bytes())
337 }
338 vstate2 := *vstate
339 vstate2.entryID = *vs.Ref
340 err := checkValid(&vstate2, e)
341 if err != nil {
342 return errors.Wrap(err, "checking value source")
343 }
344
345 var dest *bc.ValueDestination
346 switch ref := e.(type) {
347 case *bc.Issuance:
348 if vs.Position != 0 {
349 return errors.Wrapf(errPosition, "invalid position %d for issuance source", vs.Position)
350 }
351 dest = ref.WitnessDestination
352
353 case *bc.Spend:
354 if vs.Position != 0 {
355 return errors.Wrapf(errPosition, "invalid position %d for spend source", vs.Position)
356 }
357 dest = ref.WitnessDestination
358
359 case *bc.Mux:
360 if vs.Position >= uint64(len(ref.WitnessDestinations)) {
361 return errors.Wrapf(errPosition, "invalid position %d for %d-destination mux source", vs.Position, len(ref.WitnessDestinations))
362 }
363 dest = ref.WitnessDestinations[vs.Position]
364
365 default:
366 return errors.Wrapf(bc.ErrEntryType, "value source is %T, should be issuance, spend, or mux", e)
367 }
368
369 if dest.Ref == nil || *dest.Ref != vstate.entryID {
370 return errors.Wrapf(errMismatchedReference, "value source for %x has disagreeing destination %x", vstate.entryID.Bytes(), dest.Ref.Bytes())
371 }
372
373 if dest.Position != vstate.sourcePos {
374 return errors.Wrapf(errMismatchedPosition, "value source position %d disagrees with %d", dest.Position, vstate.sourcePos)
375 }
376
377 eq, err := dest.Value.Equal(vs.Value)
378 if err != nil {
379 return errors.Sub(errMissingField, err)
380 }

Callers 1

checkValidFunction · 0.85

Calls 4

checkValidFunction · 0.85
BytesMethod · 0.45
EqualMethod · 0.45
SubMethod · 0.45

Tested by

no test coverage detected