[C++] テンプレートクラス, STL vector

クラスの雛形から色々な型を扱うクラスを作成できる。

template <class T>
class Array {
    private:
        T data[5];
    public:
        void setData(int num, T d);
        T getData(int num);
};

template <class T>
void Array<T>::setData(int num, T d){
    if ( num < 0 || num > 4)
        cout << "配列の範囲を超えています。\n";
    else
        data[num] = d;
}

template <class T>
T Array<T>::getData(int num){
    if (num < 0 || num > 4){
        cout << "配列の範囲を超えています\n";
        return data[0];
    } 
    else
        return data[num];
}
int main() {
    cout << "int型の配列を作成します\n";
    Array<int> i_array;
    i_array.setData(0, 80);
    i_array.setData(1, 60);
    i_array.setData(2, 58);
    i_array.setData(3, 77);
    i_array.setData(4, 57);

    for(int i=0; i<5; i++)
        cout << i_array.getData(i) << '\n';

    cout << "double型の配列を作成します。\n";
    Array<double> d_array;
    d_array.setData(0, 35.5);
    d_array.setData(1, 45.6);
    d_array.setData(2, 26.8);
    d_array.setData(3, 76.2);
    d_array.setData(4, 85.5);

    for (int j=0; j<5; j++)
        cout << d_array.getData(j) << '\n';

    return 0;
}

C++では standard template libraryをSTLと呼ぶ
vectorは数が決まっていない複数のデータを管理できる構造

#include <iostream>
#include <vector>
using namespace std;


int main() {
    
    int num;
    vector<int> vt;

    cout << "幾つ整数データを入力しますか?\n";
    cin >> num;

    for(int i=0; i<num; i++){
        int data;
        cout << "整数を入力してください。\n";
        cin >> data;
        vt.push_back(data);
    }

    cout << "表示します。\n";
    vector<int>::iterator it = vt.begin();
    while(it != vt.end()){
        cout << *it;
        cout << "-";
        it++;
    }
    cout << "\n";


    return 0;
}

vt.begin(), vt.end()で先頭・末尾のデータを知ることができる

### STLによるアルゴリズム

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int main() {
    
    vector<int> vt;
    for(int i=0; i<10; i++){
        vt.push_back(i);
    }

    cout << "並び替え前は";
    vector<int>::iterator it = vt.begin();
    while(it != vt.end()){
        cout << *it;
        it++;
    }
    cout << "です。\n";

    cout << "逆順にすると";
    reverse(vt.begin(), vt.end());
    it = vt.begin();
    while(it != vt.end()){
        cout << *it;
        it++;
    }
    cout << "です。\n";

    cout << "並び替え後は";
    sort(vt.begin(), vt.end());
    it = vt.begin();
    while(it != vt.end()){
        cout << *it;
        it++;
    }
    cout << "です。\n";


    return 0;
}

主なアルゴリズムにはcount, find, reverse, replace, remove, merge, sort, searchなどがある。

[C++] コピーコンストラクタ

オブジェクトが他のオブジェクトで初期化される場合に呼び出されるコンストラクタ
Car::Car(const Car& c){}

class Car {
    private:
        int num;
        double gas;
        char* pName;
    public:
        Car(char* pN, int n, double g);
        ~Car();
        Car(const Car& c);
        Car& operator=(const Car& c);
};

Car::Car(char* pN, int n, double g) {
    pName = new char[strlen(pN)+1];
    strcpy(pName, pN);
    num = n;
    gas = g;
    cout << pName << "を作成しました。\n";
}
Car::~Car(){
    cout << pName << "を破棄します。\n";
    delete[] pName;
}
Car::Car(const Car& c){
    cout << c.pName << "で初期化します。\n";
    pName = new char[strlen(c.pName) + strlen("のコピー1") + 1];
    strcat(pName, c.pName);
    strcat(pName, "のコピー1");
    num = c.num;
    gas = c.gas;
}

Car& Car::operator=(const Car& c){
    cout << pName << "に" << c.pName << "を代入します。\n";
    if(this != &c) {
        delete[] pName;
        pName = new char[strlen(c.pName) + strlen("のコピー2") + 1];
        strcat(pName, c.pName);
        strcat(pName, "のコピー2");
        num = c.num;
        gas = c.gas;
    }
    return *this;
}

int main() {
    Car mycar("mycar", 1234, 25.5);
    
    Car car1 = mycar;

    Car car2("car2", 0, 0);
    car2 = mycar;

    return 0;
}

[C++] クラスのメモリ確保

コンストラクタの中で動的にメモリを確保した場合、デストラクタ(destructor)を定義してメモリを解放する 
デストラクタはチルダをつける
デストラクタはオブジェクトの破棄のタイミングで自動的に解放される

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class Car {
    private:
        int num;
        double gas;
        char* pName;
    public:
        Car(char* pN, int n, double g);
        ~Car();
        void show();
};

Car::Car(char* pN, int n, double g) {
    pName = new char[strlen(pN)+1];
    strcpy(pName, pN);
    num = n;
    gas = g;
    cout << pName << "を作成しました。\n";
}
Car::~Car(){
    cout << pName << "を破棄します。\n";
    delete[] pName;
}
void Car::show(){
    cout << "車のナンバーは" << num << "です。\n";
    cout << "ガソリン量は" << gas << "です。\n";
    cout << "名前は" << pName << "です。\n";
}

int main() {
    Car car1("mycar", 1234, 25.5);
    car1.show();

    return 0;
}

[C++] 演算子のオーバーロード

– 演算子について新しい用法を定義することを演算子のオーバーロード(operator overloading)と言う
戻り値の型 operator 演算子(引数);

– 演算子の左オペランドがそのクラス以外の値を使うことをfriendをつけたfriend functionという

– 単項演算子をオーバーロードする
戻り値の型 operator 演算子();

class Point {
    private:
        int x;
        int y;
    public:
        Point(int a = 0, int b = 0) {x= a; y = b;}
        void setX(int a) {x=a;}
        void setY(int b) {y=b;}
        void show() {cout << " x:" << x << " y:" << y << '\n';}
        Point operator++();
        Point operator++(int d);
        friend Point operator+(Point p1, Point p2);
        friend Point operator+(Point p, int a);
        friend Point operator+(int a, Point p);
};

Point Point::operator++(){
    x++;
    y++;
    return *this;
}

Point Point::operator++(int d){
    Point p = *this;
    x++;
    y++;
    return p;
}

Point operator+(Point p1, Point p2){
    Point tmp;
    tmp.x = p1.x + p2.x;
    tmp.y = p1.y + p2.y;
    return tmp;
}
Point operator+(Point p, int a){
    Point tmp;
    tmp.x = p.x + a;
    tmp.y = p.y + a;
    return tmp;
}

Point operator+(int a, Point p) {
    Point tmp;
    tmp.x = a + p.x;
    tmp.y = a + p.y;
    return tmp;
}

int main() {
    Point p1(1, 2);
    Point p2(3, 6);
    p1 = p1+p2;
    p1++;
    p1 = p1 + 3;
    p2 = 3 + p2;

    p1.show();
    p2.show();

    return 0;
}

$ g++ -o sample sample.cpp && ./sample
x:8 y:12
x:6 y:9

[C++] classの階層

direct base classとindirect base classがある。
2つ以上のクラスを継承したクラスを多重継承(multiple inheritance)という
deriveは派生という意味

class Base1 {
    protected:
        int bs1;
    public:
        Base1(int b1=0) {bs1=b1;}
        void showBs1();
};

class Base2 {
    protected:
        int bs2;
    public:
        Base2(int b2=0) {bs2=b2;}
        void showBs2();
};

class Derived: public Base1, public Base2 {
    protected:
        int dr;
    public:
        Derived(int d=0){dr=d;}
        void showDr();
};

void Base1::showBs1() {
    cout << "bs1は" << bs1 << "です。\n";
}

void Base2::showBs2() {
    cout << "bs2は" << bs2 << "です。\n";
}

void Derived::showDr(){
    cout << "drは" << dr << "です。\n";
}

int main(){
    Derived drv;

    drv.showBs1();
    drv.showBs2();
    drv.showDr();
    return 0;
}

メンバ関数が同じ場合

int main(){
    Derived drv;

    drv.Base1::showBs();
    drv.Base2::showBs();
    drv.showDr();
    return 0;
}

仮想基本クラス(virtual base class)

class Base0 {
    protected:
        int bs0;
    public:
        Base0(int b0=0) {bs0=b0;}
        void showBs0();
};

class Base1 : public virtual Base0{
    protected:
        int bs1;
    public:
        Base1(int b1=0) {bs1=b1;}
        void showBs();
};

class Base2 : public virtual Base0{
    protected:
        int bs2;
    public:
        Base2(int b2=0) {bs2=b2;}
        void showBs();
};

class Derived: public Base1, public Base2 {
    protected:
        int dr;
    public:
        Derived(int d=0){dr=d;}
        void showDr();
};
void Base0::showBs0() {
    cout << "bs0は" << bs0 << "です。\n";
}

void Base1::showBs() {
    cout << "bs1は" << bs1 << "です。\n";
}

void Base2::showBs() {
    cout << "bs2は" << bs2 << "です。\n";
}

void Derived::showDr(){
    cout << "drは" << dr << "です。\n";
}

int main(){
    Derived drv;

    drv.showBs0();
    return 0;
}

[C++] 純粋仮想関数(pure virual function)

純粋仮想関数は仮想関数の宣言の最後に =0という指定をつけたもの
純粋仮想関数を1つでも持つ抽象クラスはオブジェクトを作成できない。

class Vehicle {
    protected:
        int speed;
    public:
        void setSpeed(int s);
        virtual void show() = 0;
};

class Car : public Vehicle {
    private:
        int num;
        double gas;
    public:
        Car(int n, double g);
        void show();
};

class RacingCar : public Car {
    private:
        int course;
    public:
        RacingCar();
        void setCourse(int c);
        void show();
};

class Plane : public Vehicle {
    private:
        int flight;
    public:
        Plane(int f);
        void show();
};

void Vehicle::setSpeed(int s){
    speed = s;
    cout << "速度を" << speed << "にしました。\n";
}

Car::Car(int n, double g) {
    num = n;
    gas = g;
    cout << "ナンバー" << num  << "ガソリン量" << gas << "の車を作成しました。\n";
}

void Car::show(){

    cout << "車のナンバーは" << num << "です。\n";
    cout << "ガソリン量は" << gas << "です。\n";
    cout << "速度は" << speed << "です。\n";
}

Plane::Plane(int f) {
    flight = f;
    cout << "便" << flight << "の飛行機を作成しました。\n";
}

void Plane::show() {
    cout << "飛行機の便は" << flight << "です。\n";
    cout << "速度は" << speed << "です。\n";
}


int main() {
    
    Vehicle* pVc[2];

    Car car1(1234, 20.5);
    pVc[0] = &car1;
    pVc[0]->setSpeed(60);

    Plane pln1(232);
    pVc[1] = &pln1;
    pVc[1]->setSpeed(500);

    for(int i=0; i<2; i++){
        pVc[i]->show();
    }

    return 0;
}

typeid演算子でオブジェクトのclassを調べることができる。
ヘッダにtypeinfoをインクルードする

int main() {
    
    Vehicle* pVc[2];

    Car car1(1234, 20.5);
    pVc[0] = &car1;

    Plane pln1(232);
    pVc[1] = &pln1;

    for(int i=0; i<2; i++) {
        if(typeid(*pVc[i]) == typeid(Car))
            cout << (i+1) << "番目は" << typeid(Car).name() << "です。\n";
        else
            cout << (i+1) << "番目は" << typeid(Car).name() << "ではありません。" << typeid(*pVc[i]).name() <<"です。\n";
    }

    return 0;
}

$ g++ -o sample sample.cpp && ./sample
ナンバー1234ガソリン量20.5の車を作成しました。
便232の飛行機を作成しました。
1番目は3Carです。
2番目は3Carではありません。5Planeです。

typeid(Car).name()だとoutputがおかしい。

[C++] 派生クラスと基本クラス

protectedとすると、派生クラスからアクセスできる。

class Car {
    protected:
        int num;
        double gas;
    public:
        Car();
        void setCar(int n, double g);
        void show();
};

class RacingCar : public Car {
    private:
        int course;
    public:
        RacingCar();
        void setCourse(int c);
        void show();
};

Car::Car() {
    num = 0;
    gas = 0.0;
    cout << "車を作成しました。\n";
}


void Car::setCar(int n, double g){
    num = n;
    gas = g;
    cout << "車のナンバーを" << num << "ガソリン量を" << gas << "にしました。\n";
}

void Car::show(){

    cout << "車のナンバーは" << num << "です。\n";
    cout << "ガソリン量は" << gas << "です。\n";
}

RacingCar::RacingCar(){
    course = 0;
    cout << "レーシングカーを作成しました。\n";
}

void RacingCar::setCourse(int c){
    course = c;
    cout << "コース番号を" << course << "にしました。\n";
}

void RacingCar::show(){
    cout << "レーシングカーのナンバーは" << num << "です。\n";
    cout << "ガソリンの量は" << gas << "です。\n";
    cout << "コース番号は" << num << "です。\n";

}

int main() {
    
    RacingCar rccar1;
    rccar1.setCar(1234, 20.5);
    rccar1.setCourse(5);

    rccar1.show();

    return 0;
}

$ g++ -o sample sample.cpp && ./sample
車を作成しました。
レーシングカーを作成しました。
車のナンバーを1234ガソリン量を20.5にしました。
コース番号を5にしました。
レーシングカーのナンバーは1234です。
ガソリンの量は20.5です。
コース番号は1234です。

派生クラスで定義したメンバ関数が機能することをオーバーライドという

[C++] classの様々な機能

拡張クラスが既存のクラスのメンバを受け継ぐことを継承(inheritance)と呼ぶ
基本クラス、新しいクラスは派生クラスと呼ぶ

### 継承

class Car {
    private:
        int num;
        double gas;
    public:
        Car();
        void setCar(int n, double g);
        void show();
};

class RacingCar : public Car {
    private:
        int course;
    public:
        RacingCar();
        void setCourse(int c);
};

Car::Car() {
    num = 0;
    gas = 0.0;
    cout << "車を作成しました。\n";
}

void Car::setCar(int n, double g){
    num = n;
    gas = g;
    cout << "車のナンバーを" << num << "ガソリン量を" << gas << "にしました。\n";
}

void Car::show(){

    cout << "車のナンバーは" << num << "です。\n";
    cout << "ガソリン量は" << gas << "です。\n";
}

RacingCar::RacingCar(){
    course = 0;
    cout << "レーシングカーを作成しました。\n";
}

void RacingCar::setCourse(int c){
    course = c;
    cout << "コース番号を" << course << "にしました。\n";
}

int main() {
    
    RacingCar rccar1;

    rccar1.setCar(1234, 20.5);
    rccar1.setCourse(5);
    
    return 0;
}

$ g++ -o sample sample.cpp && ./sample
車を作成しました。
レーシングカードを作成しました。
車のナンバーを1234ガソリン量を20.5にしました。
コース番号を5にしました。

### 基本クラスのコンストラクタを指定
RacingCar::RacingCar(int n, double g, int c): Car(n, g) が呼び出されるようになる。

class Car {
    private:
        int num;
        double gas;
    public:
        Car();
        Car(int n, double g);
        void setCar(int n, double g);
        void show();
};

class RacingCar : public Car {
    private:
        int course;
    public:
        RacingCar();
        RacingCar(int n, double g, int c);
        void setCourse(int c);
};

Car::Car() {
    num = 0;
    gas = 0.0;
    cout << "車を作成しました。\n";
}

Car::Car(int n, double g){
    num = n;
    gas = g;
    cout << "ナンバー" << num << "ガソリン量" << gas << "の車を作成しました。\n";
}

void Car::setCar(int n, double g){
    num = n;
    gas = g;
    cout << "車のナンバーを" << num << "ガソリン量を" << gas << "にしました。\n";
}

void Car::show(){

    cout << "車のナンバーは" << num << "です。\n";
    cout << "ガソリン量は" << gas << "です。\n";
}

RacingCar::RacingCar(){
    course = 0;
    cout << "レーシングカーを作成しました。\n";
}

RacingCar::RacingCar(int n, double g, int c): Car(n, g){
    course = 0;
    cout << "コース番号" << course << "のレーシングカーを作成しました。\n";
}

void RacingCar::setCourse(int c){
    course = c;
    cout << "コース番号を" << course << "にしました。\n";
}

int main() {
    
    RacingCar rccar1(1234, 20.5, 5);
    
    return 0;
}

[C++] classの機能: constructor

constructorは戻り値を持たない
constractorは className::className(引数)となる

class Car {
    private:
        int num;
        double gas;
    public:
        Car();
        void show();
};

Car::Car() {
    num = 0;
    gas = 0.0;
    cout << "車を作成しました。\n";
}

void Car::show(){

    cout << "車のナンバーは" << num << "です。\n";
    cout << "ガソリン量は" << gas << "です。\n";
}

int main() {
    
    Car car1;

    car1.show();
    
    return 0;
}

引数の数・型が異なっていれば、同じ名前の関数を複数定義できる。これをオーバーっロードという。

class Car {
    private:
        int num;
        double gas;
    public:
        Car();
        Car(int n, double g);
        void show();
};

Car::Car() {
    num = 0;
    gas = 0.0;
    cout << "車を作成しました。\n";
}

Car::Car(int n, double g){
    num = n;
    gas = g;
    cout << "車のナンバーは" << num << "ガソリン量は" << gas << "の車を作成しました。\n";
}


void Car::show(){

    cout << "車のナンバーは" << num << "です。\n";
    cout << "ガソリン量は" << gas << "です。\n";
}

int main() {
    
    Car car1;
    Car car2(1234, 20.5);
    
    return 0;
}

$ g++ -o sample sample.cpp && ./sample
車を作成しました。
車のナンバーは1234ガソリン量は20.5の車を作成しました。

### コンストラクタの応用
オブジェクtの配列を作成

int main() {
    
    Car mycars[3] = {
        Car(),
        Car(1234, 25.5),
        Car(4567, 52.2)
    };
    
    return 0;
}

$ g++ -o sample sample.cpp && ./sample
車を作成しました。
車のナンバーは1234ガソリン量は25.5の車を作成しました。
車のナンバーは4567ガソリン量は52.2の車を作成しました。

int main() {
    
    Car cars[3];
    
    return 0;
}

$ g++ -o sample sample.cpp && ./sample
車を作成しました。
車を作成しました。
車を作成しました。

class Car {
    private:
        int num;
        double gas;
    public:
        Car(int n=0, double g=0);
        void show();
};
//
int main() {
    
    Car car1;
    Car car2(1234, 20.5);
    
    return 0;
}

オブジェクトに関連づけられないメンバを静的メンバという
関数の前にstaticを付与する

class Car {
    private:
        int num;
        double gas;
    public:
        static int sum;
        Car();
        void setCar(int n, double g);
        void show();
        static void showSum();
};

Car::Car() {
    num = 0;
    gas = 0.0;
    sum++;
    cout << "車を作成しました。\n";
}

void Car::setCar(int n, double g){
    num = n;
    gas = g;
    cout << "車のナンバーは" << num << "ガソリン量は" << gas << "の車を作成しました。\n";
}

void Car::showSum(){
    cout << "車は全部で" << sum << "台あります。\n";
}

void Car::show(){

    cout << "車のナンバーは" << num << "です。\n";
    cout << "ガソリン量は" << gas << "です。\n";
}

int Car::sum = 0;

int main() {
    
    Car::showSum();

    Car car1;
    car1.setCar(1234, 20.5);

    Car::showSum();

    Car car2;
    car2.setCar(4567, 30.5);

    Car::showSum();
    
    return 0;
}

Proof of Work

from helper import hash256
block_id = hash256(bytes.fromhex('020000208ec39428b17323fa0ddec8e887b4a7c5\
3b8c0a0a220cfd0000000000000000005b0750fce0a889502d40508d39576821155e9c9e3f5c31\
57f961db38fd8b25be1e77a759e93c0118a4ffd71d'))[::-1]
print('{}'.format(block_id.hex()).zfill(64))

$ python3 test.py
0000000000000000007e9e4c586439b0cdbe13b1370bdd9435d76a644d047523

proof of workはビットコイン全てのブロックヘッダーのハッシュが一定のターゲットを下回らなければならないという要件
ターゲットはビットフィールドから計算される256ビットの数値
exponent(最後の1バイト)とcoefficient(残りの3バイト)
target = coefficient * 256^(exponent-3)

from helper import little_endian_to_int
bits = bytes.fromhex('e93c0118')
exponent = bits[-1]
coefficient = little_endian_to_int(bits[:-1])
target = coefficient * 256**(exponent - 3)
print('{:x}'.format(target).zfill(64))

$ python3 test.py
0000000000000000013ce9000000000000000000000000000000000000000000

有効なProof-of-workはブロックヘッダーハッシュでリトルエンディアン整数として解釈した時にターゲットを下回った時。

proof = little_endian_to_int(hash256(bytes.fromhex('020000208ec39428b17323\
fa0ddec8e887b4a7c53b8c0a0a220cfd0000000000000000005b0750fce0a889502d40508d3957\
6821155e9c9e3f5c3157f961db38fd8b25be1e77a759e93c0118a4ffd71d')))
print(proof < target)
def bits_to_target(bits):
    exponent = bits[-1]
    coefficient = little_endian_to_int(bit[:-1])
    return coefficient * 256**(exponent - 3)

### difficulty
difficulty = 0xffff * 256 ** (0x1d – 3) / target

from helper import little_endian_to_int, hash256
bits = bytes.fromhex('e93c0118')
exponent = bits[-1]
coefficient = little_endian_to_int(bits[:-1])
target = coefficient * 256**(exponent - 3)
difficulty = 0xffff * 256**(0x1d - 3) / target
print(difficulty)
    def target(self):
        return bits_to_target(self.bits)

    def difficulty(self):
        lowest = 0xffff * 256**(0x1d - 3)
        return lowest / self.target

### Proof-of-Workが十分であることの確認
Proof-of-Workはブロックヘッダーのhash256を計算し、これをリトルエンディアン整数として解釈することで求める
これがターゲットより小さい場合、Proof-of-workは有効

    def check_pow(self):
        sha = hash256(self.serialize())
        proof = little_endian_to_int(sha)
        return proof < self.target

### ディフィカルティ調整(difficulty adjustment period)
2016ブロックごとに変更
new_target = previous_target * time_differential/(2週間)

from io import BytesIO
from block import Block
from helper import TWO_WEEKS
last_block = Block.parse(BytesIO(bytes.fromhex('00000020fdf740b0e49cf75bb3\
d5168fb3586f7613dcc5cd89675b0100000000000000002e37b144c0baced07eb7e7b64da916cd\
3121f2427005551aeb0ec6a6402ac7d7f0e4235954d801187f5da9f5')))
first_block = Block.parse(BytesIO(bytes.fromhex('000000201ecd89664fd205a37\
566e694269ed76e425803003628ab010000000000000000bfcade29d080d9aae8fd461254b0418\
05ae442749f2a40100440fc0e3d5868e55019345954d80118a1721b2e')))
time_differential = last_block.timestamp - first_block.timestamp
if time_differential > TWO_WEEKS * 4:
    time_differential = TWO_WEEKS * 4
if time_differential < TWO_WEEKS // 4:
    time_differential = TWO_WEEKS // 4
new_target = last_block.target() * time_differential // TWO_WEEKS
print('{:x}'.format(new_target).zfill(64))

$ python3 test.py
0000000000000000007615000000000000000000000000000000000000000000

最後の2015ブロックを見つけるのに8週間以上かかる場合は、difficultyを減らしすぎないようにする
最後の2015ブロックを見つけるのに3.5日より短い場合は、difficultyを増やしすぎないようにする

def target_to_bits(target):
    raw_bytes = target.to_bytes(32, 'big')
    raw_bytes = raw_bytes.lstrip(b'\x00')
    if raw_bytes[0] > 0x7f:
        exponent = len(raw_bytes) + 1
        coefficient = b'\x00' + raw_bytes[:2]
    else:
        exponent = len(raw_bytes)
        coefficient = raw_bytes[:3]
    new_bits = coefficient[::-1] + bytes([exponent])
    return new_bits

ディフィカルティ調整期間の最初のblock idと最後のblock id
Block 471744
000000203471101bbda3fe307664b3283a9ef0e97d9a38a7eacd88000000000000000000
10c8aba8479bbaa5e0848152fd3c2289ca50e1c3e58c9a4faaafbdf5803c5448ddb84559
7e8b0118e43a81d3

Block 473759
02000020f1472d9db4b563c35f97c428ac903f23b7fc055d1cfc26000000000000000000
b3f449fcbe1bc4cfbcb8283a0d2c037f961a3fdf2b8bedc144973735eea707e126425859
7e8b0118e5f00474

これから、新しいbitを計算する

from io import BytesIO
from block import Block
from helper import TWO_WEEKS, target_to_bits
last_block = Block.parse(BytesIO(bytes.fromhex('000000203471101bbda3fe307664b3283a9ef0e97d9a38a7eacd88000000000000000000\
10c8aba8479bbaa5e0848152fd3c2289ca50e1c3e58c9a4faaafbdf5803c5448ddb84559\
7e8b0118e43a81d3')))
first_block = Block.parse(BytesIO(bytes.fromhex('02000020f1472d9db4b563c35f97c428ac903f23b7fc055d1cfc26000000000000000000\
b3f449fcbe1bc4cfbcb8283a0d2c037f961a3fdf2b8bedc144973735eea707e126425859\
7e8b0118e5f00474')))
time_differential = last_block.timestamp - first_block.timestamp
if time_differential > TWO_WEEKS * 4:
    time_differential = TWO_WEEKS * 4
if time_differential < TWO_WEEKS // 4:
    time_differential = TWO_WEEKS // 4
new_target = last_block.target() * time_differential // TWO_WEEKS
new_bits = target_to_bits(new_target)
print(new_bits.hex())

$ python3 test.py
80df6217

def calculate_new_bits(previous_bits, time_differential):
    if time_differential > TWO_WEEKS * 4:
        time_differential = TWO_WEEKS * 4
    if time_differential < TWO_WEEKS // 4:
        time_differential = TWO_WEEKS // 4
    new_target = bits_to_target(previous_bits) * time_differential // TWO_WEEKS
    return target_to_bits(new_target)