Skip to content

Commit

Permalink
Merge pull request #737 from MinterTeam/dev
Browse files Browse the repository at this point in the history
v2.6.1
  • Loading branch information
danil-lashin committed Dec 28, 2021
2 parents 6b078d8 + f747393 commit 38a6ddb
Show file tree
Hide file tree
Showing 50 changed files with 4,267 additions and 1,943 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
docker:
env:
CONTAINER_NAME: minter_node
CONTAINER_TIMEOUT_SEC: 30
CONTAINER_TIMEOUT_SEC: 60
API_RUN_PORT: 8843
SECRET_DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_HUB_PASSWORD }}
SECRET_DOCKER_HUB_USER: ${{ secrets.DOCKER_HUB_USER }}
Expand All @@ -27,7 +27,7 @@ jobs:
with:
fetch-depth: 1

# if secret DOCKER_HUB_REPO is not set DOCKER_HUB_USER will be used instead of REPO
# if secret DOCKER_HUB_REPO is not set DOCKER_HUB_USER will be used instead of REPO
# otherwise secrets are empty and repo "testbuild" will be used
- name: Set envs
env:
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: docker

on:
push:
branches:
- master
- dev

pull_request:
branches:
- master
- dev

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.17'
- run: go mod tidy && go build ./...
- name: Test
run: go test -cover ./...
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## [v2.6.1](https://github.com/MinterTeam/minter-go-node/tree/v2.6.1)

[Full Changelog](https://github.com/MinterTeam/minter-go-node/compare/v2.6.0...v2.6.1)

### Added

- Support for state sync snapshots

### Fixed

- Bug evaluating the exchange of coins through a bankor in API `/v2/estimate_coin_sell_all`

## [v2.6.0](https://github.com/MinterTeam/minter-go-node/tree/v2.6.0)

[Full Changelog](https://github.com/MinterTeam/minter-go-node/compare/v2.5.0...v2.6.0)
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile-ci
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ WORKDIR /minter
RUN mkdir /minter/data
EXPOSE 8843
ENTRYPOINT ["/usr/bin/minter"]
CMD ["node", "--home-dir", "/minter", "--genesis", "https://github.com/MinterTeam/minter-go-node/releases/download/v1.2-testnet-9/genesis.json"]
CMD ["node", "--home-dir", "/minter", "--state-sync"]
5 changes: 4 additions & 1 deletion api/v2/service/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ func (s *Service) Candidate(ctx context.Context, req *pb.CandidateRequest) (*pb.

if req.Height != 0 {
cState.Candidates().LoadCandidates()
cState.Candidates().LoadStakesOfCandidate(pubkey)
}

candidate := cState.Candidates().GetCandidate(pubkey)
if candidate == nil {
return nil, status.Error(codes.NotFound, "Candidate not found")
}

if req.Height != 0 {
cState.Candidates().LoadStakesOfCandidate(pubkey)
}

result := makeResponseCandidate(cState, candidate, true, req.NotShowStakes)
if cState.Validators().GetByPublicKey(candidate.PubKey) != nil {
result.Validator = true
Expand Down
3 changes: 2 additions & 1 deletion api/v2/service/estimate_coin_buy.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (s *Service) calcBuyFromPool(ctx context.Context, value *big.Int, cState *s
coinSell = cState.Coins().GetCoin(sellCoinID)
}

errResp, sellValue := transaction.CheckSwap(swapChecker, coinSell, coinBuy, coins.MaxCoinSupply(), buyValue, true)
errResp, sellValue, _ := transaction.CheckSwap(swapChecker, coinSell, coinBuy, coins.MaxCoinSupply(), buyValue, true)
if errResp != nil {
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
Expand Down Expand Up @@ -283,6 +283,7 @@ func (s *Service) calcBuyPoolWithCommission(ctx context.Context, commissions *co

commissionPoolSwapper := cState.Swap().GetSwapper(requestCoinCommissionID, types.GetBaseCoinID())
if commissionFromPool && requestCoinCommissionID != types.GetBaseCoinID() {
commissionInBaseCoin, _ = commissionPoolSwapper.CalculateBuyForSellWithOrders(commission)
commissionPoolSwapper = commissionPoolSwapper.AddLastSwapStepWithOrders(commission, commissionInBaseCoin, false)
}

Expand Down
7 changes: 4 additions & 3 deletions api/v2/service/estimate_coin_sell.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (s *Service) calcSellPoolWithCommission(ctx context.Context, commissions *c

commissionPoolSwapper := cState.Swap().GetSwapper(requestCoinCommissionID, types.GetBaseCoinID())
if commissionFromPool && requestCoinCommissionID != types.GetBaseCoinID() {
commissionInBaseCoin, _ = commissionPoolSwapper.CalculateBuyForSellWithOrders(commission)
commissionPoolSwapper = commissionPoolSwapper.AddLastSwapStepWithOrders(commission, commissionInBaseCoin, false)
}
if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
Expand All @@ -219,13 +220,13 @@ func (s *Service) commissionInCoin(cState *state.CheckState, coinCommissionID ty
commission = commissionInBaseCoin
isSwapFromPool = true
case types.GetBaseCoinID():
commission = cState.Swap().GetSwapper(commissionsCoin, types.GetBaseCoinID()).CalculateBuyForSellWithOrders(commissionInBaseCoin)
commission, _ = cState.Swap().GetSwapper(commissionsCoin, types.GetBaseCoinID()).CalculateBuyForSellWithOrders(commissionInBaseCoin)
if commission == nil {
return nil, false, s.createError(status.New(codes.FailedPrecondition, "Not possible to pay commission"), transaction.EncodeError(code.NewCommissionCoinNotSufficient("", "")))
}
default:
if !commissionsCoin.IsBaseCoin() {
commissionInBaseCoin = cState.Swap().GetSwapper(commissionsCoin, types.GetBaseCoinID()).CalculateBuyForSellWithOrders(commissionInBaseCoin)
commissionInBaseCoin, _ = cState.Swap().GetSwapper(commissionsCoin, types.GetBaseCoinID()).CalculateBuyForSellWithOrders(commissionInBaseCoin)
if commissionInBaseCoin == nil {
return nil, false, s.createError(status.New(codes.FailedPrecondition, "Not possible to pay commission"), transaction.EncodeError(code.NewCommissionCoinNotSufficient("", "")))
}
Expand Down Expand Up @@ -275,7 +276,7 @@ func (s *Service) calcSellFromPool(ctx context.Context, value *big.Int, cState *
}
}

errResp, buyValue := transaction.CheckSwap(swapChecker, coinSell, coinBuy, sellValue, big.NewInt(0), false)
errResp, buyValue, _ := transaction.CheckSwap(swapChecker, coinSell, coinBuy, sellValue, big.NewInt(0), false)
if errResp != nil {
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
Expand Down
24 changes: 7 additions & 17 deletions api/v2/service/estimate_coin_sell_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *Service) EstimateCoinSellAll(ctx context.Context, req *pb.EstimateCoinS
if req.SwapFrom == pb.SwapFrom_bancor || req.SwapFrom == pb.SwapFrom_optimal {
commissionInBaseCoin := new(big.Int).Set(commissions.SellAllBancor)
if !commissions.Coin.IsBaseCoin() {
commissionInBaseCoin = cState.Swap().GetSwapper(types.GetBaseCoinID(), commissions.Coin).CalculateSellForBuyWithOrders(commissionInBaseCoin)
commissionInBaseCoin, _ = cState.Swap().GetSwapper(types.GetBaseCoinID(), commissions.Coin).CalculateSellForBuyWithOrders(commissionInBaseCoin)
}
if req.GasPrice > 1 {
commissionInBaseCoin.Mul(commissionInBaseCoin, big.NewInt(int64(req.GasPrice)))
Expand Down Expand Up @@ -125,7 +125,7 @@ func (s *Service) EstimateCoinSellAll(ctx context.Context, req *pb.EstimateCoinS
if req.SwapFrom == pb.SwapFrom_pool || req.SwapFrom == pb.SwapFrom_optimal {
commissionInBaseCoin := new(big.Int).Set(commissions.SellAllPoolBase)
if !commissions.Coin.IsBaseCoin() {
commissionInBaseCoin = cState.Swap().GetSwapper(types.GetBaseCoinID(), commissions.Coin).CalculateSellForBuyWithOrders(commissionInBaseCoin)
commissionInBaseCoin, _ = cState.Swap().GetSwapper(types.GetBaseCoinID(), commissions.Coin).CalculateSellForBuyWithOrders(commissionInBaseCoin)
}
if req.GasPrice > 1 {
commissionInBaseCoin.Mul(commissionInBaseCoin, big.NewInt(int64(req.GasPrice)))
Expand Down Expand Up @@ -226,29 +226,19 @@ func (s *Service) calcSellAllFromBancor(value *big.Int, coinTo *coins.Model, coi
}

if !coinFrom.ID().IsBaseCoin() {
value = formula.CalculateSaleReturn(coinFrom.Volume(), coinFrom.Reserve(), coinFrom.Crr(), value)
if errResp := transaction.CheckReserveUnderflow(coinFrom, value); errResp != nil {
var errResp *transaction.Response
value, errResp = transaction.CalculateSaleReturnAndCheck(coinFrom, value)
if errResp != nil {
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
}

if !coinTo.ID().IsBaseCoin() {
value = formula.CalculatePurchaseReturn(coinTo.Volume(), coinTo.Reserve(), coinTo.Crr(), value)
if errResp := transaction.CheckForCoinSupplyOverflow(coinTo, value); errResp != nil {
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
}

if !coinFrom.ID().IsBaseCoin() {
value = formula.CalculateSaleReturn(coinFrom.Volume(), coinFrom.Reserve(), coinFrom.Crr(), value)
if errResp := transaction.CheckReserveUnderflow(coinFrom, value); errResp != nil {
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
}
value.Sub(value, commissionInBaseCoin)

if value.Sign() != 1 {
return nil, status.New(codes.FailedPrecondition, "Not enough coins to pay commission").Err()
}

if !coinTo.ID().IsBaseCoin() {
value = formula.CalculatePurchaseReturn(coinTo.Volume(), coinTo.Reserve(), coinTo.Crr(), value)
if errResp := transaction.CheckForCoinSupplyOverflow(coinTo, value); errResp != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/v2/service/estimate_tx_commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *Service) EstimateTxCommission(ctx context.Context, req *pb.EstimateTxCo
commissions := cState.Commission().GetCommissions()
price := decodedTx.Price(commissions)
if !commissions.Coin.IsBaseCoin() {
price = cState.Swap().GetSwapper(commissions.Coin, types.GetBaseCoinID()).CalculateBuyForSellWithOrders(price)
price, _ = cState.Swap().GetSwapper(commissions.Coin, types.GetBaseCoinID()).CalculateBuyForSellWithOrders(price)
}
if price == nil {
return nil, s.createError(status.New(codes.FailedPrecondition, "Not possible to pay commission"), transaction.EncodeError(code.NewCommissionCoinNotSufficient("", "")))
Expand Down
33 changes: 23 additions & 10 deletions cmd/minter/cmd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ package cmd
import (
"context"
"fmt"

"io"
"net/http"
_ "net/http/pprof" // nolint: gosec // securely exposed on separate, optional port
"net/url"
"os"
"syscall"

apiV2 "github.com/MinterTeam/minter-go-node/api/v2"
serviceApi "github.com/MinterTeam/minter-go-node/api/v2/service"
"github.com/MinterTeam/minter-go-node/cli/service"
Expand All @@ -21,6 +13,7 @@ import (
"github.com/MinterTeam/minter-go-node/coreV2/statistics"
"github.com/MinterTeam/minter-go-node/log"
"github.com/MinterTeam/minter-go-node/version"
"github.com/cosmos/cosmos-sdk/snapshots"
"github.com/spf13/cobra"
tmCfg "github.com/tendermint/tendermint/config"
tmLog "github.com/tendermint/tendermint/libs/log"
Expand All @@ -31,6 +24,12 @@ import (
"github.com/tendermint/tendermint/proxy"
rpc "github.com/tendermint/tendermint/rpc/client/local"
tmTypes "github.com/tendermint/tendermint/types"
"io"
"net/http"
_ "net/http/pprof" // nolint: gosec // securely exposed on separate, optional port
"net/url"
"os"
"syscall"
)

// RunNode is the command that allows the CLI to start a node.
Expand Down Expand Up @@ -88,10 +87,25 @@ func runNode(cmd *cobra.Command) error {
if err != nil {
return err
}
app := minter.NewMinterBlockchain(storages, cfg, cmd.Context(), 720, 0)
app := minter.NewMinterBlockchain(storages, cfg, cmd.Context(), 720, 0, logger.With("module", "node"))

if cfg.SnapshotInterval > 0 || cfg.StateSync.Enable {
snapshotDB, err := storages.InitSnapshotLevelDB("data/snapshots/metadata", minter.GetDbOpts(cfg.StateMemAvailable))
if err != nil {
return err
}

snapshotStore, err := snapshots.NewStore(snapshotDB, storages.GetMinterHome()+"/data/snapshots")
if err != nil {
panic(err)
}

app.SetSnapshotStore(snapshotStore, cfg.SnapshotInterval, cfg.SnapshotKeepRecent)
}

// start TM node
node := startTendermintNode(app, tmConfig, logger, storages.GetMinterHome())

client := app.RpcClient()

if !cfg.ValidatorMode {
Expand Down Expand Up @@ -217,7 +231,6 @@ func startTendermintNode(app *minter.Blockchain, cfg *tmCfg.Config, logger tmLog
//
// return mempoolReactor, mempool
//},
nil,
logger.With("module", "tendermint"),
)

Expand Down
9 changes: 9 additions & 0 deletions cmd/minter/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,14 @@ var RootCmd = &cobra.Command{
if peers != "" {
cfg.P2P.PersistentPeers = peers
}

stateSync, err := cmd.Flags().GetBool("state-sync")
if err != nil {
panic(err)
}

if stateSync {
cfg.StateSync.Enable = stateSync
}
},
}
1 change: 1 addition & 0 deletions cmd/minter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func main() {
rootCmd.PersistentFlags().String("pprof-addr", "0.0.0.0:6060", "pprof listen addr")
rootCmd.PersistentFlags().String("genesis", "https://github.com/MinterTeam/minter-go-node/releases/download/v2.0/genesis.json", "path with the genesis file to download")
rootCmd.PersistentFlags().String("persistent-peers", "", "")
rootCmd.PersistentFlags().Bool("state-sync", false, "enable state sync snapshots")

cmd.ExportCommand.Flags().Uint64("height", 0, "export height")
cmd.ExportCommand.Flags().Bool("indent", false, "using indent")
Expand Down
16 changes: 15 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Storage struct {
minterConfig string
eventDB db.DB
stateDB db.DB
snapshotDB db.DB
}

func (s *Storage) SetMinterConfig(minterConfig string) {
Expand All @@ -30,8 +31,21 @@ func (s *Storage) StateDB() db.DB {
return s.stateDB
}

func (s *Storage) SnapshotDB() db.DB {
return s.snapshotDB
}

func NewStorage(home string, config string) *Storage {
return &Storage{eventDB: db.NewMemDB(), stateDB: db.NewMemDB(), minterConfig: config, minterHome: home}
return &Storage{eventDB: db.NewMemDB(), stateDB: db.NewMemDB(), snapshotDB: db.NewMemDB(), minterConfig: config, minterHome: home}
}

func (s *Storage) InitSnapshotLevelDB(name string, opts *opt.Options) (db.DB, error) {
levelDB, err := db.NewGoLevelDBWithOpts(name, s.GetMinterHome(), opts)
if err != nil {
return nil, err
}
s.snapshotDB = levelDB
return s.snapshotDB, nil
}

func (s *Storage) InitEventLevelDB(name string, opts *opt.Options) (db.DB, error) {
Expand Down
Loading

0 comments on commit 38a6ddb

Please sign in to comment.