MCPcopy Index your code
hub / github.com/btcsuite/btcd / SwapN

Method SwapN

txscript/stack.go:273–290  ·  view source on GitHub ↗

SwapN swaps the top N items on the stack with those below them. Stack transformation: SwapN(1): [... x1 x2] -> [... x2 x1] SwapN(2): [... x1 x2 x3 x4] -> [... x3 x4 x1 x2]

(n int32)

Source from the content-addressed store, hash-verified

271// SwapN(1): [... x1 x2] -> [... x2 x1]
272// SwapN(2): [... x1 x2 x3 x4] -> [... x3 x4 x1 x2]
273func (s *stack) SwapN(n int32) error {
274 if n < 1 {
275 str := fmt.Sprintf("attempt to swap %d stack items", n)
276 return scriptError(ErrInvalidStackOperation, str)
277 }
278
279 entry := 2*n - 1
280 for i := n; i > 0; i-- {
281 // Swap 2n-1th entry to top.
282 so, err := s.nipN(entry)
283 if err != nil {
284 return err
285 }
286
287 s.PushByteArray(so)
288 }
289 return nil
290}
291
292// OverN copies N items N items back to the top of the stack.
293//

Callers 3

TestStackFunction · 0.95
opcode2SwapFunction · 0.80
opcodeSwapFunction · 0.80

Calls 3

nipNMethod · 0.95
PushByteArrayMethod · 0.95
scriptErrorFunction · 0.85

Tested by 1

TestStackFunction · 0.76