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

[Constructor] Rosetta Constructor DSL #176

Merged
merged 39 commits into from
Oct 5, 2020
Merged

Conversation

patrick-ogrady
Copy link
Contributor

@patrick-ogrady patrick-ogrady commented Oct 4, 2020

Relates: coinbase/mesh-cli#137

This PR adds a simple DSL (domain-specific language) for writing Workflows. For now, we are just calling this Rosetta Constructor DSL but we could introduce a name for this "language" if it gets more sophisticated.

Old Way

[
  {
    "name": "request_funds",
    "concurrency": 1,
    "scenarios": [
      {
        "name": "find_account",
        "actions": [
          {
            "input": "{\"symbol\":\"tBTC\", \"decimals\":8}",
            "type": "set_variable",
            "output_path": "currency"
          },
          {
            "input": "{\"minimum_balance\":{\"value\": \"0\", \"currency\": {{currency}}}, \"create_limit\":1}",
            "type": "find_balance",
            "output_path": "random_account"
          }
        ]
      },
      {
        "name": "request",
        "actions": [
          {
            "type": "load_env",
            "output_path": "min_balance",
            "input": "MIN_BALANCE"
          },
          {
            "type": "math",
            "ouput_path": "adjusted_min",
            "input": "{\"operation\":\"addition\", \"left_value\": {{min_balance}}, \"right_value\": \"600\"}"
          },
          {
            "input": "{\"account_identifier\": {{random_account.account_identifier}}, \"minimum_balance\":{\"value\": {{adjusted_min}}, \"currency\": {{currency}}}}",
            "type": "find_balance",
            "output_path": "loaded_account"
          }
        ]
      }
    ]
  },
  {
    "name": "create_account",
    "concurrency": 1,
    "scenarios": [
      {
        "name": "create_account",
        "actions": [
          {
            "input": "{\"network\":\"Testnet3\", \"blockchain\":\"Bitcoin\"}",
            "type": "set_variable",
            "output_path": "network"
          },
          {
            "input": "{\"curve_type\": \"secp256k1\"}",
            "type": "generate_key",
            "output_path": "key"
          },
          {
            "input": "{\"network_identifier\": {{network}}, \"public_key\": {{key.public_key}}}",
            "type": "derive",
            "output_path": "account"
          },
          {
            "input": "{\"account_identifier\": {{account.account_identifier}}, \"keypair\": {{key}}}",
            "type": "save_account"
          }
        ]
      }
    ]
  }
]

New Way

request_funds(1){ 
  find_account{
    currency = {
      "symbol":"tBTC",
      "decimals":8
    };
    random_account = find_balance({
      "minimum_balance":{
        "value": "0",
        "currency": {{currency}}
      }, 
      "create_limit":1
    });
  },
  request{
    min_balance = load_env("MIN_BALANCE");
    adjusted_min = {{min_balance}} + 600;
    loaded_account = find_balance({
      "account_identifier": {{random_account.account_identifier}},
      "minimum_balance":{
        "value": {{adjusted_min}},
        "currency": {{currency}}
      }
    });
  }
}

create_account(1){
  create_account{
    network = {"network":"Testnet3", "blockchain":"Bitcoin"};
    key = generate_key({"curve_type":"secp256k1"});
    account = derive({
      "network_identifier": {{network}},
      "public_key": {{key.public_key}}
    });
    save_account({
      "account_identifier": {{account.account_identifier}},
      "keypair": {{key}}
    });
  }
}

Changes

  • Create a DSL
  • recognize set_variable or just equal
  • Write language rules in README (at most 1 function call per line)
  • Add README for constructor package
  • Add constructor to top level README
  • Add context for all calls
  • Populate custom error with last line read and line number
  • Create typed errors
  • Add native Math parsing
  • ensure referenced variables exist
    • Add previous scenario names to variable map
  • Ensure Workflows don't have same names
  • Ensure Scenarios don't have same name

Future PR

To build a wallet using this DSL, all you need to do is populate state with some values from a request before running.

@heimdall-asguard
Copy link

If Heimdall is stuck, please click the following link: https://heimdall-public.cbhq.net/v1/pull_request/stuck?pr_number=176&repository=coinbase/rosetta-sdk-go

@coveralls
Copy link

coveralls commented Oct 4, 2020

Pull Request Test Coverage Report for Build 8526

  • 299 of 319 (93.73%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.6%) to 78.415%

Changes Missing Coverage Covered Lines Changed/Added Lines %
constructor/dsl/dsl.go 36 39 92.31%
constructor/dsl/parser.go 256 273 93.77%
Totals Coverage Status
Change from base Build 8217: 0.6%
Covered Lines: 6521
Relevant Lines: 8316

💛 - Coveralls

@patrick-ogrady patrick-ogrady changed the title [Constructor] Create a DSL [Constructor] Babel: Rosetta Constructor DSL Oct 5, 2020
@patrick-ogrady patrick-ogrady changed the title [Constructor] Babel: Rosetta Constructor DSL [Constructor] Rosetta Constructor DSL Oct 5, 2020
Copy link
Contributor

@qiwu7 qiwu7 left a comment

Choose a reason for hiding this comment

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

🚀 🔥 this is awesome! great documentation on this :)

@patrick-ogrady patrick-ogrady merged commit 0de1c24 into master Oct 5, 2020
@patrick-ogrady patrick-ogrady deleted the patrick/workflow-dsl branch October 5, 2020 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants