Skip to content Skip to sidebar Skip to footer

Membuat Program Simulasi Mining (Nambang) Bitcoin dengan Visual Basic 6.0 (VB6)

Bitcoin (BTC) adalah mata uang digital terdesentralisasi pertama yang beroperasi secara peer-to-peer (P2P) tanpa perantara seperti bank, menggunakan teknologi blockchain sebagai buku besar publik untuk mencatat semua transaksi secara aman, transparan, dan tidak bisa diubah. Diciptakan oleh sosok misterius bernama Satoshi Nakamoto pada tahun 2009, Bitcoin memungkinkan transaksi langsung antar individu.

Untuk mendapatkan Bitcoin melalui penambangan (mining), Anda harus memecahkan teka-teki kriptografi yang rumit menggunakan perangkat keras komputer khusus dan daya komputasi yang sangat besar. Proses ini memvalidasi transaksi dan mengamankan jaringan Bitcoin, di mana penambang (miner) yang berhasil akan mendapatkan imbalan berupa Bitcoin baru dan biaya transaksi. 


Proses Mining

Hasil Mining (VScode)

Kali ini saya akan membuatkan simulasi program Mining Bitcoin dengan Visual Basic 6.0, program ini tidak menghasilkan Bitcoin secara nyata yang bisa di perjual belikan.

Berikut adalah langkah-langkah Membuat Program Simulasi Mining (Nambang) Bitcoin dengan Visual Basic 6.0 (VB6) :


1. Buka Form vb6 Standar EXE

2. Pada Form tanamkan bebebrapa Lebel :

Label1        Caption:Siap        name:lblStatus

Label2        Caption:Hashrate        name:lblHashrate

Label3        Caption:Difficulty        name:lblDifficulty

Label3        Caption:Balance        name:lblStatus

3. Tambahkan beberapa CommandButton :

Command1        Caption:Star        name:cmdStart

Command2        Caption:Stop        name:cmdStop

Command3        Caption:Save       name:cmdSave

Command4        Caption:Load        name:cmdLoad

4. Tambahkan 1 Timer dengan Interval:10  Enabled=False name:tmrMining

5. Tambahkan 1 Listbox dengan name:lstBlockchain

6. Desain Gambar seperti dibawah ini :

Desain Form Mining Bitcoin

7. Buka Jendela Kode copy-paste semua kode dibawah ini di bagian General-Declaration :


Dim LastBlockTime As Single

Dim BlockInterval As Single


Option Explicit


Private Type Transaction

    FromAddr As String

    ToAddr As String

    Amount As Double

End Type


Private Type Block

    Index As Long

    Timestamp As String

    nonce As Long

    PrevHash As String

    hash As String

    Difficulty As Long

    Reward As Double

End Type


Dim Blockchain(1 To 1000) As Block

Dim BlockCount As Long


Dim TxPool(1 To 100) As Transaction

Dim TxCount As Long


Dim Difficulty As Long

Dim Balance As Double

Dim Reward As Double

Dim HalvingInterval As Long


Dim HashCounter As Long

Dim StartTime As Single

Dim Mining As Boolean

Private Function SimpleSHA256(ByVal sInput As String) As String

    Dim i As Long

    Dim h As Long


    For i = 1 To Len(sInput)

        h = h + Asc(Mid$(sInput, i, 1)) * i

    Next i


    SimpleSHA256 = Hex$(h Mod 2147483647)

End Function

Private Sub CreateGenesisBlock()

    BlockCount = 1

    Blockchain(1).Index = 1

    Blockchain(1).Timestamp = Now

    Blockchain(1).nonce = 0

    Blockchain(1).PrevHash = "0"

    Blockchain(1).Difficulty = Difficulty

    Blockchain(1).Reward = 0

    Blockchain(1).hash = SimpleSHA256("GENESIS")


    lstBlockchain.AddItem "Genesis Block | Hash: " & Blockchain(1).hash

End Sub

Private Sub cmdStart_Click()


    Mining = True

    tmrMining.Enabled = True


    HashCounter = 0

    StartTime = Timer


    lblStatus.Caption = "Sedang Mining (SIMULASI)"

    UpdateButtonState


End Sub

Private Sub cmdStop_Click()

    Mining = False

    tmrMining.Enabled = False

    lblStatus.Caption = "Mining berhenti"


    UpdateButtonState

End Sub


Private Sub tmrMining_Timer()


    If Not Mining Then Exit Sub


    ' ===== HASHRATE SIMULASI =====

    HashCounter = HashCounter + 50   ' pura-pura banyak hash


    Dim elapsed As Single

    elapsed = Timer - StartTime

    If elapsed <= 0 Then elapsed = 1


    lblHashrate.Caption = "Hashrate: " & _

        Format(HashCounter / elapsed, "0") & " H/s"


    ' ===== BLOCK SETIAP 10 DETIK =====

    If LastBlockTime = 0 Or Timer - LastBlockTime >= BlockInterval Then

        AddBlock CLng(Rnd * 100000000), "SIMULATED_HASH"

    End If


End Sub

Private Sub AddBlock(ByVal nonce As Long, ByVal hash As String)


    LastBlockTime = Timer


    BlockCount = BlockCount + 1

    Blockchain(BlockCount).Index = BlockCount

    Blockchain(BlockCount).Timestamp = Now

    Blockchain(BlockCount).nonce = nonce

    Blockchain(BlockCount).PrevHash = Blockchain(BlockCount - 1).hash

    Blockchain(BlockCount).hash = hash

    Blockchain(BlockCount).Difficulty = Difficulty

    Blockchain(BlockCount).Reward = Reward


    Balance = Balance + Reward

    lblBalance.Caption = "Balance: " & _

        Format(Balance, "0.00000000") & " BTC"


    lstBlockchain.AddItem "Block #" & BlockCount & _

        " | Reward: " & Reward & " BTC"


    CheckHalving


End Sub

Private Sub CheckHalving()

    If BlockCount Mod HalvingInterval = 0 Then

        Reward = Reward / 2

        MsgBox "Halving! Reward sekarang: " & Reward & " BTC"

    End If

End Sub


Private Sub AdjustDifficulty()

    If BlockCount Mod 5 = 0 Then

        Difficulty = Difficulty + 1

        lblDifficulty.Caption = "Difficulty: " & Difficulty

    End If

End Sub

Private Sub cmdSave_Click()


    ' 1. Jangan izinkan save saat mining

    If Mining Then

        MsgBox "Stop mining dulu sebelum save!", vbExclamation

        Exit Sub

    End If


    ' 2. Tentukan lokasi file

    Dim sPath As String

    sPath = Environ("USERPROFILE") & "\Documents\blockchain.dat"


    ' 3. Simpan data

    Open sPath For Output As #1

        Print #1, BlockCount

        Print #1, Balance

        Print #1, Difficulty

        Print #1, Reward

    Close #1


    MsgBox "Data berhasil disimpan di:" & vbCrLf & sPath, vbInformation


End Sub


Private Sub cmdLoad_Click()


    If Mining Then

        MsgBox "Stop mining dulu sebelum load!", vbExclamation

        Exit Sub

    End If


    Dim sPath As String

    sPath = Environ("USERPROFILE") & "\Documents\blockchain.dat"


    If Dir(sPath) = "" Then

        MsgBox "File tidak ditemukan di Documents", vbExclamation

        Exit Sub

    End If


    Open sPath For Input As #1


        If EOF(1) Then

            MsgBox "Data file kosong / rusak"

            Close #1

            Exit Sub

        End If


        Input #1, BlockCount

        Input #1, Balance

        Input #1, Difficulty

        Input #1, Reward


    Close #1


    lblBalance.Caption = "Balance: " & _

        Format(Balance, "0.00000000") & " BTC"


    MsgBox "Data berhasil di-load", vbInformation


End Sub



Private Sub Form_Load()


    Mining = False

    Randomize


    Difficulty = 1        ' hanya tampilan

    Reward = 6.25

    HalvingInterval = 10

    Balance = 0

    HashCounter = 0


    BlockInterval = 10    ' ? 10 detik 1 block

    LastBlockTime = 0


    CreateGenesisBlock


    lblDifficulty.Caption = "Difficulty: SIMULASI"

    lblBalance.Caption = "Balance: 0 BTC"

    lblHashrate.Caption = "Hashrate: 0 H/s"

    lblStatus.Caption = "Siap"


End Sub

Private Sub UpdateButtonState()

    cmdStart.Enabled = Not Mining

    cmdStop.Enabled = Mining

    cmdLoad.Enabled = Not Mining

    cmdSave.Enabled = Not Mining


End Sub


Private Sub UpdateStatus()

    If Mining Then

        lblStatus.Caption = "Status: MINING"

    Else

        lblStatus.Caption = "Status: IDLE"

    End If

End Sub


8. Jalankan Project Anda kemudian tekan Star untuk memulai Mining Bitcoin, Stop untuk berhenti , Save untuk menyimpan hasil Mining.

Catatan :

Untuk melihat hasil Mining cek di Documents dengan nama File blockchain.dat, buka file tersebut dengan Prorgam VScode

Post a Comment for "Membuat Program Simulasi Mining (Nambang) Bitcoin dengan Visual Basic 6.0 (VB6)"