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

All system contract compatibility and better tests #52

Merged
merged 30 commits into from
Aug 26, 2023
Merged

Conversation

aaroncox
Copy link
Member

@aaroncox aaroncox commented Aug 20, 2023

Alright… some updates!

I fixed the dot notation issue I mentioned with the eosio contract, which led to a bunch of other things - some of which I also managed to fix. But there’s more 😅

The dot notation thing was that in the system contract, there are tables with . in the name, which when passed in to the factory, was creating object properties with . in them, like powup.state: value and causing errors.

Fix was easy, using JSON.stringify(): f6cfcaf

I then went in and rewrote the tests to loop over and test against 4 contracts: eosio , eosio.token, eosio.msig and rewards.gm. All the testing data is setup to process these, but the tests are commented out preventing the failure. To enable them, uncomment here:

// import Eosio from '$test/data/contracts/mock-eosio'
import EosioToken from '$test/data/contracts/mock-eosio.token'
import RewardsGm from '$test/data/contracts/mock-rewards.gm'
// import EosioMsig from '$test/data/contracts/mock-eosio.msig'

and here:

// eosio: {
// mock: Eosio,
// generated: null,
// },
// 'eosio.msig': {
// mock: EosioMsig,
// generated: null,
// },

This is going to cause a number of errors that need to be fixed. There's a bunch of edge cases I don't think we noticed in our limited testing that these additional contracts are now showing. This includes:

abi.types

The "types" top level field of the ABI needs to be used to resolve mapping issues. These are "mappings" essentially between various types and the eosio contract uses them. At build time we need to read this data and link things properly. Here's the relevant part from the eosio contract abi and an example:

{
    "version": "eosio::abi/1.2",
    "types": [
        {
            "new_type_name": "block_signing_authority",
            "type": "variant_block_signing_authority_v0"
        }
    ],
    "structs": [
        {
            "name": "block_signing_authority_v0",
            "base": "",
            "fields": [
                {
                    "name": "threshold",
                    "type": "uint32"
                },
                {
                    "name": "keys",
                    "type": "key_weight[]"
                }
            ]
        },
        {
            "name": "regproducer2",
            "base": "",
            "fields": [
                {
                    "name": "producer",
                    "type": "name"
                },
                {
                    "name": "producer_authority",
                    "type": "block_signing_authority"
                },
                {
                    "name": "url",
                    "type": "string"
                },
                {
                    "name": "location",
                    "type": "uint16"
                }
            ]
        },
    ],
    "variants": [
        {
            "name": "variant_block_signing_authority_v0",
            "types": [
                "block_signing_authority_v0"
            ]
        }
    ]
}

You can see regproducer2 using block_signing_authority. This isn't a struct, it's a top level type, that maps to a variant. Resolving this will lead to the appropriate type...

block_signing_authority (regproducer2.producer_authority)
-> variant_block_signing_authority_v0 (from types on the abi)
    -> block_signing_authority_v0 (from variants on the abi)

This would mean that on the regproducer2 action, we should map it like this:

@Struct.type('regproducer2')
export class Regproducer2 extends Struct {
@Struct.field(Name)
producer!: Name
@Struct.field(BlockSigningAuthorityV0)
producer_authority!: BlockSigningAuthorityV0
@Struct.field('string')
url!: string
@Struct.field(UInt16)
location!: UInt16
}

On the above link, notice how I already corrected the "mock" version of the code to be what's expected 👍

hoisting

Within the type declarations of the generated code we're going to need to somehow do hoisting.

If you generate the eosio contract, you'll notice that going in order from the ABI the generated code will throw errors. It's because some types are being used before they are defined, like with ProducerKey and ProducerSchedule. We need the output code to detect this and then order them so these errors don't occur.

The abi2core library did this, and has a function which might solve this problem:

https://github.com/greymass/abi2core/blob/master/src/toposort.ts

missing types

Some types aren't resolving properly, you'll notice these if you run against the eosio and eosio.msig contracts. Things like block_timestamp_type or Varuint32. In the "mock" versions of the contracts I fixed these, but the generator needs to have a mapping for these.

If you simply run the tests you'll see them!

values ending with $

Unsure what's going on, but there are lots of $ at the end some values that aren't being removed. You'll see these again if you run the tests.

'UInt64',
'UInt8',
]
const EOSIO_CORE_CLASSES: string[] = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably rename this variable too 😄 Maybe to ANTELOPE_CLASSES?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agreed, didn't even think about that 😂

@@ -68,3 +69,21 @@ export function generateActionsNamespace(abi: ABI.Def): ts.ModuleDeclaration {
ts.NodeFlags.Namespace
)
}

function findParamTypeString(typeString: string, namespace: string | null, abi: ABI.Def): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method specific to interfaces? If not, I would put it in the helper.ts file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is exclusive to the interfaces, since it's only used for ActionParams as it finds the loose types like NameType or IntType.

import * as ts from 'typescript'
import {findAbiType} from './helpers'

export function generateTableMap(abi: ABI.Def): ts.VariableStatement {
// Map over tables to create the object properties
const tableProperties = abi.tables.map((table) =>
ts.factory.createPropertyAssignment(
String(table.name),
JSON.stringify(table.name),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right? lol....

return 'boolean'
}

if (['String', 'Boolean', 'Number'].includes(fieldType)) {
Copy link
Contributor

@dafuga dafuga Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, but if Boolean isn't a thing then we should probably remove it from this line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it is or isn't a thing, so I didn't want to remove it 😅

@@ -0,0 +1,77 @@
import {Action, Name} from '@wharfkit/antelope'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need a tests/helpers folder for functions like these 🤔

})
const contract = await contractKit.load('eosio.token')
const result = await contract.table('accounts', 'wharfkittest').get()
assert.instanceOf(result.balance, Asset)
Copy link
Contributor

@dafuga dafuga Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test really make sure that the correct scope is used? 🤔 Wouldn't the balance field always return an Asset?

Copy link
Contributor

@dafuga dafuga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left you some comments, but this looks great! I think that we can go ahead and merge this whenever you're ready

@aaroncox
Copy link
Member Author

@dafuga I didn't want to merge this yet. Instead I figured I'd just hand this branch off to you to work with, since the original comment on this PR still has a bunch of code generation stuff that needs to be addressed to fix the failing tests within.

@dafuga
Copy link
Contributor

dafuga commented Aug 25, 2023

Sounds good! I'll make a new PR using this as a base branch and we can just merge them both at the same time.

@aaroncox aaroncox changed the title Rewrote tests to remove recursion and throw All system contract compatibility and better tests Aug 26, 2023
@aaroncox aaroncox merged commit d1b2afd into dev Aug 26, 2023
3 checks passed
@dafuga dafuga deleted the better-tests branch August 28, 2023 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants