Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[neotest] panic when trying to deploy contract that requests storage context in init() #3120

Closed
ixje opened this issue Sep 5, 2023 · 3 comments
Labels
go Go language related question Further information is requested test Unit tests

Comments

@ixje
Copy link
Contributor

ixje commented Sep 5, 2023

Current Behavior

I was trying out neotest and figured I'd try to create tests for the nep-17 token in the examples folder.

token_test.go

package tokencontract

import (
	"testing"

	"github.com/nspcc-dev/neo-go/pkg/neotest"
	"github.com/nspcc-dev/neo-go/pkg/neotest/chain"
)

func newTokenClient(t *testing.T) *neotest.ContractInvoker {
	bc, acc := chain.NewSingle(t)
	e := neotest.NewExecutor(t, bc, acc, acc)
	c := neotest.CompileFile(t, e.CommitteeHash, ".", "token.yml")
	e.DeployContract(t, c, nil)
	return e.CommitteeInvoker(c.Hash)
}

func TestToken_Basics(t *testing.T) {
	c := newTokenClient(t)
	c.Signers = []neotest.Signer{c.NewAccount(t)}
	c.Invoke(t, "ANT", "symbol")
	c.Invoke(t, decimals, "decimals")
	c.Invoke(t, 11000000*multiplier, "totalSupply")
}

Trying to execute the test fail in the DeployContract call with the below error

panic: interface conversion: interface {} is nil, not storage.Context

goroutine 1 [running]:
github.com/nspcc-dev/neo-go/pkg/interop/storage.GetContext()
	/home/erik/go/pkg/mod/github.com/nspcc-dev/neo-go/pkg/interop@v0.0.0-20221202075445-cb5c18dc73eb/storage/storage.go:52 +0x5d
github.com/nspcc-dev/neo-go/examples/token.init.0()
	/home/erik/code/neo-go/examples/token/token.go:32 +0xd4

which is here

ctx = storage.GetContext()

Expected Behavior

I expect no errors on deploy

Steps to Reproduce

  1. Place the above code in this folder https://github.com/nspcc-dev/neo-go/blob/master/examples/token/ in a file called token_test.go
  2. run TestToken_Basics

Your Environment

@ixje ixje added the bug Something isn't working label Sep 5, 2023
@ixje ixje changed the title [neotest] panic when trying to deploy contract that request storage context in init() [neotest] panic when trying to deploy contract that requests storage context in init() Sep 5, 2023
@roman-khimov
Copy link
Member

package tokencontract

Place the above code in this folder https://github.com/nspcc-dev/neo-go/blob/master/examples/token/ in a file called token_test.go

Well, that's the issue and it can't be fixed. You're creating a test file in the same Go module as the contract itself and this contract has an init(). So when the test module is compiled it gets the same init() inside which can't work in non-SC context.

You can try package tokencontract_test though. Since you don't (and shouldn't) import the smart contract package this should work by creating a different package that doesn't have any init().

@ixje
Copy link
Contributor Author

ixje commented Sep 5, 2023

Well, that's the issue and it can't be fixed. You're creating a test file in the same Go module as the contract itself and this contract has an init(). So when the test module is compiled it gets the same init() inside which can't work in non-SC context.

Oh I didn't know init() is some special function that always gets called.

You can try package tokencontract_test though. Since you don't (and shouldn't) import the smart contract package this should work by creating a different package that doesn't have any init().

I tried package tokencontract_test but the result is the same 🤔

@roman-khimov
Copy link
Member

roman-khimov commented Sep 5, 2023

Apparently it gets compiled into the package_test anyway, even though it's not imported directly. Which is somewhat surprising to me, but golang/go#58823.

So, an additional folder/package is needed.

@ixje ixje closed this as completed Sep 5, 2023
@roman-khimov roman-khimov added question Further information is requested go Go language related test Unit tests and removed bug Something isn't working labels Sep 5, 2023
roman-khimov added a commit that referenced this issue Sep 5, 2023
We don't want anyone to have the same problem.

Signed-off-by: Roman Khimov <roman@nspcc.ru>
roman-khimov added a commit that referenced this issue Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go Go language related question Further information is requested test Unit tests
Projects
None yet
Development

No branches or pull requests

2 participants