mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2026-03-01 04:28:51 +00:00
neotest: Provide the ability to write any values in the tested contract storage #1086
Labels
No labels
I1
I2
I3
I4
S1
S2
S3
S4
U0
U1
U2
U3
U3
U4
blocked
bug
bug
cli
compiler
config
config
consensus
dependencies
discussion
documentation
enhancement
epic
feature
go
good first issue
help wanted
neotest
network
oracle
performance
question
rpc
security
smartcontract
task
task
task
test
vm
wallet
windows
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
nspcc-dev/neo-go#1086
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @cthulhu-rider on GitHub (Feb 22, 2023).
Context
I try to test migration of contracts' storage items using framework provided by
neotestpackage. Sometimes contract stores items within its methods, so technically I could just call exact method. The inconvenience is that such elements are stored in the contract as a result of API methods that are not always easy to call within the test due to the need to follow business logic. At the same time, I did not find other ways.Proposal
Add functionality to
neotestpackage which allows to directly write key-value item into the contract storage.Possible solution
The least affecting approach I thought about is to add analogue of https://pkg.go.dev/github.com/nspcc-dev/neo-go@v0.101.0/pkg/neotest#CompileFile
which, after reading the source code of the contract (but before compiling), adds direct-write method for the life of the test contract. You can call this method later like any other one.
@roman-khimov commented on GitHub (Feb 22, 2023):
Changing contract on the fly is not a good idea, we're supposed to compile/deploy them exactly. Currently it's possible to use custom stores for
chain.NewSingleWithCustomConfigAndStore()andchain.NewMultiWithCustomConfigAndStorewhere you have a complete storage API (seems like you're already doing it). But as you know underlying store may be a bit outdated compared to the internalBlockchainmemory cache.Blockchainis specifically designed to never expose its memcache/DAO, but we may think about some specific hooks for tests, that I think will be safer and more appropriate way to handle this problem.@cthulhu-rider commented on GitHub (Feb 22, 2023):
Ofc test hooks in core lib would be much better than contract surgery.
@cthulhu-rider commented on GitHub (Mar 1, 2023):
Here's a workaround suggested by @roman-khimov for the case here when you just want to add contract with some data in advance.
However, it is worth noting that after that contract invocations fail with
contract not foundexceptions. https://pkg.go.dev/github.com/nspcc-dev/neo-go/pkg/core#Blockchain.GetContractState returnsnil, but https://pkg.go.dev/github.com/nspcc-dev/neo-go/pkg/core#Blockchain.GetContractScriptHash works.@roman-khimov commented on GitHub (Mar 1, 2023):
Please try
Yeah, it's weird. But otherwise management contract's cache is not really initialized, because we're starting with an (almost) clean DB, it's inited for the genesis and that's it.
@cthulhu-rider commented on GitHub (Mar 1, 2023):
It really works. The only thing I also needed to do is to make no-op
Closemethod of thestorage.Storepassed on the first init. Without this,blockChain.Closereset underlying maps.