github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/tests/stdlibs/std/std.go (about)

     1  package std
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/gnolang/gno/gnovm/stdlibs"
     9  
    10  	gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
    11  	"github.com/gnolang/gno/gnovm/stdlibs/std"
    12  	"github.com/gnolang/gno/tm2/pkg/crypto"
    13  )
    14  
    15  func AssertOriginCall(m *gno.Machine) {
    16  	if !IsOriginCall(m) {
    17  		m.Panic(typedString("invalid non-origin call"))
    18  	}
    19  }
    20  
    21  func typedString(s gno.StringValue) gno.TypedValue {
    22  	tv := gno.TypedValue{T: gno.StringType}
    23  	tv.SetString(s)
    24  	return tv
    25  }
    26  
    27  func IsOriginCall(m *gno.Machine) bool {
    28  	tname := m.Frames[0].Func.Name
    29  	switch tname {
    30  	case "main": // test is a _filetest
    31  		return len(m.Frames) == 3
    32  	case "runtest": // test is a _test
    33  		return len(m.Frames) == 7
    34  	}
    35  	// support init() in _filetest
    36  	// XXX do we need to distinguish from 'runtest'/_test?
    37  	// XXX pretty hacky even if not.
    38  	if strings.HasPrefix(string(tname), "init.") {
    39  		return len(m.Frames) == 3
    40  	}
    41  	panic("unable to determine if test is a _test or a _filetest")
    42  }
    43  
    44  func TestCurrentRealm(m *gno.Machine) string {
    45  	return m.Realm.Path
    46  }
    47  
    48  func TestSkipHeights(m *gno.Machine, count int64) {
    49  	ctx := m.Context.(std.ExecContext)
    50  	ctx.Height += count
    51  	m.Context = ctx
    52  }
    53  
    54  func ClearStoreCache(m *gno.Machine) {
    55  	if gno.IsDebug() && testing.Verbose() {
    56  		m.Store.Print()
    57  		fmt.Println("========================================")
    58  		fmt.Println("CLEAR CACHE (RUNTIME)")
    59  		fmt.Println("========================================")
    60  	}
    61  	m.Store.ClearCache()
    62  	m.PreprocessAllFilesAndSaveBlockNodes()
    63  	if gno.IsDebug() && testing.Verbose() {
    64  		m.Store.Print()
    65  		fmt.Println("========================================")
    66  		fmt.Println("CLEAR CACHE DONE")
    67  		fmt.Println("========================================")
    68  	}
    69  }
    70  
    71  func X_callerAt(m *gno.Machine, n int) string {
    72  	if n <= 0 {
    73  		m.Panic(typedString("GetCallerAt requires positive arg"))
    74  		return ""
    75  	}
    76  	// Add 1 to n to account for the GetCallerAt (gno fn) frame.
    77  	n++
    78  	if n > m.NumFrames()-1 {
    79  		// NOTE: the last frame's LastPackage
    80  		// is set to the original non-frame
    81  		// package, so need this check.
    82  		m.Panic(typedString("frame not found"))
    83  		return ""
    84  	}
    85  	if n == m.NumFrames()-1 {
    86  		// This makes it consistent with GetOrigCaller and TestSetOrigCaller.
    87  		ctx := m.Context.(std.ExecContext)
    88  		return string(ctx.OrigCaller)
    89  	}
    90  	return string(m.MustLastCallFrame(n).LastPackage.GetPkgAddr().Bech32())
    91  }
    92  
    93  func X_testSetOrigCaller(m *gno.Machine, addr string) {
    94  	ctx := m.Context.(std.ExecContext)
    95  	ctx.OrigCaller = crypto.Bech32Address(addr)
    96  	m.Context = ctx
    97  }
    98  
    99  func X_testSetOrigPkgAddr(m *gno.Machine, addr string) {
   100  	ctx := m.Context.(std.ExecContext)
   101  	ctx.OrigPkgAddr = crypto.Bech32Address(addr)
   102  	m.Context = ctx
   103  }
   104  
   105  func X_testSetPrevRealm(m *gno.Machine, pkgPath string) {
   106  	m.Frames[m.NumFrames()-2].LastPackage = &gno.PackageValue{PkgPath: pkgPath}
   107  }
   108  
   109  func X_testSetPrevAddr(m *gno.Machine, addr string) {
   110  	// clear all frames to return mocked origin caller
   111  	for i := m.NumFrames() - 1; i > 0; i-- {
   112  		m.Frames[i].LastPackage = nil
   113  	}
   114  
   115  	ctx := m.Context.(stdlibs.ExecContext)
   116  	ctx.OrigCaller = crypto.Bech32Address(addr)
   117  	m.Context = ctx
   118  }
   119  
   120  func X_testSetOrigSend(m *gno.Machine,
   121  	sentDenom []string, sentAmt []int64,
   122  	spentDenom []string, spentAmt []int64,
   123  ) {
   124  	ctx := m.Context.(std.ExecContext)
   125  	ctx.OrigSend = std.CompactCoins(sentDenom, sentAmt)
   126  	spent := std.CompactCoins(spentDenom, spentAmt)
   127  	ctx.OrigSendSpent = &spent
   128  	m.Context = ctx
   129  }
   130  
   131  func X_testIssueCoins(m *gno.Machine, addr string, denom []string, amt []int64) {
   132  	ctx := m.Context.(std.ExecContext)
   133  	banker := ctx.Banker
   134  	for i := range denom {
   135  		banker.IssueCoin(crypto.Bech32Address(addr), denom[i], amt[i])
   136  	}
   137  }