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

Function checkValidDest

protocol/validation/validation.go:388–444  ·  view source on GitHub ↗
(vs *validationState, vd *bc.ValueDestination)

Source from the content-addressed store, hash-verified

386}
387
388func checkValidDest(vs *validationState, vd *bc.ValueDestination) error {
389 if vd == nil {
390 return errors.Wrap(errMissingField, "empty value destination")
391 }
392 if vd.Ref == nil {
393 return errors.Wrap(errMissingField, "missing ref on value destination")
394 }
395 if vd.Value == nil || vd.Value.AssetId == nil {
396 return errors.Wrap(errMissingField, "missing value on value source")
397 }
398
399 e, ok := vs.tx.Entries[*vd.Ref]
400 if !ok {
401 return errors.Wrapf(bc.ErrMissingEntry, "entry for value destination %x not found", vd.Ref.Bytes())
402 }
403 var src *bc.ValueSource
404 switch ref := e.(type) {
405 case *bc.Output:
406 if vd.Position != 0 {
407 return errors.Wrapf(errPosition, "invalid position %d for output destination", vd.Position)
408 }
409 src = ref.Source
410
411 case *bc.Retirement:
412 if vd.Position != 0 {
413 return errors.Wrapf(errPosition, "invalid position %d for retirement destination", vd.Position)
414 }
415 src = ref.Source
416
417 case *bc.Mux:
418 if vd.Position >= uint64(len(ref.Sources)) {
419 return errors.Wrapf(errPosition, "invalid position %d for %d-source mux destination", vd.Position, len(ref.Sources))
420 }
421 src = ref.Sources[vd.Position]
422
423 default:
424 return errors.Wrapf(bc.ErrEntryType, "value destination is %T, should be output, retirement, or mux", e)
425 }
426
427 if src.Ref == nil || *src.Ref != vs.entryID {
428 return errors.Wrapf(errMismatchedReference, "value destination for %x has disagreeing source %x", vs.entryID.Bytes(), src.Ref.Bytes())
429 }
430
431 if src.Position != vs.destPos {
432 return errors.Wrapf(errMismatchedPosition, "value destination position %d disagrees with %d", src.Position, vs.destPos)
433 }
434
435 eq, err := src.Value.Equal(vd.Value)
436 if err != nil {
437 return errors.Sub(errMissingField, err)
438 }
439 if !eq {
440 return errors.Wrapf(errMismatchedValue, "destination value %v disagrees with %v", src.Value, vd.Value)
441 }
442
443 return nil
444}
445

Callers 1

checkValidFunction · 0.85

Calls 3

BytesMethod · 0.45
EqualMethod · 0.45
SubMethod · 0.45

Tested by

no test coverage detected