diff --git a/astrobwt/astrobwt.go b/astrobwt/astrobwt.go index 208a46c..7dc5b99 100644 --- a/astrobwt/astrobwt.go +++ b/astrobwt/astrobwt.go @@ -1,6 +1,8 @@ package astrobwt import "fmt" +import "unsafe" +import "encoding/binary" import "golang.org/x/crypto/sha3" import "golang.org/x/crypto/salsa20/salsa" @@ -11,8 +13,6 @@ var x = fmt.Sprintf const stage1_length int = 9973 // it is a prime func POW16(inputdata []byte) (outputhash [32]byte) { - - var output [stage1_length]byte var counter [16]byte key := sha3.Sum256(inputdata) @@ -23,14 +23,18 @@ func POW16(inputdata []byte) (outputhash [32]byte) { var sa [stage1_length]int16 text_16_0alloc(stage1[:], sa[:]) - for i := range sa { - output[i] = stage1[sa[i]] + if LittleEndian { + var s *[stage1_length * 2]byte = (*[stage1_length * 2]byte)(unsafe.Pointer(&sa)) + outputhash = sha3.Sum256(s[:]) + return + } else { + var s [stage1_length * 2]byte + for i := range sa { + binary.LittleEndian.PutUint16(s[i<<1:], uint16(sa[i])) + } + outputhash = sha3.Sum256(s[:]) + return } - - // fmt.Printf("input %+v\n",inputdata) - // fmt.Printf("sa %+v\n",sa) - outputhash = sha3.Sum256(output[:]) - return } @@ -43,7 +47,7 @@ func text_16_0alloc(text []byte, sa []int16) { } func POW32(inputdata []byte) (outputhash [32]byte) { - var output [stage1_length]byte + var sa16 [stage1_length]int16 var counter [16]byte key := sha3.Sum256(inputdata) @@ -51,10 +55,23 @@ func POW32(inputdata []byte) (outputhash [32]byte) { salsa.XORKeyStream(stage1[:stage1_length], stage1[:stage1_length], &counter, &key) var sa [stage1_length]int32 text_32_0alloc(stage1[:], sa[:]) + for i := range sa { - output[i] = stage1[sa[i]] + sa16[i] = int16(sa[i]) + } + + if LittleEndian { + var s *[stage1_length * 2]byte = (*[stage1_length * 2]byte)(unsafe.Pointer(&sa16)) + outputhash = sha3.Sum256(s[:]) + return + } else { + var s [stage1_length * 2]byte + for i := range sa { + binary.LittleEndian.PutUint16(s[i<<1:], uint16(sa[i])) + } + outputhash = sha3.Sum256(s[:]) + return } - outputhash = sha3.Sum256(output[:]) return } diff --git a/astrobwt/astrobwt_test.go b/astrobwt/astrobwt_test.go index cc194b5..eebe6ee 100644 --- a/astrobwt/astrobwt_test.go +++ b/astrobwt/astrobwt_test.go @@ -43,7 +43,7 @@ func TestSuffixArrayOptimized(t *testing.T) { func TestPows(t *testing.T) { - for loop_var := 0; loop_var < 100000; loop_var++ { + for loop_var := 0; loop_var < 1; loop_var++ { seed := time.Now().UnixNano() //seed = 1635948770488138379 @@ -60,6 +60,7 @@ func TestPows(t *testing.T) { if result16 != result32 { t.Fatalf("pow test failed, seed %d %x %x ", seed, result16, result32) } + } } diff --git a/astrobwt/endian_big.go b/astrobwt/endian_big.go new file mode 100644 index 0000000..f60f37b --- /dev/null +++ b/astrobwt/endian_big.go @@ -0,0 +1,9 @@ +//go:build armbe || arm64be || mips || mips64 || ppc64 || s390 || s390x || sparc || sparc64 +// +build armbe arm64be mips mips64 ppc64 s390 s390x sparc sparc64 + +package astrobwt + +const LittleEndian = false +const BigEndian = true + +// see https://github.com/golang/go/blob/master/src/go/build/syslist.go diff --git a/astrobwt/endian_little.go b/astrobwt/endian_little.go new file mode 100644 index 0000000..cc6cb75 --- /dev/null +++ b/astrobwt/endian_little.go @@ -0,0 +1,9 @@ +//go:build amd64 || amd64p32 || 386 || arm || arm64 || mipsle || mips64le || mips64p32le || ppc64le || riscv || riscv64 || wasm || loong64 +// +build amd64 amd64p32 386 arm arm64 mipsle mips64le mips64p32le ppc64le riscv riscv64 wasm loong64 + +package astrobwt + +const LittleEndian = true +const BigEndian = false + +//see https://github.com/golang/go/blob/master/src/go/build/syslist.go diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 80e54f2..e7ba2bc 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -1122,7 +1122,7 @@ func (chain *Blockchain) Add_TX_To_Pool(tx *transaction.Transaction) error { if tx.IsRegistration() { // registration tx will not go any forward tx_hash := tx.GetHash() - if chain.simulator == false && tx_hash[0] != 0 && tx_hash[1] != 0 { + if chain.simulator == false && !(tx_hash[0] == 0 && tx_hash[1] == 0 && tx_hash[2] <= 0x3) { return fmt.Errorf("TX doesn't solve Pow") } diff --git a/blockchain/supply_test.go b/blockchain/supply_test.go new file mode 100644 index 0000000..34a2929 --- /dev/null +++ b/blockchain/supply_test.go @@ -0,0 +1,35 @@ +// Copyright 2017-2022 DERO Project. All rights reserved. +// Use of this source code in any form is governed by RESEARCH license. +// license can be found in the LICENSE file. +// GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8 +// +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package blockchain + +import "testing" +import "github.com/deroproject/derohe/globals" + + +func Test_Supply(t *testing.T) { + + supply_at_0 := CalcBlockReward(0) + + for i := uint64(0); i < 10; i++ { + supply := CalcBlockReward(i * RewardReductionInterval) + t.Logf("Supply at height %d %s", i*RewardReductionInterval, globals.FormatMoney(supply)) + if supply != supply_at_0>>i { + t.Errorf("supply not halvening as needed ") + return + } + } +} diff --git a/blockchain/transaction_execute.go b/blockchain/transaction_execute.go index d7812ae..563c559 100644 --- a/blockchain/transaction_execute.go +++ b/blockchain/transaction_execute.go @@ -124,15 +124,12 @@ func (chain *Blockchain) process_miner_transaction(bl *block.Block, genesis bool base_reward := CalcBlockReward(uint64(height)) full_reward := base_reward + fees - integrator_reward := full_reward * 167 / 10000 - - //full_reward is divided into equal parts for all miner blocks - // integrator only gets 1.67 % of block reward + //full_reward is divided into equal parts for all miner blocks + miner address // since perfect division is not possible, ( see money handling) // any left over change is delivered to main miner who integrated the full block - share := (full_reward - integrator_reward) / uint64(len(bl.MiniBlocks)) // one block integrator, this is integer division - leftover := full_reward - integrator_reward - (share * uint64(len(bl.MiniBlocks))) // only integrator will get this + share := full_reward / uint64(len(bl.MiniBlocks)) // one block integrator, this is integer division + leftover := full_reward - (share * uint64(len(bl.MiniBlocks))) // only integrator will get this { // giver integrator his reward balance_serialized, err := balance_tree.Get(tx.MinerAddress[:]) @@ -140,8 +137,8 @@ func (chain *Blockchain) process_miner_transaction(bl *block.Block, genesis bool panic(err) } nb := new(crypto.NonceBalance).Deserialize(balance_serialized) - nb.Balance = nb.Balance.Plus(new(big.Int).SetUint64(integrator_reward + leftover)) // add miners reward to miners balance homomorphically - balance_tree.Put(tx.MinerAddress[:], nb.Serialize()) // reserialize and store + nb.Balance = nb.Balance.Plus(new(big.Int).SetUint64(share + leftover)) // add miners reward to miners balance homomorphically + balance_tree.Put(tx.MinerAddress[:], nb.Serialize()) // reserialize and store } // all the other miniblocks will get their share @@ -238,6 +235,7 @@ func (chain *Blockchain) process_transaction(changed map[crypto.Hash]*graviton.T nb.NonceHeight = height } tree.Put(key_compressed, nb.Serialize()) // reserialize and store + } } diff --git a/cmd/dero-wallet-cli/easymenu_post_open.go b/cmd/dero-wallet-cli/easymenu_post_open.go index a3519a1..0964156 100644 --- a/cmd/dero-wallet-cli/easymenu_post_open.go +++ b/cmd/dero-wallet-cli/easymenu_post_open.go @@ -139,7 +139,7 @@ func handle_easymenu_post_open_command(l *readline.Instance, line string) (proce reg_tx = wallet.GetRegistrationTX() hash := reg_tx.GetHash() - if hash[0] == 0 && hash[1] == 0 { + if hash[0] == 0 && hash[1] == 0 && hash[2] <= 0x3 { break } } diff --git a/cmd/derod/rpc/websocket_server.go b/cmd/derod/rpc/websocket_server.go index 7c25e03..6354957 100644 --- a/cmd/derod/rpc/websocket_server.go +++ b/cmd/derod/rpc/websocket_server.go @@ -73,6 +73,7 @@ type metrics_generator struct{} func (metrics_generator) LogRequest(ctx context.Context, req *jrpc2.Request) {} func (metrics_generator) LogResponse(ctx context.Context, resp *jrpc2.Response) { + defer globals.Recover(2) req := jrpc2.InboundRequest(ctx) // we cannot do anything here if req == nil { return diff --git a/config/config.go b/config/config.go index 16c0ef8..0bf2f4b 100644 --- a/config/config.go +++ b/config/config.go @@ -107,7 +107,7 @@ var Mainnet = CHAIN_CONFIG{Name: "mainnet", } var Testnet = CHAIN_CONFIG{Name: "testnet", // testnet will always have last 3 bytes 0 - Network_ID: uuid.FromBytesOrNil([]byte{0x59, 0xd7, 0xf7, 0xe9, 0xdd, 0x48, 0xd5, 0xfd, 0x13, 0x0a, 0xf6, 0xe0, 0x83, 0x00, 0x00, 0x00}), + Network_ID: uuid.FromBytesOrNil([]byte{0x59, 0xd7, 0xf7, 0xe9, 0xdd, 0x48, 0xd5, 0xfd, 0x13, 0x0a, 0xf6, 0xe0, 0x86, 0x00, 0x00, 0x00}), GETWORK_Default_Port: 10100, P2P_Default_Port: 40401, RPC_Default_Port: 40402, diff --git a/config/version.go b/config/version.go index 5b5d12e..01acd63 100644 --- a/config/version.go +++ b/config/version.go @@ -20,4 +20,4 @@ import "github.com/blang/semver/v4" // right now it has to be manually changed // do we need to include git commitsha?? -var Version = semver.MustParse("3.4.109-0.DEROHE.STARGATE+18012022") +var Version = semver.MustParse("3.4.111-43.DEROHE.STARGATE+18012022")