From 63a5d8d5027cfc2ccbca6533a5e654b8e721eb09 Mon Sep 17 00:00:00 2001 From: OsauravO Date: Sun, 28 Apr 2024 16:15:10 +0530 Subject: [PATCH] Refactoring --- go.mod | 5 -- go.sum | 39 --------------- main.go | 137 ++++++++++++++++------------------------------------- output.txt | 6 +-- 4 files changed, 44 insertions(+), 143 deletions(-) delete mode 100644 go.sum diff --git a/go.mod b/go.mod index deb9445..b2bd2b2 100644 --- a/go.mod +++ b/go.mod @@ -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 -) diff --git a/go.sum b/go.sum deleted file mode 100644 index a8d100d..0000000 --- a/go.sum +++ /dev/null @@ -1,39 +0,0 @@ -github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= -github.com/btcsuite/btcd v0.20.1-beta h1:Ik4hyJqN8Jfyv3S4AGBOmyouMsYE3EdYODkMbQjwPGw= -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 h1:2+ZP7EfsZV7Vvmx3TIqSlSzATMkTAKqM14YGFPoSKjI= -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= diff --git a/main.go b/main.go index 91516b4..463e299 100644 --- a/main.go +++ b/main.go @@ -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] @@ -42,7 +41,7 @@ func reverseBytes(data []byte) []byte { type BlockHeader struct { Version uint32 - PreverseByteslockHash string + PrblockHash string MerkleRoot string Time int64 Bits uint32 @@ -99,7 +98,7 @@ type MerkleTree struct { var blockHeader = BlockHeader{ Version: 7, - PreverseByteslockHash: "0000000000000000000000000000000000000000000000000000000000000000", + PrblockHash: "0000000000000000000000000000000000000000000000000000000000000000", MerkleRoot: "", Time: time.Now().Unix(), Bits: 0x1f00ffff, @@ -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 } @@ -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) @@ -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)) @@ -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)}) } @@ -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 @@ -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) @@ -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) } @@ -716,5 +662,4 @@ func main() { // Create an output file and write block data writeBlockData(blockHeader, coinbaseTx, transactionIDs) } -} - +} \ No newline at end of file diff --git a/output.txt b/output.txt index 4b53338..bab64d0 100644 --- a/output.txt +++ b/output.txt @@ -1,6 +1,6 @@ -07000000000000000000000000000000000000000000000000000000000000000000000027a51c6f2c505dda24e565b0827cbcbe8f7a024551a8379896bc363ad84e091ec0d62d66ffff001ffc2d0200 -010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff1b03951a0604f15ccf5609013803062b9b5a0100072f425443432f20ffffffff027891860100000000160014df4bf9f3621073202be59ae590f55f42879a21a00000000000000000266a24aa21a9ed857be26be2d48ea615af6abc625860f4a86ed2bef314e57681b02d17efd13bc10120000000000000000000000000000000000000000000000000000000000000000000000000 -35638ec7d9e798458871742209c407b7e9111c30955d71bf2f3f7c8fe81dd683 +070000000000000000000000000000000000000000000000000000000000000000000000aeb45f1bbf1b863ffac6db63cb34a24d3e20a04dcd8a07d9ba5d4c301c572fe180232e66ffff001f9e320000 +010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff1b03951a0604f15ccf5609013803062b9b5a0100072f425443432f20ffffffff027891860100000000160014df4bf9f3621073202be59ae590f55f42879a21a00000000000000000266a24aa21a9ed99171a393943106a454cfae7971989270d276d042541b25f2c14ea5487d3b8b80120000000000000000000000000000000000000000000000000000000000000000000000000 +4ccf620acb55f525fec4f6cd7a6c0b5ef2de4e59aa9936e8c1b386aeb4f822f0 82f9f96db7bdbb9e70626747632e373b34eefd50d613dfea7092744169591b6e 7cb2a4f55245bae141a5d6ad51c08d7a9fdf2c2b905e4d97639ed80b82e69800 a9e537569db3c64340ed5abcdd983e9bb1b6ad6f90c93bc80d31c5cc0490bcea