Skip to content

Commit 7b8338b

Browse files
committed
Add ContractMigrateVersion to AnalysisReport
1 parent b0193cc commit 7b8338b

File tree

6 files changed

+76
-3
lines changed

6 files changed

+76
-3
lines changed

ibc_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ func TestAnalyzeCode(t *testing.T) {
302302
require.NoError(t, err)
303303
require.False(t, report.HasIBCEntryPoints)
304304
require.Equal(t, "", report.RequiredCapabilities)
305+
require.Nil(t, report.ContractMigrateVersion)
305306

306307
// Store IBC contract
307308
wasm2, err := os.ReadFile(IBC_TEST_CONTRACT)
@@ -313,6 +314,7 @@ func TestAnalyzeCode(t *testing.T) {
313314
require.NoError(t, err)
314315
require.True(t, report2.HasIBCEntryPoints)
315316
require.Equal(t, "iterator,stargate", report2.RequiredCapabilities)
317+
require.Nil(t, report2.ContractMigrateVersion)
316318
}
317319

318320
func TestIBCMsgGetChannel(t *testing.T) {

internal/api/bindings.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ typedef struct UnmanagedVector {
194194
uintptr_t cap;
195195
} UnmanagedVector;
196196

197+
/**
198+
* A version of `Option<u64>` that can be used safely in FFI.
199+
*/
200+
typedef struct OptionalU64 {
201+
bool is_some;
202+
uint64_t value;
203+
} OptionalU64;
204+
197205
/**
198206
* The result type of the FFI function analyze_code.
199207
*
@@ -217,6 +225,13 @@ typedef struct AnalysisReport {
217225
* This is never None/nil.
218226
*/
219227
struct UnmanagedVector required_capabilities;
228+
/**
229+
* The migrate version of the contract.
230+
* This is None if the contract does not have a migrate version and the `migrate` entrypoint
231+
* needs to be called for every migration (if present).
232+
* If it is `Some(version)`, it only needs to be called if the `version` increased.
233+
*/
234+
struct OptionalU64 contract_migrate_version;
220235
} AnalysisReport;
221236

222237
typedef struct Metrics {

internal/api/lib.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,15 @@ func AnalyzeCode(cache Cache, checksum []byte) (*types.AnalysisReport, error) {
161161
}
162162
requiredCapabilities := string(copyAndDestroyUnmanagedVector(report.required_capabilities))
163163
entrypoints := string(copyAndDestroyUnmanagedVector(report.entrypoints))
164+
var contract_migrate_version *uint64
165+
if report.contract_migrate_version.is_some {
166+
contract_migrate_version = (*uint64)(&report.contract_migrate_version.value)
167+
}
164168
res := types.AnalysisReport{
165-
HasIBCEntryPoints: bool(report.has_ibc_entry_points),
166-
RequiredCapabilities: requiredCapabilities,
167-
Entrypoints: strings.Split(entrypoints, ","),
169+
HasIBCEntryPoints: bool(report.has_ibc_entry_points),
170+
RequiredCapabilities: requiredCapabilities,
171+
Entrypoints: strings.Split(entrypoints, ","),
172+
ContractMigrateVersion: contract_migrate_version,
168173
}
169174
return &res, nil
170175
}

libwasmvm/bindings.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ typedef struct UnmanagedVector {
194194
uintptr_t cap;
195195
} UnmanagedVector;
196196

197+
/**
198+
* A version of `Option<u64>` that can be used safely in FFI.
199+
*/
200+
typedef struct OptionalU64 {
201+
bool is_some;
202+
uint64_t value;
203+
} OptionalU64;
204+
197205
/**
198206
* The result type of the FFI function analyze_code.
199207
*
@@ -217,6 +225,13 @@ typedef struct AnalysisReport {
217225
* This is never None/nil.
218226
*/
219227
struct UnmanagedVector required_capabilities;
228+
/**
229+
* The migrate version of the contract.
230+
* This is None if the contract does not have a migrate version and the `migrate` entrypoint
231+
* needs to be called for every migration (if present).
232+
* If it is `Some(version)`, it only needs to be called if the `version` increased.
233+
*/
234+
struct OptionalU64 contract_migrate_version;
220235
} AnalysisReport;
221236

222237
typedef struct Metrics {

libwasmvm/src/cache.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ pub struct AnalysisReport {
253253
/// An UTF-8 encoded comma separated list of required capabilities.
254254
/// This is never None/nil.
255255
pub required_capabilities: UnmanagedVector,
256+
/// The migrate version of the contract.
257+
/// This is None if the contract does not have a migrate version and the `migrate` entrypoint
258+
/// needs to be called for every migration (if present).
259+
/// If it is `Some(version)`, it only needs to be called if the `version` increased.
260+
pub contract_migrate_version: OptionalU64,
256261
}
257262

258263
impl From<cosmwasm_vm::AnalysisReport> for AnalysisReport {
@@ -261,6 +266,8 @@ impl From<cosmwasm_vm::AnalysisReport> for AnalysisReport {
261266
has_ibc_entry_points,
262267
required_capabilities,
263268
entrypoints,
269+
contract_migrate_version,
270+
..
264271
} = report;
265272

266273
let required_capabilities_utf8 = set_to_csv(required_capabilities).into_bytes();
@@ -269,6 +276,30 @@ impl From<cosmwasm_vm::AnalysisReport> for AnalysisReport {
269276
has_ibc_entry_points,
270277
required_capabilities: UnmanagedVector::new(Some(required_capabilities_utf8)),
271278
entrypoints: UnmanagedVector::new(Some(entrypoints)),
279+
contract_migrate_version: contract_migrate_version.into(),
280+
}
281+
}
282+
}
283+
284+
/// A version of `Option<u64>` that can be used safely in FFI.
285+
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
286+
#[repr(C)]
287+
pub struct OptionalU64 {
288+
is_some: bool,
289+
value: u64,
290+
}
291+
292+
impl From<Option<u64>> for OptionalU64 {
293+
fn from(opt: Option<u64>) -> Self {
294+
match opt {
295+
None => OptionalU64 {
296+
is_some: false,
297+
value: 0, // value is ignored
298+
},
299+
Some(value) => OptionalU64 {
300+
is_some: true,
301+
value,
302+
},
272303
}
273304
}
274305
}

types/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ type AnalysisReport struct {
169169
HasIBCEntryPoints bool
170170
RequiredCapabilities string
171171
Entrypoints []string
172+
// ContractMigrateVersion is the migrate version of the contract
173+
// This is nil if the contract does not have a migrate version and the `migrate` entrypoint
174+
// needs to be called for every migration (if present).
175+
// If it is some number, the entrypoint only needs to be called if it increased.
176+
ContractMigrateVersion *uint64
172177
}
173178

174179
type Metrics struct {

0 commit comments

Comments
 (0)