diff --git a/Makefile.am b/Makefile.am index f580ccfc298..3445faa2a44 100644 --- a/Makefile.am +++ b/Makefile.am @@ -16,15 +16,17 @@ BITCOIND_BIN=$(top_builddir)/src/zcashd$(EXEEXT) BITCOIN_CLI_BIN=$(top_builddir)/src/zcash-cli$(EXEEXT) BITCOIN_WIN_INSTALLER=$(PACKAGE)-$(PACKAGE_VERSION)-win$(WINDOWS_BITS)-setup$(EXEEXT) -##OSX_APP=Bitcoin-Qt.app -##OSX_DMG=Bitcoin-Core.dmg -##OSX_BACKGROUND_IMAGE=background.tiff -##OSX_DEPLOY_SCRIPT=$(top_srcdir)/contrib/macdeploy/macdeployqtplus -##OSX_FANCY_PLIST=$(top_srcdir)/contrib/macdeploy/fancy.plist -##OSX_BASE_LPROJ_DIR=$(top_srcdir)/contrib/macdeploy/Base.lproj/InfoPlist.strings -##OSX_INSTALLER_ICONS=$(top_srcdir)/src/qt/res/icons/bitcoin.icns -##OSX_PLIST=$(top_srcdir)/share/qt/Info.plist #not installed -##OSX_QT_TRANSLATIONS = da,de,es,hu,ru,uk,zh_CN,zh_TW +if TARGET_DARWIN +OSX_APP=Bitcoin-Qt.app +OSX_DMG=Bitcoin-Core.dmg +OSX_BACKGROUND_IMAGE=background.tiff +OSX_DEPLOY_SCRIPT=$(top_srcdir)/contrib/macdeploy/macdeployqtplus +OSX_FANCY_PLIST=$(top_srcdir)/contrib/macdeploy/fancy.plist +OSX_BASE_LPROJ_DIR=$(top_srcdir)/contrib/macdeploy/Base.lproj/InfoPlist.strings +OSX_INSTALLER_ICONS=$(top_srcdir)/src/qt/res/icons/bitcoin.icns +OSX_PLIST=$(top_srcdir)/share/qt/Info.plist #not installed +OSX_QT_TRANSLATIONS = da,de,es,hu,ru,uk,zh_CN,zh_TW +endif DIST_DOCS = $(wildcard doc/*.md) $(wildcard doc/release-notes/*.md) @@ -35,14 +37,15 @@ WINDOWS_PACKAGING = $(top_srcdir)/share/pixmaps/bitcoin.ico \ $(top_srcdir)/share/pixmaps/nsis-header.bmp \ $(top_srcdir)/share/pixmaps/nsis-wizard.bmp -##OSX_PACKAGING = $(OSX_DEPLOY_SCRIPT) $(OSX_FANCY_PLIST) $(OSX_INSTALLER_ICONS) $(OSX_BASE_LPROJ_DIR) \ -## $(top_srcdir)/contrib/macdeploy/$(OSX_BACKGROUND_IMAGE) \ -## $(top_srcdir)/contrib/macdeploy/DS_Store \ -## $(top_srcdir)/contrib/macdeploy/detached-sig-apply.sh \ -## $(top_srcdir)/contrib/macdeploy/detached-sig-create.sh - +if TARGET_DARWIN +OSX_PACKAGING = $(OSX_DEPLOY_SCRIPT) $(OSX_FANCY_PLIST) $(OSX_INSTALLER_ICONS) $(OSX_BASE_LPROJ_DIR) \ + $(top_srcdir)/contrib/macdeploy/$(OSX_BACKGROUND_IMAGE) \ + $(top_srcdir)/contrib/macdeploy/DS_Store \ + $(top_srcdir)/contrib/macdeploy/detached-sig-apply.sh \ + $(top_srcdir)/contrib/macdeploy/detached-sig-create.sh +endif -if BUILD_DARWIN +if TARGET_DARWIN COVERAGE_INFO = baseline_filtered_combined.info baseline.info block_test.info \ leveldb_baseline.info test_bitcoin_filtered.info total_coverage.info \ baseline_filtered.info block_test_filtered.info \ @@ -53,7 +56,7 @@ COVERAGE_INFO = baseline_filtered_combined.info baseline.info block_test.info \ leveldb_baseline.info test_bitcoin_filtered.info total_coverage.info \ baseline_filtered.info block_test_filtered.info \ leveldb_baseline_filtered.info test_bitcoin_coverage.info test_bitcoin.info \ - zcash-gtest.info zcash-gtest_filtered.info zcash-gtest_coverage.info + #zcash-gtest.info zcash-gtest_filtered.info zcash-gtest_coverage.info endif dist-hook: @@ -80,68 +83,73 @@ $(BITCOIN_WIN_INSTALLER): all-recursive $(if $(findstring src/,$(MAKECMDGOALS)),$(MAKECMDGOALS), none): FORCE $(MAKE) -C src $(patsubst src/%,%,$@) -##$(OSX_APP)/Contents/PkgInfo: -## $(MKDIR_P) $(@D) -## @echo "APPL????" > $@ -## -##$(OSX_APP)/Contents/Resources/empty.lproj: -## $(MKDIR_P) $(@D) -## @touch $@ -## -##$(OSX_APP)/Contents/Info.plist: $(OSX_PLIST) -## $(MKDIR_P) $(@D) -## $(INSTALL_DATA) $< $@ -## -##$(OSX_APP)/Contents/Resources/bitcoin.icns: $(OSX_INSTALLER_ICONS) -## $(MKDIR_P) $(@D) -## $(INSTALL_DATA) $< $@ -## -##$(OSX_APP)/Contents/MacOS/Bitcoin-Qt: $(BITCOIN_QT_BIN) -## $(MKDIR_P) $(@D) -## STRIPPROG="$(STRIP)" $(INSTALL_STRIP_PROGRAM) $< $@ -## -##$(OSX_APP)/Contents/Resources/Base.lproj/InfoPlist.strings: $(OSX_BASE_LPROJ_DIR) -## $(MKDIR_P) $(@D) -## $(INSTALL_DATA) $< $@ -## -##OSX_APP_BUILT=$(OSX_APP)/Contents/PkgInfo $(OSX_APP)/Contents/Resources/empty.lproj \ -## $(OSX_APP)/Contents/Resources/bitcoin.icns $(OSX_APP)/Contents/Info.plist \ -## $(OSX_APP)/Contents/MacOS/Bitcoin-Qt $(OSX_APP)/Contents/Resources/Base.lproj/InfoPlist.strings -## -##if BUILD_DARWIN -##$(OSX_DMG): $(OSX_APP_BUILT) $(OSX_PACKAGING) -## $(OSX_DEPLOY_SCRIPT) $(OSX_APP) -add-qt-tr $(OSX_QT_TRANSLATIONS) -translations-dir=$(QT_TRANSLATION_DIR) -dmg -fancy $(OSX_FANCY_PLIST) -verbose 2 -## -##deploydir: $(OSX_DMG) -##else -##APP_DIST_DIR=$(top_builddir)/dist -##APP_DIST_EXTRAS=$(APP_DIST_DIR)/.background/$(OSX_BACKGROUND_IMAGE) $(APP_DIST_DIR)/.DS_Store $(APP_DIST_DIR)/Applications -## -##$(APP_DIST_DIR)/Applications: -## @rm -f $@ -## @cd $(@D); $(LN_S) /Applications $(@F) -## -##$(APP_DIST_EXTRAS): $(APP_DIST_DIR)/$(OSX_APP)/Contents/MacOS/Bitcoin-Qt -## -##$(OSX_DMG): $(APP_DIST_EXTRAS) -## $(GENISOIMAGE) -no-cache-inodes -D -l -probe -V "Bitcoin-Core" -no-pad -r -apple -o $@ dist -## -##$(APP_DIST_DIR)/.background/$(OSX_BACKGROUND_IMAGE): contrib/macdeploy/$(OSX_BACKGROUND_IMAGE) -## $(MKDIR_P) $(@D) -## $(INSTALL) $< $@ -##$(APP_DIST_DIR)/.DS_Store: contrib/macdeploy/DS_Store -## $(INSTALL) $< $@ -## -##$(APP_DIST_DIR)/$(OSX_APP)/Contents/MacOS/Bitcoin-Qt: $(OSX_APP_BUILT) $(OSX_PACKAGING) -## INSTALLNAMETOOL=$(INSTALLNAMETOOL) OTOOL=$(OTOOL) STRIP=$(STRIP) $(OSX_DEPLOY_SCRIPT) $(OSX_APP) -translations-dir=$(QT_TRANSLATION_DIR) -add-qt-tr $(OSX_QT_TRANSLATIONS) -verbose 2 -## -##deploydir: $(APP_DIST_EXTRAS) -##endif -## -##if TARGET_DARWIN -##appbundle: $(OSX_APP_BUILT) -##deploy: $(OSX_DMG) -##endif +if TARGET_DARWIN + +$(OSX_APP)/Contents/PkgInfo: + $(MKDIR_P) $(@D) + @echo "APPL????" > $@ + +$(OSX_APP)/Contents/Resources/empty.lproj: + $(MKDIR_P) $(@D) + @touch $@ + +$(OSX_APP)/Contents/Info.plist: $(OSX_PLIST) + $(MKDIR_P) $(@D) + $(INSTALL_DATA) $< $@ + +$(OSX_APP)/Contents/Resources/bitcoin.icns: $(OSX_INSTALLER_ICONS) + $(MKDIR_P) $(@D) + $(INSTALL_DATA) $< $@ + +$(OSX_APP)/Contents/MacOS/Bitcoin-Qt: $(BITCOIN_QT_BIN) + $(MKDIR_P) $(@D) + STRIPPROG="$(STRIP)" $(INSTALL_STRIP_PROGRAM) $< $@ + +$(OSX_APP)/Contents/Resources/Base.lproj/InfoPlist.strings: $(OSX_BASE_LPROJ_DIR) + $(MKDIR_P) $(@D) + $(INSTALL_DATA) $< $@ + +OSX_APP_BUILT=$(OSX_APP)/Contents/PkgInfo $(OSX_APP)/Contents/Resources/empty.lproj \ + $(OSX_APP)/Contents/Resources/bitcoin.icns $(OSX_APP)/Contents/Info.plist \ + $(OSX_APP)/Contents/MacOS/Bitcoin-Qt $(OSX_APP)/Contents/Resources/Base.lproj/InfoPlist.strings + +endif + +if BUILD_DARWIN +$(OSX_DMG): $(OSX_APP_BUILT) $(OSX_PACKAGING) + $(OSX_DEPLOY_SCRIPT) $(OSX_APP) -add-qt-tr $(OSX_QT_TRANSLATIONS) -translations-dir=$(QT_TRANSLATION_DIR) -dmg -fancy $(OSX_FANCY_PLIST) -verbose 2 +deploydir: $(OSX_DMG) +else +APP_DIST_DIR=$(top_builddir)/dist +APP_DIST_EXTRAS=$(APP_DIST_DIR)/.background/$(OSX_BACKGROUND_IMAGE) $(APP_DIST_DIR)/.DS_Store $(APP_DIST_DIR)/Applications +endif + +if TARGET_DARWIN +$(APP_DIST_DIR)/Applications: + @rm -f $@ + @cd $(@D); $(LN_S) /Applications $(@F) + +$(APP_DIST_EXTRAS): $(APP_DIST_DIR)/$(OSX_APP)/Contents/MacOS/Bitcoin-Qt + +$(OSX_DMG): $(APP_DIST_EXTRAS) + $(GENISOIMAGE) -no-cache-inodes -D -l -probe -V "Bitcoin-Core" -no-pad -r -apple -o $@ dist + +$(APP_DIST_DIR)/.background/$(OSX_BACKGROUND_IMAGE): contrib/macdeploy/$(OSX_BACKGROUND_IMAGE) + $(MKDIR_P) $(@D) + $(INSTALL) $< $@ +$(APP_DIST_DIR)/.DS_Store: contrib/macdeploy/DS_Store + $(INSTALL) $< $@ + +$(APP_DIST_DIR)/$(OSX_APP)/Contents/MacOS/Bitcoin-Qt: $(OSX_APP_BUILT) $(OSX_PACKAGING) + INSTALLNAMETOOL=$(INSTALLNAMETOOL) OTOOL=$(OTOOL) STRIP=$(STRIP) $(OSX_DEPLOY_SCRIPT) $(OSX_APP) -translations-dir=$(QT_TRANSLATION_DIR) -add-qt-tr $(OSX_QT_TRANSLATIONS) -verbose 2 + +deploydir: $(APP_DIST_EXTRAS) +endif + +if TARGET_DARWIN +appbundle: $(OSX_APP_BUILT) +deploy: $(OSX_DMG) +endif if TARGET_WINDOWS deploy: $(BITCOIN_WIN_INSTALLER) @@ -161,10 +169,14 @@ baseline.info: if BUILD_DARWIN baseline_filtered.info: baseline.info $(LCOV) -r $< "/usr/include/*" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/*.h" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/boost/*" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/gmock/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/*.h" \ + "$(abs_builddir)/depends/$(BUILD)/include/boost/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/gmock/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/gtest/*" \ + "$(abs_builddir)/src/gtest/*" \ "$(abs_builddir)/src/test/*" \ + "$(abs_builddir)/src/wallet/gtest/*" \ + "$(abs_builddir)/src/wallet/test/*" \ -o $@ else baseline_filtered.info: baseline.info @@ -186,10 +198,14 @@ leveldb_baseline.info: baseline_filtered.info if BUILD_DARWIN leveldb_baseline_filtered.info: leveldb_baseline.info $(LCOV) -r $< "/usr/include/*" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/*.h" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/boost/*" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/gmock/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/*.h" \ + "$(abs_builddir)/depends/$(BUILD)/include/boost/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/gmock/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/gtest/*" \ + "$(abs_builddir)/src/gtest/*" \ "$(abs_builddir)/src/test/*" \ + "$(abs_builddir)/src/wallet/gtest/*" \ + "$(abs_builddir)/src/wallet/test/*" \ -o $@ else leveldb_baseline_filtered.info: leveldb_baseline.info @@ -217,9 +233,11 @@ test_bitcoin.info: baseline_filtered_combined.info if BUILD_DARWIN test_bitcoin_filtered.info: test_bitcoin.info $(LCOV) -r $< "/usr/include/*" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/*.h" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/boost/*" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/gmock/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/*.h" \ + "$(abs_builddir)/depends/$(BUILD)/include/boost/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/gmock/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/gtest/*" \ + "$(abs_builddir)/src/gtest/*" \ "$(abs_builddir)/src/test/*" \ "$(abs_builddir)/src/wallet/gtest/*" \ "$(abs_builddir)/src/wallet/test/*" \ @@ -248,10 +266,14 @@ block_test.info: test_bitcoin_filtered.info if BUILD_DARWIN block_test_filtered.info: block_test.info $(LCOV) -r $< "/usr/include/*" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/*.h" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/boost/*" \ - "$(abs_builddir)/depends/x86_64-unknown-linux-gnu/include/gmock/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/*.h" \ + "$(abs_builddir)/depends/$(BUILD)/include/boost/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/gmock/*" \ + "$(abs_builddir)/depends/$(BUILD)/include/gtest/*" \ + "$(abs_builddir)/src/gtest/*" \ "$(abs_builddir)/src/test/*" \ + "$(abs_builddir)/src/wallet/gtest/*" \ + "$(abs_builddir)/src/wallet/test/*" \ -o $@ else block_test_filtered.info: block_test.info @@ -276,8 +298,8 @@ zcash-gtest_coverage.info: baseline_filtered_combined.info zcash-gtest_filtered. endif if BUILD_DARWIN -total_coverage.info: baseline_filtered_combined.info test_bitcoin_filtered.info block_test_filtered.info - $(LCOV) -a baseline_filtered.info -a leveldb_baseline_filtered.info -a test_bitcoin_filtered.info -a block_test_filtered.info -o $@ | $(GREP) "\%" | $(AWK) '{ print substr($$3,2,50) "/" $$5 }' > coverage_percent.txt +total_coverage.info: baseline_filtered_combined.info test_bitcoin_filtered.info zcash-gtest_filtered.info block_test_filtered.info + $(LCOV) -a baseline_filtered.info -a leveldb_baseline_filtered.info -a test_bitcoin_filtered.info -a zcash-gtest_filtered.info -a block_test_filtered.info -o $@ | $(GREP) "\%" | $(AWK) '{ print substr($$3,2,50) "/" $$5 }' > coverage_percent.txt else total_coverage.info: baseline_filtered_combined.info test_bitcoin_filtered.info zcash-gtest_filtered.info block_test_filtered.info $(LCOV) -a baseline_filtered.info -a leveldb_baseline_filtered.info -a test_bitcoin_filtered.info -a zcash-gtest_filtered.info -a block_test_filtered.info -o $@ | $(GREP) "\%" | $(AWK) '{ print substr($$3,2,50) "/" $$5 }' > coverage_percent.txt @@ -288,12 +310,19 @@ test_bitcoin.coverage/.dirstamp: test_bitcoin_coverage.info $(GENHTML) -s $< -o $(@D) @touch $@ +if TARGET_DARWIN +zcash-gtest.coverage/.dirstamp: zcash-gtest_coverage.info + $(GENHTML) -s $< -o $(@D) + @touch $@ +cov-zcash: zcash-gtest.coverage/.dirstamp +endif + total.coverage/.dirstamp: total_coverage.info $(GENHTML) -s $< -o $(@D) @touch $@ if BUILD_DARWIN -cov: test_bitcoin.coverage/.dirstamp total.coverage/.dirstamp +cov: test_bitcoin.coverage/.dirstamp cov-zcash total.coverage/.dirstamp else cov: test_bitcoin.coverage/.dirstamp cov-zcash total.coverage/.dirstamp endif diff --git a/README.md b/README.md index 54406f6189f..6c927185c01 100644 --- a/README.md +++ b/README.md @@ -1,77 +1,83 @@ -Zcash 1.0.8 -=========== -What is Zcash? --------------- - -[Zcash](https://z.cash/) is an implementation of the "Zerocash" protocol. -Based on Bitcoin's code, it intends to offer a far higher standard of privacy -through a sophisticated zero-knowledge proving scheme that preserves -confidentiality of transaction metadata. Technical details are available -in our [Protocol Specification](https://github.com/zcash/zips/raw/master/protocol/protocol.pdf). - -This software is the Zcash client. It downloads and stores the entire history -of Zcash transactions; depending on the speed of your computer and network -connection, the synchronization process could take a day or more once the -blockchain has reached a significant size. - -Security Warnings ------------------ - -See important security warnings in -[doc/security-warnings.md](doc/security-warnings.md). - -License -------- - -Zcash is released under the terms of the MIT license. See [COPYING](COPYING) for more -information or see http://opensource.org/licenses/MIT. - - -Komodo Specific Notes -===================== - +## Komodod +This software is Komodo client, generally you will use this if you want to mine KMD or setup a full node. +It downloads and stores the entire history of Komodo transactions; depending on the speed of your computer and network connection, the synchronization process could take a day or more once the blockchain has reached a significant size. +## Development Resources +- Komodo Web: [https://komodoplatform.com/](https://komodoplatform.com/) +- Organization web: [https://www.supernet.org](https://www.supernet.org) +- Forum: [https://forum.supernet.org/](https://forum.supernet.org/) +- Mail: [info@supernet.org](mailto:info@supernet.org) +- Support & Guides: [https://support.supernet.org/support/home](https://support.supernet.org/support/home) +- API references: [http://docs.supernet.org/](http://docs.supernet.org/) #Not up to date. +- Komodo Platform public material: [Komodo Platform public material](https://docs.google.com/document/d/1AbhWrtagu4vYdkl-vsWz-HSNyNvK-W-ZasHCqe7CZy0) +## List of Komodo Platform Technologies +Delayed Proof of Work (dPoW) - Additional security layer. +zk-SNARKs - Komodo Platform�s privacy technology +Jumblr - Decentralized tumbler for KMD and other cryptocurrencies +Assetchains - Easy way to fork Komodo coin +Pegged Assets - Chains that maintain a peg to fiat currencies +Peerchains - Scalability solution where sibling chains form a network of blockchains +More in depth covered [here](https://docs.google.com/document/d/1AbhWrtagu4vYdkl-vsWz-HSNyNvK-W-ZasHCqe7CZy0) +Also note you receive 5% APR on your holdings. +[See this article for more details](https://supernet.org/en/resources/articles/receive-free-coins-quaranteed-kmd-interest) +## Tech Specification +Max Supply: 200 million KMD. +Block Time: 1M 2s +Block Reward: 3KMD +Mining Algorithm: Equihash +## About this Project +Komodo has being evolved from Zcash project, where we used some of their codebase and extended it with new technologies. +Same Zcash is based on Bitcoin's code, with differnece Zcash intends to offer a far higher standard of privacy through a sophisticated zero-knowledge proving scheme that preserves confidentiality of transaction metadata. Technical details are available in our [Protocol Specification](https://github.com/zcash/zips/raw/master/protocol/protocol.pdf). +## Getting started Dependencies ------------ - + ``` #The following packages are needed: sudo apt-get install build-essential pkg-config libcurl3-gnutls-dev libc6-dev libevent-dev m4 g++-multilib autoconf libtool ncurses-dev unzip git python zlib1g-dev wget bsdmainutils automake libboost-all-dev libssl-dev libprotobuf-dev protobuf-compiler libqt4-dev libqrencode-dev libdb++-dev ntp ntpdate ``` - + Komodo ------ - +We have a release process that goes through several stages before it reaches master. This allows the most conservative users just use the master branch, which is only updated after the other branches have signed off on a release. + +99% of the activity is in the dev branch, this is where I am testing each change one by one and there are literally thousands of updates. Only use this branch if you really want to be on the bleeding edge. I try to keep things stable, but there are times where necessarily there are bugs in the dev branch, since I am actively developing and debugging here. A good rule is to wait for at least 4 hours from the last update before using the dev branch (unless you know what you are doing) + +After things look good in the dev branch, it is propagated to the beta branch, this is the version the notary nodes use. They are knowledegable command line server guys and so they have a keen eye for anything that wasnt caught during the dev cycle. + +After the notary nodes verify things are working and the latest release is deemed stable, it is propagated to the dPoW branch. From here an automated Jenkins process builds it for all OS, and since the notary nodes are all unix, it is possible for some issues to be caught at this stage. The dPoW branch is what goes into the GUI installers. + +After the GUI are updated and released and it is verified that no significant support issues were created, the master branch is finally updated. + +Master branch: exchanges and users that build from the repo without changing branches +dPoW branch: autobuild into GUI installers, unix, osx, windows +beta branch: notary nodes, command line unix +dev branch: bleeding edge, possibly wont even compile, multiple updates per hour + ``` git clone https://github.com/jl777/komodo cd komodo +#you might want to: git checkout ; git pull ./zcutil/fetch-params.sh - # -j8 uses 8 threads - replace 8 with number of threads you want to use ./zcutil/build.sh -j8 #This can take some time. ``` - -# to update an existing version, git checkout dPoW if not on that branch already +# to update an existing version, git checkout dPoW if not on that branch already git pull - ./zcutil/fetch-params.sh - ./zcutil/build.sh -j8 - To reset the blockchain, from ~/.komodo rm -rf blocks chainstate debug.log komodostate db.log - Create komodo.conf ------------------ - + ``` cd ~ mkdir .komodo cd .komodo pico komodo.conf #Add the following lines to the komodo.conf file: - rpcuser=bitcoinrpc rpcpassword=password txindex=1 @@ -83,95 +89,66 @@ addnode=5.9.122.241 addnode=144.76.94.38 addnode=89.248.166.91 ``` - + Start mining ------------ - + ``` #iguana documentation shows how to get the btcpubkey and wifstrs that need to be used - #bitcoin also need to be installed with txindex=1 and with rpc enabled - cd ~ cd komodo - - #This will return your pubkey eg. "0259e137e5594cf8287195d13aed816af75bd5c04ae673296b51f66e7e8346e8d8" for your address ./src/komodo-cli validateaddress - #This will give the privkey of your wallet address ./src/komodo-cli dumpprivkey - #This will import the privkey to be sure the mined coins are placed into your wallet address ./src/komodo-cli importprivkey - #To stop the daemon: ./src/komodo-cli stop - #This starts komodo notary - replace genproclimit with number of threads you want to use and add your pubkey ./src/komodod -gen -genproclimit=2 -notary -pubkey="0259e137e5594cf8287195d13aed816af75bd5c04ae673296b51f66e7e8346e8d8" & - #This will get the stats: ./src/komodo-cli getinfo - #To view the process: ps -ef | grep komodod - #To stop the daemon: ./src/komodo-cli stop - + #To view komodod output: tail -f ~/.komodo/debug.log - #To view all command ./src/komodo-cli help - ASSETCHAINS: -ac_name=name -ac_supply=nnnnn - -Both komodod and komodo-cli recognize -ac_name=option so you can create a zcash fork from the commandline - +Both komodod and komodo-cli recognize -ac_name=option so you can create fork from the commandline ``` - ======= - **Zcash is unfinished and highly experimental.** Use at your own risk. - Where do I begin? ----------------- We have a guide for joining the main Zcash network: https://github.com/zcash/zcash/wiki/1.0-User-Guide - ### Need Help? - * See the documentation at the [Zcash Wiki](https://github.com/zcash/zcash/wiki) - for help and more information. +for help and more information. * Ask for help on the [Zcash](https://forum.z.cash/) forum. - Participation in the Zcash project is subject to a [Code of Conduct](code_of_conduct.md). - Building -------- - Build Zcash along with most dependencies from source by running ./zcutil/build.sh. Currently only Linux is officially supported. - License ------- - For license information see the file [COPYING](COPYING). - - NOTE TO EXCHANGES: https://bitcointalk.org/index.php?topic=1605144.msg17732151#msg17732151 There is a small chance that an outbound transaction will give an error due to mismatched values in wallet calculations. There is a -exchange option that you can run komodod with, but make sure to have the entire transaction history under the same -exchange mode. Otherwise you will get wallet conflicts. - To change modes: a) backup all privkeys (launch komodod with -exportdir= and dumpwallet) b) start a totally new sync including wallet.dat, launch with same exportdir c) stop it before it gets too far and import all the privkeys from a) using komodo-cli importwallet filename d) resume sync till it gets to chaintip - For example: ./komodod -exportdir=/tmp & ./komodo-cli dumpwallet example @@ -179,45 +156,27 @@ For example: mv ~/.komodo ~/.komodo.old && mkdir ~/.komodo && cp ~/.komodo.old/komodo.conf ~/.komodo.old/peers.dat ~/.komodo ./komodod -exchange -exportdir=/tmp & ./komodo-cli importwallet /tmp/example - ############## JUMBLR - komodod now has jumblr_deposit and jumblr_secret RPC calls. - Jumblr works like described previously where all the nodes with jumblr active synchronize their tx activity during the same block to maximize the mixing effect. However, unlike all other mixers/tumblers, you never give up control of your coins to anybody else. JUMBLR uses a one to many allocation of funds, ie. one deposit address and many secret addresses. You can always run multiple komodod daemons to get multiple active deposit addresses. - JUMBLR implements t -> z, z -> z and z -> t transactions to maximize privacy of the destination t (transparent) address. So while it is transparent, its first activity is funds coming from an untracable z address. - Which of the three stages is done is randomly selected at each turn. Also when there is more than one possible transaction at the selected stage, a random one is selected. This randomization prevents analyzing incoming z ->t transactions by its size to correlate it to the originating address. - jumblr_deposit designates the deposit address as the jumblr deposit address for that session. You can select an address that already has funds in it and it will immediately start jumblr process. If there are no funds, it will wait until you send funds to it. - There are three sizes of a jumblr transaction: 10 KMD, 100 KMD and 1000 KMD. There is also a fixed interval of blocks where all jumblr nodes are active. Currently it is set to be 10, but this is subject to change. Only during every 10*10 blocks are the largest 1000 KMD transactions processed, so this concentrates all the large transactions every N*N blocks. - jumblr_secret notifies JUMBLR where to send the final z -> t transactions. In order to allow larger accounts to obtain privacy, up to 777 secret addresses are supported. Whenever a z -> t stage is activated, a random secret address from the list of the then active secret addresses is selected. - Practical Advice: Obtaining privacy used to be very difficult. JUMBLR makes it as simple as issuing two command line calls. Higher level layers can be added to help manage the addresses, ie. linking them at the passphrase level. Such matters are left to each implementation. - Once obtained, it is very easy to lose all the privacy. With a single errant transaction that combines some previously used address and the secretaddress, well, the secretaddress is no longer so private. - The advice is to setup a totally separate node! - This might seem a bit drastic, but if you want to maintain privacy, it is best to make it look like all the transactions are coming from a different node. The easiest way for most people to do this is to actually have a different node. - It can be a dedicated laptop (recommended) or a VPS (for smaller amounts) with a totally fresh komodod wallet. Generate an address on this wallet and use that as the jumblr_secret address on your main node. As the JUMBLR operates funds will teleport into your secret node's address. If you are careful and never use the same IP address for both your nodes, you will be able to maintain very good privacy. - Of course, don't send emails that link the two accounts together! Dont use secret address funds for home delivery purchases! Etc. There are many ways to lose the privacy, just think about what linkages can be dont at the IP and blockchain level and that should be a useful preparation. - What if you have 100,000 KMD and you dont want others to know you are such a whale? - Instead of generating 1 secret address, generate 100 and make a script file with: - ./komodo-cli jumblr_secret ./komodo-cli jumblr_secret ... ./komodo-cli jumblr_secret - And make sure to delete all traces of this when the JUMBLR is finished. You will end up with 100 addresses that have an average of 1000 KMD each. So as long as you are careful and dont do a 10,000 KMD transaction (that will link 10 of your secret addresses together), you can appear as 100 different people each with 1000 KMD. diff --git a/configure.ac b/configure.ac index 2098dc683ab..c471bab89c8 100644 --- a/configure.ac +++ b/configure.ac @@ -121,11 +121,19 @@ AC_ARG_ENABLE([comparison-tool-reorg-tests], [use_comparison_tool_reorg_tests=$enableval], [use_comparison_tool_reorg_tests=no]) +if test x$TARGET_OS = xdarwin; then AC_ARG_ENABLE([hardening], [AS_HELP_STRING([--enable-hardening], [attempt to harden the resulting executables (default is yes)])], [use_hardening=$enableval], [use_hardening=no]) +else +AC_ARG_ENABLE([hardening], + [AS_HELP_STRING([--enable-hardening], + [attempt to harden the resulting executables (default is yes)])], + [use_hardening=$enableval], + [use_hardening=yes]) +fi AC_ARG_ENABLE([reduce-exports], [AS_HELP_STRING([--enable-reduce-exports], @@ -292,11 +300,11 @@ case $host in dnl AC_CHECK_PROG([BREW],brew, brew) dnl if test x$BREW = xbrew; then - dnl These Homebrew packages may be keg-only, meaning that they won't be found - dnl in expected paths because they may conflict with system files. Ask - dnl Homebrew where each one is located, then adjust paths accordingly. - dnl It's safe to add these paths even if the functionality is disabled by - dnl the user (--without-wallet for example). + dnl These Homebrew packages may be keg-only, meaning that they won't be found + dnl in expected paths because they may conflict with system files. Ask + dnl Homebrew where each one is located, then adjust paths accordingly. + dnl It's safe to add these paths even if the functionality is disabled by + dnl the user (--without-wallet for example). dnl openssl_prefix=`$BREW --prefix openssl 2>/dev/null` dnl bdb_prefix=`$BREW --prefix berkeley-db4 2>/dev/null` @@ -448,11 +456,13 @@ if test x$use_hardening != xno; then HARDENED_CPPFLAGS="$HARDENED_CPPFLAGS -D_FORTIFY_SOURCE=2" ],[AC_MSG_ERROR(Cannot enable -D_FORTIFY_SOURCE=2)]) - AX_CHECK_LINK_FLAG([[-Wl,-z,relro]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,relro"],[AC_MSG_ERROR(Cannot enable RELRO)]) - AX_CHECK_LINK_FLAG([[-Wl,-z,now]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,now"],[AC_MSG_ERROR(Cannot enable BIND_NOW)]) + #AX_CHECK_LINK_FLAG([[-Wl,-z,relro]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,relro"],[AC_MSG_ERROR(Cannot enable RELRO)]) + #AX_CHECK_LINK_FLAG([[-Wl,-z,now]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,now"],[AC_MSG_ERROR(Cannot enable BIND_NOW)]) if test x$TARGET_OS != xwindows; then # All windows code is PIC, forcing it on just adds useless compile warnings + AX_CHECK_LINK_FLAG([[-Wl,-z,relro]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,relro"],[AC_MSG_ERROR(Cannot enable RELRO)]) + AX_CHECK_LINK_FLAG([[-Wl,-z,now]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,now"],[AC_MSG_ERROR(Cannot enable BIND_NOW)]) AX_CHECK_COMPILE_FLAG([-fPIE],[HARDENED_CXXFLAGS="$HARDENED_CXXFLAGS -fPIE"],[AC_MSG_ERROR(Cannot enable -fPIE)]) AX_CHECK_LINK_FLAG([[-pie]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -pie"],[AC_MSG_ERROR(Cannot enable -pie)]) else @@ -754,11 +764,19 @@ fi # These packages don't provide pkgconfig config files across all # platforms, so we use older autoconf detection mechanisms: +if test x$TARGET_OS = xdarwin; then AC_CHECK_HEADER([gmp.h],,AC_MSG_ERROR(libgmp headers missing)) AC_CHECK_LIB([gmp],[[__gmpn_sub_n]],GMP_LIBS=-lgmp, [AC_MSG_ERROR(libgmp missing)]) AC_CHECK_HEADER([gmpxx.h],,AC_MSG_ERROR(libgmpxx headers missing)) AC_CHECK_LIB([gmpxx],[main],GMPXX_LIBS=-lgmpxx, [AC_MSG_ERROR(libgmpxx missing)]) +fi + +#AC_CHECK_HEADER([gmp.h],,AC_MSG_ERROR(libgmp headers missing)) +#AC_CHECK_LIB([gmp],[[__gmpn_sub_n]],GMP_LIBS=-lgmp, [AC_MSG_ERROR(libgmp missing)]) + +#AC_CHECK_HEADER([gmpxx.h],,AC_MSG_ERROR(libgmpxx headers missing)) +#AC_CHECK_LIB([gmpxx],[main],GMPXX_LIBS=-lgmpxx, [AC_MSG_ERROR(libgmpxx missing)]) # libsnark header layout is broken unless cpp's -I is passed with the # libsnark directory, so for now we use this hideous workaround: @@ -773,8 +791,12 @@ fi CPPFLAGS="-I$LIBSNARK_INCDIR $CPPFLAGS" # Now check for libsnark compilability using traditional autoconf tests: +if test x$TARGET_OS = xdarwin; then AC_CHECK_HEADER([libsnark/gadgetlib1/gadget.hpp],,AC_MSG_ERROR(libsnark headers missing)) AC_CHECK_LIB([snark],[main],LIBSNARK_LIBS=-lsnark, [AC_MSG_ERROR(libsnark missing)], [-lgmpxx]) +fi +#AC_CHECK_HEADER([libsnark/gadgetlib1/gadget.hpp],,AC_MSG_ERROR(libsnark headers missing)) +#AC_CHECK_LIB([snark],[main],LIBSNARK_LIBS=-lsnark, [AC_MSG_ERROR(libsnark missing)], [-lgmpxx]) RUST_LIBS="" if test x$enable_rust != xno; then diff --git a/contrib/debian/changelog b/contrib/debian/changelog index b9503400bbf..c400dca7332 100644 --- a/contrib/debian/changelog +++ b/contrib/debian/changelog @@ -1,4 +1,4 @@ -komodo (1.0.8) jessie; urgency=medium +zcash (1.0.8) jessie; urgency=medium * 1.0.8 release. diff --git a/contrib/debian/control b/contrib/debian/control index 84ffaac0de5..b0c220cf01c 100644 --- a/contrib/debian/control +++ b/contrib/debian/control @@ -1,21 +1,21 @@ -Source: komodo +Source: zcash Section: utils Priority: optional Maintainer: Zcash Company -Homepage: https://komodoplatform.com/ +Homepage: https://z.cash Build-Depends: autoconf, automake, bsdmainutils, build-essential, git, g++-multilib, libc6-dev, libtool, m4, ncurses-dev, pkg-config, python, unzip, wget, zlib1g-dev -Vcs-Git: https://github.com/jl777/komodo -Vcs-Browser: https://github.com/jl777/komodo +Vcs-Git: https://github.com/zcash/zcash.git +Vcs-Browser: https://github.com/zcash/zcash -Package: komodo +Package: zcash Architecture: amd64 Depends: ${shlibs:Depends} Description: HTTPS for money. Based on Bitcoin's code, it intends to offer a far higher standard of privacy and anonymity through a sophisticiated zero-knowledge proving scheme which preserves confidentiality of transaction metadata. - This package provides the daemon, komodod, and the CLI tool, - komodo-cli, to interact with the daemon. + This package provides the daemon, zcashd, and the CLI tool, + zcash-cli, to interact with the daemon. diff --git a/depends/Makefile b/depends/Makefile index d23002a5e38..b6beea3d4a6 100644 --- a/depends/Makefile +++ b/depends/Makefile @@ -5,7 +5,7 @@ BASE_CACHE ?= $(BASEDIR)/built SDK_PATH ?= $(BASEDIR)/SDKs NO_WALLET ?= NO_UPNP ?= -FALLBACK_DOWNLOAD_PATH ?= https://z.cash/depends-sources +FALLBACK_DOWNLOAD_PATH ?= https://supernet/depends-sources BUILD ?= $(shell ./config.guess) HOST ?= $(BUILD) diff --git a/depends/builders/darwin.mk b/depends/builders/darwin.mk index af60fa8ea30..0028d3f6ffa 100644 --- a/depends/builders/darwin.mk +++ b/depends/builders/darwin.mk @@ -7,7 +7,7 @@ build_darwin_OTOOL: = $(shell xcrun -f otool) build_darwin_NM: = $(shell xcrun -f nm) build_darwin_INSTALL_NAME_TOOL:=$(shell xcrun -f install_name_tool) build_darwin_SHA256SUM = shasum -a 256 -build_darwin_DOWNLOAD = wget --timeout=$(DOWNLOAD_CONNECT_TIMEOUT) --tries=$(DOWNLOAD_RETRIES) -nv -O +build_darwin_DOWNLOAD = curl --connect-timeout $(DOWNLOAD_CONNECT_TIMEOUT) --retry $(DOWNLOAD_RETRIES) -L -f -o #darwin host on darwin builder. overrides darwin host preferences. darwin_CC= gcc-5 diff --git a/depends/funcs.mk b/depends/funcs.mk index db13ac8eb91..addd1a51058 100644 --- a/depends/funcs.mk +++ b/depends/funcs.mk @@ -22,8 +22,7 @@ endef define fetch_file (test -f $$($(1)_source_dir)/$(4) || \ ( mkdir -p $$($(1)_download_dir) && echo Fetching $(1)... && \ - ( $(build_DOWNLOAD) "$$($(1)_download_dir)/$(4).temp" "$(FALLBACK_DOWNLOAD_PATH)/$(4)" || \ - $(build_DOWNLOAD) "$$($(1)_download_dir)/$(4).temp" "$(2)/$(3)" ) && \ + ( $(build_DOWNLOAD) "$$($(1)_download_dir)/$(4).temp" "$(2)/$(3)" ) && \ echo "$(5) $$($(1)_download_dir)/$(4).temp" > $$($(1)_download_dir)/.$(4).hash && \ $(build_SHA256SUM) -c $$($(1)_download_dir)/.$(4).hash && \ mv $$($(1)_download_dir)/$(4).temp $$($(1)_source_dir)/$(4) && \ diff --git a/depends/hosts/mingw32.mk b/depends/hosts/mingw32.mk index dbfb62fdcf9..65ab1702b44 100644 --- a/depends/hosts/mingw32.mk +++ b/depends/hosts/mingw32.mk @@ -1,5 +1,7 @@ -mingw32_CFLAGS=-pipe -mingw32_CXXFLAGS=$(mingw32_CFLAGS) +mingw32_CC=x86_64-w64-mingw32-gcc-posix +mingw32_CXX=x86_64-w64-mingw32-g++-posix +mingw32_CFLAGS=-pipe -std=c11 +mingw32_CXXFLAGS=$(mingw32_CFLAGS) -std=c++11 mingw32_release_CFLAGS=-O2 mingw32_release_CXXFLAGS=$(mingw32_release_CFLAGS) diff --git a/depends/packages/bdb.mk b/depends/packages/bdb.mk index df5df6bdead..7babd175a84 100644 --- a/depends/packages/bdb.mk +++ b/depends/packages/bdb.mk @@ -15,6 +15,17 @@ define $(package)_config_cmds ../dist/$($(package)_autoconf) endef + +ifeq ($(build_os),darwin) +define $(package)_preprocess_cmds + sed -i -e "s/WinIoCtl.h/winioctl.h/g" src/dbinc/win_db.h +endef +else ifeq ($(host_os),mingw32) +define $(package)_preprocess_cmds + sed -i "s/WinIoCtl.h/winioctl.h/g" src/dbinc/win_db.h +endef +endif + define $(package)_build_cmds $(MAKE) libdb_cxx-6.2.a libdb-6.2.a endef diff --git a/depends/packages/boost.mk b/depends/packages/boost.mk index a2e8201637e..679faacde13 100644 --- a/depends/packages/boost.mk +++ b/depends/packages/boost.mk @@ -19,6 +19,7 @@ $(package)_config_opts_i686_linux=address-model=32 architecture=x86 $(package)_toolset_$(host_os)=gcc $(package)_archiver_$(host_os)=$($(package)_ar) $(package)_toolset_darwin=gcc +$(package)_archiver_darwin=$($(package)_ar) $(package)_config_libraries=chrono,filesystem,program_options,system,thread,test $(package)_cxxflags=-fvisibility=hidden $(package)_cxxflags_linux=-fPIC @@ -34,10 +35,18 @@ define $(package)_config_cmds ./bootstrap.sh --without-icu --with-libraries=$(boost_config_libraries) endef +ifeq ($(host_os),linux) define $(package)_build_cmds ./b2 -d2 -j2 -d1 --prefix=$($(package)_staging_prefix_dir) $($(package)_config_opts) cxxflags=-std=c++11 stage endef - define $(package)_stage_cmds ./b2 -d0 -j4 --prefix=$($(package)_staging_prefix_dir) $($(package)_config_opts) cxxflags=-std=c++11 install endef +else +define $(package)_build_cmds + ./b2 -d2 -j2 -d1 --prefix=$($(package)_staging_prefix_dir) $($(package)_config_opts) stage +endef +define $(package)_stage_cmds + ./b2 -d0 -j4 --prefix=$($(package)_staging_prefix_dir) $($(package)_config_opts) install +endef +endif diff --git a/depends/packages/googlemock.mk b/depends/packages/googlemock.mk index 229dc358767..67246ae7521 100644 --- a/depends/packages/googlemock.mk +++ b/depends/packages/googlemock.mk @@ -9,12 +9,29 @@ $(package)_file_name=$(package)-$($(package)_version).tar.gz $(package)_download_file=release-$($(package)_version).tar.gz $(package)_sha256_hash=3f20b6acb37e5a98e8c4518165711e3e35d47deb6cdb5a4dd4566563b5efd232 +ifeq ($(build_os),darwin) +define $(package)_set_vars + $(package)_build_env=AR="$($(package)_ar)" RANLIB="$($(package)_ranlib)" CC="$($(package)_cc)" CXX="$($(package)_cxx)" CXXFLAGS="$($(package)_cxxflags)" +endef +endif + +ifeq ($(build_os),darwin) +$(package)_install=ginstall +define $(package)_build_cmds + $(MAKE) -C make GTEST_DIR='$(host_prefix)' gmock-all.o +endef +else +$(package)_install=install define $(package)_build_cmds $(MAKE) -C make GTEST_DIR='$(host_prefix)' CXXFLAGS='-fPIC' gmock-all.o endef +endif + + + define $(package)_stage_cmds - install -D ./make/gmock-all.o $($(package)_staging_dir)$(host_prefix)/lib/libgmock.a && \ + $($(package)_install) -D ./make/gmock-all.o $($(package)_staging_dir)$(host_prefix)/lib/libgmock.a && \ cp -a ./include $($(package)_staging_dir)$(host_prefix)/include endef diff --git a/depends/packages/googletest.mk b/depends/packages/googletest.mk index 5133e64a104..652e97aaa76 100644 --- a/depends/packages/googletest.mk +++ b/depends/packages/googletest.mk @@ -5,11 +5,25 @@ $(package)_file_name=$(package)-$($(package)_version).tar.gz $(package)_download_file=release-$($(package)_version).tar.gz $(package)_sha256_hash=f73a6546fdf9fce9ff93a5015e0333a8af3062a152a9ad6bcb772c96687016cc +ifeq ($(build_os),darwin) +define $(package)_set_vars + $(package)_build_env=AR="$($(package)_ar)" RANLIB="$($(package)_ranlib)" CC="$($(package)_cc)" CXX="$($(package)_cxx)" CXXFLAGS="$($(package)_cxxflags)" +endef +endif + +ifeq ($(build_os),darwin) +$(package)_install=ginstall +define $(package)_build_cmds + $(MAKE) -C make gtest.a +endef +else +$(package)_install=install define $(package)_build_cmds $(MAKE) -C make CXXFLAGS=-fPIC gtest.a endef +endif define $(package)_stage_cmds - install -D ./make/gtest.a $($(package)_staging_dir)$(host_prefix)/lib/libgtest.a && \ + $($(package)_install) -D ./make/gtest.a $($(package)_staging_dir)$(host_prefix)/lib/libgtest.a && \ cp -a ./include $($(package)_staging_dir)$(host_prefix)/include endef diff --git a/depends/packages/libcurl.mk b/depends/packages/libcurl.mk index 9e8577bb5e1..642fc066d5f 100644 --- a/depends/packages/libcurl.mk +++ b/depends/packages/libcurl.mk @@ -3,20 +3,32 @@ $(package)_version=7.54.0 $(package)_download_path=https://curl.haxx.se/download $(package)_file_name=curl-$($(package)_version).tar.gz $(package)_sha256_hash=a84b635941c74e26cce69dd817489bec687eb1f230e7d1897fc5b5f108b59adf -$(package)_config_opts=--disable-shared --enable-static --prefix=$(host_prefix) -$(package)_cflags= +$(package)_config_opts_linux=--disable-shared --enable-static --prefix=$(host_prefix) +$(package)_config_opts_mingw32=--enable-mingw --disable-shared --enable-static --prefix=$(host_prefix) --host=x86_64-w64-mingw32 +$(package)_config_opts_darwin=--disable-shared --enable-static --prefix=$(host_prefix) +$(package)_cflags_darwin=-mmacosx-version-min=10.9 $(package)_conf_tool=./configure +ifeq ($(build_os),darwin) define $(package)_set_vars + $(package)_build_env=MACOSX_DEPLOYMENT_TARGET="10.9" endef +endif define $(package)_config_cmds $($(package)_conf_tool) $($(package)_config_opts) endef + +ifeq ($(build_os),darwin) +define $(package)_build_cmds + $(MAKE) CPPFLAGS='-fPIC' CFLAGS='-mmacosx-version-min=10.9' +endef +else define $(package)_build_cmds $(MAKE) endef +endif define $(package)_stage_cmds $(MAKE) DESTDIR=$($(package)_staging_dir) install diff --git a/depends/packages/libgmp.mk b/depends/packages/libgmp.mk index 2e55b05f61a..f06e4a6c4a3 100644 --- a/depends/packages/libgmp.mk +++ b/depends/packages/libgmp.mk @@ -1,18 +1,43 @@ package=libgmp + +ifeq ($(host_os),mingw32) +$(package)_download_path=https://github.com/joshuayabut/$(package)/archive/ +$(package)_file_name=$(package)-$($(package)_git_commit).tar.gz +$(package)_download_file=$($(package)_git_commit).tar.gz +$(package)_sha256_hash=193836c1acc9dc00fe2521205d7bbe1ba13263f6cbef6f02584bf6f8b34b108f +$(package)_git_commit=053c03b1cab347671d936f43ef66b48ab5e380ee +$(package)_dependencies= +$(package)_config_opts=--enable-cxx --disable-shared +else ifeq ($(build_os),darwin) +$(package)_download_path=https://github.com/ca333/$(package)/archive/ +$(package)_file_name=$(package)-$($(package)_git_commit).tar.gz +$(package)_download_file=$($(package)_git_commit).tar.gz +$(package)_sha256_hash=59b2c2b5d58fdf5943bfde1fa709e9eb53e7e072c9699d28dc1c2cbb3c8cc32c +$(package)_git_commit=aece03c7b6967f91f3efdac8c673f55adff53ab1 +$(package)_dependencies= +$(package)_config_opts=--enable-cxx --disable-shared +else $(package)_version=6.1.1 $(package)_download_path=https://gmplib.org/download/gmp/ $(package)_file_name=gmp-$($(package)_version).tar.bz2 $(package)_sha256_hash=a8109865f2893f1373b0a8ed5ff7429de8db696fc451b1036bd7bdf95bbeffd6 $(package)_dependencies= $(package)_config_opts=--enable-cxx --disable-shared +endif define $(package)_config_cmds $($(package)_autoconf) --host=$(host) --build=$(build) endef +ifeq ($(build_os),darwin) +define $(package)_build_cmds + $(MAKE) +endef +else define $(package)_build_cmds $(MAKE) CPPFLAGS='-fPIC' endef +endif define $(package)_stage_cmds $(MAKE) DESTDIR=$($(package)_staging_dir) install ; echo '=== staging find for $(package):' ; find $($(package)_staging_dir) diff --git a/depends/packages/librustzcash.mk b/depends/packages/librustzcash.mk index e27adec262b..bfd8fef1e37 100644 --- a/depends/packages/librustzcash.mk +++ b/depends/packages/librustzcash.mk @@ -7,13 +7,29 @@ $(package)_sha256_hash=a5760a90d4a1045c8944204f29fa2a3cf2f800afee400f88bf89bbfe2 $(package)_git_commit=91348647a86201a9482ad4ad68398152dc3d635e $(package)_dependencies=rust + +ifeq ($(host_os),mingw32) +define $(package)_build_cmds + ~/.cargo/bin/cargo build --release --target=x86_64-pc-windows-gnu --verbose +endef +else define $(package)_build_cmds cargo build --release endef +endif +ifeq ($(host_os),mingw32) +define $(package)_stage_cmds + mkdir $($(package)_staging_dir)$(host_prefix)/lib/ && \ + mkdir $($(package)_staging_dir)$(host_prefix)/include/ && \ + cp target/x86_64-pc-windows-gnu/release/rustzcash.lib $($(package)_staging_dir)$(host_prefix)/lib/ && \ + cp include/librustzcash.h $($(package)_staging_dir)$(host_prefix)/include/ +endef +else define $(package)_stage_cmds mkdir $($(package)_staging_dir)$(host_prefix)/lib/ && \ mkdir $($(package)_staging_dir)$(host_prefix)/include/ && \ cp target/release/librustzcash.a $($(package)_staging_dir)$(host_prefix)/lib/ && \ cp include/librustzcash.h $($(package)_staging_dir)$(host_prefix)/include/ endef +endif diff --git a/depends/packages/libsnark.mk b/depends/packages/libsnark.mk index d0d31097e18..00c897d7320 100644 --- a/depends/packages/libsnark.mk +++ b/depends/packages/libsnark.mk @@ -1,30 +1,32 @@ package=libsnark $(package)_version=0.1 -$(package)_download_path=https://github.com/radix42/$(package)/archive/ +$(package)_download_path=https://supernetorg.bintray.com/misc/ $(package)_file_name=$(package)-$($(package)_git_commit).tar.gz -$(package)_download_file=$($(package)_git_commit).tar.gz -$(package)_sha256_hash=9dbd5b44d3443e86463e934bfe1023cab4ca5948f8d74c23a67d9535c28d2584 -$(package)_git_commit=9be18569b8abcda1245c3912877075259599c0f1 +$(package)_download_file=$(package)-$($(package)_git_commit).tar.gz +$(package)_sha256_hash=47478adc2ae88c448dc736d59dfe007de6478e41e88d2d4d2ff4135a17ee6f90 +$(package)_git_commit=3854b20c25e8bc567aab2b558dec84d45f4a3e73 $(package)_dependencies=libgmp libsodium ifeq ($(build_os),darwin) +define $(package)_set_vars + $(package)_build_env=CC="$($(package)_cc)" CXX="$($(package)_cxx)" + $(package)_build_env+=CXXFLAGS="$($(package)_cxxflags) -DBINARY_OUTPUT -DSTATICLIB -DNO_PT_COMPRESSION=1 " +endef +define $(package)_build_cmds + $(MAKE) lib DEPINST=$(host_prefix) CURVE=ALT_BN128 MULTICORE=1 NO_PROCPS=1 NO_GTEST=1 NO_DOCS=1 STATIC=1 NO_SUPERCOP=1 FEATUREFLAGS=-DMONTGOMERY_OUTPUT OPTFLAGS="-O2 -march=x86-64" +endef +else ifeq ($(host_os),mingw32) define $(package)_build_cmds -CC=gcc-5 CXX=g++-5 CXXFLAGS="-arch x86_64 -DBINARY_OUTPUT -DNO_PT_COMPRESSION=1" $(MAKE) lib DEPINST=$(host_prefix) CURVE=ALT_BN128 MULTICORE=0 NO_PROCPS=1 NO_GTEST=1 NO_DOCS=1 STATIC=1 NO_SUPERCOP=1 FEATUREFLAGS=-DMONTGOMERY_OUTPUT + CXX="x86_64-w64-mingw32-g++-posix" CXXFLAGS="-DBINARY_OUTPUT -DPTW32_STATIC_LIB -DSTATICLIB -DNO_PT_COMPRESSION=1 -fopenmp" $(MAKE) lib DEPINST=$(host_prefix) CURVE=ALT_BN128 MULTICORE=1 NO_PROCPS=1 NO_GTEST=1 NO_DOCS=1 STATIC=1 NO_SUPERCOP=1 FEATUREFLAGS=-DMONTGOMERY_OUTPUT OPTFLAGS="-O2 -march=x86-64" endef else define $(package)_build_cmds - CXXFLAGS="-fPIC -DBINARY_OUTPUT -DNO_PT_COMPRESSION=1" $(MAKE) lib DEPINST=$(host_prefix) CURVE=ALT_BN128 MULTICORE=1 NO_PROCPS=1 NO_GTEST=1 NO_DOCS=1 STATIC=1 NO_SUPERCOP=1 FEATUREFLAGS=-DMONTGOMERY_OUTPUT OPTFLAGS="-O2 -march=x86-64" + CXXFLAGS="-fPIC -DBINARY_OUTPUT -DNO_PT_COMPRESSION=1" $(MAKE) lib DEPINST=$(host_prefix) CURVE=ALT_BN128 MULTICORE=1 NO_PROCPS=1 NO_GTEST=1 NO_DOCS=1 STATIC=1 NO_SUPERCOP=1 FEATUREFLAGS=-DMONTGOMERY_OUTPUT OPTFLAGS="-O2 -march=x86-64" endef - - - - endif - - define $(package)_stage_cmds $(MAKE) install STATIC=1 DEPINST=$(host_prefix) PREFIX=$($(package)_staging_dir)$(host_prefix) CURVE=ALT_BN128 NO_SUPERCOP=1 endef diff --git a/depends/packages/libsodium.mk b/depends/packages/libsodium.mk index d7717bbfc7d..371225014f7 100644 --- a/depends/packages/libsodium.mk +++ b/depends/packages/libsodium.mk @@ -1,6 +1,6 @@ package=libsodium $(package)_version=1.0.11 -$(package)_download_path=https://download.libsodium.org/libsodium/releases/ +$(package)_download_path=https://supernetorg.bintray.com/misc $(package)_file_name=libsodium-1.0.11.tar.gz $(package)_sha256_hash=a14549db3c49f6ae2170cbbf4664bd48ace50681045e8dbea7c8d9fb96f9c765 $(package)_dependencies= diff --git a/depends/packages/packages.mk b/depends/packages/packages.mk index 6fa8c0589ba..b1951431af6 100644 --- a/depends/packages/packages.mk +++ b/depends/packages/packages.mk @@ -1,10 +1,10 @@ rust_packages := rust librustzcash zcash_packages := libsnark libgmp libsodium -ifeq ($(build_os),darwin) -packages := boost openssl libevent zeromq $(zcash_packages) +ifeq ($(host_os),linux) + packages := boost openssl libevent zeromq $(zcash_packages) googletest googlemock else -packages := boost openssl libevent zeromq $(zcash_packages) googletest googlemock + packages := boost openssl libevent zeromq $(zcash_packages) googletest googlemock libcurl endif diff --git a/depends/packages/rust.mk b/depends/packages/rust.mk index cdefbdbee8c..2e3f0b204d8 100644 --- a/depends/packages/rust.mk +++ b/depends/packages/rust.mk @@ -1,8 +1,16 @@ package=rust $(package)_version=1.16.0 $(package)_download_path=https://static.rust-lang.org/dist +ifeq ($(build_os),darwin) +$(package)_file_name=rust-$($(package)_version)-x86_64-apple-darwin.tar.gz +$(package)_sha256_hash=2d08259ee038d3a2c77a93f1a31fc59e7a1d6d1bbfcba3dba3c8213b2e5d1926 +else ifeq ($(host_os),mingw32) +$(package)_file_name=rust-$($(package)_version)-i686-unknown-linux-gnu.tar.gz +$(package)_sha256_hash=b5859161ebb182d3b75fa14a5741e5de87b088146fb0ef4a30f3b2439c6179c5 +else $(package)_file_name=rust-$($(package)_version)-x86_64-unknown-linux-gnu.tar.gz $(package)_sha256_hash=48621912c242753ba37cad5145df375eeba41c81079df46f93ffb4896542e8fd +endif define $(package)_stage_cmds ./install.sh --destdir=$($(package)_staging_dir) --prefix=$(host_prefix)/native --disable-ldconfig diff --git a/depends/packages/zeromq.mk b/depends/packages/zeromq.mk index 4b335c54cdc..8d324a3f4fa 100644 --- a/depends/packages/zeromq.mk +++ b/depends/packages/zeromq.mk @@ -1,3 +1,18 @@ +ifeq ($(host_os),mingw32) +$(package)_version=4.2.2-1 +$(package)_download_path=https://github.com/ca333/libzmq/archive/ +$(package)_download_file=v$($(package)_version).tar.gz +$(package)_file_name=libzmq-$($(package)_version).tar.gz +$(package)_sha256_hash=0e225b85ce11be23bf7eb7d3f25c6686728bf30d5c31f61c12d37bb646c69962 + +define $(package)_set_vars + $(package)_build_env+= + $(package)_config_opts=--enable-shared=false --enable-static --host=x86_64-w64-mingw32 + $(package)_config_opts_mingw32=--enable-shared=false --enable-static --host=x86_64-w64-mingw32 + $(package)_cflags=-Wno-error -Wall -Wno-pedantic-ms-format -DLIBCZMQ_EXPORTS -DZMQ_DEFINED_STDINT -lws2_32 -liphlpapi -lrpcrt4 + $(package)_conf_tool=./configure +endef +else package=zeromq $(package)_version=4.2.1 $(package)_download_path=https://github.com/zeromq/libzmq/releases/download/v$($(package)_version)/ @@ -8,10 +23,20 @@ define $(package)_set_vars $(package)_config_opts=--without-documentation --disable-shared --disable-curve $(package)_config_opts_linux=--with-pic endef +endif +ifeq ($(host_os),mingw32) +define $(package)_preprocess_cmds + cd $($(package)_build_subdir); ./autogen.sh +endef +define $(package)_config_cmds + $($(package)_conf_tool) $($(package)_config_opts) CFLAGS="-Wno-error -Wall -Wno-pedantic-ms-format -DLIBCZMQ_EXPORTS -DZMQ_DEFINED_STDINT -lws2_32 -liphlpapi -lrpcrt4" +endef +else define $(package)_config_cmds $($(package)_autoconf) endef +endif define $(package)_build_cmds $(MAKE) src/libzmq.la diff --git a/makeRelease.sh b/makeRelease.sh new file mode 100755 index 00000000000..5d68f0b0384 --- /dev/null +++ b/makeRelease.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +binaries=("komodo-cli" "komodod") + +for binary in "${binaries[@]}"; +do + # find the dylibs to copy for komodod + DYLIBS=`otool -L src/$binary | grep "/usr/local" | awk -F' ' '{ print $1 }'` + echo "copying $DYLIBS to $src" + # copy the dylibs to the srcdir + for dylib in $DYLIBS; do cp -rf $dylib src/; done + + # modify komodod to point to dylibs + echo "modifying $binary to use local libraries" + for dylib in $DYLIBS; do install_name_tool -change $dylib @executable_path/`basename $dylib` src/$binary; done; + chmod +x src/$binary +done diff --git a/src/Makefile.am b/src/Makefile.am index ce0bd6cd380..72805281aec 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -23,7 +23,15 @@ BITCOIN_INCLUDES=-I$(builddir) -I$(builddir)/obj $(BOOST_CPPFLAGS) $(LEVELDB_CPP BITCOIN_INCLUDES += -I$(srcdir)/secp256k1/include BITCOIN_INCLUDES += -I$(srcdir)/univalue/include +if TARGET_WINDOWS +LIBBITCOIN_SERVER=libbitcoin_server.a -lcurl +endif +if TARGET_DARWIN LIBBITCOIN_SERVER=libbitcoin_server.a -lcurl +else +LIBBITCOIN_SERVER=libbitcoin_server.a +endif + LIBBITCOIN_WALLET=libbitcoin_wallet.a LIBBITCOIN_COMMON=libbitcoin_common.a LIBBITCOIN_CLI=libbitcoin_cli.a diff --git a/src/assetchains b/src/assetchains index 4fcc83fac6b..6b80a62eae9 100755 --- a/src/assetchains +++ b/src/assetchains @@ -36,6 +36,7 @@ komodo_asset BET 999999 komodo_asset CRYPTO 999999 komodo_asset HODL 9999999 komodo_asset SHARK 1401 +komodo_asset MSHARK 1400000 komodo_asset BOTS 999999 komodo_asset MGW 999999 #komodo_asset MVP 1000000 @@ -44,6 +45,7 @@ komodo_asset WLC 210000000 komodo_asset KV 1000000 komodo_asset CEAL 366666666 komodo_asset MESH 1000007 +komodo_asset MNZ 257142858 komodo_asset USD komodo_asset EUR diff --git a/src/assetchains.old b/src/assetchains.old index 13cb7f29ba1..601eda111ac 100755 --- a/src/assetchains.old +++ b/src/assetchains.old @@ -1,6 +1,6 @@ #!/bin/bash set -x -delay=10 +delay=60 source pubkey.txt echo $pubkey @@ -13,6 +13,7 @@ echo $pubkey ./komodod -pubkey=$pubkey -ac_name=CRYPTO -ac_supply=999999 -addnode=78.47.196.146 $1 & ./komodod -pubkey=$pubkey -ac_name=HODL -ac_supply=9999999 -addnode=78.47.196.146 $1 & ./komodod -pubkey=$pubkey -ac_name=SHARK -ac_supply=1401 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=MSHARK -ac_supply=1400000 -addnode=78.47.196.146 $1 & ./komodod -pubkey=$pubkey -ac_name=BOTS -ac_supply=999999 -addnode=78.47.196.146 $1 & ./komodod -pubkey=$pubkey -ac_name=MGW -ac_supply=999999 -addnode=78.47.196.146 $1 & #./komodod -pubkey=$pubkey -ac_name=MVP -ac_supply=1000000 -addnode=78.47.196.146 $1 & @@ -21,6 +22,7 @@ echo $pubkey ./komodod -pubkey=$pubkey -ac_name=KV -ac_supply=1000000 -addnode=78.47.196.146 $1 & ./komodod -pubkey=$pubkey -ac_name=CEAL -ac_supply=366666666 -addnode=78.47.196.146 $1 & ./komodod -pubkey=$pubkey -ac_name=MESH -ac_supply=1000007 -addnode=78.47.196.146 $1 & +./komodod -pubkey=$pubkey -ac_name=MNZ -ac_supply=257142858 -addnode=51.15.138.138 $1 & sleep $delay ./komodod -pubkey=$pubkey -ac_name=USD -addnode=78.47.196.146 $1 & diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index ed36fa32dcc..cac650a0049 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -72,7 +72,7 @@ class CConnectionFailed : public std::runtime_error #include "komodo_globals.h" #include "komodo_utils.h" -#include "cJSON.c" +#include "komodo_cJSON.c" #include "komodo_notary.h" void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint32_t KMDtimestamp,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout) @@ -86,7 +86,7 @@ static bool AppInitRPC(int argc, char* argv[]) // Parameters // ParseParameters(argc, argv); - komodo_args(); + komodo_args(argv[0]); if (argc<2 || mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help") || mapArgs.count("-version")) { std::string strUsage = _("Komodo RPC client version") + " " + FormatFullVersion() + "\n" + PrivacyInfo(); if (!mapArgs.count("-version")) { @@ -196,7 +196,7 @@ UniValue CallRPC(const string& strMethod, const UniValue& params) { std::string host = GetArg("-rpcconnect", "127.0.0.1"); int port = GetArg("-rpcport", BaseParams().RPCPort()); - + BITCOIND_PORT = port; // Obtain event base raii_event_base base = obtain_event_base(); diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 7679fa961fa..bf4934da488 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -20,6 +20,11 @@ #include +#ifdef _WIN32 +#define frpintf(...) +#define printf(...) +#endif + /* Introduction text for doxygen: */ /*! \mainpage Developer documentation @@ -37,7 +42,8 @@ */ static bool fDaemon; -extern char ASSETCHAINS_SYMBOL[16]; +#define KOMODO_ASSETCHAIN_MAXLEN 65 +extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; void komodo_passport_iteration(); void WaitForShutdown(boost::thread_group* threadGroup) @@ -106,14 +112,18 @@ bool AppInit(int argc, char* argv[]) try { - void komodo_args(); - komodo_args(); - fprintf(stderr,"call komodo_args NOTARY_PUBKEY.(%s)\n",NOTARY_PUBKEY.c_str()); + void komodo_args(char *argv0); + komodo_args(argv[0]); + fprintf(stderr,"call komodo_args.(%s) NOTARY_PUBKEY.(%s)\n",argv[0],NOTARY_PUBKEY.c_str()); while ( ASSETCHAIN_INIT == 0 ) { //if ( komodo_is_issuer() != 0 ) // komodo_passport_iteration(); + #ifdef _WIN32 + boost::this_thread::sleep_for(boost::chrono::seconds(1)); + #else sleep(1); + #endif } printf("initialized %s\n",ASSETCHAINS_SYMBOL); if (!boost::filesystem::is_directory(GetDataDir(false))) @@ -164,7 +174,7 @@ bool AppInit(int argc, char* argv[]) exit(1); } -#ifndef WIN32 +#ifndef _WIN32 fDaemon = GetBoolArg("-daemon", false); if (fDaemon) { diff --git a/src/cJSON.c b/src/cJSON.c old mode 100755 new mode 100644 index 82b78a40346..b67ca698df2 --- a/src/cJSON.c +++ b/src/cJSON.c @@ -1,1136 +1,2699 @@ - /* - Copyright (c) 2009 Dave Gamble - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - */ + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ /* cJSON */ /* JSON parser in C. */ + +#ifdef __GNUC__ +#pragma GCC visibility push(default) +#endif + +#include +#include #include +#include +#include +#include +#include +#include + +#ifdef __GNUC__ +#pragma GCC visibility pop +#endif #include "cJSON.h" -#ifndef DBL_EPSILON -#define DBL_EPSILON 2.2204460492503131E-16 +/* define our own boolean type */ +//#define true ((cJSON_bool)1) +//#define false ((cJSON_bool)0) + +typedef struct { + const unsigned char *json; + size_t position; +} cJSON_error; +static cJSON_error global_error = { NULL, 0 }; + +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) +{ + return (const char*) (global_error.json + global_error.position); +} + +/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 5) || (CJSON_VERSION_PATCH != 9) + #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. #endif -static const char *ep; +CJSON_PUBLIC(const char*) cJSON_Version(void) +{ + static char version[15]; + sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + + return version; +} + +/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ +static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) +{ + if ((string1 == NULL) || (string2 == NULL)) + { + return 1; + } + + if (string1 == string2) + { + return 0; + } + + for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) + { + if (*string1 == '\0') + { + return 0; + } + } + + return tolower(*string1) - tolower(*string2); +} + +typedef struct internal_hooks +{ + void *(*allocate)(size_t size); + void (*deallocate)(void *pointer); + void *(*reallocate)(void *pointer, size_t size); +} internal_hooks; + +static internal_hooks global_hooks = { malloc, free, realloc }; + +static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) +{ + size_t length = 0; + unsigned char *copy = NULL; + + if (string == NULL) + { + return NULL; + } + + length = strlen((const char*)string) + sizeof(""); + if (!(copy = (unsigned char*)hooks->allocate(length))) + { + return NULL; + } + memcpy(copy, string, length); + + return copy; +} + +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) +{ + if (hooks == NULL) + { + /* Reset hooks */ + global_hooks.allocate = malloc; + global_hooks.deallocate = free; + global_hooks.reallocate = realloc; + return; + } + + global_hooks.allocate = malloc; + if (hooks->malloc_fn != NULL) + { + global_hooks.allocate = hooks->malloc_fn; + } + + global_hooks.deallocate = free; + if (hooks->free_fn != NULL) + { + global_hooks.deallocate = hooks->free_fn; + } + + /* use realloc only if both free and malloc are used */ + global_hooks.reallocate = NULL; + if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) + { + global_hooks.reallocate = realloc; + } +} + +/* Internal constructor. */ +static cJSON *cJSON_New_Item(const internal_hooks * const hooks) +{ + cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); + if (node) + { + memset(node, '\0', sizeof(cJSON)); + } + + return node; +} + +/* Delete a cJSON structure. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) +{ + cJSON *next = NULL; + while (item != NULL) + { + next = item->next; + if (!(item->type & cJSON_IsReference) && (item->child != NULL)) + { + cJSON_Delete(item->child); + } + if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) + { + global_hooks.deallocate(item->valuestring); + } + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + global_hooks.deallocate(item->string); + } + global_hooks.deallocate(item); + item = next; + } +} + +/* get the decimal point character of the current locale */ +static unsigned char get_decimal_point(void) +{ + struct lconv *lconv = localeconv(); + return (unsigned char) lconv->decimal_point[0]; +} + +typedef struct +{ + const unsigned char *content; + size_t length; + size_t offset; + size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ + internal_hooks hooks; +} parse_buffer; + +/* check if the given size is left to read in a given parse buffer (starting with 1) */ +#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) +#define cannot_read(buffer, size) (!can_read(buffer, size)) +/* check if the buffer can be accessed at the given index (starting with 0) */ +#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) +#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) +/* get a pointer to the buffer at the position */ +#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) + +/* Parse the input text to generate a number, and populate the result into item. */ +static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) +{ + double number = 0; + unsigned char *after_end = NULL; + unsigned char number_c_string[64]; + unsigned char decimal_point = get_decimal_point(); + size_t i = 0; + + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; + } + + /* copy the number into a temporary buffer and replace '.' with the decimal point + * of the current locale (for strtod) + * This also takes care of '\0' not necessarily being available for marking the end of the input */ + for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) + { + switch (buffer_at_offset(input_buffer)[i]) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + case '-': + case 'e': + case 'E': + number_c_string[i] = buffer_at_offset(input_buffer)[i]; + break; + + case '.': + number_c_string[i] = decimal_point; + break; + + default: + goto loop_end; + } + } +loop_end: + number_c_string[i] = '\0'; + + number = strtod((const char*)number_c_string, (char**)&after_end); + if (number_c_string == after_end) + { + return false; /* parse_error */ + } + + item->valuedouble = number; + + /* use saturation in case of overflow */ + if (number >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (number <= INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)number; + } + + item->type = cJSON_Number; + + input_buffer->offset += (size_t)(after_end - number_c_string); + return true; +} + +/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) +{ + if (number >= INT_MAX) + { + object->valueint = INT_MAX; + } + else if (number <= INT_MIN) + { + object->valueint = INT_MIN; + } + else + { + object->valueint = (int)number; + } + + return object->valuedouble = number; +} + +typedef struct +{ + unsigned char *buffer; + size_t length; + size_t offset; + size_t depth; /* current nesting depth (for formatted printing) */ + cJSON_bool noalloc; + cJSON_bool format; /* is this print a formatted print */ + internal_hooks hooks; +} printbuffer; + +/* realloc printbuffer if necessary to have at least "needed" bytes more */ +static unsigned char* ensure(printbuffer * const p, size_t needed) +{ + unsigned char *newbuffer = NULL; + size_t newsize = 0; + + if ((p == NULL) || (p->buffer == NULL)) + { + return NULL; + } + + if ((p->length > 0) && (p->offset >= p->length)) + { + /* make sure that offset is valid */ + return NULL; + } + + if (needed > INT_MAX) + { + /* sizes bigger than INT_MAX are currently not supported */ + return NULL; + } + + needed += p->offset + 1; + if (needed <= p->length) + { + return p->buffer + p->offset; + } + + if (p->noalloc) { + return NULL; + } + + /* calculate new buffer size */ + if (needed > (INT_MAX / 2)) + { + /* overflow of int, use INT_MAX if possible */ + if (needed <= INT_MAX) + { + newsize = INT_MAX; + } + else + { + return NULL; + } + } + else + { + newsize = needed * 2; + } + + if (p->hooks.reallocate != NULL) + { + /* reallocate with realloc if available */ + newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); + if (newbuffer == NULL) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + } + else + { + /* otherwise reallocate manually */ + newbuffer = (unsigned char*)p->hooks.allocate(newsize); + if (!newbuffer) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + if (newbuffer) + { + memcpy(newbuffer, p->buffer, p->offset + 1); + } + p->hooks.deallocate(p->buffer); + } + p->length = newsize; + p->buffer = newbuffer; + + return newbuffer + p->offset; +} + +/* calculate the new length of the string in a printbuffer and update the offset */ +static void update_offset(printbuffer * const buffer) +{ + const unsigned char *buffer_pointer = NULL; + if ((buffer == NULL) || (buffer->buffer == NULL)) + { + return; + } + buffer_pointer = buffer->buffer + buffer->offset; + + buffer->offset += strlen((const char*)buffer_pointer); +} + +/* Render the number nicely from the given item into a string. */ +static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + double d = item->valuedouble; + int length = 0; + size_t i = 0; + unsigned char number_buffer[26]; /* temporary buffer to print the number into */ + unsigned char decimal_point = get_decimal_point(); + double test; + + if (output_buffer == NULL) + { + return false; + } + + /* This checks for NaN and Infinity */ + if ((d * 0) != 0) + { + length = sprintf((char*)number_buffer, "null"); + } + else + { + /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ + length = sprintf((char*)number_buffer, "%1.15g", d); + + /* Check whether the original double can be recovered */ + if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || ((double)test != d)) + { + /* If not, print with 17 decimal places of precision */ + length = sprintf((char*)number_buffer, "%1.17g", d); + } + } + + /* sprintf failed or buffer overrun occured */ + if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) + { + return false; + } + + /* reserve appropriate space in the output */ + output_pointer = ensure(output_buffer, (size_t)length); + if (output_pointer == NULL) + { + return false; + } + + /* copy the printed number to the output and replace locale + * dependent decimal point with '.' */ + for (i = 0; i < ((size_t)length); i++) + { + if (number_buffer[i] == decimal_point) + { + output_pointer[i] = '.'; + continue; + } + + output_pointer[i] = number_buffer[i]; + } + output_pointer[i] = '\0'; + + output_buffer->offset += (size_t)length; + + return true; +} + +/* parse 4 digit hexadecimal number */ +static unsigned parse_hex4(const unsigned char * const input) +{ + unsigned int h = 0; + size_t i = 0; + + for (i = 0; i < 4; i++) + { + /* parse digit */ + if ((input[i] >= '0') && (input[i] <= '9')) + { + h += (unsigned int) input[i] - '0'; + } + else if ((input[i] >= 'A') && (input[i] <= 'F')) + { + h += (unsigned int) 10 + input[i] - 'A'; + } + else if ((input[i] >= 'a') && (input[i] <= 'f')) + { + h += (unsigned int) 10 + input[i] - 'a'; + } + else /* invalid */ + { + return 0; + } + + if (i < 3) + { + /* shift left to make place for the next nibble */ + h = h << 4; + } + } + + return h; +} + +/* converts a UTF-16 literal to UTF-8 + * A literal can be one or two sequences of the form \uXXXX */ +static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) +{ + long unsigned int codepoint = 0; + unsigned int first_code = 0; + const unsigned char *first_sequence = input_pointer; + unsigned char utf8_length = 0; + unsigned char utf8_position = 0; + unsigned char sequence_length = 0; + unsigned char first_byte_mark = 0; + + if ((input_end - first_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + /* get the first utf16 sequence */ + first_code = parse_hex4(first_sequence + 2); + + /* check that the code is valid */ + if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) + { + goto fail; + } + + /* UTF16 surrogate pair */ + if ((first_code >= 0xD800) && (first_code <= 0xDBFF)) + { + const unsigned char *second_sequence = first_sequence + 6; + unsigned int second_code = 0; + sequence_length = 12; /* \uXXXX\uXXXX */ + + if ((input_end - second_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u')) + { + /* missing second half of the surrogate pair */ + goto fail; + } + + /* get the second utf16 sequence */ + second_code = parse_hex4(second_sequence + 2); + /* check that the code is valid */ + if ((second_code < 0xDC00) || (second_code > 0xDFFF)) + { + /* invalid second half of the surrogate pair */ + goto fail; + } + + + /* calculate the unicode codepoint from the surrogate pair */ + codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); + } + else + { + sequence_length = 6; /* \uXXXX */ + codepoint = first_code; + } + + /* encode as UTF-8 + * takes at maximum 4 bytes to encode: + * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ + if (codepoint < 0x80) + { + /* normal ascii, encoding 0xxxxxxx */ + utf8_length = 1; + } + else if (codepoint < 0x800) + { + /* two bytes, encoding 110xxxxx 10xxxxxx */ + utf8_length = 2; + first_byte_mark = 0xC0; /* 11000000 */ + } + else if (codepoint < 0x10000) + { + /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ + utf8_length = 3; + first_byte_mark = 0xE0; /* 11100000 */ + } + else if (codepoint <= 0x10FFFF) + { + /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ + utf8_length = 4; + first_byte_mark = 0xF0; /* 11110000 */ + } + else + { + /* invalid unicode codepoint */ + goto fail; + } + + /* encode as utf8 */ + for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) + { + /* 10xxxxxx */ + (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); + codepoint >>= 6; + } + /* encode first byte */ + if (utf8_length > 1) + { + (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF); + } + else + { + (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); + } + + *output_pointer += utf8_length; + + return sequence_length; + +fail: + return 0; +} + +/* Parse the input text into an unescaped cinput, and populate item. */ +static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) +{ + const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; + const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; + unsigned char *output_pointer = NULL; + unsigned char *output = NULL; + + /* not a string */ + if (buffer_at_offset(input_buffer)[0] != '\"') + { + goto fail; + } + + { + /* calculate approximate size of the output (overestimate) */ + size_t allocation_length = 0; + size_t skipped_bytes = 0; + while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) + { + /* is escape sequence */ + if (input_end[0] == '\\') + { + if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) + { + /* prevent buffer overflow when last input character is a backslash */ + goto fail; + } + skipped_bytes++; + input_end++; + } + input_end++; + } + if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) + { + goto fail; /* string ended unexpectedly */ + } + + /* This is at most how much we need for the output */ + allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; + output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); + if (output == NULL) + { + goto fail; /* allocation failure */ + } + } + + output_pointer = output; + /* loop through the string literal */ + while (input_pointer < input_end) + { + if (*input_pointer != '\\') + { + *output_pointer++ = *input_pointer++; + } + /* escape sequence */ + else + { + unsigned char sequence_length = 2; + if ((input_end - input_pointer) < 1) + { + goto fail; + } + + switch (input_pointer[1]) + { + case 'b': + *output_pointer++ = '\b'; + break; + case 'f': + *output_pointer++ = '\f'; + break; + case 'n': + *output_pointer++ = '\n'; + break; + case 'r': + *output_pointer++ = '\r'; + break; + case 't': + *output_pointer++ = '\t'; + break; + case '\"': + case '\\': + case '/': + *output_pointer++ = input_pointer[1]; + break; + + /* UTF-16 literal */ + case 'u': + sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); + if (sequence_length == 0) + { + /* failed to convert UTF16-literal to UTF-8 */ + goto fail; + } + break; + + default: + goto fail; + } + input_pointer += sequence_length; + } + } + + /* zero terminate the output */ + *output_pointer = '\0'; + + item->type = cJSON_String; + item->valuestring = (char*)output; + + input_buffer->offset = (size_t) (input_end - input_buffer->content); + input_buffer->offset++; + + return true; + +fail: + if (output != NULL) + { + input_buffer->hooks.deallocate(output); + } + + if (input_pointer != NULL) + { + input_buffer->offset = (size_t)(input_pointer - input_buffer->content); + } + + return false; +} + +/* Render the cstring provided to an escaped version that can be printed. */ +static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) +{ + const unsigned char *input_pointer = NULL; + unsigned char *output = NULL; + unsigned char *output_pointer = NULL; + size_t output_length = 0; + /* numbers of additional characters needed for escaping */ + size_t escape_characters = 0; + + if (output_buffer == NULL) + { + return false; + } + + /* empty string */ + if (input == NULL) + { + output = ensure(output_buffer, sizeof("\"\"")); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "\"\""); + + return true; + } + + /* set "flag" to 1 if something needs to be escaped */ + for (input_pointer = input; *input_pointer; input_pointer++) + { + switch (*input_pointer) + { + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + /* one character escape sequence */ + escape_characters++; + break; + default: + if (*input_pointer < 32) + { + /* UTF-16 escape sequence uXXXX */ + escape_characters += 5; + } + break; + } + } + output_length = (size_t)(input_pointer - input) + escape_characters; + + output = ensure(output_buffer, output_length + sizeof("\"\"")); + if (output == NULL) + { + return false; + } + + /* no characters have to be escaped */ + if (escape_characters == 0) + { + output[0] = '\"'; + memcpy(output + 1, input, output_length); + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; + } + + output[0] = '\"'; + output_pointer = output + 1; + /* copy the string */ + for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) + { + if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) + { + /* normal character, copy */ + *output_pointer = *input_pointer; + } + else + { + /* character needs to be escaped */ + *output_pointer++ = '\\'; + switch (*input_pointer) + { + case '\\': + *output_pointer = '\\'; + break; + case '\"': + *output_pointer = '\"'; + break; + case '\b': + *output_pointer = 'b'; + break; + case '\f': + *output_pointer = 'f'; + break; + case '\n': + *output_pointer = 'n'; + break; + case '\r': + *output_pointer = 'r'; + break; + case '\t': + *output_pointer = 't'; + break; + default: + /* escape and print as unicode codepoint */ + sprintf((char*)output_pointer, "u%04x", *input_pointer); + output_pointer += 4; + break; + } + } + } + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; +} + +/* Invoke print_string_ptr (which is useful) on an item. */ +static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) +{ + return print_string_ptr((unsigned char*)item->valuestring, p); +} + +/* Predeclare these prototypes. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer); + +/* Utility to jump whitespace and cr/lf */ +static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL)) + { + return NULL; + } + + while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) + { + buffer->offset++; + } + + if (buffer->offset == buffer->length) + { + buffer->offset--; + } + + return buffer; +} + +/* Parse an object - create a new root, and populate. */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + cJSON *item = NULL; + + /* reset error position */ + global_error.json = NULL; + global_error.position = 0; + + if (value == NULL) + { + goto fail; + } + + buffer.content = (const unsigned char*)value; + buffer.length = strlen((const char*)value) + sizeof(""); + buffer.offset = 0; + buffer.hooks = global_hooks; + + item = cJSON_New_Item(&global_hooks); + if (item == NULL) /* memory fail */ + { + goto fail; + } + + if (!parse_value(item, buffer_skip_whitespace(&buffer))) + { + /* parse failure. ep is set. */ + goto fail; + } + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) + { + buffer_skip_whitespace(&buffer); + if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') + { + goto fail; + } + } + if (return_parse_end) + { + *return_parse_end = (const char*)buffer_at_offset(&buffer); + } + + return item; + +fail: + if (item != NULL) + { + cJSON_Delete(item); + } + + if (value != NULL) + { + cJSON_error local_error; + local_error.json = (const unsigned char*)value; + local_error.position = 0; + + if (buffer.offset < buffer.length) + { + local_error.position = buffer.offset; + } + else if (buffer.length > 0) + { + local_error.position = buffer.length - 1; + } + + if (return_parse_end != NULL) + { + *return_parse_end = (const char*)local_error.json + local_error.position; + } + + global_error = local_error; + } + + return NULL; +} + +/* Default options for cJSON_Parse */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) +{ + return cJSON_ParseWithOpts(value, 0, 0); +} + +#define cjson_min(a, b) ((a < b) ? a : b) + +static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks) +{ + printbuffer buffer[1]; + unsigned char *printed = NULL; + + memset(buffer, 0, sizeof(buffer)); + + /* create buffer */ + buffer->buffer = (unsigned char*) hooks->allocate(256); + buffer->format = format; + buffer->hooks = *hooks; + if (buffer->buffer == NULL) + { + goto fail; + } + + /* print the value */ + if (!print_value(item, buffer)) + { + goto fail; + } + update_offset(buffer); + + /* check if reallocate is available */ + if (hooks->reallocate != NULL) + { + printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->length); + buffer->buffer = NULL; + if (printed == NULL) { + goto fail; + } + } + else /* otherwise copy the JSON over to a new buffer */ + { + printed = (unsigned char*) hooks->allocate(buffer->offset + 1); + if (printed == NULL) + { + goto fail; + } + memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); + printed[buffer->offset] = '\0'; /* just to be sure */ + + /* free the buffer */ + hooks->deallocate(buffer->buffer); + } + + return printed; + +fail: + if (buffer->buffer != NULL) + { + hooks->deallocate(buffer->buffer); + } + + if (printed != NULL) + { + hooks->deallocate(printed); + } + + return NULL; +} + +/* Render a cJSON item/entity/structure to text. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) +{ + return (char*)print(item, true, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) +{ + return (char*)print(item, false, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if (prebuffer < 0) + { + return NULL; + } + + p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); + if (!p.buffer) + { + return NULL; + } + + p.length = (size_t)prebuffer; + p.offset = 0; + p.noalloc = false; + p.format = fmt; + p.hooks = global_hooks; + + if (!print_value(item, &p)) + { + global_hooks.deallocate(p.buffer); + return NULL; + } + + return (char*)p.buffer; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if ((len < 0) || (buf == NULL)) + { + return false; + } + + p.buffer = (unsigned char*)buf; + p.length = (size_t)len; + p.offset = 0; + p.noalloc = true; + p.format = fmt; + p.hooks = global_hooks; + + return print_value(item, &p); +} + +/* Parser core - when encountering text, process appropriately. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) +{ + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; /* no input */ + } + + /* parse the different types of values */ + /* null */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0)) + { + item->type = cJSON_NULL; + input_buffer->offset += 4; + return true; + } + /* false */ + if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0)) + { + item->type = cJSON_False; + input_buffer->offset += 5; + return true; + } + /* true */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0)) + { + item->type = cJSON_True; + item->valueint = 1; + input_buffer->offset += 4; + return true; + } + /* string */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) + { + return parse_string(item, input_buffer); + } + /* number */ + if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) + { + return parse_number(item, input_buffer); + } + /* array */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) + { + return parse_array(item, input_buffer); + } + /* object */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) + { + return parse_object(item, input_buffer); + } + + + return false; +} + +/* Render a value to text. */ +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output = NULL; + + if ((item == NULL) || (output_buffer == NULL)) + { + return false; + } + + switch ((item->type) & 0xFF) + { + case cJSON_NULL: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "null"); + return true; + + case cJSON_False: + output = ensure(output_buffer, 6); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "false"); + return true; + + case cJSON_True: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "true"); + return true; + + case cJSON_Number: + return print_number(item, output_buffer); + + case cJSON_Raw: + { + size_t raw_length = 0; + if (item->valuestring == NULL) + { + if (!output_buffer->noalloc) + { + output_buffer->hooks.deallocate(output_buffer->buffer); + } + return false; + } + + raw_length = strlen(item->valuestring) + sizeof(""); + output = ensure(output_buffer, raw_length); + if (output == NULL) + { + return false; + } + memcpy(output, item->valuestring, raw_length); + return true; + } + + case cJSON_String: + return print_string(item, output_buffer); + + case cJSON_Array: + return print_array(item, output_buffer); + + case cJSON_Object: + return print_object(item, output_buffer); + + default: + return false; + } +} + +/* Build an array from input text. */ +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* head of the linked list */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (buffer_at_offset(input_buffer)[0] != '[') + { + /* not an array */ + goto fail; + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) + { + /* empty array */ + goto success; + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse next value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') + { + goto fail; /* expected end of array */ + } + +success: + input_buffer->depth--; + + item->type = cJSON_Array; + item->child = head; + + input_buffer->offset++; + + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an array to text */ +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_element = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output array. */ + /* opening square bracket */ + output_pointer = ensure(output_buffer, 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer = '['; + output_buffer->offset++; + output_buffer->depth++; + + while (current_element != NULL) + { + if (!print_value(current_element, output_buffer)) + { + return false; + } + update_offset(output_buffer); + if (current_element->next) + { + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ','; + if(output_buffer->format) + { + *output_pointer++ = ' '; + } + *output_pointer = '\0'; + output_buffer->offset += length; + } + current_element = current_element->next; + } + + output_pointer = ensure(output_buffer, 2); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ']'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Build an object from the text. */ +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* linked list head */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) + { + goto fail; /* not an object */ + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) + { + goto success; /* empty object */ + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse the name of the child */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_string(current_item, input_buffer)) + { + goto fail; /* faile to parse name */ + } + buffer_skip_whitespace(input_buffer); + + /* swap valuestring and string, because we parsed the name */ + current_item->string = current_item->valuestring; + current_item->valuestring = NULL; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) + { + goto fail; /* invalid object */ + } + + /* parse the value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) + { + goto fail; /* expected end of object */ + } + +success: + input_buffer->depth--; + + item->type = cJSON_Object; + item->child = head; + + input_buffer->offset++; + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an object to text. */ +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_item = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output: */ + length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer++ = '{'; + output_buffer->depth++; + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + output_buffer->offset += length; + + while (current_item) + { + if (output_buffer->format) + { + size_t i; + output_pointer = ensure(output_buffer, output_buffer->depth); + if (output_pointer == NULL) + { + return false; + } + for (i = 0; i < output_buffer->depth; i++) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += output_buffer->depth; + } + + /* print key */ + if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ':'; + if (output_buffer->format) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += length; + + /* print value */ + if (!print_value(current_item, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + /* print comma if not last */ + length = (size_t) ((output_buffer->format ? 1 : 0) + (current_item->next ? 1 : 0)); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + if (current_item->next) + { + *output_pointer++ = ','; + } + + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + *output_pointer = '\0'; + output_buffer->offset += length; + + current_item = current_item->next; + } + + output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); + if (output_pointer == NULL) + { + return false; + } + if (output_buffer->format) + { + size_t i; + for (i = 0; i < (output_buffer->depth - 1); i++) + { + *output_pointer++ = '\t'; + } + } + *output_pointer++ = '}'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Get Array size/item / object item. */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) +{ + cJSON *child = NULL; + size_t size = 0; + + if (array == NULL) + { + return 0; + } + + child = array->child; + + while(child != NULL) + { + size++; + child = child->next; + } + + /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ + + return (int)size; +} + +static cJSON* get_array_item(const cJSON *array, size_t index) +{ + cJSON *current_child = NULL; + + if (array == NULL) + { + return NULL; + } + + current_child = array->child; + while ((current_child != NULL) && (index > 0)) + { + index--; + current_child = current_child->next; + } + + return current_child; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) +{ + if (index < 0) + { + return NULL; + } + + return get_array_item(array, (size_t)index); +} + +static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive) +{ + cJSON *current_element = NULL; + + if ((object == NULL) || (name == NULL)) + { + return NULL; + } + + current_element = object->child; + if (case_sensitive) + { + while ((current_element != NULL) && (strcmp(name, current_element->string) != 0)) + { + current_element = current_element->next; + } + } + else + { + while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) + { + current_element = current_element->next; + } + } + + return current_element; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, false); +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string) +{ + return cJSON_GetObjectItem(object, string) ? 1 : 0; +} + +/* Utility for array list handling. */ +static void suffix_object(cJSON *prev, cJSON *item) +{ + prev->next = item; + item->prev = prev; +} + +/* Utility for handling references. */ +static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks) +{ + cJSON *reference = NULL; + if (item == NULL) + { + return NULL; + } + + reference = cJSON_New_Item(hooks); + if (reference == NULL) + { + return NULL; + } + + memcpy(reference, item, sizeof(cJSON)); + reference->string = NULL; + reference->type |= cJSON_IsReference; + reference->next = reference->prev = NULL; + return reference; +} + +/* Add item to array/object. */ +CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item) +{ + cJSON *child = NULL; + + if ((item == NULL) || (array == NULL)) + { + return; + } + + child = array->child; + + if (child == NULL) + { + /* list is empty, start new one */ + array->child = item; + } + else + { + /* append to the end */ + while (child->next) + { + child = child->next; + } + suffix_object(child, item); + } +} + +CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) +{ + if (item == NULL) + { + return; + } + + /* call cJSON_AddItemToObjectCS for code reuse */ + cJSON_AddItemToObjectCS(object, (char*)cJSON_strdup((const unsigned char*)string, &global_hooks), item); + /* remove cJSON_StringIsConst flag */ + item->type &= ~cJSON_StringIsConst; +} + +#if defined (__clang__) || ((__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic push +#endif +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wcast-qual" +#endif + +/* Add an item to an object with constant string as key */ +CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) +{ + if ((item == NULL) || (string == NULL)) + { + return; + } + if (!(item->type & cJSON_StringIsConst) && item->string) + { + global_hooks.deallocate(item->string); + } + item->string = (char*)string; + item->type |= cJSON_StringIsConst; + cJSON_AddItemToArray(object, item); +} +#if defined (__clang__) || ((__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic pop +#endif + +CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) +{ + if (array == NULL) + { + return; + } + + cJSON_AddItemToArray(array, create_reference(item, &global_hooks)); +} + +CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) +{ + if ((object == NULL) || (string == NULL)) + { + return; + } + + cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks)); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) +{ + if ((parent == NULL) || (item == NULL)) + { + return NULL; + } + + if (item->prev != NULL) + { + /* not the first element */ + item->prev->next = item->next; + } + if (item->next != NULL) + { + /* not the last element */ + item->next->prev = item->prev; + } + + if (item == parent->child) + { + /* first element */ + parent->child = item->next; + } + /* make sure the detached item doesn't point anywhere anymore */ + item->prev = NULL; + item->next = NULL; + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) +{ + if (which < 0) + { + return NULL; + } + + return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) +{ + cJSON_Delete(cJSON_DetachItemFromArray(array, which)); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItem(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObject(object, string)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); +} + +/* Replace array/object items with new ones. */ +CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) +{ + cJSON *after_inserted = NULL; + + if (which < 0) + { + return; + } + + after_inserted = get_array_item(array, (size_t)which); + if (after_inserted == NULL) + { + cJSON_AddItemToArray(array, newitem); + return; + } + + newitem->next = after_inserted; + newitem->prev = after_inserted->prev; + after_inserted->prev = newitem; + if (after_inserted == array->child) + { + array->child = newitem; + } + else + { + newitem->prev->next = newitem; + } +} -long stripquotes(char *str) +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) { - long len,offset; - if ( str == 0 ) - return(0); - len = strlen(str); - if ( str[0] == '"' && str[len-1] == '"' ) - str[len-1] = 0, offset = 1; - else offset = 0; - return(offset); -} + if ((parent == NULL) || (replacement == NULL) || (item == NULL)) + { + return false; + } -const char *cJSON_GetErrorPtr(void) {return ep;} + if (replacement == item) + { + return true; + } -static int32_t cJSON_strcasecmp(const char *s1,const char *s2) -{ - if (!s1) return (s1==s2)?0:1;if (!s2) return 1; - for(; tolower((int32_t)(*s1)) == tolower((int32_t)(*s2)); ++s1, ++s2) if(*s1 == 0) return 0; - return tolower((int32_t)(*(const unsigned char *)s1)) - tolower((int32_t)(*(const unsigned char *)s2)); -} + replacement->next = item->next; + replacement->prev = item->prev; -static void *(*cJSON_malloc)(size_t sz) = malloc; -static void (*cJSON_free)(void *ptr) = free; + if (replacement->next != NULL) + { + replacement->next->prev = replacement; + } + if (replacement->prev != NULL) + { + replacement->prev->next = replacement; + } + if (parent->child == item) + { + parent->child = replacement; + } -static char* cJSON_strdup(const char* str) -{ - size_t len; - char* copy; - - len = strlen(str) + 1; - if (!(copy = (char*)cJSON_malloc(len+1))) return 0; - memcpy(copy,str,len); - return copy; + item->next = NULL; + item->prev = NULL; + cJSON_Delete(item); + + return true; } -void cJSON_InitHooks(cJSON_Hooks* hooks) +CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) { - if (!hooks) { /* Reset hooks */ - cJSON_malloc = malloc; - cJSON_free = free; + if (which < 0) + { return; } - - cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc; - cJSON_free = (hooks->free_fn)?hooks->free_fn:free; -} -/* Internal constructor. */ -static cJSON *cJSON_New_Item(void) -{ - cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON)); - if (node) memset(node,0,sizeof(cJSON)); - return node; + cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); } -/* Delete a cJSON structure. */ -void cJSON_Delete(cJSON *c) +static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive) { - cJSON *next; - while (c) - { - next=c->next; - if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child); - if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring); - if (c->string) cJSON_free(c->string); - cJSON_free(c); - c=next; - } -} - -/* Parse the input text to generate a number, and populate the result into item. */ -static const char *parse_number(cJSON *item,const char *num) -{ - double n=0,sign=1,scale=0;int32_t subscale=0,signsubscale=1; - - if (*num=='-') sign=-1,num++; /* Has sign? */ - if (*num=='0') num++; /* is zero */ - if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */ - if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */ - if (*num=='e' || *num=='E') /* Exponent? */ - { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */ - while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */ - } - - n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */ - - item->valuedouble=n; - item->valueint=(int64_t)n; - item->type=cJSON_Number; - return num; -} - -/* Render the number nicely from the given item into a string. */ -static char *print_number(cJSON *item) -{ - char *str; - double d = item->valuedouble; - if ( fabs(((double)item->valueint) - d) <= DBL_EPSILON && d >= (1. - DBL_EPSILON) && d < (1LL << 62) )//d <= INT_MAX && d >= INT_MIN ) - { - str = (char *)cJSON_malloc(24); /* 2^64+1 can be represented in 21 chars + sign. */ - if ( str != 0 ) - sprintf(str,"%lld",(long long)item->valueint); - } - else - { - str = (char *)cJSON_malloc(66); /* This is a nice tradeoff. */ - if ( str != 0 ) - { - if ( fabs(floor(d) - d) <= DBL_EPSILON && fabs(d) < 1.0e60 ) - sprintf(str,"%.0f",d); - //else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d); - else - sprintf(str,"%.8f",d); - } - } - return str; -} - -static unsigned parse_hex4(const char *str) -{ - unsigned h=0; - if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; - h=h<<4;str++; - if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; - h=h<<4;str++; - if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; - h=h<<4;str++; - if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; - return h; -} - -/* Parse the input text into an unescaped cstring, and populate item. */ -static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; -static const char *parse_string(cJSON *item,const char *str) -{ - const char *ptr=str+1;char *ptr2;char *out;int32_t len=0;unsigned uc,uc2; - if (*str!='\"') {ep=str;return 0;} /* not a string! */ - - while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; // Skip escaped quotes - - out=(char*)cJSON_malloc(len+2); /* This is how long we need for the string, roughly. */ - if (!out) return 0; - - ptr=str+1;ptr2=out; - while (*ptr!='\"' && *ptr) - { - if (*ptr!='\\') - { - if ( *ptr == '%' && is_hexstr((char *)&ptr[1],2) && isprint(_decode_hex((char *)&ptr[1])) != 0 ) - *ptr2++ = _decode_hex((char *)&ptr[1]), ptr += 3; - else *ptr2++ = *ptr++; - } - else - { - ptr++; - switch (*ptr) - { - case 'b': *ptr2++='\b'; break; - case 'f': *ptr2++='\f'; break; - case 'n': *ptr2++='\n'; break; - case 'r': *ptr2++='\r'; break; - case 't': *ptr2++='\t'; break; - case 'u': // transcode utf16 to utf8 - uc=parse_hex4(ptr+1);ptr+=4; // get the unicode char - - if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; // check for invalid - - if (uc>=0xD800 && uc<=0xDBFF) // UTF16 surrogate pairs - { - if (ptr[1]!='\\' || ptr[2]!='u') break; // missing second-half of surrogate. - uc2=parse_hex4(ptr+3);ptr+=6; - if (uc2<0xDC00 || uc2>0xDFFF) break; // invalid second-half of surrogate - uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF)); - } - - len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len; - - switch (len) { - case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; - case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; - case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; - case 1: *--ptr2 =(uc | firstByteMark[len]); - } - ptr2+=len; - break; - default: *ptr2++=*ptr; break; - } - ptr++; - } - } - *ptr2=0; - if (*ptr=='\"') ptr++; - item->valuestring=out; - item->type=cJSON_String; - return ptr; -} - -/* Render the cstring provided to an escaped version that can be printed. */ -static char *print_string_ptr(const char *str) -{ - const char *ptr;char *ptr2,*out;int32_t len=0;unsigned char token; - - if (!str) return cJSON_strdup(""); - ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;} - - out=(char*)cJSON_malloc(len+3+1); - if (!out) return 0; - - ptr2=out;ptr=str; - *ptr2++='\"'; - while (*ptr) - { - if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++; - else - { - *ptr2++='\\'; - switch (token=*ptr++) - { - case '\\': *ptr2++='\\'; break; - case '\"': *ptr2++='\"'; break; - case '\b': *ptr2++='b'; break; - case '\f': *ptr2++='f'; break; - case '\n': *ptr2++='n'; break; - case '\r': *ptr2++='r'; break; - case '\t': *ptr2++='t'; break; - default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */ - } - } - } - *ptr2++='\"';*ptr2++=0; - return out; -} -/* Invote print_string_ptr (which is useful) on an item. */ -static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);} + if ((replacement == NULL) || (string == NULL)) + { + return false; + } -/* Predeclare these prototypes. */ -static const char *parse_value(cJSON *item,const char *value); -static char *print_value(cJSON *item,int32_t depth,int32_t fmt); -static const char *parse_array(cJSON *item,const char *value); -static char *print_array(cJSON *item,int32_t depth,int32_t fmt); -static const char *parse_object(cJSON *item,const char *value); -static char *print_object(cJSON *item,int32_t depth,int32_t fmt); + /* replace the name in the replacement */ + if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) + { + cJSON_free(replacement->string); + } + replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + replacement->type &= ~cJSON_StringIsConst; -/* Utility to jump whitespace and cr/lf */ -static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;} + cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); -/* Parse an object - create a new root, and populate. */ -cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int32_t require_null_terminated) -{ - const char *end=0; - cJSON *c=cJSON_New_Item(); - ep=0; - if (!c) return 0; /* memory fail */ - - end=parse_value(c,skip(value)); - if (!end) {cJSON_Delete(c);return 0;} /* parse failure. ep is set. */ - - /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ - if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}} - if (return_parse_end) *return_parse_end=end; - return c; -} -/* Default options for cJSON_Parse */ -cJSON *cJSON_Parse(const char *value) -{ - return(cJSON_ParseWithOpts(value,0,0)); + return true; } -/* Render a cJSON item/entity/structure to text. */ -char *cJSON_Print(cJSON *item) +CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) { - return(print_value(item,0,1)); + replace_item_in_object(object, string, newitem, false); } -char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);} -/* Parser core - when encountering text, process appropriately. */ -static const char *parse_value(cJSON *item,const char *value) +CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) { - if (!value) return 0; /* Fail on null. */ - if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; } - if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; } - if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; } - if (*value=='\"') { return parse_string(item,value); } - if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); } - if (*value=='[') { return parse_array(item,value); } - if (*value=='{') { return parse_object(item,value); } - - ep=value;return 0; /* failure. */ + replace_item_in_object(object, string, newitem, true); } -/* Render a value to text. */ -static char *print_value(cJSON *item,int32_t depth,int32_t fmt) -{ - char *out=0; - if (!item) return 0; - switch ((item->type)&255) - { - case cJSON_NULL: out=cJSON_strdup("null"); break; - case cJSON_False: out=cJSON_strdup("false");break; - case cJSON_True: out=cJSON_strdup("true"); break; - case cJSON_Number: out=print_number(item);break; - case cJSON_String: out=print_string(item);break; - case cJSON_Array: out=print_array(item,depth,fmt);break; - case cJSON_Object: out=print_object(item,depth,fmt);break; - } - return out; -} +/* Create basic types: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_NULL; + } -/* Build an array from input text. */ -static const char *parse_array(cJSON *item,const char *value) -{ - cJSON *child; - if (*value!='[') {ep=value;return 0;} /* not an array! */ - - item->type=cJSON_Array; - value=skip(value+1); - if (*value==']') return value+1; /* empty array. */ - - item->child=child=cJSON_New_Item(); - if (!item->child) return 0; /* memory fail */ - value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */ - if (!value) return 0; - - while (*value==',') - { - cJSON *new_item; - if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */ - child->next=new_item;new_item->prev=child;child=new_item; - value=skip(parse_value(child,skip(value+1))); - if (!value) return 0; /* memory fail */ - } - - if (*value==']') return value+1; /* end of array */ - ep=value;return 0; /* malformed. */ + return item; } -/* Render an array to text */ -static char *print_array(cJSON *item,int32_t depth,int32_t fmt) -{ - char **entries; - char *out=0,*ptr,*ret;int32_t len=5; - cJSON *child=item->child; - int32_t numentries=0,i=0,fail=0; - - /* How many entries in the array? */ - while (child) numentries++,child=child->next; - /* Explicitly handle numentries==0 */ - if (!numentries) - { - out=(char*)cJSON_malloc(3+1); - if (out) strcpy(out,"[]"); - return out; - } - /* Allocate an array to hold the values for each */ - entries=(char**)cJSON_malloc((1+numentries)*sizeof(char*)); - if (!entries) return 0; - memset(entries,0,numentries*sizeof(char*)); - /* Retrieve all the results: */ - child=item->child; - while (child && !fail) - { - ret=print_value(child,depth+1,fmt); - entries[i++]=ret; - if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1; - child=child->next; - } - - /* If we didn't fail, try to malloc the output string */ - if (!fail) out=(char*)cJSON_malloc(len+1); - /* If that fails, we fail. */ - if (!out) fail=1; - - /* Handle failure. */ - if (fail) - { - for (i=0;itype = cJSON_True; + } -/* Build an object from the text. */ -static const char *parse_object(cJSON *item,const char *value) -{ - cJSON *child; - if (*value!='{') {ep=value;return 0;} /* not an object! */ - - item->type=cJSON_Object; - value=skip(value+1); - if (*value=='}') return value+1; /* empty array. */ - - item->child=child=cJSON_New_Item(); - if (!item->child) return 0; - value=skip(parse_string(child,skip(value))); - if (!value) return 0; - child->string=child->valuestring;child->valuestring=0; - if (*value!=':') {ep=value;return 0;} /* fail! */ - value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */ - if (!value) return 0; - - while (*value==',') - { - cJSON *new_item; - if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */ - child->next=new_item;new_item->prev=child;child=new_item; - value=skip(parse_string(child,skip(value+1))); - if (!value) return 0; - child->string=child->valuestring;child->valuestring=0; - if (*value!=':') {ep=value;return 0;} /* fail! */ - value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */ - if (!value) return 0; - } - - if (*value=='}') return value+1; /* end of array */ - ep=value;return 0; /* malformed. */ + return item; } -/* Render an object to text. */ -static char *print_object(cJSON *item,int32_t depth,int32_t fmt) +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) { - char **entries=0,**names=0; - char *out=0,*ptr,*ret,*str;int32_t len=7,i=0,j; - cJSON *child=item->child,*firstchild; - int32_t numentries=0,fail=0; - // Count the number of entries - firstchild = child; - while ( child ) + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) { - numentries++; - child = child->next; - if ( child == firstchild ) - { - printf("cJSON infinite loop detected\n"); - break; - } + item->type = cJSON_False; } - /* Explicitly handle empty object case */ - if (!numentries) - { - out=(char*)cJSON_malloc(fmt?depth+4+1:3+1); - if (!out) return 0; - ptr=out;*ptr++='{'; - if (fmt) {*ptr++='\n';for (i=0;ichild;depth++;if (fmt) len+=depth; - while ( child ) - { - names[i]=str=print_string_ptr(child->string); - entries[i++]=ret=print_value(child,depth,fmt); - if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1; - child=child->next; - if ( child == firstchild ) - break; - } - - /* Try to allocate the output string */ - if (!fail) out=(char*)cJSON_malloc(len+1); - if (!out) fail=1; - - /* Handle failure */ - if (fail) - { - for (i=0;ichild;int32_t i=0;while(c)i++,c=c->next;return i;} -cJSON *cJSON_GetArrayItem(cJSON *array,int32_t item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;} -cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;} - -/* Utility for array list handling. */ -static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;} -/* Utility for handling references. */ -static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();if (!ref) return 0;memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;} -/* Add item to array/object. */ -void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!item) return; if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}} -void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);} -void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));} -void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));} - -cJSON *cJSON_DetachItemFromArray(cJSON *array,int32_t which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0; - if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;} -void cJSON_DeleteItemFromArray(cJSON *array,int32_t which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));} -cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int32_t i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;} -void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));} + return item; +} -/* Replace array/object items with new ones. */ -void cJSON_ReplaceItemInArray(cJSON *array,int32_t which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return; - newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem; - if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);} -void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int32_t i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}} +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool b) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = b ? cJSON_True : cJSON_False; + } -/* Create basic types: */ -cJSON *cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;} -cJSON *cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;} -cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;} -cJSON *cJSON_CreateBool(int32_t b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;} -cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int64_t)num;}return item;} -cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;} -cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;} -cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;} + return item; +} -/* Create Arrays: */ -cJSON *cJSON_CreateIntArray(int64_t *numbers,int32_t count) {int32_t i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} -cJSON *cJSON_CreateFloatArray(float *numbers,int32_t count) {int32_t i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} -cJSON *cJSON_CreateDoubleArray(double *numbers,int32_t count) {int32_t i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} -cJSON *cJSON_CreateStringArray(char **strings,int32_t count) {int32_t i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Number; + item->valuedouble = num; -/* Duplication */ -cJSON *cJSON_Duplicate(cJSON *item,int32_t recurse) -{ - cJSON *newitem,*cptr,*nptr=0,*newchild; - /* Bail on bad ptr */ - if (!item) return 0; - /* Create new item */ - newitem=cJSON_New_Item(); - if (!newitem) return 0; - /* Copy over all vars */ - newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble; - if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}} - if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}} - /* If non-recursive, then we're done! */ - if (!recurse) return newitem; - /* Walk the ->next chain for the child. */ - cptr=item->child; - while (cptr) - { - newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */ - if (!newchild) {cJSON_Delete(newitem);return 0;} - if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */ - else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */ - cptr=cptr->next; - } - return newitem; -} - -void cJSON_Minify(char *json) -{ - char *into=json; - while (*json) - { - if (*json==' ') json++; - else if (*json=='\t') json++; // Whitespace characters. - else if (*json=='\r') json++; - else if (*json=='\n') json++; - else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; // double-slash comments, to end of line. - else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} // multiline comments. - else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} // string literals, which are \" sensitive. - else *into++=*json++; // All other characters. - } - *into=0; // and null-terminate. -} - -// the following written by jl777 -/****************************************************************************** - * Copyright © 2014-2017 The SuperNET Developers. * - * * - * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * - * the top-level directory of this distribution for the individual copyright * - * holder information and the developer policies on copyright and licensing. * - * * - * Unless otherwise agreed in a custom licensing agreement, no part of the * - * SuperNET software, including this file may be copied, modified, propagated * - * or distributed except according to the terms contained in the LICENSE file * - * * - * Removal or modification of this copyright notice is prohibited. * - * * - ******************************************************************************/ - -void copy_cJSON(struct destbuf *dest,cJSON *obj) -{ - char *str; - int i; - long offset; - dest->buf[0] = 0; - if ( obj != 0 ) - { - str = cJSON_Print(obj); - if ( str != 0 ) - { - offset = stripquotes(str); - //strcpy(dest,str+offset); - for (i=0; ibuf[i]= str[offset+i]) == 0 ) - break; - dest->buf[i] = 0; - free(str); + /* use saturation in case of overflow */ + if (num >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (num <= INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)num; } } -} -void copy_cJSON2(char *dest,int32_t maxlen,cJSON *obj) -{ - struct destbuf tmp; - maxlen--; - dest[0] = 0; - if ( maxlen > sizeof(tmp.buf) ) - maxlen = sizeof(tmp.buf); - copy_cJSON(&tmp,obj); - if ( strlen(tmp.buf) < maxlen ) - strcpy(dest,tmp.buf); - else dest[0] = 0; + return item; } -int64_t _get_cJSON_int(cJSON *json) +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) { - struct destbuf tmp; - if ( json != 0 ) + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) { - copy_cJSON(&tmp,json); - if ( tmp.buf[0] != 0 ) - return(calc_nxt64bits(tmp.buf)); + item->type = cJSON_String; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } } - return(0); + + return item; } -int64_t get_cJSON_int(cJSON *json,char *field) +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) { - cJSON *numjson; - if ( json != 0 ) + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) { - numjson = cJSON_GetObjectItem(json,field); - if ( numjson != 0 ) - return(_get_cJSON_int(numjson)); + item->type = cJSON_Raw; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } } - return(0); -} -int64_t conv_floatstr(char *numstr) -{ - double val,corr; - val = atof(numstr); - corr = (val < 0.) ? -0.50000000001 : 0.50000000001; - return((int64_t)(val * SATOSHIDEN + corr)); + return item; } -int64_t _conv_cJSON_float(cJSON *json) +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) { - int64_t conv_floatstr(char *); - struct destbuf tmp; - if ( json != 0 ) + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) { - copy_cJSON(&tmp,json); - return(conv_floatstr(tmp.buf)); + item->type=cJSON_Array; } - return(0); -} -int64_t conv_cJSON_float(cJSON *json,char *field) -{ - if ( json != 0 ) - return(_conv_cJSON_float(cJSON_GetObjectItem(json,field))); - return(0); + return item; } -int32_t extract_cJSON_str(char *dest,int32_t max,cJSON *json,char *field) +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) { - int32_t safecopy(char *dest,char *src,long len); - char *str; - cJSON *obj; - int32_t len; - long offset; - dest[0] = 0; - obj = cJSON_GetObjectItem(json,field); - if ( obj != 0 ) + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) { - str = cJSON_Print(obj); - offset = stripquotes(str); - len = safecopy(dest,str+offset,max); - free(str); - return(len); + item->type = cJSON_Object; } - return(0); + + return item; } -cJSON *gen_list_json(char **list) +/* Create Arrays: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) { - cJSON *array,*item; - array = cJSON_CreateArray(); - while ( list != 0 && *list != 0 && *list[0] != 0 ) + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) { - item = cJSON_CreateString(*list++); - cJSON_AddItemToArray(array,item); + return NULL; } - return(array); -} -uint64_t get_API_nxt64bits(cJSON *obj) -{ - uint64_t nxt64bits = 0; - struct destbuf tmp; - if ( obj != 0 ) + a = cJSON_CreateArray(); + for(i = 0; a && (i < (size_t)count); i++) { - if ( is_cJSON_Number(obj) != 0 ) - return((uint64_t)obj->valuedouble); - copy_cJSON(&tmp,obj); - nxt64bits = calc_nxt64bits(tmp.buf); + n = cJSON_CreateNumber(numbers[i]); + if (!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; } - return(nxt64bits); + + return a; } -uint64_t j64bits(cJSON *json,char *field) { if ( field == 0 ) return(get_API_nxt64bits(json)); return(get_API_nxt64bits(cJSON_GetObjectItem(json,field))); } -uint64_t j64bitsi(cJSON *json,int32_t i) { return(get_API_nxt64bits(cJSON_GetArrayItem(json,i))); } -uint64_t get_satoshi_obj(cJSON *json,char *field) +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) { - int32_t i,n; - uint64_t prev,satoshis,mult = 1; - struct destbuf numstr,checkstr; - cJSON *numjson; - numjson = cJSON_GetObjectItem(json,field); - copy_cJSON(&numstr,numjson); - satoshis = prev = 0; mult = 1; n = (int32_t)strlen(numstr.buf); - for (i=n-1; i>=0; i--,mult*=10) + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) { - satoshis += (mult * (numstr.buf[i] - '0')); - if ( satoshis < prev ) - printf("get_satoshi_obj numstr.(%s) i.%d prev.%llu vs satoshis.%llu\n",numstr.buf,i,(unsigned long long)prev,(unsigned long long)satoshis); - prev = satoshis; + return NULL; } - sprintf(checkstr.buf,"%llu",(long long)satoshis); - if ( strcmp(checkstr.buf,numstr.buf) != 0 ) + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) { - printf("SATOSHI GREMLIN?? numstr.(%s) -> %.8f -> (%s)\n",numstr.buf,dstr(satoshis),checkstr.buf); + n = cJSON_CreateNumber((double)numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; } - return(satoshis); -} -void add_satoshis_json(cJSON *json,char *field,uint64_t satoshis) -{ - cJSON *obj; - char numstr[64]; - sprintf(numstr,"%lld",(long long)satoshis); - obj = cJSON_CreateString(numstr); - cJSON_AddItemToObject(json,field,obj); - if ( satoshis != get_satoshi_obj(json,field) ) - printf("error adding satoshi obj %ld -> %ld\n",(unsigned long)satoshis,(unsigned long)get_satoshi_obj(json,field)); + return a; } -char *cJSON_str(cJSON *json) +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) { - if ( json != 0 && is_cJSON_String(json) != 0 ) - return(json->valuestring); - return(0); -} + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; -void jadd(cJSON *json,char *field,cJSON *item) { if ( json != 0 )cJSON_AddItemToObject(json,field,item); } -void jaddstr(cJSON *json,char *field,char *str) { if ( json != 0 && str != 0 ) cJSON_AddItemToObject(json,field,cJSON_CreateString(str)); } -void jaddnum(cJSON *json,char *field,double num) { if ( json != 0 )cJSON_AddItemToObject(json,field,cJSON_CreateNumber(num)); } -void jadd64bits(cJSON *json,char *field,uint64_t nxt64bits) { char numstr[64]; sprintf(numstr,"%llu",(long long)nxt64bits), jaddstr(json,field,numstr); } -void jaddi(cJSON *json,cJSON *item) { if ( json != 0 ) cJSON_AddItemToArray(json,item); } -void jaddistr(cJSON *json,char *str) { if ( json != 0 ) cJSON_AddItemToArray(json,cJSON_CreateString(str)); } -void jaddinum(cJSON *json,double num) { if ( json != 0 ) cJSON_AddItemToArray(json,cJSON_CreateNumber(num)); } -void jaddi64bits(cJSON *json,uint64_t nxt64bits) { char numstr[64]; sprintf(numstr,"%llu",(long long)nxt64bits), jaddistr(json,numstr); } -char *jstr(cJSON *json,char *field) { if ( json == 0 ) return(0); if ( field == 0 ) return(cJSON_str(json)); return(cJSON_str(cJSON_GetObjectItem(json,field))); } - -char *jstri(cJSON *json,int32_t i) { return(cJSON_str(cJSON_GetArrayItem(json,i))); } -char *jprint(cJSON *json,int32_t freeflag) -{ - char *str; - /*static portable_mutex_t mutex; static int32_t initflag; - if ( initflag == 0 ) + if ((count < 0) || (numbers == NULL)) { - portable_mutex_init(&mutex); - initflag = 1; - }*/ - if ( json == 0 ) - return(clonestr((char *)"{}")); - //portable_mutex_lock(&mutex); - //usleep(5000); - str = cJSON_Print(json), _stripwhite(str,' '); - if ( freeflag != 0 ) - free_json(json); - //portable_mutex_unlock(&mutex); - return(str); -} + return NULL; + } -bits256 get_API_bits256(cJSON *obj) -{ - bits256 hash; char *str; - memset(hash.bytes,0,sizeof(hash)); - if ( obj != 0 ) + a = cJSON_CreateArray(); + + for(i = 0;a && (i < (size_t)count); i++) { - if ( is_cJSON_String(obj) != 0 && (str= obj->valuestring) != 0 && strlen(str) == 64 ) - decode_hex(hash.bytes,sizeof(hash),str); + n = cJSON_CreateNumber(numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; } - return(hash); + + return a; } -bits256 jbits256(cJSON *json,char *field) { if ( field == 0 ) return(get_API_bits256(json)); return(get_API_bits256(cJSON_GetObjectItem(json,field))); } -bits256 jbits256i(cJSON *json,int32_t i) { return(get_API_bits256(cJSON_GetArrayItem(json,i))); } -void jaddbits256(cJSON *json,char *field,bits256 hash) { char str[65]; bits256_str(str,hash), jaddstr(json,field,str); } -void jaddibits256(cJSON *json,bits256 hash) { char str[65]; bits256_str(str,hash), jaddistr(json,str); } -char *get_cJSON_fieldname(cJSON *obj) +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count) { - if ( obj != 0 ) + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (strings == NULL)) { - if ( obj->child != 0 && obj->child->string != 0 ) - return(obj->child->string); - else if ( obj->string != 0 ) - return(obj->string); + return NULL; } - return((char *)""); -} -int32_t jnum(cJSON *obj,char *field) -{ - char *str; int32_t polarity = 1; - if ( field != 0 ) - obj = jobj(obj,field); - if ( obj != 0 ) + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) { - if ( is_cJSON_Number(obj) != 0 ) - return(obj->valuedouble); - else if ( is_cJSON_String(obj) != 0 && (str= jstr(obj,0)) != 0 ) + n = cJSON_CreateString(strings[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else { - if ( str[0] == '-' ) - polarity = -1, str++; - return(polarity * (int32_t)calc_nxt64bits(str)); + suffix_object(p,n); } + p = n; } - return(0); -} -void ensure_jsonitem(cJSON *json,char *field,char *value) -{ - cJSON *obj = cJSON_GetObjectItem(json,field); - if ( obj == 0 ) - cJSON_AddItemToObject(json,field,cJSON_CreateString(value)); - else cJSON_ReplaceItemInObject(json,field,cJSON_CreateString(value)); + return a; } -int32_t in_jsonarray(cJSON *array,char *value) +/* Duplication */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) { - int32_t i,n; - struct destbuf remote; - if ( array != 0 && is_cJSON_Array(array) != 0 ) + cJSON *newitem = NULL; + cJSON *child = NULL; + cJSON *next = NULL; + cJSON *newchild = NULL; + + /* Bail on bad ptr */ + if (!item) + { + goto fail; + } + /* Create new item */ + newitem = cJSON_New_Item(&global_hooks); + if (!newitem) + { + goto fail; + } + /* Copy over all vars */ + newitem->type = item->type & (~cJSON_IsReference); + newitem->valueint = item->valueint; + newitem->valuedouble = item->valuedouble; + if (item->valuestring) { - n = cJSON_GetArraySize(array); - for (i=0; ivaluestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); + if (!newitem->valuestring) { - if ( array == 0 || n == 0 ) - break; - copy_cJSON(&remote,cJSON_GetArrayItem(array,i)); - if ( strcmp(remote.buf,value) == 0 ) - return(1); + goto fail; } } - return(0); -} + if (item->string) + { + newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); + if (!newitem->string) + { + goto fail; + } + } + /* If non-recursive, then we're done! */ + if (!recurse) + { + return newitem; + } + /* Walk the ->next chain for the child. */ + child = item->child; + while (child != NULL) + { + newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) + { + goto fail; + } + if (next != NULL) + { + /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + next->next = newchild; + newchild->prev = next; + next = newchild; + } + else + { + /* Set newitem->child and move to it */ + newitem->child = newchild; + next = newchild; + } + child = child->next; + } -int32_t myatoi(char *str,int32_t range) -{ - long x; char *ptr; - x = strtol(str,&ptr,10); - if ( range != 0 && x >= range ) - x = (range - 1); - return((int32_t)x); -} + return newitem; -int32_t get_API_int(cJSON *obj,int32_t val) -{ - struct destbuf buf; - if ( obj != 0 ) +fail: + if (newitem != NULL) { - if ( is_cJSON_Number(obj) != 0 ) - return((int32_t)obj->valuedouble); - copy_cJSON(&buf,obj); - val = myatoi(buf.buf,0); - if ( val < 0 ) - val = 0; + cJSON_Delete(newitem); } - return(val); + + return NULL; } -int32_t jint(cJSON *json,char *field) { if ( json == 0 ) return(0); if ( field == 0 ) return(get_API_int(json,0)); return(get_API_int(cJSON_GetObjectItem(json,field),0)); } -int32_t jinti(cJSON *json,int32_t i) { if ( json == 0 ) return(0); return(get_API_int(cJSON_GetArrayItem(json,i),0)); } -uint32_t get_API_uint(cJSON *obj,uint32_t val) +CJSON_PUBLIC(void) cJSON_Minify(char *json) { - struct destbuf buf; - if ( obj != 0 ) + unsigned char *into = (unsigned char*)json; + + if (json == NULL) + { + return; + } + + while (*json) { - if ( is_cJSON_Number(obj) != 0 ) - return((uint32_t)obj->valuedouble); - copy_cJSON(&buf,obj); - val = myatoi(buf.buf,0); + if (*json == ' ') + { + json++; + } + else if (*json == '\t') + { + /* Whitespace characters. */ + json++; + } + else if (*json == '\r') + { + json++; + } + else if (*json=='\n') + { + json++; + } + else if ((*json == '/') && (json[1] == '/')) + { + /* double-slash comments, to end of line. */ + while (*json && (*json != '\n')) + { + json++; + } + } + else if ((*json == '/') && (json[1] == '*')) + { + /* multiline comments. */ + while (*json && !((*json == '*') && (json[1] == '/'))) + { + json++; + } + json += 2; + } + else if (*json == '\"') + { + /* string literals, which are \" sensitive. */ + *into++ = (unsigned char)*json++; + while (*json && (*json != '\"')) + { + if (*json == '\\') + { + *into++ = (unsigned char)*json++; + } + *into++ = (unsigned char)*json++; + } + *into++ = (unsigned char)*json++; + } + else + { + /* All other characters. */ + *into++ = (unsigned char)*json++; + } } - return(val); + + /* and null-terminate. */ + *into = '\0'; } -uint32_t juint(cJSON *json,char *field) { if ( json == 0 ) return(0); if ( field == 0 ) return(get_API_uint(json,0)); return(get_API_uint(cJSON_GetObjectItem(json,field),0)); } -uint32_t juinti(cJSON *json,int32_t i) { if ( json == 0 ) return(0); return(get_API_uint(cJSON_GetArrayItem(json,i),0)); } -double get_API_float(cJSON *obj) +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) { - double val = 0.; - struct destbuf buf; - if ( obj != 0 ) + if (item == NULL) { - if ( is_cJSON_Number(obj) != 0 ) - return(obj->valuedouble); - copy_cJSON(&buf,obj); - val = atof(buf.buf); + return false; } - return(val); + + return (item->type & 0xFF) == cJSON_Invalid; } -double jdouble(cJSON *json,char *field) +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) { - if ( json != 0 ) + if (item == NULL) { - if ( field == 0 ) - return(get_API_float(json)); - else return(get_API_float(cJSON_GetObjectItem(json,field))); - } else return(0.); + return false; + } + + return (item->type & 0xFF) == cJSON_False; } -double jdoublei(cJSON *json,int32_t i) +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) { - if ( json != 0 ) - return(get_API_float(cJSON_GetArrayItem(json,i))); - else return(0.); + if (item == NULL) + { + return false; + } + + return (item->type & 0xff) == cJSON_True; } -cJSON *jobj(cJSON *json,char *field) { if ( json != 0 ) return(cJSON_GetObjectItem(json,field)); return(0); } -void jdelete(cJSON *json,char *field) +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) { - if ( jobj(json,field) != 0 ) - cJSON_DeleteItemFromObject(json,field); + if (item == NULL) + { + return false; + } + + return (item->type & (cJSON_True | cJSON_False)) != 0; } +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } -cJSON *jduplicate(cJSON *json) { return(cJSON_Duplicate(json,1)); } + return (item->type & 0xFF) == cJSON_NULL; +} -cJSON *jitem(cJSON *array,int32_t i) { if ( array != 0 && is_cJSON_Array(array) != 0 && cJSON_GetArraySize(array) > i ) return(cJSON_GetArrayItem(array,i)); return(0); } -cJSON *jarray(int32_t *nump,cJSON *json,char *field) +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) { - cJSON *array; - if ( json != 0 ) + if (item == NULL) { - if ( field == 0 ) - array = json; - else array = cJSON_GetObjectItem(json,field); - if ( array != 0 && is_cJSON_Array(array) != 0 && (*nump= cJSON_GetArraySize(array)) > 0 ) - return(array); + return false; } - *nump = 0; - return(0); + + return (item->type & 0xFF) == cJSON_Number; } -int32_t expand_nxt64bits(char *NXTaddr,uint64_t nxt64bits) +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) { - int32_t i,n; - uint64_t modval; - char rev[64]; - for (i=0; nxt64bits!=0; i++) + if (item == NULL) { - modval = nxt64bits % 10; - rev[i] = (char)(modval + '0'); - nxt64bits /= 10; + return false; } - n = i; - for (i=0; itype & 0xFF) == cJSON_String; } -char *nxt64str(uint64_t nxt64bits) +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) { - static char NXTaddr[64]; - expand_nxt64bits(NXTaddr,nxt64bits); - return(NXTaddr); + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Array; } -char *nxt64str2(uint64_t nxt64bits) +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) { - static char NXTaddr[64]; - expand_nxt64bits(NXTaddr,nxt64bits); - return(NXTaddr); + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Object; } -int32_t cmp_nxt64bits(const char *str,uint64_t nxt64bits) +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) { - char expanded[64]; - if ( str == 0 )//|| str[0] == 0 || nxt64bits == 0 ) - return(-1); - if ( nxt64bits == 0 && str[0] == 0 ) - return(0); - expand_nxt64bits(expanded,nxt64bits); - return(strcmp(str,expanded)); + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Raw; } -uint64_t calc_nxt64bits(const char *NXTaddr) +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive) { - int32_t c; - int64_t n,i,polarity = 1; - uint64_t lastval,mult,nxt64bits = 0; - if ( NXTaddr == 0 ) + if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) || cJSON_IsInvalid(a)) { - printf("calling calc_nxt64bits with null ptr!\n"); - return(0); + return false; } - n = strlen(NXTaddr); - if ( n >= 22 ) + + /* check if type is valid */ + switch (a->type & 0xFF) { - printf("calc_nxt64bits: illegal NXTaddr.(%s) too long\n",NXTaddr); - return(0); + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + case cJSON_Number: + case cJSON_String: + case cJSON_Raw: + case cJSON_Array: + case cJSON_Object: + break; + + default: + return false; } - else if ( strcmp(NXTaddr,"0") == 0 || strcmp(NXTaddr,"false") == 0 ) + + /* identical objects are equal */ + if (a == b) { - // printf("zero address?\n"); getchar(); - return(0); + return true; } - if ( NXTaddr[0] == '-' ) - polarity = -1, NXTaddr++, n--; - mult = 1; - lastval = 0; - for (i=n-1; i>=0; i--,mult*=10) + + switch (a->type & 0xFF) { - c = NXTaddr[i]; - if ( c < '0' || c > '9' ) + /* in these cases and equal type is enough */ + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + return true; + + case cJSON_Number: + if (a->valuedouble == b->valuedouble) + { + return true; + } + return false; + + case cJSON_String: + case cJSON_Raw: + if ((a->valuestring == NULL) || (b->valuestring == NULL)) + { + return false; + } + if (strcmp(a->valuestring, b->valuestring) == 0) + { + return true; + } + + return false; + + case cJSON_Array: + { + cJSON *a_element = a->child; + cJSON *b_element = b->child; + + for (; (a_element != NULL) && (b_element != NULL);) + { + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + + a_element = a_element->next; + b_element = b_element->next; + } + + /* one of the arrays is longer than the other */ + if (a_element != b_element) { + return false; + } + + return true; + } + + case cJSON_Object: { - printf("calc_nxt64bits: illegal char.(%c %d) in (%s).%d\n",c,c,NXTaddr,(int32_t)i); -#ifdef __APPLE__ - //while ( 1 ) + cJSON *a_element = NULL; + cJSON *b_element = NULL; + cJSON_ArrayForEach(a_element, a) { - //sleep(60); - printf("calc_nxt64bits: illegal char.(%c %d) in (%s).%d\n",c,c,NXTaddr,(int32_t)i); + /* TODO This has O(n^2) runtime, which is horrible! */ + b_element = get_object_item(b, a_element->string, case_sensitive); + if (b_element == NULL) + { + return false; + } + + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } } -#endif - return(0); + + /* doing this twice, once on a and b to prevent true comparison if a subset of b + * TODO: Do this the proper way, this is just a fix for now */ + cJSON_ArrayForEach(b_element, b) + { + a_element = get_object_item(a, b_element->string, case_sensitive); + if (a_element == NULL) + { + return false; + } + + if (!cJSON_Compare(b_element, a_element, case_sensitive)) + { + return false; + } + } + + return true; } - nxt64bits += mult * (c - '0'); - if ( nxt64bits < lastval ) - printf("calc_nxt64bits: warning: 64bit overflow %llx < %llx\n",(long long)nxt64bits,(long long)lastval); - lastval = nxt64bits; + + default: + return false; } - while ( *NXTaddr == '0' && *NXTaddr != 0 ) - NXTaddr++; - if ( cmp_nxt64bits(NXTaddr,nxt64bits) != 0 ) - printf("error calculating nxt64bits: %s -> %llx -> %s\n",NXTaddr,(long long)nxt64bits,nxt64str(nxt64bits)); - if ( polarity < 0 ) - return(-(int64_t)nxt64bits); - return(nxt64bits); } -cJSON *addrs_jsonarray(uint64_t *addrs,int32_t num) +CJSON_PUBLIC(void *) cJSON_malloc(size_t size) { - int32_t j; cJSON *array; - array = cJSON_CreateArray(); - for (j=0; j #include @@ -31,199 +28,241 @@ #include #include -//#include "../crypto777/OS_portable.h" - -#define MAX_JSON_FIELD 4096 // on the big side +#ifndef cJSON__h +#define cJSON__h #ifdef __cplusplus extern "C" { #endif - - /* cJSON Types: */ -#define cJSON_False 0 -#define cJSON_True 1 -#define cJSON_NULL 2 -#define cJSON_Number 3 -#define cJSON_String 4 -#define cJSON_Array 5 -#define cJSON_Object 6 - -#define is_cJSON_Null(json) ((json) != 0 && ((json)->type & 0xff) == cJSON_NULL) -#define is_cJSON_Array(json) ((json) != 0 && ((json)->type & 0xff) == cJSON_Array) -#define is_cJSON_String(json) ((json) != 0 && ((json)->type & 0xff) == cJSON_String) -#define is_cJSON_Number(json) ((json) != 0 && ((json)->type & 0xff) == cJSON_Number) -#define is_cJSON_Object(json) ((json) != 0 && ((json)->type & 0xff) == cJSON_Object) -#define is_cJSON_True(json) ((json) != 0 && ((json)->type & 0xff) == cJSON_True) -#define is_cJSON_False(json) ((json) != 0 && ((json)->type & 0xff) == cJSON_False) - + +/* project version */ +#define CJSON_VERSION_MAJOR 1 +#define CJSON_VERSION_MINOR 5 +#define CJSON_VERSION_PATCH 9 + +#include + +/* cJSON Types: */ +#define cJSON_Invalid (0) +#define cJSON_False (1 << 0) +#define cJSON_True (1 << 1) +#define cJSON_NULL (1 << 2) +#define cJSON_Number (1 << 3) +#define cJSON_String (1 << 4) +#define cJSON_Array (1 << 5) +#define cJSON_Object (1 << 6) +#define cJSON_Raw (1 << 7) /* raw json */ + #define cJSON_IsReference 256 - - /* The cJSON structure: */ - typedef struct cJSON { - struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ - struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ - - int32_t type; /* The type of the item, as above. */ - - char *valuestring; /* The item's string, if type==cJSON_String */ - int64_t valueint; /* The item's number, if type==cJSON_Number */ - double valuedouble; /* The item's number, if type==cJSON_Number */ - - char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ - } cJSON; - - typedef struct cJSON_Hooks { - void *(*malloc_fn)(size_t sz); - void (*free_fn)(void *ptr); - } cJSON_Hooks; - - /* Supply malloc, realloc and free functions to cJSON */ - extern void cJSON_InitHooks(cJSON_Hooks* hooks); - - - /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */ - extern cJSON *cJSON_Parse(const char *value); - /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */ - extern char *cJSON_Print(cJSON *item); - /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */ - extern char *cJSON_PrintUnformatted(cJSON *item); - /* Delete a cJSON entity and all subentities. */ - extern void cJSON_Delete(cJSON *c); - - /* Returns the number of items in an array (or object). */ - extern int cJSON_GetArraySize(cJSON *array); - /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */ - extern cJSON *cJSON_GetArrayItem(cJSON *array,int32_t item); - /* Get item "string" from object. Case insensitive. */ - extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string); - - /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ - extern const char *cJSON_GetErrorPtr(void); - - /* These calls create a cJSON item of the appropriate type. */ - extern cJSON *cJSON_CreateNull(void); - extern cJSON *cJSON_CreateTrue(void); - extern cJSON *cJSON_CreateFalse(void); - extern cJSON *cJSON_CreateBool(int32_t b); - extern cJSON *cJSON_CreateNumber(double num); - extern cJSON *cJSON_CreateString(const char *string); - extern cJSON *cJSON_CreateArray(void); - extern cJSON *cJSON_CreateObject(void); - - /* These utilities create an Array of count items. */ - extern cJSON *cJSON_CreateIntArray(int64_t *numbers,int32_t count); - extern cJSON *cJSON_CreateFloatArray(float *numbers,int32_t count); - extern cJSON *cJSON_CreateDoubleArray(double *numbers,int32_t count); - extern cJSON *cJSON_CreateStringArray(char **strings,int32_t count); - - /* Append item to the specified array/object. */ - extern void cJSON_AddItemToArray(cJSON *array, cJSON *item); - extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item); - /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ - extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); - extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item); - - /* Remove/Detatch items from Arrays/Objects. */ - extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int32_t which); - extern void cJSON_DeleteItemFromArray(cJSON *array,int32_t which); - extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string); - extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string); - - /* Update array items. */ - extern void cJSON_ReplaceItemInArray(cJSON *array,int32_t which,cJSON *newitem); - extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); - - /* Duplicate a cJSON item */ - extern cJSON *cJSON_Duplicate(cJSON *item,int32_t recurse); - /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will - need to be released. With recurse!=0, it will duplicate any children connected to the item. - The item->next and ->prev pointers are always zero on return from Duplicate. */ - - /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ - extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int32_t require_null_terminated); - - extern void cJSON_Minify(char *json); - - /* Macros for creating things quickly. */ -#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) -#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) -#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse()) -#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b)) -#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n)) -#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) - - struct destbuf { char buf[MAX_JSON_FIELD]; }; - - /* When assigning an integer value, it needs to be propagated to valuedouble too. */ -#define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val)) -#define jfieldstr get_cJSON_fieldname - - char *cJSON_str(cJSON *json); - char *jstr(cJSON *json,char *field); - char *jprint(cJSON *json,int32_t freeflag); - int32_t jint(cJSON *json,char *field); - uint32_t juint(cJSON *json,char *field); - char *jstri(cJSON *json,int32_t i); - int32_t jinti(cJSON *json,int32_t i); - uint32_t juinti(cJSON *json,int32_t i); - uint64_t j64bitsi(cJSON *json,int32_t i); - double jdoublei(cJSON *json,int32_t i); - double jdouble(cJSON *json,char *field); - cJSON *jobj(cJSON *json,char *field); - cJSON *jarray(int32_t *nump,cJSON *json,char *field); - cJSON *jitem(cJSON *array,int32_t i); - uint64_t j64bits(cJSON *json,char *field); - void jadd(cJSON *json,char *field,cJSON *item); - void jaddstr(cJSON *json,char *field,char *str); - void jaddnum(cJSON *json,char *field,double num); - void jadd64bits(cJSON *json,char *field,uint64_t nxt64bits); - void jaddi(cJSON *json,cJSON *item); - void jaddistr(cJSON *json,char *str); - void jaddinum(cJSON *json,double num); - void jaddi64bits(cJSON *json,uint64_t nxt64bits); - void jdelete(cJSON *object,char *string); - cJSON *jduplicate(cJSON *json); - int32_t jnum(cJSON *obj,char *field); - - bits256 jbits256(cJSON *json,char *field); - bits256 jbits256i(cJSON *json,int32_t i); - void jaddbits256(cJSON *json,char *field,bits256 hash); - void jaddibits256(cJSON *json,bits256 hash); - void copy_cJSON(struct destbuf *dest,cJSON *obj); - void copy_cJSON2(char *dest,int32_t maxlen,cJSON *obj); - cJSON *gen_list_json(char **list); - int32_t extract_cJSON_str(char *dest,int32_t max,cJSON *json,char *field); - - void free_json(cJSON *json); - int64_t _conv_cJSON_float(cJSON *json); - int64_t conv_cJSON_float(cJSON *json,char *field); - int64_t get_cJSON_int(cJSON *json,char *field); - void add_satoshis_json(cJSON *json,char *field,uint64_t satoshis); - uint64_t get_satoshi_obj(cJSON *json,char *field); - - int32_t get_API_int(cJSON *obj,int32_t val); - uint32_t get_API_uint(cJSON *obj,uint32_t val); - uint64_t get_API_nxt64bits(cJSON *obj); - double get_API_float(cJSON *obj); - char *get_cJSON_fieldname(cJSON *obj); - void ensure_jsonitem(cJSON *json,char *field,char *value); - int32_t in_jsonarray(cJSON *array,char *value); - char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char *command,char *params); - uint64_t calc_nxt64bits(const char *str); - int32_t expand_nxt64bits(char *str,uint64_t nxt64bits); - char *nxt64str(uint64_t nxt64bits); - char *nxt64str2(uint64_t nxt64bits); - cJSON *addrs_jsonarray(uint64_t *addrs,int32_t num); - int32_t myatoi(char *str,int32_t range); - - char *stringifyM(char *str); -#define replace_backslashquotes unstringify - char *unstringify(char *str); -#define jtrue cJSON_CreateTrue -#define jfalse cJSON_CreateFalse - -#define jfieldname get_cJSON_fieldname +#define cJSON_StringIsConst 512 + +/* The cJSON structure: */ +typedef struct cJSON +{ + /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ + struct cJSON *next; + struct cJSON *prev; + /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ + struct cJSON *child; + + /* The type of the item, as above. */ + int type; + + /* The item's string, if type==cJSON_String and type == cJSON_Raw */ + char *valuestring; + /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */ + int valueint; + /* The item's number, if type==cJSON_Number */ + double valuedouble; + + /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ + char *string; +} cJSON; + +typedef struct cJSON_Hooks +{ + void *(*malloc_fn)(size_t sz); + void (*free_fn)(void *ptr); +} cJSON_Hooks; + +typedef int cJSON_bool; + +#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) +#define __WINDOWS__ +#endif +#ifdef __WINDOWS__ + +/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 2 define options: + +CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols +CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default) +CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol + +For *nix builds that support visibility attribute, you can define similar behavior by + +setting default visibility to hidden by adding +-fvisibility=hidden (for gcc) +or +-xldscope=hidden (for sun cc) +to CFLAGS + +then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does + +*/ + +/* export symbols by default, this is necessary for copy pasting the C and header file */ +#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_EXPORT_SYMBOLS +#endif + +#if defined(CJSON_HIDE_SYMBOLS) +#define CJSON_PUBLIC(type) type __stdcall +#elif defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllexport) type __stdcall +#elif defined(CJSON_IMPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllimport) type __stdcall +#endif +#else /* !WIN32 */ +#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY) +#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type +#else +#define CJSON_PUBLIC(type) type +#endif +#endif + +/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them. + * This is to prevent stack overflows. */ +#ifndef CJSON_NESTING_LIMIT +#define CJSON_NESTING_LIMIT 1000 +#endif + +/* returns the version of cJSON as a string */ +CJSON_PUBLIC(const char*) cJSON_Version(void); + +/* Supply malloc, realloc and free functions to cJSON */ +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); + +/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */ +/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); +/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ +/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated); + +/* Render a cJSON entity to text for transfer/storage. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); +/* Render a cJSON entity to text for transfer/storage without any formatting. */ +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); +/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt); +/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */ +/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */ +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format); +/* Delete a cJSON entity and all subentities. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *c); + +/* Returns the number of items in an array (or object). */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); +/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */ +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); +/* Get item "string" from object. Case insensitive. */ +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string); +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string); +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); +/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); + +/* These functions check the type of an item */ +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item); + +/* These calls create a cJSON item of the appropriate type. */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean); +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); +/* raw json */ +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw); +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); + +/* These utilities create an Array of count items. */ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count); + +/* Append item to the specified array/object. */ +CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); +/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. + * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before + * writing to `item->string` */ +CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); +/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ +CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); + +/* Remove/Detatch items from Arrays/Objects. */ +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); + +/* Update array items. */ +CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */ +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement); +CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); +CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); +CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem); + +/* Duplicate a cJSON item */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); +/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will +need to be released. With recurse!=0, it will duplicate any children connected to the item. +The item->next and ->prev pointers are always zero on return from Duplicate. */ +/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal. + * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */ +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive); + + +CJSON_PUBLIC(void) cJSON_Minify(char *json); + +/* Macros for creating things quickly. */ +#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) +#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) +#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse()) +#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b)) +#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n)) +#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) +#define cJSON_AddRawToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateRaw(s)) + +/* When assigning an integer value, it needs to be propagated to valuedouble too. */ +#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) +/* helper for the cJSON_SetNumberValue macro */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); +#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) + +/* Macro for iterating over an array or object */ +#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) + +/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */ +CJSON_PUBLIC(void *) cJSON_malloc(size_t size); +CJSON_PUBLIC(void) cJSON_free(void *object); #ifdef __cplusplus } diff --git a/src/chainparams.cpp b/src/chainparams.cpp index da825b62c30..13409c8c45a 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -30,7 +30,9 @@ using namespace std; * + Contains no strange transactions */ void *chainparams_commandline(void *ptr); -extern char ASSETCHAINS_SYMBOL[16]; +#include "komodo_defs.h" + +extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; extern uint16_t ASSETCHAINS_PORT; extern uint32_t ASSETCHAIN_INIT; extern uint32_t ASSETCHAINS_MAGIC; @@ -57,7 +59,7 @@ class CMainParams : public CChainParams { consensus.nPowMaxAdjustUp = 16; // 16% adjustment up consensus.nPowTargetSpacing = 1 * 60; consensus.fPowAllowMinDifficultyBlocks = true; //false; - /** + /** * The message start string is designed to be unlikely to occur in normal data. * The characters are rarely used upper ASCII, not valid as UTF-8, and produce * a large 32-bit integer with any alignment. @@ -131,7 +133,7 @@ class CMainParams : public CChainParams { }; if ( pthread_create((pthread_t *)malloc(sizeof(pthread_t)),NULL,chainparams_commandline,(void *)&consensus) != 0 ) { - + } } }; @@ -141,7 +143,11 @@ void *chainparams_commandline(void *ptr) { while ( ASSETCHAINS_PORT == 0 ) { + #ifdef _WIN32 + boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); + #else sleep(1); + #endif } //fprintf(stderr,">>>>>>>> port.%u\n",ASSETCHAINS_PORT); if ( ASSETCHAINS_SYMBOL[0] != 0 ) diff --git a/src/coins.cpp b/src/coins.cpp index 27cac5a6f23..37d96901077 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -8,6 +8,7 @@ #include "random.h" #include "version.h" #include "policy/fees.h" +#include "komodo_defs.h" #include @@ -387,7 +388,7 @@ const CScript &CCoinsViewCache::GetSpendFor(const CTxIn& input) const //uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime); uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue); -extern char ASSETCHAINS_SYMBOL[16]; +extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; CAmount CCoinsViewCache::GetValueIn(int32_t nHeight,int64_t *interestp,const CTransaction& tx,uint32_t tiptime) const { diff --git a/src/compat.h b/src/compat.h index feaa544e256..27afc8b2aba 100644 --- a/src/compat.h +++ b/src/compat.h @@ -10,7 +10,7 @@ #include "config/bitcoin-config.h" #endif -#ifdef WIN32 +#ifdef _WIN32 #ifdef _WIN32_WINNT #undef _WIN32_WINNT #endif @@ -46,7 +46,7 @@ #include #endif -#ifdef WIN32 +#ifdef _WIN32 #define MSG_DONTWAIT 0 #else typedef u_int SOCKET; @@ -64,7 +64,7 @@ typedef u_int SOCKET; #define SOCKET_ERROR -1 #endif -#ifdef WIN32 +#ifdef _WIN32 #ifndef S_IRUSR #define S_IRUSR 0400 #define S_IWUSR 0200 @@ -78,7 +78,7 @@ typedef u_int SOCKET; #define MSG_NOSIGNAL 0 #endif -#ifndef WIN32 +#ifndef _WIN32 // PRIO_MAX is not defined on Solaris #ifndef PRIO_MAX #define PRIO_MAX 20 @@ -94,7 +94,7 @@ size_t strnlen( const char *start, size_t max_len); #endif // HAVE_DECL_STRNLEN bool static inline IsSelectableSocket(SOCKET s) { -#ifdef WIN32 +#ifdef _WIN32 return true; #else return (s < FD_SETSIZE); diff --git a/src/crypto/equihash.cpp b/src/crypto/equihash.cpp index 3a9f3f042aa..9e6d185902b 100644 --- a/src/crypto/equihash.cpp +++ b/src/crypto/equihash.cpp @@ -18,6 +18,9 @@ #include "crypto/equihash.h" #include "util.h" +#ifndef __linux__ +#include "compat/endian.h" +#endif #include #include @@ -25,6 +28,7 @@ #include +/* #ifdef __APPLE__ #include #include @@ -48,7 +52,7 @@ #define __LITTLE_ENDIAN LITTLE_ENDIAN #define __BYTE_ORDER BYTE_ORDER #endif - +*/ EhSolverCancelledException solver_cancelled; template diff --git a/src/dpowassets b/src/dpowassets index aaca8bcfbe7..6c1c75c2ced 100755 --- a/src/dpowassets +++ b/src/dpowassets @@ -14,6 +14,7 @@ curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dp curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CRYPTO\",\"pubkey\":\"$pubkey\"}" curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HODL\",\"pubkey\":\"$pubkey\"}" curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"SHARK\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MSHARK\",\"pubkey\":\"$pubkey\"}" curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"BOTS\",\"pubkey\":\"$pubkey\"}" curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MGW\",\"pubkey\":\"$pubkey\"}" #curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MVP\",\"pubkey\":\"$pubkey\"}" @@ -22,39 +23,41 @@ curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dp curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"KV\",\"pubkey\":\"$pubkey\"}" curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CEAL\",\"pubkey\":\"$pubkey\"}" curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MESH\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MNZ\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CHIPS\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"USD\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"EUR\",\"pubkey\":\"$pubkey\"}" - -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"JPY\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"GBP\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"AUD\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CAD\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CHF\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"NZD\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CNY\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"RUB\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MXN\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"BRL\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"INR\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HKD\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"TRY\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"ZAR\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"PLN\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"NOK\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"SEK\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"DKK\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CZK\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HUF\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"ILS\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"KRW\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MYR\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"PHP\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"RON\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"SGD\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"THB\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"BGN\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"IDR\",\"pubkey\":\"$pubkey\"}" -curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HRK\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"USD\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"EUR\",\"pubkey\":\"$pubkey\"}" + # +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"JPY\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"GBP\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"AUD\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CAD\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CHF\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"NZD\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CNY\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"RUB\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MXN\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"BRL\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"INR\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HKD\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"TRY\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"ZAR\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"PLN\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"NOK\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"SEK\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"DKK\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"CZK\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HUF\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"ILS\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"KRW\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MYR\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"PHP\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"RON\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"SGD\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"THB\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"BGN\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"IDR\",\"pubkey\":\"$pubkey\"}" +#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"HRK\",\"pubkey\":\"$pubkey\"}" diff --git a/src/fiat-cli b/src/fiat-cli index 119f25c6d91..5c8940f8293 100755 --- a/src/fiat-cli +++ b/src/fiat-cli @@ -41,6 +41,7 @@ echo bet; fiat/bet $1 $2 $3 $4 echo crypto; fiat/crypto $1 $2 $3 $4 echo hodl; fiat/hodl $1 $2 $3 $4 echo shark; fiat/shark $1 $2 $3 $4 +echo mshark; fiat/mshark $1 $2 $3 $4 echo bots; fiat/bots $1 $2 $3 $4 echo mgw; fiat/mgw $1 $2 $3 $4 #echo mvp; fiat/mvp $1 $2 $3 $4 @@ -49,3 +50,4 @@ echo wlc; fiat/wlc $1 $2 $3 $4 echo kv; fiat/kv $1 $2 $3 $4 echo ceal; fiat/ceal $1 $2 $3 $4 echo mesh; fiat/mesh $1 $2 $3 $4 +echo mnz; fiat/mnz $1 $2 $3 $4 diff --git a/src/fiat/mnz b/src/fiat/mnz new file mode 100755 index 00000000000..f4e1f3501f0 --- /dev/null +++ b/src/fiat/mnz @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=MNZ $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/mshark b/src/fiat/mshark new file mode 100755 index 00000000000..1f0c950c668 --- /dev/null +++ b/src/fiat/mshark @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=MSHARK $1 $2 $3 $4 $5 $6 diff --git a/src/gtest/test_transaction.cpp b/src/gtest/test_transaction.cpp index a339f7652a9..e7558868550 100644 --- a/src/gtest/test_transaction.cpp +++ b/src/gtest/test_transaction.cpp @@ -38,8 +38,13 @@ TEST(Transaction, JSDescriptionRandomized) { libzcash::JSOutput(addr, 50), libzcash::JSOutput(addr, 50) }; + #ifdef __LP64__ // required for building on MacOS + boost::array inputMap; + boost::array outputMap; + #else boost::array inputMap; boost::array outputMap; + #endif { auto jsdesc = JSDescription::Randomized( @@ -48,12 +53,22 @@ TEST(Transaction, JSDescriptionRandomized) { inputMap, outputMap, 0, 0, false); + #ifdef __LP64__ // required for building on MacOS + std::set inputSet(inputMap.begin(), inputMap.end()); + std::set expectedInputSet {0, 1}; + #else std::set inputSet(inputMap.begin(), inputMap.end()); std::set expectedInputSet {0, 1}; + #endif EXPECT_EQ(expectedInputSet, inputSet); + #ifdef __LP64__ // required for building on MacOS + std::set outputSet(outputMap.begin(), outputMap.end()); + std::set expectedOutputSet {0, 1}; + #else std::set outputSet(outputMap.begin(), outputMap.end()); std::set expectedOutputSet {0, 1}; + #endif EXPECT_EQ(expectedOutputSet, outputSet); } @@ -64,8 +79,13 @@ TEST(Transaction, JSDescriptionRandomized) { inputMap, outputMap, 0, 0, false, GenZero); + #ifdef __LP64__ // required for building on MacOS + boost::array expectedInputMap {1, 0}; + boost::array expectedOutputMap {1, 0}; + #else boost::array expectedInputMap {1, 0}; boost::array expectedOutputMap {1, 0}; + #endif EXPECT_EQ(expectedInputMap, inputMap); EXPECT_EQ(expectedOutputMap, outputMap); } @@ -77,8 +97,13 @@ TEST(Transaction, JSDescriptionRandomized) { inputMap, outputMap, 0, 0, false, GenMax); + #ifdef __LP64__ // required for building on MacOS + boost::array expectedInputMap {0, 1}; + boost::array expectedOutputMap {0, 1}; + #else boost::array expectedInputMap {0, 1}; boost::array expectedOutputMap {0, 1}; + #endif EXPECT_EQ(expectedInputMap, inputMap); EXPECT_EQ(expectedOutputMap, outputMap); } diff --git a/src/httprpc.cpp b/src/httprpc.cpp index 2a465ee54e5..d6b317537e9 100644 --- a/src/httprpc.cpp +++ b/src/httprpc.cpp @@ -126,6 +126,15 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &) // singleton request if (valRequest.isObject()) { jreq.parse(valRequest); + + if (!RPCAuthorized(authHeader.second)) { + LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", req->GetPeer().ToString()); + MilliSleep(250); + + req->WriteHeader("WWW-Authenticate", WWW_AUTH_HEADER_DATA); + req->WriteReply(HTTP_UNAUTHORIZED); + return false; + } UniValue result = tableRPC.execute(jreq.strMethod, jreq.params); diff --git a/src/init.cpp b/src/init.cpp index 7905b268e36..fe10401a867 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -40,7 +40,7 @@ #include #include -#ifndef WIN32 +#ifndef _WIN32 #include #endif @@ -74,7 +74,7 @@ bool fFeeEstimatesInitialized = false; static CZMQNotificationInterface* pzmqNotificationInterface = NULL; #endif -#ifdef WIN32 +#ifdef _WIN32 // Win32 LevelDB doesn't use file descriptors, and the ones used for // accessing block files don't count towards the fd_set size limit // anyway. @@ -236,7 +236,7 @@ void Shutdown() } #endif -#ifndef WIN32 +#ifndef _WIN32 try { boost::filesystem::remove(GetPidFile()); } catch (const boost::filesystem::filesystem_error& e) { @@ -334,7 +334,7 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += HelpMessageOpt("-maxorphantx=", strprintf(_("Keep at most unconnectable transactions in memory (default: %u)"), DEFAULT_MAX_ORPHAN_TRANSACTIONS)); strUsage += HelpMessageOpt("-par=", strprintf(_("Set the number of script verification threads (%u to %d, 0 = auto, <0 = leave that many cores free, default: %d)"), -(int)boost::thread::hardware_concurrency(), MAX_SCRIPTCHECK_THREADS, DEFAULT_SCRIPTCHECK_THREADS)); -#ifndef WIN32 +#ifndef _WIN32 strUsage += HelpMessageOpt("-pid=", strprintf(_("Specify pid file (default: %s)"), "komodod.pid")); #endif strUsage += HelpMessageOpt("-prune=", strprintf(_("Reduce storage requirements by pruning (deleting) old blocks. This mode disables wallet support and is incompatible with -txindex. " @@ -716,7 +716,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) // Disable confusing "helpful" text message on abort, Ctrl-C _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); #endif -#ifdef WIN32 +#ifdef _WIN32 // Enable Data Execution Prevention (DEP) // Minimum supported OS versions: WinXP SP3, WinVista >= SP1, Win Server 2008 // A failure is non-critical and needs no further attention! @@ -733,7 +733,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) if (!SetupNetworking()) return InitError("Error: Initializing networking failed"); -#ifndef WIN32 +#ifndef _WIN32 if (GetBoolArg("-sysperms", false)) { #ifdef ENABLE_WALLET if (!GetBoolArg("-disablewallet", false)) @@ -1047,7 +1047,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) return InitError(strprintf(_("Cannot obtain a lock on data directory %s. Komodo is probably already running.") + " %s.", strDataDir, e.what())); } -#ifndef WIN32 +#ifndef _WIN32 CreatePidFile(GetPidFile(), getpid()); #endif if (GetBoolArg("-shrinkdebugfile", !fDebug)) diff --git a/src/komodo-tx.cpp b/src/komodo-tx.cpp index 19d0440ddf2..645bc30a24e 100644 --- a/src/komodo-tx.cpp +++ b/src/komodo-tx.cpp @@ -27,6 +27,7 @@ using namespace std; #include "arith_uint256.h" #include "komodo_structs.h" #include "komodo_globals.h" +#include "komodo_defs.h" #include "komodo_interest.h" diff --git a/src/komodo.h b/src/komodo.h index e81522eb513..e8ab6d6be17 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -15,6 +15,11 @@ #ifndef H_KOMODO_H #define H_KOMODO_H +#include "komodo_defs.h" + +#ifdef _WIN32 +#define printf(...) +#endif // Todo: // verify: reorgs @@ -38,7 +43,7 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block); #include "komodo_utils.h" #include "komodo_curve25519.h" -#include "cJSON.c" +#include "komodo_cJSON.c" #include "komodo_bitcoind.h" #include "komodo_interest.h" #include "komodo_pax.h" @@ -52,14 +57,14 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char void komodo_currentheight_set(int32_t height) { - char symbol[16],dest[16]; struct komodo_state *sp; + char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) sp->CURRENT_HEIGHT = height; } int32_t komodo_currentheight() { - char symbol[16],dest[16]; struct komodo_state *sp; + char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) return(sp->CURRENT_HEIGHT); else return(0); @@ -76,7 +81,8 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0); if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) ) errs++; - //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht); + if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && func != 'T' ) + printf("[%s] matched.%d fpos.%ld func.(%d %c) ht.%d\n",ASSETCHAINS_SYMBOL,matched,ftell(fp),func,func,ht); if ( func == 'P' ) { if ( (num= fgetc(fp)) <= 64 ) @@ -99,8 +105,8 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char errs++; if ( fread(¬arized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) ) errs++; - if ( 0 && sp != 0 ) - printf("%s load[%s.%d] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,notarized_height,notarized_hash.ToString().c_str()); + if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && sp != 0 ) + printf("%s load[%s.%d -> %s] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,dest,notarized_height,notarized_hash.ToString().c_str()); //if ( matched != 0 ) global independent states -> inside *sp komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height); } @@ -152,7 +158,7 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char { if ( fread(opret,1,olen,fp) != olen ) errs++; - if ( 0 && matched != 0 ) + if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && matched != 0 ) { int32_t i; for (i=0; i %s] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,dest,notarized_height,notarized_hash.ToString().c_str()); + //if ( matched != 0 ) global independent states -> inside *sp + komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height); + } + else if ( func == 'U' ) // deprecated + { + uint8_t n,nid; uint256 hash; uint64_t mask; + n = filedata[fpos++]; + nid = filedata[fpos++]; + //printf("U %d %d\n",n,nid); + if ( memread(&mask,sizeof(mask),filedata,&fpos,datalen) != sizeof(mask) ) + errs++; + if ( memread(&hash,sizeof(hash),filedata,&fpos,datalen) != sizeof(hash) ) + errs++; + } + else if ( func == 'K' ) + { + int32_t kheight; + if ( memread(&kheight,sizeof(kheight),filedata,&fpos,datalen) != sizeof(kheight) ) + errs++; + komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0); + } + else if ( func == 'T' ) + { + int32_t kheight,ktimestamp; + if ( memread(&kheight,sizeof(kheight),filedata,&fpos,datalen) != sizeof(kheight) ) + errs++; + if ( memread(&ktimestamp,sizeof(ktimestamp),filedata,&fpos,datalen) != sizeof(ktimestamp) ) + errs++; + //if ( matched != 0 ) global independent states -> inside *sp + //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp); + komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp); + } + else if ( func == 'R' ) + { + uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[16384]; + if ( memread(&txid,sizeof(txid),filedata,&fpos,datalen) != sizeof(txid) ) + errs++; + if ( memread(&v,sizeof(v),filedata,&fpos,datalen) != sizeof(v) ) + errs++; + if ( memread(&ovalue,sizeof(ovalue),filedata,&fpos,datalen) != sizeof(ovalue) ) + errs++; + if ( memread(&olen,sizeof(olen),filedata,&fpos,datalen) != sizeof(olen) ) + errs++; + if ( olen < sizeof(opret) ) + { + if ( memread(opret,olen,filedata,&fpos,datalen) != olen ) + errs++; + if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && matched != 0 ) + { + int32_t i; for (i=0; i global PAX + } else + { + int32_t i; + for (i=0; i global PVALS + //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht); + komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals); + //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals); + } else printf("error loading pvals[%d]\n",numpvals); + } + else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func); + *fposp = fpos; + return(func); + } + return(-1); +} + void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint32_t KMDtimestamp,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout) { static FILE *fp; static int32_t errs,didinit; - struct komodo_state *sp; char fname[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33]; + struct komodo_state *sp; char fname[512],symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; int32_t retval,ht,func; uint8_t num,pubkeys[64][33]; if ( didinit == 0 ) { portable_mutex_init(&KOMODO_KV_mutex); @@ -200,17 +338,24 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) { KOMODO_INITDONE = (uint32_t)time(NULL); + printf("[%s] no komodo_stateptr\n",ASSETCHAINS_SYMBOL); return; } + //printf("[%s] (%s) -> (%s)\n",ASSETCHAINS_SYMBOL,symbol,dest); if ( fp == 0 ) { komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"komodostate"); if ( (fp= fopen(fname,"rb+")) != 0 ) { - while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 ) - ; + if ( (retval= komodo_faststateinit(sp,fname,symbol,dest)) > 0 ) + fseek(fp,0,SEEK_END); + else + { + fprintf(stderr,"komodo_faststateinit retval.%d\n",retval); + while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 ) + ; + } } else fp = fopen(fname,"wb+"); - printf("fname.(%s) fpos.%ld\n",fname,ftell(fp)); KOMODO_INITDONE = (uint32_t)time(NULL); } if ( height <= 0 ) @@ -331,7 +476,7 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scriptbuf,int32_t scriptlen,int32_t height,uint256 txhash,int32_t i,int32_t j,uint64_t *voutmaskp,int32_t *specialtxp,int32_t *notarizedheightp,uint64_t value,int32_t notarized,uint64_t signedmask) { static uint256 zero; static FILE *signedfp; - int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33]; struct komodo_state *sp; char symbol[16],dest[16]; + int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33]; struct komodo_state *sp; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) return(-1); if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) @@ -385,6 +530,8 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr opretlen = scriptbuf[len++]; opretlen += (scriptbuf[len++] << 8); } + if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) + printf("[%s] notarized.%d notarizedht.%d sp.Nht %d sp.ht %d opretlen.%d (%c %c %c)\n",ASSETCHAINS_SYMBOL,notarized,*notarizedheightp,sp->NOTARIZED_HEIGHT,sp->CURRENT_HEIGHT,opretlen,scriptbuf[len+32*2+4],scriptbuf[len+32*2+4+1],scriptbuf[len+32*2+4+2]); if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 ) { len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); @@ -397,8 +544,8 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr sp->NOTARIZED_DESTTXID = desttxid; komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0); len += 4; - if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 ) - printf("%s ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s) lens.(%d %d)\n",ASSETCHAINS_SYMBOL,height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),(char *)&scriptbuf[len],opretlen,len); + if ( ASSETCHAINS_SYMBOL[0] != 0 ) + printf("[%s] ht.%d NOTARIZED.%d %s.%s %sTXID.%s lens.(%d %d)\n",ASSETCHAINS_SYMBOL,height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),opretlen,len); if ( ASSETCHAINS_SYMBOL[0] == 0 ) { if ( signedfp == 0 ) @@ -487,7 +634,7 @@ int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys void komodo_connectblock(CBlockIndex *pindex,CBlock& block) { static int32_t hwmheight; - uint64_t signedmask,voutmask; char symbol[16],dest[16]; struct komodo_state *sp; + uint64_t signedmask,voutmask; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; uint8_t scriptbuf[4096],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 kmdtxid,zero,btctxid,txhash; int32_t i,j,k,numnotaries,notarized,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count; memset(&zero,0,sizeof(zero)); @@ -541,9 +688,12 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block) } else printf("cant get scriptPubKey for ht.%d txi.%d vin.%d\n",height,i,j); } numvalid = bitweight(signedmask); - if ( (((height < 90000 || (signedmask & 1) != 0) && numvalid >= KOMODO_MINRATIFY) || numvalid > (numnotaries/5)) ) + if ( (((height < 90000 || (signedmask & 1) != 0) && numvalid >= KOMODO_MINRATIFY) || + (numvalid >= KOMODO_MINRATIFY && ASSETCHAINS_SYMBOL[0] != 0) || + numvalid > (numnotaries/5)) ) { - printf("%s ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d <<<<<<<<<<< notarized\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts); + if ( height > 500000 || ASSETCHAINS_SYMBOL[0] != 0 ) + printf("[%s] ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d <<<<<<<<<<< notarized\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts); notarized = 1; } if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 ) @@ -585,7 +735,8 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block) } if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 ) printf(") "); - //printf("%s ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d notarized.%d special.%d isratification.%d\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts,notarized,specialtx,isratification); + if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 ) + printf("[%s] ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d notarized.%d special.%d isratification.%d\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts,notarized,specialtx,isratification); if ( notarized != 0 && (notarizedheight != 0 || specialtx != 0) ) { if ( isratification != 0 ) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 08fb4be5187..29849f29504 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -16,14 +16,16 @@ // komodo functions that interact with bitcoind C++ #ifdef _WIN32 -#include -#include +#include +#include #else #include #include #endif -#define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"curl",(char *)"http://127.0.0.1:7776",0,0,(char *)(cmdstr)) +#include "komodo_defs.h" + +//#define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"curl",(char *)"http://127.0.0.1:7776",0,0,(char *)(cmdstr)) struct MemoryStruct { char *memory; size_t size; }; struct return_string { char *ptr; size_t len; }; @@ -164,7 +166,7 @@ char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char * curl_handle = curl_easy_init(); init_string(&s); headers = curl_slist_append(0,"Expect:"); - + curl_easy_setopt(curl_handle,CURLOPT_USERAGENT,"mozilla/4.0");//"Mozilla/4.0 (compatible; )"); curl_easy_setopt(curl_handle,CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl_handle,CURLOPT_URL, url); @@ -193,7 +195,7 @@ char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char * bracket0 = (char *)"["; bracket1 = (char *)"]"; } - + databuf = (char *)malloc(256 + strlen(command) + strlen(params)); sprintf(databuf,"{\"id\":\"jl777\",\"method\":\"%s\",\"params\":%s%s%s}",command,bracket0,params,bracket1); //printf("url.(%s) userpass.(%s) databuf.(%s)\n",url,userpass,databuf); @@ -233,7 +235,7 @@ char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char * free(s.ptr); sleep((1< 0 ) { vout = jitem(vouts,n-1); - //printf("vout.(%s)\n",jprint(vout,0)); + if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) + printf("vout.(%s)\n",jprint(vout,0)); if ( (skey= jobj(vout,(char *)"scriptPubKey")) != 0 ) { if ( (hexstr= jstr(skey,(char *)"hex")) != 0 ) @@ -476,12 +485,12 @@ int32_t komodo_verifynotarization(char *symbol,char *dest,int32_t height,int32_t return(retval); } -uint256 komodo_getblockhash(int32_t height) +/*uint256 komodo_getblockhash(int32_t height) { uint256 hash; char params[128],*hexstr,*jsonstr; cJSON *result; int32_t i; uint8_t revbuf[32]; memset(&hash,0,sizeof(hash)); sprintf(params,"[%d]",height); - if ( (jsonstr= komodo_issuemethod(KMDUSERPASS,(char *)"getblockhash",params,7771)) != 0 ) + if ( (jsonstr= komodo_issuemethod(KMDUSERPASS,(char *)"getblockhash",params,BITCOIND_PORT)) != 0 ) { if ( (result= cJSON_Parse(jsonstr)) != 0 ) { @@ -502,12 +511,12 @@ uint256 komodo_getblockhash(int32_t height) return(hash); } -uint256 _komodo_getblockhash(int32_t height); +uint256 _komodo_getblockhash(int32_t height);*/ uint64_t komodo_seed(int32_t height) { uint64_t seed = 0; - if ( 0 ) // problem during init time, seeds are needed for loading blockindex, so null seeds... + /*if ( 0 ) // problem during init time, seeds are needed for loading blockindex, so null seeds... { uint256 hash,zero; CBlockIndex *pindex; memset(&hash,0,sizeof(hash)); @@ -524,7 +533,7 @@ uint64_t komodo_seed(int32_t height) printf(" seed.%d\n",height); seed = arith_uint256(hash.GetHex()).GetLow64(); } - else + else*/ { seed = (height << 13) ^ (height << 2); seed <<= 21; @@ -552,7 +561,7 @@ uint32_t komodo_txtime(uint256 hash) void komodo_disconnect(CBlockIndex *pindex,CBlock& block) { - char symbol[16],dest[16]; struct komodo_state *sp; + char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; //fprintf(stderr,"disconnect ht.%d\n",pindex->nHeight); komodo_init(pindex->nHeight); if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) @@ -883,4 +892,3 @@ int32_t komodo_validate_interest(const CTransaction &tx,int32_t txheight,uint32_ } return(0); } - diff --git a/src/komodo_cJSON.c b/src/komodo_cJSON.c new file mode 100755 index 00000000000..ca2c6517c3c --- /dev/null +++ b/src/komodo_cJSON.c @@ -0,0 +1,559 @@ + +/* + Copyright (c) 2009 Dave Gamble + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +/* cJSON */ +/* JSON parser in C. */ +#include + +#include "cJSON.h" +#include "komodo_cJSON.h" +#include "cJSON.c" + +#ifndef DBL_EPSILON +#define DBL_EPSILON 2.2204460492503131E-16 +#endif + +static const char *ep; + +long stripquotes(char *str) +{ + long len,offset; + if ( str == 0 ) + return(0); + len = strlen(str); + if ( str[0] == '"' && str[len-1] == '"' ) + str[len-1] = 0, offset = 1; + else offset = 0; + return(offset); +} + +static int32_t cJSON_strcasecmp(const char *s1,const char *s2) +{ + if (!s1) return (s1==s2)?0:1;if (!s2) return 1; + for(; tolower((int32_t)(*s1)) == tolower((int32_t)(*s2)); ++s1, ++s2) if(*s1 == 0) return 0; + return tolower((int32_t)(*(const unsigned char *)s1)) - tolower((int32_t)(*(const unsigned char *)s2)); +} + +// the following written by jl777 +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +void copy_cJSON(struct destbuf *dest,cJSON *obj) +{ + char *str; + int i; + long offset; + dest->buf[0] = 0; + if ( obj != 0 ) + { + str = cJSON_Print(obj); + if ( str != 0 ) + { + offset = stripquotes(str); + //strcpy(dest,str+offset); + for (i=0; ibuf[i]= str[offset+i]) == 0 ) + break; + dest->buf[i] = 0; + free(str); + } + } +} + +void copy_cJSON2(char *dest,int32_t maxlen,cJSON *obj) +{ + struct destbuf tmp; + maxlen--; + dest[0] = 0; + if ( maxlen > sizeof(tmp.buf) ) + maxlen = sizeof(tmp.buf); + copy_cJSON(&tmp,obj); + if ( strlen(tmp.buf) < maxlen ) + strcpy(dest,tmp.buf); + else dest[0] = 0; +} + +int64_t _get_cJSON_int(cJSON *json) +{ + struct destbuf tmp; + if ( json != 0 ) + { + copy_cJSON(&tmp,json); + if ( tmp.buf[0] != 0 ) + return(calc_nxt64bits(tmp.buf)); + } + return(0); +} + +int64_t get_cJSON_int(cJSON *json,char *field) +{ + cJSON *numjson; + if ( json != 0 ) + { + numjson = cJSON_GetObjectItem(json,field); + if ( numjson != 0 ) + return(_get_cJSON_int(numjson)); + } + return(0); +} + +int64_t conv_floatstr(char *numstr) +{ + double val,corr; + val = atof(numstr); + corr = (val < 0.) ? -0.50000000001 : 0.50000000001; + return((int64_t)(val * SATOSHIDEN + corr)); +} + +int64_t _conv_cJSON_float(cJSON *json) +{ + int64_t conv_floatstr(char *); + struct destbuf tmp; + if ( json != 0 ) + { + copy_cJSON(&tmp,json); + return(conv_floatstr(tmp.buf)); + } + return(0); +} + +int64_t conv_cJSON_float(cJSON *json,char *field) +{ + if ( json != 0 ) + return(_conv_cJSON_float(cJSON_GetObjectItem(json,field))); + return(0); +} + +int32_t extract_cJSON_str(char *dest,int32_t max,cJSON *json,char *field) +{ + int32_t safecopy(char *dest,char *src,long len); + char *str; + cJSON *obj; + int32_t len; + long offset; + dest[0] = 0; + obj = cJSON_GetObjectItem(json,field); + if ( obj != 0 ) + { + str = cJSON_Print(obj); + offset = stripquotes(str); + len = safecopy(dest,str+offset,max); + free(str); + return(len); + } + return(0); +} + +cJSON *gen_list_json(char **list) +{ + cJSON *array,*item; + array = cJSON_CreateArray(); + while ( list != 0 && *list != 0 && *list[0] != 0 ) + { + item = cJSON_CreateString(*list++); + cJSON_AddItemToArray(array,item); + } + return(array); +} + +uint64_t get_API_nxt64bits(cJSON *obj) +{ + uint64_t nxt64bits = 0; + struct destbuf tmp; + if ( obj != 0 ) + { + if ( cJSON_IsNumber(obj) != 0 ) + return((uint64_t)obj->valuedouble); + copy_cJSON(&tmp,obj); + nxt64bits = calc_nxt64bits(tmp.buf); + } + return(nxt64bits); +} +uint64_t j64bits(cJSON *json,char *field) { if ( field == 0 ) return(get_API_nxt64bits(json)); return(get_API_nxt64bits(cJSON_GetObjectItem(json,field))); } +uint64_t j64bitsi(cJSON *json,int32_t i) { return(get_API_nxt64bits(cJSON_GetArrayItem(json,i))); } + +uint64_t get_satoshi_obj(cJSON *json,char *field) +{ + int32_t i,n; + uint64_t prev,satoshis,mult = 1; + struct destbuf numstr,checkstr; + cJSON *numjson; + numjson = cJSON_GetObjectItem(json,field); + copy_cJSON(&numstr,numjson); + satoshis = prev = 0; mult = 1; n = (int32_t)strlen(numstr.buf); + for (i=n-1; i>=0; i--,mult*=10) + { + satoshis += (mult * (numstr.buf[i] - '0')); + if ( satoshis < prev ) + printf("get_satoshi_obj numstr.(%s) i.%d prev.%llu vs satoshis.%llu\n",numstr.buf,i,(unsigned long long)prev,(unsigned long long)satoshis); + prev = satoshis; + } + sprintf(checkstr.buf,"%llu",(long long)satoshis); + if ( strcmp(checkstr.buf,numstr.buf) != 0 ) + { + printf("SATOSHI GREMLIN?? numstr.(%s) -> %.8f -> (%s)\n",numstr.buf,dstr(satoshis),checkstr.buf); + } + return(satoshis); +} + +void add_satoshis_json(cJSON *json,char *field,uint64_t satoshis) +{ + cJSON *obj; + char numstr[64]; + sprintf(numstr,"%lld",(long long)satoshis); + obj = cJSON_CreateString(numstr); + cJSON_AddItemToObject(json,field,obj); + if ( satoshis != get_satoshi_obj(json,field) ) + printf("error adding satoshi obj %ld -> %ld\n",(unsigned long)satoshis,(unsigned long)get_satoshi_obj(json,field)); +} + +char *cJSON_str(cJSON *json) +{ + if ( json != 0 && cJSON_IsString(json) != 0 ) + return(json->valuestring); + return(0); +} + +void jadd(cJSON *json,char *field,cJSON *item) { if ( json != 0 )cJSON_AddItemToObject(json,field,item); } +void jaddstr(cJSON *json,char *field,char *str) { if ( json != 0 && str != 0 ) cJSON_AddItemToObject(json,field,cJSON_CreateString(str)); } +void jaddnum(cJSON *json,char *field,double num) { if ( json != 0 )cJSON_AddItemToObject(json,field,cJSON_CreateNumber(num)); } +void jadd64bits(cJSON *json,char *field,uint64_t nxt64bits) { char numstr[64]; sprintf(numstr,"%llu",(long long)nxt64bits), jaddstr(json,field,numstr); } +void jaddi(cJSON *json,cJSON *item) { if ( json != 0 ) cJSON_AddItemToArray(json,item); } +void jaddistr(cJSON *json,char *str) { if ( json != 0 ) cJSON_AddItemToArray(json,cJSON_CreateString(str)); } +void jaddinum(cJSON *json,double num) { if ( json != 0 ) cJSON_AddItemToArray(json,cJSON_CreateNumber(num)); } +void jaddi64bits(cJSON *json,uint64_t nxt64bits) { char numstr[64]; sprintf(numstr,"%llu",(long long)nxt64bits), jaddistr(json,numstr); } +char *jstr(cJSON *json,char *field) { if ( json == 0 ) return(0); if ( field == 0 ) return(cJSON_str(json)); return(cJSON_str(cJSON_GetObjectItem(json,field))); } + +char *jstri(cJSON *json,int32_t i) { return(cJSON_str(cJSON_GetArrayItem(json,i))); } +char *jprint(cJSON *json,int32_t freeflag) +{ + char *str; + /*static portable_mutex_t mutex; static int32_t initflag; + if ( initflag == 0 ) + { + portable_mutex_init(&mutex); + initflag = 1; + }*/ + if ( json == 0 ) + return(clonestr((char *)"{}")); + //portable_mutex_lock(&mutex); + //usleep(5000); + str = cJSON_Print(json), _stripwhite(str,' '); + if ( freeflag != 0 ) + free_json(json); + //portable_mutex_unlock(&mutex); + return(str); +} + +bits256 get_API_bits256(cJSON *obj) +{ + bits256 hash; char *str; + memset(hash.bytes,0,sizeof(hash)); + if ( obj != 0 ) + { + if ( cJSON_IsString(obj) != 0 && (str= obj->valuestring) != 0 && strlen(str) == 64 ) + decode_hex(hash.bytes,sizeof(hash),str); + } + return(hash); +} +bits256 jbits256(cJSON *json,char *field) { if ( field == 0 ) return(get_API_bits256(json)); return(get_API_bits256(cJSON_GetObjectItem(json,field))); } +bits256 jbits256i(cJSON *json,int32_t i) { return(get_API_bits256(cJSON_GetArrayItem(json,i))); } +void jaddbits256(cJSON *json,char *field,bits256 hash) { char str[65]; bits256_str(str,hash), jaddstr(json,field,str); } +void jaddibits256(cJSON *json,bits256 hash) { char str[65]; bits256_str(str,hash), jaddistr(json,str); } + +char *get_cJSON_fieldname(cJSON *obj) +{ + if ( obj != 0 ) + { + if ( obj->child != 0 && obj->child->string != 0 ) + return(obj->child->string); + else if ( obj->string != 0 ) + return(obj->string); + } + return((char *)""); +} + +int32_t jnum(cJSON *obj,char *field) +{ + char *str; int32_t polarity = 1; + if ( field != 0 ) + obj = jobj(obj,field); + if ( obj != 0 ) + { + if ( cJSON_IsNumber(obj) != 0 ) + return(obj->valuedouble); + else if ( cJSON_IsString(obj) != 0 && (str= jstr(obj,0)) != 0 ) + { + if ( str[0] == '-' ) + polarity = -1, str++; + return(polarity * (int32_t)calc_nxt64bits(str)); + } + } + return(0); +} + +void ensure_jsonitem(cJSON *json,char *field,char *value) +{ + cJSON *obj = cJSON_GetObjectItem(json,field); + if ( obj == 0 ) + cJSON_AddItemToObject(json,field,cJSON_CreateString(value)); + else cJSON_ReplaceItemInObject(json,field,cJSON_CreateString(value)); +} + +int32_t in_jsonarray(cJSON *array,char *value) +{ + int32_t i,n; + struct destbuf remote; + if ( array != 0 && cJSON_IsArray(array) != 0 ) + { + n = cJSON_GetArraySize(array); + for (i=0; i= range ) + x = (range - 1); + return((int32_t)x); +} + +int32_t get_API_int(cJSON *obj,int32_t val) +{ + struct destbuf buf; + if ( obj != 0 ) + { + if ( cJSON_IsNumber(obj) != 0 ) + return((int32_t)obj->valuedouble); + copy_cJSON(&buf,obj); + val = myatoi(buf.buf,0); + if ( val < 0 ) + val = 0; + } + return(val); +} + +int32_t jint(cJSON *json,char *field) { if ( json == 0 ) return(0); if ( field == 0 ) return(get_API_int(json,0)); return(get_API_int(cJSON_GetObjectItem(json,field),0)); } +int32_t jinti(cJSON *json,int32_t i) { if ( json == 0 ) return(0); return(get_API_int(cJSON_GetArrayItem(json,i),0)); } + +uint32_t get_API_uint(cJSON *obj,uint32_t val) +{ + struct destbuf buf; + if ( obj != 0 ) + { + if ( cJSON_IsNumber(obj) != 0 ) + return((uint32_t)obj->valuedouble); + copy_cJSON(&buf,obj); + val = myatoi(buf.buf,0); + } + return(val); +} +uint32_t juint(cJSON *json,char *field) { if ( json == 0 ) return(0); if ( field == 0 ) return(get_API_uint(json,0)); return(get_API_uint(cJSON_GetObjectItem(json,field),0)); } +uint32_t juinti(cJSON *json,int32_t i) { if ( json == 0 ) return(0); return(get_API_uint(cJSON_GetArrayItem(json,i),0)); } + +double get_API_float(cJSON *obj) +{ + double val = 0.; + struct destbuf buf; + if ( obj != 0 ) + { + if ( cJSON_IsNumber(obj) != 0 ) + return(obj->valuedouble); + copy_cJSON(&buf,obj); + val = atof(buf.buf); + } + return(val); +} + +double jdouble(cJSON *json,char *field) +{ + if ( json != 0 ) + { + if ( field == 0 ) + return(get_API_float(json)); + else return(get_API_float(cJSON_GetObjectItem(json,field))); + } else return(0.); +} + +double jdoublei(cJSON *json,int32_t i) +{ + if ( json != 0 ) + return(get_API_float(cJSON_GetArrayItem(json,i))); + else return(0.); +} + +cJSON *jobj(cJSON *json,char *field) { if ( json != 0 ) return(cJSON_GetObjectItem(json,field)); return(0); } + +void jdelete(cJSON *json,char *field) +{ + if ( jobj(json,field) != 0 ) + cJSON_DeleteItemFromObject(json,field); +} + +cJSON *jduplicate(cJSON *json) { return(cJSON_Duplicate(json,1)); } + +cJSON *jitem(cJSON *array,int32_t i) { if ( array != 0 && cJSON_IsArray(array) != 0 && cJSON_GetArraySize(array) > i ) return(cJSON_GetArrayItem(array,i)); return(0); } +cJSON *jarray(int32_t *nump,cJSON *json,char *field) +{ + cJSON *array; + if ( json != 0 ) + { + if ( field == 0 ) + array = json; + else array = cJSON_GetObjectItem(json,field); + if ( array != 0 && cJSON_IsArray(array) != 0 && (*nump= cJSON_GetArraySize(array)) > 0 ) + return(array); + } + *nump = 0; + return(0); +} + +int32_t expand_nxt64bits(char *NXTaddr,uint64_t nxt64bits) +{ + int32_t i,n; + uint64_t modval; + char rev[64]; + for (i=0; nxt64bits!=0; i++) + { + modval = nxt64bits % 10; + rev[i] = (char)(modval + '0'); + nxt64bits /= 10; + } + n = i; + for (i=0; i= 22 ) + { + printf("calc_nxt64bits: illegal NXTaddr.(%s) too long\n",NXTaddr); + return(0); + } + else if ( strcmp(NXTaddr,"0") == 0 || strcmp(NXTaddr,"false") == 0 ) + { + // printf("zero address?\n"); getchar(); + return(0); + } + if ( NXTaddr[0] == '-' ) + polarity = -1, NXTaddr++, n--; + mult = 1; + lastval = 0; + for (i=n-1; i>=0; i--,mult*=10) + { + c = NXTaddr[i]; + if ( c < '0' || c > '9' ) + { + printf("calc_nxt64bits: illegal char.(%c %d) in (%s).%d\n",c,c,NXTaddr,(int32_t)i); +#ifdef __APPLE__ + //while ( 1 ) + { + //sleep(60); + printf("calc_nxt64bits: illegal char.(%c %d) in (%s).%d\n",c,c,NXTaddr,(int32_t)i); + } +#endif + return(0); + } + nxt64bits += mult * (c - '0'); + if ( nxt64bits < lastval ) + printf("calc_nxt64bits: warning: 64bit overflow %llx < %llx\n",(long long)nxt64bits,(long long)lastval); + lastval = nxt64bits; + } + while ( *NXTaddr == '0' && *NXTaddr != 0 ) + NXTaddr++; + if ( cmp_nxt64bits(NXTaddr,nxt64bits) != 0 ) + printf("error calculating nxt64bits: %s -> %llx -> %s\n",NXTaddr,(long long)nxt64bits,nxt64str(nxt64bits)); + if ( polarity < 0 ) + return(-(int64_t)nxt64bits); + return(nxt64bits); +} + +cJSON *addrs_jsonarray(uint64_t *addrs,int32_t num) +{ + int32_t j; cJSON *array; + array = cJSON_CreateArray(); + for (j=0; j +#include +#include +#include +#include +#include +#include + +#include "cJSON.h" + +//#include "../crypto777/OS_portable.h" + +#define MAX_JSON_FIELD 4096 // on the big side + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* Macros for creating things quickly. */ +#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) +#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) +#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse()) +#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b)) +#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n)) +#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) + + struct destbuf { char buf[MAX_JSON_FIELD]; }; + +#define jfieldstr get_cJSON_fieldname + + char *cJSON_str(cJSON *json); + char *jstr(cJSON *json,char *field); + char *jprint(cJSON *json,int32_t freeflag); + int32_t jint(cJSON *json,char *field); + uint32_t juint(cJSON *json,char *field); + char *jstri(cJSON *json,int32_t i); + int32_t jinti(cJSON *json,int32_t i); + uint32_t juinti(cJSON *json,int32_t i); + uint64_t j64bitsi(cJSON *json,int32_t i); + double jdoublei(cJSON *json,int32_t i); + double jdouble(cJSON *json,char *field); + cJSON *jobj(cJSON *json,char *field); + cJSON *jarray(int32_t *nump,cJSON *json,char *field); + cJSON *jitem(cJSON *array,int32_t i); + uint64_t j64bits(cJSON *json,char *field); + void jadd(cJSON *json,char *field,cJSON *item); + void jaddstr(cJSON *json,char *field,char *str); + void jaddnum(cJSON *json,char *field,double num); + void jadd64bits(cJSON *json,char *field,uint64_t nxt64bits); + void jaddi(cJSON *json,cJSON *item); + void jaddistr(cJSON *json,char *str); + void jaddinum(cJSON *json,double num); + void jaddi64bits(cJSON *json,uint64_t nxt64bits); + void jdelete(cJSON *object,char *string); + cJSON *jduplicate(cJSON *json); + int32_t jnum(cJSON *obj,char *field); + + bits256 jbits256(cJSON *json,char *field); + bits256 jbits256i(cJSON *json,int32_t i); + void jaddbits256(cJSON *json,char *field,bits256 hash); + void jaddibits256(cJSON *json,bits256 hash); + void copy_cJSON(struct destbuf *dest,cJSON *obj); + void copy_cJSON2(char *dest,int32_t maxlen,cJSON *obj); + cJSON *gen_list_json(char **list); + int32_t extract_cJSON_str(char *dest,int32_t max,cJSON *json,char *field); + + void free_json(cJSON *json); + int64_t _conv_cJSON_float(cJSON *json); + int64_t conv_cJSON_float(cJSON *json,char *field); + int64_t get_cJSON_int(cJSON *json,char *field); + void add_satoshis_json(cJSON *json,char *field,uint64_t satoshis); + uint64_t get_satoshi_obj(cJSON *json,char *field); + + int32_t get_API_int(cJSON *obj,int32_t val); + uint32_t get_API_uint(cJSON *obj,uint32_t val); + uint64_t get_API_nxt64bits(cJSON *obj); + double get_API_float(cJSON *obj); + char *get_cJSON_fieldname(cJSON *obj); + void ensure_jsonitem(cJSON *json,char *field,char *value); + int32_t in_jsonarray(cJSON *array,char *value); + char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char *command,char *params); + uint64_t calc_nxt64bits(const char *str); + int32_t expand_nxt64bits(char *str,uint64_t nxt64bits); + char *nxt64str(uint64_t nxt64bits); + char *nxt64str2(uint64_t nxt64bits); + cJSON *addrs_jsonarray(uint64_t *addrs,int32_t num); + int32_t myatoi(char *str,int32_t range); + + char *stringifyM(char *str); +#define replace_backslashquotes unstringify + char *unstringify(char *str); +#define jtrue cJSON_CreateTrue +#define jfalse cJSON_CreateFalse + +#define jfieldname get_cJSON_fieldname + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 6a0ee3fa68f..a7283e2650b 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -20,7 +20,9 @@ #include #include #include - +#ifdef _WIN32 +#include +#endif bits320 fmul(const bits320 in2,const bits320 in); bits320 fexpand(bits256 basepoint); bits256 fcontract(const bits320 input); @@ -144,7 +146,7 @@ bits320 fsquare_times(const bits320 in,uint64_t count) t[2] = ((uint128_t) d0) * r2 + ((uint128_t) r1) * r1 + (((uint128_t) d4) * (r3 )); t[3] = ((uint128_t) d0) * r3 + ((uint128_t) d1) * r2 + (((uint128_t) r4) * (d419 )); t[4] = ((uint128_t) d0) * r4 + ((uint128_t) d1) * r3 + (((uint128_t) r2) * (r2 )); - + r0 = (uint64_t)t[0] & 0x7ffffffffffffLL; c = (uint64_t)(t[0] >> 51); t[1] += c; r1 = (uint64_t)t[1] & 0x7ffffffffffffLL; c = (uint64_t)(t[1] >> 51); t[2] += c; r2 = (uint64_t)t[2] & 0x7ffffffffffffLL; c = (uint64_t)(t[2] >> 51); @@ -401,9 +403,9 @@ div_by_2_25(const limb v) * On entry: |output[i]| < 280*2^54 */ static void freduce_coefficients(limb *output) { unsigned i; - + output[10] = 0; - + for (i = 0; i < 10; i += 2) { limb over = div_by_2_26(output[i]); /* The entry condition (that |output[i]| < 280*2^54) means that over is, at @@ -412,7 +414,7 @@ static void freduce_coefficients(limb *output) { * 281*2^54. */ output[i] -= over << 26; output[i+1] += over; - + /* For the first iteration, |output[i+1]| < 281*2^54, thus |over| < * 281*2^29. When this is added to the next limb, the resulting bound can * be approximated as 281*2^54. @@ -427,9 +429,9 @@ static void freduce_coefficients(limb *output) { output[0] += output[10] << 4; output[0] += output[10] << 1; output[0] += output[10]; - + output[10] = 0; - + /* Now output[1..9] are reduced, and |output[0]| < 2^26 + 19*281*2^29 * So |over| will be no more than 2^16. */ { @@ -437,7 +439,7 @@ static void freduce_coefficients(limb *output) { output[0] -= over << 26; output[1] += over; } - + /* Now output[0,2..9] are reduced, and |output[1]| < 2^25 + 2^16 < 2^26. The * bound on |output[1]| is sufficient to meet our needs. */ } @@ -576,11 +578,11 @@ static void fcontract32(u8 *output, limb *input_limbs) int j; s32 input[10]; s32 mask; - + /* |input_limbs[i]| < 2^26, so it's valid to convert to an s32. */ for (i = 0; i < 10; i++) input[i] = (s32)input_limbs[i]; - + for (j = 0; j < 2; ++j) { for (i = 0; i < 9; ++i) { if ((i & 1) == 1) { @@ -597,7 +599,7 @@ static void fcontract32(u8 *output, limb *input_limbs) input[i+1] = input[i+1] - carry; } } - + /* There's no greater limb for input[9] to borrow from, but we can multiply * by 19 and borrow from input[0], which is valid mod 2^255-19. */ { @@ -606,19 +608,19 @@ static void fcontract32(u8 *output, limb *input_limbs) input[9] = input[9] + (carry << 25); input[0] = input[0] - (carry * 19); } - + /* After the first iteration, input[1..9] are non-negative and fit within * 25 or 26 bits, depending on position. However, input[0] may be * negative. */ } - + /* The first borrow-propagation pass above ended with every limb except (possibly) input[0] non-negative. - + If input[0] was negative after the first pass, then it was because of a carry from input[9]. On entry, input[9] < 2^26 so the carry was, at most, one, since (2**26-1) >> 25 = 1. Thus input[0] >= -19. - + In the second pass, each limb is decreased by at most one. Thus the second borrow-propagation pass could only have wrapped around to decrease input[0] again if the first pass left input[0] negative *and* input[1] @@ -630,7 +632,7 @@ static void fcontract32(u8 *output, limb *input_limbs) input[0] = input[0] + (carry << 26); input[1] = input[1] - carry; } - + /* All input[i] are now non-negative. However, there might be values between * 2^25 and 2^26 in a limb which is, nominally, 25 bits wide. */ for (j = 0; j < 2; j++) { @@ -645,21 +647,21 @@ static void fcontract32(u8 *output, limb *input_limbs) input[i+1] += carry; } } - + { const s32 carry = input[9] >> 25; input[9] &= 0x1ffffff; input[0] += 19*carry; } } - + /* If the first carry-chain pass, just above, ended up with a carry from * input[9], and that caused input[0] to be out-of-bounds, then input[0] was * < 2^26 + 2*19, because the carry was, at most, two. * * If the second pass carried from input[9] again then input[0] is < 2*19 and * the input[9] -> input[0] carry didn't push input[0] out of bounds. */ - + /* It still remains the case that input might be between 2^255-19 and 2^255. * In this case, input[1..9] must take their maximum value and input[0] must * be >= (2^255-19) & 0x3ffffff, which is 0x3ffffed. */ @@ -671,11 +673,11 @@ static void fcontract32(u8 *output, limb *input_limbs) mask &= s32_eq(input[i], 0x3ffffff); } } - + /* mask is either 0xffffffff (if input >= 2^255-19) and zero otherwise. Thus * this conditionally subtracts 2^255-19. */ input[0] -= mask & 0x3ffffed; - + for (i = 1; i < 10; i++) { if ((i & 1) == 1) { input[i] -= mask & 0x1ffffff; @@ -683,7 +685,7 @@ static void fcontract32(u8 *output, limb *input_limbs) input[i] -= mask & 0x3ffffff; } } - + input[1] <<= 2; input[2] <<= 3; input[3] <<= 5; @@ -882,12 +884,18 @@ inline bits320 crecip(const bits320 z) /* 2^255 - 21 */ return(fmul(t0, a)); } +#ifndef _WIN32 void OS_randombytes(unsigned char *x,long xlen); +#endif bits256 rand256(int32_t privkeyflag) { bits256 randval; + #ifndef __WIN32 OS_randombytes(randval.bytes,sizeof(randval)); + #else + randombytes_buf(randval.bytes,sizeof(randval)); + #endif if ( privkeyflag != 0 ) randval.bytes[0] &= 0xf8, randval.bytes[31] &= 0x7f, randval.bytes[31] |= 0x40; return(randval); diff --git a/src/komodo_defs.h b/src/komodo_defs.h new file mode 100644 index 00000000000..db792c44e7c --- /dev/null +++ b/src/komodo_defs.h @@ -0,0 +1,9 @@ +#ifndef KOMODO_DEFS_H +#define KOMODO_DEFS_H + +#define ASSETCHAINS_MINHEIGHT 128 +#define KOMODO_ELECTION_GAP 2000 +#define ROUNDROBIN_DELAY 61 +#define KOMODO_ASSETCHAIN_MAXLEN 65 + +#endif diff --git a/src/komodo_events.h b/src/komodo_events.h index 110c1f757e5..ff054c3bb8b 100644 --- a/src/komodo_events.h +++ b/src/komodo_events.h @@ -15,6 +15,7 @@ #ifndef H_KOMODOEVENTS_H #define H_KOMODOEVENTS_H +#include "komodo_defs.h" struct komodo_event *komodo_eventadd(struct komodo_state *sp,int32_t height,char *symbol,uint8_t type,uint8_t *data,uint16_t datalen) { @@ -41,11 +42,13 @@ void komodo_eventadd_notarized(struct komodo_state *sp,char *symbol,int32_t heig struct komodo_event_notarized N; if ( komodo_verifynotarization(symbol,dest,height,notarizedheight,notarized_hash,notarized_desttxid) != 0 ) { - if ( height > 50000 ) - printf("[%s] error validating notarization ht.%d notarized_height.%d\n",ASSETCHAINS_SYMBOL,height,notarizedheight); + if ( height > 50000 || ASSETCHAINS_SYMBOL[0] != 0 ) + printf("[%s] error validating notarization ht.%d notarized_height.%d, if on a pruned %s node this can be ignored\n",ASSETCHAINS_SYMBOL,height,notarizedheight,dest); } else { + if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) + fprintf(stderr,"validated [%s] ht.%d notarized %d\n",ASSETCHAINS_SYMBOL,height,notarizedheight); memset(&N,0,sizeof(N)); N.blockhash = notarized_hash; N.desttxid = notarized_desttxid; diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 088e8d4158f..d655f2aa52c 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -14,6 +14,7 @@ ******************************************************************************/ // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse +#include "komodo_defs.h" int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base) { @@ -105,7 +106,7 @@ void komodo_paxdelete(struct pax_transaction *pax) void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,uint8_t type,int32_t height,int32_t otherheight,char *source,int32_t approved) // assetchain context { - struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s; + struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN],*s; //if ( KOMODO_PAX == 0 ) // return; //if ( strcmp(symbol,ASSETCHAINS_SYMBOL) != 0 ) @@ -197,7 +198,7 @@ int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transactio int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int64_t *srcvalues,int32_t *kmdheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo) { - struct pax_transaction p,*pax; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t type,rmd160[20]; uint64_t fiatoshis; char symbol[16]; + struct pax_transaction p,*pax; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t type,rmd160[20]; uint64_t fiatoshis; char symbol[KOMODO_ASSETCHAIN_MAXLEN]; //if ( KOMODO_PAX == 0 ) // return(0); incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4)); @@ -274,7 +275,7 @@ int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t che return(0); else { - if ( kmdheight >= 238000 ) + if ( ASSETCHAINS_SYMBOL[0] != 0 ) printf("ht.%d ignore mismatched %s value %lld vs checkvalue %lld -> ratio.%d\n",kmdheight,symbol,(long long)value,(long long)checkvalue,ratio); return(-1); } @@ -290,7 +291,7 @@ int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t che uint64_t komodo_paxtotal() { - struct pax_transaction *pax,*pax2,*tmp,*tmp2; char symbol[16],dest[16],*str; int32_t i,ht; int64_t checktoshis; uint64_t seed,total = 0; struct komodo_state *basesp; + struct pax_transaction *pax,*pax2,*tmp,*tmp2; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN],*str; int32_t i,ht; int64_t checktoshis; uint64_t seed,total = 0; struct komodo_state *basesp; if ( KOMODO_PASSPORT_INITDONE == 0 ) //KOMODO_PAX == 0 || return(0); if ( komodo_isrealtime(&ht) == 0 ) @@ -464,7 +465,7 @@ int32_t komodo_pending_withdraws(char *opretstr) // todo: enforce deterministic int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo) { - struct pax_transaction *pax,*tmp; char symbol[16],dest[16]; uint8_t *script,opcode,opret[16384],data[16384]; int32_t i,baseid,ht,len=0,opretlen=0,numvouts=1; struct komodo_state *sp; uint64_t available,deposited,issued,withdrawn,approved,redeemed,mask,sum = 0; + struct pax_transaction *pax,*tmp; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; uint8_t *script,opcode,opret[16384],data[16384]; int32_t i,baseid,ht,len=0,opretlen=0,numvouts=1; struct komodo_state *sp; uint64_t available,deposited,issued,withdrawn,approved,redeemed,mask,sum = 0; if ( KOMODO_PASSPORT_INITDONE == 0 )//KOMODO_PAX == 0 || return(0); struct komodo_state *kmdsp = komodo_stateptrget((char *)"KMD"); @@ -652,7 +653,7 @@ void komodo_passport_iteration(); int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing { static uint256 array[64]; static int32_t numbanned,indallvouts; - int32_t i,j,k,n,ht,baseid,txn_count,activation,num,opretlen,offset=1,errs=0,matched=0,kmdheights[256],otherheights[256]; uint256 hash,txids[256]; char symbol[16],base[16]; uint16_t vouts[256]; int8_t baseids[256]; uint8_t *script,opcode,rmd160s[256*20]; uint64_t total,available,deposited,issued,withdrawn,approved,redeemed,checktoshis,seed; int64_t values[256],srcvalues[256]; struct pax_transaction *pax; struct komodo_state *sp; + int32_t i,j,k,n,ht,baseid,txn_count,activation,num,opretlen,offset=1,errs=0,matched=0,kmdheights[256],otherheights[256]; uint256 hash,txids[256]; char symbol[KOMODO_ASSETCHAIN_MAXLEN],base[KOMODO_ASSETCHAIN_MAXLEN]; uint16_t vouts[256]; int8_t baseids[256]; uint8_t *script,opcode,rmd160s[256*20]; uint64_t total,available,deposited,issued,withdrawn,approved,redeemed,checktoshis,seed; int64_t values[256],srcvalues[256]; struct pax_transaction *pax; struct komodo_state *sp; activation = 235300; if ( *(int32_t *)&array[0] == 0 ) numbanned = komodo_bannedset(&indallvouts,array,(int32_t)(sizeof(array)/sizeof(*array))); @@ -1036,8 +1037,6 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 //printf("komodo_opreturn skip %s\n",ASSETCHAINS_SYMBOL); return("assetchain"); } - //else if ( KOMODO_PAX == 0 ) - // return("nopax"); memset(baseids,0xff,sizeof(baseids)); memset(values,0,sizeof(values)); memset(srcvalues,0,sizeof(srcvalues)); @@ -1048,8 +1047,11 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 if ( opretbuf[0] == 'K' && opretlen != 40 ) { komodo_kvupdate(opretbuf,opretlen,value); + return("kv"); } - else if ( opretbuf[0] == 'D' ) + else if ( ASSETCHAINS_SYMBOL[0] == 0 && KOMODO_PAX == 0 ) + return("nopax"); + if ( opretbuf[0] == 'D' ) { tokomodo = 0; if ( opretlen == 38 ) // any KMD tx @@ -1147,7 +1149,9 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 { if ( baseids[i] < 0 ) { - printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]); + static uint32_t counter; + if ( counter++ < 0 ) + printf("%d of %d illegal baseid.%d, this can be ignored\n",i,n,baseids[i]); continue; } bitcoin_address(coinaddr,60,&rmd160s[i*20],20); @@ -1349,12 +1353,281 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 return(typestr); } +int32_t komodo_parsestatefiledata(struct komodo_state *sp,uint8_t *filedata,long *fposp,long datalen,char *symbol,char *dest); + +void komodo_stateind_set(struct komodo_state *sp,uint32_t *inds,int32_t n,uint8_t *filedata,long datalen,char *symbol,char *dest) +{ + uint8_t func; long lastK,lastT,lastN,lastV,fpos,lastfpos; int32_t i,count,doissue,iter,numn,numv,numN,numV,numR; uint32_t tmp,prevpos100,offset; + count = numR = numN = numV = numn = numv = 0; + lastK = lastT = lastN = lastV = -1; + for (iter=0; iter<2; iter++) + { + for (lastfpos=fpos=prevpos100=i=0; i> 8); + fpos = prevpos100 + offset; + if ( lastfpos >= datalen || (filedata[lastfpos] != func && func != 0) ) + printf("i.%d/n.%d lastfpos.%ld >= datalen.%ld or [%d] != func.%d\n",i,n,lastfpos,datalen,filedata[lastfpos],func); + else if ( iter == 0 ) + { + switch ( func ) + { + default: case 'P': case 'U': case 'D': + inds[i] &= 0xffffff00; + break; + case 'K': + lastK = lastfpos; + inds[i] &= 0xffffff00; + break; + case 'T': + lastT = lastfpos; + inds[i] &= 0xffffff00; + break; + case 'N': + lastN = lastfpos; + numN++; + break; + case 'V': + lastV = lastfpos; + numV++; + break; + case 'R': + numR++; + break; + } + } + else + { + doissue = 0; + if ( func == 'K' ) + { + if ( lastK == lastfpos ) + doissue = 1; + } + else if ( func == 'T' ) + { + if ( lastT == lastfpos ) + doissue = 1; + } + else if ( func == 'N' ) + { + if ( numn > numN-128 ) + doissue = 1; + numn++; + } + else if ( func == 'V' ) + { + if ( KOMODO_PAX != 0 && numv > numV-1440 ) + doissue = 1; + numv++; + } + else if ( func == 'R' ) + doissue = 1; + if ( doissue != 0 ) + { + //printf("issue %c total.%d lastfpos.%ld\n",func,count,lastfpos); + komodo_parsestatefiledata(sp,filedata,&lastfpos,datalen,symbol,dest); + count++; + } + } + } + lastfpos = fpos; + } + } + printf("numR.%d numV.%d numN.%d count.%d\n",numR,numV,numN,count); + /*else if ( func == 'K' ) // KMD height: stop after 1st + else if ( func == 'T' ) // KMD height+timestamp: stop after 1st + + else if ( func == 'N' ) // notarization, scan backwards 1440+ blocks; + else if ( func == 'V' ) // price feed: can stop after 1440+ + else if ( func == 'R' ) // opreturn:*/ +} + +void *OS_loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep) +{ + FILE *fp; + long filesize,buflen = *allocsizep; + uint8_t *buf = *bufp; + *lenp = 0; + if ( (fp= fopen(fname,"rb")) != 0 ) + { + fseek(fp,0,SEEK_END); + filesize = ftell(fp); + if ( filesize == 0 ) + { + fclose(fp); + *lenp = 0; + printf("OS_loadfile null size.(%s)\n",fname); + return(0); + } + if ( filesize > buflen ) + { + *allocsizep = filesize; + *bufp = buf = (uint8_t *)realloc(buf,(long)*allocsizep+64); + } + rewind(fp); + if ( buf == 0 ) + printf("Null buf ???\n"); + else + { + if ( fread(buf,1,(long)filesize,fp) != (unsigned long)filesize ) + printf("error reading filesize.%ld\n",(long)filesize); + buf[filesize] = 0; + } + fclose(fp); + *lenp = filesize; + //printf("loaded.(%s)\n",buf); + } //else printf("OS_loadfile couldnt load.(%s)\n",fname); + return(buf); +} + +uint8_t *OS_fileptr(long *allocsizep,char *fname) +{ + long filesize = 0; uint8_t *buf = 0; void *retptr; + *allocsizep = 0; + retptr = OS_loadfile(fname,&buf,&filesize,allocsizep); + return((uint8_t *)retptr); +} + +long komodo_stateind_validate(struct komodo_state *sp,char *indfname,uint8_t *filedata,long datalen,uint32_t *prevpos100p,uint32_t *indcounterp,char *symbol,char *dest) +{ + FILE *fp; long fsize,lastfpos=0,fpos=0; uint8_t *inds,func; int32_t i,n; uint32_t offset,tmp,prevpos100 = 0; + *indcounterp = *prevpos100p = 0; + if ( (inds= OS_fileptr(&fsize,indfname)) != 0 ) + { + lastfpos = 0; + fprintf(stderr,"inds.%p validate %s fsize.%ld datalen.%ld n.%ld lastfpos.%ld\n",inds,indfname,fsize,datalen,fsize / sizeof(uint32_t),lastfpos); + if ( (fsize % sizeof(uint32_t)) == 0 ) + { + n = (int32_t)(fsize / sizeof(uint32_t)); + for (i=0; i n-10 ) + printf("%d: tmp.%08x [%c] prevpos100.%u\n",i,tmp,tmp&0xff,prevpos100); + if ( (i % 100) == 0 ) + prevpos100 = tmp; + else + { + func = (tmp & 0xff); + offset = (tmp >> 8); + fpos = prevpos100 + offset; + if ( lastfpos >= datalen || filedata[lastfpos] != func ) + { + printf("validate.%d error (%u %d) prev100 %u -> fpos.%ld datalen.%ld [%d] (%c) vs (%c) lastfpos.%ld\n",i,offset,func,prevpos100,fpos,datalen,lastfpos < datalen ? filedata[lastfpos] : -1,func,filedata[lastfpos],lastfpos); + return(-1); + } + } + lastfpos = fpos; + } + *indcounterp = n; + *prevpos100p = prevpos100; + if ( sp != 0 ) + komodo_stateind_set(sp,(uint32_t *)inds,n,filedata,fpos,symbol,dest); + //printf("free inds.%p %s validated[%d] fpos.%ld datalen.%ld, offset %ld vs fsize.%ld\n",inds,indfname,i,fpos,datalen,i * sizeof(uint32_t),fsize); + free(inds); + return(fpos); + } else printf("wrong filesize %s %ld\n",indfname,fsize); + } + free(inds); + fprintf(stderr,"indvalidate return -1\n"); + return(-1); +} + +long komodo_indfile_update(FILE *indfp,uint32_t *prevpos100p,long lastfpos,long newfpos,uint8_t func,uint32_t *indcounterp) +{ + uint32_t tmp; + if ( indfp != 0 ) + { + tmp = ((uint32_t)(newfpos - *prevpos100p) << 8) | (func & 0xff); + if ( ftell(indfp)/sizeof(uint32_t) != *indcounterp ) + printf("indfp fpos %ld -> ind.%ld vs counter.%u\n",ftell(indfp),ftell(indfp)/sizeof(uint32_t),*indcounterp); + //fprintf(stderr,"ftell.%ld indcounter.%u lastfpos.%ld newfpos.%ld func.%02x\n",ftell(indfp),*indcounterp,lastfpos,newfpos,func); + fwrite(&tmp,1,sizeof(tmp),indfp), (*indcounterp)++; + if ( (*indcounterp % 100) == 0 ) + { + *prevpos100p = (uint32_t)newfpos; + fwrite(prevpos100p,1,sizeof(*prevpos100p),indfp), (*indcounterp)++; + } + } + return(newfpos); +} + +int32_t komodo_faststateinit(struct komodo_state *sp,char *fname,char *symbol,char *dest) +{ + FILE *indfp; char indfname[1024]; uint8_t *filedata; long validated=-1,datalen,fpos,lastfpos; uint32_t tmp,prevpos100,indcounter,starttime; int32_t func,finished = 0; + starttime = (uint32_t)time(NULL); + safecopy(indfname,fname,sizeof(indfname)-4); + strcat(indfname,".ind"); + if ( (filedata= OS_fileptr(&datalen,fname)) != 0 ) + { + if ( 1 )//datalen >= (1LL << 32) || GetArg("-genind",0) != 0 || (validated= komodo_stateind_validate(0,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest)) < 0 ) + { + lastfpos = fpos = 0; + indcounter = prevpos100 = 0; + if ( (indfp= fopen(indfname,"wb")) != 0 ) + fwrite(&prevpos100,1,sizeof(prevpos100),indfp), indcounter++; + fprintf(stderr,"processing %s %ldKB, validated.%ld\n",fname,datalen/1024,validated); + while ( (func= komodo_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest)) >= 0 ) + { + lastfpos = komodo_indfile_update(indfp,&prevpos100,lastfpos,fpos,func,&indcounter); + } + if ( indfp != 0 ) + { + fclose(indfp); + if ( (fpos= komodo_stateind_validate(0,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest)) < 0 ) + printf("unexpected komodostate.ind validate failure %s datalen.%ld\n",indfname,datalen); + else printf("%s validated fpos.%ld\n",indfname,fpos); + } + finished = 1; + fprintf(stderr,"took %d seconds to process %s %ldKB\n",(int32_t)(time(NULL)-starttime),fname,datalen/1024); + } + else if ( validated > 0 ) + { + if ( (indfp= fopen(indfname,"rb+")) != 0 ) + { + lastfpos = fpos = validated; + fprintf(stderr,"datalen.%ld validated %ld -> indcounter %u, prevpos100 %u offset.%ld\n",datalen,validated,indcounter,prevpos100,indcounter * sizeof(uint32_t)); + if ( fpos < datalen ) + { + fseek(indfp,indcounter * sizeof(uint32_t),SEEK_SET); + if ( ftell(indfp) == indcounter * sizeof(uint32_t) ) + { + while ( (func= komodo_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest)) >= 0 ) + { + lastfpos = komodo_indfile_update(indfp,&prevpos100,lastfpos,fpos,func,&indcounter); + if ( lastfpos != fpos ) + fprintf(stderr,"unexpected lastfpos.%ld != %ld\n",lastfpos,fpos); + } + } + fclose(indfp); + } + if ( komodo_stateind_validate(sp,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest) < 0 ) + printf("unexpected komodostate.ind validate failure %s datalen.%ld\n",indfname,datalen); + else + { + printf("%s validated updated from validated.%ld to %ld new.[%ld] -> indcounter %u, prevpos100 %u offset.%ld | elapsed %d seconds\n",indfname,validated,fpos,fpos-validated,indcounter,prevpos100,indcounter * sizeof(uint32_t),(int32_t)(time(NULL) - starttime)); + finished = 1; + } + } + } else printf("komodo_faststateinit unexpected case\n"); + free(filedata); + return(finished == 1); + } + return(-1); +} + void komodo_passport_iteration() { - static long lastpos[34]; static char userpass[33][1024]; static uint32_t lasttime; - int32_t maxseconds = 30; - FILE *fp; int32_t baseid,n,ht,isrealtime,expired,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[16],dest[16]; uint32_t buf[3],starttime; cJSON *infoobj,*result; uint64_t RTmask = 0; - //printf("PASSPORT.(%s)\n",ASSETCHAINS_SYMBOL); + static long lastpos[34]; static char userpass[33][1024]; static uint32_t lasttime,callcounter; + int32_t maxseconds = 10; + FILE *fp; uint8_t *filedata; long fpos,datalen,lastfpos; int32_t baseid,limit,n,ht,isrealtime,expired,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; uint32_t buf[3],starttime; cJSON *infoobj,*result; uint64_t RTmask = 0; expired = 0; while ( KOMODO_INITDONE == 0 ) { @@ -1365,10 +1638,12 @@ void komodo_passport_iteration() if ( ASSETCHAINS_SYMBOL[0] == 0 ) { refid = 33; + limit = 10000000; jumblr_iteration(); } else { + limit = 10000000; refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0 if ( refid == 0 ) { @@ -1382,13 +1657,9 @@ void komodo_passport_iteration() return; }*/ starttime = (uint32_t)time(NULL); - if ( starttime == lasttime ) - { - usleep(1000); - return; - } + if ( callcounter++ < 1 ) + limit = 10000; lasttime = starttime; - //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid); for (baseid=32; baseid>=0; baseid--) { if ( time(NULL) >= starttime+maxseconds ) @@ -1396,26 +1667,39 @@ void komodo_passport_iteration() sp = 0; isrealtime = 0; base = (char *)CURRENCIES[baseid]; - if ( baseid+1 != refid ) + //printf("PASSPORT %s baseid+1 %d refid.%d\n",ASSETCHAINS_SYMBOL,baseid+1,refid); + if ( baseid+1 != refid ) // only need to import state from a different coin { - if ( baseid == 32 || ASSETCHAINS_SYMBOL[0] == 0 ) + if ( baseid == 32 ) // only care about KMD's state { refsp->RTmask &= ~(1LL << baseid); komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate"); komodo_nameset(symbol,dest,base); sp = komodo_stateptrget(symbol); n = 0; - if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 ) + if ( lastpos[baseid] == 0 && (filedata= OS_fileptr(&datalen,fname)) != 0 ) + { + fpos = 0; + fprintf(stderr,"%s processing %s %ldKB\n",ASSETCHAINS_SYMBOL,fname,datalen/1024); + while ( komodo_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest) >= 0 ) + lastfpos = fpos; + fprintf(stderr,"%s took %d seconds to process %s %ldKB\n",ASSETCHAINS_SYMBOL,(int32_t)(time(NULL)-starttime),fname,datalen/1024); + lastpos[baseid] = lastfpos; + free(filedata), filedata = 0; + datalen = 0; + } + else if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 ) { fseek(fp,0,SEEK_END); + //fprintf(stderr,"couldnt OS_fileptr(%s), freading %ldKB\n",fname,ftell(fp)/1024); if ( ftell(fp) > lastpos[baseid] ) { if ( ASSETCHAINS_SYMBOL[0] != 0 ) printf("%s passport refid.%d %s fname.(%s) base.%s %ld %ld\n",ASSETCHAINS_SYMBOL,refid,symbol,fname,base,ftell(fp),lastpos[baseid]); fseek(fp,lastpos[baseid],SEEK_SET); - while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 && n < 1000 ) + while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 && n < limit ) { - if ( n == 999 ) + if ( n == limit-1 ) { if ( time(NULL) < starttime+maxseconds ) n = 0; @@ -1428,11 +1712,11 @@ void komodo_passport_iteration() n++; } lastpos[baseid] = ftell(fp); - if ( lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 ) + if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 ) printf("from.(%s) lastpos[%s] %ld isrt.%d\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid],komodo_isrealtime(&ht)); } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp)); fclose(fp); - } //else printf("error.(%s) %p\n",fname,sp); + } else fprintf(stderr,"load error.(%s) %p\n",fname,sp); komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime"); if ( (fp= fopen(fname,"rb")) != 0 ) { diff --git a/src/komodo_globals.h b/src/komodo_globals.h index 6d8d6c325bf..144064d7ac1 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -13,6 +13,8 @@ * * ******************************************************************************/ +#include "komodo_defs.h" + void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t kheight,uint32_t ktime,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout); void komodo_init(int32_t height); int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp); @@ -30,6 +32,7 @@ pthread_mutex_t komodo_mutex; #define KOMODO_ELECTION_GAP 2000 //((ASSETCHAINS_SYMBOL[0] == 0) ? 2000 : 100) #define IGUANA_MAXSCRIPTSIZE 10001 +#define KOMODO_ASSETCHAIN_MAXLEN 65 struct pax_transaction *PAX; int32_t NUM_PRICES; uint32_t *PVALS; @@ -41,18 +44,18 @@ struct komodo_state KOMODO_STATES[34]; int COINBASE_MATURITY = _COINBASE_MATURITY;//100; int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE,KOMODO_PAX,KOMODO_EXCHANGEWALLET,KOMODO_REWIND; -int32_t KOMODO_LASTMINED,prevKOMODO_LASTMINED; +int32_t KOMODO_LASTMINED,prevKOMODO_LASTMINED,JUMBLR_PAUSE; std::string NOTARY_PUBKEY,ASSETCHAINS_NOTARIES; uint8_t NOTARY_PUBKEY33[33]; -char ASSETCHAINS_SYMBOL[16]; +char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; uint16_t ASSETCHAINS_PORT; uint32_t ASSETCHAIN_INIT; uint32_t ASSETCHAINS_MAGIC = 2387029918; uint64_t ASSETCHAINS_SUPPLY = 10; uint32_t KOMODO_INITDONE; -char KMDUSERPASS[4096],BTCUSERPASS[4096]; uint16_t BITCOIND_PORT = 7771; +char KMDUSERPASS[4096],BTCUSERPASS[4096]; uint16_t KMD_PORT = 7771,BITCOIND_PORT = 7771; uint64_t PENDING_KOMODO_TX; struct komodo_kv *KOMODO_KV; diff --git a/src/komodo_interest.h b/src/komodo_interest.h index 6355173d803..63d0152ca40 100644 --- a/src/komodo_interest.h +++ b/src/komodo_interest.h @@ -13,13 +13,22 @@ * * ******************************************************************************/ +#include "komodo_defs.h" + +#define SATOSHIDEN ((uint64_t)100000000L) +#define dstr(x) ((double)(x) / SATOSHIDEN) + +#define KOMODO_ENDOFERA 7777777 #define KOMODO_INTEREST ((uint64_t)(0.05 * COIN)) // 5% int64_t MAX_MONEY = 200000000 * 100000000LL; +#ifdef notanymore uint64_t komodo_earned_interest(int32_t height,int64_t paidinterest) { static uint64_t *interests; static int32_t maxheight; - uint64_t total; int32_t ind,incr = 100000; + uint64_t total; int32_t ind,incr = 10000; + // need to make interests persistent before 2030, or just hardfork interest/mining rewards disable after MAX_MONEY is exceeded + return(0); if ( height >= maxheight ) { if ( interests == 0 ) @@ -36,16 +45,20 @@ uint64_t komodo_earned_interest(int32_t height,int64_t paidinterest) } ind = (height << 1); if ( paidinterest < 0 ) // request + { return(interests[ind]); + } else { - if ( interests[ind + 1] != paidinterest ) + if ( interests[ind + 1] != paidinterest ) // need to handle skips like at 80000 { + //fprintf(stderr,"interests.%d %.8f %.8f vs paidinterest %.8f\n",height,dstr(interests[ind]),dstr(interests[ind+1]),dstr(paidinterest)); interests[ind + 1] = paidinterest; - if ( height == 0 ) - interests[ind] = interests[ind + 1]; - else interests[ind] = interests[ind - 2] + interests[ind + 1]; - total = interests[ind]; + if ( height <= 1 ) + interests[ind] = 0; + else interests[ind] = interests[ind - 2] + interests[ind - 1]; + total = interests[ind] + paidinterest; + //fprintf(stderr,"reset interests[height.%d to maxheight.%d] <- %.8f\n",height,maxheight,dstr(total)); for (++height; height nLockTime && (minutes= (tiptime - nLockTime) / 60) >= 60 ) + { + if ( minutes > 365 * 24 * 60 ) + minutes = 365 * 24 * 60; + minutes -= 59; + interest = ((nValue / 10512000) * minutes); + } + return(interest); +} + +uint64_t komodo_interestnew(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime) +{ + uint64_t interest = 0; + if ( txheight < KOMODO_ENDOFERA && nLockTime >= LOCKTIME_THRESHOLD && tiptime != 0 && nLockTime < tiptime && nValue >= 10*COIN ) //komodo_moneysupply(txheight) < MAX_MONEY && + interest = _komodo_interestnew(nValue,nLockTime,tiptime); + return(interest); +} uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime) { - int32_t minutes,exception; uint64_t numerator,denominator,interest = 0; uint32_t activation; + int32_t minutes,exception; uint64_t interestnew,numerator,denominator,interest = 0; uint32_t activation; activation = 1491350400; // 1491350400 5th April if ( ASSETCHAINS_SYMBOL[0] != 0 ) return(0); - if ( komodo_moneysupply(txheight) < MAX_MONEY && nLockTime >= LOCKTIME_THRESHOLD && tiptime != 0 && nLockTime < tiptime && nValue >= 10*COIN ) + if ( txheight >= KOMODO_ENDOFERA ) + return(0); + if ( nLockTime >= LOCKTIME_THRESHOLD && tiptime != 0 && nLockTime < tiptime && nValue >= 10*COIN ) //komodo_moneysupply(txheight) < MAX_MONEY && { if ( (minutes= (tiptime - nLockTime) / 60) >= 60 ) { @@ -109,13 +147,24 @@ uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uin numerator = (nValue / 20); // assumes 5%! if ( txheight < 250000 ) interest = (numerator / denominator); - else interest = (numerator * minutes) / ((uint64_t)365 * 24 * 60); + else if ( txheight < 1000000 ) + { + interest = (numerator * minutes) / ((uint64_t)365 * 24 * 60); + interestnew = _komodo_interestnew(nValue,nLockTime,tiptime); + if ( interest < interestnew ) + printf("path0 current interest %.8f vs new %.8f for ht.%d %.8f locktime.%u tiptime.%u\n",dstr(interest),dstr(interestnew),txheight,dstr(nValue),nLockTime,tiptime); + } + else interest = _komodo_interestnew(nValue,nLockTime,tiptime); } - else + else if ( txheight < 1000000 ) { numerator = (nValue * KOMODO_INTEREST); interest = (numerator / denominator) / COIN; + interestnew = _komodo_interestnew(nValue,nLockTime,tiptime); + if ( interest < interestnew ) + printf("path0 current interest %.8f vs new %.8f for ht.%d %.8f locktime.%u tiptime.%u\n",dstr(interest),dstr(interestnew),txheight,dstr(nValue),nLockTime,tiptime); } + else interest = _komodo_interestnew(nValue,nLockTime,tiptime); } else { @@ -132,12 +181,16 @@ uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uin interest = (numerator / denominator) / COIN; else interest = ((numerator * minutes) / ((uint64_t)365 * 24 * 60)) / COIN; } - else + else if ( txheight < 1000000 ) { numerator = (nValue / 20); // assumes 5%! interest = ((numerator * minutes) / ((uint64_t)365 * 24 * 60)); //fprintf(stderr,"interest %llu %.8f <- numerator.%llu minutes.%d\n",(long long)interest,(double)interest/COIN,(long long)numerator,(int32_t)minutes); + interestnew = _komodo_interestnew(nValue,nLockTime,tiptime); + if ( interest < interestnew )//|| (interestnew < 0.9999*interest && (interest-interestnew) > 50000) ) + printf("path1 current interest %.8f vs new %.8f for ht.%d %.8f locktime.%u tiptime.%u\n",dstr(interest),dstr(interestnew),txheight,dstr(nValue),nLockTime,tiptime); } + else interest = _komodo_interestnew(nValue,nLockTime,tiptime); } if ( 0 && numerator == (nValue * KOMODO_INTEREST) ) fprintf(stderr,"komodo_interest.%d %lld %.8f nLockTime.%u tiptime.%u minutes.%d interest %lld %.8f (%llu / %llu) prod.%llu\n",txheight,(long long)nValue,(double)nValue/COIN,nLockTime,tiptime,minutes,(long long)interest,(double)interest/COIN,(long long)numerator,(long long)denominator,(long long)(numerator * minutes)); diff --git a/src/komodo_jumblr.h b/src/komodo_jumblr.h index 96d225c4697..951ed9de0d4 100755 --- a/src/komodo_jumblr.h +++ b/src/komodo_jumblr.h @@ -24,6 +24,11 @@ z_sendmany "fromaddress" [{"address":... ,"amount":..., "memo":""},...] ( minconf ) ( fee ) */ +#ifdef _WIN32 +#include +#endif +#include "komodo_defs.h" + #define JUMBLR_ADDR "RGhxXpXSSBTBm9EvNsXnTQczthMCxHX91t" #define JUMBLR_BTCADDR "18RmTJe9qMech8siuhYfMtHo8RtcN1obC6" #define JUMBLR_MAXSECRETADDRS 777 @@ -78,7 +83,7 @@ char *jumblr_importaddress(char *address) { char params[1024]; sprintf(params,"[\"%s\", \"%s\", false]",address,address); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"importaddress",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"importaddress",params,BITCOIND_PORT)); } char *jumblr_validateaddress(char *addr) @@ -86,7 +91,7 @@ char *jumblr_validateaddress(char *addr) char params[1024]; sprintf(params,"[\"%s\"]",addr); printf("validateaddress.%s\n",params); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"validateaddress",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"validateaddress",params,BITCOIND_PORT)); } int32_t Jumblr_secretaddrfind(char *searchaddr) @@ -137,7 +142,7 @@ int32_t Jumblr_depositaddradd(char *depositaddr) // external { if ( (retjson= cJSON_Parse(retstr)) != 0 ) { - if ( (ismine= jobj(retjson,(char *)"ismine")) != 0 && is_cJSON_True(ismine) != 0 ) + if ( (ismine= jobj(retjson,(char *)"ismine")) != 0 && cJSON_IsTrue(ismine) != 0 ) { retval = 0; safecopy(Jumblr_deposit,depositaddr,sizeof(Jumblr_deposit)); @@ -145,7 +150,7 @@ int32_t Jumblr_depositaddradd(char *depositaddr) // external else { retval = JUMBLR_ERROR_NOTINWALLET; - printf("%s not in wallet: ismine.%p %d %s\n",depositaddr,ismine,is_cJSON_True(ismine),jprint(retjson,0)); + printf("%s not in wallet: ismine.%p %d %s\n",depositaddr,ismine,cJSON_IsTrue(ismine),jprint(retjson,0)); } free_json(retjson); } @@ -155,6 +160,16 @@ int32_t Jumblr_depositaddradd(char *depositaddr) // external return(retval); } +#ifdef _WIN32 +void OS_randombytes(unsigned char *x,long xlen) +{ + HCRYPTPROV prov = 0; + CryptAcquireContextW(&prov, NULL, NULL,PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT); + CryptGenRandom(prov, xlen, x); + CryptReleaseContext(prov, 0); +} +#endif + int32_t Jumblr_secretaddr(char *secretaddr) { uint32_t r; @@ -207,28 +222,28 @@ char *jumblr_zgetnewaddress() { char params[1024]; sprintf(params,"[]"); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getnewaddress",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getnewaddress",params,BITCOIND_PORT)); } char *jumblr_zlistoperationids() { char params[1024]; sprintf(params,"[]"); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_listoperationids",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_listoperationids",params,BITCOIND_PORT)); } char *jumblr_zgetoperationresult(char *opid) { char params[1024]; sprintf(params,"[[\"%s\"]]",opid); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getoperationresult",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getoperationresult",params,BITCOIND_PORT)); } char *jumblr_zgetoperationstatus(char *opid) { char params[1024]; sprintf(params,"[[\"%s\"]]",opid); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getoperationstatus",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getoperationstatus",params,BITCOIND_PORT)); } char *jumblr_sendt_to_z(char *taddr,char *zaddr,double amount) @@ -238,7 +253,7 @@ char *jumblr_sendt_to_z(char *taddr,char *zaddr,double amount) return(clonestr((char *)"{\"error\":\"illegal address in t to z\"}")); sprintf(params,"[\"%s\", [{\"address\":\"%s\",\"amount\":%.8f}, {\"address\":\"%s\",\"amount\":%.8f}], 1, %.8f]",taddr,zaddr,amount-fee-JUMBLR_TXFEE,JUMBLR_ADDR,fee,JUMBLR_TXFEE); printf("t -> z: %s\n",params); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_sendmany",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_sendmany",params,BITCOIND_PORT)); } char *jumblr_sendz_to_z(char *zaddrS,char *zaddrD,double amount) @@ -249,7 +264,7 @@ char *jumblr_sendz_to_z(char *zaddrS,char *zaddrD,double amount) //sprintf(params,"[\"%s\", [{\"address\":\"%s\",\"amount\":%.8f}, {\"address\":\"%s\",\"amount\":%.8f}], 1, %.8f]",zaddrS,zaddrD,amount-fee-JUMBLR_TXFEE,JUMBLR_ADDR,fee,JUMBLR_TXFEE); sprintf(params,"[\"%s\", [{\"address\":\"%s\",\"amount\":%.8f}], 1, %.8f]",zaddrS,zaddrD,amount-fee-JUMBLR_TXFEE,JUMBLR_TXFEE); printf("z -> z: %s\n",params); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_sendmany",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_sendmany",params,BITCOIND_PORT)); } char *jumblr_sendz_to_t(char *zaddr,char *taddr,double amount) @@ -259,56 +274,56 @@ char *jumblr_sendz_to_t(char *zaddr,char *taddr,double amount) return(clonestr((char *)"{\"error\":\"illegal address in z to t\"}")); sprintf(params,"[\"%s\", [{\"address\":\"%s\",\"amount\":%.8f}, {\"address\":\"%s\",\"amount\":%.8f}], 1, %.8f]",zaddr,taddr,amount-fee-JUMBLR_TXFEE,JUMBLR_ADDR,fee,JUMBLR_TXFEE); printf("z -> t: %s\n",params); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_sendmany",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_sendmany",params,BITCOIND_PORT)); } char *jumblr_zlistaddresses() { char params[1024]; sprintf(params,"[]"); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_listaddresses",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_listaddresses",params,BITCOIND_PORT)); } char *jumblr_zlistreceivedbyaddress(char *addr) { char params[1024]; sprintf(params,"[\"%s\", 1]",addr); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_listreceivedbyaddress",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_listreceivedbyaddress",params,BITCOIND_PORT)); } char *jumblr_getreceivedbyaddress(char *addr) { char params[1024]; sprintf(params,"[\"%s\", 1]",addr); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"getreceivedbyaddress",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"getreceivedbyaddress",params,BITCOIND_PORT)); } char *jumblr_importprivkey(char *wifstr) { char params[1024]; sprintf(params,"[\"%s\", \"\", false]",wifstr); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"importprivkey",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"importprivkey",params,BITCOIND_PORT)); } char *jumblr_zgetbalance(char *addr) { char params[1024]; sprintf(params,"[\"%s\", 1]",addr); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getbalance",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getbalance",params,BITCOIND_PORT)); } char *jumblr_listunspent(char *coinaddr) { char params[1024]; sprintf(params,"[1, 99999999, [\"%s\"]]",coinaddr); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"listunspent",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"listunspent",params,BITCOIND_PORT)); } char *jumblr_gettransaction(char *txidstr) { char params[1024]; sprintf(params,"[\"%s\", 1]",txidstr); - return(jumblr_issuemethod(KMDUSERPASS,(char *)"getrawtransaction",params,7771)); + return(jumblr_issuemethod(KMDUSERPASS,(char *)"getrawtransaction",params,BITCOIND_PORT)); } int32_t jumblr_numvins(bits256 txid) @@ -352,7 +367,7 @@ int64_t jumblr_balance(char *addr) //printf("jumblr.[%s].(%s)\n","KMD",retstr); if ( (retjson= cJSON_Parse(retstr)) != 0 ) { - if ( (n= cJSON_GetArraySize(retjson)) > 0 && is_cJSON_Array(retjson) != 0 ) + if ( (n= cJSON_GetArraySize(retjson)) > 0 && cJSON_IsArray(retjson) != 0 ) for (i=0; i 0 && is_cJSON_Array(array) != 0 ) + if ( (n= cJSON_GetArraySize(array)) > 0 && cJSON_IsArray(array) != 0 ) { //printf("%s -> n%d\n",retstr,n); for (i=0; i 0 && is_cJSON_Array(array) != 0 ) + if ( (n= cJSON_GetArraySize(array)) > 0 && cJSON_IsArray(array) != 0 ) { for (i=0; i= SATOSHIDEN * ((JUMBLR_INCR + 3*fee)*100 + 3*JUMBLR_TXFEE) ) amount = SATOSHIDEN * ((JUMBLR_INCR + 3*fee)*100 + 3*JUMBLR_TXFEE); @@ -769,4 +790,3 @@ void jumblr_iteration() break; } } - diff --git a/src/komodo_kv.h b/src/komodo_kv.h index e121a793e2d..0d86fadd02a 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -16,6 +16,8 @@ #ifndef H_KOMODOKV_H #define H_KOMODOKV_H +#include "komodo_defs.h" + int32_t komodo_kvcmp(uint8_t *refvalue,uint16_t refvaluesize,uint8_t *value,uint16_t valuesize) { if ( refvalue == 0 && value == 0 ) @@ -106,7 +108,9 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) key = &opretbuf[13]; if ( keylen+13 > opretlen ) { - printf("komodo_kvupdate: keylen.%d + 13 > opretlen.%d\n",keylen,opretlen); + static uint32_t counter; + if ( ++counter < 1 ) + printf("komodo_kvupdate: keylen.%d + 13 > opretlen.%d, this can be ignored\n",keylen,opretlen); return; } valueptr = &key[keylen]; diff --git a/src/komodo_notary.h b/src/komodo_notary.h index e47638c8e74..a306f848faf 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -13,6 +13,10 @@ * * ******************************************************************************/ +#include "komodo_defs.h" + +#include "komodo_cJSON.h" + #define KOMODO_MAINNET_START 178999 const char *Notaries_genesis[][2] = @@ -170,7 +174,7 @@ int32_t komodo_ratify_threshold(int32_t height,uint64_t signedmask) int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height) { int32_t i,htind,n; uint64_t mask = 0; struct knotary_entry *kp,*tmp; - if ( height >= 180000 ) + if ( height >= 180000 || ASSETCHAINS_SYMBOL[0] != 0 ) { n = (int32_t)(sizeof(Notaries_elected)/sizeof(*Notaries_elected)); for (i=0; i %d nHeight\n",notarized_height,nHeight); return; } + if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) + printf("[%s] komodo_notarized_update nHeight.%d notarized_height.%d\n",ASSETCHAINS_SYMBOL,nHeight,notarized_height); portable_mutex_lock(&komodo_mutex); sp->NPOINTS = (struct notarized_checkpoint *)realloc(sp->NPOINTS,(sp->NUM_NPOINTS+1) * sizeof(*sp->NPOINTS)); np = &sp->NPOINTS[sp->NUM_NPOINTS++]; @@ -303,7 +309,7 @@ void komodo_notarized_update(struct komodo_state *sp,int32_t nHeight,int32_t not //struct komodo_state *komodo_stateptr(char *symbol,char *dest); int32_t komodo_notarized_height(uint256 *hashp,uint256 *txidp) { - char symbol[16],dest[16]; struct komodo_state *sp; + char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) { *hashp = sp->NOTARIZED_HASH; @@ -320,26 +326,57 @@ int32_t komodo_notarized_height(uint256 *hashp,uint256 *txidp) int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp) { - struct notarized_checkpoint *np = 0; int32_t i; char symbol[16],dest[16]; struct komodo_state *sp; + struct notarized_checkpoint *np = 0; int32_t i=0,flag = 0; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) { if ( sp->NUM_NPOINTS > 0 ) { - for (i=0; iNUM_NPOINTS; i++) + flag = 0; + if ( sp->last_NPOINTSi < sp->NUM_NPOINTS && sp->last_NPOINTSi > 0 ) { - if ( sp->NPOINTS[i].nHeight >= nHeight ) - break; - np = &sp->NPOINTS[i]; + np = &sp->NPOINTS[sp->last_NPOINTSi-1]; + if ( np->nHeight < nHeight ) + { + for (i=sp->last_NPOINTSi; iNUM_NPOINTS; i++) + { + if ( sp->NPOINTS[i].nHeight >= nHeight ) + { + //printf("flag.1 i.%d np->ht %d [%d].ht %d >= nHeight.%d, last.%d num.%d\n",i,np->nHeight,i,sp->NPOINTS[i].nHeight,nHeight,sp->last_NPOINTSi,sp->NUM_NPOINTS); + flag = 1; + break; + } + np = &sp->NPOINTS[i]; + sp->last_NPOINTSi = i; + } + } + } + if ( flag == 0 ) + { + np = 0; + for (i=0; iNUM_NPOINTS; i++) + { + if ( sp->NPOINTS[i].nHeight >= nHeight ) + { + //printf("i.%d np->ht %d [%d].ht %d >= nHeight.%d\n",i,np->nHeight,i,sp->NPOINTS[i].nHeight,nHeight); + break; + } + np = &sp->NPOINTS[i]; + sp->last_NPOINTSi = i; + } } } if ( np != 0 ) { + //char str[65],str2[65]; printf("[%s] notarized_ht.%d\n",ASSETCHAINS_SYMBOL,np->notarized_height); + if ( np->nHeight >= nHeight || (i < sp->NUM_NPOINTS && np[1].nHeight < nHeight) ) + printf("warning: flag.%d i.%d np->ht %d [1].ht %d >= nHeight.%d\n",flag,i,np->nHeight,np[1].nHeight,nHeight); *notarized_hashp = np->notarized_hash; *notarized_desttxidp = np->notarized_desttxid; return(np->notarized_height); } } memset(notarized_hashp,0,sizeof(*notarized_hashp)); + memset(notarized_desttxidp,0,sizeof(*notarized_desttxidp)); return(0); } diff --git a/src/komodo_pax.h b/src/komodo_pax.h index 55b14bd4aa1..bcf6fa5d7e2 100644 --- a/src/komodo_pax.h +++ b/src/komodo_pax.h @@ -13,6 +13,8 @@ * * ******************************************************************************/ +#include "komodo_defs.h" + #define USD 0 #define EUR 1 #define JPY 2 @@ -233,7 +235,7 @@ int32_t komodo_pax_opreturn(int32_t height,uint8_t *opret,int32_t maxsize) { static uint32_t lastcrc; FILE *fp; char fname[512]; uint32_t crc32,check,timestamp; int32_t i,n=0,retval,fsize,len=0; uint8_t data[8192]; -#ifdef WIN32 +#ifdef _WIN32 sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodofeed"); #else sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodofeed"); @@ -638,7 +640,11 @@ uint64_t komodo_paxprice(uint64_t *seedp,int32_t height,char *base,char *rel,uin if ( ASSETCHAINS_SYMBOL[0] == 0 && chainActive.Tip() != 0 && height > chainActive.Tip()->nHeight ) { if ( height < 100000000 ) - printf("komodo_paxprice height.%d vs tip.%d\n",height,chainActive.Tip()->nHeight); + { + static uint32_t counter; + if ( counter++ < 3 ) + printf("komodo_paxprice height.%d vs tip.%d\n",height,chainActive.Tip()->nHeight); + } return(0); } *seedp = komodo_seed(height); diff --git a/src/komodo_structs.h b/src/komodo_structs.h index a35e7e008a8..40212c3e3d6 100644 --- a/src/komodo_structs.h +++ b/src/komodo_structs.h @@ -13,10 +13,12 @@ * * ******************************************************************************/ +#include "komodo_defs.h" + #include "uthash.h" #include "utlist.h" -/*#ifdef WIN32 +/*#ifdef _WIN32 #define PACKED #else #define PACKED __attribute__((packed)) @@ -40,6 +42,7 @@ #define KOMODO_KVPROTECTED 1 #define KOMODO_KVBINARY 2 #define KOMODO_KVDURATION 1440 +#define KOMODO_ASSETCHAIN_MAXLEN 65 union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; }; typedef union _bits256 bits256; @@ -60,7 +63,7 @@ struct komodo_event uint16_t len; int32_t height; uint8_t type,reorged; - char symbol[16]; + char symbol[KOMODO_ASSETCHAIN_MAXLEN]; uint8_t space[]; }; @@ -71,7 +74,7 @@ struct pax_transaction uint64_t komodoshis,fiatoshis,validated; int32_t marked,height,otherheight,approved,didstats,ready; uint16_t vout; - char symbol[16],source[16],coinaddr[64]; uint8_t rmd160[20],type,buf[35]; + char symbol[KOMODO_ASSETCHAIN_MAXLEN],source[KOMODO_ASSETCHAIN_MAXLEN],coinaddr[64]; uint8_t rmd160[20],type,buf[35]; }; struct knotary_entry { UT_hash_handle hh; uint8_t pubkey[33],notaryid; }; @@ -84,7 +87,7 @@ struct komodo_state int32_t SAVEDHEIGHT,CURRENT_HEIGHT,NOTARIZED_HEIGHT; uint32_t SAVEDTIMESTAMP; uint64_t deposited,issued,withdrawn,approved,redeemed,shorted; - struct notarized_checkpoint *NPOINTS; int32_t NUM_NPOINTS; + struct notarized_checkpoint *NPOINTS; int32_t NUM_NPOINTS,last_NPOINTSi; struct komodo_event **Komodo_events; int32_t Komodo_numevents; uint32_t RTbufs[64][3]; uint64_t RTmask; }; diff --git a/src/komodo_utils.h b/src/komodo_utils.h index 7cf081638ef..ef64ad75c1c 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -12,6 +12,13 @@ * Removal or modification of this copyright notice is prohibited. * * * ******************************************************************************/ +#include "komodo_defs.h" + +#ifdef _WIN32 +#include +#include +#include +#endif #define SATOSHIDEN ((uint64_t)100000000L) #define dstr(x) ((double)(x) / SATOSHIDEN) @@ -108,13 +115,13 @@ static inline int32_t sha256_vcompress(struct sha256_vstate * md,uint8_t *buf) LOAD32H(W[i],buf + (4*i)); for (i=16; i<64; i++) // fill W[16..63] W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; - + #define RND(a,b,c,d,e,f,g,h,i,ki) \ t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ t1 = Sigma0(a) + Maj(a, b, c); \ d += t0; \ h = t0 + t1; - + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf); @@ -345,19 +352,19 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) { uint32_t aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,X[16]; int i; - + /* load words X */ for (i = 0; i < 16; i++){ LOAD32L(X[i], buf + (4 * i)); } - + /* load state */ aa = aaa = md->state[0]; bb = bbb = md->state[1]; cc = ccc = md->state[2]; dd = ddd = md->state[3]; ee = eee = md->state[4]; - + /* round 1 */ FF(aa, bb, cc, dd, ee, X[ 0], 11); FF(ee, aa, bb, cc, dd, X[ 1], 14); @@ -375,7 +382,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) FF(cc, dd, ee, aa, bb, X[13], 7); FF(bb, cc, dd, ee, aa, X[14], 9); FF(aa, bb, cc, dd, ee, X[15], 8); - + /* round 2 */ GG(ee, aa, bb, cc, dd, X[ 7], 7); GG(dd, ee, aa, bb, cc, X[ 4], 6); @@ -393,7 +400,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) GG(bb, cc, dd, ee, aa, X[14], 7); GG(aa, bb, cc, dd, ee, X[11], 13); GG(ee, aa, bb, cc, dd, X[ 8], 12); - + /* round 3 */ HH(dd, ee, aa, bb, cc, X[ 3], 11); HH(cc, dd, ee, aa, bb, X[10], 13); @@ -411,7 +418,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) HH(aa, bb, cc, dd, ee, X[11], 12); HH(ee, aa, bb, cc, dd, X[ 5], 7); HH(dd, ee, aa, bb, cc, X[12], 5); - + /* round 4 */ II(cc, dd, ee, aa, bb, X[ 1], 11); II(bb, cc, dd, ee, aa, X[ 9], 12); @@ -429,7 +436,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) II(ee, aa, bb, cc, dd, X[ 5], 6); II(dd, ee, aa, bb, cc, X[ 6], 5); II(cc, dd, ee, aa, bb, X[ 2], 12); - + /* round 5 */ JJ(bb, cc, dd, ee, aa, X[ 4], 9); JJ(aa, bb, cc, dd, ee, X[ 0], 15); @@ -447,7 +454,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) JJ(dd, ee, aa, bb, cc, X[ 6], 8); JJ(cc, dd, ee, aa, bb, X[15], 5); JJ(bb, cc, dd, ee, aa, X[13], 6); - + /* parallel round 1 */ JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8); JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9); @@ -465,7 +472,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14); JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12); JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6); - + /* parallel round 2 */ III(eee, aaa, bbb, ccc, ddd, X[ 6], 9); III(ddd, eee, aaa, bbb, ccc, X[11], 13); @@ -483,7 +490,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) III(bbb, ccc, ddd, eee, aaa, X[ 9], 15); III(aaa, bbb, ccc, ddd, eee, X[ 1], 13); III(eee, aaa, bbb, ccc, ddd, X[ 2], 11); - + /* parallel round 3 */ HHH(ddd, eee, aaa, bbb, ccc, X[15], 9); HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7); @@ -501,7 +508,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13); HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7); HHH(ddd, eee, aaa, bbb, ccc, X[13], 5); - + /* parallel round 4 */ GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15); GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5); @@ -519,7 +526,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5); GGG(ddd, eee, aaa, bbb, ccc, X[10], 15); GGG(ccc, ddd, eee, aaa, bbb, X[14], 8); - + /* parallel round 5 */ FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8); FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5); @@ -537,7 +544,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13); FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11); FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11); - + /* combine results */ ddd += cc + md->state[1]; /* final result for md->state[0] */ md->state[1] = md->state[2] + dd + eee; @@ -545,7 +552,7 @@ static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) md->state[3] = md->state[4] + aa + bbb; md->state[4] = md->state[0] + bb + ccc; md->state[0] = ddd; - + return 0; } @@ -622,10 +629,10 @@ int rmd160_vdone(struct rmd160_vstate * md, unsigned char *out) } /* increase the length of the message */ md->length += md->curlen * 8; - + /* append the '1' bit */ md->buf[md->curlen++] = (unsigned char)0x80; - + /* if the length is currently above 56 bytes we append zeros * then compress. Then we can fall back to padding zeros and length * encoding like normal. @@ -724,13 +731,13 @@ static const uint32_t crc32_tab[] = { uint32_t calc_crc32(uint32_t crc,const void *buf,size_t size) { const uint8_t *p; - + p = (const uint8_t *)buf; crc = crc ^ ~0U; - + while (size--) crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); - + return crc ^ ~0U; } @@ -1106,6 +1113,7 @@ double OS_milliseconds() return(millis); } +#ifndef _WIN32 void OS_randombytes(unsigned char *x,long xlen) { static int fd = -1; @@ -1135,6 +1143,7 @@ void OS_randombytes(unsigned char *x,long xlen) xlen -= i; } } +#endif void lock_queue(queue_t *queue) { @@ -1181,7 +1190,11 @@ void *queue_delete(queue_t *queue,struct queueitem *copy,int32_t copysize) { DL_FOREACH(queue->list,item) { + #ifdef _WIN32 + if ( item == copy || (item->allocsize == copysize && memcmp((void *)((intptr_t)item + sizeof(struct queueitem)),(void *)((intptr_t)copy + sizeof(struct queueitem)),copysize) == 0) ) + #else if ( item == copy || (item->allocsize == copysize && memcmp((void *)((long)item + sizeof(struct queueitem)),(void *)((long)copy + sizeof(struct queueitem)),copysize) == 0) ) + #endif { DL_DELETE(queue->list,item); portable_mutex_unlock(&queue->mutex); @@ -1250,9 +1263,9 @@ void iguana_initQ(queue_t *Q,char *name) free(item); } -void komodo_userpass(char *username,char *password,FILE *fp) +uint16_t komodo_userpass(char *username,char *password,FILE *fp) { - char *rpcuser,*rpcpassword,*str,line[8192]; + char *rpcuser,*rpcpassword,*str,line[8192]; uint16_t port = 0; rpcuser = rpcpassword = 0; username[0] = password[0] = 0; while ( fgets(line,sizeof(line),fp) != 0 ) @@ -1264,6 +1277,11 @@ void komodo_userpass(char *username,char *password,FILE *fp) rpcuser = parse_conf_line(str,(char *)"rpcuser"); else if ( (str= strstr(line,(char *)"rpcpassword")) != 0 ) rpcpassword = parse_conf_line(str,(char *)"rpcpassword"); + else if ( (str= strstr(line,(char *)"rpcport")) != 0 ) + { + port = atoi(parse_conf_line(str,(char *)"rpcport")); + //printf("rpcport.%u in file\n",port); + } } if ( rpcuser != 0 && rpcpassword != 0 ) { @@ -1275,6 +1293,7 @@ void komodo_userpass(char *username,char *password,FILE *fp) free(rpcuser); if ( rpcpassword != 0 ) free(rpcpassword); + return(port); } void komodo_statefname(char *fname,char *symbol,char *str) @@ -1294,7 +1313,7 @@ void komodo_statefname(char *fname,char *symbol,char *str) } else { -#ifdef WIN32 +#ifdef _WIN32 strcat(fname,"\\"); #else strcat(fname,"/"); @@ -1304,7 +1323,7 @@ void komodo_statefname(char *fname,char *symbol,char *str) { strcat(fname,symbol); //printf("statefname.(%s) -> (%s)\n",symbol,fname); -#ifdef WIN32 +#ifdef _WIN32 strcat(fname,"\\"); #else strcat(fname,"/"); @@ -1317,7 +1336,7 @@ void komodo_statefname(char *fname,char *symbol,char *str) void komodo_configfile(char *symbol,uint16_t port) { static char myusername[512],mypassword[8192]; - FILE *fp; uint8_t buf2[33]; char fname[512],buf[128],username[512],password[8192]; uint32_t crc,r,r2,i; + FILE *fp; uint16_t kmdport; uint8_t buf2[33]; char fname[512],buf[128],username[512],password[8192]; uint32_t crc,r,r2,i; if ( symbol != 0 && port != 0 ) { r = (uint32_t)time(NULL); @@ -1326,13 +1345,17 @@ void komodo_configfile(char *symbol,uint16_t port) memcpy(&buf[sizeof(r)],&r2,sizeof(r2)); memcpy(&buf[sizeof(r)+sizeof(r2)],symbol,strlen(symbol)); crc = calc_crc32(0,(uint8_t *)buf,(int32_t)(sizeof(r)+sizeof(r2)+strlen(symbol))); + #ifdef _WIN32 + randombytes_buf(buf2,sizeof(buf2)); + #else OS_randombytes(buf2,sizeof(buf2)); + #endif for (i=0; i userpass.(%s)\n",fname,KMDUSERPASS); } else printf("couldnt open.(%s)\n",fname); } -int32_t komodo_userpass(char *userpass,char *symbol) +uint16_t komodo_userpass(char *userpass,char *symbol) { - FILE *fp; char fname[512],username[512],password[512],confname[16]; + FILE *fp; uint16_t port = 0; char fname[512],username[512],password[512],confname[KOMODO_ASSETCHAIN_MAXLEN]; userpass[0] = 0; if ( strcmp("KMD",symbol) == 0 ) { @@ -1394,12 +1418,12 @@ int32_t komodo_userpass(char *userpass,char *symbol) komodo_statefname(fname,symbol,confname); if ( (fp= fopen(fname,"rb")) != 0 ) { - komodo_userpass(username,password,fp); + port = komodo_userpass(username,password,fp); sprintf(userpass,"%s:%s",username,password); fclose(fp); return((int32_t)strlen(userpass)); } - return(-1); + return(port); } uint32_t komodo_assetmagic(char *symbol,uint64_t supply) @@ -1460,7 +1484,7 @@ char *iguanafmtstr = (char *)"curl --url \"http://127.0.0.1:7778\" --data \"{\\\ int32_t komodo_whoami(char *pubkeystr,int32_t height) { - int32_t i,notaryid; + int32_t i,notaryid; for (i=0; i<33; i++) sprintf(&pubkeystr[i<<1],"%02x",NOTARY_PUBKEY33[i]); pubkeystr[66] = 0; @@ -1468,10 +1492,20 @@ int32_t komodo_whoami(char *pubkeystr,int32_t height) return(notaryid); } -void komodo_args() +char *argv0suffix[] = +{ + (char *)"mnzd", (char *)"mnz-cli", (char *)"mnzd.exe", (char *)"mnz-cli.exe" +}; + +char *argv0names[] = +{ + (char *)"MNZ", (char *)"MNZ", (char *)"MNZ", (char *)"MNZ" +}; + +void komodo_args(char *argv0) { extern int64_t MAX_MONEY; - std::string name,addn; char *dirname,fname[512],magicstr[9]; uint8_t magic[4]; FILE *fp; int32_t i,baseid,len; + std::string name,addn; char *dirname,fname[512],arg0str[64],magicstr[9]; uint8_t magic[4]; FILE *fp; int32_t i,baseid,len,n; IS_KOMODO_NOTARY = GetBoolArg("-notary", false); if ( (KOMODO_EXCHANGEWALLET= GetBoolArg("-exchange", false)) != 0 ) fprintf(stderr,"KOMODO_EXCHANGEWALLET mode active\n"); @@ -1482,6 +1516,20 @@ void komodo_args() KOMODO_PAX = 1; } else KOMODO_PAX = GetArg("-pax",0); name = GetArg("-ac_name",""); + if ( argv0 != 0 ) + { + len = (int32_t)strlen(argv0); + for (i=0; i matches suffix (%s) -> ac_name.(%s)\n",argv0,argv0suffix[i],argv0names[i]); + name = argv0names[i]; + break; + } + } + } if ( (KOMODO_REWIND= GetArg("-rewind",0)) != 0 ) { printf("KOMODO_REWIND %d\n",KOMODO_REWIND); @@ -1501,7 +1549,11 @@ void komodo_args() while ( (dirname= (char *)GetDataDir(false).string().c_str()) == 0 || dirname[0] == 0 ) { fprintf(stderr,"waiting for datadir\n"); + #ifndef _WIN32 sleep(3); + #else + boost::this_thread::sleep(boost::posix_time::milliseconds(3000)); + #endif } //fprintf(stderr,"Got datadir.(%s)\n",dirname); if ( ASSETCHAINS_SYMBOL[0] != 0 ) @@ -1510,6 +1562,7 @@ void komodo_args() extern int COINBASE_MATURITY; komodo_configfile(ASSETCHAINS_SYMBOL,ASSETCHAINS_PORT + 1); COINBASE_MATURITY = 1; + LogPrintf("ASSETCHAINS_PORT %s %u\n",ASSETCHAINS_SYMBOL,ASSETCHAINS_PORT); } ASSETCHAINS_NOTARIES = GetArg("-ac_notaries",""); komodo_assetchain_pubkeys((char *)ASSETCHAINS_NOTARIES.c_str()); @@ -1532,12 +1585,12 @@ void komodo_args() for (iter=0; iter<2; iter++) { strcpy(fname,GetDataDir().string().c_str()); -#ifdef WIN32 +#ifdef _WIN32 while ( fname[strlen(fname)-1] != '\\' ) fname[strlen(fname)-1] = 0; if ( iter == 0 ) - strcat(fname,".komodo\\komodo.conf"); - else strcat(fname,".bitcoin\\bitcoin.conf"); + strcat(fname,"Komodo\\komodo.conf"); + else strcat(fname,"Bitcoin\\bitcoin.conf"); #else while ( fname[strlen(fname)-1] != '/' ) fname[strlen(fname)-1] = 0; @@ -1562,6 +1615,7 @@ void komodo_args() break; } } + BITCOIND_PORT = GetArg("-rpcport", BaseParams().RPCPort()); //fprintf(stderr,"%s chain params initialized\n",ASSETCHAINS_SYMBOL); } @@ -1595,6 +1649,3 @@ struct komodo_state *komodo_stateptr(char *symbol,char *dest) komodo_nameset(symbol,dest,ASSETCHAINS_SYMBOL); return(komodo_stateptrget(symbol)); } - - - diff --git a/src/main.cpp b/src/main.cpp index c14fd7995e6..e067490729a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,6 +49,7 @@ using namespace std; */ CCriticalSection cs_main; +extern uint8_t NOTARY_PUBKEY33[33]; BlockMap mapBlockIndex; CChain chainActive; @@ -1489,8 +1490,8 @@ bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex) return true; } -uint64_t komodo_moneysupply(int32_t height); -extern char ASSETCHAINS_SYMBOL[16]; +//uint64_t komodo_moneysupply(int32_t height); +extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; extern uint32_t ASSETCHAINS_MAGIC; extern uint64_t ASSETCHAINS_SUPPLY; @@ -1501,7 +1502,7 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams) { if ( nHeight == 1 ) return(100000000 * COIN); // ICO allocation - else if ( komodo_moneysupply(nHeight) < MAX_MONEY ) + else if ( nHeight < KOMODO_ENDOFERA ) //komodo_moneysupply(nHeight) < MAX_MONEY ) return(3 * COIN); else return(0); } @@ -2364,7 +2365,8 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin return false; control.Add(vChecks); } - komodo_earned_interest(pindex->nHeight,sum); + //if ( ASSETCHAINS_SYMBOL[0] == 0 ) + // komodo_earned_interest(pindex->nHeight,sum); CTxUndo undoDummy; if (i > 0) { blockundo.vtxundo.push_back(CTxUndo()); @@ -4494,7 +4496,14 @@ void static ProcessGetData(CNode* pfrom) else { if (inv.type == MSG_BLOCK) + { + //uint256 hash; int32_t z; + //hash = block.GetHash(); + //for (z=31; z>=0; z--) + // fprintf(stderr,"%02x",((uint8_t *)&hash)[z]); + //fprintf(stderr," send block %d\n",komodo_block2height(&block)); pfrom->PushMessage("block", block); + } else // MSG_FILTERED_BLOCK) { LOCK(pfrom->cs_filter); @@ -4583,6 +4592,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, { const CChainParams& chainparams = Params(); LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id); + //fprintf(stderr, "recv: %s peer=%d\n", SanitizeString(strCommand).c_str(), (int32_t)pfrom->GetId()); if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) { LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n"); @@ -4956,13 +4966,23 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, vector vHeaders; int nLimit = MAX_HEADERS_RESULTS; LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString(), pfrom->id); - for (; pindex; pindex = chainActive.Next(pindex)) + if ( pfrom->lasthdrsreq >= chainActive.Height()-MAX_HEADERS_RESULTS || pfrom->lasthdrsreq != (int32_t)(pindex ? pindex->nHeight : -1) ) { - vHeaders.push_back(pindex->GetBlockHeader()); - if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop) - break; + pfrom->lasthdrsreq = (int32_t)(pindex ? pindex->nHeight : -1); + for (; pindex; pindex = chainActive.Next(pindex)) + { + vHeaders.push_back(pindex->GetBlockHeader()); + if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop) + break; + } + pfrom->PushMessage("headers", vHeaders); + } + else if ( NOTARY_PUBKEY33[0] != 0 ) + { + static uint32_t counter; + if ( counter++ < 3 ) + fprintf(stderr,"you can ignore redundant getheaders from peer.%d %d prev.%d\n",(int32_t)pfrom->id,(int32_t)(pindex ? pindex->nHeight : -1),pfrom->lasthdrsreq); } - pfrom->PushMessage("headers", vHeaders); } @@ -5143,8 +5163,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, // Headers message had its maximum size; the peer may have more headers. // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue // from there instead. - LogPrint("net", "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->id, pfrom->nStartingHeight); - pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexLast), uint256()); + if ( pfrom->sendhdrsreq >= chainActive.Height()-MAX_HEADERS_RESULTS || pindexLast->nHeight != pfrom->sendhdrsreq ) + { + pfrom->sendhdrsreq = (int32_t)pindexLast->nHeight; + LogPrint("net", "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->id, pfrom->nStartingHeight); + pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexLast), uint256()); + } } CheckBlockIndex(); diff --git a/src/metrics.cpp b/src/metrics.cpp index fd8000548b6..b45b5dc6b35 100644 --- a/src/metrics.cpp +++ b/src/metrics.cpp @@ -14,7 +14,12 @@ #include #include #include +#ifdef _WIN32 +#include +#include +#else #include +#endif #include void AtomicTimer::start() @@ -406,11 +411,17 @@ void ThreadShowMetricsScreen() // Get current window size if (isTTY) { - struct winsize w; - w.ws_col = 0; - if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1 && w.ws_col != 0) { - cols = w.ws_col; - } +#ifdef _WIN32 + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + cols = csbi.srWindow.Right - csbi.srWindow.Left + 1; + #else + struct winsize w; + w.ws_col = 0; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1 && w.ws_col != 0) { + cols = w.ws_col; + } +#endif } if (isScreen) { diff --git a/src/miner.cpp b/src/miner.cpp index 721b19cef5a..5c12d0af6fe 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -106,11 +106,10 @@ void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams); } -#define ASSETCHAINS_MINHEIGHT 128 -#define KOMODO_ELECTION_GAP 2000 -#define ROUNDROBIN_DELAY 61 +#include "komodo_defs.h" + extern int32_t ASSETCHAINS_SEED,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAIN_INIT,KOMODO_INITDONE,KOMODO_ON_DEMAND,KOMODO_INITDONE,KOMODO_PASSPORT_INITDONE; -extern char ASSETCHAINS_SYMBOL[16]; +extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; extern std::string NOTARY_PUBKEY; extern uint8_t NOTARY_PUBKEY33[33]; uint32_t Mining_start,Mining_height; diff --git a/src/net.cpp b/src/net.cpp index 073353be513..0dcdae7db81 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -17,7 +17,7 @@ #include "ui_interface.h" #include "crypto/common.h" -#ifdef WIN32 +#ifdef _WIN32 #include #else #include @@ -42,7 +42,7 @@ // Fix for ancient MinGW versions, that don't have defined these in ws2tcpip.h. // Todo: Can be removed when our pull-tester is upgraded to a modern MinGW version. -#ifdef WIN32 +#ifdef _WIN32 #ifndef PROTECTION_LEVEL_UNRESTRICTED #define PROTECTION_LEVEL_UNRESTRICTED 10 #endif @@ -930,7 +930,7 @@ static void AcceptConnection(const ListenSocket& hListenSocket) { // According to the internet TCP_NODELAY is not carried into accepted sockets // on all platforms. Set it again here just to be sure. int set = 1; -#ifdef WIN32 +#ifdef _WIN32 setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int)); #else setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int)); @@ -1727,7 +1727,7 @@ bool BindListenPort(const CService &addrBind, string& strError, bool fWhiteliste } -#ifndef WIN32 +#ifndef _WIN32 #ifdef SO_NOSIGPIPE // Different way of disabling SIGPIPE on BSD setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int)); @@ -1753,13 +1753,13 @@ bool BindListenPort(const CService &addrBind, string& strError, bool fWhiteliste // and enable it by default or not. Try to enable it, if possible. if (addrBind.IsIPv6()) { #ifdef IPV6_V6ONLY -#ifdef WIN32 +#ifdef _WIN32 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int)); #else setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int)); #endif #endif -#ifdef WIN32 +#ifdef _WIN32 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED; setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int)); #endif @@ -1800,7 +1800,7 @@ void static Discover(boost::thread_group& threadGroup) if (!fDiscover) return; -#ifdef WIN32 +#ifdef _WIN32 // Get local host IP char pszHostName[256] = ""; if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR) @@ -1945,7 +1945,7 @@ class CNetCleanup delete pnodeLocalHost; pnodeLocalHost = NULL; -#ifdef WIN32 +#ifdef _WIN32 // Shutdown Windows Sockets WSACleanup(); #endif diff --git a/src/net.h b/src/net.h index f7ebf11f619..6f28f3872eb 100644 --- a/src/net.h +++ b/src/net.h @@ -22,7 +22,7 @@ #include #include -#ifndef WIN32 +#ifndef _WIN32 #include #endif @@ -264,6 +264,7 @@ class CNode std::string addrName; CService addrLocal; int nVersion; + int lasthdrsreq,sendhdrsreq; // strSubVer is whatever byte array we read from the wire. However, this field is intended // to be printed out, displayed to humans in various forms and so on. So we sanitize it and // store the sanitized version in cleanSubVer. The original should be used when dealing with diff --git a/src/netbase.cpp b/src/netbase.cpp index c65d66ac1e4..6a28532f1eb 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -26,7 +26,7 @@ #include #endif -#ifndef WIN32 +#ifndef _WIN32 #if HAVE_INET_PTON #include #endif @@ -129,7 +129,7 @@ bool static LookupIntern(const char *pszName, std::vector& vIP, unsign aiHint.ai_socktype = SOCK_STREAM; aiHint.ai_protocol = IPPROTO_TCP; aiHint.ai_family = AF_UNSPEC; -#ifdef WIN32 +#ifdef _WIN32 aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST; #else aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST; @@ -454,7 +454,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe #endif //Disable Nagle's algorithm -#ifdef WIN32 +#ifdef _WIN32 setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int)); #else setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int)); @@ -488,7 +488,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe return false; } socklen_t nRetSize = sizeof(nRet); -#ifdef WIN32 +#ifdef _WIN32 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR) #else if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR) @@ -505,7 +505,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe return false; } } -#ifdef WIN32 +#ifdef _WIN32 else if (WSAGetLastError() != WSAEISCONN) #else else @@ -1348,7 +1348,7 @@ bool operator<(const CSubNet& a, const CSubNet& b) return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0)); } -#ifdef WIN32 +#ifdef _WIN32 std::string NetworkErrorString(int err) { char buf[256]; @@ -1386,7 +1386,7 @@ bool CloseSocket(SOCKET& hSocket) { if (hSocket == INVALID_SOCKET) return false; -#ifdef WIN32 +#ifdef _WIN32 int ret = closesocket(hSocket); #else int ret = close(hSocket); @@ -1398,7 +1398,7 @@ bool CloseSocket(SOCKET& hSocket) bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking) { if (fNonBlocking) { -#ifdef WIN32 +#ifdef _WIN32 u_long nOne = 1; if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR) { #else @@ -1409,7 +1409,7 @@ bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking) return false; } } else { -#ifdef WIN32 +#ifdef _WIN32 u_long nZero = 0; if (ioctlsocket(hSocket, FIONBIO, &nZero) == SOCKET_ERROR) { #else diff --git a/src/netbase.h b/src/netbase.h index 21ac0aa4c50..83ce226633b 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -22,7 +22,7 @@ extern bool fNameLookup; /** -timeout default */ static const int DEFAULT_CONNECT_TIMEOUT = 5000; -#ifdef WIN32 +#ifdef _WIN32 // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error #undef SetPort #endif diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index 7573cd63d33..9f4a0eae00b 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -47,13 +47,13 @@ JSDescription JSDescription::Randomized( const uint256& anchor, boost::array& inputs, boost::array& outputs, -#ifdef __APPLE__ + #ifdef __LP64__ boost::array& inputMap, boost::array& outputMap, -#else + #else boost::array& inputMap, boost::array& outputMap, -#endif + #endif CAmount vpub_old, CAmount vpub_new, bool computeProof, @@ -151,7 +151,7 @@ CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::MIN_CURRENT_ CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime), vjoinsplit(tx.vjoinsplit), joinSplitPubKey(tx.joinSplitPubKey), joinSplitSig(tx.joinSplitSig) { - + } uint256 CMutableTransaction::GetHash() const diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index b40f2d03169..699b7a89f59 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -12,7 +12,10 @@ #include "serialize.h" #include "uint256.h" #include "consensus/consensus.h" + +#ifndef __APPLE__ #include +#endif #include @@ -87,13 +90,13 @@ class JSDescription const uint256& rt, boost::array& inputs, boost::array& outputs, -#ifdef __APPLE__ + #ifdef __LP64__ boost::array& inputMap, boost::array& outputMap, -#else + #else boost::array& inputMap, boost::array& outputMap, -#endif + #endif CAmount vpub_old, CAmount vpub_new, bool computeProof = true, // Set to false in some tests diff --git a/src/protocol.cpp b/src/protocol.cpp index dd855aa33aa..33b9ee77315 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -8,7 +8,7 @@ #include "util.h" #include "utilstrencodings.h" -#ifndef WIN32 +#ifndef _WIN32 # include #endif diff --git a/src/purge b/src/purge new file mode 100755 index 00000000000..08928e92281 --- /dev/null +++ b/src/purge @@ -0,0 +1,5 @@ +rm -rf ~/.komodo/$1/chainstate +rm -rf ~/.komodo/$1/database +rm -rf ~/.komodo/$1/blocks +rm -rf ~/.komodo/$1/komodostate + diff --git a/src/random.cpp b/src/random.cpp index 29faa32a77b..87b3f648e02 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -6,7 +6,7 @@ #include "random.h" #include "support/cleanse.h" -#ifdef WIN32 +#ifdef _WIN32 #include "compat.h" // for Windows API #endif #include "serialize.h" // for begin_ptr(vec) @@ -15,7 +15,7 @@ #include -#ifndef WIN32 +#ifndef _WIN32 #include #endif @@ -24,7 +24,7 @@ static inline int64_t GetPerformanceCounter() { int64_t nCounter = 0; -#ifdef WIN32 +#ifdef _WIN32 QueryPerformanceCounter((LARGE_INTEGER*)&nCounter); #else timeval t; diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index e4574860cd9..10f61776579 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -47,7 +47,7 @@ double GetDifficultyINTERNAL(const CBlockIndex* blockindex, bool networkDifficul int nShiftAmount = (powLimit >> 24) & 0xff; double dDiff = - (double)(powLimit & 0x00ffffff) / + (double)(powLimit & 0x00ffffff) / (double)(bits & 0x00ffffff); while (nShift < nShiftAmount) @@ -309,7 +309,7 @@ UniValue getblockhash(const UniValue& params, bool fHelp) return pblockindex->GetBlockHash().GetHex(); } -uint256 _komodo_getblockhash(int32_t nHeight) +/*uint256 _komodo_getblockhash(int32_t nHeight) { uint256 hash; LOCK(cs_main); @@ -323,7 +323,7 @@ uint256 _komodo_getblockhash(int32_t nHeight) printf(" blockhash.%d\n",nHeight); } else memset(&hash,0,sizeof(hash)); return(hash); -} +}*/ UniValue getblockheader(const UniValue& params, bool fHelp) { @@ -512,10 +512,12 @@ UniValue gettxoutsetinfo(const UniValue& params, bool fHelp) return ret; } +#include "komodo_defs.h" + #define IGUANA_MAXSCRIPTSIZE 10001 #define KOMODO_KVDURATION 1440 #define KOMODO_KVBINARY 2 -extern char ASSETCHAINS_SYMBOL[16]; +extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime); uint32_t komodo_txtime(uint256 hash); uint64_t komodo_paxprice(uint64_t *seedp,int32_t height,char *base,char *rel,uint64_t basevolume); @@ -606,14 +608,14 @@ UniValue minerids(const UniValue& params, bool fHelp) for (j=0; j<33; j++) sprintf(&hexstr[j*2],"%02x",pubkeys[i][j]); item.push_back(Pair("notaryid", i)); - + bitcoin_address(kmdaddr,60,pubkeys[i],33); m = (int32_t)strlen(kmdaddr); kmdaddress.resize(m); ptr = (char *)kmdaddress.data(); memcpy(ptr,kmdaddr,m); item.push_back(Pair("KMDaddress", kmdaddress)); - + item.push_back(Pair("pubkey", hex)); item.push_back(Pair("blocks", tally[i])); a.push_back(item); @@ -775,7 +777,7 @@ UniValue paxprices(const UniValue& params, bool fHelp) else { CBlockIndex *pblockindex = chainActive[heights[i]]; - + item.push_back(Pair("t", (int64_t)pblockindex->nTime)); item.push_back(Pair("p", (double)prices[i] / COIN)); a.push_back(item); @@ -976,7 +978,11 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp) ZCIncrementalMerkleTree tree; pcoinsTip->GetAnchorAt(pcoinsTip->GetBestAnchor(), tree); + #ifdef __APPLE__ + obj.push_back(Pair("commitments", (uint64_t)tree.size())); + #else obj.push_back(Pair("commitments", tree.size())); + #endif const Consensus::Params& consensusParams = Params().GetConsensus(); CBlockIndex* tip = chainActive.Tip(); @@ -1055,7 +1061,9 @@ UniValue getchaintips(const UniValue& params, bool fHelp) setTips.insert(item.second); BOOST_FOREACH(const PAIRTYPE(const uint256, CBlockIndex*)& item, mapBlockIndex) { - const CBlockIndex* pprev = item.second->pprev; + const CBlockIndex* pprev=0; + if ( item.second != 0 ) + pprev = item.second->pprev; if (pprev) setTips.erase(pprev); } @@ -1064,40 +1072,43 @@ UniValue getchaintips(const UniValue& params, bool fHelp) setTips.insert(chainActive.Tip()); /* Construct the output array. */ - UniValue res(UniValue::VARR); + UniValue res(UniValue::VARR); const CBlockIndex *forked; BOOST_FOREACH(const CBlockIndex* block, setTips) - { - UniValue obj(UniValue::VOBJ); - obj.push_back(Pair("height", block->nHeight)); - obj.push_back(Pair("hash", block->phashBlock->GetHex())); - - const int branchLen = block->nHeight - chainActive.FindFork(block)->nHeight; - obj.push_back(Pair("branchlen", branchLen)); - - string status; - if (chainActive.Contains(block)) { - // This block is part of the currently active chain. - status = "active"; - } else if (block->nStatus & BLOCK_FAILED_MASK) { - // This block or one of its ancestors is invalid. - status = "invalid"; - } else if (block->nChainTx == 0) { - // This block cannot be connected because full block data for it or one of its parents is missing. - status = "headers-only"; - } else if (block->IsValid(BLOCK_VALID_SCRIPTS)) { - // This block is fully validated, but no longer part of the active chain. It was probably the active block once, but was reorganized. - status = "valid-fork"; - } else if (block->IsValid(BLOCK_VALID_TREE)) { - // The headers for this block are valid, but it has not been validated. It was probably never part of the most-work chain. - status = "valid-headers"; - } else { - // No clue. - status = "unknown"; + BOOST_FOREACH(const CBlockIndex* block, setTips) + { + UniValue obj(UniValue::VOBJ); + obj.push_back(Pair("height", block->nHeight)); + obj.push_back(Pair("hash", block->phashBlock->GetHex())); + forked = chainActive.FindFork(block); + if ( forked != 0 ) + { + const int branchLen = block->nHeight - forked->nHeight; + obj.push_back(Pair("branchlen", branchLen)); + + string status; + if (chainActive.Contains(block)) { + // This block is part of the currently active chain. + status = "active"; + } else if (block->nStatus & BLOCK_FAILED_MASK) { + // This block or one of its ancestors is invalid. + status = "invalid"; + } else if (block->nChainTx == 0) { + // This block cannot be connected because full block data for it or one of its parents is missing. + status = "headers-only"; + } else if (block->IsValid(BLOCK_VALID_SCRIPTS)) { + // This block is fully validated, but no longer part of the active chain. It was probably the active block once, but was reorganized. + status = "valid-fork"; + } else if (block->IsValid(BLOCK_VALID_TREE)) { + // The headers for this block are valid, but it has not been validated. It was probably never part of the most-work chain. + status = "valid-headers"; + } else { + // No clue. + status = "unknown"; + } + obj.push_back(Pair("status", status)); + } + res.push_back(obj); } - obj.push_back(Pair("status", status)); - - res.push_back(obj); - } return res; } diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index 42307470f46..6008e341dbe 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -47,7 +47,7 @@ uint64_t komodo_interestsum(); int32_t komodo_longestchain(); int32_t komodo_notarized_height(uint256 *hashp,uint256 *txidp); int32_t komodo_whoami(char *pubkeystr,int32_t height); -extern int32_t KOMODO_LASTMINED; +extern int32_t KOMODO_LASTMINED,JUMBLR_PAUSE; extern char ASSETCHAINS_SYMBOL[]; int32_t notarizedtxid_height(char *dest,char *txidstr,int32_t *kmdnotarized_heightp); #define KOMODO_VERSION "0.1.1" @@ -224,6 +224,26 @@ UniValue jumblr_secret(const UniValue& params, bool fHelp) return(result); } +UniValue jumblr_pause(const UniValue& params, bool fHelp) +{ + int32_t retval; UniValue result(UniValue::VOBJ); + if (fHelp ) + throw runtime_error("jumblr_pause\n"); + JUMBLR_PAUSE = 1; + result.push_back(Pair("result", "paused")); + return(result); +} + +UniValue jumblr_resume(const UniValue& params, bool fHelp) +{ + int32_t retval; UniValue result(UniValue::VOBJ); + if (fHelp ) + throw runtime_error("jumblr_resume\n"); + JUMBLR_PAUSE = 0; + result.push_back(Pair("result", "resumed")); + return(result); +} + UniValue validateaddress(const UniValue& params, bool fHelp) { if (fHelp || params.size() != 1) diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index 54f49798c59..dac9d49e84d 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -22,6 +22,8 @@ #include "wallet/wallet.h" #endif +#include "komodo_defs.h" + #include #include @@ -144,7 +146,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) out.push_back(Pair("value", ValueFromAmount(txout.nValue))); if ( pindex != 0 && tx.nLockTime != 0 && (tipindex= chainActive.Tip()) != 0 ) { - extern char ASSETCHAINS_SYMBOL[16]; + extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; interest = komodo_interest(pindex->nHeight,txout.nValue,tx.nLockTime,tipindex->nTime); if ( 0 && strcmp("REVS",ASSETCHAINS_SYMBOL) == 0 ) fprintf(stderr,"TxtoJSON interest %llu %.8f (%d %llu %u %u)\n",(long long)interest,(double)interest/COIN,(int32_t)pindex->nHeight,(long long)txout.nValue,(uint32_t)tx.nLockTime,(int32_t)tipindex->nTime); diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 518ba4693da..94521004de4 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -338,6 +338,8 @@ static const CRPCCommand vRPCCommands[] = { "util", "z_validateaddress", &z_validateaddress, true }, /* uses wallet if enabled */ { "util", "jumblr_deposit", &jumblr_deposit, true }, { "util", "jumblr_secret", &jumblr_secret, true }, + { "util", "jumblr_pause", &jumblr_pause, true }, + { "util", "jumblr_resume", &jumblr_resume, true }, /* Not shown in help */ { "hidden", "invalidateblock", &invalidateblock, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index ecf7b0573fd..f5997247244 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -248,6 +248,8 @@ extern UniValue zc_sample_joinsplit(const UniValue& params, bool fHelp); extern UniValue jumblr_deposit(const UniValue& params, bool fHelp); extern UniValue jumblr_secret(const UniValue& params, bool fHelp); +extern UniValue jumblr_pause(const UniValue& params, bool fHelp); +extern UniValue jumblr_resume(const UniValue& params, bool fHelp); extern UniValue getrawtransaction(const UniValue& params, bool fHelp); // in rcprawtransaction.cpp extern UniValue listunspent(const UniValue& params, bool fHelp); diff --git a/src/support/pagelocker.cpp b/src/support/pagelocker.cpp index 440e0a5193a..d0b0d4dd8c2 100644 --- a/src/support/pagelocker.cpp +++ b/src/support/pagelocker.cpp @@ -8,7 +8,7 @@ #include "config/bitcoin-config.h" #endif -#ifdef WIN32 +#ifdef _WIN32 #ifdef _WIN32_WINNT #undef _WIN32_WINNT #endif @@ -49,7 +49,7 @@ static inline size_t GetSystemPageSize() bool MemoryPageLocker::Lock(const void* addr, size_t len) { -#ifdef WIN32 +#ifdef _WIN32 return VirtualLock(const_cast(addr), len) != 0; #else return mlock(addr, len) == 0; @@ -58,7 +58,7 @@ bool MemoryPageLocker::Lock(const void* addr, size_t len) bool MemoryPageLocker::Unlock(const void* addr, size_t len) { -#ifdef WIN32 +#ifdef _WIN32 return VirtualUnlock(const_cast(addr), len) != 0; #else return munlock(addr, len) == 0; diff --git a/src/test/alert_tests.cpp b/src/test/alert_tests.cpp index ab3841c4813..ef607daccc7 100644 --- a/src/test/alert_tests.cpp +++ b/src/test/alert_tests.cpp @@ -340,7 +340,7 @@ BOOST_AUTO_TEST_CASE(AlertNotify) // Windows built-in echo semantics are different than posixy shells. Quotes and // whitespace are printed literally. -#ifndef WIN32 +#ifndef _WIN32 BOOST_CHECK_EQUAL(r[0], "Alert 1"); BOOST_CHECK_EQUAL(r[1], "Alert 2, cancels 1"); BOOST_CHECK_EQUAL(r[2], "Alert 2, cancels 1"); diff --git a/src/util.cpp b/src/util.cpp index 4c901d5862d..be75175733f 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -15,6 +15,7 @@ #include "sync.h" #include "utilstrencodings.h" #include "utiltime.h" +#include "komodo_defs.h" #include @@ -23,7 +24,7 @@ #include #endif -#ifndef WIN32 +#ifndef _WIN32 // for posix_fallocate #ifdef __linux__ @@ -335,7 +336,7 @@ void ParseParameters(int argc, const char* const argv[]) strValue = str.substr(is_index+1); str = str.substr(0, is_index); } -#ifdef WIN32 +#ifdef _WIN32 boost::to_lower(str); if (boost::algorithm::starts_with(str, "/")) str = "-" + str.substr(1); @@ -419,7 +420,7 @@ std::string HelpMessageOpt(const std::string &option, const std::string &message static std::string FormatException(const std::exception* pex, const char* pszThread) { -#ifdef WIN32 +#ifdef _WIN32 char pszModule[MAX_PATH] = ""; GetModuleFileNameA(NULL, pszModule, sizeof(pszModule)); #else @@ -441,13 +442,13 @@ void PrintExceptionContinue(const std::exception* pex, const char* pszThread) strMiscWarning = message; } -extern char ASSETCHAINS_SYMBOL[16]; +extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; //int64_t MAX_MONEY = 200000000 * 100000000LL; boost::filesystem::path GetDefaultDataDir() { namespace fs = boost::filesystem; - char symbol[16]; + char symbol[KOMODO_ASSETCHAIN_MAXLEN]; if ( ASSETCHAINS_SYMBOL[0] != 0 ) strcpy(symbol,ASSETCHAINS_SYMBOL); else symbol[0] = 0; @@ -455,7 +456,7 @@ boost::filesystem::path GetDefaultDataDir() // Windows >= Vista: C:\Users\Username\AppData\Roaming\Zcash // Mac: ~/Library/Application Support/Zcash // Unix: ~/.zcash -#ifdef WIN32 +#ifdef _WIN32 // Windows if ( symbol[0] == 0 ) return GetSpecialFolderPath(CSIDL_APPDATA) / "Komodo"; @@ -502,7 +503,7 @@ static boost::filesystem::path ZC_GetBaseParamsDir() // Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams // Mac: ~/Library/Application Support/ZcashParams // Unix: ~/.zcash-params -#ifdef WIN32 +#ifdef _WIN32 // Windows return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams"; #else @@ -604,7 +605,14 @@ boost::filesystem::path GetConfigFile() char confname[512]; if ( ASSETCHAINS_SYMBOL[0] != 0 ) sprintf(confname,"%s.conf",ASSETCHAINS_SYMBOL); - else strcpy(confname,"komodo.conf"); + else + { +#ifdef __APPLE__ + strcpy(confname,"Komodo.conf"); +#else + strcpy(confname,"komodo.conf"); +#endif + } boost::filesystem::path pathConfigFile(GetArg("-conf",confname)); if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile; @@ -636,9 +644,12 @@ void ReadConfigFile(map& mapSettingsRet, } // If datadir is changed in .conf file: ClearDatadirCache(); + extern uint16_t BITCOIND_PORT; + BITCOIND_PORT = GetArg("-rpcport",BaseParams().RPCPort()); + //fprintf(stderr,"from conf file %s RPC %u, used to be %u\n",ASSETCHAINS_SYMBOL,BITCOIND_PORT,BITCOIND_PORT); } -#ifndef WIN32 +#ifndef _WIN32 boost::filesystem::path GetPidFile() { boost::filesystem::path pathPidFile(GetArg("-pid", "komodod.pid")); @@ -659,13 +670,13 @@ void CreatePidFile(const boost::filesystem::path &path, pid_t pid) bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest) { -#ifdef WIN32 +#ifdef _WIN32 return MoveFileExA(src.string().c_str(), dest.string().c_str(), MOVEFILE_REPLACE_EXISTING) != 0; #else int rc = std::rename(src.string().c_str(), dest.string().c_str()); return (rc == 0); -#endif /* WIN32 */ +#endif /* _WIN32 */ } /** @@ -690,7 +701,7 @@ bool TryCreateDirectory(const boost::filesystem::path& p) void FileCommit(FILE *fileout) { fflush(fileout); // harmless if redundantly called -#ifdef WIN32 +#ifdef _WIN32 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout)); FlushFileBuffers(hFile); #else @@ -805,7 +816,7 @@ void ShrinkDebugFile() fclose(file); } -#ifdef WIN32 +#ifdef _WIN32 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate) { namespace fs = boost::filesystem; @@ -828,7 +839,7 @@ boost::filesystem::path GetTempPath() { #else // TODO: remove when we don't support filesystem v2 anymore boost::filesystem::path path; -#ifdef WIN32 +#ifdef _WIN32 char pszPath[MAX_PATH] = ""; if (GetTempPathA(MAX_PATH, pszPath)) @@ -888,7 +899,7 @@ void SetupEnvironment() bool SetupNetworking() { -#ifdef WIN32 +#ifdef _WIN32 // Initialize Windows Sockets WSADATA wsadata; int ret = WSAStartup(MAKEWORD(2,2), &wsadata); @@ -900,15 +911,15 @@ bool SetupNetworking() void SetThreadPriority(int nPriority) { -#ifdef WIN32 +#ifdef _WIN32 SetThreadPriority(GetCurrentThread(), nPriority); -#else // WIN32 +#else // _WIN32 #ifdef PRIO_THREAD setpriority(PRIO_THREAD, 0, nPriority); #else // PRIO_THREAD setpriority(PRIO_PROCESS, 0, nPriority); #endif // PRIO_THREAD -#endif // WIN32 +#endif // _WIN32 } std::string PrivacyInfo() diff --git a/src/util.h b/src/util.h index 361a9271f2d..424c6693a94 100644 --- a/src/util.h +++ b/src/util.h @@ -124,7 +124,7 @@ boost::filesystem::path GetDefaultDataDir(); const boost::filesystem::path &GetDataDir(bool fNetSpecific = true); void ClearDatadirCache(); boost::filesystem::path GetConfigFile(); -#ifndef WIN32 +#ifndef _WIN32 boost::filesystem::path GetPidFile(); void CreatePidFile(const boost::filesystem::path &path, pid_t pid); #endif @@ -133,7 +133,7 @@ class missing_zcash_conf : public std::runtime_error { missing_zcash_conf() : std::runtime_error("Missing komodo.conf") { } }; void ReadConfigFile(std::map& mapSettingsRet, std::map >& mapMultiSettingsRet); -#ifdef WIN32 +#ifdef _WIN32 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true); #endif boost::filesystem::path GetTempPath(); @@ -150,7 +150,7 @@ std::string LicenseInfo(); inline bool IsSwitchChar(char c) { -#ifdef WIN32 +#ifdef _WIN32 return c == '-' || c == '/'; #else return c == '-'; diff --git a/src/wallet/asyncrpcoperation_sendmany.cpp b/src/wallet/asyncrpcoperation_sendmany.cpp index 5befe9fb125..4e8a20be628 100644 --- a/src/wallet/asyncrpcoperation_sendmany.cpp +++ b/src/wallet/asyncrpcoperation_sendmany.cpp @@ -63,15 +63,15 @@ AsyncRPCOperation_sendmany::AsyncRPCOperation_sendmany( if (minDepth < 0) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Minconf cannot be negative"); } - + if (fromAddress.size() == 0) { throw JSONRPCError(RPC_INVALID_PARAMETER, "From address parameter missing"); } - + if (tOutputs.size() == 0 && zOutputs.size() == 0) { throw JSONRPCError(RPC_INVALID_PARAMETER, "No recipients"); } - + fromtaddr_ = CBitcoinAddress(fromAddress); isfromtaddr_ = fromtaddr_.IsValid(); isfromzaddr_ = false; @@ -86,7 +86,7 @@ AsyncRPCOperation_sendmany::AsyncRPCOperation_sendmany( if (!pwalletMain->GetSpendingKey(addr, key)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid from address, no spending key found for zaddr"); } - + isfromzaddr_ = true; frompaymentaddress_ = addr; spendingkey_ = key; @@ -199,13 +199,13 @@ bool AsyncRPCOperation_sendmany::main_impl() { throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Could not find any non-coinbase UTXOs to spend."); } } - } + } } - + if (isfromzaddr_ && !find_unspent_notes()) { throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds, no unspent notes found for zaddr from address."); } - + CAmount t_inputs_total = 0; for (SendManyInputUTXO & t : t_inputs_) { t_inputs_total += std::get<2>(t); @@ -237,7 +237,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { strprintf("Insufficient transparent funds, have %s, need %s", FormatMoney(t_inputs_total), FormatMoney(targetAmount))); } - + if (isfromzaddr_ && (z_inputs_total < targetAmount)) { throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strprintf("Insufficient protected funds, have %s, need %s", @@ -306,18 +306,18 @@ bool AsyncRPCOperation_sendmany::main_impl() { /** * SCENARIO #1 - * + * * taddr -> taddrs - * + * * There are no zaddrs or joinsplits involved. */ if (isPureTaddrOnlyTx) { add_taddr_outputs_to_tx(); - + CAmount funds = selectedUTXOAmount; CAmount fundsSpent = t_outputs_total + minersFee; CAmount change = funds - fundsSpent; - + if (change > 0) { add_taddr_change_output_to_tx(0,change); @@ -326,7 +326,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { FormatMoney(change) ); } - + UniValue obj(UniValue::VOBJ); obj.push_back(Pair("rawtxn", EncodeHexTx(tx_))); sign_send_raw_transaction(obj); @@ -336,7 +336,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { * END SCENARIO #1 */ - + // Prepare raw transaction to handle JoinSplits CMutableTransaction mtx(tx_); mtx.nVersion = 2; @@ -373,10 +373,10 @@ bool AsyncRPCOperation_sendmany::main_impl() { /** * SCENARIO #2 - * + * * taddr -> taddrs * -> zaddrs - * + * * Note: Consensus rule states that coinbase utxos can only be sent to a zaddr. * Local wallet rule does not allow any change when sending coinbase utxos * since there is currently no way to specify a change address and we don't @@ -384,11 +384,11 @@ bool AsyncRPCOperation_sendmany::main_impl() { */ if (isfromtaddr_) { add_taddr_outputs_to_tx(); - + CAmount funds = selectedUTXOAmount; CAmount fundsSpent = t_outputs_total + minersFee + z_outputs_total; CAmount change = funds - fundsSpent; - + if (change > 0) { if (selectedUTXOCoinbase) { assert(isSingleZaddrOutput); @@ -425,7 +425,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { jso.memo = get_memo_from_hex_string(hexMemo); } info.vjsout.push_back(jso); - + // Funds are removed from the value pool and enter the private pool info.vpub_old += value; } @@ -436,21 +436,21 @@ bool AsyncRPCOperation_sendmany::main_impl() { } /** * END SCENARIO #2 - */ - - - + */ + + + /** * SCENARIO #3 - * + * * zaddr -> taddrs * -> zaddrs - * + * * Processing order: * Part 1: taddrs and miners fee - * Part 2: zaddrs + * Part 2: zaddrs */ - + /** * SCENARIO #3 * Part 1: Add to the transparent value pool. @@ -498,7 +498,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { wtxDepth ); - + // Put value back into the value pool if (noteFunds >= taddrTargetAmount) { jsChange = noteFunds - taddrTargetAmount; @@ -516,7 +516,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { if (jsChange > 0) { info.vjsout.push_back(JSOutput()); info.vjsout.push_back(JSOutput(frompaymentaddress_, jsChange)); - + LogPrint("zrpcunsafe", "%s: generating note for change (amount=%s)\n", getId(), FormatMoney(jsChange) @@ -535,13 +535,13 @@ bool AsyncRPCOperation_sendmany::main_impl() { /** * SCENARIO #3 * Part 2: Send to zaddrs by chaining JoinSplits together and immediately consuming any change - */ + */ if (z_outputs_total>0) { - // Keep track of treestate within this transaction + // Keep track of treestate within this transaction boost::unordered_map intermediates; std::vector previousCommitments; - + while (zOutputsDeque.size() > 0) { AsyncJoinSplitInfo info; info.vpub_old = 0; @@ -557,20 +557,20 @@ bool AsyncRPCOperation_sendmany::main_impl() { if (tx_.vjoinsplit.size() > 0) { prevJoinSplit = tx_.vjoinsplit.back(); } - + // If there is no change, the chain has terminated so we can reset the tracked treestate. if (jsChange==0 && tx_.vjoinsplit.size() > 0) { intermediates.clear(); previousCommitments.clear(); } - + // // Consume change as the first input of the JoinSplit. // if (jsChange > 0) { LOCK2(cs_main, pwalletMain->cs_wallet); - // Update tree state with previous joinsplit + // Update tree state with previous joinsplit ZCIncrementalMerkleTree tree; auto it = intermediates.find(prevJoinSplit.anchor); if (it != intermediates.end()) { @@ -578,7 +578,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { } else if (!pcoinsTip->GetAnchorAt(prevJoinSplit.anchor, tree)) { throw JSONRPCError(RPC_WALLET_ERROR, "Could not find previous JoinSplit anchor"); } - + assert(changeOutputIndex != -1); boost::optional changeWitness; int n = 0; @@ -610,9 +610,9 @@ bool AsyncRPCOperation_sendmany::main_impl() { Note note = plaintext.note(frompaymentaddress_); info.notes.push_back(note); - + jsInputValue += plaintext.value; - + LogPrint("zrpcunsafe", "%s: spending change (amount=%s)\n", getId(), FormatMoney(plaintext.value) @@ -623,7 +623,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { } } - + // // Consume spendable non-change notes // @@ -649,9 +649,9 @@ bool AsyncRPCOperation_sendmany::main_impl() { vOutPoints.push_back(jso); vInputNotes.push_back(note); - + jsInputValue += noteFunds; - + int wtxHeight = -1; int wtxDepth = -1; { @@ -670,14 +670,14 @@ bool AsyncRPCOperation_sendmany::main_impl() { wtxDepth ); } - - // Add history of previous commitments to witness + + // Add history of previous commitments to witness if (vInputNotes.size() > 0) { if (vInputWitnesses.size()==0) { throw JSONRPCError(RPC_WALLET_ERROR, "Could not find witness for note commitment"); } - + for (auto & optionalWitness : vInputWitnesses) { if (!optionalWitness) { throw JSONRPCError(RPC_WALLET_ERROR, "Witness for note commitment is null"); @@ -695,18 +695,18 @@ bool AsyncRPCOperation_sendmany::main_impl() { } // The jsAnchor is null if this JoinSplit is at the start of a new chain - if (jsAnchor.IsNull()) { - jsAnchor = inputAnchor; + if (jsAnchor.IsNull()) { + jsAnchor = inputAnchor; } // Add spendable notes as inputs std::copy(vInputNotes.begin(), vInputNotes.end(), std::back_inserter(info.notes)); } - + // // Find recipient to transfer funds to - // + // SendManyRecipient smr = zOutputsDeque.front(); std::string address = std::get<0>(smr); CAmount value = std::get<1>(smr); @@ -722,7 +722,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { } outAmount += minersFee; } - + if (jsInputValue > outAmount) { jsChange = jsInputValue - outAmount; } else if (outAmount > jsInputValue) { @@ -737,12 +737,12 @@ bool AsyncRPCOperation_sendmany::main_impl() { value -= minersFee; } } - + if (!minersFeeProcessed) { minersFeeProcessed = true; info.vpub_new += minersFee; // funds flowing back to public pool } - + // create output for recipient PaymentAddress pa = CZCPaymentAddress(address).Get(); JSOutput jso = JSOutput(pa, value); @@ -750,7 +750,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { jso.memo = get_memo_from_hex_string(hexMemo); } info.vjsout.push_back(jso); - + // create output for any change if (jsChange>0) { info.vjsout.push_back(JSOutput(frompaymentaddress_, jsChange)); @@ -779,7 +779,7 @@ bool AsyncRPCOperation_sendmany::main_impl() { * Raw transaction as hex string should be in object field "rawtxn" */ void AsyncRPCOperation_sendmany::sign_send_raw_transaction(UniValue obj) -{ +{ // Sign the raw transaction UniValue rawtxnValue = find_value(obj, "rawtxn"); if (rawtxnValue.isNull()) { @@ -870,7 +870,7 @@ bool AsyncRPCOperation_sendmany::find_utxos(bool fAcceptCoinbase=false) { if (isCoinbase && fAcceptCoinbase==false) { continue; } - + CAmount nValue = out.tx->vout[out.i].nValue; SendManyInputUTXO utxo(out.tx->GetHash(), out.i, nValue, isCoinbase); t_inputs_.push_back(utxo); @@ -904,7 +904,7 @@ bool AsyncRPCOperation_sendmany::find_unspent_notes() { HexStr(data).substr(0, 10) ); } - + if (z_inputs_.size() == 0) { return false; } @@ -986,13 +986,13 @@ UniValue AsyncRPCOperation_sendmany::perform_joinsplit( {info.vjsin[0], info.vjsin[1]}; boost::array outputs {info.vjsout[0], info.vjsout[1]}; -#ifdef __APPLE__ + #ifdef __LP64__ boost::array inputMap; boost::array outputMap; -#else + #else boost::array inputMap; boost::array outputMap; -#endif + #endif JSDescription jsdesc = JSDescription::Randomized( *pzcashParams, joinSplitPubKey_, @@ -1132,7 +1132,7 @@ void AsyncRPCOperation_sendmany::add_taddr_change_output_to_tx(CBitcoinAddress * boost::array AsyncRPCOperation_sendmany::get_memo_from_hex_string(std::string s) { boost::array memo = {{0x00}}; - + std::vector rawMemo = ParseHex(s.c_str()); // If ParseHex comes across a non-hex char, it will stop but still return results so far. @@ -1140,11 +1140,11 @@ boost::array AsyncRPCOperation_sendmany::get_memo_f if (slen % 2 !=0 || (slen>0 && rawMemo.size()!=slen/2)) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Memo must be in hexadecimal format"); } - + if (rawMemo.size() > ZC_MEMO_SIZE) { throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Memo size of %d is too big, maximum allowed is %d", rawMemo.size(), ZC_MEMO_SIZE)); } - + // copy vector into boost array int lenMemo = rawMemo.size(); for (int i = 0; i < ZC_MEMO_SIZE && i < lenMemo; i++) { @@ -1167,4 +1167,3 @@ UniValue AsyncRPCOperation_sendmany::getStatus() const { obj.push_back(Pair("params", contextinfo_ )); return obj; } - diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index 766e2afdada..c6beb8a5262 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -13,7 +13,7 @@ #include -#ifndef WIN32 +#ifndef _WIN32 #include #endif diff --git a/src/wallet/gtest/test_transaction.cpp b/src/wallet/gtest/test_transaction.cpp new file mode 100755 index 00000000000..e7558868550 --- /dev/null +++ b/src/wallet/gtest/test_transaction.cpp @@ -0,0 +1,110 @@ +#include + +#include "primitives/transaction.h" +#include "zcash/Note.hpp" +#include "zcash/Address.hpp" + +extern ZCJoinSplit* params; +extern int GenZero(int n); +extern int GenMax(int n); + +TEST(Transaction, JSDescriptionRandomized) { + // construct a merkle tree + ZCIncrementalMerkleTree merkleTree; + + libzcash::SpendingKey k = libzcash::SpendingKey::random(); + libzcash::PaymentAddress addr = k.address(); + + libzcash::Note note(addr.a_pk, 100, uint256(), uint256()); + + // commitment from coin + uint256 commitment = note.cm(); + + // insert commitment into the merkle tree + merkleTree.append(commitment); + + // compute the merkle root we will be working with + uint256 rt = merkleTree.root(); + + auto witness = merkleTree.witness(); + + // create JSDescription + uint256 pubKeyHash; + boost::array inputs = { + libzcash::JSInput(witness, note, k), + libzcash::JSInput() // dummy input of zero value + }; + boost::array outputs = { + libzcash::JSOutput(addr, 50), + libzcash::JSOutput(addr, 50) + }; + #ifdef __LP64__ // required for building on MacOS + boost::array inputMap; + boost::array outputMap; + #else + boost::array inputMap; + boost::array outputMap; + #endif + + { + auto jsdesc = JSDescription::Randomized( + *params, pubKeyHash, rt, + inputs, outputs, + inputMap, outputMap, + 0, 0, false); + + #ifdef __LP64__ // required for building on MacOS + std::set inputSet(inputMap.begin(), inputMap.end()); + std::set expectedInputSet {0, 1}; + #else + std::set inputSet(inputMap.begin(), inputMap.end()); + std::set expectedInputSet {0, 1}; + #endif + EXPECT_EQ(expectedInputSet, inputSet); + + #ifdef __LP64__ // required for building on MacOS + std::set outputSet(outputMap.begin(), outputMap.end()); + std::set expectedOutputSet {0, 1}; + #else + std::set outputSet(outputMap.begin(), outputMap.end()); + std::set expectedOutputSet {0, 1}; + #endif + EXPECT_EQ(expectedOutputSet, outputSet); + } + + { + auto jsdesc = JSDescription::Randomized( + *params, pubKeyHash, rt, + inputs, outputs, + inputMap, outputMap, + 0, 0, false, GenZero); + + #ifdef __LP64__ // required for building on MacOS + boost::array expectedInputMap {1, 0}; + boost::array expectedOutputMap {1, 0}; + #else + boost::array expectedInputMap {1, 0}; + boost::array expectedOutputMap {1, 0}; + #endif + EXPECT_EQ(expectedInputMap, inputMap); + EXPECT_EQ(expectedOutputMap, outputMap); + } + + { + auto jsdesc = JSDescription::Randomized( + *params, pubKeyHash, rt, + inputs, outputs, + inputMap, outputMap, + 0, 0, false, GenMax); + + #ifdef __LP64__ // required for building on MacOS + boost::array expectedInputMap {0, 1}; + boost::array expectedOutputMap {0, 1}; + #else + boost::array expectedInputMap {0, 1}; + boost::array expectedOutputMap {0, 1}; + #endif + EXPECT_EQ(expectedInputMap, inputMap); + EXPECT_EQ(expectedOutputMap, outputMap); + } +} diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 61cf68d8fce..dbc3aab848d 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -114,7 +114,7 @@ UniValue getnewaddress(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 1) throw runtime_error( "getnewaddress ( \"account\" )\n" @@ -191,7 +191,7 @@ UniValue getaccountaddress(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() != 1) throw runtime_error( "getaccountaddress \"account\"\n" @@ -223,7 +223,7 @@ UniValue getrawchangeaddress(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 1) throw runtime_error( "getrawchangeaddress\n" @@ -258,7 +258,7 @@ UniValue setaccount(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 1 || params.size() > 2) throw runtime_error( "setaccount \"zcashaddress\" \"account\"\n" @@ -304,7 +304,7 @@ UniValue getaccount(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() != 1) throw runtime_error( "getaccount \"zcashaddress\"\n" @@ -336,7 +336,7 @@ UniValue getaddressesbyaccount(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() != 1) throw runtime_error( "getaddressesbyaccount \"account\"\n" @@ -418,7 +418,7 @@ UniValue sendtoaddress(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 2 || params.size() > 5) throw runtime_error( "sendtoaddress \"zcashaddress\" amount ( \"comment\" \"comment-to\" subtractfeefromamount )\n" @@ -471,6 +471,7 @@ UniValue sendtoaddress(const UniValue& params, bool fHelp) return wtx.GetHash().GetHex(); } +#include "komodo_defs.h" #define KOMODO_KVPROTECTED 1 #define KOMODO_KVBINARY 2 @@ -479,7 +480,8 @@ UniValue sendtoaddress(const UniValue& params, bool fHelp) uint64_t PAX_fiatdest(uint64_t *seedp,int32_t tokomodo,char *destaddr,uint8_t pubkey37[37],char *coinaddr,int32_t height,char *base,int64_t fiatoshis); int32_t komodo_opreturnscript(uint8_t *script,uint8_t type,uint8_t *opret,int32_t opretlen); #define CRYPTO777_KMDADDR "RXL3YXG2ceaB6C5hfJcN4fvmLH2C34knhA" -extern char ASSETCHAINS_SYMBOL[16]; +extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; +extern int32_t KOMODO_PAX; int32_t komodo_is_issuer(); int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp); int32_t komodo_isrealtime(int32_t *kmdheightp); @@ -612,6 +614,10 @@ UniValue paxdeposit(const UniValue& params, bool fHelp) { uint64_t available,deposited,issued,withdrawn,approved,redeemed,seed,komodoshis = 0; int32_t height; char destaddr[64]; uint8_t i,pubkey37[33]; bool fSubtractFeeFromAmount = false; + if ( KOMODO_PAX == 0 ) + { + throw runtime_error("paxdeposit dispabled without -pax"); + } if ( komodo_is_issuer() != 0 ) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "paxdeposit only from KMD"); if (!EnsureWalletIsAvailable(fHelp)) @@ -689,7 +695,7 @@ UniValue listaddressgroupings(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp) throw runtime_error( "listaddressgroupings\n" @@ -740,7 +746,7 @@ UniValue signmessage(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() != 2) throw runtime_error( "signmessage \"zcashaddress\" \"message\"\n" @@ -796,7 +802,7 @@ UniValue getreceivedbyaddress(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 1 || params.size() > 2) throw runtime_error( "getreceivedbyaddress \"zcashaddress\" ( minconf )\n" @@ -854,7 +860,7 @@ UniValue getreceivedbyaccount(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 1 || params.size() > 2) throw runtime_error( "getreceivedbyaccount \"account\" ( minconf )\n" @@ -943,7 +949,7 @@ UniValue getbalance(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 3) throw runtime_error( "getbalance ( \"account\" minconf includeWatchonly )\n" @@ -1015,7 +1021,7 @@ UniValue getunconfirmedbalance(const UniValue ¶ms, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 0) throw runtime_error( "getunconfirmedbalance\n" @@ -1031,7 +1037,7 @@ UniValue movecmd(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 3 || params.size() > 5) throw runtime_error( "move \"fromaccount\" \"toaccount\" amount ( minconf \"comment\" )\n" @@ -1103,7 +1109,7 @@ UniValue sendfrom(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 3 || params.size() > 6) throw runtime_error( "sendfrom \"fromaccount\" \"tozcashaddress\" amount ( minconf \"comment\" \"comment-to\" )\n" @@ -1168,7 +1174,7 @@ UniValue sendmany(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 2 || params.size() > 5) throw runtime_error( "sendmany \"fromaccount\" {\"address\":amount,...} ( minconf \"comment\" [\"address\",...] )\n" @@ -1282,7 +1288,7 @@ UniValue addmultisigaddress(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 2 || params.size() > 3) { string msg = "addmultisigaddress nrequired [\"key\",...] ( \"account\" )\n" @@ -1463,7 +1469,7 @@ UniValue listreceivedbyaddress(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 3) throw runtime_error( "listreceivedbyaddress ( minconf includeempty includeWatchonly)\n" @@ -1500,7 +1506,7 @@ UniValue listreceivedbyaccount(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 3) throw runtime_error( "listreceivedbyaccount ( minconf includeempty includeWatchonly)\n" @@ -1567,7 +1573,11 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe entry.push_back(Pair("fee", ValueFromAmount(-nFee))); if (fLong) WalletTxToJSON(wtx, entry); + #ifdef __APPLE__ + entry.push_back(Pair("size", (uint64_t)static_cast(wtx).GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION))); + #else entry.push_back(Pair("size", static_cast(wtx).GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION))); + #endif ret.push_back(entry); } } @@ -1604,7 +1614,11 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe entry.push_back(Pair("vout", r.vout)); if (fLong) WalletTxToJSON(wtx, entry); - entry.push_back(Pair("size", static_cast(wtx).GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION))); + #ifdef __APPLE__ + entry.push_back(Pair("size", (uint64_t)static_cast(wtx).GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION))); + #else + entry.push_back(Pair("size", static_cast(wtx).GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION))); + #endif ret.push_back(entry); } } @@ -1632,7 +1646,7 @@ UniValue listtransactions(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 4) throw runtime_error( "listtransactions ( \"account\" count from includeWatchonly)\n" @@ -1754,7 +1768,7 @@ UniValue listaccounts(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 2) throw runtime_error( "listaccounts ( minconf includeWatchonly)\n" @@ -1834,7 +1848,7 @@ UniValue listsinceblock(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp) throw runtime_error( "listsinceblock ( \"blockhash\" target-confirmations includeWatchonly)\n" @@ -1925,7 +1939,7 @@ UniValue gettransaction(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 1 || params.size() > 2) throw runtime_error( "gettransaction \"txid\" ( includeWatchonly )\n" @@ -2014,7 +2028,7 @@ UniValue backupwallet(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() != 1) throw runtime_error( "backupwallet \"destination\"\n" @@ -2057,7 +2071,7 @@ UniValue keypoolrefill(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 1) throw runtime_error( "keypoolrefill ( newsize )\n" @@ -2101,7 +2115,7 @@ UniValue walletpassphrase(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (pwalletMain->IsCrypted() && (fHelp || params.size() != 2)) throw runtime_error( "walletpassphrase \"passphrase\" timeout\n" @@ -2163,7 +2177,7 @@ UniValue walletpassphrasechange(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (pwalletMain->IsCrypted() && (fHelp || params.size() != 2)) throw runtime_error( "walletpassphrasechange \"oldpassphrase\" \"newpassphrase\"\n" @@ -2209,7 +2223,7 @@ UniValue walletlock(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (pwalletMain->IsCrypted() && (fHelp || params.size() != 0)) throw runtime_error( "walletlock\n" @@ -2316,7 +2330,7 @@ UniValue lockunspent(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 1 || params.size() > 2) throw runtime_error( "lockunspent unlock [{\"txid\":\"txid\",\"vout\":n},...]\n" @@ -2400,7 +2414,7 @@ UniValue listlockunspent(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 0) throw runtime_error( "listlockunspent\n" @@ -2449,7 +2463,7 @@ UniValue settxfee(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() < 1 || params.size() > 1) throw runtime_error( "settxfee amount\n" @@ -2476,7 +2490,7 @@ UniValue getwalletinfo(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() != 0) throw runtime_error( "getwalletinfo\n" @@ -2518,7 +2532,7 @@ UniValue resendwallettransactions(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() != 0) throw runtime_error( "resendwallettransactions\n" @@ -2545,7 +2559,7 @@ UniValue listunspent(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) return NullUniValue; - + if (fHelp || params.size() > 3) throw runtime_error( "listunspent ( minconf maxconf [\"address\",...] )\n" @@ -3241,8 +3255,8 @@ UniValue z_listaddresses(const UniValue& params, bool fHelp) CAmount getBalanceTaddr(std::string transparentAddress, int minDepth=1) { set setAddress; vector vecOutputs; - CAmount balance = 0; - + CAmount balance = 0; + if (transparentAddress.length() > 0) { CBitcoinAddress taddr = CBitcoinAddress(transparentAddress); if (!taddr.IsValid()) { @@ -3250,7 +3264,7 @@ CAmount getBalanceTaddr(std::string transparentAddress, int minDepth=1) { } setAddress.insert(taddr); } - + LOCK2(cs_main, pwalletMain->cs_wallet); pwalletMain->AvailableCoins(vecOutputs, false, NULL, true); @@ -3270,7 +3284,7 @@ CAmount getBalanceTaddr(std::string transparentAddress, int minDepth=1) { continue; } } - + CAmount nValue = out.tx->vout[out.i].nValue; // komodo_interest balance += nValue; } @@ -3318,7 +3332,7 @@ UniValue z_listreceivedbyaddress(const UniValue& params, bool fHelp) if (nMinDepth < 0) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Minimum number of confirmations cannot be less than 0"); } - + // Check that the from address is valid. auto fromaddress = params[0].get_str(); @@ -3333,8 +3347,8 @@ UniValue z_listreceivedbyaddress(const UniValue& params, bool fHelp) if (!pwalletMain->HaveSpendingKey(zaddr)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "From address does not belong to this node, zaddr spending key not found."); } - - + + UniValue result(UniValue::VARR); std::vector entries; pwalletMain->GetFilteredNotes(entries, fromaddress, nMinDepth, false); @@ -3382,7 +3396,7 @@ UniValue z_getbalance(const UniValue& params, bool fHelp) if (nMinDepth < 0) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Minimum number of confirmations cannot be less than 0"); } - + // Check that the from address is valid. auto fromaddress = params[0].get_str(); bool fromTaddr = false; @@ -3449,7 +3463,7 @@ UniValue z_gettotalbalance(const UniValue& params, bool fHelp) } // getbalance and "getbalance * 1 true" should return the same number - // but they don't because wtx.GetAmounts() does not handle tx where there are no outputs + // but they don't because wtx.GetAmounts() does not handle tx where there are no outputs // pwalletMain->GetBalance() does not accept min depth parameter // so we use our own method to get balance of utxos. CAmount nBalance = getBalanceTaddr("", nMinDepth); @@ -3479,7 +3493,7 @@ UniValue z_getoperationresult(const UniValue& params, bool fHelp) "\nResult:\n" "\" [object, ...]\" (array) A list of JSON objects\n" ); - + // This call will remove finished operations return z_getoperationstatus_IMPL(params, true); } @@ -3499,7 +3513,7 @@ UniValue z_getoperationstatus(const UniValue& params, bool fHelp) "\nResult:\n" "\" [object, ...]\" (array) A list of JSON objects\n" ); - + // This call is idempotent so we don't want to remove finished operations return z_getoperationstatus_IMPL(params, false); } @@ -3524,7 +3538,7 @@ UniValue z_getoperationstatus_IMPL(const UniValue& params, bool fRemoveFinishedO for (auto id : ids) { if (useFilter && !filter.count(id)) continue; - + std::shared_ptr operation = q->getOperationForId(id); if (!operation) { continue; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 13045f21e7c..67f7a7fe0b8 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -37,8 +37,10 @@ unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET; bool bSpendZeroConfChange = true; bool fSendFreeTransactions = false; bool fPayAtLeastCustomFee = true; +#include "komodo_defs.h" + extern int32_t KOMODO_EXCHANGEWALLET; -extern char ASSETCHAINS_SYMBOL[16]; +extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; /** * Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) @@ -2202,7 +2204,7 @@ CAmount CWallet::GetImmatureWatchOnlyBalance() const /** * populate vCoins with vector of available COutputs. */ -uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime); +uint64_t komodo_interestnew(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime); uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue); void CWallet::AvailableCoins(vector& vCoins, bool fOnlyConfirmed, const CCoinControl *coinControl, bool fIncludeZeroValue, bool fIncludeCoinBase) const @@ -2250,9 +2252,9 @@ void CWallet::AvailableCoins(vector& vCoins, bool fOnlyConfirmed, const komodo_accrued_interest(&txheight,&locktime,wtxid,i,0,pcoin->vout[i].nValue); if ( (tipindex= chainActive.Tip()) != 0 ) { - interest = komodo_interest(txheight,pcoin->vout[i].nValue,locktime,tipindex->nTime); + interest = komodo_interestnew(txheight,pcoin->vout[i].nValue,locktime,tipindex->nTime); } else interest = 0; - //interest = komodo_interest(chainActive.Tip()->nHeight+1,pcoin->vout[i].nValue,pcoin->nLockTime,chainActive.Tip()->nTime); + //interest = komodo_interestnew(chainActive.Tip()->nHeight+1,pcoin->vout[i].nValue,pcoin->nLockTime,chainActive.Tip()->nTime); if ( interest != 0 ) { //printf("wallet nValueRet %.8f += interest %.8f ht.%d lock.%u/%u tip.%u\n",(double)pcoin->vout[i].nValue/COIN,(double)interest/COIN,txheight,locktime,pcoin->nLockTime,tipindex->nTime); @@ -2333,13 +2335,12 @@ static void ApproximateBestSubset(vector vCoins,set >& setCoinsRet, CAmount& nValueRet, uint64_t *interestp) const +bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, vector vCoins,set >& setCoinsRet, CAmount& nValueRet) const { - uint64_t interests[10000],lowest_interest = 0; int32_t count = 0; + int32_t count = 0; //uint64_t lowest_interest = 0; setCoinsRet.clear(); - memset(interests,0,sizeof(interests)); + //memset(interests,0,sizeof(interests)); nValueRet = 0; - //*interestp = 0; // List of values less than target pair > coinLowestLarger; coinLowestLarger.first = std::numeric_limits::max(); @@ -2368,20 +2369,20 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int { setCoinsRet.insert(coin.second); nValueRet += coin.first; - if ( KOMODO_EXCHANGEWALLET == 0 ) - *interestp += pcoin->vout[i].interest; + //if ( KOMODO_EXCHANGEWALLET == 0 ) + // *interestp += pcoin->vout[i].interest; return true; } else if (n < nTargetValue + CENT) { vValue.push_back(coin); nTotalLower += n; - if ( KOMODO_EXCHANGEWALLET == 0 && count < sizeof(interests)/sizeof(*interests) ) - { + //if ( KOMODO_EXCHANGEWALLET == 0 && count < sizeof(interests)/sizeof(*interests) ) + //{ //fprintf(stderr,"count.%d %.8f\n",count,(double)pcoin->vout[i].interest/COIN); - interests[count++] = pcoin->vout[i].interest; - } - if ( count >= sizeof(interests)/sizeof(*interests) && nTotalLower > 2*nTargetValue + CENT ) + //interests[count++] = pcoin->vout[i].interest; + //} + if ( nTotalLower > 4*nTargetValue + CENT ) { //fprintf(stderr,"why bother with all the utxo if we have double what is needed?\n"); break; @@ -2390,8 +2391,8 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int else if (n < coinLowestLarger.first) { coinLowestLarger = coin; - if ( KOMODO_EXCHANGEWALLET == 0 ) - lowest_interest = pcoin->vout[i].interest; + //if ( KOMODO_EXCHANGEWALLET == 0 ) + // lowest_interest = pcoin->vout[i].interest; } } @@ -2401,8 +2402,8 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int { setCoinsRet.insert(vValue[i].second); nValueRet += vValue[i].first; - if ( KOMODO_EXCHANGEWALLET == 0 && i < count ) - *interestp += interests[i]; + //if ( KOMODO_EXCHANGEWALLET == 0 && i < count ) + // *interestp += interests[i]; } return true; } @@ -2413,8 +2414,8 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int return false; setCoinsRet.insert(coinLowestLarger.second); nValueRet += coinLowestLarger.first; - if ( KOMODO_EXCHANGEWALLET == 0 ) - *interestp += lowest_interest; + //if ( KOMODO_EXCHANGEWALLET == 0 ) + // *interestp += lowest_interest; return true; } @@ -2434,8 +2435,8 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int { setCoinsRet.insert(coinLowestLarger.second); nValueRet += coinLowestLarger.first; - if ( KOMODO_EXCHANGEWALLET == 0 ) - *interestp += lowest_interest; + //if ( KOMODO_EXCHANGEWALLET == 0 ) + // *interestp += lowest_interest; } else { for (unsigned int i = 0; i < vValue.size(); i++) @@ -2443,8 +2444,8 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int { setCoinsRet.insert(vValue[i].second); nValueRet += vValue[i].first; - if ( KOMODO_EXCHANGEWALLET == 0 && i < count ) - *interestp += interests[i]; + //if ( KOMODO_EXCHANGEWALLET == 0 && i < count ) + // *interestp += interests[i]; } LogPrint("selectcoins", "SelectCoins() best subset: "); @@ -2457,15 +2458,15 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int return true; } -bool CWallet::SelectCoins(const CAmount& nTargetValue, set >& setCoinsRet, CAmount& nValueRet, bool& fOnlyCoinbaseCoinsRet, bool& fNeedCoinbaseCoinsRet, const CCoinControl* coinControl,uint64_t *interestp) const +bool CWallet::SelectCoins(const CAmount& nTargetValue, set >& setCoinsRet, CAmount& nValueRet, bool& fOnlyCoinbaseCoinsRet, bool& fNeedCoinbaseCoinsRet, const CCoinControl* coinControl) const { // Output parameter fOnlyCoinbaseCoinsRet is set to true when the only available coins are coinbase utxos. - uint64_t tmp,interest = 0; int32_t retval; - if ( interestp == 0 ) - { - interestp = &tmp; - *interestp = 0; - } + uint64_t tmp; int32_t retval; + //if ( interestp == 0 ) + //{ + // interestp = &tmp; + // *interestp = 0; + //} vector vCoinsNoCoinbase, vCoinsWithCoinbase; AvailableCoins(vCoinsNoCoinbase, true, coinControl, false, false); AvailableCoins(vCoinsWithCoinbase, true, coinControl, false, true); @@ -2507,8 +2508,8 @@ bool CWallet::SelectCoins(const CAmount& nTargetValue, setvout[out.i].nValue; - if ( KOMODO_EXCHANGEWALLET == 0 ) - *interestp += out.tx->vout[out.i].interest; + //if ( KOMODO_EXCHANGEWALLET == 0 ) + // *interestp += out.tx->vout[out.i].interest; setCoinsRet.insert(make_pair(out.tx, out.i)); } return (nValueRet >= nTargetValue); @@ -2548,36 +2549,16 @@ bool CWallet::SelectCoins(const CAmount& nTargetValue, set& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, CAmount& nFeeRet, int& nChangePosRet, std::string& strFailReason, const CCoinControl* coinControl, bool sign) { - uint64_t interest2,interest = 0; CAmount nValue = 0; unsigned int nSubtractFeeFromAmount = 0; + uint64_t interest2 = 0; CAmount nValue = 0; unsigned int nSubtractFeeFromAmount = 0; BOOST_FOREACH (const CRecipient& recipient, vecSend) { if (nValue < 0 || recipient.nAmount < 0) @@ -2684,7 +2665,7 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt nFeeRet = 0; while (true) { - interest = 0; + //interest = 0; txNew.vin.clear(); txNew.vout.clear(); wtxNew.fFromMe = true; @@ -2733,7 +2714,7 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt bool fOnlyCoinbaseCoins = false; bool fNeedCoinbaseCoins = false; interest2 = 0; - if (!SelectCoins(nTotalValue, setCoins, nValueIn, fOnlyCoinbaseCoins, fNeedCoinbaseCoins, coinControl,&interest)) + if (!SelectCoins(nTotalValue, setCoins, nValueIn, fOnlyCoinbaseCoins, fNeedCoinbaseCoins, coinControl)) { if (fOnlyCoinbaseCoins && Params().GetConsensus().fCoinbaseMustBeProtected) { strFailReason = _("Coinbase funds can only be sent to a zaddr"); @@ -2762,13 +2743,13 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt age += 1; dPriority += (double)nCredit * age; } - if ( KOMODO_EXCHANGEWALLET != 0 ) - { + //if ( KOMODO_EXCHANGEWALLET != 0 ) + //{ //fprintf(stderr,"KOMODO_EXCHANGEWALLET disable interest sum %.8f, interest2 %.8f\n",(double)interest/COIN,(double)interest2/COIN); - interest = 0; // interest2 also - } + //interest = 0; // interest2 also + //} CAmount nChange = (nValueIn - nValue + interest2); -fprintf(stderr,"wallet change %.8f (%.8f - %.8f) interest %.8f interest2 %.8f total %.8f\n",(double)nChange/COIN,(double)nValueIn/COIN,(double)nValue/COIN,(double)interest2/COIN,(double)interest/COIN,(double)nTotalValue/COIN); +fprintf(stderr,"wallet change %.8f (%.8f - %.8f) interest2 %.8f total %.8f\n",(double)nChange/COIN,(double)nValueIn/COIN,(double)nValue/COIN,(double)interest2/COIN,(double)nTotalValue/COIN); if (nSubtractFeeFromAmount == 0) nChange -= nFeeRet; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index ed7edbbc1d8..01285ca2bef 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -161,7 +161,7 @@ class JSOutPoint // Transaction hash uint256 hash; // Index into CTransaction.vjoinsplit -#ifdef __APPLE__ +#ifdef __LP64__ uint64_t js; #else size_t js; @@ -288,7 +288,7 @@ class CMerkleTx : public CTransaction uint256 hashBlock; std::vector vMerkleBranch; int nIndex; - + // memory only mutable bool fMerkleVerified; @@ -337,7 +337,7 @@ class CMerkleTx : public CTransaction bool AcceptToMemoryPool(bool fLimitFree=true, bool fRejectAbsurdFee=true); }; -/** +/** * A transaction with a bunch of additional info that only the owner cares about. * It includes any unrecorded transactions needed to link it back to the block chain. */ @@ -577,14 +577,14 @@ class CWalletKey -/** +/** * A CWallet is an extension of a keystore, which also maintains a set of transactions and balances, * and provides the ability to create new transactions. */ class CWallet : public CCryptoKeyStore, public CValidationInterface { private: - bool SelectCoins(const CAmount& nTargetValue, std::set >& setCoinsRet, CAmount& nValueRet, bool& fOnlyCoinbaseCoinsRet, bool& fNeedCoinbaseCoinsRet, const CCoinControl *coinControl = NULL,uint64_t *interestp = NULL) const; + bool SelectCoins(const CAmount& nTargetValue, std::set >& setCoinsRet, CAmount& nValueRet, bool& fOnlyCoinbaseCoinsRet, bool& fNeedCoinbaseCoinsRet, const CCoinControl *coinControl = NULL) const; CWalletDB *pwalletdbEncryption; @@ -811,7 +811,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface bool CanSupportFeature(enum WalletFeature wf) { AssertLockHeld(cs_wallet); return nWalletMaxVersion >= wf; } void AvailableCoins(std::vector& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl = NULL, bool fIncludeZeroValue=false, bool fIncludeCoinBase=true) const; - bool SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, std::vector vCoins, std::set >& setCoinsRet, CAmount& nValueRet,uint64_t *interestp) const; + bool SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, std::vector vCoins, std::set >& setCoinsRet, CAmount& nValueRet) const; bool IsSpent(const uint256& hash, unsigned int n) const; bool IsSpent(const uint256& nullifier) const; @@ -880,7 +880,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface //! Adds an encrypted spending key to the store, and saves it to disk (virtual method, declared in crypter.h) bool AddCryptedSpendingKey(const libzcash::PaymentAddress &address, const libzcash::ViewingKey &vk, const std::vector &vchCryptedSecret); - /** + /** * Increment the next transaction order id * @return next transaction order id */ @@ -1012,8 +1012,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface //! Verify the wallet database and perform salvage if required static bool Verify(const std::string& walletFile, std::string& warningString, std::string& errorString); - - /** + + /** * Address book entry changed. * @note called with lock cs_wallet held. */ @@ -1022,7 +1022,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface const std::string &purpose, ChangeType status)> NotifyAddressBookChanged; - /** + /** * Wallet transaction added, removed or updated. * @note called with lock cs_wallet held. */ @@ -1039,10 +1039,10 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface bool GetBroadcastTransactions() const { return fBroadcastTransactions; } /** Set whether this wallet broadcasts transactions. */ void SetBroadcastTransactions(bool broadcast) { fBroadcastTransactions = broadcast; } - + /* Find notes filtered by payment address, min depth, ability to spend */ void GetFilteredNotes(std::vector & outEntries, std::string address, int minDepth=1, bool ignoreSpent=true); - + }; /** A key allocated from the key pool. */ @@ -1070,7 +1070,7 @@ class CReserveKey }; -/** +/** * Account information. * Stored in wallet with key "acc"+string account name. */ @@ -1101,7 +1101,7 @@ class CAccount -/** +/** * Internal transfers. * Database key is acentry. */ diff --git a/src/zcash/CreateJoinSplit.cpp b/src/zcash/CreateJoinSplit.cpp index 7fdc8d95328..9c7760e4f57 100644 --- a/src/zcash/CreateJoinSplit.cpp +++ b/src/zcash/CreateJoinSplit.cpp @@ -6,8 +6,10 @@ #include "primitives/transaction.h" #include "zcash/JoinSplit.hpp" #include "libsnark/common/profiling.hpp" -char ASSETCHAINS_SYMBOL[16]; +#include "komodo_defs.h" +char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; int64_t MAX_MONEY = 200000000 * 100000000LL; +uint16_t BITCOIND_PORT = 7771; using namespace libzcash; diff --git a/user-config.jam b/user-config.jam new file mode 100644 index 00000000000..e473b46e786 --- /dev/null +++ b/user-config.jam @@ -0,0 +1 @@ +using : : : " " "" "" "" "" "" : ; diff --git a/zcutil/build-debian-package.sh b/zcutil/build-debian-package.sh index 11b68badc93..8c2d05c3f8f 100755 --- a/zcutil/build-debian-package.sh +++ b/zcutil/build-debian-package.sh @@ -5,8 +5,8 @@ set -e set -x -BUILD_PATH="/tmp/kmdbuild" -PACKAGE_NAME="komodo" +BUILD_PATH="/tmp/zcbuild" +PACKAGE_NAME="zcash" SRC_PATH=`pwd` SRC_DEB=$SRC_PATH/contrib/debian SRC_DOC=$SRC_PATH/doc @@ -17,7 +17,7 @@ if [ ! -d $BUILD_PATH ]; then mkdir $BUILD_PATH fi -PACKAGE_VERSION=$($SRC_PATH/src/komodod --version | grep version | cut -d' ' -f4 | tr -d v) +PACKAGE_VERSION=$($SRC_PATH/src/zcashd --version | grep version | cut -d' ' -f4 | tr -d v) BUILD_DIR="$BUILD_PATH/$PACKAGE_NAME-$PACKAGE_VERSION-amd64" if [ -d $BUILD_DIR ]; then @@ -28,7 +28,7 @@ DEB_BIN=$BUILD_DIR/usr/bin DEB_CMP=$BUILD_DIR/usr/share/bash-completion/completions DEB_DOC=$BUILD_DIR/usr/share/doc/$PACKAGE_NAME DEB_MAN=$BUILD_DIR/usr/share/man/man1 -mkdir -p $BUILD_DIR/DEBIAN $DEB_BIN $DEB_DOC +mkdir -p $BUILD_DIR/DEBIAN $DEB_CMP $DEB_BIN $DEB_DOC $DEB_MAN chmod 0755 -R $BUILD_DIR/* # Package maintainer scripts (currently empty) @@ -37,32 +37,32 @@ chmod 0755 -R $BUILD_DIR/* #cp $SRC_DEB/preinst $BUILD_DIR/DEBIAN #cp $SRC_DEB/prerm $BUILD_DIR/DEBIAN # Copy binaries -cp $SRC_PATH/src/komodod $DEB_BIN -cp $SRC_PATH/src/komodo-cli $DEB_BIN -cp $SRC_PATH/zcutil/fetch-params.sh $DEB_BIN/komodo-fetch-params +cp $SRC_PATH/src/zcashd $DEB_BIN +cp $SRC_PATH/src/zcash-cli $DEB_BIN +cp $SRC_PATH/zcutil/fetch-params.sh $DEB_BIN/zcash-fetch-params # Copy docs cp $SRC_PATH/doc/release-notes/release-notes-1.0.0.md $DEB_DOC/changelog cp $SRC_DEB/changelog $DEB_DOC/changelog.Debian cp $SRC_DEB/copyright $DEB_DOC cp -r $SRC_DEB/examples $DEB_DOC # Copy manpages -#cp $SRC_DOC/man/zcashd.1 $DEB_MAN -#cp $SRC_DOC/man/zcash-cli.1 $DEB_MAN -#cp $SRC_DOC/man/zcash-fetch-params.1 $DEB_MAN -# Copy bash completion files TODO: fix them for komodo -#cp $SRC_PATH/contrib/bitcoind.bash-completion $DEB_CMP/komodod -#cp $SRC_PATH/contrib/bitcoin-cli.bash-completion $DEB_CMP/komodod-cli +cp $SRC_DOC/man/zcashd.1 $DEB_MAN +cp $SRC_DOC/man/zcash-cli.1 $DEB_MAN +cp $SRC_DOC/man/zcash-fetch-params.1 $DEB_MAN +# Copy bash completion files +cp $SRC_PATH/contrib/bitcoind.bash-completion $DEB_CMP/zcashd +cp $SRC_PATH/contrib/bitcoin-cli.bash-completion $DEB_CMP/zcash-cli # Gzip files gzip --best -n $DEB_DOC/changelog gzip --best -n $DEB_DOC/changelog.Debian -#gzip --best -n $DEB_MAN/zcashd.1 -#gzip --best -n $DEB_MAN/zcash-cli.1 -#gzip --best -n $DEB_MAN/zcash-fetch-params.1 +gzip --best -n $DEB_MAN/zcashd.1 +gzip --best -n $DEB_MAN/zcash-cli.1 +gzip --best -n $DEB_MAN/zcash-fetch-params.1 cd $SRC_PATH/contrib # Create the control file -dpkg-shlibdeps $DEB_BIN/komodod $DEB_BIN/komodo-cli +dpkg-shlibdeps $DEB_BIN/zcashd $DEB_BIN/zcash-cli dpkg-gencontrol -P$BUILD_DIR # Create the Debian package diff --git a/zcutil/build-mac.sh b/zcutil/build-mac.sh index e503d09742f..d53f4052e90 100755 --- a/zcutil/build-mac.sh +++ b/zcutil/build-mac.sh @@ -43,7 +43,7 @@ PREFIX="$(pwd)/depends/$TRIPLET" make "$@" -C ./depends/ V=1 NO_QT=1 ./autogen.sh -CPPFLAGS="-I$PREFIX/include -arch x86_64" LDFLAGS="-L$PREFIX/lib -arch x86_64 -Wl,-no_pie -fopenmp" \ +CPPFLAGS="-I$PREFIX/include -arch x86_64" LDFLAGS="-L$PREFIX/lib -arch x86_64 -Wl,-no_pie" \ CXXFLAGS='-arch x86_64 -I/usr/local/Cellar/gcc5/5.4.0/include/c++/5.4.0 -I$PREFIX/include -fwrapv -fno-strict-aliasing -Werror -g -Wl,-undefined -Wl,dynamic_lookup' \ ./configure --prefix="${PREFIX}" --with-gui=no "$HARDENING_ARG" "$LCOV_ARG" diff --git a/zcutil/build-win.sh b/zcutil/build-win.sh new file mode 100755 index 00000000000..65e0c89230d --- /dev/null +++ b/zcutil/build-win.sh @@ -0,0 +1,17 @@ +#!/bin/bash +HOST=x86_64-w64-mingw32 +CXX=x86_64-w64-mingw32-g++-posix +CC=x86_64-w64-mingw32-gcc-posix +PREFIX="$(pwd)/depends/$HOST" + +set -eu -o pipefail + +set -x +cd "$(dirname "$(readlink -f "$0")")/.." + +cd depends/ && make HOST=$HOST V=1 NO_QT=1 && cd ../ +./autogen.sh +CONFIG_SITE=$PWD/depends/x86_64-w64-mingw32/share/config.site CXXFLAGS="-DPTW32_STATIC_LIB -DCURL_STATICLIB -DCURVE_ALT_BN128 -fopenmp -pthread" ./configure --prefix="${PREFIX}" --host=x86_64-w64-mingw32 --enable-static --disable-shared +sed -i 's/-lboost_system-mt /-lboost_system-mt-s /' configure +cd src/ +CC="${CC}" CXX="${CXX}" make V=1 komodod.exe komodo-cli.exe komodo-tx.exe