Skip to content

Commit

Permalink
Merge pull request #617 from MinterTeam/dev
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
danil-lashin authored Apr 17, 2021
2 parents 3bd9f0d + 7966b73 commit 32fa8f1
Show file tree
Hide file tree
Showing 13 changed files with 1,276 additions and 108 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- dev

pull_request:
branches:
Expand All @@ -14,7 +15,7 @@ jobs:
env:
CONTAINER_NAME: minter_node
CONTAINER_TIMEOUT_SEC: 30
API_RUN_PORT: 8841
API_RUN_PORT: 8843
SECRET_DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_HUB_PASSWORD }}
SECRET_DOCKER_HUB_USER: ${{ secrets.DOCKER_HUB_USER }}
SECRET_DOCKER_HUB_REPO: ${{ secrets.DOCKER_HUB_REPO }}
Expand All @@ -29,6 +30,8 @@ jobs:
# 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:
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
run: |
echo ::set-env name=VERSION::$(awk -F\" '/Version =/ { print $2; exit }' < version/version.go)
echo ::set-env name=DOCKER_REPO::$(if [[ "$SECRET_DOCKER_HUB_REPO" == "" ]]; then if [[ "$SECRET_DOCKER_HUB_USER" == "" ]]; then echo "testbuild"; else echo "$SECRET_DOCKER_HUB_USER"; fi; else echo "$SECRET_DOCKER_HUB_REPO"; fi)
Expand All @@ -37,13 +40,17 @@ jobs:
run: docker build -t $DOCKER_REPO/$DOCKER_IMAGE:$VERSION . -f ./Dockerfile-ci

- name: Start docker container
run: docker run -d --name $CONTAINER_NAME -p $API_RUN_PORT:8841 $DOCKER_REPO/$DOCKER_IMAGE:$VERSION
run: docker run -d --name $CONTAINER_NAME -p $API_RUN_PORT:8843 $DOCKER_REPO/$DOCKER_IMAGE:$VERSION

- name: Check container is still running
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
run: |
echo ::set-env name=RUN_TEST_RESULT::$(sleep $CONTAINER_TIMEOUT_SEC && if [[ $(docker inspect -f "{{.State.Running}}" $CONTAINER_NAME 2> /dev/null) == true ]]; then echo OK; else echo FAIL; fi)
- name: Check api is available by HTTP (response code is 200)
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
run: |
echo ::set-env name=API_TEST_RESULT::$(if [[ $(curl -LIs localhost:$API_RUN_PORT -o /dev/null -w '%{http_code}') == 200 ]]; then echo OK; else echo FAIL; fi)
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

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

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

### Fixed

- Correction of votes

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

[Full Changelog](https://github.com/MinterTeam/minter-go-node/compare/v2.0.2...v2.0.3)
Expand Down
18 changes: 14 additions & 4 deletions coreV2/appdb/appdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,9 @@ type Version struct {
Height uint64
}

func (appDB *AppDB) GetVersion(height uint64) string {
appDB.GetVersions()

func (appDB *AppDB) GetVersionName(height uint64) string {
lastVersionName := ""
for _, version := range appDB.versions {
for _, version := range appDB.GetVersions() {
if version.Height > height {
return lastVersionName
}
Expand All @@ -262,6 +260,18 @@ func (appDB *AppDB) GetVersion(height uint64) string {
return lastVersionName
}

func (appDB *AppDB) GetVersionHeight(name string) uint64 {
var lastVersionHeight uint64
for _, version := range appDB.GetVersions() {
if version.Name == name {
return lastVersionHeight
}
lastVersionHeight = version.Height
}

return lastVersionHeight
}

func (appDB *AppDB) GetVersions() []*Version {
if len(appDB.versions) == 0 {
result, err := appDB.db.Get([]byte(versionsPath))
Expand Down
169 changes: 102 additions & 67 deletions coreV2/minter/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
tmjson "github.com/tendermint/tendermint/libs/json"
tmNode "github.com/tendermint/tendermint/node"
rpc "github.com/tendermint/tendermint/rpc/client/local"
"log"
"math/big"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -64,13 +65,14 @@ type Blockchain struct {
// currentMempool is responsive for prevent sending multiple transactions from one address in one block
currentMempool *sync.Map

lock sync.RWMutex
haltHeight uint64
cfg *config.Config
storages *utils.Storage
stopChan context.Context
stopped bool
grace *upgrades.Grace
lock sync.RWMutex
haltHeight uint64
cfg *config.Config
storages *utils.Storage
stopChan context.Context
stopped bool
grace *upgrades.Grace
knownUpdates map[string]struct{}
}

// NewMinterBlockchain creates Minter Blockchain instance, should be only called once
Expand Down Expand Up @@ -107,6 +109,12 @@ func NewMinterBlockchain(storages *utils.Storage, cfg *config.Config, ctx contex
return app
}

func graceForUpdate(height uint64) *upgrades.GracePeriod {
return upgrades.NewGracePeriod(height, height+120)
}

const haltBlockV210 = 3431238

func (blockchain *Blockchain) initState() {
initialHeight := blockchain.appDB.GetStartHeight()
currentHeight := blockchain.appDB.GetLastHeight()
Expand All @@ -126,7 +134,15 @@ func (blockchain *Blockchain) initState() {
blockchain.stateCheck = state.NewCheckState(stateDeliver)

grace := upgrades.NewGrace()
grace.AddGracePeriods(upgrades.NewGracePeriod(initialHeight, initialHeight+120))
grace.AddGracePeriods(upgrades.NewGracePeriod(initialHeight, initialHeight+120),
upgrades.NewGracePeriod(haltBlockV210, haltBlockV210+120))
blockchain.knownUpdates = map[string]struct{}{
"": {}, // default version
// add more for update
}
for _, v := range blockchain.UpdateVersions() {
grace.AddGracePeriods(graceForUpdate(v.Height))
}
blockchain.grace = grace
}

Expand Down Expand Up @@ -196,7 +212,15 @@ func (blockchain *Blockchain) BeginBlock(req abciTypes.RequestBeginBlock) abciTy

blockchain.calculatePowers(blockchain.stateDeliver.Validators.GetValidators())

if blockchain.isApplicationHalted(height) {
if blockchain.isApplicationHalted(height) && !blockchain.grace.IsUpgradeBlock(height) {
log.Printf("Application halted at height %d\n", height)
blockchain.stop()
return abciTypes.ResponseBeginBlock{}
}

versionName := blockchain.appDB.GetVersionName(height)
if _, ok := blockchain.knownUpdates[versionName]; !ok {
log.Printf("Update your node binary to the latest version: %s", versionName)
blockchain.stop()
return abciTypes.ResponseBeginBlock{}
}
Expand Down Expand Up @@ -290,64 +314,75 @@ func (blockchain *Blockchain) EndBlock(req abciTypes.RequestEndBlock) abciTypes.
blockchain.stateDeliver.Validators.PayRewards()
}

if prices := blockchain.isUpdateCommissionsBlock(height); len(prices) != 0 {
blockchain.stateDeliver.Commission.SetNewCommissions(prices)
price := blockchain.stateDeliver.Commission.GetCommissions()
blockchain.eventsDB.AddEvent(&eventsdb.UpdateCommissionsEvent{
Coin: uint64(price.Coin),
PayloadByte: price.PayloadByte.String(),
Send: price.Send.String(),
BuyBancor: price.BuyBancor.String(),
SellBancor: price.SellBancor.String(),
SellAllBancor: price.SellAllBancor.String(),
BuyPoolBase: price.BuyPoolBase.String(),
BuyPoolDelta: price.BuyPoolDelta.String(),
SellPoolBase: price.SellPoolBase.String(),
SellPoolDelta: price.SellPoolDelta.String(),
SellAllPoolBase: price.SellAllPoolBase.String(),
SellAllPoolDelta: price.SellAllPoolDelta.String(),
CreateTicker3: price.CreateTicker3.String(),
CreateTicker4: price.CreateTicker4.String(),
CreateTicker5: price.CreateTicker5.String(),
CreateTicker6: price.CreateTicker6.String(),
CreateTicker7_10: price.CreateTicker7to10.String(),
CreateCoin: price.CreateCoin.String(),
CreateToken: price.CreateToken.String(),
RecreateCoin: price.RecreateCoin.String(),
RecreateToken: price.RecreateToken.String(),
DeclareCandidacy: price.DeclareCandidacy.String(),
Delegate: price.Delegate.String(),
Unbond: price.Unbond.String(),
RedeemCheck: price.RedeemCheck.String(),
SetCandidateOn: price.SetCandidateOn.String(),
SetCandidateOff: price.SetCandidateOff.String(),
CreateMultisig: price.CreateMultisig.String(),
MultisendBase: price.MultisendBase.String(),
MultisendDelta: price.MultisendDelta.String(),
EditCandidate: price.EditCandidate.String(),
SetHaltBlock: price.SetHaltBlock.String(),
EditTickerOwner: price.EditTickerOwner.String(),
EditMultisig: price.EditMultisig.String(),
EditCandidatePublicKey: price.EditCandidatePublicKey.String(),
CreateSwapPool: price.CreateSwapPool.String(),
AddLiquidity: price.AddLiquidity.String(),
RemoveLiquidity: price.RemoveLiquidity.String(),
EditCandidateCommission: price.EditCandidateCommission.String(),
MintToken: price.MintToken.String(),
BurnToken: price.BurnToken.String(),
VoteCommission: price.VoteCommission.String(),
VoteUpdate: price.VoteUpdate.String(),
})
}
blockchain.stateDeliver.Commission.Delete(height)

if v, ok := blockchain.isUpdateNetworkBlock(height); ok {
blockchain.appDB.AddVersion(v, height)
blockchain.eventsDB.AddEvent(&eventsdb.UpdateNetworkEvent{
Version: v,
})
}
blockchain.stateDeliver.Updates.Delete(height)
{
var updateCommissionsBlockPrices []byte
if height < haltBlockV210 {
updateCommissionsBlockPrices = blockchain.isUpdateCommissionsBlock(height)
} else {
updateCommissionsBlockPrices = blockchain.isUpdateCommissionsBlockV2(height)
}
if prices := updateCommissionsBlockPrices; len(prices) != 0 {
blockchain.stateDeliver.Commission.SetNewCommissions(prices)
price := blockchain.stateDeliver.Commission.GetCommissions()
blockchain.eventsDB.AddEvent(&eventsdb.UpdateCommissionsEvent{
Coin: uint64(price.Coin),
PayloadByte: price.PayloadByte.String(),
Send: price.Send.String(),
BuyBancor: price.BuyBancor.String(),
SellBancor: price.SellBancor.String(),
SellAllBancor: price.SellAllBancor.String(),
BuyPoolBase: price.BuyPoolBase.String(),
BuyPoolDelta: price.BuyPoolDelta.String(),
SellPoolBase: price.SellPoolBase.String(),
SellPoolDelta: price.SellPoolDelta.String(),
SellAllPoolBase: price.SellAllPoolBase.String(),
SellAllPoolDelta: price.SellAllPoolDelta.String(),
CreateTicker3: price.CreateTicker3.String(),
CreateTicker4: price.CreateTicker4.String(),
CreateTicker5: price.CreateTicker5.String(),
CreateTicker6: price.CreateTicker6.String(),
CreateTicker7_10: price.CreateTicker7to10.String(),
CreateCoin: price.CreateCoin.String(),
CreateToken: price.CreateToken.String(),
RecreateCoin: price.RecreateCoin.String(),
RecreateToken: price.RecreateToken.String(),
DeclareCandidacy: price.DeclareCandidacy.String(),
Delegate: price.Delegate.String(),
Unbond: price.Unbond.String(),
RedeemCheck: price.RedeemCheck.String(),
SetCandidateOn: price.SetCandidateOn.String(),
SetCandidateOff: price.SetCandidateOff.String(),
CreateMultisig: price.CreateMultisig.String(),
MultisendBase: price.MultisendBase.String(),
MultisendDelta: price.MultisendDelta.String(),
EditCandidate: price.EditCandidate.String(),
SetHaltBlock: price.SetHaltBlock.String(),
EditTickerOwner: price.EditTickerOwner.String(),
EditMultisig: price.EditMultisig.String(),
EditCandidatePublicKey: price.EditCandidatePublicKey.String(),
CreateSwapPool: price.CreateSwapPool.String(),
AddLiquidity: price.AddLiquidity.String(),
RemoveLiquidity: price.RemoveLiquidity.String(),
EditCandidateCommission: price.EditCandidateCommission.String(),
MintToken: price.MintToken.String(),
BurnToken: price.BurnToken.String(),
VoteCommission: price.VoteCommission.String(),
VoteUpdate: price.VoteUpdate.String(),
})
}
blockchain.stateDeliver.Commission.Delete(height)
}

{
if v, ok := blockchain.isUpdateNetworkBlockV2(height); ok {
blockchain.appDB.AddVersion(v, height)
blockchain.eventsDB.AddEvent(&eventsdb.UpdateNetworkEvent{
Version: v,
})
blockchain.grace.AddGracePeriods(graceForUpdate(height))
}
blockchain.stateDeliver.Updates.Delete(height)
}

hasChangedPublicKeys := false
if blockchain.stateDeliver.Candidates.IsChangedPublicKeys() {
Expand Down
40 changes: 37 additions & 3 deletions coreV2/minter/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func (blockchain *Blockchain) isApplicationHalted(height uint64) bool {
return false
}

// Deprecated
func (blockchain *Blockchain) isUpdateCommissionsBlock(height uint64) []byte {
commissions := blockchain.stateDeliver.Commission.GetVotes(height)
if len(commissions) == 0 {
Expand Down Expand Up @@ -326,16 +327,49 @@ func (blockchain *Blockchain) isUpdateCommissionsBlock(height uint64) []byte {
return nil
}

func (blockchain *Blockchain) isUpdateNetworkBlock(height uint64) (string, bool) {
func (blockchain *Blockchain) isUpdateCommissionsBlockV2(height uint64) []byte {
commissions := blockchain.stateDeliver.Commission.GetVotes(height)
if len(commissions) == 0 {
return nil
}
// calculate total power of validators
maxVotingResult := big.NewFloat(0)

var price string
for _, commission := range commissions {
totalVotedPower := big.NewInt(0)
for _, vote := range commission.Votes {
if power, ok := blockchain.validatorsPowers[vote]; ok {
totalVotedPower.Add(totalVotedPower, power)
}
}
votingResult := new(big.Float).Quo(
new(big.Float).SetInt(totalVotedPower),
new(big.Float).SetInt(blockchain.totalPower),
)

if maxVotingResult.Cmp(votingResult) == -1 {
maxVotingResult = votingResult
price = commission.Price
}
}
if maxVotingResult.Cmp(big.NewFloat(votingPowerConsensus)) == 1 {
return []byte(price)
}

return nil
}

func (blockchain *Blockchain) isUpdateNetworkBlockV2(height uint64) (string, bool) {
versions := blockchain.stateDeliver.Updates.GetVotes(height)
if len(versions) == 0 {
return "", false
}
// calculate total power of validators
maxVotingResult, totalVotedPower := big.NewFloat(0), big.NewInt(0)

maxVotingResult := big.NewFloat(0)
var version string
for _, v := range versions {
totalVotedPower := big.NewInt(0)
for _, vote := range v.Votes {
if power, ok := blockchain.validatorsPowers[vote]; ok {
totalVotedPower.Add(totalVotedPower, power)
Expand Down
7 changes: 4 additions & 3 deletions coreV2/transaction/sell_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,14 @@ func TestSellCoinTxCustomToCustomCustom2Commission(t *testing.T) {
// check received coins
buyCoinBalance := cState.Accounts.GetBalance(addr, coinToBuyID)
bipReturn := formula.CalculateSaleReturn(initialVolume1, initialReserve1, crr1, toSell)
estimatedReturn := formula.CalculatePurchaseReturn(initialVolume2, initialReserve2, crr2, bipReturn)
commissions := cState.Commission.GetCommissions()
commissionInBaseCoin := tx.Commission(tx.Price(commissions))
if !commissions.Coin.IsBaseCoin() {
commissionInBaseCoin = cState.Swap.GetSwapper(types.GetBaseCoinID(), commissions.Coin).CalculateSellForBuy(commissionInBaseCoin)
}
commission := formula.CalculateSaleAmount(big.NewInt(0).Add(initialVolume2, estimatedReturn), big.NewInt(0).Add(initialReserve2, bipReturn), crr2, commissionInBaseCoin)
commission := formula.CalculateSaleAmount(initialVolume2, initialReserve2, crr2, commissionInBaseCoin)

estimatedReturn := formula.CalculatePurchaseReturn(big.NewInt(0).Sub(initialVolume2, commission), big.NewInt(0).Sub(initialReserve2, commissionInBaseCoin), crr2, bipReturn)

estimatedBuyBalance := big.NewInt(0).Set(estimatedReturn)
estimatedBuyBalance.Sub(estimatedBuyBalance, commission)
Expand Down Expand Up @@ -704,7 +705,7 @@ func TestSellCoinTxCustomToCustomCustom2Commission(t *testing.T) {
}

estimatedSupply := big.NewInt(0).Set(initialVolume2)
estimatedSupply.Add(estimatedSupply, formula.CalculatePurchaseReturn(initialVolume2, initialReserve2, crr2, formula.CalculateSaleReturn(initialVolume1, initialReserve1, crr1, toSell)))
estimatedSupply.Add(estimatedSupply, formula.CalculatePurchaseReturn(big.NewInt(0).Sub(initialVolume2, commission), big.NewInt(0).Sub(initialReserve2, commissionInBaseCoin), crr2, formula.CalculateSaleReturn(initialVolume1, initialReserve1, crr1, toSell)))
estimatedSupply.Sub(estimatedSupply, commission)
if coinData.Volume().Cmp(estimatedSupply) != 0 {
t.Fatalf("Wrong coin supply")
Expand Down
Loading

0 comments on commit 32fa8f1

Please sign in to comment.