Skip to content

Commit

Permalink
Merge pull request #85 from MinterTeam/dev
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
danil-lashin authored Aug 22, 2018
2 parents 9d600c3 + b011a14 commit 448914a
Show file tree
Hide file tree
Showing 36 changed files with 748 additions and 221 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@

- [api] Add validators rewards to block api

## 0.2.0
*Aug 22th, 2018*

BREAKING CHANGES

- [testnet] New testnet id
- [core] New rewards
- [core] Validators list are now updated each 12 blocks
- [core] Set DAO commission to 10%
- [core] Add Developers commission of 10%
- [core] Now stake of custom coin is calculated by selling all such staked coins
- [api] Reformatted candidates and validators endpoints
- [api] tx.return tags are now encoded as strings

IMPROVEMENT

- [tendermint] Update tendermint to 0.23.0
- [api] Add block reward to api
- [api] Add bip_value field to Stake
- [api] Add /api/candidates endpoint
- [api] Add /api/estimateTxCommission endpoint
- [gui] Minor GUI update

## 0.1.9
*Aug 19th, 2018*

Expand Down
15 changes: 12 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

[[constraint]]
name = "github.com/tendermint/tendermint"
version = "=v0.22.8"
version = "=v0.23.0"

[[constraint]]
branch = "v1"
Expand Down
2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func RunApi(b *minter.Blockchain, node *node.Node) {
router := mux.NewRouter().StrictSlash(true)

router.HandleFunc("/api/bipVolume", GetBipVolume).Methods("GET")
router.HandleFunc("/api/candidates", GetCandidates).Methods("GET")
router.HandleFunc("/api/candidate/{pubkey}", GetCandidate).Methods("GET")
router.HandleFunc("/api/validators", GetValidators).Methods("GET")
router.HandleFunc("/api/balance/{address}", GetBalance).Methods("GET")
Expand All @@ -52,6 +53,7 @@ func RunApi(b *minter.Blockchain, node *node.Node) {
router.HandleFunc("/api/coinInfo/{symbol}", GetCoinInfo).Methods("GET")
router.HandleFunc("/api/estimateCoinSell", EstimateCoinSell).Methods("GET")
router.HandleFunc("/api/estimateCoinBuy", EstimateCoinBuy).Methods("GET")
router.HandleFunc("/api/estimateTxCommission", EstimateTxCommission).Methods("GET")

c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
Expand Down
9 changes: 6 additions & 3 deletions api/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"fmt"
"github.com/MinterTeam/minter-go-node/core/rewards"
"github.com/MinterTeam/minter-go-node/core/transaction"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/gorilla/mux"
Expand All @@ -21,6 +22,7 @@ type BlockResponse struct {
TotalTxs int64 `json:"total_txs"`
Transactions []BlockTransactionResponse `json:"transactions"`
Precommits json.RawMessage `json:"precommits"`
BlockReward string `json:"block_reward"`
}

type BlockTransactionResponse struct {
Expand Down Expand Up @@ -51,8 +53,8 @@ func Block(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(Response{
Code: 0,
Result: err.Error(),
Code: 0,
Log: err.Error(),
})
return
}
Expand Down Expand Up @@ -97,8 +99,9 @@ func Block(w http.ResponseWriter, r *http.Request) {
Time: block.Block.Time,
NumTxs: block.Block.NumTxs,
TotalTxs: block.Block.TotalTxs,
Precommits: json.RawMessage(precommits),
Transactions: txs,
Precommits: json.RawMessage(precommits),
BlockReward: rewards.GetRewardForBlock(uint64(height)).String(),
}

err = json.NewEncoder(w).Encode(Response{
Expand Down
2 changes: 1 addition & 1 deletion api/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetCandidate(w http.ResponseWriter, r *http.Request) {
Result: struct {
Candidate Candidate `json:"candidate"`
}{
Candidate: makeResponseCandidate(*candidate),
Candidate: makeResponseCandidate(*candidate, cState),
},
})
}
28 changes: 28 additions & 0 deletions api/candidates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package api

import (
"encoding/json"
"net/http"
)

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

cState := GetStateForRequest(r)

candidates := cState.GetStateCandidates().GetData()

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

var result []Candidate

for _, candidate := range candidates {
result = append(result, makeResponseCandidate(candidate, cState))
}

w.WriteHeader(http.StatusOK)

json.NewEncoder(w).Encode(Response{
Code: 0,
Result: result,
})
}
61 changes: 61 additions & 0 deletions api/estimate_tx_commission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package api

import (
"encoding/json"
"fmt"
"github.com/MinterTeam/minter-go-node/core/transaction"
"github.com/MinterTeam/minter-go-node/formula"
"github.com/MinterTeam/minter-go-node/hexutil"
"math/big"
"net/http"
)

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

cState := GetStateForRequest(r)

query := r.URL.Query()
rawTx := query.Get("tx")
bytesTx, _ := hexutil.Decode(rawTx)

tx, err := transaction.DecodeFromBytes(bytesTx)

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

commissionInBaseCoin := big.NewInt(0).Mul(tx.GasPrice, big.NewInt(tx.Gas()))
commissionInBaseCoin.Mul(commissionInBaseCoin, transaction.CommissionMultiplier)
commission := big.NewInt(0).Set(commissionInBaseCoin)

if !tx.GasCoin.IsBaseCoin() {
coin := cState.GetStateCoin(tx.GasCoin)

if coin.ReserveBalance().Cmp(commissionInBaseCoin) < 0 {

w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(Response{
Code: 0,
Log: fmt.Sprintf("Coin reserve balance is not sufficient for transaction. Has: %s, required %s", coin.ReserveBalance().String(), commissionInBaseCoin.String()),
})
return
}

commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin)
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Response{
Code: 0,
Result: struct {
Commission string `json:"commission"`
}{
Commission: commission.String(),
},
})
}
55 changes: 42 additions & 13 deletions api/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,62 @@ import (
"fmt"
"github.com/MinterTeam/minter-go-node/core/state"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/MinterTeam/minter-go-node/core/validators"
"net/http"
"strconv"
)

type Stake struct {
Owner types.Address `json:"owner"`
Coin types.CoinSymbol `json:"coin"`
Value string `json:"value"`
BipValue string `json:"bip_value"`
}

type Candidate struct {
CandidateAddress types.Address `json:"candidate_address"`
TotalStake string `json:"total_stake"`
PubKey string `json:"pub_key"`
Commission uint `json:"commission"`
AccumReward string `json:"accumulated_reward"`
Stakes []state.Stake `json:"stakes"`
Stakes []Stake `json:"stakes"`
CreatedAtBlock uint `json:"created_at_block"`
Status byte `json:"status"`
AbsentTimes uint `json:"absent_times"`
}

func makeResponseCandidate(c state.Candidate) Candidate {
type Validator struct {
AccumReward string `json:"accumulated_reward"`
AbsentTimes uint `json:"absent_times"`
Candidate Candidate `json:"candidate"`
}

func makeResponseValidator(v state.Validator, state *state.StateDB) Validator {
return Validator{
AccumReward: v.AccumReward.String(),
AbsentTimes: v.AbsentTimes,
Candidate: makeResponseCandidate(*state.GetStateCandidate(v.PubKey), state),
}
}

func makeResponseCandidate(c state.Candidate, state *state.StateDB) Candidate {

stakes := make([]Stake, len(c.Stakes))

for i, stake := range c.Stakes {
stakes[i] = Stake{
Owner: stake.Owner,
Coin: stake.Coin,
Value: stake.Value.String(),
BipValue: stake.BipValue(state).String(),
}
}

return Candidate{
CandidateAddress: c.CandidateAddress,
TotalStake: c.TotalBipStake.String(),
PubKey: fmt.Sprintf("Mp%x", c.PubKey),
Commission: c.Commission,
AccumReward: c.AccumReward.String(),
Stakes: c.Stakes,
Stakes: stakes,
CreatedAtBlock: c.CreatedAtBlock,
Status: c.Status,
AbsentTimes: c.AbsentTimes,
}
}

Expand All @@ -44,19 +72,20 @@ func GetValidators(w http.ResponseWriter, r *http.Request) {
height = int(blockchain.Height())
}

_, candidates := GetStateForRequest(r).GetValidators(validators.GetValidatorsCountForBlock(uint64(height)))
rState := GetStateForRequest(r)
vals := rState.GetStateValidators().Data()

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

var responseCandidates []Candidate
var responseValidators []Validator

for _, candidate := range candidates {
responseCandidates = append(responseCandidates, makeResponseCandidate(candidate))
for _, val := range vals {
responseValidators = append(responseValidators, makeResponseValidator(val, rState))
}

json.NewEncoder(w).Encode(Response{
Code: 0,
Result: responseCandidates,
Result: responseValidators,
})
}
2 changes: 1 addition & 1 deletion cmd/minter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func startTendermintNode(app *minter.Blockchain) *tmNode.Node {
proxy.NewLocalClientCreator(app),
genesis.GetTestnetGenesis,
tmNode.DefaultDBProvider,
tmNode.DefaultMetricsProvider,
tmNode.DefaultMetricsProvider(cfg.Instrumentation),
log.With("module", "tendermint"),
)

Expand Down
4 changes: 2 additions & 2 deletions core/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package dao
import "github.com/MinterTeam/minter-go-node/core/types"

var (
Address = types.HexToAddress("Mxa93163fdf10724dc4785ff5cbfb9ac0b5949409f")
Commission = 5
Address = types.HexToAddress("Mxee81347211c72524338f9680072af90744333146")
Commission = 10
)
8 changes: 8 additions & 0 deletions core/developers/developers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package developers

import "github.com/MinterTeam/minter-go-node/core/types"

var (
Address = types.HexToAddress("Mx444c4f1953ea170f74eabef4eee52ed8276a7d5e")
Commission = 10
)
1 change: 1 addition & 0 deletions core/developers/developers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package developers
Loading

0 comments on commit 448914a

Please sign in to comment.