2022-06-08 19:34:09 +00:00
|
|
|
package native_test
|
2020-12-13 15:26:35 +00:00
|
|
|
|
|
|
|
import (
|
2023-11-30 12:55:17 +00:00
|
|
|
"encoding/json"
|
2020-12-13 15:26:35 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-06-08 12:17:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/basicchain"
|
2022-03-10 14:12:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2023-08-03 16:48:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2022-03-10 14:12:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest/chain"
|
2023-11-30 12:55:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2023-08-03 16:48:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2020-12-15 10:53:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2023-08-03 16:48:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2023-11-30 12:55:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-07-23 12:52:30 +00:00
|
|
|
func TestManagement_GetNEP17Contracts(t *testing.T) {
|
|
|
|
t.Run("empty chain", func(t *testing.T) {
|
2022-03-10 14:12:04 +00:00
|
|
|
bc, validators, committee := chain.NewMulti(t)
|
|
|
|
e := neotest.NewExecutor(t, bc, validators, committee)
|
|
|
|
|
|
|
|
require.ElementsMatch(t, []util.Uint160{e.NativeHash(t, nativenames.Neo),
|
|
|
|
e.NativeHash(t, nativenames.Gas)}, bc.GetNEP17Contracts())
|
2021-07-23 12:52:30 +00:00
|
|
|
})
|
|
|
|
|
2022-03-10 14:12:04 +00:00
|
|
|
t.Run("basic chain", func(t *testing.T) {
|
2022-12-06 13:34:38 +00:00
|
|
|
bc, validators, committee := chain.NewMultiWithCustomConfig(t, func(c *config.Blockchain) {
|
2022-06-08 12:17:40 +00:00
|
|
|
c.P2PSigExtensions = true // `basicchain.Init` requires Notary enabled
|
2022-03-10 14:12:04 +00:00
|
|
|
})
|
|
|
|
e := neotest.NewExecutor(t, bc, validators, committee)
|
2022-06-08 19:34:09 +00:00
|
|
|
basicchain.Init(t, "../../../", e)
|
2022-03-10 14:12:04 +00:00
|
|
|
|
|
|
|
require.ElementsMatch(t, []util.Uint160{e.NativeHash(t, nativenames.Neo),
|
|
|
|
e.NativeHash(t, nativenames.Gas), e.ContractHash(t, 1)}, bc.GetNEP17Contracts())
|
2021-07-23 12:52:30 +00:00
|
|
|
})
|
|
|
|
}
|
2023-08-03 16:48:55 +00:00
|
|
|
|
2023-08-10 10:54:42 +00:00
|
|
|
func TestManagement_DeployUpdate_HFBasilisk(t *testing.T) {
|
2023-08-03 16:48:55 +00:00
|
|
|
bc, acc := chain.NewSingleWithCustomConfig(t, func(c *config.Blockchain) {
|
|
|
|
c.Hardforks = map[string]uint32{
|
|
|
|
config.HFBasilisk.String(): 2,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
e := neotest.NewExecutor(t, bc, acc, acc)
|
|
|
|
|
|
|
|
ne, err := nef.NewFile([]byte{byte(opcode.JMP), 0x05})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m := &manifest.Manifest{
|
2024-07-26 08:43:10 +00:00
|
|
|
Name: "ctr",
|
|
|
|
Features: json.RawMessage("{}"),
|
|
|
|
Groups: []manifest.Group{},
|
2024-07-26 09:32:49 +00:00
|
|
|
Trusts: manifest.WildPermissionDescs{Wildcard: true},
|
2023-08-03 16:48:55 +00:00
|
|
|
ABI: manifest.ABI{
|
|
|
|
Methods: []manifest.Method{
|
|
|
|
{
|
|
|
|
Name: "main",
|
|
|
|
Offset: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctr := &neotest.Contract{
|
|
|
|
|
|
|
|
Hash: state.CreateContractHash(e.Validator.ScriptHash(), ne.Checksum, m.Name),
|
|
|
|
NEF: ne,
|
|
|
|
Manifest: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block 1: no script check on deploy.
|
|
|
|
e.DeployContract(t, ctr, nil)
|
|
|
|
e.AddNewBlock(t)
|
|
|
|
|
|
|
|
// Block 3: script check on deploy.
|
|
|
|
ctr.Manifest.Name = "other name"
|
|
|
|
e.DeployContractCheckFAULT(t, ctr, nil, "invalid contract script: invalid offset 5 ip at 0")
|
|
|
|
}
|
2023-11-30 12:55:17 +00:00
|
|
|
|
|
|
|
func TestManagement_CallInTheSameBlock(t *testing.T) {
|
|
|
|
bc, acc := chain.NewSingle(t)
|
|
|
|
|
|
|
|
e := neotest.NewExecutor(t, bc, acc, acc)
|
|
|
|
|
|
|
|
ne, err := nef.NewFile([]byte{byte(opcode.PUSH1)})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m := &manifest.Manifest{
|
2024-07-26 08:43:10 +00:00
|
|
|
Name: "ctr",
|
|
|
|
Features: json.RawMessage("{}"),
|
|
|
|
Groups: []manifest.Group{},
|
2024-07-26 09:32:49 +00:00
|
|
|
Trusts: manifest.WildPermissionDescs{Wildcard: true},
|
2023-11-30 12:55:17 +00:00
|
|
|
ABI: manifest.ABI{
|
|
|
|
Methods: []manifest.Method{
|
|
|
|
{
|
|
|
|
Name: "main",
|
|
|
|
Offset: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("same tx", func(t *testing.T) {
|
|
|
|
h := state.CreateContractHash(e.Validator.ScriptHash(), ne.Checksum, m.Name)
|
|
|
|
|
|
|
|
neb, err := ne.Bytes()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rawManifest, err := json.Marshal(m)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b := smartcontract.NewBuilder()
|
|
|
|
b.InvokeWithAssert(bc.ManagementContractHash(), "deploy", neb, rawManifest) // We need to drop the resulting contract and ASSERT does that.
|
|
|
|
b.InvokeWithAssert(bc.ManagementContractHash(), "hasMethod", h, "main", 0)
|
|
|
|
b.InvokeMethod(h, "main")
|
|
|
|
|
|
|
|
script, err := b.Script()
|
|
|
|
require.NoError(t, err)
|
|
|
|
txHash := e.InvokeScript(t, script, []neotest.Signer{e.Validator})
|
|
|
|
e.CheckHalt(t, txHash, stackitem.Make(1))
|
|
|
|
})
|
|
|
|
t.Run("next tx", func(t *testing.T) {
|
|
|
|
m.Name = "another contract"
|
|
|
|
h := state.CreateContractHash(e.Validator.ScriptHash(), ne.Checksum, m.Name)
|
|
|
|
|
2024-08-09 08:55:22 +00:00
|
|
|
txDeploy := e.NewDeployTx(t, &neotest.Contract{Hash: h, NEF: ne, Manifest: m}, nil)
|
2023-11-30 12:55:17 +00:00
|
|
|
txHasMethod := e.NewTx(t, []neotest.Signer{e.Validator}, bc.ManagementContractHash(), "hasMethod", h, "main", 0)
|
|
|
|
|
|
|
|
txCall := e.NewUnsignedTx(t, h, "main") // Test invocation doesn't give true GAS cost before deployment.
|
|
|
|
txCall = e.SignTx(t, txCall, 1_0000_0000, e.Validator)
|
|
|
|
|
|
|
|
e.AddNewBlock(t, txDeploy, txHasMethod, txCall)
|
|
|
|
e.CheckHalt(t, txHasMethod.Hash(), stackitem.Make(true))
|
|
|
|
e.CheckHalt(t, txCall.Hash(), stackitem.Make(1))
|
|
|
|
})
|
|
|
|
}
|