From f7ef47b389015135d4c312293d787c6c15a73f1a Mon Sep 17 00:00:00 2001 From: Matthew Whitehead Date: Mon, 21 Oct 2024 16:38:55 +0100 Subject: [PATCH] Add optimizer settings to contract build options --- kaleido/platform/cms_build.go | 53 ++++++++++++++++++++++++++++++ kaleido/platform/cms_build_test.go | 12 +++++++ 2 files changed, 65 insertions(+) diff --git a/kaleido/platform/cms_build.go b/kaleido/platform/cms_build.go index be3e33b..90d1e2f 100644 --- a/kaleido/platform/cms_build.go +++ b/kaleido/platform/cms_build.go @@ -26,6 +26,8 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" @@ -47,6 +49,7 @@ type CMSBuildResourceModel struct { SolcVersion types.String `tfsdk:"solc_version"` Precompiled CMSBuildPrecompiledResourceModel `tfsdk:"precompiled"` GitHub CMSBuildGithubResourceModel `tfsdk:"github"` + Optimizer *CMSBuildOptimizerResourceModel `tfsdk:"optimizer"` SourceCode CMSBuildSourceCodeResourceModel `tfsdk:"source_code"` ABI types.String `tfsdk:"abi"` Bytecode types.String `tfsdk:"bytecode"` @@ -66,6 +69,12 @@ type CMSBuildGithubResourceModel struct { AuthToken types.String `tfsdk:"auth_token"` } +type CMSBuildOptimizerResourceModel struct { + Enabled types.Bool `tfsdk:"enabled"` + Runs types.Int64 `tfsdk:"runs"` + ViaIR types.Bool `tfsdk:"via_ir"` +} + type CMSBuildSourceCodeResourceModel struct { ContractName types.String `tfsdk:"contract_name"` FileContents types.String `tfsdk:"file_contents"` @@ -81,6 +90,7 @@ type CMSBuildAPIModel struct { EVMVersion string `json:"evmVersion,omitempty"` SolcVersion string `json:"solcVersion,omitempty"` GitHub *CMSBuildGithubAPIModel `json:"github,omitempty"` + Optimizer *CMSBuildOptimizerAPIModel `json:"optimizer,omitempty"` SourceCode *CMSBuildSourceCodeAPIModel `json:"sourceCode,omitempty"` ABI interface{} `json:"abi,omitempty"` Bytecode string `json:"bytecode,omitempty"` @@ -96,6 +106,12 @@ type CMSBuildGithubAPIModel struct { CommitHash string `json:"commitHash,omitempty"` } +type CMSBuildOptimizerAPIModel struct { + Enabled bool `json:"enabled,omitempty"` + Runs float64 `json:"runs,omitempty"` + ViaIR bool `json:"viaIR,omitempty"` +} + type CMSBuildSourceCodeAPIModel struct { ContractName string `json:"contractName,omitempty"` FileContents string `json:"fileContents,omitempty"` @@ -258,6 +274,38 @@ func (r *cms_buildResource) Schema(_ context.Context, _ resource.SchemaRequest, "commit_hash": &schema.StringAttribute{ Computed: true, }, + "optimizer": &schema.SingleNestedAttribute{ + Optional: true, + Computed: true, + Attributes: map[string]schema.Attribute{ + "enabled": &schema.BoolAttribute{ + Optional: true, + PlanModifiers: []planmodifier.Bool{boolplanmodifier.RequiresReplace()}, + }, + "runs": &schema.Int64Attribute{ + Optional: true, + PlanModifiers: []planmodifier.Int64{int64planmodifier.RequiresReplace()}, + }, + "via_ir": &schema.BoolAttribute{ + Optional: true, + PlanModifiers: []planmodifier.Bool{boolplanmodifier.RequiresReplace()}, + }, + }, + Default: objectdefault.StaticValue( + types.ObjectValueMust( + map[string]attr.Type{ + "enabled": types.BoolType, + "runs": types.Int64Type, + "via_ir": types.BoolType, + }, + map[string]attr.Value{ + "enabled": types.BoolValue(false), + "runs": types.Int64Value(0), + "via_ir": types.BoolValue(false), + }, + ), + ), + }, }, } } @@ -274,6 +322,11 @@ func (data *CMSBuildResourceModel) toAPI(api *CMSBuildAPIModel, isUpdate bool) { // different source being retrieved. api.EVMVersion = data.EVMVersion.ValueString() api.SolcVersion = data.SolcVersion.ValueString() + api.Optimizer = &CMSBuildOptimizerAPIModel{ + Enabled: data.Optimizer.Enabled.ValueBool(), + Runs: float64(data.Optimizer.Runs.ValueInt64()), + ViaIR: data.Optimizer.ViaIR.ValueBool(), + } switch data.Type.ValueString() { case "precompiled": _ = json.Unmarshal(([]byte)(data.Precompiled.ABI.ValueString()), &api.ABI) diff --git a/kaleido/platform/cms_build_test.go b/kaleido/platform/cms_build_test.go index 26ac19c..7c1ccc9 100644 --- a/kaleido/platform/cms_build_test.go +++ b/kaleido/platform/cms_build_test.go @@ -39,6 +39,11 @@ resource "kaleido_platform_cms_build" "cms_build1" { contract_name = "Firefly" auth_token = "token12345" } + optimizer = { + enabled = true + runs = 150 + via_ir = true + } } ` @@ -55,6 +60,11 @@ resource "kaleido_platform_cms_build" "cms_build1" { contract_name = "Firefly" auth_token = "token12345" } + optimizer = { + enabled = true + runs = 150 + via_ir = true + } } ` @@ -113,6 +123,7 @@ func TestCMSBuild1(t *testing.T) { "created": "%[2]s", "updated": "%[3]s", "name": "build1", + "optimizer":{"enabled": true, "runs": 150, "viaIR": true}, "path": "some/new/path", "description": "shiny contract that does things and stuff", "github": { @@ -195,6 +206,7 @@ func TestCMSBuildPreCompiled(t *testing.T) { "created": "%[2]s", "updated": "%[3]s", "name": "build2", + "optimizer": {}, "path": "some/path", "abi": [{"some":"precompiled_abi"}], "bytecode": "0xB17EC0DE",