diff options
-rw-r--r-- | cmd/mist/gui.go | 2 | ||||
-rw-r--r-- | cmd/mist/ui_lib.go | 3 | ||||
-rw-r--r-- | core/block_processor.go | 7 | ||||
-rw-r--r-- | core/chain_manager.go | 28 | ||||
-rw-r--r-- | core/genesis.go | 5 | ||||
-rw-r--r-- | core/types/block.go | 14 | ||||
-rw-r--r-- | eth/backend.go | 3 | ||||
-rw-r--r-- | eth/protocol.go | 2 | ||||
-rw-r--r-- | ethutil/big.go | 9 | ||||
-rw-r--r-- | ethutil/math/dist.go | 80 | ||||
-rw-r--r-- | ethutil/math/dist_test.go | 66 | ||||
-rw-r--r-- | miner/worker.go | 10 |
12 files changed, 197 insertions, 32 deletions
diff --git a/cmd/mist/gui.go b/cmd/mist/gui.go index 4af0cff43..cbd8daf2f 100644 --- a/cmd/mist/gui.go +++ b/cmd/mist/gui.go @@ -159,8 +159,6 @@ func (gui *Gui) Stop() { gui.win.Hide() } - gui.uiLib.jsEngine.Stop() - guilogger.Infoln("Stopped") } diff --git a/cmd/mist/ui_lib.go b/cmd/mist/ui_lib.go index 4fa6e8e55..098e8fca5 100644 --- a/cmd/mist/ui_lib.go +++ b/cmd/mist/ui_lib.go @@ -58,7 +58,8 @@ type UiLib struct { } func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib { - lib := &UiLib{XEth: xeth.New(eth), engine: engine, eth: eth, assetPath: assetPath, jsEngine: javascript.NewJSRE(eth), filterCallbacks: make(map[int][]int)} //, filters: make(map[int]*xeth.JSFilter)} + x := xeth.New(eth) + lib := &UiLib{XEth: x, engine: engine, eth: eth, assetPath: assetPath, jsEngine: javascript.NewJSRE(x), filterCallbacks: make(map[int][]int)} //, filters: make(map[int]*xeth.JSFilter)} lib.filterManager = filter.NewFilterManager(eth.EventMux()) go lib.filterManager.Start() diff --git a/core/block_processor.go b/core/block_processor.go index c2799dc48..34c12729c 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -253,9 +253,6 @@ func (sm *BlockProcessor) ValidateHeader(block, parent *types.Header) error { return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd) } - //expl := CalcGasLimit(parent, block) - //if expl.Cmp(block.Header().GasLimit) != 0 { - // block.gasLimit - parent.gasLimit <= parent.gasLimit / 1024 a := new(big.Int).Sub(block.GasLimit, parent.GasLimit) b := new(big.Int).Div(parent.GasLimit, big.NewInt(1024)) @@ -263,8 +260,8 @@ func (sm *BlockProcessor) ValidateHeader(block, parent *types.Header) error { return fmt.Errorf("GasLimit check failed for block %v (%v > %v)", block.GasLimit, a, b) } - if block.Time < parent.Time { - return ValidationError("Block timestamp not after prev block (%v - %v)", block.Time, parent.Time) + if block.Time <= parent.Time { + return ValidationError("Block timestamp not after or equal to prev block (%v - %v)", block.Time, parent.Time) } if int64(block.Time) > time.Now().Unix() { diff --git a/core/chain_manager.go b/core/chain_manager.go index f2382cf8d..d2a6560c1 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -31,15 +31,18 @@ type StateQuery interface { func CalcDifficulty(block, parent *types.Header) *big.Int { diff := new(big.Int) - //adjust := new(big.Int).Rsh(parent.Difficulty(), 10) - //if block.Time() >= parent.Time()+8 { - adjust := new(big.Int).Div(parent.Difficulty, big.NewInt(2048)) + min := big.NewInt(2048) + adjust := new(big.Int).Div(parent.Difficulty, min) if (block.Time - parent.Time) < 8 { diff.Add(parent.Difficulty, adjust) } else { diff.Sub(parent.Difficulty, adjust) } + if diff.Cmp(GenesisDiff) < 0 { + return GenesisDiff + } + return diff } @@ -378,9 +381,12 @@ func (bc *ChainManager) Stop() { } func (self *ChainManager) InsertChain(chain types.Blocks) error { + println("insert chain start") self.tsmu.Lock() defer self.tsmu.Unlock() + defer println("insert chain end") + for _, block := range chain { // Call in to the block processor and check for errors. It's likely that if one block fails // all others will fail too (unless a known block is returned). @@ -422,14 +428,18 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { self.mu.Unlock() if canonical { - jsonlogger.LogJson(&logger.EthChainNewHead{ - BlockHash: ethutil.Bytes2Hex(block.Hash()), - BlockNumber: block.Number(), - ChainHeadHash: ethutil.Bytes2Hex(cblock.Hash()), - BlockPrevHash: ethutil.Bytes2Hex(block.ParentHash()), - }) + /* + jsonlogger.LogJson(&logger.EthChainNewHead{ + BlockHash: ethutil.Bytes2Hex(block.Hash()), + BlockNumber: block.Number(), + ChainHeadHash: ethutil.Bytes2Hex(cblock.Hash()), + BlockPrevHash: ethutil.Bytes2Hex(block.ParentHash()), + }) + */ self.setTransState(state.New(block.Root(), self.db)) self.eventMux.Post(ChainEvent{block, td}) + } else { + //self.eventMux. } if split { diff --git a/core/genesis.go b/core/genesis.go index f675be53f..a3f5dfb38 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -22,8 +22,10 @@ var ZeroHash512 = make([]byte, 64) var EmptyShaList = crypto.Sha3(ethutil.Encode([]interface{}{})) var EmptyListRoot = crypto.Sha3(ethutil.Encode("")) +var GenesisDiff = big.NewInt(131072) + func GenesisBlock(db ethutil.Database) *types.Block { - genesis := types.NewBlock(ZeroHash256, ZeroHash160, nil, big.NewInt(2048), 42, "") + genesis := types.NewBlock(ZeroHash256, ZeroHash160, nil, GenesisDiff, 42, "") genesis.Header().Number = ethutil.Big0 genesis.Header().GasLimit = big.NewInt(1000000) genesis.Header().GasUsed = ethutil.Big0 @@ -53,7 +55,6 @@ func GenesisBlock(db ethutil.Database) *types.Block { } statedb.Sync() genesis.Header().Root = statedb.Root() - fmt.Println(genesis) return genesis } diff --git a/core/types/block.go b/core/types/block.go index 18a21e8c6..31c7c2b87 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -40,12 +40,12 @@ type Header struct { Time uint64 // Extra data Extra string - // Nonce - Nonce []byte - // Mix digest for quick checking to prevent DOS - MixDigest ethutil.Bytes // SeedHash used for light client verification SeedHash ethutil.Bytes + // Mix digest for quick checking to prevent DOS + MixDigest ethutil.Bytes + // Nonce + Nonce []byte } func (self *Header) rlpData(withNonce bool) []interface{} { @@ -62,9 +62,11 @@ func (self *Header) rlpData(withNonce bool) []interface{} { self.GasLimit, self.GasUsed, self.Time, - self.Extra} + self.Extra, + self.SeedHash, + } if withNonce { - fields = append(fields, self.SeedHash, self.MixDigest, self.Nonce) + fields = append(fields, self.MixDigest, self.Nonce) } return fields diff --git a/eth/backend.go b/eth/backend.go index 8b76e0ca3..4e021a901 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -19,7 +19,6 @@ import ( "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/nat" - "github.com/ethereum/go-ethereum/pow/ezp" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/whisper" ) @@ -185,7 +184,7 @@ func New(config *Config) (*Ethereum, error) { hasBlock := eth.chainManager.HasBlock insertChain := eth.chainManager.InsertChain - eth.blockPool = blockpool.New(hasBlock, insertChain, ezp.Verify) + eth.blockPool = blockpool.New(hasBlock, insertChain, pow.Verify) netprv, err := config.nodeKey() if err != nil { diff --git a/eth/protocol.go b/eth/protocol.go index b86f33614..b52c7db42 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -256,7 +256,7 @@ func (self *ethProtocol) handle() error { return self.protoError(ErrDecode, "msg %v: %v", msg, err) } hash := request.Block.Hash() - fmt.Println("received block: %x", hash) + fmt.Printf("received block: %x\n", hash) _, chainHead, _ := self.chainManager.Status() jsonlogger.LogJson(&logger.EthChainReceivedNewBlock{ diff --git a/ethutil/big.go b/ethutil/big.go index 2ff1c72d8..b77e0af8c 100644 --- a/ethutil/big.go +++ b/ethutil/big.go @@ -25,12 +25,19 @@ func Big(num string) *big.Int { // BigD // // Shortcut for new(big.Int).SetBytes(...) -func BigD(data []byte) *big.Int { +func Bytes2Big(data []byte) *big.Int { n := new(big.Int) n.SetBytes(data) return n } +func BigD(data []byte) *big.Int { return Bytes2Big(data) } + +func String2Big(num string) *big.Int { + n := new(big.Int) + n.SetString(num, 0) + return n +} func BitTest(num *big.Int, i int) bool { return num.Bit(i) > 0 diff --git a/ethutil/math/dist.go b/ethutil/math/dist.go new file mode 100644 index 000000000..262aa8591 --- /dev/null +++ b/ethutil/math/dist.go @@ -0,0 +1,80 @@ +package math + +import ( + "math/big" + "sort" + + "github.com/ethereum/go-ethereum/ethutil" +) + +type Summer interface { + Sum(i int) *big.Int + Len() int +} + +func Sum(slice Summer) (sum *big.Int) { + sum = new(big.Int) + + for i := 0; i < slice.Len(); i++ { + sum.Add(sum, slice.Sum(i)) + } + return +} + +type Vector struct { + Gas, Price *big.Int +} + +type VectorsBy func(v1, v2 Vector) bool + +func (self VectorsBy) Sort(vectors []Vector) { + bs := vectorSorter{ + vectors: vectors, + by: self, + } + sort.Sort(bs) +} + +type vectorSorter struct { + vectors []Vector + by func(v1, v2 Vector) bool +} + +func (v vectorSorter) Len() int { return len(v.vectors) } +func (v vectorSorter) Less(i, j int) bool { return v.by(v.vectors[i], v.vectors[j]) } +func (v vectorSorter) Swap(i, j int) { v.vectors[i], v.vectors[j] = v.vectors[j], v.vectors[i] } + +func PriceSort(v1, v2 Vector) bool { return v1.Price.Cmp(v2.Price) < 0 } +func GasSort(v1, v2 Vector) bool { return v1.Gas.Cmp(v2.Gas) < 0 } + +type vectorSummer struct { + vectors []Vector + by func(v Vector) *big.Int +} + +type VectorSum func(v Vector) *big.Int + +func (v VectorSum) Sum(vectors []Vector) *big.Int { + vs := vectorSummer{ + vectors: vectors, + by: v, + } + return Sum(vs) +} + +func (v vectorSummer) Len() int { return len(v.vectors) } +func (v vectorSummer) Sum(i int) *big.Int { return v.by(v.vectors[i]) } + +func GasSum(v Vector) *big.Int { return v.Gas } + +var etherInWei = new(big.Rat).SetInt(ethutil.String2Big("1000000000000000000")) + +func GasPrice(bp, gl, ep *big.Int) *big.Int { + BP := new(big.Rat).SetInt(bp) + GL := new(big.Rat).SetInt(gl) + EP := new(big.Rat).SetInt(ep) + GP := new(big.Rat).Quo(BP, GL) + GP = GP.Quo(GP, EP) + + return GP.Mul(GP, etherInWei).Num() +} diff --git a/ethutil/math/dist_test.go b/ethutil/math/dist_test.go new file mode 100644 index 000000000..90e302f44 --- /dev/null +++ b/ethutil/math/dist_test.go @@ -0,0 +1,66 @@ +package math + +import ( + "fmt" + "math/big" + "testing" +) + +type summer struct { + numbers []*big.Int +} + +func (s summer) Len() int { return len(s.numbers) } +func (s summer) Sum(i int) *big.Int { + return s.numbers[i] +} + +func TestSum(t *testing.T) { + summer := summer{numbers: []*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}} + sum := Sum(summer) + if sum.Cmp(big.NewInt(6)) != 0 { + t.Errorf("not 6", sum) + } +} + +func TestDist(t *testing.T) { + var vectors = []Vector{ + Vector{big.NewInt(1000), big.NewInt(1234)}, + Vector{big.NewInt(500), big.NewInt(10023)}, + Vector{big.NewInt(1034), big.NewInt(1987)}, + Vector{big.NewInt(1034), big.NewInt(1987)}, + Vector{big.NewInt(8983), big.NewInt(1977)}, + Vector{big.NewInt(98382), big.NewInt(1887)}, + Vector{big.NewInt(12398), big.NewInt(1287)}, + Vector{big.NewInt(12398), big.NewInt(1487)}, + Vector{big.NewInt(12398), big.NewInt(1987)}, + Vector{big.NewInt(12398), big.NewInt(128)}, + Vector{big.NewInt(12398), big.NewInt(1987)}, + Vector{big.NewInt(1398), big.NewInt(187)}, + Vector{big.NewInt(12328), big.NewInt(1927)}, + Vector{big.NewInt(12398), big.NewInt(1987)}, + Vector{big.NewInt(22398), big.NewInt(1287)}, + Vector{big.NewInt(1370), big.NewInt(1981)}, + Vector{big.NewInt(12398), big.NewInt(1957)}, + Vector{big.NewInt(42198), big.NewInt(1987)}, + } + + VectorsBy(GasSort).Sort(vectors) + fmt.Println(vectors) + + BP := big.NewInt(15) + GL := big.NewInt(1000000) + EP := big.NewInt(100) + fmt.Println("BP", BP, "GL", GL, "EP", EP) + GP := GasPrice(BP, GL, EP) + fmt.Println("GP =", GP, "Wei per GU") + + S := len(vectors) / 4 + fmt.Println("L", len(vectors), "S", S) + for i := 1; i <= S*4; i += S { + fmt.Printf("T%d = %v\n", i, vectors[i]) + } + + g := VectorSum(GasSum).Sum(vectors) + fmt.Printf("G = ∑g* (%v)\n", g) +} diff --git a/miner/worker.go b/miner/worker.go index 38fc81ea5..29992b327 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -7,6 +7,7 @@ import ( "sync" "time" + "github.com/ethereum/ethash" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethutil" @@ -138,7 +139,7 @@ out: } break out case <-timer.C: - minerlogger.Debugln("Hash rate:", self.HashRate(), "Khash") + minerlogger.Infoln("Hash rate:", self.HashRate(), "Khash") } } @@ -164,7 +165,6 @@ func (self *worker) wait() { if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil { self.mux.Post(core.NewMinedBlockEvent{self.current.block}) - fmt.Println("GOOD BLOCK", self.current.block) } else { self.commitNewWork() } @@ -190,7 +190,11 @@ func (self *worker) commitNewWork() { self.mu.Lock() defer self.mu.Unlock() - self.current = env(self.chain.NewBlock(self.coinbase), self.eth) + block := self.chain.NewBlock(self.coinbase) + seednum := ethash.GetSeedBlockNum(block.NumberU64()) + block.Header().SeedHash = self.chain.GetBlockByNumber(seednum).SeedHash() + + self.current = env(block, self.eth) parent := self.chain.GetBlock(self.current.block.ParentHash()) self.current.coinbase.SetGasPool(core.CalcGasLimit(parent, self.current.block)) |