Skip to content

Commit

Permalink
Merge pull request #82 from kaleido-io/contract-optimizer
Browse files Browse the repository at this point in the history
Add optimizer settings to contract build options
  • Loading branch information
peterbroadhurst authored Oct 21, 2024
2 parents 8844ec9 + f7ef47b commit 33e4774
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
53 changes: 53 additions & 0 deletions kaleido/platform/cms_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
Expand All @@ -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"`
Expand All @@ -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"`
Expand All @@ -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"`
Expand Down Expand Up @@ -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),
},
),
),
},
},
}
}
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions kaleido/platform/cms_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
`

Expand All @@ -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
}
}
`

Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 33e4774

Please sign in to comment.