sakura vpcのalma linuxでbitcoin coreを稼働したい

### sakura vpsの契約
1GB メモリ、100ssdで契約する。1ヶ月990円。
OSはalma linuxをインストール

### メモリ増強
1GBでは足りないらしいので、メモリを増強する
$ sudo fallocate -l 8G /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
no label, UUID=6a1c4a72-0340-40f3-be3a-9b7245afc3f0
$ sudo swapon /swapfile
$ free
total used free shared buff/cache available
Mem: 985552 126696 506488 31216 352368 689008
Swap: 8388604 0 8388604

### ポート解放
パケットフィルターでTCPの8333ポートを解放する

### alma linuxにbitcoin coreをinstall
$ sudo dnf install epel-release
$ sudo dnf upgrade
$ sudo dnf install snapd
$ sudo systemctl enable –now snapd.socket
$ sudo ln -s /var/lib/snapd/snap /snap
$ sudo snap install bitcoin-core
2023-10-31T08:08:07+09:00 INFO Waiting for automatic snapd restart…
bitcoin-core 25.1 from Bitcoin Core installed

### bit coin
$ bitcoin-core.cli –version
Bitcoin Core RPC client version v25.1.0
Copyright (C) 2009-2023 The Bitcoin Core developers

Please contribute if you find Bitcoin Core useful. Visit
for further information about the software.
The source code is available from .

This is experimental software.
Distributed under the MIT software license, see the accompanying file COPYING
or

$ snap info bitcoin-core
name: bitcoin-core
summary: Fully validating Bitcoin peer-to-peer network node, wallet and GUI
publisher: Bitcoin Core
store-url: https://snapcraft.io/bitcoin-core
contact: https://github.com/bitcoin-core/packaging/issues/new?title=snap:
license: unset
description: |
Bitcoin Core connects to the Bitcoin peer-to-peer network to download and
fully validate blocks and transactions. It also includes a wallet and
graphical user interface.
commands:
– bitcoin-core.cli
– bitcoin-core.daemon
– bitcoin-core.qt
– bitcoin-core.tx
– bitcoin-core.util
– bitcoin-core.wallet

$ bitcoin-core.cli -getinfo
error: timeout on transient error: Could not connect to the server 127.0.0.1:8332

Make sure the bitcoind server is running and that you are connecting to the correct RPC port.

### 再起動
VPS再起動

$ bitcoin-core.daemon -testnet

snapでinstallすると、実行コマンドが違うので注意が必要

shell scriptでメモリ使用率

#!/bin/bash

YMD=`date +"%Y/%m/%d %p %I:%M:%S"`

MEM_LOG="memory.log"

MEM_USED=`free | grep Mem | awk '{print($2-$3)/$2*100}'`
SWAP_USED=`free | grep Swap | awk '{print($3)/$2*100}'`

echo "$HOSTNAME memory is $MEM_USED %, date is $YMD" >> MEM_LOG
echo "$HOSTNAME swap is $SWAP_USED %, date is $YMD" >> MEM_LOG

vagrant memory is 86.4592 %, date is 2023/10/21 PM 07:25:24
vagrant swap is 4.532 %, date is 2023/10/21 PM 07:25:24

merkletree

こちらの記事を参考に動かしてみる
https://alis.to/gaxiiiiiiiiiiii/articles/3dy7vLZn0g89

トランザクションのハッシュ値作成の箇所は、Node(left.hash + right.hash)として、hash値を単純に横に繋げてそれをsha256でハッシュ化してるところが面白い。

from hashlib import sha256

class Node:
    def __init__(self, data):
        self.left     = None
        self.right    = None
        self.parent   = None
        self.sibling  = None
        self.position = None
        self.data     = data
        self.hash = sha256(data.encode()).hexdigest()
       
class Tree:
    def __init__(self, leaves):
        self.leaves = [Node(leaf) for leaf in leaves]
        self.layer  = self.leaves[::]
        self.root   = None
        self.build_tree()
    
    def build_layer(self):
        new_layer = []
        
        if len(self.layer) % 2 == 1:
            self.layer.append(self.layer[-1])
        
        for i in range(0, len(self.layer), 2):
            left = self.layer[i]
            right = self.layer[i+1]
            parent = Node(left.hash + right.hash)
            
            left.parent = parent
            left.sibling = right
            left.position = "left"
            
            right.parent = parent
            right.sibling = left
            right.position = "right"
            
            parent.left = left
            parent.right = right
            
            new_layer.append(parent)
        
        self.layer = new_layer
    
    def build_tree(self):
        while len(self.layer) > 1:
            self.build_layer()
        self.root = self.layer[0].hash
    
    def search(self, data):
        target = None
        hash_value = sha256(data.encode()).hexdigest()
        for node in self.leaves:
            if node.hash == hash_value:
                target = node
        return target
    
    def get_pass(self, data):
        target = self.search(data)
        markle_pass = []
        if not(target):
            return
        markle_pass.append(target.hash)
        while target.parent:
            sibling = target.sibling
            markle_pass.append((sibling.hash, sibling.position))
            target = target.parent
        return markle_pass   
      
def caluculator(markle_pass):
    value = markle_pass[0]
    for node in markle_pass[1:]:
        sib = node[0]
        position = node[1]
        if position == "right":
            value = sha256(value.encode() + sib.encode()).hexdigest()
        else:
            value = sha256(sib.encode() + value.encode()).hexdigest()
    return value  

VS code + parallels + vagrant + ubuntu2204

MacのM1チップではvurtualboxに対応していないため、vagrant + parallelsでubuntu22.04の環境を構築する

% sw_vers
ProductName: macOS
ProductVersion: 13.0
BuildVersion: 22A380

### vagrantインストールおよびubuntuの構築
$ brew install hashicorp/tap/hashicorp-vagrant
$ vagrant -v
$ vagrant plugin install vagrant-parallels
$ vagrant init bento/ubuntu-22.04-arm64

parallelsのsubscriptionを購入する。1年で1万円程度。
https://www.parallels.com/

$ vagrant up –provider=parallels
$ vagrant ssh
$ sudo apt update

### vsコードからの設定
vsコードをインストール
https://azure.microsoft.com/ja-jp/products/visual-studio-code

ExtensionsでRemoteDevelopmentをインストール
次いで、Parallels Desktopもインストールしておく(あまり意味はない)

左メニューのremote exploreでSSHの歯車アイコンから、
Users/${name}/.ssh/configを開く
ここに、vagrant ssh-configの値をそのまま入力する

  HostName 10.211.55.3
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/mac/MyVagrant/ubuntu2204/.vagrant/machines/default/parallels/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  PubkeyAcceptedKeyTypes +ssh-rsa
  HostKeyAlgorithms +ssh-rsa

ここで下記の記事のようにhostname, user, port, identityfileのみだとうまく接続出来ないので注意が必要
https://qiita.com/yamashin0616/items/87e042b3f5ec52e57017

後は接続するだけです。Cyberduck、sublimeから変えたいと思っていたので割と簡単に出来て良かった。

【python】マークルツリーのtx1とtx2を組み合わせたhash計算

マークルツリーでsha256のハッシュ計算を行う際に、単純にハッシュ値をつなぎ合わせて、それをさらにsha256で計算しているようだ。
トランザクション1とトランザクション2からのハッシュ値は何度実行しても同じになる。

import hashlib

tx1 = 'abc'
tx2 = 'cde'

tx1hash = hashlib.sha256(tx1.encode()).hexdigest()
print(tx1hash)
tx2hash = hashlib.sha256(tx2.encode()).hexdigest()
print(tx2hash)
tx12 = tx1hash + tx2hash
tx12hash = hashlib.sha256(tx12.encode()).hexdigest()
print(tx12hash)

$ python3 test.py
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
08a018a9549220d707e11c5c4fe94d8dd60825f010e71efaa91e5e784f364d7b
e7ea6d8ded8ff13bb4e3fccadb13878004f69bad2d3d9d33f071c13a650303ba

【Python】listのappend

appendとすると、リストの最後に追加される

names = ["Alice", "Bob", "Charlie"]

names.append("Dave")
print(names)

$ python3 test.py
[‘Alice’, ‘Bob’, ‘Charlie’, ‘Dave’]

カッコで囲んで二つ入れると

names = [(“Alice”,”20″), (“Bob”,”23″), (“Charlie”,”25″)]

names.append((“Dave”,”12″))
print(names)
print(names[0][1])
[/code]
基本的に一つしか入らないがタプルとして入れることもできる
$ python3 test.py
[(‘Alice’, ’20’), (‘Bob’, ’23’), (‘Charlie’, ’25’), (‘Dave’, ’12’)]
20

【Python】リストのコロン(:)の使い方: start, end, step

リストのコロンはstart, step, endという意味

for i in range(50)[::10]:
	print(i, end=' ')

$ python3 test.py
0 10 20 30 40

マイナスの場合は前に戻っていく

for i in range(50)[::-10]:
	print(i, end=' ')

$ python3 test.py
49 39 29 19 9

何も指定しない場合は全てのリストが処理される

for i in range(50)[::]:
	print(i, end=' ')

$ python3 test.py
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

【Python】classの__init__の使い方

pythonでの関数の呼び出し

def some_function(something):
	print(something)

some_function("hello world")

Classにした場合
L インスタンスを生成して処理を実行する
  L クラスの中の関数をメソッドと呼ぶ

class SomeClass:
	def __init__(self, something):
		self.something = something

	def some_function(self):
		print(self.something)


c = SomeClass("hello world")
c.some_function()
class SomeClass:
	def __init__(self, something):
		self.something = something

	def some_function(self):
		print(self.something)

	def some_function2(self):
		return self.something


c = SomeClass("hello world")
c.some_function()
print(c.some_function2())

selfはインスタンス自身のこと
initはデータの初期化の関数

class MyStatus:
	def __init__(self, age, name, height, weight):
		self.age = age
		self.name = name
		self.height = height
		self.weight = weight

	def print_name(self):
		print(self.name)

	def print_age(self):
		print(self.age)

	def print_height(self):
		print(self.height)

	def print_weight(self):
		print(self.weight)

a = MyStatus(14, "yamada", 160, 55)
a.print_height()	 

コーヒーメーカーのClassを作ってみる

class CoffeeMaker:
	def __init__(self, water, brand, tempreture):
		self.water = water # mill
		self.brand = brand
		self.tempreture = tempreture

	def boiling(self):
		boilingTime = self.water / 10 * self.tempreture
		print(boilingTime)

	def brew(self):
		print(self.brand + "を滴れました")

a = CoffeeMaker(300, "brend", 3)
a.boiling()	
a.brew() 

$ python3 test.py
90.0
brendを滴れました

なるほど、Classと関数とinitの使い方について、自分で例を作ると構造を理解できますね。

phpの場合はinitのところが__contructになります。引数はconstructで定義しています。

class Dog {
private $name;

public function __construct($name) {
$this->name = $name;
}

public function getName(){
echo $this->name;
}
}

$dog = new Dog(“Pochi”);

$dog->getName();
[/code]

ini_set('display_errors', "On");

class CoffeeShop {
	private $order;
	private $pay;

	public function __construct($order, $pay) {
		$this->order = $order;
		$this->pay = $pay;
	}

	public function coffeePrice(){
		switch ($this->order) {
			case "ブレンド":
				echo "price is 330 yen\n";
				$price = 330;
				break;
			case "アメリカン":
				echo "price is 350 yen\n";
				$price = 350;
				break;
			case "アイスコーヒー":
				echo "price is 380 yen\n";
				$price = 380;
				break;
			default:
				echo "not available at this store";
		}

		$change = $this->pay - $price;
		return $change;
	}

	
}

$a = new CoffeeShop("ブレンド", 1000);
$b = $a->coffeePrice();
echo $b;

【python】sha256

Pythonのハッシュ値を求める関数はhashlibにある

import hashlib

x = 'abc'

m = hashlib.md5(x.encode()).hexdigest()
sha256 = hashlib.sha256(x.encode()).hexdigest()
sha384 = hashlib.sha384(x.encode()).hexdigest()
sha512 = hashlib.sha512(x.encode()).hexdigest()


print(m)
print(sha256)
print(sha384)
print(sha512)

encodeはコードの符号化
hexdigest は16進数文字列に変換

$ python3 test.py
900150983cd24fb0d6963f7d28e17f72
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7
ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f