Skip to content

Commit

Permalink
Merge pull request #25 from MinterTeam/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
danil-lashin authored Jun 13, 2018
2 parents 3c154a0 + 0e9a7d1 commit 93fb051
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 46 deletions.
52 changes: 42 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,55 @@ _NOTE: This is alpha software. Please contact us if you intend to run it in prod
You'll need [docker](https://docker.com/) and [docker compose](https://docs.docker.com/compose/) installed.

Clone Minter to your machine
```
git clone https://github.com/MinterTeam/minter-go-node.git
cd minter
```bash
$ git clone https://github.com/MinterTeam/minter-go-node.git
$ cd minter-go-node
```

Prepare configs
```
mkdir -p ~/.tendermint/data
mkdir -p ~/.minter/data
```bash
$ mkdir -p ~/.tendermint/data
$ mkdir -p ~/.minter/data

chmod -R 0777 ~/.tendermint
chmod -R 0777 ~/.minter
$ chmod -R 0777 ~/.tendermint
$ chmod -R 0777 ~/.minter

cp -R networks/testnet/ ~/.tendermint/config
$ cp -R networks/testnet/ ~/.tendermint/config
```

Start Minter
```bash
$ docker-compose up
```
docker-compose up

## Build and run manually

Install [Tendermint 0.20](https://github.com/tendermint/tendermint/blob/master/docs/install.rst)

```bash
$ mkdir $GOPATH/src/github.com/MinterTeam
$ cd $GOPATH/src/github.com/MinterTeam
$ git clone https://github.com/MinterTeam/minter-go-node.git

$ cd minter-go-node
$ make get_tools
$ make get_vendor_deps

$ make install

$ mkdir -p ~/.tendermint/data
$ mkdir -p ~/.minter/data

$ cp -R networks/testnet/ ~/.tendermint/config
```

Run Tendermint
```bash
$ tendermint node
```

Run Minter

```bash
$ minter
```
14 changes: 14 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (

"github.com/MinterTeam/minter-go-node/cmd/utils"
"github.com/MinterTeam/minter-go-node/core/minter"
"github.com/MinterTeam/minter-go-node/core/state"
"github.com/tendermint/tendermint/rpc/core/types"
"github.com/tendermint/tendermint/rpc/lib/client"
"strconv"
"time"
)

Expand Down Expand Up @@ -68,3 +70,15 @@ type Response struct {
Result interface{} `json:"result,omitempty"`
Log string `json:"log,omitempty"`
}

func GetStateForRequest(r *http.Request) *state.StateDB {
height, _ := strconv.Atoi(r.URL.Query().Get("height"))

cState := blockchain.CurrentState()

if height > 0 {
cState, _ = blockchain.GetStateForHeight(height)
}

return cState
}
4 changes: 3 additions & 1 deletion api/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ type BalanceRequest struct {

func GetBalance(w http.ResponseWriter, r *http.Request) {

cState := GetStateForRequest(r)

vars := mux.Vars(r)
address := types.HexToAddress(vars["address"])

balance := BalanceResponse{}
balances := blockchain.CurrentState().GetBalances(address)
balances := cState.GetBalances(address)

for k, v := range balances.Data {
balance[k.String()] = v.String()
Expand Down
12 changes: 8 additions & 4 deletions api/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ func Block(w http.ResponseWriter, r *http.Request) {
"height": height,
}, result)

w.Header().Set("Content-Type", "application/json; charset=UTF-8")

if err != nil {
panic(err)
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(Response{
Code: 0,
Result: err.Error(),
})
return
}

// TODO: check error

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)

err = json.NewEncoder(w).Encode(Response{
Expand Down
9 changes: 1 addition & 8 deletions api/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/gorilla/mux"
"net/http"
"strconv"
"strings"
)

Expand All @@ -16,13 +15,7 @@ func GetCandidate(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pubkey := types.Hex2Bytes(strings.TrimLeft(vars["pubkey"], "Mx"))

height, _ := strconv.Atoi(r.URL.Query().Get("height"))

cState := blockchain.CurrentState()

if height > 0 {
cState, _ = blockchain.GetStateForHeight(height)
}
cState := GetStateForRequest(r)

candidate := cState.GetStateCandidate(pubkey)

Expand Down
4 changes: 3 additions & 1 deletion api/coin_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ type CoinInfoResponse struct {

func GetCoinInfo(w http.ResponseWriter, r *http.Request) {

cState := GetStateForRequest(r)

vars := mux.Vars(r)
symbol := vars["symbol"]

var coinSymbol types.CoinSymbol

copy(coinSymbol[:], []byte(symbol))

coin := blockchain.CurrentState().GetStateCoin(coinSymbol).Data()
coin := cState.GetStateCoin(coinSymbol).Data()

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
Expand Down
10 changes: 6 additions & 4 deletions api/estimate_coin_exchange_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

func EstimateCoinExchangeReturn(w http.ResponseWriter, r *http.Request) {

cState := GetStateForRequest(r)

query := r.URL.Query()
fromCoin := query.Get("from_coin")
toCoin := query.Get("to_coin")
Expand All @@ -24,14 +26,14 @@ func EstimateCoinExchangeReturn(w http.ResponseWriter, r *http.Request) {
var result *big.Int

if fromCoinSymbol == blockchain.BaseCoin {
coin := blockchain.CurrentState().GetStateCoin(toCoinSymbol).Data()
coin := cState.GetStateCoin(toCoinSymbol).Data()
result = formula.CalculatePurchaseReturn(coin.Volume, coin.ReserveBalance, coin.Crr, value)
} else if toCoinSymbol == blockchain.BaseCoin {
coin := blockchain.CurrentState().GetStateCoin(fromCoinSymbol).Data()
coin := cState.GetStateCoin(fromCoinSymbol).Data()
result = formula.CalculateSaleReturn(coin.Volume, coin.ReserveBalance, coin.Crr, value)
} else {
coinFrom := blockchain.CurrentState().GetStateCoin(fromCoinSymbol).Data()
coinTo := blockchain.CurrentState().GetStateCoin(toCoinSymbol).Data()
coinFrom := cState.GetStateCoin(fromCoinSymbol).Data()
coinTo := cState.GetStateCoin(toCoinSymbol).Data()

val := formula.CalculateSaleReturn(coinFrom.Volume, coinFrom.ReserveBalance, coinFrom.Crr, value)
result = formula.CalculatePurchaseReturn(coinTo.Volume, coinTo.ReserveBalance, coinTo.Crr, val)
Expand Down
12 changes: 8 additions & 4 deletions api/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ func Transaction(w http.ResponseWriter, r *http.Request) {
"hash": decoded,
}, result)

w.Header().Set("Content-Type", "application/json; charset=UTF-8")

if err != nil {
panic(err)
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(Response{
Code: 0,
Result: err.Error(),
})
return
}

// TODO: check error

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)

err = json.NewEncoder(w).Encode(Response{
Expand Down
4 changes: 3 additions & 1 deletion api/transaction_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type TransactionCountResponse uint64

func GetTransactionCount(w http.ResponseWriter, r *http.Request) {

cState := GetStateForRequest(r)

vars := mux.Vars(r)
address := types.HexToAddress(vars["address"])

Expand All @@ -19,6 +21,6 @@ func GetTransactionCount(w http.ResponseWriter, r *http.Request) {

json.NewEncoder(w).Encode(Response{
Code: 0,
Result: blockchain.CurrentState().GetNonce(address),
Result: cState.GetNonce(address),
})
}
14 changes: 9 additions & 5 deletions api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,18 @@ func Transactions(w http.ResponseWriter, r *http.Request) {
"per_page": 100,
}, rpcResult)

w.Header().Set("Content-Type", "application/json; charset=UTF-8")

if err != nil {
panic(err)
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(Response{
Code: 0,
Result: err.Error(),
})
return
}

// TODO: check error
w.WriteHeader(http.StatusOK)

result := make([]TransactionResponse, len(rpcResult.Txs))

Expand Down Expand Up @@ -92,9 +99,6 @@ func Transactions(w http.ResponseWriter, r *http.Request) {
}
}

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)

err = json.NewEncoder(w).Encode(Response{
Code: 0,
Result: result,
Expand Down
36 changes: 28 additions & 8 deletions core/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,16 @@ type DeclareCandidacyData struct {

func (s DeclareCandidacyData) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
// TODO: complete marshal function
}{})
Address types.Address
PubKey string
Commission uint
Stake string
}{
Address: s.Address,
PubKey: fmt.Sprintf("Mp%x", s.PubKey),
Commission: s.Commission,
Stake: s.Stake.String(),
})
}

type DelegateData struct {
Expand All @@ -152,8 +160,12 @@ type DelegateData struct {

func (s DelegateData) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
// TODO: complete marshal function
}{})
PubKey string
Stake string
}{
PubKey: fmt.Sprintf("Mp%x", s.PubKey),
Stake: s.Stake.String(),
})
}

type RedeemCheckData struct {
Expand All @@ -163,8 +175,12 @@ type RedeemCheckData struct {

func (s RedeemCheckData) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
// TODO: complete marshal function
}{})
RawCheck string
Proof string
}{
RawCheck: fmt.Sprintf("Mc%x", s.RawCheck),
Proof: fmt.Sprintf("%x", s.Proof),
})
}

type UnbondData struct {
Expand All @@ -174,8 +190,12 @@ type UnbondData struct {

func (s UnbondData) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
// TODO: complete marshal function
}{})
PubKey string
Value string
}{
PubKey: fmt.Sprintf("Mp%x", s.PubKey),
Value: s.Value.String(),
})
}

func (tx *Transaction) Serialize() ([]byte, error) {
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
- ~/.tendermint:/tendermint
ports:
- "46656:46656"
- "46657:46657"
restart: always
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:46657/health"]
Expand Down

0 comments on commit 93fb051

Please sign in to comment.