Skip to content

Commit

Permalink
core: move contract notifications check under HFBasilisk
Browse files Browse the repository at this point in the history
Follow the neo-project/neo#2884.
A part of the neo-project/neo#2810.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
AnnaShaleva committed Aug 10, 2023
1 parent 87fd075 commit 4bb7591
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/node-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ protocol-related settings described in the table below.
| --- | --- | --- | --- | --- |
| CommitteeHistory | map[uint32]uint32 | none | Number of committee members after the given height, for example `{0: 1, 20: 4}` sets up a chain with one committee member since the genesis and then changes the setting to 4 committee members at the height of 20. `StandbyCommittee` committee setting must have the number of keys equal or exceeding the highest value in this option. Blocks numbers where the change happens must be divisible by the old and by the new values simultaneously. If not set, committee size is derived from the `StandbyCommittee` setting and never changes. |
| GarbageCollectionPeriod | `uint32` | 10000 | Controls MPT garbage collection interval (in blocks) for configurations with `RemoveUntraceableBlocks` enabled and `KeepOnlyLatestState` disabled. In this mode the node stores a number of MPT trees (corresponding to `MaxTraceableBlocks` and `StateSyncInterval`), but the DB needs to be clean from old entries from time to time. Doing it too often will cause too much processing overhead, doing it too rarely will leave more useless data in the DB. This setting is deprecated in favor of the same setting in the ApplicationConfiguration and will be removed in future node versions. If both settings are used, ApplicationConfiguration is prioritized over this one. |
| Hardforks | `map[string]uint32` | [] | The set of incompatible changes that affect node behaviour starting from the specified height. The default value is an empty set which should be interpreted as "each known hard-fork is applied from the zero blockchain height". The list of valid hard-fork names:<br>• `Aspidochelone` represents hard-fork introduced in [#2469](https://github.com/nspcc-dev/neo-go/pull/2469) (ported from the [reference](https://github.com/neo-project/neo/pull/2712)). It adjusts the prices of `System.Contract.CreateStandardAccount` and `System.Contract.CreateMultisigAccount` interops so that the resulting prices are in accordance with `sha256` method of native `CryptoLib` contract. It also includes [#2519](https://github.com/nspcc-dev/neo-go/pull/2519) (ported from the [reference](https://github.com/neo-project/neo/pull/2749)) that adjusts the price of `System.Runtime.GetRandom` interop and fixes its vulnerability. A special NeoGo-specific change is included as well for ContractManagement's update/deploy call flags behaviour to be compatible with pre-0.99.0 behaviour that was changed because of the [3.2.0 protocol change](https://github.com/neo-project/neo/pull/2653).<br>• `Basilisk` represents hard-fork introduced in [#3056](https://github.com/nspcc-dev/neo-go/pull/3056) (ported from the [reference](https://github.com/neo-project/neo/pull/2881)). It enables strict smart contract script check against a set of JMP instructions and against method boundaries enabled on contract deploy or update. It also includes [#3080](https://github.com/nspcc-dev/neo-go/pull/3080) (ported from the [reference](https://github.com/neo-project/neo/pull/2883)) that increases `stackitem.Integer` JSON parsing precision up to the maximum value supported by the NeoVM.|
| Hardforks | `map[string]uint32` | [] | The set of incompatible changes that affect node behaviour starting from the specified height. The default value is an empty set which should be interpreted as "each known hard-fork is applied from the zero blockchain height". The list of valid hard-fork names:<br>• `Aspidochelone` represents hard-fork introduced in [#2469](https://github.com/nspcc-dev/neo-go/pull/2469) (ported from the [reference](https://github.com/neo-project/neo/pull/2712)). It adjusts the prices of `System.Contract.CreateStandardAccount` and `System.Contract.CreateMultisigAccount` interops so that the resulting prices are in accordance with `sha256` method of native `CryptoLib` contract. It also includes [#2519](https://github.com/nspcc-dev/neo-go/pull/2519) (ported from the [reference](https://github.com/neo-project/neo/pull/2749)) that adjusts the price of `System.Runtime.GetRandom` interop and fixes its vulnerability. A special NeoGo-specific change is included as well for ContractManagement's update/deploy call flags behaviour to be compatible with pre-0.99.0 behaviour that was changed because of the [3.2.0 protocol change](https://github.com/neo-project/neo/pull/2653).<br>• `Basilisk` represents hard-fork introduced in [#3056](https://github.com/nspcc-dev/neo-go/pull/3056) (ported from the [reference](https://github.com/neo-project/neo/pull/2881)). It enables strict smart contract script check against a set of JMP instructions and against method boundaries enabled on contract deploy or update. It also includes [#3080](https://github.com/nspcc-dev/neo-go/pull/3080) (ported from the [reference](https://github.com/neo-project/neo/pull/2883)) that increases `stackitem.Integer` JSON parsing precision up to the maximum value supported by the NeoVM. It also includes [#3085](https://github.com/nspcc-dev/neo-go/pull/3085) (ported from the [reference](https://github.com/neo-project/neo/pull/2810)) that enables strict check for notifications emitted by a contract to precisely match the events specified in the contract manifest. |
| KeepOnlyLatestState | `bool` | `false` | Specifies if MPT should only store the latest state (or a set of latest states, see `P2PStateExcangeExtensions` section for details). If true, DB size will be smaller, but older roots won't be accessible. This value should remain the same for the same database. | This setting is deprecated in favor of the same setting in the ApplicationConfiguration and will be removed in future node versions. If both settings are used, setting any of them to true enables the function. |
| Magic | `uint32` | `0` | Magic number which uniquely identifies Neo network. |
| MaxBlockSize | `uint32` | `262144` | Maximum block size in bytes. |
Expand Down
5 changes: 3 additions & 2 deletions pkg/config/hardfork.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const (
// https://github.com/neo-project/neo/pull/2749).
HFAspidochelone Hardfork = 1 << iota // Aspidochelone
// HFBasilisk represents hard-fork introduced in #3056 (ported from
// https://github.com/neo-project/neo/pull/2881) and #3080 (ported from
// https://github.com/neo-project/neo/pull/2883).
// https://github.com/neo-project/neo/pull/2881), #3080 (ported from
// https://github.com/neo-project/neo/pull/2883) and #3085 (ported from
// https://github.com/neo-project/neo/pull/2810).
HFBasilisk // Basilisk
)

Expand Down
16 changes: 13 additions & 3 deletions pkg/core/interop/runtime/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/big"

"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/vm"
Expand Down Expand Up @@ -83,15 +84,24 @@ func Notify(ic *interop.Context) error {
if err != nil {
return errors.New("notifications are not allowed in dynamic scripts")
}
ev := ctr.Manifest.ABI.GetEvent(name)
var (
ev = ctr.Manifest.ABI.GetEvent(name)
checkErr error
)
if ev == nil {
ic.Log.Info("bad notification", zap.String("contract", curHash.StringLE()), zap.String("event", name), zap.Error(fmt.Errorf("event %s does not exist", name)))
checkErr = fmt.Errorf("notification %s does not exist", name)
} else {
err = ev.CheckCompliance(args)
if err != nil {
ic.Log.Info("bad notification", zap.String("contract", curHash.StringLE()), zap.String("event", name), zap.Error(err))
checkErr = fmt.Errorf("notification %s is invalid: %w", name, err)
}
}
if checkErr != nil {
if ic.IsHardforkEnabled(config.HFBasilisk) {
return checkErr
}
ic.Log.Info("bad notification", zap.String("contract", curHash.StringLE()), zap.String("event", name), zap.Error(checkErr))
}

// But it has to be serializable, otherwise we either have some broken
// (recursive) structure inside or an interop item that can't be used
Expand Down
60 changes: 60 additions & 0 deletions pkg/core/interop/runtime/ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,63 @@ func TestNotify(t *testing.T) {
require.NotEqual(t, arr, ev.Item)
})
}

func TestSystemRuntimeNotify_HFBasilisk(t *testing.T) {
const ntfName = "Hello, world!"

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)

script := io.NewBufBinWriter()
emit.Array(script.BinWriter, stackitem.Make(true)) // Boolean instead of Integer declared in manifest
emit.String(script.BinWriter, ntfName)
emit.Syscall(script.BinWriter, interopnames.SystemRuntimeNotify)
require.NoError(t, script.Err)
ne, err := nef.NewFile(script.Bytes())
require.NoError(t, err)

m := &manifest.Manifest{
Name: "ctr",
ABI: manifest.ABI{
Methods: []manifest.Method{
{
Name: "main",
Offset: 0,
ReturnType: smartcontract.VoidType,
},
},
Events: []manifest.Event{
{
Name: ntfName,
Parameters: []manifest.Parameter{
{
Name: "int",
Type: smartcontract.IntegerType,
},
},
},
},
},
}
ctr := &neotest.Contract{
Hash: state.CreateContractHash(e.Validator.ScriptHash(), ne.Checksum, m.Name),
NEF: ne,
Manifest: m,
}
ctrInv := e.NewInvoker(ctr.Hash, e.Validator)

// Block 1: deploy contract.
e.DeployContract(t, ctr, nil)

// Block 2: bad event should be logged.
ctrInv.Invoke(t, nil, "main")

// Block 3: bad event should fault the execution.
ctrInv.InvokeFail(t,
"System.Runtime.Notify failed: notification Hello, world! is invalid: parameter 0 type mismatch: Integer (manifest) vs Boolean (notification)",
"main")
}

0 comments on commit 4bb7591

Please sign in to comment.