Skip to content

Commit

Permalink
push 1
Browse files Browse the repository at this point in the history
  • Loading branch information
pred695 committed Apr 14, 2024
1 parent bd59aed commit 58f4237
Show file tree
Hide file tree
Showing 33 changed files with 1,814 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*.txt
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cSpell.words": [
"Merkle"
],
"cSpell.ignoreWords": [
"lnode",
"rnode"
]
}
61 changes: 61 additions & 0 deletions Prioritize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"encoding/hex"
"encoding/json"
"os"
"sort"
)

type TxInfo struct {
TxID string
WTxID string
Fee uint64
Weight uint64
}

func comp(a, b TxInfo) bool {
return float64(a.Fee)/float64(a.Weight) > float64(b.Fee)/float64(b.Weight)
}
func Prioritize() (uint64, []string, []string) {
var permittedTxIDs []string
var permittedWTxIDs []string
dir := "./mempool"
files, _ := os.ReadDir(dir)
var txInfo []TxInfo
for _, file := range files {
txData, err := jsonData(dir + "/" + file.Name())
Handle(err)
var tx Transaction
err = json.Unmarshal([]byte(txData), &tx)
var fee uint64 = 0
for _, vin := range tx.Vin {
fee += vin.Prevout.Value
}
for _, vout := range tx.Vout {
fee -= vout.Value
}
serialized, _ := serializeTransaction(&tx)
segserialized, _ := SegWitSerialize(&tx)
txID := to_sha(to_sha(serialized))
wtxID := to_sha(to_sha(segserialized))
txInfo = append(txInfo, TxInfo{TxID: hex.EncodeToString(txID), WTxID: hex.EncodeToString(wtxID), Fee: fee, Weight: uint64(calculateWitnessSize(&tx) + CalculateBaseSize(&tx)*4)})

}
sort.Slice(txInfo, func(i, j int) bool {
return comp(txInfo[i], txInfo[j])
})
var PermissibleTxs []TxInfo
var PermissibleWeight uint64 = 4000000
var reward uint64 = 0
for _, tx := range txInfo {
if PermissibleWeight >= tx.Weight {
PermissibleTxs = append(PermissibleTxs, tx)
PermissibleWeight -= tx.Weight
permittedTxIDs = append(permittedTxIDs, tx.TxID)
permittedWTxIDs = append(permittedWTxIDs, tx.WTxID)
reward += tx.Fee
}
}
return reward, permittedTxIDs, permittedWTxIDs
}
150 changes: 150 additions & 0 deletions address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package main

import (
"encoding/json"
"fmt"
"os"
)

var (
ct_p2pkh = 0
ct_p2sh = 0
ct_p2wpkh = 0
ct_p2wsh = 0
)

func Address() {
dir := "./mempool"
files, err := os.ReadDir(dir)
Handle(err)
for _, file := range files {
txData, err := jsonData(dir + "/" + file.Name())
Handle(err)
var tx Transaction
err = json.Unmarshal([]byte(txData), &tx)
Handle(err)
for _, vin := range tx.Vin {
if vin.Prevout.ScriptpubkeyType == "p2pkh" {
pubkey_asm := vin.Prevout.ScriptpubkeyAsm
address := p2pkh(pubkey_asm)
if string(address) == vin.Prevout.ScriptpubkeyAddress {
// fmt.Println(vin.Prevout.ScriptpubkeyAddress)
// fmt.Printf(" %s\n", address)
ct_p2pkh++
continue
} else {
fmt.Println("Address not matched")
fmt.Println("Address: ", address)
fmt.Println("Scriptpubkey Address: ", vin.Prevout.ScriptpubkeyAddress)
}
}

if vin.Prevout.ScriptpubkeyType == "p2sh" {
pubkey_asm := vin.Prevout.ScriptpubkeyAsm
address := p2sh(pubkey_asm)
if string(address) == vin.Prevout.ScriptpubkeyAddress {
// fmt.Println(vin.Prevout.ScriptpubkeyAddress)
// fmt.Printf(" %s\n", address)
ct_p2sh++
continue
} else {
fmt.Println("Address not matched")
fmt.Println("Address: ", address)
fmt.Println("Scriptpubkey Address: ", vin.Prevout.ScriptpubkeyAddress)
}
}

if vin.Prevout.ScriptpubkeyType == "v0_p2wpkh" {
pubkey_asm := vin.Prevout.ScriptpubkeyAsm
address := p2wpkh(pubkey_asm)
if string(address) == vin.Prevout.ScriptpubkeyAddress {
// fmt.Println(vin.Prevout.ScriptpubkeyAddress)
// fmt.Printf(" %s\n", address)
ct_p2wpkh++
continue
} else {
fmt.Println("Address not matched")
fmt.Println("Address: ", address)
fmt.Println("Scriptpubkey Address: ", vin.Prevout.ScriptpubkeyAddress)
}
}

if vin.Prevout.ScriptpubkeyType == "v0_p2wsh" {
pubkey_asm := vin.Prevout.ScriptpubkeyAsm
address := p2wsh(pubkey_asm)
if string(address) == vin.Prevout.ScriptpubkeyAddress {
// fmt.Println(vin.Prevout.ScriptpubkeyAddress)
// fmt.Printf(" %s\n", address)
ct_p2wsh++
continue
} else {
fmt.Println("Address not matched")
fmt.Println("Address: ", address)
fmt.Println("Scriptpubkey Address: ", vin.Prevout.ScriptpubkeyAddress)
}
}
}
for _, vout := range tx.Vout {
if vout.ScriptpubkeyType == "p2pkh" {
pubkey_asm := vout.ScriptpubkeyAsm
address := p2pkh(pubkey_asm)
if string(address) == vout.ScriptpubkeyAddress {
// fmt.Println(vout.ScriptpubkeyAddress)
// fmt.Printf(" %s\n", address)
ct_p2pkh++
} else {
fmt.Println("Address not matched")
fmt.Println("Address: ", address)
fmt.Println("Scriptpubkey Address: ", vout.ScriptpubkeyAddress)
}
}

if vout.ScriptpubkeyType == "p2sh" {
pubkey_asm := vout.ScriptpubkeyAsm
address := p2sh(pubkey_asm)
if string(address) == vout.ScriptpubkeyAddress {
// fmt.Println(vout.ScriptpubkeyAddress)
// fmt.Printf(" %s\n", address)
ct_p2sh++
continue
} else {
fmt.Println("Address not matched")
fmt.Println("Address: ", address)
fmt.Println("Scriptpubkey Address: ", vout.ScriptpubkeyAddress)
}
}

if vout.ScriptpubkeyType == "v0_p2wpkh" {
pubkey_asm := vout.ScriptpubkeyAsm
address := p2wpkh(pubkey_asm)
if string(address) == vout.ScriptpubkeyAddress {
// fmt.Println(vout.ScriptpubkeyAddress)
// fmt.Printf(" %s\n", address)
ct_p2wpkh++
} else {
fmt.Println("Address not matched")
fmt.Printf("Address: %s\n", address)
fmt.Println("Scriptpubkey Address: ", vout.ScriptpubkeyAddress)
}
}

if vout.ScriptpubkeyType == "v0_p2wsh" {
pubkey_asm := vout.ScriptpubkeyAsm
address := p2wsh(pubkey_asm)
if string(address) == vout.ScriptpubkeyAddress {
// fmt.Println(vout.ScriptpubkeyAddress)
// fmt.Printf(" %s\n", address)
ct_p2wsh++
} else {
fmt.Println("Address not matched")
fmt.Printf("Address: %s\n", address)
fmt.Println("Scriptpubkey Address: ", vout.ScriptpubkeyAddress)
}
}
}
}
fmt.Println("Count of p2pkh address matched: ", ct_p2pkh)
fmt.Println("Count of p2sh address matched: ", ct_p2sh)
fmt.Println("Count of p2wpkh address matched: ", ct_p2wpkh)
fmt.Println("Count of p2wpkh address matched: ", ct_p2wsh)
}
43 changes: 43 additions & 0 deletions coinbase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

func Coinbase(netReward uint64) *Transaction {
witnessCommitment := CreateWitnessMerkle()
coinbaseTx := Transaction{
Version: 1,
Vin: []Input{
{
TxID: "0000000000000000000000000000000000000000000000000000000000000000",
Vout: 0xffffffff,
Prevout: Prevout{
Scriptpubkey: "0014df4bf9f3621073202be59ae590f55f42879a21a0",
ScriptpubkeyAsm: "0014df4bf9f3621073202be59ae590f55f42879a21a0",
ScriptpubkeyType: "p2pkh",
ScriptpubkeyAddress: "bc1qma9lnumzzpejq2l9ntjepa2lg2re5gdqn3nf0c",
Value: uint64(netReward),
},
IsCoinbase: true,
Sequence: 0xffffffff,
Scriptsig: "03951a0604f15ccf5609013803062b9b5a0100072f425443432f20",
Witness: []string{"0000000000000000000000000000000000000000000000000000000000000000"},
},
},
Vout: []Prevout{
{
Scriptpubkey: "0014df4bf9f3621073202be59ae590f55f42879a21a0",
ScriptpubkeyAsm: "0014df4bf9f3621073202be59ae590f55f42879a21a0",
ScriptpubkeyType: "p2pkh",
ScriptpubkeyAddress: "bc1qma9lnumzzpejq2l9ntjepa2lg2re5gdqn3nf0c",
Value: uint64(netReward),
},
{
Scriptpubkey: "6a24" + "aa21a9ed" + witnessCommitment, //OPRETURN +OP_PUSHBYTES_36+ commitment header + witnessCommitment
ScriptpubkeyAsm: "OP_RETURN" + "OP_PUSHBYTES_36" + "aa21a9ed" + witnessCommitment,
ScriptpubkeyType: "op_return",
ScriptpubkeyAddress: "bc1qma9lnumzzpejq2l9ntjepa2lg2re5gdqn3nf0c",
Value: uint64(0),
},
},
Locktime: 0,
}
return &coinbaseTx
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/pred695/code-challenge-2024-pred695

go 1.22.0

require (
github.com/btcsuite/btcutil v1.0.2
github.com/mr-tron/base58 v1.2.0
)
37 changes: 37 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
github.com/btcsuite/btcutil v1.0.2 h1:9iZ1Terx9fMIOtq1VrwdqfsATL9MC2l8ZrUY6YZ2uts=
github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY=
github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
48 changes: 48 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"encoding/hex"
"os"
"time"
)

type BlockHeader struct {
version uint32
prevBlockHash string
merkleRoot string
time int64
bits string
nonce uint32
}

var Bh BlockHeader = BlockHeader{
version: 7,
prevBlockHash: "",
merkleRoot: "",
time: time.Now().Unix(),
bits: "1d00ffff",
nonce: 0,
}

func main() {
netReward, TxIDs, _ := Prioritize()

cbTx := Coinbase(netReward)
serializedcbTx, _ := serializeTransaction(cbTx)
TxIDs = append([]string{hex.EncodeToString(to_sha(to_sha(serializedcbTx)))}, TxIDs...)
mkr := NewMerkleTree(TxIDs)
Bh.merkleRoot = hex.EncodeToString(mkr.Data)

if ProofOfWork(&Bh) {
file, _ := os.Create("output.txt")
defer file.Close()

serializedBh := SerializeBlockHeader(&Bh)
file.WriteString(hex.EncodeToString(serializedBh) + "\n")
file.WriteString(hex.EncodeToString(serializedcbTx) + "\n")
for _, tx := range TxIDs {
file.WriteString(tx + "\n")
}
}

}
Loading

0 comments on commit 58f4237

Please sign in to comment.