Skip to content

Commit

Permalink
merge 1.0.5 testnet to master
Browse files Browse the repository at this point in the history
  • Loading branch information
fengzi committed Jun 19, 2018
2 parents 5bce7f1 + 1dce757 commit fa4ad54
Show file tree
Hide file tree
Showing 119 changed files with 6,118 additions and 495 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,4 @@ Thanks.
The go-nebulas project is licensed under the [GNU Lesser General Public License Version 3.0 (“LGPL v3”)](https://www.gnu.org/licenses/lgpl-3.0.en.html).

For the more information about licensing, please refer to [Licensing](https://github.com/nebulasio/wiki/blob/master/licensing.md) page.

164 changes: 164 additions & 0 deletions account/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
package account

import (
"crypto/md5"
"fmt"
"testing"
"time"

"os"

"github.com/nebulasio/go-nebulas/core"
"github.com/nebulasio/go-nebulas/crypto"
"github.com/nebulasio/go-nebulas/crypto/hash"
"github.com/nebulasio/go-nebulas/crypto/keystore"
"github.com/nebulasio/go-nebulas/util"
"github.com/nebulasio/go-nebulas/util/byteutils"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -223,3 +229,161 @@ func TestManager_SignTransactionWithPassphrase(t *testing.T) {
})
}
}

func TestForCryptoJS(t *testing.T) {
type args struct {
method string
input string
output string
}

tests := []args{
{
method: "sha256",
input: "Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem.",
output: "a32d6d686968192663b9c9e21e6a3ba1ba9b2e288470c2f98b790256530933e0",
},

{
method: "sha3256",
input: "Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem.",
output: "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b",
},

{
method: "ripemd160",
input: "Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem.",
output: "4236aa9974eb7b9ddb0f7a7ed06d4bf3d9c0e386",
},

{
method: "md5",
input: "Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem.",
output: "9954125a33a380c3117269cff93f76a7",
},

{
method: "base64",
input: "Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem.",
output: "TmVidWxhcyBpcyBhIG5leHQgZ2VuZXJhdGlvbiBwdWJsaWMgYmxvY2tjaGFpbiwgYWltaW5nIGZvciBhIGNvbnRpbnVvdXNseSBpbXByb3ZpbmcgZWNvc3lzdGVtLg==",
},
}

for _, tt := range tests {
t.Run(tt.method, func(t *testing.T) {
out := ""
switch tt.method {
case "sha256":
out = byteutils.Hex(hash.Sha256([]byte(tt.input)))
case "sha3256":
out = byteutils.Hex(hash.Sha3256([]byte(tt.input)))
case "ripemd160":
out = byteutils.Hex(hash.Ripemd160([]byte(tt.input)))
case "md5":
r := md5.Sum([]byte(tt.input))
out = byteutils.Hex(r[:])
case "base64":
out = string(hash.Base64Encode([]byte(tt.input)))
}
assert.Equal(t, tt.output, out)
})
}

// recoverAddress
manager, _ := NewManager(nil)
addr, err := manager.NewAccount([]byte("passphrase"))
assert.Nil(t, err, "new address err")
err = manager.Unlock(addr, []byte("passphrase"), keystore.DefaultUnlockDuration)
assert.Nil(t, err, "unlock err")
key, err := manager.ks.GetUnlocked(addr.String())
assert.Nil(t, err, "get key err")

signature, err := crypto.NewSignature(1)
assert.Nil(t, err, "get signature err")

err = signature.InitSign(key.(keystore.PrivateKey))
assert.Nil(t, err, "init signature err")

data := hash.Sha3256([]byte("Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem."))
assert.Equal(t, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", byteutils.Hex(data))
signData, err := signature.Sign(data)
assert.Nil(t, err, "sign data err")
signHex := byteutils.Hex(signData)
// assert.Equal(t, "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101", byteutils.Hex(signData))

// recover
rb, err := byteutils.FromHex(signHex)
assert.Nil(t, err, "from hex error")
rAddr, err := core.RecoverSignerFromSignature(keystore.Algorithm(1), data, rb)
assert.Nil(t, err, "recover err")
assert.Equal(t, addr.String(), rAddr.String())

acc, err := manager.getAccount(addr)
assert.Nil(t, err, "get acc err")
err = manager.Remove(addr, []byte("passphrase"))
assert.Nil(t, err)
err = os.Remove(acc.path)
assert.Nil(t, err)
}
func TestCryptoPerformance(t *testing.T) {
// test sha256
start := time.Now()
out := ""
for i := 0; i < 1000; i++ {
out = byteutils.Hex(hash.Sha256([]byte("Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem.")))
}
cost := time.Now().Sub(start).Nanoseconds()
fmt.Println("sha256", out, cost)

// test md5
start = time.Now()
for i := 0; i < 1000; i++ {
r := md5.Sum([]byte("Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem."))
out = byteutils.Hex(r[:])
}
cost = time.Now().Sub(start).Nanoseconds()
fmt.Println("md5", out, cost)

// test recover address

manager, _ := NewManager(nil)
addr, err := manager.NewAccount([]byte("passphrase"))
assert.Nil(t, err, "new address err")
err = manager.Unlock(addr, []byte("passphrase"), keystore.DefaultUnlockDuration)
assert.Nil(t, err, "unlock err")
key, err := manager.ks.GetUnlocked(addr.String())
assert.Nil(t, err, "get key err")

signature, err := crypto.NewSignature(1)
assert.Nil(t, err, "get signature err")

err = signature.InitSign(key.(keystore.PrivateKey))
assert.Nil(t, err, "init signature err")

data := hash.Sha3256([]byte("Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem."))
assert.Equal(t, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", byteutils.Hex(data))
signData, err := signature.Sign(data)
assert.Nil(t, err, "sign data err")
signHex := byteutils.Hex(signData)

// recover
rb, err := byteutils.FromHex(signHex)
assert.Nil(t, err, "from hex error")
rAddr, err := core.RecoverSignerFromSignature(keystore.Algorithm(1), data, rb)
start = time.Now()
for i := 0; i < 1000; i++ {
core.RecoverSignerFromSignature(keystore.Algorithm(1), data, rb)
}
cost = time.Now().Sub(start).Nanoseconds()
fmt.Println("recoverAddress", rAddr.String(), cost)

assert.Nil(t, err, "recover err")
assert.Equal(t, addr.String(), rAddr.String())

acc, err := manager.getAccount(addr)
assert.Nil(t, err, "get acc err")
err = manager.Remove(addr, []byte("passphrase"))
assert.Nil(t, err)
err = os.Remove(acc.path)
assert.Nil(t, err)
}
5 changes: 3 additions & 2 deletions cmd/v8/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"time"

"github.com/nebulasio/go-nebulas/core"
"github.com/nebulasio/go-nebulas/core/state"
"github.com/nebulasio/go-nebulas/nf/nvm"
"github.com/nebulasio/go-nebulas/storage"
Expand All @@ -34,9 +35,9 @@ func main() {

mem, _ := storage.NewMemoryStorage()
context, _ := state.NewWorldState(nil, mem)
contract, _ := context.CreateContractAccount([]byte("account2"), nil)
contract, _ := context.CreateContractAccount([]byte("account2"), nil, nil)

ctx, err := nvm.NewContext(nil, nil, contract, context)
ctx, err := nvm.NewContext(core.MockBlock(nil, 1), nil, contract, context)
if err == nil {
engine := nvm.NewV8Engine(ctx)
result, err := engine.RunScriptSource(string(data), 0)
Expand Down
2 changes: 2 additions & 0 deletions consensus/dpos/dpos.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var (
metricsBlockPackingTime = metrics.NewGauge("neb.block.packing")
metricsBlockWaitingTime = metrics.NewGauge("neb.block.waiting")
metricsLruPoolSlotBlock = metrics.NewGauge("neb.block.lru.poolslot")
metricsMintBlock = metrics.NewCounter("neb.block.mint")
)

// Dpos Delegate Proof-of-Stake
Expand Down Expand Up @@ -682,6 +683,7 @@ func (dpos *Dpos) mintBlock(now int64) error {
"end": time.Now().Unix(),
}).Info("Minted new block")

metricsMintBlock.Inc(1)
// try to push the new block on chain
// if failed, return all txs back

Expand Down
16 changes: 16 additions & 0 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,14 @@ func (block *Block) RandomSeed() string {
return ""
}

// RandomProof block random proof (VRF)
func (block *Block) RandomProof() string {
if block.height >= RandomAvailableHeight {
return byteutils.Hex(block.header.random.VrfProof)
}
return ""
}

// RandomAvailable check if Math.random available in contract
func (block *Block) RandomAvailable() bool {
return block.height >= RandomAvailableHeight
Expand Down Expand Up @@ -1290,3 +1298,11 @@ func LoadBlockFromStorage(hash byteutils.Hash, chain *BlockChain) (*Block, error
block.storage = chain.storage
return block, nil
}

// MockBlock nf/nvm/engine.CheckV8Run() & cmd/v8/main.go
func MockBlock(header *BlockHeader, height uint64) *Block {
return &Block{
header: header,
height: height,
}
}
7 changes: 4 additions & 3 deletions core/block_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,10 @@ func (pool *BlockPool) push(sender string, block *Block) error {
}
return ErrInvalidBlockCannotFindParentInLocalAndTrySync
}

if err := pool.downloadParent(sender, lb.block); err != nil {
return err
if !bc.IsActiveSyncing() {
if err := pool.downloadParent(sender, lb.block); err != nil {
return err
}
}
return ErrInvalidBlockCannotFindParentInLocalAndTryDownload
}
Expand Down
5 changes: 5 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,11 @@ func (bc *BlockChain) StartActiveSync() bool {
return false
}

// IsActiveSyncing returns true if being syncing
func (bc *BlockChain) IsActiveSyncing() bool {
return bc.syncService.IsActiveSyncing()
}

// ConsensusHandler return consensus handler.
func (bc *BlockChain) ConsensusHandler() Consensus {
return bc.consensusHandler
Expand Down
Loading

0 comments on commit fa4ad54

Please sign in to comment.