Skip to content

Commit

Permalink
Made the verification texts use the new mining pool + app (ethereum#4)
Browse files Browse the repository at this point in the history
* Switched to new mobile mining app.

* Fixed a bug.

* Cleaned up some code and added comments.

* Added todo and reformatted.

* Small changes.
  • Loading branch information
marekolszewski authored Jun 7, 2018
1 parent b22967e commit b507ead
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 64 deletions.
99 changes: 99 additions & 0 deletions abe/abe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2017 The Celo Authors
// This file is part of the celo library.
//
// The celo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The celo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the celo library. If not, see <http://www.gnu.org/licenses/>.

package abe

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
)

func SendVerificationTexts(block *types.Block, coinbase common.Address, accountManager *accounts.Manager) {
numTransactions := 0
for range block.Transactions() {
numTransactions += 1
}
log.Debug("\n[Celo] New block: "+block.Hash().Hex()+", "+strconv.Itoa(numTransactions), nil, nil)

wallet, err := accountManager.Find(accounts.Account{Address: coinbase})
if err != nil {
log.Error("[Celo] Failed to get account", "err", err)
return
}

// Iterate through all transactions in the currently sealed block to find verification
// transactions.
for _, tx := range block.Transactions() {
data := string(tx.Data())
log.Debug("[Celo] Block transaction:: "+tx.Hash().Hex()+" "+data, nil, nil)

if len(data) > 0 && (strings.HasPrefix(data, "reqVerify") || strings.HasPrefix(data, "reqAndVerify")) {
// Get the phone number to send the secret to.
dataArray := strings.Split(data, "-")
phone := dataArray[len(dataArray)-1]
log.Debug("[Celo] Sending text to phone: "+phone, nil, nil)

if len(phone) <= 0 {
log.Error("[Celo] Invalid phone number: "+phone, nil, nil)
continue
}

// Construct the secret code to be sent via SMS.
nonce := tx.Nonce()
unsignedCode := common.BytesToHash([]byte(string(phone) + string(nonce)))
code, err := wallet.SignHash(accounts.Account{Address: coinbase}, unsignedCode.Bytes())
if err != nil {
log.Error("[Celo] Failed to sign phone number for sending over SMS", "err", err)
continue
}
hexCode := hexutil.Encode(code[:])
log.Debug("[Celo] Secret code: "+hexCode+" "+string(len(code)), nil, nil)
secret := fmt.Sprintf("Gem verification code: %s", hexCode)
log.Debug("[Celo] New verification request: "+tx.Hash().Hex()+" "+phone, nil, nil)

// Send the actual text message using our mining pool.
// TODO: Make mining pool be configurable via command line arguments.
url := "https://mining-pool.celo.org/send-text"
values := map[string]string{"phoneNumber": phone, "message": secret}
jsonValue, _ := json.Marshal(values)
_, err = http.Post(url, "application/json", bytes.NewBuffer(jsonValue))
log.Debug("[Celo] SMS send Url: "+url, nil, nil)

// Retry 5 times if we fail.
for i := 0; i < 5; i++ {
if err == nil {
break
}
log.Debug("[Celo] Got an error when trying to send SMS to: "+url, nil, nil)
time.Sleep(100 * time.Millisecond)
_, err = http.Post(url, "application/json", bytes.NewBuffer(jsonValue))
}

log.Debug("[Celo] Sent SMS", nil, nil)
}
}
}
67 changes: 3 additions & 64 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ import (
"sync"
"sync/atomic"
"time"
"net/http"
"net/url"
"strings"
"strconv"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/abe"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc"
Expand All @@ -40,7 +36,6 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/common/hexutil"
"gopkg.in/fatih/set.v0"
)

Expand Down Expand Up @@ -327,7 +322,6 @@ func (self *worker) wait() {
mustCommitNewWork = false
}


// Broadcast the block and announce chain insertion event
self.mux.Post(core.NewMinedBlockEvent{Block: block})
var (
Expand All @@ -340,61 +334,8 @@ func (self *worker) wait() {
}
self.chain.PostChainEvents(events, logs)


// Added for Gem.
numTransactions := 0
for range block.Transactions() {
numTransactions += 1
}

log.Debug("\n!!! New block: " + block.Hash().Hex() + ", " + strconv.Itoa(numTransactions), nil, nil)
wallet, err := self.eth.AccountManager().Find(accounts.Account{Address: self.coinbase})

for _, tx := range block.Transactions() {
data := string(tx.Data())

log.Debug("!!! PM - TX: " + tx.Hash().Hex() + " " + data, nil, nil)

if len(data) > 0 && (strings.HasPrefix(data, "reqVerify") || strings.HasPrefix(data, "reqAndVerify")) {
dataArray := strings.Split(data, "-")
phone := dataArray[len(dataArray) - 1]
log.Debug("!!! PM - phone: " + phone, nil, nil)

nonce := tx.Nonce()
unsignedCode := common.BytesToHash([]byte(string(phone) + string(nonce)))
code, err := wallet.SignHash(accounts.Account{Address: self.coinbase}, unsignedCode.Bytes())
if (err != nil) {
log.Error("!!! Failed to sign phone number for sending over SMS", "err", err)
continue
}

hexCode := hexutil.Encode(code[:])
log.Debug("!!! PM - New code: " + hexCode + " " + string(len(code)), nil, nil)

msg := fmt.Sprintf("Gem verification code: %s", hexCode)
secret := url.QueryEscape(msg)
log.Debug("!!! PM - New Verification request: " + tx.Hash().Hex() + " " + phone, nil, nil)
if len(phone) > 0 {
ip := "24.130.115.83"
url := fmt.Sprintf("http://%s:8081/?phone=%s&msg=%s", ip, phone, secret)
log.Debug("!!! PM - SMS Url: " + url, nil, nil)

_, err := http.Get(url)

for i := 0; i < 5; i++ {
if (err == nil) {
break
}
log.Debug("!!! PM - GOT AN ERROR WHEN TRYING TO SEND SMS TO: " + url, nil, nil)
time.Sleep(100 * time.Millisecond)
_, err = http.Get(url)
}

log.Debug("!!! PM - Sent SMS", nil, nil)
}
}
}
// End added for Gem.
// Added for Celo
abe.SendVerificationTexts(block, self.coinbase, self.eth.AccountManager())

// Insert the block into the set of pending ones to wait for confirmations
self.unconfirmed.Insert(block.NumberU64(), block.Hash())
Expand Down Expand Up @@ -549,8 +490,6 @@ func (self *worker) commitNewWork() {
return
}

// log.Debug("!!! Sealed new block: " + work.Block.Hash().Hex(), nil, nil)

// We only care about logging if we're actually mining.
if atomic.LoadInt32(&self.mining) == 1 {
log.Info("Commit new mining work", "number", work.Block.Number(), "txs", work.tcount, "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart)))
Expand Down

0 comments on commit b507ead

Please sign in to comment.