2018年8月30日

安裝Parity筆記

安裝rust

curl https://sh.rustup.rs -sSf | sh

安裝parity

  
git clone https://github.com/paritytech/parity-ethereum
cd parity-ethereum
git checkout stable //只安裝stable版本
cargo build --release --features final

或直接下載 binary :
https://github.com/paritytech/parity-ethereum/releases

缺少套件

  • libudev.h
yum install systemd-devel
  • cmake3:
下載cmak3 source (https://cmake.org/)
./bootstrap --prefix=/usr/local
make
make install

執行

./parity --jsonrpc-interface=w.x.y.z --jsonrpc-apis=eth,web3,personal,net,traces --jsonrpc-cors=all --chain=ropsten --jsonrpc-threads=1 --tracing=on
interface 表示要綁在哪一張網卡上

參數說明

  • --jsonrpc-interface 綁定rpc service的IP
  • --jsonrpc-apis 5 RPC支援的API
  • --jsonrpc-cors 允許CORS的網域
  • --chain 使用的ether網路名稱
  • --jsonrpc-threads RPC服務耗用CPU的資源
  • --tracing=on 開啟trace模式
  • --geth geth相容模式,讓RCP的指令可以跟geth相容一點點
  • --warp 使用snapshot方式同步最近的資料,之後有空的時候會慢慢同步前面的資料

config

  • parity也可以使用設定檔,設定檔位址在~/.local/share/io.parity.ethereum/config.toml
[parity]
mode = "active"
mode_timeout = 300
mode_alarm = 3600
auto_update = "critical"
release_track = "current"
public_node = false
no_download = false
no_consensus = false
no_persistent_txqueue = false

chain = "kovan"
base_path = "$HOME/.local/share/io.parity.ethereum"
db_path = "$HOME/.local/share/io.parity.ethereum/chains"
keys_path = "$HOME/.local/share/io.parity.ethereum/keys"
identity = ""
light = false

[rpc]
disable = false
port = 8545
interface = "w.x.y.z"
cors = ["all"]
apis = ["web3", "eth", "net", "personal", "traces"]
hosts = ["none"]
server_threads = 1

[websockets]
disable = true

[ipc]
disable = true

[footprint]
tracing = "on"

[misc]
logging = "own_tx=trace"
log_file = "/var/log/parity.log"
color = true

問題排除

  • 錯誤訊息 : TraceDB resync required
    修改~/.local/share/io.parity.ethereum/chains/test/user_defaults,把tracing改為true
  • 當啟動tracing模式的時候將無法使用warp sync

RPC 執行範例

  • Command
curl --data '{"method":"trace_filter","params":[{"fromBlock":"0x1","toBlock":"latest","fromAddress":["0x..............."]"]}],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST w.x.y.z:8545
curl -X POST --data '{"jsonrpc": "2.0", "id":"curltest", "method": "eth_getTransactionByHash", "params": ["0x..............."] }' -H 'content-type: application/json' http://w.x.y.z:8545
  • Result
{"jsonrpc":"2.0","result":[{....}],"id":1}

2018年6月13日

重設Mysql密碼(5.7 以上)

關閉mysql 服務:
sudo mysqld_safe --skip-grant-tables &
sudo /etc/init.d/mysqld stop
mysql -uroot 
登入後指令:
use mysql;          
update user set authentication_string=PASSWORD("") where User='root';
update user set plugin="mysql_native_password" where User='root';
flush privileges;
quit;
重設密碼:
mysqladmin -u root password

2018年4月17日

乙太坊筆記 – 建立虛擬鏈

  • 初始化私有ETH網路,可以用puppeth指令產生創世區塊的json內容
geth --datadir “./privatechain” init custom_genesis.json
  • 建立兩個節點,這樣之後一個節點可以提供rpc服務,一個可以用來挖礦
  • 如果是要在同一台機器上建立不同的節點,所有的節點都必須在獨立的目錄底下運作

        {
          "config": {
            "chainId": 657,
            "homesteadBlock": 1,
            "eip150Block": 2,
            "eip150Hash": "0x000000000000000000000000000000000...",
            "eip155Block": 3,
            "eip158Block": 3,
            "byzantiumBlock": 4,
            "ethash": {}
          },
          "nonce": "0x0",
          "timestamp": "0x5ad4459f",
          "extraData": "0x000000000000000000000000000000000...",
          "gasLimit": "0x47b760",
          "difficulty": "0x80000",
          "mixHash": "0x000000000000000000000000000000000...",
          "coinbase": "0x000000000000000000000000000000000...",
          "alloc": {
            "xxxx": {
              "balance": "0x2000000000000000000000000000000000..."
            }
          },
          "number": "0x0",
          "gasUsed": "0x0",
          "parentHash": "0x000000000000000000000000000000000..."
        }
        
  • custom_genesis.json 裡面可以放一些錢給測試用的帳號:
    "alloc": {
               "0xxxxxx":
                   {"balance": "10000"}
            }
        
  • 在console啟動節點
    • 常用參數
      • --networkid 為啟動在哪個網路id,避免衝突
      • --datadir 為指定區塊鏈資料位置
      • --ipcdisable 不加這個參數的話在本地啟動多個服務會出現access deny(CentOS沒這個問題,windows才會)
      • --config 後面可以加上自定義的TOML檔案來設定參數
      • 在<datadir>/geth/底下的static-nodes.json可以定義要和這個node同步的peers
      • 指令後面加上 dumpconfig 可以把設定值匯出成toml檔案
    • node1(監聽2000 port) :
    geth --datadir ./privatechain --networkid 1510 
    --port 2000 --ipcdisable console
            
    • node2(監聽2001 port) :
    geth --datadir ./privatechain --networkid 1510 
    --port 2001 --ipcdisable console
            
  • 讓兩個節點可以互相溝通
    • 在node1執行 admin.nodeInfo
      在輸出的結果中找到enode 資訊,會有類似底下的格式 :
      enode://xxxxx@[::]:2000,把enode的xxxx抄下來
    • 回到node2,執行
      admin.addPeer("enode://xxxxx@127.0.0.1:2000")
      127.0.0.1:2000 是node1監聽的位置
      照相同的步驟在node1把node2的端點加進去
      執行 admin.peers 檢查一下是否兩個node都互相連結了
    • 每次重啟node都需要重新做一次,可以直接toml設定檔來寫入設定,避免每次都要重新加
  • 在console模式底下的指令:
    • 查看帳戶
      eth.accounts

    • 建立帳號,設定密碼 123456
      personal.newAccount("123456")

      產生的私鑰會放在keystore目錄裡面
    • 解鎖帳號,這樣就不需要輸入密鑰也能交易
      personal.unlockAccount('0xxxxxx','pass')

  • 啟動RPC服務
    geth --datadir ./privatechain --networkid 1510 --rpc

會開始監聽 8545 port



  • 挖礦
    geth --datadir ./privatechain --networkid 1510 
            --port 2001 --ipcdisable --mine --minerthreads=1 
    --etherbase=0xxxxx 

    • etherbase 用來指定挖礦的coin要歸戶給哪個帳戶
    • minerthreads 表示要用幾個thread來挖礦



  • 交易
    Ethereum EVM的單位是Wei
    1 Ether = 1,000,000,000,000,000,000 Wei
    交易費用 = 實際用到的Gas * Gas Price
    如果Gas Limit不夠,交易將會無法完成,但是手續費會被扣掉
    如果Gas足夠,剩下的Gas會釋出所以不會多收手續費
  • 注意事項
    • 不管是要在哪一個node對虛擬鏈下交易指令,都必須在那一台node上啟動挖礦,否則交易都會卡在pending裡面(有時候在各個node可以查到交易成功,但是用了第三方套件例如metamask則會無法顯示正確交易結果)
    • 有時候挖礦指令會當掉,必須先
      miner.stop()
      miner.start(1)
      重新開啟
  • 每個node最後執行的指令
    • node 1 :
      geth --datadir ./privatechain --networkid 1510 
              --port 2000 --config config.toml --rpc 
      --rpcaddr x.x.x.x --rpccorsdomain "*" console
    • node 2:
      geth --datadir ./privatechain --networkid 1510 
              --port 2001 --config config.toml --mine 
      --minerthreads=1 --etherbase=0xxxxx console
  • 2018年3月15日

    Let's encrypt 支援Wildcard SSL

    也許是太多奧客客訴了,現在acme.sh 指令後面要加上 --yes-I-know-dns-manual-mode-enough-go-ahead-please

    Let's encrypt已經在3/13號開始支援wildcard ssl了,不過Wildcard SSL只能用dns-01做驗證,並且必須使用ACME 2.0。以下就記錄一下設定過程:

    1.安裝acme.sh

    專案網址為 https://github.com/Neilpang/acme.sh,可以在linux主機上安裝支援Bash, dash和sh的shell script
    目前certbot還不支援wildcard ssl,所以先用這個吧
    
    curl https://get.acme.sh | sh
    
    
    等他跑完就完成了,他會安裝在/.acme.sh/ 目錄底下,並協助設定環境變數。
    重新登出後或使用source .bashrc 指令更新一下環境變數就可以直接使用了

    2.取得驗證用的dns紀錄和生成需求(以*.example.com為例):

    
    acme.sh  --issue -d *.example.com --dns  \
      --yes-I-know-dns-manual-mode-enough-go-ahead-please
    
    
    接下來let's encrypt會請你設定一個名為 _acme-challenge.example.com 的DNS TXT Record,確定設定好之後再執行下面的指令。
    PS. 記住要確定設定好了再做(先用nslookup或dig 好好確認一下),不然他會再生出一組新的需求,並重新產生一個新的TXT內容,然後你就要再設定一次DNS。 

    3.再來就是要產生cert和key了

    如果成功會告訴你他把key和cert都放在/root/.acme.sh/*.example.com 底下。
    
    acme.sh  --renew -d *.example.com \ 
     --yes-I-know-dns-manual-mode-enough-go-ahead-please
    
     

    4.重新把你需要的cert和key發布到其他目錄:

    其實要自己搬過去也是可以啦
    
    acme.sh  --installcert  -d  *.example.com   \
            --cert-file /etc/nginx/ssl/example.com.cert \
            --key-file   /etc/nginx/ssl/example.com.private.key
     

    5.設定nginx 來支援ssl:

        
    ssl_certificate /etc/nginx/ssl/example.com.cert;
    ssl_certificate_key /etc/nginx/ssl/example.com.private.key;
    
    

    6.重啟nginx:

        
    service nginx restart
    
    

    2017年12月14日

    利用docker-compose建立tomcat和apache整合的服務

    本文為記錄公司專案中整合 apache和tomcat在docker上執行的實例,Apache套件是另外安裝,tomcat則是拿搭配JRE 1.7的版本 

    Dockerfile - Apache
    FROM ubuntu
    ENV TZ=Asia/Taipei
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    RUN apt-get update && apt-get install -y --no-install-recommends apache2 libapache2-mod-jk
    RUN a2enmod proxy
    RUN a2enmod proxy_http
    RUN a2enmod proxy_balancer
    RUN a2enmod proxy_wstunnel
    
    ADD ./conf/apache2.conf /etc/apache2/apache2.conf
    ADD ./conf/000-default.conf /etc/apache2/sites-enabled/000-default.conf
    ADD ./conf/worker.properties /etc/libapache2-mod-jk/workers.properties
    ADD ./conf/jk.conf /etc/apache2/mods-available/jk.conf
    VOLUME ["/var/log/apache2"]
    EXPOSE 80 443
    
    CMD ["apachectl", "-k", "start", "-DFOREGROUND"]

    conf/jk.conf
    JKMount /htdocs/* web_1

    conf/apache2.conf
    LoadModule jk_module modules/mod_jk.so
    JkWorkersFile /etc/libapache2-mod-jk/workers.properties
    JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
    Dockerfile - Tomcat

    FROM tomcat:7-jre7
    ENV TZ=Asia/Taipei
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    #把編譯好的war檔包進去
    COPY ./webapps/wales-test.war /usr/local/tomcat/webapps/
    # 一些tomcat的context 設定 
    COPY ./wales-test.xml /usr/local/tomcat/conf/Catalina/localhost/ 
    COPY ./lib/mysql-connector-java-5.1.28-bin.jar /usr/local/tomcat/lib/ 
    COPY ./lib/proxool-0.9.1.jar /usr/local/tomcat/lib/ 
    COPY ./lib/proxool-cglib.jar /usr/local/tomcat/lib/ 
    COPY ./lib/commons-logging-1.2.jar /usr/local/tomcat/lib/ 
    EXPOSE 8009

    docker-compose.yml
    
    version: "2" 
      services:
        tomcat:
          volumes:
          - ./tomcat/logs:/usr/local/tomcat/logs 
          expose:
          - 8009
          Image: wales /tomcat
          container_name: "tomcat"
          build: ./tomcat
          environment:
          - JAVA_OPTS=-Djava.security.egd=file:/dev/./urandom
          networks:
            wales_net:
                ipv4_address: 192.168.1.2
    
        http:
          volumes:
          - ./httpd/logs:/var/log/apache2
          ports:
          - "80:80"
          - "443:443"
          image: wales/httpd
          container_name: "httpd"
          build: ./httpd
          networks:
            wales_net:
                ipv4_address: 192.168.1.3
      
      #另外指定docker要用的網路區段
      networks:
        wales_net:
            driver: bridge
            ipam:
                driver: default 
                config:
                    subnet: 192.168.1.0/24
        
    啟動服務

    docker build -t devmaster/httpd httpd/
    docker build -t devmaster/tomcat tomcat/
    docker-compose up -d

    2017年3月25日

    Virtualbox - Host Only Adapter問題解決方案

           自從使用Windows 10之後使用VirtualBox就一直遇到困難,由於我會在Virtual Box的guest裡面安裝Centos,再利用 host來存取guest的服務,所以我會設定兩張網卡:一張是NAT,另一張則是僅限主機,但是在改用Windows 10之後就一直踢到鐵板,甚至連vagrant 和docker toolbar也不能使用。



           本來還想說難道是我自己的問題?為什麼台灣沒有半個人遇到?現在想想應該是大部分的工程師為了要讓自己潮一點都改用mac了吧

           在國外一些論壇已經有很多人發現這個問題,大部分都是指向HostOnly Adapter裡面的VirtualBox NDIS6 Bridged Networking Driver這個服務,只要將他disable就可以,但是見鬼的是我根本沒有這個服務,倒是多了一個Kaspersky Anti-virus NDIS Filter,所以這個方法對我來說也沒用。

           最後找到有另外一個和我同病相憐的網友,則是提供了在安裝Virtual Box就要先根除NDIS6的方法,安裝Virtual Box的時候順便加上以下的指令:
    VirtualBox-5.1.18-114002-Win  -msiparams NETWORKTYPE=NDIS5
     然後他就 Works like a charm了~

    2016年11月26日

    不需要利用ntpdate就可以校時

           網路上看到有人的Server需透過proxy才能上網,但是ntpdate不支援proxy設定;竟然想到用wget連到google去取得http response heade來作時間同步,真是有夠神。
    http_proxy=http://youruser:yourpassword@proxyip:proxyport;export http_proxy
    date -s "$(wget --no-cache -S -O /dev/null google.com 2>&1 | sed -n -e '/ *Date: */ {' -e s///p -e q -e '}')"
           來源網址 :

    精選文章

    利用Selenium IDE進行UI測試

    安裝與執行 Selenium IDE是Firefox的一個plugin,用來做UI的自動測試:下載網址為http://docs.seleniumhq.org/download/ 安裝後,點一下firefox 的工具 -> Selenium IDE ,就會...