Skip to content

Instantly share code, notes, and snippets.

@jdevoo
Last active October 25, 2017 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdevoo/7ab242450b8117f73b2bc8076d5436fe to your computer and use it in GitHub Desktop.
Save jdevoo/7ab242450b8117f73b2bc8076d5436fe to your computer and use it in GitHub Desktop.

infrastructure

docker images installed

REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
ethereum/solc        stable              efe0ae340953        4 weeks ago         13.1MB
ethereum/client-go   alpine              314091749c85        5 weeks ago         40.3MB

init.bat

@echo off

REM remove previous data
if exist shared\conf\static-nodes.json del shared\conf\static-nodes.json shared\conf\acct* shared\conf\peer*

REM initialize chain with networkid 11
echo.
rmdir \Users\P3300011\playground\p2p\shared\peer1 /s/q
mkdir \Users\P3300011\playground\p2p\shared\peer1
docker run --rm -v /c/Users/P3300011/playground/p2p/shared/conf:/etc/geth -v /c/Users/P3300011/playground/p2p/shared/peer1:/root ethereum/client-go:alpine --datadir /root/.ethereum init /etc/geth/genesis.json
REM start node, calculate enode address and create account
pushd .
cd shared\peer1
docker run -d --rm -v /c/Users/P3300011/playground/p2p/shared/peer1:/root --name peer1 ethereum/client-go:alpine --nodiscover --networkid 11 --rpc --ipcpath /var/run/geth.ipc --identity peer1
REM wait 2 seconds
ping 127.0.0.1 -n 2 > nul
for /f %%i in ('docker exec peer1 /geth --exec "admin.nodeInfo.id" attach ipc:/var/run/geth.ipc') do set enodeid=%%i
for /f %%i in ('docker inspect -f "{{.NetworkSettings.IPAddress}}" peer1') do set ipaddress=%%i
echo|set /p=""enode://%enodeid:"=%@%ipaddress%:30303"" > ..\conf\peer1
for /f %%i in ('docker exec peer1 /geth --exec "personal.newAccount('passphrase1')" attach ipc:/var/run/geth.ipc') do set acct=%%i
echo|set /p="%acct:"=%" > ..\conf\acct1
popd

REM initialize chain with networkid 11
echo.
rmdir \Users\P3300011\playground\p2p\shared\peer2 /s/q
mkdir \Users\P3300011\playground\p2p\shared\peer2
docker run --rm -v /c/Users/P3300011/playground/p2p/shared/conf:/etc/geth -v /c/Users/P3300011/playground/p2p/shared/peer2:/root ethereum/client-go:alpine --datadir /root/.ethereum init /etc/geth/genesis.json
REM start node, calculate enode address and create account
pushd .
cd shared\peer2
docker run -d --rm -v /c/Users/P3300011/playground/p2p/shared/peer2:/root --name peer2 ethereum/client-go:alpine --nodiscover --networkid 11 --rpc --ipcpath /var/run/geth.ipc --identity peer2
ping 127.0.0.1 -n 2 > nul
for /f %%i in ('docker exec peer2 /geth --exec "admin.nodeInfo.id" attach ipc:/var/run/geth.ipc') do set enodeid=%%i
for /f %%i in ('docker inspect -f "{{.NetworkSettings.IPAddress}}" peer2') do set ipaddress=%%i
echo|set /p=""enode://%enodeid:"=%@%ipaddress%:30303"" > ..\conf\peer2
for /f %%i in ('docker exec peer2 /geth --exec "personal.newAccount('passphrase2')" attach ipc:/var/run/geth.ipc') do set acct=%%i
echo|set /p="%acct:"=%" > ..\conf\acct2
popd

REM collect enode addresses into static-nodes.json
echo.
> shared\conf\static-nodes.json (
        @echo [
        @type shared\conf\peer1
        @echo ,
        @type shared\conf\peer2
        @echo.
        @echo ]
)
docker cp shared\conf\static-nodes.json peer1:/root/.ethereum
docker cp shared\conf\static-nodes.json peer2:/root/.ethereum
type shared\conf\static-nodes.json

REM stop nodes and clean up
echo.
docker stop peer1 peer2
set enodeid=
set ipaddress=
set acct=

launch.bat

@echo off
docker run -d --rm -v /c/Users/P3300011/playground/p2p/shared/peer1:/root --name peer1 ethereum/client-go:alpine --nodiscover --networkid 11 --mine --minerthreads 1 --rpc --ipcpath /var/run/geth.ipc --identity peer1
docker run -d --rm -v /c/Users/P3300011/playground/p2p/shared/peer2:/root --name peer2 -p 8545:8545 ethereum/client-go:alpine --nodiscover --networkid 11 --rpc --rpcaddr "0.0.0.0" --rpccorsdomain "http://localhost:3000" --ipcpath /var/run/geth.ipc --identity peer2

length.bat

@docker exec peer2 /geth --exec "web3.eth.blockNumber" attach ipc:/var/run/geth.ipc

attach.bat

@docker exec -it %1 /geth attach ipc:/var/run/geth.ipc

balance.bat

@echo off
set /p acct=<.\shared\conf\%1
docker exec peer2 /geth --exec "web3.fromWei(eth.getBalance('%acct%'), 'ether')" attach ipc:/var/run/geth.ipc

transfer.bat

@echo off
if "%1" == "" goto usage

rem miner account
set /p acct1=<.\shared\conf\acct1

if not exist .\shared\conf\%2 goto missing
set /p acct2=<.\shared\conf\%2
if not "%acct2:~43,43%" == "" goto invalid

docker exec peer1 /geth --exec "personal.unlockAccount(eth.coinbase, 'passphrase1'); eth.sendTransaction({from:'%acct1%', to:'%acct2%', value:web3.toWei(%1, 'ether')})" attach ipc:/var/run/geth.ipc

set acct1=
set acct2=
exit /b

:usage
echo Transfer ether amount from miner to specified account
echo Usage:
echo   transfer ^<ether^> ^<acct^>
exit /b

:missing
echo Error: account missing
exit /b

:invalid
echo Not an account file

smart contracts

Dockerfile

FROM node:6.9.1-alpine

RUN apk add --update --no-cache \
  alpine-sdk libc6-compat python py-pip

RUN npm -g install truffle

USER node
WORKDIR /home/node

docker-compose.yaml

app:
  build: .
  volumes:
    - ./app:/home/node/app
  ports:
    - 3000:3000
  external_links:
    - peer2
  command: ./app/node_modules/http-server/bin/http-server -p 3000 app/www

make.bat

@echo off
docker run --rm -v /c/Users/P3300011/playground/p2p/doc-auth/contracts:/contracts ethereum/solc:stable --abi --overwrite -o /contracts/build /contracts/src/SingleSignerAuthority.sol
docker run --rm -v /c/Users/P3300011/playground/p2p/doc-auth/contracts:/contracts ethereum/solc:stable --bin --overwrite -o /contracts/build /contracts/src/SingleSignerAuthority.sol

deploy.bat

@echo off

> \Users\P3300011\playground\p2p\shared\peer2\deploy.js (
        @echo personal.unlockAccount^(eth.coinbase, 'passphrase2'^);
        @echo.
        @set /p=var doc_auth = web3.eth.contract(<nul
        @type contracts\build\SingleSignerAuthority.abi
        @echo ^);
        @echo.
        @set /p=var storageInstance = doc_auth.new({from: eth.coinbase, data: '0x<nul
        @type contracts\build\SingleSignerAuthority.bin
        @echo ', gas: 400000},
        @echo  function^(e, contract^) {
        @echo    if ^(!e^) {
        @echo      if ^(!contract.address^) {
        @echo        console.log^('transaction ' + contract.transactionHash + ' sent; waiting to be mined'^);
        @echo      } else {
        @echo        console.log^('contract mined at address: ' + contract.address^);
        @echo      }
        @echo    } else {
        @echo      console.log^(e^);
        @echo    }
        @echo  }^);
    @echo while ^(!eth.getTransactionReceipt^(storageInstance.transactionHash^)^) {}
)

docker exec -it peer2 /geth --jspath "/root" --exec "loadScript('deploy.js')" attach ipc:/var/run/geth.ipc
del \Users\P3300011\playground\p2p\shared\peer2\deploy.js

serve.bat

@docker-compose run -d --rm --service-ports app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment