Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrument contract address + invoked function name #86

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions solarkraft/src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,26 @@ export function instrumentMonitor(

// Declaration of "Next" (according to `contractCall.fields` and `contractCall.method` / `.methodArgs`)
const envRecord = tlaJsonRecord([
{ name: 'height', kind: 'TlaInt', value: contractCall.height },
{ name: 'timestamp', kind: 'TlaInt', value: contractCall.timestamp },
// env.ledger().sequence()
{
name: 'sequence',
value: tlaJsonValEx('TlaInt', contractCall.height),
},
// env.ledger().timestamp()
{
name: 'timestamp',
value: tlaJsonValEx('TlaInt', contractCall.timestamp),
},
// env.current_contract_address()
{
name: 'current_contract_address',
value: tlaJsonValEx('TlaStr', contractCall.contractId),
},
// extra environment that's useful in monitor specifications
{
name: 'invoked_function_name',
value: tlaJsonValEx('TlaStr', contractCall.method),
},
])

const currentInstrumented = tlaJsonAnd(
Expand Down Expand Up @@ -247,6 +265,10 @@ export function tlaJsonOfNative(
forceVec: boolean = false,
hint?: any
): any {
assert(
v !== null && v !== undefined,
`Expected value, got null or undefined`
)
if (typeof v === 'object') {
if (Array.isArray(v)) {
// a JS array
Expand Down Expand Up @@ -434,14 +456,14 @@ function tlaJsonValEx(kind: string, value: any): any {
* @param bindings Interleaved array of field names and field values.
* @returns The record in Apalache IR JSON: https://apalache.informal.systems/docs/adr/005adr-json.html
*/
function tlaJsonRecord(bindings: any): any {
function tlaJsonRecord(bindings: { name; value }[]): any {
return {
type: 'Untyped',
kind: 'OperEx',
oper: 'RECORD',
args: bindings.flatMap((binding) => [
tlaJsonValEx('TlaStr', binding.name),
tlaJsonValEx(binding.kind, binding.value),
args: bindings.flatMap(({ name, value }) => [
tlaJsonValEx('TlaStr', name),
value,
]),
}
}
Expand Down
34 changes: 33 additions & 1 deletion solarkraft/test/unit/verify.instrumentedMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const instrumentedMonitor = {
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'height',
value: 'sequence',
},
},
{
Expand All @@ -111,6 +111,38 @@ const instrumentedMonitor = {
value: 1716393856,
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'current_contract_address',
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: '0xqwer',
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'invoked_function_name',
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'Claim',
},
},
],
},
{
Expand Down
34 changes: 33 additions & 1 deletion solarkraft/test/unit/verify.instrumentedMonitor2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const instrumentedMonitor = {
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'height',
value: 'sequence',
},
},
{
Expand All @@ -143,6 +143,38 @@ const instrumentedMonitor = {
value: 1716393856,
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'current_contract_address',
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: '0xqwer',
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'invoked_function_name',
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'Claim',
},
},
],
},
{
Expand Down
34 changes: 33 additions & 1 deletion solarkraft/test/unit/verify.instrumentedMonitor3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const instrumentedMonitor = {
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'height',
value: 'sequence',
},
},
{
Expand All @@ -86,6 +86,38 @@ const instrumentedMonitor = {
value: 1716393856,
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'current_contract_address',
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: '0xqwer',
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'invoked_function_name',
},
},
{
kind: 'ValEx',
type: 'Untyped',
value: {
kind: 'TlaStr',
value: 'foo',
},
},
],
},
{
Expand Down
Loading