Regarding Oracle Node as a NEAR Protocol Contract implemented in Rust - rust

Recently, I have been executing the implementation of this repository:
https://github.com/smartcontractkit/near-protocol-contracts
So, everything is working fine in this...
But what I want to ask in this is:
When I executed the request command:
near call oracle.$NEAR_ACCT request '{"payment": "10", "spec_id": "dW5pcXVlIHNwZWMgaWQ=", "callback_address": "client.'$NEAR_ACCT'", "callback_method": "token_price_callback", "nonce": "11", "data_version": "1", "data": "eyJnZXQiOiJodHRwczovL21pbi1hcGkuY3J5cHRvY29tcGFyZS5jb20vZGF0YS9wcmljZT9mc3ltPUVUSCZ0c3ltcz1VU0QiLCJwYXRoIjoiVVNEIiwidGltZXMiOjEwMH0="}' --accountId client.$NEAR_ACCT --gas 300000000000000
The resultant transaction was:
https://explorer.testnet.near.org/transactions/Gr4ddg77Hj1KN2EB3W7vErc6aaDq8sNPfo6KnQWkN9rm
And then, I executed the fulfill_request command:
near call oracle.$NEAR_ACCT fulfill_request '{"account": "client.'$NEAR_ACCT'", "nonce": "11", "data": "Nzg2"}' --accountId oracle-node.$NEAR_ACCT --gas 300000000000000
Then, the resultant transaction was:
https://explorer.testnet.near.org/transactions/39XZF81s9vGDzbUkobQZJufGxAX7wNrPT336S8TEnk29
NOTE:
As we can see in the first command that is request, the data parameter that is passed is:
eyJnZXQiOiJodHRwczovL21pbi1hcGkuY3J5cHRvY29tcGFyZS5jb20vZGF0YS9wcmljZT9mc3ltPUVUSCZ0c3ltcz1VU0QiLCJwYXRoIjoiVVNEIiwidGltZXMiOjEwMH0=
When we base64 decode that, then it comes out to be as:
{"get":"https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD","path":"USD","times":100}
Similarly, in the second command that is fulfill_request, I passed the data parameter as:
Nzg2
When we base64 decode that, it comes out to be as:
786
And, that is also the result of the second transaction that you can see by scrolling to the midway of https://explorer.testnet.near.org/transactions/39XZF81s9vGDzbUkobQZJufGxAX7wNrPT336S8TEnk29.
We can see the result as:
Client contract received price: "786"
So, basically what I want is:
To get the response of https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD as the result of fulfill_request command, instead of hard-coded 786.

Related

GPT3 completion with insertion - invalid argument :suffix

I am trying out completions using insertions.
It seems that I am supposed to use a parameter called suffix: to inform where the end of the insert goes.
The payload to the endpoint: POST /v1/completions
{
"model": "code-davinci-002",
"prompt": "Write a JSON document for a person with first name, last name, email and phone number\n\n{\n",
"suffix": "\n}",
"temperature": 0,
"max_tokens": 256,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}
I tried doing this from a ruby implementation of GPT3.
parameters
=> {
:model=>"code-davinci-001",
:prompt=>"generate some JSON for a person with first and last name {",
:max_tokens=>250,
:temperature=>0,
:top_p=>1,
:frequency_penalty=>0,
:presence_penalty=>0,
:suffix=>"\n}"}
post(url: "/v1/completions", parameters: parameters)
I get an invalid argument error for suffix
{"error"=>{"message"=>"Unrecognized request argument supplied: suffix", "type"=>"invalid_request_error", "param"=>nil, "code"=>nil}}
I looked at the Payload from OpenAI vs the payload from the Ruby Library and saw the issue.
My ruby library was setting the model to code-davinci-001 while OpenAI was using code-davinci-002.
As soon as I manually altered the model: attribute in debug, the completion started working correctly.
{
"id"=>"cmpl-5yJ8b01Cw26W6ZIHoRSOb71Dc4QvH",
"object"=>"text_completion",
"created"=>1665054929,
"model"=>"code-davinci-002",
"choices"=>
[{"text"=>"\n \"firstName\": \"John\",\n \"lastName\": \"Smith\"",
"index"=>0,
"logprobs"=>nil,
"finish_reason"=>"stop"}],
"usage"=>{"prompt_tokens"=>14, "completion_tokens"=>19,
"total_tokens"=>33}
}

JSR223 PostProcessor error logs due to same json path in previous multiple response model

I have seen below error in the logs in JSR223 PostProcessor despite the script running successfully and the Jmeter steps having no issues.
Caused by: java.lang.NullPointerException: Cannot get property 'result' on null object
My JSR223 PostProcessor - groovy 3.0.7 script looks like this:
import groovy.json.JsonSlurper;
String json = prev.getResponseDataAsString();
def root = new JsonSlurper().parseText(json);
def C_accountNumber = root.myAccount.result[0].accountNumber;
vars.put("C_accountNumber", C_accountNumber);
The response just above the above script
{
"myAccount": {
"result": [
{
"accountNumber": "Something",
"accountAddress": "Something",
}
]
}
}
However, the thing I noticed was, well before the above script in multiple previous step responses also I see below path. (exactly same path myAccount.result[0] )
So I am guessing this caused the issue because as one of the previous responses, I can't see accountNumber despite having myAccount.result[0].
e.g.
{
"myAccount": {
"result": [
{
"country": "Something",
"address": "Something",
}
]
}
}
Because this part String json = prev.getResponseDataAsString(); in the groovy script go through all the previous responses but NOT just immediate before. Is my understanding correct?
Is there way to get rid of these errors in the log?
Your post processor should be defined as a child of the HTTP request
JMeter Post-Processor is execute in scope, for specific HTTP request add it as a child of the request
Post-Processor executes some action after a Sampler Request has been made. If a Post-Processor is attached to a Sampler element, then it will execute just after that sampler element runs.
Be aware of JMeter Scoping Rules
If you put the PostProcessor at the same level as several Samplers - it will be applied to all of them
So if you want to execute the PostProcessor after a certain sampler only - you need to make the PostProcessor a child of the particular Sampler:

How to force nodejs script argument to take value from choices list using commander?

I am using commander in node.js script. I am able to set default value to one argument.
var args = require('commander')
// set 'pending' as defaut value of status
args.option('-s --status <statusString>', 'Status to filter questions', 'pending').parse(process.argv)
console.log('status:', args.status)
How can I force status value to be from ["pending", "rejected", "accepted", "hold"] only? I did not find anything relevant in documention.
This is what I could achieve:
var options = ["pending", "rejected", "accepted", "hold"];
args.option(
'-s --status <statusString>',
`Status to filter questions: ${options}`,
function(val, _) {
if (!options.includes(val)) {
throw Error(`${val} is not from ${options}`);
}
return val;
},
'pending')
.parse(process.argv)
Not perfect, since you need to format help string and validate input value by yourself. Also, throwing an error from the validation function is not handled by Commander nicely and causes it to fail with the whole stacktrace in the output. I could not find a better way to tell Commander that the input is invalid.
In my case I finally just switched to argparse, which is a clone of the Python command line parser, and seems to be better thought over. This is how you limit choices with it:
const ArgumentParser = require('argparse').ArgumentParser;
const argparser = new ArgumentParser({ description: 'An example'});
argparser.addArgument('--status', {
choices: ['pending', 'rejected', 'accepted', 'hold'],
defaultValue: 'pending'});
const args = argparser.parseArgs();
This will do the job, including nice help message and input validation.
I think that in your case this is what you were looking for:
args
.addOption(
new Option('-s --status <statusString>', 'Status to filter questions')
.choices(["pending", "rejected", "accepted", "hold"])
.default('pending')
)
.parse(process.argv)
Also please take a look to another specific example I've prepared and tested successfully (NPM commander v8.2.0)
program
.addOption(
new Option('-s --status <statusString>', 'Status to filter questions')
.choices(["pending", "rejected", "accepted", "hold"])
.default('pending')
)
.action((args) => {
console.log(args)
})
Side note: please notice that for this second example I've used a slightly different naming convention for clarity: I've used program (vs original args) in the first line, as I was planning to use args name for the variable array received in the arrow function used in action() instead. Don't let that change confuse you! ;)
IMPORTANT: more on the official examples, direct link to a related example right here: https://github.com/tj/commander.js/blob/HEAD/examples/options-extra.js#L13

Azure Stream Analytics: Specified cast is not valid

I'm having a hard time tracking down a casting error in Azure Stream Analytics. The input data is coming from an Azure IoT Hub. Here's my query code:
-- Create average data from raw data
WITH
AverageSensorData AS
(
SELECT
[Node],
[SensorType],
udf.round(AVG([Value]), 2) AS [Value],
MAX(TRY_CAST([Timestamp] AS DateTime)) AS [Timestamp]
FROM [SensorData]
WHERE TRY_CAST([Timestamp] AS DateTime) IS NOT NULL
GROUP BY
[Node],
[SensorType],
TumblingWindow(minute, 2)
)
-- Insert average data into PowerBI-Output
SELECT
[Node],
[SensorType]
[Value],
[Timestamp],
DATETIMEFROMPARTS(
DATEPART(yy, [Timestamp]),
DATEPART(mm, [Timestamp]),
DATEPART(dd, [Timestamp]),
0, 0, 0, 0) AS [Date]
INTO [SensorDataAveragePowerBI]
FROM [AverageSensorData]
While this runs fine most of the time (at least for a couple of hundreds or thousands of input entities) it will eventually fail. After having turned on Diagnostic logs I was able to find the following eror message in the corresponding execution log (in reality it was in JSON format, I cleaned it up a little for readability):
Message: Runtime exception occurred while processing events, - Specified cast is not valid. OutputSourceAlias: averagesensordata; Type: SqlRuntimeError, Correlation ID: 49938626-c1a3-4f19-b18d-ee2c5a5332f9
And here's some JSON input that probably caused the error:
[
{
"Date": "2017-04-27T00:00:00.0000000",
"EventEnqueuedUtcTime": "2017-04-27T07:53:52.3900000Z",
"EventProcessedUtcTime": "2017-04-27T07:53:50.6877268Z",
"IoTHub": {
/* Some more data that is not being used */
},
"Node": "IoT_Lab",
"PartitionId": 0,
"SensorType": "temperature",
"Timestamp": "2017-04-27T09:53:50.0000000",
"Value": 21.06
},
{
"Date": "2017-04-27T00:00:00.0000000",
"EventEnqueuedUtcTime": "2017-04-27T07:53:53.6300000Z",
"EventProcessedUtcTime": "2017-04-27T07:53:52.0157515Z",
"IoTHub": {
/* Some more data that is not being used */
},
"Node": "IT_Services",
"PartitionId": 2,
"SensorType": "temperature",
"Timestamp": "2017-04-27T09:53:52.0000000",
"Value": 27.0
}
]
The first entry was the last one to go through, so the second one might have been the one breaking everything. I'm not sure, though, and do not see any suspicious values here. If I upload this as test data within the Azure portal then no errors are being raised.
The query above explicitely uses casting for the [Timestamp] column. But since I'm using TRY_CAST I wouldn't expect any casting errors:
Returns a value cast to the specified data type if the cast succeeds; otherwise, returns null.
As I said, the error only appears once in a while (sometimes after 20 minutes and sometimes after a couple of hours) and cannot be reproduced explicitely. Does anyone have an idea about where the error originates or if there is a chance of getting more detailed error information?
Thanks a lot in advance.
UPDATE: Here's the source of the udf.round function:
function main(value, decimalPlaces) {
if (typeof(value) === 'number'){
var decimalMultiplier = 1;
if (decimalPlaces){
decimalMultiplier = Math.pow(10, decimalPlaces);
}
return Math.round(value * decimalMultiplier) / decimalMultiplier
}
return value;
}
Unfortunately, it's been a while since I wrote this, so I'm not a hundred percent sure why I wrote exactly this code. One thing I do remember, though, is, that I all the analyzed messages always contained a valid number in the respective field. I still think that there's a good chance of this function being responsible for my problem.

Cannot access properties on a populate()'d object from Mongoose?

This is very odd... I'm using populate() with a ref to fill in an array within my schema, but then the properties are inaccessible. In other words, the schema is like this:
new Model('User',{
'name': String,
'installations': [ {type: String, ref: 'Installations'} ],
'count': Number,
}
Of course, Insallations is another model.
Then I find & populate a set of users...
model.find({count: 0}).populate('installations').exec( function(e, d){
for(var k in d)
{
var user = d[k];
for(var i in user.installations)
{
console.log(user.installations[i]);
}
}
} );
So far so good! I see nice data printed out, like this:
{ runs: 49,
hardware: 'macbookpro10,1/x86_64',
mode: 'debug',
version: '0.1' }
However, if I try to actually ACCESS any of those properties, they're all undefined! For example, if I add another console log:
console.log(user.installations[i].mode);
Then I see "undefined" printed for this log.
If I try to operate on the object, like this:
Object.keys(user.installations[i]).forEach(function(key) { } );
Then I get a typical "[TypeError: Object.keys called on non-object]" error, indicating that user.installations[i] is not an object (even though it is outputted to the console as if it were). So, I even tried something ugly like...
var install = JSON.parse(JSON.stringify(user.installations[i]));
console.log(install, install.mode);
And, again, the first output (install) is a nice object containing the property 'mode'... but the 2nd output is undefined.
What gives?
Finally, I solved this...
I tried doing a console.log(typeof user.installations[i]); and got "string" as the output. This seemed odd, given that printing the object directly created console output (above) that looked like a normal object, not a string. So, I tried doing a JSON.parse(); on the object, but received the error "SyntaxError: Unexpected token r"
Finally, I realized what was going on. The "pretty console output" I described above was the result of a string formatted with \n (newlines). I had not expected that, for whatever reason. The JSON.parse() error is due to the fact that there is a known necessity with the node.js parser when attempting to parse object keys without quotations; see the SO question here:
Why does JSON.parse('{"key" : "value"}') do just fine but JSON.parse('{key : "value"}') doesn't? .
Specifically, note that the JSON parser in my case is failing on the character 'r', the fist character of "runs," which is the first key in my JSON string (above). At first I was afraid I needed to get a custom JSON parser, but then the problem hit me.
Look back to my original schema. I used a String-type to define the installation ref, because the array field was storing the installations's _id property as a String. I assume the .populate() field is converting the object to a String on output.
Finally, I looked at the Mongoose docs a little closer and realized I'm supposed to be referencing the objects based upon Schema.ObjectID. This explains everything, but certainly gives me some fixing to do in my schemas and code elsewhere...

Resources