Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
klim0v committed Sep 22, 2020
1 parent ca7d011 commit bc73bfd
Show file tree
Hide file tree
Showing 26 changed files with 46 additions and 77 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func responseTime(b *minter.Blockchain) func(f func(http.ResponseWriter, *http.R
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
f(w, r)
go b.StatisticData().SetApiTime(time.Now().Sub(start), r.URL.Path)
go b.StatisticData().SetApiTime(time.Since(start), r.URL.Path)
}
}
}
Expand Down
22 changes: 1 addition & 21 deletions api/v2/service/data_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (

func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
var m proto.Message
switch data.(type) {
switch d := data.(type) {
case *transaction.BuyCoinData:
d := data.(*transaction.BuyCoinData)
m = &pb.BuyCoinData{
CoinToBuy: &pb.Coin{
Id: d.CoinToBuy.String(),
Expand All @@ -30,13 +29,11 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
MaximumValueToSell: d.MaximumValueToSell.String(),
}
case *transaction.EditCoinOwnerData:
d := data.(*transaction.EditCoinOwnerData)
m = &pb.EditCoinOwnerData{
Symbol: d.Symbol.String(),
NewOwner: d.NewOwner.String(),
}
case *transaction.CreateCoinData:
d := data.(*transaction.CreateCoinData)
m = &pb.CreateCoinData{
Name: d.Name,
Symbol: d.Symbol.String(),
Expand All @@ -46,7 +43,6 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
MaxSupply: d.MaxSupply.String(),
}
case *transaction.CreateMultisigData:
d := data.(*transaction.CreateMultisigData)
weights := make([]string, 0, len(d.Weights))
for _, weight := range d.Weights {
weights = append(weights, strconv.Itoa(int(weight)))
Expand All @@ -61,7 +57,6 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
Addresses: addresses,
}
case *transaction.DeclareCandidacyData:
d := data.(*transaction.DeclareCandidacyData)
m = &pb.DeclareCandidacyData{
Address: d.Address.String(),
PubKey: d.PubKey.String(),
Expand All @@ -73,7 +68,6 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
Stake: d.Stake.String(),
}
case *transaction.DelegateData:
d := data.(*transaction.DelegateData)
m = &pb.DelegateData{
PubKey: d.PubKey.String(),
Coin: &pb.Coin{
Expand All @@ -83,21 +77,18 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
Value: d.Value.String(),
}
case *transaction.EditCandidateData:
d := data.(*transaction.EditCandidateData)
m = &pb.EditCandidateData{
PubKey: d.PubKey.String(),
RewardAddress: d.RewardAddress.String(),
OwnerAddress: d.OwnerAddress.String(),
ControlAddress: d.ControlAddress.String(),
}
case *transaction.EditCandidatePublicKeyData:
d := data.(*transaction.EditCandidatePublicKeyData)
m = &pb.EditCandidatePublicKeyData{
PubKey: d.PubKey.String(),
NewPubKey: d.NewPubKey.String(),
}
case *transaction.EditMultisigData:
d := data.(*transaction.EditMultisigData)
weights := make([]string, 0, len(d.Weights))
for _, weight := range d.Weights {
weights = append(weights, strconv.Itoa(int(weight)))
Expand All @@ -112,7 +103,6 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
Addresses: addresses,
}
case *transaction.MultisendData:
d := data.(*transaction.MultisendData)
list := make([]*pb.SendData, 0, len(d.List))
for _, item := range d.List {
list = append(list, &pb.SendData{
Expand All @@ -128,12 +118,10 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
List: list,
}
case *transaction.PriceVoteData:
d := data.(*transaction.PriceVoteData)
m = &pb.PriceVoteData{
Price: strconv.Itoa(int(d.Price)),
}
case *transaction.RecreateCoinData:
d := data.(*transaction.RecreateCoinData)
m = &pb.RecreateCoinData{
Name: d.Name,
Symbol: d.Symbol.String(),
Expand All @@ -143,13 +131,11 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
MaxSupply: d.MaxSupply.String(),
}
case *transaction.RedeemCheckData:
d := data.(*transaction.RedeemCheckData)
m = &pb.RedeemCheckData{
RawCheck: base64.StdEncoding.EncodeToString(d.RawCheck),
Proof: base64.StdEncoding.EncodeToString(d.Proof[:]),
}
case *transaction.SellAllCoinData:
d := data.(*transaction.SellAllCoinData)
m = &pb.SellAllCoinData{
CoinToSell: &pb.Coin{
Id: d.CoinToSell.String(),
Expand All @@ -162,7 +148,6 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
MinimumValueToBuy: d.MinimumValueToBuy.String(),
}
case *transaction.SellCoinData:
d := data.(*transaction.SellCoinData)
m = &pb.SellCoinData{
CoinToSell: &pb.Coin{
Id: d.CoinToSell.String(),
Expand All @@ -176,7 +161,6 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
MinimumValueToBuy: d.MinimumValueToBuy.String(),
}
case *transaction.SendData:
d := data.(*transaction.SendData)
m = &pb.SendData{
Coin: &pb.Coin{
Id: d.Coin.String(),
Expand All @@ -186,23 +170,19 @@ func encode(data transaction.Data, coins coins.RCoins) (*any.Any, error) {
Value: d.Value.String(),
}
case *transaction.SetHaltBlockData:
d := data.(*transaction.SetHaltBlockData)
m = &pb.SetHaltBlockData{
PubKey: d.PubKey.String(),
Height: strconv.Itoa(int(d.Height)),
}
case *transaction.SetCandidateOnData:
d := data.(*transaction.SetCandidateOnData)
m = &pb.SetCandidateOnData{
PubKey: d.PubKey.String(),
}
case *transaction.SetCandidateOffData:
d := data.(*transaction.SetCandidateOffData)
m = &pb.SetCandidateOffData{
PubKey: d.PubKey.String(),
}
case *transaction.UnbondData:
d := data.(*transaction.UnbondData)
m = &pb.UnbondData{
PubKey: d.PubKey.String(),
Coin: &pb.Coin{
Expand Down
2 changes: 1 addition & 1 deletion api/v2/service/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (s *Service) Subscribe(request *pb.SubscribeRequest, stream pb.ApiService_S
subscriber := uuid.New().String()
sub, err := s.client.Subscribe(ctx, subscriber, request.Query)
if err != nil {
return status.Error(codes.Internal, err.Error())
return status.Error(codes.InvalidArgument, err.Error())
}
defer func() {
if err := s.client.UnsubscribeAll(context.Background(), subscriber); err != nil {
Expand Down
6 changes: 1 addition & 5 deletions api/v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ func Run(srv *service.Service, addrGRPC, addrApi string, logger log.Logger) erro
}
mux := http.NewServeMux()
mux.Handle("/v2/", http.StripPrefix("/v2", handlers.CompressHandler(allowCORS(wsproxy.WebsocketProxy(gwmux)))))
if err := serveOpenAPI(mux); err != nil {
// ignore
}

_ = serveOpenAPI(mux)
group.Go(func() error {
return http.ListenAndServe(addrApi, mux)
})
Expand All @@ -114,7 +111,6 @@ func preflightHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
return
}

func serveOpenAPI(mux *http.ServeMux) error {
Expand Down
6 changes: 3 additions & 3 deletions cli/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func NewCLI(socketPath string) (*ManagerConsole, error) {

app := cli.NewApp()
app.CommandNotFound = func(ctx *cli.Context, cmd string) {
fmt.Println(fmt.Sprintf("No help topic for '%v'", cmd))
fmt.Printf("No help topic for '%v'\n", cmd)
}
app.UseShortOptionHandling = true
jsonFlag := &cli.BoolFlag{Name: "json", Aliases: []string{"j"}, Required: false, Usage: "echo in json format"}
Expand Down Expand Up @@ -347,7 +347,7 @@ func netInfoCMD(client pb.ManagerServiceClient) func(c *cli.Context) error {
if err != nil {
return err
}
fmt.Println(string(bb.Bytes()))
fmt.Println(bb.String())
return nil
}
fmt.Println(proto.MarshalTextString(response))
Expand All @@ -367,7 +367,7 @@ func statusCMD(client pb.ManagerServiceClient) func(c *cli.Context) error {
if err != nil {
return err
}
fmt.Println(string(bb.Bytes()))
fmt.Println(bb.String())
return nil
}
fmt.Println(proto.MarshalTextString(response))
Expand Down
1 change: 0 additions & 1 deletion cli/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func StartCLIServer(socketPath string, manager pb.ManagerServiceServer, ctx cont
server.GracefulStop()
case <-kill:
}
return
}()

if err := server.Serve(lis); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions core/minter/minter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/proxy"
rpc "github.com/tendermint/tendermint/rpc/client/local"
_ "github.com/tendermint/tendermint/types"
types2 "github.com/tendermint/tendermint/types"
"math/big"
"math/rand"
Expand Down Expand Up @@ -376,7 +375,7 @@ func TestBlockchain_GetStateForHeightAndDeleteStateVersions(t *testing.T) {
t.Fatalf("Failed: %s", err.Error())
}

checkState, err = blockchain.GetStateForHeight(uint64(resultTx.Height - 1))
_, err = blockchain.GetStateForHeight(uint64(resultTx.Height - 1))
if err == nil {
t.Fatalf("Failed: %s", "state not deleted")
}
Expand Down
4 changes: 1 addition & 3 deletions core/statistics/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (d *Data) handleStartBlocks(ctx context.Context) {

for {
d.BlockStart.RLock()
ok := (height == d.BlockStart.height+1) || 0 == d.BlockStart.height
ok := (height == d.BlockStart.height+1) || d.BlockStart.height == 0
d.BlockStart.RUnlock()
if ok {
break
Expand Down Expand Up @@ -295,8 +295,6 @@ func (d *Data) handleEndBlock(ctx context.Context) {
d.Speed.startTime = time.Now().Add(-12 * time.Hour)
d.Speed.startHeight = height - (height-d.Speed.startHeight)/2
d.Speed.duration = d.Speed.duration/2 + duration.Nanoseconds()

return
}()
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/transaction/buy_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (data BuyCoinData) BasicCheck(tx *Transaction, context *state.CheckState) *
if data.CoinToSell == data.CoinToBuy {
return &Response{
Code: code.CrossConvert,
Log: fmt.Sprintf("\"From\" coin equals to \"to\" coin"),
Log: "\"From\" coin equals to \"to\" coin",
Info: EncodeError(code.NewCrossConvert(
data.CoinToSell.String(),
context.Coins().GetCoin(data.CoinToSell).Symbol().String(),
Expand Down
3 changes: 0 additions & 3 deletions core/transaction/buy_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ import (
"github.com/MinterTeam/minter-go-node/formula"
"github.com/MinterTeam/minter-go-node/helpers"
"github.com/MinterTeam/minter-go-node/rlp"
"github.com/tendermint/go-amino"
db "github.com/tendermint/tm-db"
)

var (
cdc = amino.NewCodec()

rnd = rand.New(rand.NewSource(time.Now().Unix()))
)

Expand Down
4 changes: 2 additions & 2 deletions core/transaction/create_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ func (data CreateCoinData) BasicCheck(tx *Transaction, context *state.CheckState
if context.Coins().ExistsBySymbol(data.Symbol) {
return &Response{
Code: code.CoinAlreadyExists,
Log: fmt.Sprintf("Coin already exists"),
Log: "Coin already exists",
Info: EncodeError(code.NewCoinAlreadyExists(types.StrToCoinSymbol(data.Symbol.String()).String(), context.Coins().GetCoinBySymbol(data.Symbol, 0).ID().String())),
}
}

if data.ConstantReserveRatio < 10 || data.ConstantReserveRatio > 100 {
return &Response{
Code: code.WrongCrr,
Log: fmt.Sprintf("Constant Reserve Ratio should be between 10 and 100"),
Log: "Constant Reserve Ratio should be between 10 and 100",
Info: EncodeError(code.NewWrongCrr("10", "100", strconv.Itoa(int(data.ConstantReserveRatio)))),
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/transaction/create_multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (data CreateMultisigData) BasicCheck(tx *Transaction, context *state.CheckS
if lenAddresses != lenWeights {
return &Response{
Code: code.DifferentCountAddressesAndWeights,
Log: fmt.Sprintf("Different count addresses and weights"),
Log: "Different count addresses and weights",
Info: EncodeError(code.NewDifferentCountAddressesAndWeights(fmt.Sprintf("%d", lenAddresses), fmt.Sprintf("%d", lenWeights))),
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/transaction/declare_candidacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (data DeclareCandidacyData) BasicCheck(tx *Transaction, context *state.Chec
if data.Commission < minCommission || data.Commission > maxCommission {
return &Response{
Code: code.WrongCommission,
Log: fmt.Sprintf("Commission should be between 0 and 100"),
Log: "Commission should be between 0 and 100",
Info: EncodeError(code.NewWrongCommission(fmt.Sprintf("%d", data.Commission), "0", "100")),
}
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func (data DeclareCandidacyData) Run(tx *Transaction, context state.Interface, r
if checkState.Candidates().Count() >= maxCandidatesCount && !checkState.Candidates().IsNewCandidateStakeSufficient(data.Coin, data.Stake, maxCandidatesCount) {
return Response{
Code: code.TooLowStake,
Log: fmt.Sprintf("Given stake is too low"),
Log: "Given stake is too low",
Info: EncodeError(code.NewTooLowStake(sender.String(), data.PubKey.String(), data.Stake.String(), data.Coin.String(), checkState.Coins().GetCoin(data.Coin).GetFullSymbol())),
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/transaction/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ func (data DelegateData) BasicCheck(tx *Transaction, context *state.CheckState)
if data.Value.Cmp(types.Big0) < 1 {
return &Response{
Code: code.StakeShouldBePositive,
Log: fmt.Sprintf("Stake should be positive"),
Log: "Stake should be positive",
Info: EncodeError(code.NewStakeShouldBePositive(data.Value.String())),
}
}

if !context.Candidates().Exists(data.PubKey) {
return &Response{
Code: code.CandidateNotFound,
Log: fmt.Sprintf("Candidate with such public key not found"),
Log: "Candidate with such public key not found",
Info: EncodeError(code.NewCandidateNotFound(data.PubKey.String())),
}
}
Expand All @@ -64,7 +64,7 @@ func (data DelegateData) BasicCheck(tx *Transaction, context *state.CheckState)
if !context.Candidates().IsDelegatorStakeSufficient(sender, data.PubKey, data.Coin, data.Value) {
return &Response{
Code: code.TooLowStake,
Log: fmt.Sprintf("Stake is too low"),
Log: "Stake is too low",
Info: EncodeError(code.NewTooLowStake(sender.String(), data.PubKey.String(), data.Value.String(), data.Coin.String(), context.Coins().GetCoin(data.Coin).GetFullSymbol())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/transaction/edit_candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func checkCandidateOwnership(data CandidateTx, tx *Transaction, context *state.C
if owner != sender {
return &Response{
Code: code.IsNotOwnerOfCandidate,
Log: fmt.Sprintf("Sender is not an owner of a candidate"),
Log: "Sender is not an owner of a candidate",
Info: EncodeError(code.NewIsNotOwnerOfCandidate(sender.String(), data.GetPubKey().String(), owner.String(), "")),
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/transaction/edit_multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (data EditMultisigData) BasicCheck(tx *Transaction, context *state.CheckSta
if lenWeights > 32 {
return &Response{
Code: code.TooLargeOwnersList,
Log: fmt.Sprintf("Owners list is limited to 32 items"),
Log: "Owners list is limited to 32 items",
Info: EncodeError(code.NewTooLargeOwnersList(strconv.Itoa(lenWeights), "32")),
}
}
Expand All @@ -44,7 +44,7 @@ func (data EditMultisigData) BasicCheck(tx *Transaction, context *state.CheckSta
if lenAddresses != lenWeights {
return &Response{
Code: code.DifferentCountAddressesAndWeights,
Log: fmt.Sprintf("Different count addresses and weights"),
Log: "Different count addresses and weights",
Info: EncodeError(code.NewDifferentCountAddressesAndWeights(fmt.Sprintf("%d", lenAddresses), fmt.Sprintf("%d", lenWeights))),
}
}
Expand All @@ -64,7 +64,7 @@ func (data EditMultisigData) BasicCheck(tx *Transaction, context *state.CheckSta
if usedAddresses[address] {
return &Response{
Code: code.DuplicatedAddresses,
Log: fmt.Sprintf("Duplicated multisig addresses"),
Log: "Duplicated multisig addresses",
Info: EncodeError(code.NewDuplicatedAddresses(address.String())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/transaction/multisend.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (item MultisendDataItem) MarshalJSON() ([]byte, error) {
}

func (data MultisendData) String() string {
return fmt.Sprintf("MULTISEND")
return "MULTISEND"
}

func (data MultisendData) Gas() int64 {
Expand Down
Loading

0 comments on commit bc73bfd

Please sign in to comment.