Skip to content

Instantly share code, notes, and snippets.

@rippaio
Last active July 18, 2018 05:36
Show Gist options
  • Save rippaio/2b64c64eacd79da17ec630371eb207a4 to your computer and use it in GitHub Desktop.
Save rippaio/2b64c64eacd79da17ec630371eb207a4 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/hex"
"log"
"testing"
"github.com/dchest/blake2b"
)
func TestBuildingMerkleBranch(t *testing.T) {
// These values are from https://github.com/SiaMining/Stratum/blob/master/example.py
//
// prevhash := "0000000000000079039da6f4d7790d54d774812f92e459387846524f4024afe3"
// coinb1 := "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000020000000000000004e6f6e536961000000000000000000006aba4dfb5df01095"
// coinb2 := "0000000000000000"
// merkle_branch := []string{
// "25cc1c464ed8f0a13da6c14098c2cd47526dcd64d3594a2ace794b9bc0ab704d",
// "2c162ebd012c0044cd34808a0dc9e5790f428cba73ce7b848a6f63ddd845c80e",
// "20283c26aa6cf99c126fe74021f2c5fb39baffd81814d725c57c883e16676c5d",
// }
// ntime := "2ed1705800000000"
// extranonce1 := "99cfbade"
// extranonce2 := "ef7488b3"
// nonce := "40371d049700e893"
var txBytes [][]byte
for _, hash := range txFromBlock86011 {
hashBytes, err := hex.DecodeString(hash)
if err != nil {
panic(err)
}
txBytes = append(txBytes, hashBytes)
}
hashes := buildBranch(txBytes)
for _, hash := range hashes {
log.Println(hash)
// if hash != merkle_branch[idx] {
// t.Error("Not yet bro")
// }
}
}
//This method prepends to the steps
func buildBranch(data [][]byte) (stepString []string) {
L := data
steps := [][]byte{}
PreL := [][]byte{}
StartL := 2
Ll := len(data)
if Ll > 1 {
for {
if Ll == 1 {
break
}
steps = append([][]byte{L[1]}, steps...)
if Ll%2 != 0 {
L = append([][]byte{L[len(L)-1]}, L...)
}
Ld := [][]byte{}
for i := StartL; i < Ll; i += 2 {
join := merkleJoin(L[i], L[i+1])
Ld = append([][]byte{join}, Ld...)
}
L = append(PreL[:], Ld...)
Ll = len(L)
}
}
for _, hash := range steps {
stepString = append(stepString, hex.EncodeToString(hash))
}
return stepString
}
func merkleJoin(hash1 []byte, hash2 []byte) []byte {
joined := append(hash1[:], hash2[:]...)
blake1 := blake2b.Sum256(joined)
hashed := blake2b.Sum256(blake1[:])
return hashed[:]
}
var (
//These are hashes
txFromBlock86011 = []string{
//the first transaction is a miner output
"6213b3624b7001ed726510f168144340fd359f7db1b848405e89d888ade3001e",
//These are all transaction hashes
"038778589e98c88514038d181b1479b9de911130bf17baf1888b2a8bdba12c0e",
"50363843a98fcdde4eb2cec388a0404b6b321595cac3e06a8a07460a05d08373",
"e4010784801e4fce00ce4c97b9f3b7d4379e6cf1ee59915067471aaa75684319",
"ff79ff012bcc4d208d8feda059dcc21128ed616fc256942f60bcebf30fa3d7fc",
"e97b405bcad10dc9c5ed5d8e7297f12170b7807cc1ebda64abb463c68c0fb63c",
"b8298b0492cff4dfcabf38c8c3d52c90f459048d419337d28ba87f4d1f926fb3",
"5dbdee17d7b694470b7dc834b5c546f3a8623affadac771ab25abd547f957fe4",
"72b49da376afd169feb44cb52870da6b6a77cb37e709c5ddd9a662484d2cd65f",
"a7d53667f3712282b3c40361fba3d97f0561817394609b15f69de207eca47cf5",
"e428d6c8f4caa647a5528fd740971ed1f13d566c115ecf6ebc9feb660ab5f5fe",
"1c9c797859f6e61c417c61fdcf806927fdd9d0d01abf56bcfe42ab0b271b5f2d",
"4992767c5a29c7e5a29d22697c692fb96d0470553034f618358a3a481470895b",
"fd14e25537037581a2d337e10871ff472fc6a600dcff2caf96ef2194f38d81b9",
"f2784398a47ec4a1b6fd6d3b2de6d98442454205561edb01023f20a816787416",
"5d02d75ad10e9f86fbe591f83e3d8e033868fc3a82e06819ce2a266c48830249",
"e80dd92d714b5f708f933da9dfc2146df26017decd9dfd48f4afbd083031fdb6",
"51b0cda32634c8e1fab7e2be2b034c6cb5e34594d5686847b827ab7c69d94589",
"8f655c54f969ca52cd3821dba239bbb1235ec71ea4c13d1a130369a17f49f5fa",
"4540f7a8405f17c10bb6e38f9bcd6fe742c4940d5e1bb7d57c885cd8de0d92c6",
"7bae368119d3a6fd83de493d6aeeeabf9280a52dec4a4d3027a4c2fc462edc45",
"68ef656d5a5926a3d90dd6d53d58243798c2832723e7ed5ac60b574fc2c05c0b",
"38aaf6d7879dba7164b526db3808f2e6ac7f2417fddaf8fa14dd3698e25226b6",
"9fb9f6103dfdeec6c743f97811a53cfc39aa97541cc5b002444df283514f8b96",
"c391bb6598700ceb2c2a4677ae8434a7b69e9a4a840b2efff19aabd3d260c5d5",
"f504ea92f0f21a718d4b64ea7c8155d948b9850f10095bf2502280a61413fa3c",
"128f285754f8e17b8b4c42e22df3e3e2c8ca7b9452e66dda2cc7dd0dedcf88d5",
"fe24169c1212ff4fdac74b24973d4e9affeabd85187a8908e86ad8aabd0f5843",
"3250c66da922c69be9ed8f9ce17505de22da38f4358ac820e09417b145d8bbbb",
"d99b8730dca2f4cee392f47087d1fe6555e126b9738acd40c3b701f190caf92c",
"1437e2ea5c1e077ab281d38a97bec1b5a42b0977ba64319a84490212b6b854d4",
"4aab8d7b4bcf5051d4ae9c4916f1d19d4fd9dfb0a7e401c1d9d47de72ee1054f",
"43cb8c7efac4a85d8f9da4c22a96503dabd9b7d231686dbf3eeacbc6411f04ad",
"efba729a6a13ea82c6d4c5b74607f633d6fc7b114636495d5a4c52cc3a6761c6",
"e59d761aecda8d3e4ba0ed3f70eb4b97b46da14b8f8c6e5c3afa5b4cb19c4a2c",
"95f9b79061d77187038e9a97a1450109dab27396bf654ae00bdad479bab8c3e1",
"6e5c2b4c0bf5385aae7e11f8c7188401f023a03f4efa9b680a8e57912af215c1",
"6cea6131c36cc0754a23704598e1fbfc6e242b7bc6ea09ab3836e8b1ce8db045",
"5da1538c85a3dc0671e8540d8029d185714219155489f6d834cf2ea760e1fe7f",
"3b74dfc549e9993799d87171343d4f3dfc95df93562804ba72b35a7b59455fa6",
"f7793e7be43ba63a987219559541b11e6bb46288724c24d53ef0eac00e678c11",
"d343bf285857b8faa4e436c0d8162585e9bfcdac32a80131d38656491293881a",
"f513de330c0eba7d9bd5d7601da247f2b9098f5075761930d3c224773d4cf6e2",
"20ccd4423cc21f18517a54e962c6b050447d3c80bf4b5c325a623e34909695b1",
"53251d9477d5c0187cdddab4257249af10db72f1f9a28ad02aa7d21f13f84b39",
"330a061569d139a96fc5a64449ddb65eb3c865817b40c253773340c5e23a9abb",
"89bf918d547236292c84271e7145fb294ac1d508fb04287dd890dec20b780609",
"ae02994c115230cc7e3478578d602a548ed2c4764351e954dd61da1a2cc143c7",
"ce63aa15d90088f103cfb96dde11b8c3d64bfc0cb51fa351ea8a10458af51ae0",
"f355817162e915ebd76ef7260e9556c8890a533dcc116872a90a9217ebf7dbb3",
"865c3bdb8779561f26d8d7e250cf918681ddcc0394ab4f4204ec0f25973e9bd6",
"c801b87ab81c421c6f06713e613a4601958fcd601ab63e2bf5c7cf1361e7e241",
"273ceaafdb52fa96cc0e8f8e50a52cbdda4c22194a1d6bea504f85e9fb7d4f10",
"554746f6d2fb73aa91c3ae74f02492f11f98cbf26c344701850fc426f0055cba",
"58399e6cacf53f19e4c99fc4343a89a1af59eee289c38842dd2f411b37c2923a",
"2bbe6d77f90b623f5fc9d4fef08607da8b656a4f5e261dbbcd728a254004ee49",
"ec81f2e211b4433c643aff4eab486feb8bb4a7102c69241902768aa3993186cf",
"85e3ef6f896cf0416c69ebca216e8e3373dedee17110f8afba46ae30745ca9da",
"813c62f433e07b54f01ec7ef4ad1beace5f003a0d409747e775e1c78c127f5f3",
"06f664fc15531bf5b37396e57378289adb93ce08b02800fa9c460a9e3bcae981",
"3063978fe49a003561587ba24024d127117ef8105d6550f2cb490d888e9927d5",
"414526898c56e8c49eae30776b332d698553ffa202465ece15b787c8b15e2af6",
"bf820d4b241ec78118c65396a5a5c86d826a07ce90fab45344bedbcd152d4792",
"cbf4b0d6bb1a06eb6a20484f5b48a6eda74f61a5d50b5ed9724b625b0cdedad0",
"8b8d3a16b2372a125e0e888f30c556e28521112cfb01d9be427947451647981e",
"fae64d04353d43ff86f32c0cd08882247e8a4fc351ae06a426fe1f159c1e2843",
"2720282c73b5c66267e668d8af7147ec44e63fe20d854083a16bf97b2354f67f",
"c690895fb04adef5ab2c70ff33f18ffc1fd430a666065f6b89a8dd967e3ccc13",
"d31a6a5deacdefbe03f79b1fdd7ffb967c88e3dbbfbafbb55b029d5466d37fc7",
"8bb193d9b8fac8416039d8ef8bb6c2b540875766848d1b9b30724171e8aacc4b",
"fe2af282842e66c01a1efb8c6f9167cc702df2fcb97e7497101db886cbec9caa",
"8ac0b6c9b74ee03c0fb9c49e3c27c545609020327318609983cb01deb725d109",
"4b7738b23fec1c86134dc4b031565d5911533b5bc48b0793593dfae6d827c46c",
"8d729bafc34919ebefd01b620aad32f9aba48753b5784fff24d1019fc6203872",
"20496c240f70de017a0316f50d4fca5334198485710fd75be1d5d43e9b4181f5",
"d054307e1ce2a8863f001c82b599a93a925c6a074b01c050e9a80e5dea877d98",
"47cf3398239f375f0650588f10e529c8e808ee47d7a70086e344f992920cb4e8",
"0b721a94994d2b57559d1cdd8122422fc879ceb5a3354ecbdd00fd3483a74ccf",
"3f34377cf0a9cc9226cc9e9bec80842302d261a274b44c0c9d9bc33a0b2fd4ec",
"c30e42545bba25c0b51c0e395958ef88d048d267ef15719ff60b180371499eb7",
"77f8917caabf80d1741fbf7136ee5db4988bce2d2f96c66e74c8b07ae77b20c0",
"14b932ce57fa35e1c0e4f3425ff81886781b2eb0d0c2787164ee822c1d51cd7a",
"41c2909ec7f518b7d09417da1a07493feb53174845827330a469e5a83dd80268",
"b80d8f863d14596b1b1dfa74d9587ce0919d9e0b9c97b335b0cef7f7bc91d432",
"8549c2e1d0ccbf63a9a324268262620ef13cbb01402d68dfb05abe8e57d9c10b",
"197da76d2133f780943f3477910de88a605e938acb6b71835a8f23e8c5525309",
"2f7061f7c6c269d7f52d83f20c648f96a3e9dae9626f898b8660f8454da27f3c",
"b2204d12da2d72fd13d6ba7298e932d2ce30dc28cc7170a1b3cc1c450c225759",
"c46e709fd4eff87ff3642761dc5b3021c81f7759ada6678cfd858a61ac8dc716",
"1be57e92322a510cd808d05afa04b1d3db20e842cef2487957e9aeeb1d268dfb",
"8c90354722a7fd271459db7fc25dd1b7d7b900616830ee1be395f8e7ac50895d",
"6fa64205b63f0e81a4d8595401eb30027b5ec2ac56a128c92790059ea859195e",
"d078ffb2310510cea6bf807b82a295eb1529e1b1ddccfe42cc4f2eb165938ce1",
"888c299a3de9ba0d30a68fc792d11da1d59cc6c7beae86b7390e5e5fdf78b60e",
"36ba294345e0edbf187e2489a649fc8a3485ab561adeea75d6675b9c62875db2",
"ab311a55d0f3c5ede61ffbb22291b2a79c4174f5338f271013db9ba99fb6fbbd",
"eb17d2fb638bb8b92bced9116949f00c72001036a81e007e73849c4c4845130f",
"67d2a47072d353e60e1a3e27127fe54fbbc428b2cd92d898ca8aff1008610f0d",
"2742041fa24d564177d10d8b68665c935d041b9c656f42b00ad1beb531636f3d",
"939ac2f52b6ba4d9f1a758c333221c955b34e0526274f646174e45c643736ed8",
"c1c2195b5de39f604d8ea59042574177b6847d34d99d492c1e7a2be84216d0ff",
"7a3abd57078d0511335833a6d2df079e4245410f9565b59a688de6ae7dd22983",
"aa69a7c87ff4940127cf8c0d659c07e468fd1ad7018a75933498dcf3edf9184e",
"be975a0e0c20753111513bb4cdfbf185fb5bb9ed35e58db6583734a9881e60e5",
"ed28c2ac6d2c2d34fd621e4d684cb7a61760760bd0604cb58f4eee9977432c26",
"e111ef1f7a8f9a777224c26527e4c8122403a5418089b6dc23a5f6526da9d5af",
"c0f24897ba4d2b05331a2cede97318a523a2697bac8175d54d4a63c9836d451a",
"70aade2fc6043968f7c6cf49f3191822e52e1d2e9ba7c0b66600514de6be32ec",
"119f2594e2a37e92b047b61d05c75189c8cb6a3791cbc4c41bb13a51f5050bc8",
"d90b95e0f18b2a54eb6b0e03f862e416ebd7257f3b5bad8b2167248dc7f41065",
"c2a945eea504664d1233e9c31c3d6d2855a623ded2cd7c1f9267ab6c0a08a61e",
"29a62e7e224994d8e44c26a050364fd39cc952cc8ef09d2af63751ee9e5b9092",
"a7a922e48f1ef29d50a6acd5360852c7f952a069d65b44c2abe945270313c53d",
"0c692003b6c9cc90d8cd179b5fce8b71bca77f096a0bb6b71b7608778117e3c4",
"47f7de0f5b106d68bd5bd59a0e364a9947ddfd0b6fbdeef4e3d8b12c8345e3e4",
"f71911aac385d2d65aba8f76ebbc74aaf37e4290093ddf87a37cc687b8686904",
"39817d1fe1da2d6e36c527cdf852bc90a1c5b17f6e3a4e256e8e48ce8acc0dae",
"23c66d0e34524390ee4e9adb7976ef2fd178aa9f0cdc3fc4dd5d962da7176963",
"9f735fd27b24d38c34e8b3c066bfefb20a77fcce8cebe56db010fb8d21672995",
"9dc803000f998fb75976c37d5b613c5928e3b7fc82475e4a5b06f3a44451bfce",
"614cdd578a3e2a6a5ff72b62f576c93a2b319a8f5bf9008008a94998d29b91be",
"ddc9f4dbbb1cb0a1d277d9e32f466f6d185ac79d0a38164893de483625f17792",
"e5fc6d88159a96b346f7b443fcd57ffb94c1c9248db79f95cc2034902f6b735a",
"45a25fbc11c2b47c7a1dc28cb0f561c65d47e9a4574fa9c8ed6d69afbb4669a6",
"b318fe92314099c486ddc949fe82fcde8f9a7f6bf53cbd79940dcc60a44c45d9",
"2f4170f93ad2d56b01fa08b26caed56b04ec941ca70106f437d2e91dac579a5b",
"dd66f28625866477299c89d50097ad8078c54e7831a250955fa25446d6bcd242",
"f68a4fa615933bee04bab4a0f3c0430570c4bb0f1d484f6618f3a6a57eaa88b8",
"09b08e815fcc08f2edb8426017e3a53c25121f1e3077c6a342210c33825cc785",
"6a4b962552e60f25c837bbfdaacd02d40e94dd1d5d7aea951225e0b1f496e61d",
"6cf292a71b6db2a1190a20e30e77983c8103ff1afba84e5ddb10b10be0befc18",
"18460500c130cc699d6b4861c326b3e22ad83316d1efe72fdde5052548c48e55",
"cfbcd168c36ac6a7b15677429ed07f4c7c6ae073259b1a88a9711019d9f650ba",
"853d6cae970057699eaf84eff8bf3980e35c2553c7a69b02cec7a2c7837ab773",
"5485b10d1fe6eccb63e39aca34d19b7f8b189d46466ca246e054841333b82cbd",
"6b8775f2f54bea993f00ff54df43c3ed8d2ffd179d0c026aab4fde60235a31a9",
"ebbca1acb922c09dfdd4c41af9a9097f36562e31b141582e4d3d542a2b90773a",
"8c35de8f7fc44b1a9a05bdcb0a0fd943afa468bc657e22d1ddd268b02ba513c2",
"8ba8f7192ee5e46a066464046c961ef5767c570db634704d619c7f0a4e48ad6e",
"4bfafc7620c5b2fdeabf1b58382c0a1015fae1d4e7c1a8415f71c36d92a5482c",
"f7fa77185455f4036995dbaa568e3ff1d3967bf96ea2e9aef33678d48561baa8",
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment