Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
OsauravO committed Apr 28, 2024
1 parent 3cd774d commit 63a5d8d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 143 deletions.
5 changes: 0 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
module github.com/OsauravO/code-challenge-2024-OsauravO

go 1.22.0

require (
github.com/btcsuite/btcutil v1.0.2
github.com/mr-tron/base58 v1.2.0
)
39 changes: 0 additions & 39 deletions go.sum

This file was deleted.

137 changes: 41 additions & 96 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,28 @@ import (
"sort"
"strings"
"time"
"github.com/btcsuite/btcutil/bech32"
"github.com/mr-tron/base58"

)

func uint16ToBytes(num uint16) []byte {
func u16ToB(num uint16) []byte {
buf := make([]byte, 2)
binary.LittleEndian.PutUint16(buf, num)
return buf
}

func uint32ToBytes(num uint32) []byte {
func u32ToB(num uint32) []byte {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, num)
return buf
}

func uint64ToBytes(num uint64) []byte {
func u64ToB(num uint64) []byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, num)
return buf
}

func reverseBytes(data []byte) []byte {
func rb(data []byte) []byte {
n := len(data)
for i := 0; i < n/2; i++ {
data[i], data[n-1-i] = data[n-1-i], data[i]
Expand All @@ -42,7 +41,7 @@ func reverseBytes(data []byte) []byte {

type BlockHeader struct {
Version uint32
PreverseByteslockHash string
PrblockHash string
MerkleRoot string
Time int64
Bits uint32
Expand Down Expand Up @@ -99,7 +98,7 @@ type MerkleTree struct {

var blockHeader = BlockHeader{
Version: 7,
PreverseByteslockHash: "0000000000000000000000000000000000000000000000000000000000000000",
PrblockHash: "0000000000000000000000000000000000000000000000000000000000000000",
MerkleRoot: "",
Time: time.Now().Unix(),
Bits: 0x1f00ffff,
Expand Down Expand Up @@ -151,7 +150,7 @@ func proofOfWork(blockHeader *BlockHeader) bool {
targetBytes, _ := hex.DecodeString(TargetValue)
for {
sh := srlzBhead(blockHeader)
hash := reverseBytes(sha256Hash(sha256Hash(sh)))
hash := rb(sha256Hash(sha256Hash(sh)))
if checkByteArray(hash, targetBytes) == -1 {
return true
}
Expand All @@ -174,10 +173,10 @@ func extractPubKeyHex(scriptPubKeyAsm []string) string {
return ""
}

func base58Encode(input []byte) []byte {
encoded := base58.Encode(input)
return []byte(encoded)
}
// func base58Encode(input []byte) []byte {
// encoded := base58.Encode(input)
// return []byte(encoded)
// }

func sha256Hash(data []byte) []byte {
hash := sha256.Sum256(data)
Expand Down Expand Up @@ -252,7 +251,7 @@ func CreateCoinbase(netReward uint64) *Transaction {
func merkNode(lnode *MerkleNode, rnode *MerkleNode, data []byte) *MerkleNode {
node := &MerkleNode{}
if lnode == nil && rnode == nil {
node.Data = reverseBytes(data)
node.Data = rb(data)
} else {
combinedHash := append(lnode.Data, rnode.Data...)
node.Data = sha256Hash(sha256Hash(combinedHash))
Expand Down Expand Up @@ -321,8 +320,8 @@ func Ordering() (uint64, []string, []string) {
}
serlzd, _ := serTx(&tx)
segserlzd, _ := SegWitSerialize(&tx)
txID := reverseBytes(sha256Hash(sha256Hash(serlzd)))
wtxID := reverseBytes(sha256Hash(sha256Hash(segserlzd)))
txID := rb(sha256Hash(sha256Hash(serlzd)))
wtxID := rb(sha256Hash(sha256Hash(segserlzd)))
txInfo = append(txInfo, TxInfo{TxID: hex.EncodeToString(txID), WTxID: hex.EncodeToString(wtxID), Fee: fee, Weight: uint64(calWitSize(&tx) + calBaseSize(&tx)*4)})

}
Expand Down Expand Up @@ -384,104 +383,51 @@ func SerializeVarInt(n uint64) []byte {
if n < 0xfd {
return []byte{byte(n)}
} else if n <= 0xffff {
return append([]byte{0xfd}, uint16ToBytes(uint16(n))...)
return append([]byte{0xfd}, u16ToB(uint16(n))...)
} else if n <= 0xffffffff {
return append([]byte{0xfe}, uint32ToBytes(uint32(n))...)
return append([]byte{0xfe}, u32ToB(uint32(n))...)
} else {
return append([]byte{0xff}, uint64ToBytes(n)...)
return append([]byte{0xff}, u64ToB(n)...)
}
}

func serTx(tx *Transaction) ([]byte, error) {

func serTx(tx *Transaction) []byte {
var serlzd []byte
// Serialize version

versionBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(versionBytes, tx.Version)
serlzd = append(serlzd, versionBytes...)
// Serialize vin count
vinCount := uint64(len(tx.Vin))
serlzd = append(serlzd, SerializeVarInt(vinCount)...)

// Serialize vin
serlzd = append(serlzd, u32ToB(tx.Version)...)
serlzd = append(serlzd, SerializeVarInt(uint64(len(tx.Vin)))...)
for _, vin := range tx.Vin {
txidBytes, _ := hex.DecodeString(vin.TxID)
serlzd = append(serlzd, reverseBytes(txidBytes)...)

voutBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(voutBytes, vin.Vout)
serlzd = append(serlzd, voutBytes...)

serlzd = append(serlzd, rb(txidBytes)...)
serlzd = append(serlzd, u32ToB(vin.Vout)...)
Scriptsig_bytes, _ := hex.DecodeString(vin.Scriptsig)
length_scriptsig := (uint64(len(Scriptsig_bytes)))
serlzd = append(serlzd, SerializeVarInt(length_scriptsig)...)

serlzd = append(serlzd, SerializeVarInt(uint64(len(Scriptsig_bytes)))...)
serlzd = append(serlzd, Scriptsig_bytes...)

// Serialize sequence
sequenceBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(sequenceBytes, vin.Sequence)
serlzd = append(serlzd, sequenceBytes...)

serlzd = append(serlzd, u32ToB(vin.Sequence)...)
}

// Serialize vout count
voutCount := uint64(len(tx.Vout))
serlzd = append(serlzd, SerializeVarInt(voutCount)...)

// Serialize vout
serlzd = append(serlzd, SerializeVarInt(uint64(len(tx.Vout)))...)
for _, vout := range tx.Vout {
valueBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(valueBytes, vout.Value)
serlzd = append(serlzd, valueBytes...)

// Serialize scriptPubKey length
scriptPubKeyBytes, err := hex.DecodeString(vout.Scriptpubkey)
scriptPubKeyLen := uint64(len(scriptPubKeyBytes)) // Divide by 2 if appending the length of the non decoded form to get byte length since scriptPubKey is hex encoded
serlzd = append(serlzd, SerializeVarInt(scriptPubKeyLen)...)

// Serialize scriptPubKey
if err != nil {
return nil, err
}
serlzd = append(serlzd, u64ToB(vout.Value)...)
scriptPubKeyBytes, _ := hex.DecodeString(vout.Scriptpubkey)
serlzd = append(serlzd, SerializeVarInt(uint64(len(scriptPubKeyBytes)))...)
serlzd = append(serlzd, scriptPubKeyBytes...)
}
//Locktime
locktimeBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(locktimeBytes, tx.Locktime)
serlzd = append(serlzd, locktimeBytes...)

return serlzd, nil
serlzd = append(serlzd, u32ToB(tx.Locktime)...)
return serlzd
}
func srlzBhead(bh *BlockHeader) []byte {
var serlzd []byte

versionBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(versionBytes, bh.Version)
serlzd = append(serlzd, versionBytes...)

preverseByteslockHashbytes, _ := hex.DecodeString(bh.PreverseByteslockHash)
serlzd = append(serlzd, preverseByteslockHashbytes...)

serlzd = append(serlzd, u32ToB(bh.Version)...)
prblockHashbytes, _ := hex.DecodeString(bh.PrblockHash)
serlzd = append(serlzd, prblockHashbytes...)
merkRootB, _ := hex.DecodeString(bh.MerkleRoot)
serlzd = append(serlzd, merkRootB...)

bh.Time = time.Now().Unix()
timeBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(timeBytes, uint32(bh.Time))
serlzd = append(serlzd, timeBytes...)

bitsBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(bitsBytes, bh.Bits)
serlzd = append(serlzd, bitsBytes...)

NonceBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(NonceBytes, bh.Nonce)
serlzd = append(serlzd, NonceBytes...)

serlzd = append(serlzd, u32ToB(uint32(bh.Time))...)
serlzd = append(serlzd, u32ToB(bh.Bits)...)
serlzd = append(serlzd, u32ToB(bh.Nonce)...)
return serlzd
}

func SegWitSerialize(tx *Transaction) ([]byte, error) {

var serlzd []byte
Expand All @@ -500,7 +446,7 @@ func SegWitSerialize(tx *Transaction) ([]byte, error) {
// Serialize vin
for _, vin := range tx.Vin {
txidBytes, _ := hex.DecodeString(vin.TxID)
serlzd = append(serlzd, reverseBytes(txidBytes)...)
serlzd = append(serlzd, rb(txidBytes)...)

voutBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(voutBytes, vin.Vout)
Expand Down Expand Up @@ -668,7 +614,7 @@ func generateP2PKHAddress(scriptPubKeyAsm string) []byte {

func calculateTxID(serializedTx []byte) string {
hash := doubleHash(serializedTx)
reversedHash := reverseBytes(hash)
reversedHash := rb(hash)
return hex.EncodeToString(reversedHash)
}

Expand Down Expand Up @@ -716,5 +662,4 @@ func main() {
// Create an output file and write block data
writeBlockData(blockHeader, coinbaseTx, transactionIDs)
}
}

}
6 changes: 3 additions & 3 deletions output.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
07000000000000000000000000000000000000000000000000000000000000000000000027a51c6f2c505dda24e565b0827cbcbe8f7a024551a8379896bc363ad84e091ec0d62d66ffff001ffc2d0200
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff1b03951a0604f15ccf5609013803062b9b5a0100072f425443432f20ffffffff027891860100000000160014df4bf9f3621073202be59ae590f55f42879a21a00000000000000000266a24aa21a9ed857be26be2d48ea615af6abc625860f4a86ed2bef314e57681b02d17efd13bc10120000000000000000000000000000000000000000000000000000000000000000000000000
35638ec7d9e798458871742209c407b7e9111c30955d71bf2f3f7c8fe81dd683
070000000000000000000000000000000000000000000000000000000000000000000000aeb45f1bbf1b863ffac6db63cb34a24d3e20a04dcd8a07d9ba5d4c301c572fe180232e66ffff001f9e320000
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff1b03951a0604f15ccf5609013803062b9b5a0100072f425443432f20ffffffff027891860100000000160014df4bf9f3621073202be59ae590f55f42879a21a00000000000000000266a24aa21a9ed99171a393943106a454cfae7971989270d276d042541b25f2c14ea5487d3b8b80120000000000000000000000000000000000000000000000000000000000000000000000000
4ccf620acb55f525fec4f6cd7a6c0b5ef2de4e59aa9936e8c1b386aeb4f822f0
82f9f96db7bdbb9e70626747632e373b34eefd50d613dfea7092744169591b6e
7cb2a4f55245bae141a5d6ad51c08d7a9fdf2c2b905e4d97639ed80b82e69800
a9e537569db3c64340ed5abcdd983e9bb1b6ad6f90c93bc80d31c5cc0490bcea
Expand Down

0 comments on commit 63a5d8d

Please sign in to comment.