Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.
Open
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
12 changes: 12 additions & 0 deletions examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ steps:
- run_python_test: { "script": "ml_examples.01_smart_contract_model.submit_contract", "mode": "boston"}
...
---
test_description: >
Tensor manipulation test
setup_conditions:
test_name: "tensor_test"
number_of_nodes: 1
mining_nodes: [0]
max_test_time: 60
steps:
- sleep: 5
- run_python_test: { "script": "tensor_manipulation.submit_contract"}
...
---
test_description: >
Basic contract submission test
setup_conditions:
Expand Down
79 changes: 79 additions & 0 deletions tensor_manipulation/submit_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os

from fetchai.ledger.api import LedgerApi
from fetchai.ledger.contract import Contract
from fetchai.ledger.crypto import Entity, Address

HERE = os.path.dirname(__file__)


# generic contract setup
def contract_setup(source, benefactor, options):
# Create keypair for the contract owner
entity1 = Entity()
Address(entity1)

host = options['host']
port = options['port']
# create the APIs
api = LedgerApi(host, port)

# Transfer tokens from benefactor
api.sync(api.tokens.transfer(benefactor, entity1, int(1e8), 1000))

# Create contract
contract = Contract(source, entity1)

# Deploy contract
api.sync(contract.create(api, entity1, api.tokens.balance(entity1)))

return api, contract, entity1


# run function for running on end to end tests
def run(options, benefactor):
source_file = os.path.join(HERE, "tensor_manipulation_contract.etch")

with open(source_file, "r") as fp:
source = fp.read()

print(options)

if benefactor is None or options is None:
raise Exception("Must give options and benefactor")

api, contract, entity = contract_setup(source, benefactor, options)

# add two tensors and get result
data_string = "2,3,4,5"
label_string = "1,2,3,4"
api.sync(contract.action(api, 'add', api.tokens.balance(entity),
[entity], data_string, label_string))
prediction = contract.query(api, 'getOutput')
print("Ouput is: " + prediction)

# subtract two tensors and get result
api.sync(contract.action(api, 'subtract', api.tokens.balance(entity),
[entity], data_string, label_string))
prediction = contract.query(api, 'getOutput')
print("Ouput is: " + prediction)

api.sync(contract.action(api, 'multiply', api.tokens.balance(entity),
[entity], data_string, label_string))
prediction = contract.query(api, 'getOutput')
print("Ouput is: " + prediction)

api.sync(contract.action(api, 'divide', api.tokens.balance(entity),
[entity], data_string, label_string))
prediction = contract.query(api, 'getOutput')
print("Ouput is: " + prediction)

api.sync(contract.action(api, 'copy', api.tokens.balance(entity),
[entity], data_string))
prediction = contract.query(api, 'getOutput')
print("Ouput is: " + prediction)

api.sync(contract.action(api, 'at', api.tokens.balance(entity),
[entity], data_string, 3))
prediction = contract.query(api, 'getOutput')
print("Ouput is: " + prediction)
158 changes: 158 additions & 0 deletions tensor_manipulation/tensor_manipulation_contract.etch
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//------------------------------------------------------------------------------
//
// Copyright 2019 Fetch.AI Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//------------------------------------------------------------------------------

persistent output_state : Tensor;

// initial set up creates the model and persistent data
@init
function setup(owner : Address)
use output_state;
var tensor_shape = Array<UInt64>(2);
tensor_shape[0] = 2u64;
tensor_shape[1] = 2u64;
var output = Tensor(tensor_shape);

output_state.set(output);
endfunction

// get the output state
@query
function getOutput() : String
use output_state;
var output = output_state.get();
return output.toString();
endfunction


// add two tensors and save to state
@action
function add(tensor1_string: String, tensor2_string: String)
var tensor_shape = Array<UInt64>(2);
tensor_shape[0] = 1u64;
tensor_shape[1] = 1u64;
var tensor1 = Tensor(tensor_shape);
tensor1.fromString(tensor1_string);

var tensor2 = Tensor(tensor_shape);
tensor2.fromString(tensor2_string);

use output_state;
var output = output_state.get();
output = tensor1 + tensor2;
output_state.set(output);
endfunction


// subtract two tensors and save to state
@action
function subtract(tensor1_string: String, tensor2_string: String)
var tensor_shape = Array<UInt64>(2);
tensor_shape[0] = 1u64;
tensor_shape[1] = 1u64;
var tensor1 = Tensor(tensor_shape);
tensor1.fromString(tensor1_string);

var tensor2 = Tensor(tensor_shape);
tensor2.fromString(tensor2_string);

use output_state;
var output = output_state.get();
output = tensor1 - tensor2;
output_state.set(output);
endfunction

@action
function multiply(tensor1_string: String, tensor2_string: String)
var tensor_shape = Array<UInt64>(2);
tensor_shape[0] = 1u64;
tensor_shape[1] = 1u64;
var tensor1 = Tensor(tensor_shape);
tensor1.fromString(tensor1_string);

var tensor2 = Tensor(tensor_shape);
tensor2.fromString(tensor2_string);

use output_state;
var output = output_state.get();
output = tensor1 * tensor2;
output_state.set(output);
endfunction

@action
function divide(tensor1_string: String, tensor2_string: String)
var tensor_shape = Array<UInt64>(2);
tensor_shape[0] = 1u64;
tensor_shape[1] = 1u64;
var tensor1 = Tensor(tensor_shape);
tensor1.fromString(tensor1_string);

var tensor2 = Tensor(tensor_shape);
tensor2.fromString(tensor2_string);

use output_state;
var output = output_state.get();
output = tensor1 / tensor2;
output_state.set(output);
endfunction

@action
function copy(tensor1_string: String)
var tensor_shape = Array<UInt64>(2);
tensor_shape[0] = 1u64;
tensor_shape[1] = 1u64;
var tensor1 = Tensor(tensor_shape);
tensor1.fromString(tensor1_string);

use output_state;
var output = output_state.get();
output = tensor1.copy();
output_state.set(output);
endfunction

@action
function at(tensor1_string: String, index: UInt64)
var tensor_shape = Array<UInt64>(2);
tensor_shape[0] = 1u64;
tensor_shape[1] = 1u64;
var tensor1 = Tensor(tensor_shape);
tensor1.fromString(tensor1_string);

use output_state;
tensor_shape = Array<UInt64>(1);
tensor_shape[0] = 1u64;
var output = Tensor(tensor_shape);
output.setAt(0u64, tensor1.at(0u64, index));
output_state.set(output);
endfunction

function main()
use output_state;
var tensor_shape = Array<UInt64>(2);
tensor_shape[0] = 2u64;
tensor_shape[1] = 2u64;
var output = Tensor(tensor_shape);

output_state.set(output);

var tensor1_string = "2, 3, 4, 5";
var tensor2_string = "1, 2, 3, 4";
add(tensor1_string, tensor2_string);
subtract(tensor1_string, tensor2_string);

printLn(getOutput());
endfunction