iskendria-pub / iskendria

Blockchain-based system for scientific publishing
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Program and deploy a simple smart contract written in Golang #4

Open mhdirkse opened 5 years ago

mhdirkse commented 5 years ago

[AX-4] created by mdi

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 5 years ago

by dirkse

mhdirkse commented 4 years ago

The aim is to learn how to program and deploy smart contracts. Creating the relevant contracts for Alexandria is not part of this user story.

mhdirkse commented 4 years ago

I needed the Golang SDK. I did the instructions given in: https://sawtooth.hyperledger.org/docs/core/nightly/master/app_developers_guide/go_sdk_import.html

The last command, "go generate" required a lot of dependencies before it worked. I am not sure whether I have all of them, but here are some:

C++ installation of Google Protocol Buffers: https://github.com/protocolbuffers/protobuf/blob/master/src/README.md

Python libraries: https://grpc.io/docs/quickstart/python.html

Installing Go support for protocol buffers: https://github.com/golang/protobuf/blob/master/README.md

Installing GO mocking framework: https://github.com/golang/mock

Installing GO interface for ZMQ 4: https://github.com/pebbe/zmq4

Install UUID package for Go: https://github.com/satori/go.uuid

mhdirkse commented 4 years ago

As a simple example, I am writing a smart contract that manages a crypto currency. No payment is implemented yet; everyone can create coin out of thin air.

I have coded a transaction processor with unit tests. The unit tests work.

mhdirkse commented 4 years ago

I am coding the client now. I have the skeleton of a console application. The skeleton support sub-commands and can produce a help screen.

mhdirkse commented 4 years ago

One of the sub-commands is generating keys. To make that possible, I needed a dependency:

go get -u github.com/btcsuite/btcd/btcec

mhdirkse commented 4 years ago

Installed ZMQ on Frodo, according to this link:

http://zeromq.org/distro:debian

mhdirkse commented 4 years ago

martijn@N750WU:~/go/src/github.com/mhdirkse/sawtooth-currency/client$ ./client Client tool for sawtoot-currency blockchain Set environment variable CURRENCY_SAWTOOTH_REST_IP_PORT before use

Subcommands: addCoin : addCoin createKey : createKey getSaldo : getSaldo testKey : testKey martijn@N750WU:~/go/src/github.com/mhdirkse/sawtooth-currency/client$ ./client createKey mdi3.pub mdi3.priv martijn@N750WU:~/go/src/github.com/mhdirkse/sawtooth-currency/client$ ./client testKey mdi3.pub mdi3.priv martijn@N750WU:~/go/src/github.com/mhdirkse/sawtooth-currency/client$ ./client testKey mdi3.pub mdi.priv Could not verify signature with keypair martijn@N750WU:~/go/src/github.com/mhdirkse/sawtooth-currency/client$ echo $? 1 martijn@N750WU:~/go/src/github.com/mhdirkse/sawtooth-currency/client$ ./client getSaldo mdi3.pub Reading http://77.93.70.138:8008/state/65ec1c348f9ae96b186f4e683c4d8c8da536350ba65aad2d99fd82b36b339435a731e6... Got status code 404 No saldo saved. martijn@N750WU:~/go/src/github.com/mhdirkse/sawtooth-currency/client$ ./client addCoin mdi3.pub 2 panic: runtime error: index out of range

goroutine 1 [running]: main.runAddCoin(0xc0000b4060, 0x2, 0x2) /home/martijn/go/src/github.com/mhdirkse/sawtooth-currency/client/client.go:188 +0x12ef main.handleSubCommand(0x77632f, 0x7, 0x784d9a, 0x35, 0x8adee8, 0xc0000b4060, 0x2, 0x2) /home/martijn/go/src/github.com/mhdirkse/sawtooth-currency/client/client.go:91 +0x44 main.main() /home/martijn/go/src/github.com/mhdirkse/sawtooth-currency/client/client.go:62 +0x21c martijn@N750WU:~/go/src/github.com/mhdirkse/sawtooth-currency/client$ ./client addCoin mdi3.pub mdi3.priv 2 Connecting to http://77.93.70.138:8008/batchesResponse: &{202 Accepted 202 HTTP/1.1 1 1 map[Content-Type:[application/json; charset=utf-8] Content-Length:[187] Date:[Wed, 20 Mar 2019 18:28:32 GMT] Server:[Python/3.5 aiohttp/2.3.2]] 0xc0004a2100 187 [] false false map[] 0xc000446000 } martijn@N750WU:~/go/src/github.com/mhdirkse/sawtooth-currency/client$ ./client getSaldo mdi3.pub Reading http://77.93.70.138:8008/state/65ec1c348f9ae96b186f4e683c4d8c8da536350ba65aad2d99fd82b36b339435a731e6... Got status code 200 Headers are: [application/json; charset=utf-8] [503] [Wed, 20 Mar 2019 18:28:47 GMT] [Python/3.5 aiohttp/2.3.2] Read 503 response bytes Response as string is: { "data": "CkYKQjAzZDk5MzUxZGNkNDRjNmE0MmYwODg5MTQ2NjBhOGU3OTVlMzRmZjI4ZTkyYjgzMGQ2MGQyMjdmOTliMjI1ODliYRAC", "head": "c160e3717bd355cc1e341e6256904c4660583aab15cbaec5438eabbbdcf72f2331a9d615416dd558beeb3123ddc4e33a672147b9fee710559d4ff304d9d76711", "link": "http://77.93.70.138:8008/state/65ec1c348f9ae96b186f4e683c4d8c8da536350ba65aad2d99fd82b36b339435a731e6?head=c160e3717bd355cc1e341e6256904c4660583aab15cbaec5438eabbbdcf72f2331a9d615416dd558beeb3123ddc4e33a672147b9fee710559d4ff304d9d76711" }

Saldo is 2 martijn@N750WU:~/go/src/github.com/mhdirkse/sawtooth-currency/client$

mhdirkse commented 4 years ago

I am reinstalling my development PC. I emptied my disk and I am installing everything anew.

When installing ZMQ, I chose to install version 4.3.1. This should not require libsodium. I had to install package pkg-config to make the installation succeed.

mhdirkse commented 4 years ago

The earlier comments do not make explicit that ZMQ has to be installed explicitly. It is a dependency of pebbe/zmq4.

mhdirkse commented 4 years ago

sudo -H python3 -m pip install grpcio

sudo -H python3 -m pip install grpcio-tools

mhdirkse commented 4 years ago

Very important: sudo apt-get install libssl-dev

mhdirkse commented 4 years ago

When running the Alexandria processor for the first time, I got a version conflict. It said:

mdi@frodo:~/hyperledger-sawtooth/singleNode$ ./processor panic: Failed to create ZMQ context: zmq4 was installed with ZeroMQ version 4.3.1, but the application links with version 4.2.1

goroutine 1 [running]: github.com/hyperledger/sawtooth-sdk-go/processor.(*TransactionProcessor).Start(0xc00009a1e0, 0xc000086cc0, 0x2) /home/martijn/go/src/github.com/hyperledger/sawtooth-sdk-go/processor/processor.go:87 +0x118 main.main() /home/martijn/go/src/gitlab.bbinfra.net/3estack/alexandria/processor/processor.go:21 +0x1f8 mdi@frodo:~/hyperledger-sawtooth/singleNode$

I could not upgrade ZMQ on the Frodo, because apt did not know version 4.3.1 of libzmq3-dev. Therefore I downgraded on my development PC to 4.2.1. ZMQ was built from source code on my development PC. I went to the ~/Downloads/zeromq-4.3.1 directory and did "sudo make uninstall". Then I downloaded a tarball for 4.2.1 and installed it with autogen.sh, configure, etc. I did ldconfig after installing.

I still got the same error. I am investigating further now.

mhdirkse commented 4 years ago

I checked whether the downgrade to ZMQ 4.2.1 on my development PC was successful. It was, as I saw with the following C++ program:

include

include

include

int main() { printf("ZMQ version: %d.%d.%d\n", ZMQ_VERSION_MAJOR, ZMQ_VERSION_MINOR, ZMQ_VERSION_PATCH); }

This code was saved in a file main.cpp. I compiled this C++ code using the following Makefile:

printZmqVersion: main.cpp gcc -lzmq main.cpp

When copy/pasting this, please make the second line start with a TAB character. Then build using the command "make". An executable file a.out results.

mhdirkse commented 4 years ago

I fixed the issue. I made an example GO program that only makes a Context object from pebbe/zmq4. That reproduced the error:

zmq4 was installed with ZeroMQ version 4.3.1, but the application links with version 4.2.1

Then in my pebbe checkout, I checked out a version tagged v1.0.0. That version is a few commits before master.