-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
409 additions
and
15,274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.vscode/* | ||
go.sum | ||
vendor/ | ||
output.txt | ||
/*.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package Blockchain | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/pred695/code-challenge-2024-pred695/Structs" | ||
"github.com/pred695/code-challenge-2024-pred695/Utils" | ||
) | ||
|
||
var Bh Structs.BlockHeader = Structs.BlockHeader{ | ||
Version: 7, | ||
PrevBlockHash: "0000000000000000000000000000000000000000000000000000000000000000", | ||
MerkleRoot: "", | ||
Time: time.Now().Unix(), | ||
Bits: 0x1f00ffff, | ||
Nonce: 0, | ||
} | ||
|
||
func MineBlock() { | ||
netReward, TxIDs, _ := Utils.Prioritize() | ||
|
||
cbTx := Utils.CreateCoinbase(netReward) | ||
serializedcbTx, _ := Utils.SerializeTransaction(cbTx) | ||
fmt.Printf("CBTX: %x\n", serializedcbTx) | ||
TxIDs = append([]string{hex.EncodeToString(Utils.ReverseBytes(Utils.To_sha(Utils.To_sha(serializedcbTx))))}, TxIDs...) | ||
mkr := Utils.NewMerkleTree(TxIDs) | ||
Bh.MerkleRoot = hex.EncodeToString(mkr.Data) | ||
cbtxbase := Utils.CalculateBaseSize(cbTx) | ||
cbtxwitness := Utils.CalculateWitnessSize(cbTx) | ||
fmt.Println("Cbtx wt: ", cbtxwitness+(cbtxbase*4)) | ||
if ProofOfWork(&Bh) { | ||
file, _ := os.Create("output.txt") | ||
defer file.Close() | ||
// fmt.Println(Bh.merkleRoot) | ||
// fmt.Println(Bh.nonce) | ||
serializedBh := Utils.SerializeBlockHeader(&Bh) | ||
segserialized, _ := Utils.SegWitSerialize(cbTx) | ||
file.WriteString(hex.EncodeToString(serializedBh) + "\n") | ||
file.WriteString(hex.EncodeToString(segserialized) + "\n") | ||
for _, tx := range TxIDs { | ||
file.WriteString(tx + "\n") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package Blockchain | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/pred695/code-challenge-2024-pred695/Structs" | ||
"github.com/pred695/code-challenge-2024-pred695/Utils" | ||
) | ||
|
||
const target string = "0000ffff00000000000000000000000000000000000000000000000000000000" | ||
|
||
func CompareByteArrays(a, b []byte) int { | ||
if len(a) != len(b) { | ||
panic("Arrays must have the same length") | ||
} | ||
|
||
for i := range a { | ||
if a[i] < b[i] { | ||
return -1 | ||
} else if a[i] > b[i] { | ||
return 1 | ||
} | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func ProofOfWork(bh *Structs.BlockHeader) bool { | ||
targetBytes, _ := hex.DecodeString(target) | ||
// fmt.Printf("Target: %v\n", targetBytes) | ||
for { | ||
serialized := Utils.SerializeBlockHeader(bh) | ||
hash := Utils.ReverseBytes(Utils.To_sha(Utils.To_sha(serialized))) | ||
|
||
if CompareByteArrays(hash, targetBytes) == -1 { | ||
fmt.Println("Block Mined", hex.EncodeToString(hash)) | ||
return true | ||
} | ||
if bh.Nonce < 0x0 || bh.Nonce > 0xffffffff { | ||
fmt.Println("FUCKED") | ||
return false | ||
} | ||
bh.Nonce++ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package Structs | ||
|
||
|
||
type BlockHeader struct { | ||
Version uint32 | ||
PrevBlockHash string | ||
MerkleRoot string | ||
Time int64 | ||
Bits uint32 | ||
Nonce uint32 | ||
} | ||
|
||
|
||
type Input struct { | ||
TxID string `json:"txid"` | ||
Vout uint32 `json:"vout"` | ||
Prevout Prevout `json:"prevout"` | ||
Scriptsig string `json:"scriptsig"` | ||
ScriptsigAsm string `json:"scriptsig_asm"` | ||
Witness []string `json:"witness"` | ||
IsCoinbase bool `json:"is_coinbase"` | ||
Sequence uint32 `json:"sequence"` | ||
} | ||
|
||
type Prevout struct { | ||
Scriptpubkey string `json:"scriptpubkey"` | ||
ScriptpubkeyAsm string `json:"scriptpubkey_asm"` | ||
ScriptpubkeyType string `json:"scriptpubkey_type"` | ||
ScriptpubkeyAddress string `json:"scriptpubkey_address"` | ||
Value uint64 `json:"value"` | ||
} | ||
|
||
type Transaction struct { | ||
Version uint32 `json:"version"` | ||
Locktime uint32 `json:"locktime"` | ||
Vin []Input `json:"vin"` | ||
Vout []Prevout `json:"vout"` | ||
} | ||
|
||
type TxInfo struct { | ||
TxID string | ||
WTxID string | ||
Fee uint64 | ||
Weight uint64 | ||
} | ||
type TxWeight struct { | ||
BaseSize int `json:"base_size"` // Size of non-witness data in bytes | ||
WitnessSize int `json:"witness_size"` // Size of witness data in bytes | ||
Weight int `json:"weight"` // Total weight in weight units | ||
} | ||
|
||
|
||
type MerkleNode struct { | ||
Left *MerkleNode | ||
Data []byte | ||
Right *MerkleNode | ||
} | ||
|
||
type MerkleTree struct { | ||
MerkleRoot *MerkleNode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package Utils | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/pred695/code-challenge-2024-pred695/Structs" | ||
) | ||
|
||
func NewMerkleNode(lnode *Structs.MerkleNode, rnode *Structs.MerkleNode, data []byte) *Structs.MerkleNode { | ||
var mNode Structs.MerkleNode = Structs.MerkleNode{} | ||
if lnode == nil && rnode == nil { | ||
//hash256 of the data | ||
mNode.Data = ReverseBytes(data) | ||
} else { | ||
var prevHash []byte = append(lnode.Data, rnode.Data...) | ||
mNode.Data = To_sha(To_sha(prevHash)) | ||
} | ||
mNode.Left = lnode | ||
mNode.Right = rnode | ||
return &mNode | ||
} | ||
|
||
func NewMerkleTree(leaves []string) *Structs.MerkleNode { | ||
var nodes []Structs.MerkleNode | ||
|
||
for _, leaf := range leaves { | ||
data, _ := hex.DecodeString(leaf) | ||
var node Structs.MerkleNode = *NewMerkleNode(nil, nil, data) | ||
nodes = append(nodes, node) | ||
} | ||
|
||
for len(nodes) > 1 { | ||
var newLevel []Structs.MerkleNode | ||
for i := 0; i < len(nodes); i += 2 { | ||
// Handle case where the total number of nodes is odd. | ||
if len(nodes)%2 != 0 { | ||
nodes = append(nodes, nodes[len(nodes)-1]) | ||
} | ||
node := *NewMerkleNode(&nodes[i], &nodes[i+1], nil) | ||
newLevel = append(newLevel, node) | ||
} | ||
nodes = newLevel | ||
} | ||
return &nodes[0] | ||
|
||
} | ||
|
||
func CreateWitnessMerkle() string { | ||
_, _, wTxIDs := Prioritize() | ||
wTxIDs = append([]string{"0000000000000000000000000000000000000000000000000000000000000000"}, wTxIDs...) | ||
merkleRoot := NewMerkleTree(wTxIDs) | ||
fmt.Println("WMKR: ", hex.EncodeToString(merkleRoot.Data)) | ||
commitment_string := hex.EncodeToString(merkleRoot.Data) + "0000000000000000000000000000000000000000000000000000000000000000" | ||
WitnessCommitment, _ := hex.DecodeString(commitment_string) | ||
WitnessCommitment = To_sha(To_sha(WitnessCommitment)) | ||
fmt.Println("Witness Commitment: ", hex.EncodeToString(WitnessCommitment)) | ||
return hex.EncodeToString(WitnessCommitment) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.