diff --git a/data/data_reader.go b/data/data_reader.go new file mode 100644 index 0000000..008ccec --- /dev/null +++ b/data/data_reader.go @@ -0,0 +1,32 @@ +package data + +import ( + "encoding/json" + "io" + "os" +) + +type Sample struct { + StartHeight int64 `json:"start_height"` + HeaderStrings []string `json:"header_strings"` +} + +func ReadJson(jsonFilePath string) (int64, []string, error) { + jsonFile, err := os.Open(jsonFilePath) + if err != nil { + return 0, nil, err + } + defer jsonFile.Close() + + byteValue, err := io.ReadAll(jsonFile) + if err != nil { + return 0, nil, err + } + + var dataContent Sample + if err := json.Unmarshal(byteValue, &dataContent); err != nil { + return 0, nil, err + } + + return dataContent.StartHeight, dataContent.HeaderStrings, nil +} diff --git a/data/empty.json b/data/empty.json new file mode 100644 index 0000000..0139b5e --- /dev/null +++ b/data/empty.json @@ -0,0 +1,4 @@ +{ + "start_height": 1, + "header_strings": [] +} \ No newline at end of file diff --git a/data/regtest.json b/data/regtest.json new file mode 100644 index 0000000..22cf335 --- /dev/null +++ b/data/regtest.json @@ -0,0 +1,15 @@ +{ + "start_height": 11, + "header_strings": [ + "4a8cb347715524caa17d43987d527d0c11c0510f3e3c44a85035038a9b36e338", + "568cd25931996e14d7b41a73dbf8306e4a0435eeab92aa23bb4005d69f911348", + "3b86a885250999e7c77465270553155369f39639c9f6d1caddfa1bdbbaaf8f7d", + "1ca9f0e3344df7ca4b95145ef1afd48b445d1d80cdf941ea91083ea186caaa50", + "508256b96ee547e87537e7c8c4f38da6c84201fe2eb29e919bc5b91c832e0d34", + "209b48cc83546d45fc1c50530cd532dbde1c4133445b635230e063ddeef2f30f", + "4737364655eb928ff741fb0276cec9c00f9d12cb7a6a30d556989bd8763072f9", + "5b9c7b979cbd4a382b32be69ae35f4db59b3795ae33d6462c0425f5ad05d9ebd", + "627ff72e849340d9cbd7603fc9ac884132d4886a6ddfbfd0f9b964142aa8e447", + "497cf112e892b9551bfe72deedfdad8deb5d4fed2fcbba2ba3bc9ae9f143c2af" + ] +} \ No newline at end of file diff --git a/data/sample.json b/data/sample.json new file mode 100644 index 0000000..f61ee9b --- /dev/null +++ b/data/sample.json @@ -0,0 +1,18 @@ +{ + "start_height": 204100, + "header_strings": [ + "020000004cdba1415b2c6e7808c1b3c18df1374238454f7104203475bf01000000000000c17ea9d06015dc83902911cd24837a8ba4bdc0c1d72b873f906d921e06e48d2f984a8250ef75051a72a8061a", + "01000000a7e267eae437f7c99115682d3e104999b6b5b01f34f3f5d763030000000000004beb49a1caa3941adc853cd605dd030e5ab690fcd58ac76233397653c49f07ecdb4a8250ef75051a5e58c1fe", + "0100000032e89326a365919ac08d1f0bd98d097240fd5c8dd21b7c38e5030000000000003d3645c6e6008583570d618041b2452497d55c0cb936529af92f8191e0dbdcc9764b8250ef75051ac733c738", + "010000006cb4d23bba90d14f11d0719a02bdb0218e7b9a82a4453e0a51010000000000007caa984f729a85cec6ce000b87894bb30897195d36bc6491be92e7628de00d02e54d8250ef75051ac9b86274", + "01000000a5a2223dc3f0a3f7901d14d064216d6ecf9d5dabfa44199f2b02000000000000e877c5cb9c52e27e3b2b9ff5d3b1658bd5c49ce2e3a1ef608759b44eea5f832b424e8250ef75051a47a25741", + "0100000096bacf353c5534bc30044268588fb752df9c164a3ad14bd64d04000000000000a25ec7252a9a994d864b664bb4c5a2b1d8c1d688dc82ab78128eae34481110f416518250ef75051a1ac7889f", + "020000009c37b58365fedc02e5ce0fa879e14d3ec110a3700b6476b941000000000000007e82e8388e953e200ab3e41777e4f9982ce3fe3aa46cc29a8c8f974706f76f852b508250ef75051a9e6f81c1", + "010000001f7db2278bb6aeac530fa48e0f8e75cae092a89b6cbedb838500000000000000d7dfbe2005869b3319244192ba2fef1b0c9f1cf999da51be7d8f1a7f5bb02c6bf0538250ef75051a82d85a5f", + "01000000575cdd3be997ebc63b377e78ecde7f14ef354669ff7d62543703000000000000a7b0a3354071262e02c246ad1d49d6823f6d864aef247263a07641a1d419089b5a5a8250ef75051a1e7d568e", + "01000000fb49bbe46f780f8b0aa4b99ceddf1202e76d12fa4f5da1acdf000000000000003ec200da269c721728c820e305d01d4656e4ba0f6784c4fdeea05a904e0f0aab395d8250ef75051ac332b7b5", + "02000000418574a33d0657d095a8ff4723e8e1fafedcabe32b93d1ccf6010000000000006f95d48f47f27715f6c1bcc466c4c715a17f9e2be39ce1c41106baed2d146d546f5f8250ef75051a77fc3e40", + "01000000d19ffbe9a876f329acb05feadc90dae27578762e3f538f5a08050000000000005c8524057100e644fb3680b5b5af3d79f24fc4e51f374d9205e926e9b9cafd9c38648250ef75051a20379982", + "02000000670ffede95831fb41c09d0d104285d6182ce1c8577da40506405000000000000e54435f50bfc776b8f3d9ac047963ee6bdddd8d40b69236b4d97acb52a1fdce41e678250ef75051a88842656" + ] +} \ No newline at end of file diff --git a/main.go b/main.go index 7f717aa..0796bbe 100644 --- a/main.go +++ b/main.go @@ -8,29 +8,32 @@ import ( "github.com/gonative-cc/bitcoin-lightclient/btclightclient" "github.com/gonative-cc/bitcoin-lightclient/rpcserver" + "github.com/gonative-cc/bitcoin-lightclient/data" + "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/wire" "github.com/rs/zerolog/log" ) func main() { - startHeight := 204100 - headerStrings := []string{ - `020000004cdba1415b2c6e7808c1b3c18df1374238454f7104203475bf01000000000000c17ea9d06015dc83902911cd24837a8ba4bdc0c1d72b873f906d921e06e48d2f984a8250ef75051a72a8061a`, - `01000000a7e267eae437f7c99115682d3e104999b6b5b01f34f3f5d763030000000000004beb49a1caa3941adc853cd605dd030e5ab690fcd58ac76233397653c49f07ecdb4a8250ef75051a5e58c1fe`, - `0100000032e89326a365919ac08d1f0bd98d097240fd5c8dd21b7c38e5030000000000003d3645c6e6008583570d618041b2452497d55c0cb936529af92f8191e0dbdcc9764b8250ef75051ac733c738`, - `010000006cb4d23bba90d14f11d0719a02bdb0218e7b9a82a4453e0a51010000000000007caa984f729a85cec6ce000b87894bb30897195d36bc6491be92e7628de00d02e54d8250ef75051ac9b86274`, - `01000000a5a2223dc3f0a3f7901d14d064216d6ecf9d5dabfa44199f2b02000000000000e877c5cb9c52e27e3b2b9ff5d3b1658bd5c49ce2e3a1ef608759b44eea5f832b424e8250ef75051a47a25741`, - `0100000096bacf353c5534bc30044268588fb752df9c164a3ad14bd64d04000000000000a25ec7252a9a994d864b664bb4c5a2b1d8c1d688dc82ab78128eae34481110f416518250ef75051a1ac7889f`, - `020000009c37b58365fedc02e5ce0fa879e14d3ec110a3700b6476b941000000000000007e82e8388e953e200ab3e41777e4f9982ce3fe3aa46cc29a8c8f974706f76f852b508250ef75051a9e6f81c1`, - `010000001f7db2278bb6aeac530fa48e0f8e75cae092a89b6cbedb838500000000000000d7dfbe2005869b3319244192ba2fef1b0c9f1cf999da51be7d8f1a7f5bb02c6bf0538250ef75051a82d85a5f`, - `01000000575cdd3be997ebc63b377e78ecde7f14ef354669ff7d62543703000000000000a7b0a3354071262e02c246ad1d49d6823f6d864aef247263a07641a1d419089b5a5a8250ef75051a1e7d568e`, - `01000000fb49bbe46f780f8b0aa4b99ceddf1202e76d12fa4f5da1acdf000000000000003ec200da269c721728c820e305d01d4656e4ba0f6784c4fdeea05a904e0f0aab395d8250ef75051ac332b7b5`, - `02000000418574a33d0657d095a8ff4723e8e1fafedcabe32b93d1ccf6010000000000006f95d48f47f27715f6c1bcc466c4c715a17f9e2be39ce1c41106baed2d146d546f5f8250ef75051a77fc3e40`, - `01000000d19ffbe9a876f329acb05feadc90dae27578762e3f538f5a08050000000000005c8524057100e644fb3680b5b5af3d79f24fc4e51f374d9205e926e9b9cafd9c38648250ef75051a20379982`, - `02000000670ffede95831fb41c09d0d104285d6182ce1c8577da40506405000000000000e54435f50bfc776b8f3d9ac047963ee6bdddd8d40b69236b4d97acb52a1fdce41e678250ef75051a88842656`, + // read the json file + // example: ./data/sample.json + if len(os.Args) < 2 { + log.Error().Msg("Missing filename.\nUsage: bitcoin-lightclient ") + return + } + sampleFilename := os.Args[1] + + if _, err := os.Stat(sampleFilename); os.IsNotExist(err) { + log.Error().Msgf("Sample file does not exist: %s", sampleFilename) + return } + startHeight, headerStrings, err := data.ReadJson(sampleFilename) + if err != nil { + log.Error().Msgf("Error reading data file: %s", err) + return + } headers := make([]wire.BlockHeader, len(headerStrings)) for id, headerStr := range headerStrings { @@ -38,7 +41,7 @@ func main() { headers[id] = h } - btcLC := btclightclient.NewBTCLightClientWithData(&chaincfg.MainNetParams, headers, startHeight) + btcLC := btclightclient.NewBTCLightClientWithData(&chaincfg.MainNetParams, headers, int(startHeight)) btcLC.Status() rpcService := rpcserver.NewRPCServer(btcLC)