MCPcopy Create free account
hub / github.com/chain/txvm / valueToTop

Function valueToTop

protocol/txbuilder/template.go:741–771  ·  view source on GitHub ↗
(b *txvmutil.Builder, stack []stackItem, amount int64, assetID bc.Hash)

Source from the content-addressed store, hash-verified

739}
740
741func valueToTop(b *txvmutil.Builder, stack []stackItem, amount int64, assetID bc.Hash) ([]stackItem, error) {
742 if len(stack) == 0 {
743 return nil, errors.New("empty stack")
744 }
745 for {
746 top := stack[len(stack)-1]
747 if top.amount > 0 && top.assetID == assetID {
748 if top.amount == amount {
749 return stack, nil
750 }
751 if top.amount > amount {
752 // con stack is [... val(orig - amount) val(amount)]
753 return split(b, stack, amount), nil
754 }
755 if len(stack) > 1 {
756 second := stack[len(stack)-2]
757 if second.amount > 0 && second.assetID == assetID {
758 stack = merge(b, stack, amount)
759 continue
760 }
761 }
762 }
763
764 stack = findAndRoll(b, stack, func(i int, item stackItem) bool {
765 return i != len(stack)-1 && item.amount > 0 && item.assetID == assetID
766 })
767 if stack == nil {
768 return nil, errors.WithDetailf(ErrInsufficientValue, "amount=%d, assetID=%x", amount, assetID.Bytes())
769 }
770 }
771}

Callers 1

materializeTxMethod · 0.85

Calls 6

NewFunction · 0.92
WithDetailfFunction · 0.92
splitFunction · 0.85
mergeFunction · 0.85
findAndRollFunction · 0.85
BytesMethod · 0.45

Tested by

no test coverage detected