Skip to content

Commit

Permalink
Merge pull request #4 from youchainhq/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kevinho authored May 31, 2020
2 parents 4e94399 + 1c8ef00 commit 61dc840
Show file tree
Hide file tree
Showing 21 changed files with 134 additions and 86 deletions.
2 changes: 1 addition & 1 deletion bls/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"crypto/rand"
"errors"
"fmt"
"github.com/phoreproject/bls/g2pubs"
"github.com/youchainhq/bls/g2pubs"
"log"
)

Expand Down
2 changes: 1 addition & 1 deletion bls/keys_bls12_381.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package bls

import (
"errors"
"github.com/phoreproject/bls/g2pubs"
"github.com/youchainhq/bls/g2pubs"
)

var ErrSigMismatch = errors.New("signature mismatch")
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/flags_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (

cli.IntFlag{Name: "log.level", Value: int(logging.LvlInfo), Destination: &nodeCfg.LogLevel},
cli.StringFlag{Name: "log.vmodule", Destination: &nodeCfg.LogVmodule},
cli.StringFlag{Name: "log.path", Destination: &nodeCfg.LogPath, Value: "logs"},
cli.StringFlag{Name: "log.path", Destination: &nodeCfg.LogPath},

//use in metrics/metrics.go
cli.BoolFlag{Name: "metrics", Destination: &nodeCfg.Metrics.Metrics},
Expand Down
29 changes: 15 additions & 14 deletions consensus/ucon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ var (
errInvalidSealer = errors.New("invalid sealer")
)

var (
allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks
)

func (s *Server) Author(header *types.Header) (common.Address, error) {
return common.Address{}, nil
}
Expand All @@ -75,8 +71,13 @@ func (s *Server) verifyHeader(chain consensus.ChainReader, header *types.Header,
return errUnknownBlock
}

// get consensus parameters for round
yp, err := chain.VersionForRoundWithParents(header.Number.Uint64(), parents)
if err != nil {
return err
}
// Don't waste time checking blocks from the future
if header.Time > uint64(time.Now().Add(allowedFutureBlockTime).Unix()) {
if header.Time > uint64(time.Now().Add(yp.AllowedFutureBlockTime).Unix()) {
return consensus.ErrFutureBlock
}

Expand All @@ -90,7 +91,7 @@ func (s *Server) verifyHeader(chain consensus.ChainReader, header *types.Header,
return err
}

return s.verifyCascadingFields(chain, header, parents, seal)
return s.verifyCascadingFields(chain, header, parents, seal, yp)
}

func (s *Server) verifySignature(header *types.Header) error {
Expand Down Expand Up @@ -118,7 +119,7 @@ func (s *Server) verifySignature(header *types.Header) error {
// rather depend on a batch of previous headers. The caller may optionally pass
// in a batch of parents (ascending order) to avoid looking those up from the
// database. This is useful for concurrently verifying a batch of new headers.
func (s *Server) verifyCascadingFields(chain consensus.ChainReader, header *types.Header, parents []*types.Header, seal bool) error {
func (s *Server) verifyCascadingFields(chain consensus.ChainReader, header *types.Header, parents []*types.Header, seal bool, yp *params.YouParams) error {
// The genesis block is the always valid dead-end
number := header.Number.Uint64()
if number == 0 {
Expand Down Expand Up @@ -148,17 +149,13 @@ func (s *Server) verifyCascadingFields(chain consensus.ChainReader, header *type
return consensus.ErrExistCanonical
}
if seal {
return s.verifyConsensusField(chain, header, parents)
return s.verifyConsensusField(chain, header, parents, yp)
}
return nil
}

func (s *Server) verifyConsensusField(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error {
func (s *Server) verifyConsensusField(chain consensus.ChainReader, header *types.Header, parents []*types.Header, yp *params.YouParams) error {

yp, err := chain.VersionForRoundWithParents(header.Number.Uint64(), parents)
if err != nil {
return err
}
cp := &yp.CaravelParams
seedHeader, err := s.getLookBackHeader(cp, chain, header.Number, params.LookBackSeed, parents)
if err != nil {
Expand Down Expand Up @@ -580,7 +577,11 @@ func (s *Server) VerifySeal(chain consensus.ChainReader, header *types.Header) e
if err := s.verifySignature(header); err != nil {
return err
}
return s.verifyConsensusField(chain, header, nil)
yp, err := chain.VersionForRoundWithParents(header.Number.Uint64(), nil)
if err != nil {
return err
}
return s.verifyConsensusField(chain, header, nil, yp)
}

// VerifySideChainHeader checks whether a side chain block confirms to the consensus rules
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func DefaultGenesisBlock() *Genesis {
return &Genesis{
NetworkId: params.MainNetId,
Consensus: hexutil.MustDecode("0xf84e8001a05d93025288dddb431e3f43e07c63d1a96a28bf033457c74ee3f4d8eed88d3cf601a0010000000000000000000000000000000000000000000000000000000000000001801a8207d0820fa0"),
GasLimit: 8888888,
GasLimit: 0x888888,
Alloc: decodePrealloc(mainnetAllocData),
Validators: decodeValidators(mainnetValidatorsData),
CurrVersion: params.YouV1,
Expand Down
2 changes: 1 addition & 1 deletion core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func TestDefaultGenesisBlock(t *testing.T) {
require.Greater(t, len(g.Validators), 0)
db := youdb.NewMemDatabase()
b := g.ToBlock(db)
gh := common.HexToHash("0xa8fdcf4a6b91c0e5937309c812bc77f38ff4ae16294c24ae0c654f5b6fdaeeeb")
gh := common.HexToHash("0xce0efc6c8bd2195b53bf9cce7e7fff3bfc8aeba19b1f314ab029da68a521acf6")
if b.Hash() != gh {
t.Fatal("DefaultGenesisBlock hash mismatch")
}
Expand Down
3 changes: 2 additions & 1 deletion core/protocol_version_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func TestVerifyYouVersionState(t *testing.T) {
wantNoError bool
wantErr error
}
yp, ok := params.Versions[params.YouCurrentVersion]
// SHOULD NOT use params.YouCurrentVersion here, because the test case only test V1 or V1 upgrade to V2
yp, ok := params.Versions[params.YouV1]
require.True(t, ok)

tcs := []tcS{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ require (
github.com/nanyan/golz4 v1.0.0
github.com/pborman/uuid v0.0.0-20180827223501-4c1ecd6722e8
github.com/peterh/liner v1.1.0
github.com/phoreproject/bls v0.0.0-20191211001008-9d5f85bf4a9b
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563
github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d
github.com/rs/cors v0.0.0-20180826180256-dc7332ab32be
github.com/stretchr/testify v1.4.0
github.com/syndtr/goleveldb v1.0.0
github.com/urfave/cli v1.21.0
github.com/youchainhq/bls v0.9.0
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ github.com/pborman/uuid v0.0.0-20180827223501-4c1ecd6722e8 h1:1ugHtU31mw8H2BNoNj
github.com/pborman/uuid v0.0.0-20180827223501-4c1ecd6722e8/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34=
github.com/peterh/liner v1.1.0 h1:f+aAedNJA6uk7+6rXsYBnhdo4Xux7ESLe+kcuVUF5os=
github.com/peterh/liner v1.1.0/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0=
github.com/phoreproject/bls v0.0.0-20191211001008-9d5f85bf4a9b h1:tE/F54uL3jp0ZGSKNMPGCTF003pSmtD/sQojpKADAxY=
github.com/phoreproject/bls v0.0.0-20191211001008-9d5f85bf4a9b/go.mod h1:xHJKf2TLXUA39Dhv8k5QmQOxLsbrb1KeTS/3ERfLeqc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563 h1:dY6ETXrvDG7Sa4vE8ZQG4yqWg6UnOcbqTAahkV813vQ=
Expand All @@ -121,6 +119,8 @@ github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpP
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE=
github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ=
github.com/youchainhq/bls v0.9.0 h1:qM0uQu7nIBfQxn8eR+e5A+cQWOWsQfqJqPQZznXphcg=
github.com/youchainhq/bls v0.9.0/go.mod h1:IDYz3DRnRz/O11+C+Acp7D7yCxmUg6frH9JC6WKgpMg=
golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8=
golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
16 changes: 2 additions & 14 deletions internal/debug/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
package debug

import (
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/youchainhq/go-youchain/logging"
"io"
"os"
)

func SetupLogger(printOrigin bool, level int, vmodule string, logDir string) error {
Expand All @@ -36,20 +32,12 @@ func SetupLogger(printOrigin bool, level int, vmodule string, logDir string) err
}

if logDir != "" {
//terminal handler
usecolor := (isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())) && os.Getenv("TERM") != "dumb"
output := io.Writer(os.Stdout)
if usecolor {
output = colorable.NewColorableStderr()
}

//setup file rotate handler
config := logging.NewRotateConfig()
config.LogDir = logDir
rfh := logging.NewFileRotateHandler(config, logging.TerminalFormat(usecolor))
rfh := logging.NewFileRotateHandler(config, logging.TerminalFormat(false))

ostream := logging.StreamHandler(output, logging.TerminalFormat(usecolor))
logging.GRoot().SetHandler(logging.MultiHandler(ostream, rfh))
logging.GRoot().SetHandler(rfh)
}

return nil
Expand Down
6 changes: 5 additions & 1 deletion internal/youapi/you_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ func DoEstimateGas(ctx context.Context, c *Container, args CallArgs, blockNr rpc
hi uint64
cap uint64
)
if blockNr == rpc.PendingBlockNumber && !c.youChain.Miner().Mining() {
// only the working miner has pending block. if the miner is not working, change to the latest block.
blockNr = rpc.LatestBlockNumber
}
logging.Info("DoEstimateGas", "height", blockNr.Int64(), "gasCap", gasCap)
if args.Gas != nil && uint64(*args.Gas) >= params.TxGas {
hi = uint64(*args.Gas)
Expand All @@ -406,7 +410,7 @@ func DoEstimateGas(ctx context.Context, c *Container, args CallArgs, blockNr rpc
executable := func(gas uint64) bool {
args.Gas = (*hexutil.Uint64)(&gas)
logging.Info("DoEstimateGas start doCall", "height", blockNr.Int64(), "gasCap", gasCap, "cap", cap)
_, _, failed, err := DoCall(ctx, c, args, rpc.PendingBlockNumber, vm.LocalConfig{}, 0, gasCap, false)
_, _, failed, err := DoCall(ctx, c, args, blockNr, vm.LocalConfig{}, 0, gasCap, false)
if err != nil || failed {
logging.Error("DoEstimateGas failed", "err", err, "failed", failed)
return false
Expand Down
9 changes: 5 additions & 4 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,18 @@ func (w *worker) commitNewWork(interrupt *int32) {
coinbase.SetBytes(w.engine.GetValMainAddress().Bytes())

parent := w.chain.CurrentBlock()
timestamp := uint64(time.Now().Unix())

if uint64(time.Now().Unix()) <= parent.Time() {
log.Warn("local time is far behind")
return
if timestamp <= parent.Time() {
log.Warn("local time is far behind, use parent time plus one.")
timestamp = parent.Time() + 1
}

num := parent.Number()
header := &types.Header{
ParentHash: parent.Hash(),
Number: num.Add(num, common.Big1()),
Time: uint64(time.Now().Unix()),
Time: timestamp,
Coinbase: coinbase,
GasLimit: core.CalcGasLimit(parent),
GasRewards: big.NewInt(0),
Expand Down
6 changes: 4 additions & 2 deletions p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ type Peer struct {
}

// NewPeer returns a peer for testing purposes.
func NewPeer(id enode.ID, name string, caps []Cap) *Peer {
func NewPeer(id enode.ID, name string, caps []Cap, nodetype uint16) *Peer {
//pipe, _ := net.Pipe()
node := enode.SignNull(new(enr.Record), id)
r := new(enr.Record)
r.Set(enr.NODETYPE(nodetype))
node := enode.SignNull(r, id)
conn := &conn{session: nil, transport: nil, node: node, caps: caps, name: name}
peer := newPeer(conn, nil)
close(peer.closed) // ensures Disconnect doesn't block
Expand Down
Loading

0 comments on commit 61dc840

Please sign in to comment.