Brother even blockchain tutorial Open-ethereum-pool mining pool Source Code Analysis Unlocker module

Source: Internet
Author: User

Brother even blockchain tutorial Open-ethereum-pool Ethereum mine Pool source Analysis Unlocker module
Open-ethereum-pool Ethereum Mine Pool-unlocker module

Unlocker module Configuration

Json
"Unlocker": {
????" Enabled ": false,
????" Poolfee ": 1.0,
????" Poolfeeaddress ":" ",
????" Donate ": True,
????" Depth ": 120,
????" Immaturedepth ": 20,
????" Keeptxfees ": false,
????" Interval ":" 10m ",
????" Daemon ":" http://127.0.0.1:8545 ",
????" Timeout ":" 10s "
},

Blockunlocker definition

Go
Type Blockunlocker struct {
???? Config unlockerconfig
???? Backend
storage. Redisclient
???? RPC *rpc. Rpcclient
???? halt BOOL
???? Lastfail Error
}

Unlocker Flowchart Getcandidates Principle

Go
Func (r redisclient) getcandidates (maxheight Int64) ([] Blockdata, error) {
???? Zrangebyscore eth:blocks:candidates 0 maxheight withscores
???? Option: = Redis. Zrangebyscore{min: "0", Max:strconv. Formatint (MaxHeight, Ten)}
???? CMD: = R.client.zrangebyscorewithscores (R.formatkey ("blocks", "candidates"), option)
???? If Cmd. ERR ()! = nil {
???????? return nil, CMD. ERR ()
????}
???? return Convertcandidateresults (cmd), nil
}

Func convertcandidateresults (Raw redis. Zslicecmd) []Blockdata {
???? var result []*blockdata
???? for, V: = Range raw. Val () {
???????? "Nonce:powHash:mixDigest:timestamp:diff:totalShares"
???????? Block: = blockdata{}
???????? Block. Height = Int64 (V.score)
???????? Block. Roundheight = block. Height
???????? Fields: = Strings. Split (V.member. ( String), ":")
???????? Block. Nonce = Fields[0]
???????? Block. Powhash = fields[1]
???????? Block. Mixdigest = fields[2]
???????? Block. Timestamp,
= StrConv. parseint (Fields[3], 10, 64)
???????? Block. Difficulty, = StrConv. parseint (Fields[4], 10, 64)
???????? Block. Totalshares,
= StrConv. parseint (Fields[5], 10, 64)
???????? Block.candidatekey = V.member. (string)
???????? result = Append (result, &block)
????}
???? return result
}

Writeimmatureblock principle

Go
//immature is a minor
func (R redisclient) Writeimmatureblock (TX Redis. Multi, block *blockdata) {
???? Redis 2.8.x returns "ERR source and destination objects is the same"
???? If block. Height! = block. roundheight {
???????? RENAME eth:shares:candidates:round&roundheight:nonce eth:shares:candidates:round&blockheight:nonce
???????? Tx. Rename (R.formatround (block. Roundheight, block. Nonce), R.formatround (block. Height, block. Nonce))
????}
????
???? The Zrem command is used to remove one or more members from an ordered set, and non-existent members are ignored
???? Candidates as candidate
???? Zrem eth:blocks:candidates nonce:powHash:mixDigest:timestamp:diff:totalShares
???? Tx. Zrem (R.formatkey ("blocks", "candidates"), Block.candidatekey)
????
???? Zadd eth:blocks:immature block. Height UncleHeight:Orphan:Nonce:serializeHash:Timestamp:Difficulty:TotalShares:Reward
???? Tx. Zadd (R.formatkey ("blocks", "immature"), Redis. Z{score:float64 (block. Height), Member:block.key ()})
}

Func (b *blockdata) key () string {
???? Return join (B.uncleheight, B.orphan, B.nonce, B.serializehash (), B.timestamp, B.difficulty, B.totalshares, B.Reward)
}

Writeimmatureblock principle

Go
Func (R Redisclient) Writeimmatureblock (Block blockdata, roundrewards map[string]int64) error {
???? TX: = R.client.multi ()
???? Defer TX. Close ()

???? _, Err: = Tx. Exec (func () error {
???????? What is the purpose of writing a minor block?
???????? Supplemental Unlockpendingblocks () stage, all written to the minor block
???????? R.writeimmatureblock (TX, block)
???????? Total: = Int64 (0)
???????? Traverse Roundrewards
???????? For login, Amount: = Range Roundrewards {
???????????? Sum
???????????? Total + = Amount
???????????? The Hincrby command is used to add the specified increment value to the field value in the hash table
???????????? Hincrby Eth:miners:login Immature amount
???????????? Tx. Hincrby (R.formatkey ("miners", login), "immature", amount)
????????????
???????????? Hsetnx Eth:credits:immature:Height:Hash Login Amount
???????????? The HSETNX command is used to assign a value to a field that does not exist in the hash table
???????????? Tx. Hsetnx (R.formatkey ("credits", "immature", block. Height, block. Hash), login, StrConv. Formatint (amount, ten))
????????}
????????
???????? The Hincrby command is used to add the specified increment value to the field value in the hash table
???????? Hincrby eth:finances:immature Total
???????? Tx. Hincrby (R.formatkey ("finances"), "immature", total)
???????? return nil
????})
???? return err
}

Getimmatureblocks principle
Func (R *redisclient) getimmatureblocks (maxheight Int64) ([]*blockdata, error) {???? Option: = Redis. Zrangebyscore{min: "0", Max:strconv. Formatint (MaxHeight, Ten)}???? CMD: = R.client.zrangebyscorewithscores (R.formatkey ("blocks", "immature"), option)???? If Cmd. ERR ()! = Nil {???????? return nil, CMD. ERR ()????}???? return Convertblockresults (cmd), Nil}func convertblockresults (Rows ... *redis. Zslicecmd) []*blockdata {???? var result []*blockdata???? For _, Row: = Range rows {???????? For _, V: = Range row. Val () {???????????? "UncleHeight:orphan:nonce:blockHash:timestamp:diff:totalShares:rewardInWei"???????????? Block: = blockdata{}???????????? Block. Height = Int64 (V.score)???????????? Block. Roundheight = block. Height???????????? Fields: = Strings. Split (V.member. ( String), ":")???????????? Block. Uncleheight, _ = StrConv. parseint (Fields[0], ten,???????????? Block. Uncle = block. Uncleheight > 0???????????? Block. Orphan, _ = StrConv. Parsebool (Fields[1])???????????? Block. Nonce = fields[2]???????????? BlOck. Hash = fields[3]???????????? Block. Timestamp, _ = StrConv. parseint (Fields[4], ten,???????????? Block. Difficulty, _ = StrConv. parseint (Fields[5], ten,???????????? Block. Totalshares, _ = StrConv. parseint (Fields[6], ten,???????????? Block. rewardstring = fields[7]???????????? Block. Immaturereward = fields[7]???????????? Block.immaturekey = V.member. (string)???????????? result = Append (result, &block)????????}????}???? Return result}## Writeorphan principle func (R *redisclient) Writeorphan (block *blockdata) error {???? Creditkey: = R.formatkey ("Credits", "immature", block. Roundheight, block. Hash)???? TX, Err: = R.client.watch (creditkey)???? Must decrement immatures using existing log entry???? Immaturecredits: = Tx. Hgetallmap (Creditkey)???? If err! = Nil {???????? return err????}???? Defer TX. Close ()???? _, Err = Tx. Exec (func () error {???????? R.writematuredblock (TX, block)???????? Decrement immature balances???????? Totalimmature: = Int64 (0)???????? For login, amountstring: = Range immaturecredIts. Val () {???????????? Amount, _: = StrConv. parseint (amountstring, ten)???????????? Totalimmature + = Amount???????????? Tx. Hincrby (R.formatkey ("miners", login), "immature", (Amount *-1))????????}???????? Tx. Del (Creditkey)???????? Tx. Hincrby (R.formatkey ("finances"), "immature", (totalimmature *-1))???????? return nil????})???? Return Err}func (R *redisclient) Writematuredblock (TX *redis. Multi, block *blockdata) {Tx. Del (R.formatround (block. Roundheight, block. Nonce)) Tx. Zrem (R.formatkey ("blocks", "immature"), Block.immaturekey) Tx. Zadd (R.formatkey ("blocks", "matured"), Redis. Z{score:float64 (block. Height), Member:block.key ()})}

Brother chain Tutorial Open-ethereum-pool Mining Pool Source Code Analysis unlocker module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.