From 5ba835d25b6aa67dfbbf059409b789da067cce6d Mon Sep 17 00:00:00 2001 From: Evgenii Date: Fri, 15 Feb 2019 16:38:16 +0300 Subject: [PATCH] compiler: process packages in deterministic order Imported from CityOfZion/neo-storm#60 PR. --- pkg/vm/compiler/codegen.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/vm/compiler/codegen.go b/pkg/vm/compiler/codegen.go index e91c11d49..70ba960e9 100644 --- a/pkg/vm/compiler/codegen.go +++ b/pkg/vm/compiler/codegen.go @@ -8,6 +8,7 @@ import ( "go/token" "go/types" "log" + "sort" "strconv" "strings" @@ -773,8 +774,16 @@ func CodeGen(info *buildInfo) (*bytes.Buffer, error) { // convert the entry point first c.convertFuncDecl(mainFile, main) + // sort map keys to generate code deterministically + keys := make([]*types.Package, 0, len(info.program.AllPackages)) + for p := range info.program.AllPackages { + keys = append(keys, p) + } + sort.Slice(keys, func(i, j int) bool { return keys[i].Path() < keys[j].Path() }) + // Generate the code for the program - for _, pkg := range info.program.AllPackages { + for _, k := range keys { + pkg := info.program.AllPackages[k] c.typeInfo = &pkg.Info for _, f := range pkg.Files {