compiler: iterate over autoguessed events in reproducible way

Otherwise it's undertermined which of two unnamed structures will get "Unnamed"
and "UnnamedX" which can break the test from time to time.

Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2023-11-21 18:41:58 +03:00
parent 5370034955
commit fced6a27ba
2 changed files with 24 additions and 15 deletions

View file

@ -20,12 +20,12 @@ var Hash = util.Uint160{0x33, 0x22, 0x11, 0x0, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xa
// Unnamed is a contract-specific unnamed type used by its methods. // Unnamed is a contract-specific unnamed type used by its methods.
type Unnamed struct { type Unnamed struct {
I *big.Int I *big.Int
B bool
} }
// UnnamedX is a contract-specific unnamedX type used by its methods. // UnnamedX is a contract-specific unnamedX type used by its methods.
type UnnamedX struct { type UnnamedX struct {
I *big.Int I *big.Int
B bool
} }
// ComplicatedNameEvent represents "! complicated name %$#" event emitted by the contract. // ComplicatedNameEvent represents "! complicated name %$#" event emitted by the contract.
@ -40,7 +40,7 @@ type SomeMapEvent struct {
// SomeStructEvent represents "SomeStruct" event emitted by the contract. // SomeStructEvent represents "SomeStruct" event emitted by the contract.
type SomeStructEvent struct { type SomeStructEvent struct {
S *UnnamedX S *Unnamed
} }
// SomeArrayEvent represents "SomeArray" event emitted by the contract. // SomeArrayEvent represents "SomeArray" event emitted by the contract.
@ -50,7 +50,7 @@ type SomeArrayEvent struct {
// SomeUnexportedFieldEvent represents "SomeUnexportedField" event emitted by the contract. // SomeUnexportedFieldEvent represents "SomeUnexportedField" event emitted by the contract.
type SomeUnexportedFieldEvent struct { type SomeUnexportedFieldEvent struct {
S *Unnamed S *UnnamedX
} }
// Actor is used by Contract to call state-changing methods. // Actor is used by Contract to call state-changing methods.
@ -202,7 +202,7 @@ func (res *Unnamed) FromStackItem(item stackitem.Item) error {
if !ok { if !ok {
return errors.New("not an array") return errors.New("not an array")
} }
if len(arr) != 1 { if len(arr) != 2 {
return errors.New("wrong number of structure elements") return errors.New("wrong number of structure elements")
} }
@ -216,6 +216,12 @@ func (res *Unnamed) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field I: %w", err) return fmt.Errorf("field I: %w", err)
} }
index++
res.B, err = arr[index].TryBool()
if err != nil {
return fmt.Errorf("field B: %w", err)
}
return nil return nil
} }
@ -236,7 +242,7 @@ func (res *UnnamedX) FromStackItem(item stackitem.Item) error {
if !ok { if !ok {
return errors.New("not an array") return errors.New("not an array")
} }
if len(arr) != 2 { if len(arr) != 1 {
return errors.New("wrong number of structure elements") return errors.New("wrong number of structure elements")
} }
@ -250,12 +256,6 @@ func (res *UnnamedX) FromStackItem(item stackitem.Item) error {
return fmt.Errorf("field I: %w", err) return fmt.Errorf("field I: %w", err)
} }
index++
res.B, err = arr[index].TryBool()
if err != nil {
return fmt.Errorf("field B: %w", err)
}
return nil return nil
} }
@ -497,7 +497,7 @@ func (e *SomeStructEvent) FromStackItem(item *stackitem.Array) error {
err error err error
) )
index++ index++
e.S, err = itemToUnnamedX(arr[index], nil) e.S, err = itemToUnnamed(arr[index], nil)
if err != nil { if err != nil {
return fmt.Errorf("field S: %w", err) return fmt.Errorf("field S: %w", err)
} }
@ -627,7 +627,7 @@ func (e *SomeUnexportedFieldEvent) FromStackItem(item *stackitem.Array) error {
err error err error
) )
index++ index++
e.S, err = itemToUnnamed(arr[index], nil) e.S, err = itemToUnnamedX(arr[index], nil)
if err != nil { if err != nil {
return fmt.Errorf("field S: %w", err) return fmt.Errorf("field S: %w", err)
} }

View file

@ -11,6 +11,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
@ -357,8 +358,16 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
} }
if o.GuessEventTypes { if o.GuessEventTypes {
if len(di.EmittedEvents) > 0 { if len(di.EmittedEvents) > 0 {
for eventName, eventUsages := range di.EmittedEvents { var keys = make([]string, 0, len(di.EmittedEvents))
var manifestEvent HybridEvent for k := range di.EmittedEvents {
keys = append(keys, k)
}
sort.Strings(keys)
for _, eventName := range keys {
var (
eventUsages = di.EmittedEvents[eventName]
manifestEvent HybridEvent
)
for _, e := range o.ContractEvents { for _, e := range o.ContractEvents {
if e.Name == eventName { if e.Name == eventName {
manifestEvent = e manifestEvent = e