Skip to content

Commit

Permalink
Merge pull request #106 from MinterTeam/dev
Browse files Browse the repository at this point in the history
v0.3.4
  • Loading branch information
danil-lashin authored Sep 13, 2018
2 parents d14f751 + e34e3bc commit 3be4dc0
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 107 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.3.4
*Sept 13th, 2018*

IMPROVEMENT

- [api] Optimize events. WARNING! If you are using events you should re-sync blockchain from scratch.
- [api] Refactor api

## 0.3.3
*Sept 8th, 2018*

Expand Down
20 changes: 10 additions & 10 deletions api/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type BlockTransactionResponse struct {
ServiceData []byte `json:"service_data"`
Gas int64 `json:"gas"`
GasCoin types.CoinSymbol `json:"gas_coin"`
TxResult ResponseDeliverTx `json:"tx_result"`
GasUsed int64 `json:"gas_used"`
Tags map[string]string `json:"tags"`
}

func Block(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -69,6 +70,12 @@ func Block(w http.ResponseWriter, r *http.Request) {
tx, _ := transaction.DecodeFromBytes(rawTx)
sender, _ := tx.Sender()

tags := make(map[string]string)

for _, tag := range blockResults.Results.DeliverTx[i].Tags {
tags[string(tag.Key)] = string(tag.Value)
}

txs[i] = BlockTransactionResponse{
Hash: fmt.Sprintf("Mt%x", rawTx.Hash()),
RawTx: fmt.Sprintf("%x", []byte(rawTx)),
Expand All @@ -81,15 +88,8 @@ func Block(w http.ResponseWriter, r *http.Request) {
ServiceData: tx.ServiceData,
Gas: tx.Gas(),
GasCoin: tx.GasCoin,
TxResult: ResponseDeliverTx{
Code: blockResults.Results.DeliverTx[i].Code,
Data: blockResults.Results.DeliverTx[i].Data,
Log: blockResults.Results.DeliverTx[i].Log,
Info: blockResults.Results.DeliverTx[i].Info,
GasWanted: blockResults.Results.DeliverTx[i].GasWanted,
GasUsed: blockResults.Results.DeliverTx[i].GasUsed,
Tags: blockResults.Results.DeliverTx[i].Tags,
},
GasUsed: blockResults.Results.DeliverTx[i].GasUsed,
Tags: tags,
}
}

Expand Down
25 changes: 12 additions & 13 deletions api/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,28 @@ func Transaction(w http.ResponseWriter, r *http.Request) {
decodedTx, _ := transaction.DecodeFromBytes(tx.Tx)
sender, _ := decodedTx.Sender()

tags := make(map[string]string)

for _, tag := range tx.TxResult.Tags {
tags[string(tag.Key)] = string(tag.Value)
}

err = json.NewEncoder(w).Encode(Response{
Code: 0,
Result: TransactionResponse{
Hash: common.HexBytes(tx.Tx.Hash()),
RawTx: fmt.Sprintf("%x", []byte(tx.Tx)),
Height: tx.Height,
Index: tx.Index,
TxResult: ResponseDeliverTx{
Code: tx.TxResult.Code,
Data: tx.TxResult.Data,
Log: tx.TxResult.Log,
Info: tx.TxResult.Info,
GasWanted: tx.TxResult.GasWanted,
GasUsed: tx.TxResult.GasUsed,
Tags: tx.TxResult.Tags,
},
Hash: common.HexBytes(tx.Tx.Hash()),
RawTx: fmt.Sprintf("%x", []byte(tx.Tx)),
Height: tx.Height,
Index: tx.Index,
From: sender.String(),
Nonce: decodedTx.Nonce,
GasPrice: decodedTx.GasPrice,
GasCoin: decodedTx.GasCoin,
GasUsed: tx.TxResult.GasUsed,
Type: decodedTx.Type,
Data: decodedTx.GetDecodedData(),
Payload: decodedTx.Payload,
Tags: tags,
},
})

Expand Down
38 changes: 14 additions & 24 deletions api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,15 @@ type TransactionResponse struct {
RawTx string `json:"raw_tx"`
Height int64 `json:"height"`
Index uint32 `json:"index"`
TxResult ResponseDeliverTx `json:"tx_result"`
From string `json:"from"`
Nonce uint64 `json:"nonce"`
GasPrice *big.Int `json:"gas_price"`
GasCoin types.CoinSymbol `json:"gas_coin"`
GasUsed int64 `json:"gas_used"`
Type byte `json:"type"`
Data transaction.Data `json:"data"`
Payload []byte `json:"payload"`
}

type ResponseDeliverTx struct {
Code uint32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
Log string `protobuf:"bytes,3,opt,name=log,proto3" json:"log,omitempty"`
Info string `protobuf:"bytes,4,opt,name=info,proto3" json:"info,omitempty"`
GasWanted int64 `protobuf:"varint,5,opt,name=gas_wanted,json=gas_wanted,proto3" json:"gas_wanted,omitempty"`
GasUsed int64 `protobuf:"varint,6,opt,name=gas_used,json=gas_used,proto3" json:"gas_used,omitempty"`
Tags []common.KVPair `protobuf:"bytes,7,rep,name=tags" json:"tags,omitempty"`
Tags map[string]string `json:"tags"`
}

type ResultTxSearch struct {
Expand Down Expand Up @@ -66,27 +57,26 @@ func Transactions(w http.ResponseWriter, r *http.Request) {
decodedTx, _ := transaction.DecodeFromBytes(tx.Tx)
sender, _ := decodedTx.Sender()

tags := make(map[string]string)

for _, tag := range tx.TxResult.Tags {
tags[string(tag.Key)] = string(tag.Value)
}

result[i] = TransactionResponse{
Hash: common.HexBytes(tx.Tx.Hash()),
RawTx: fmt.Sprintf("%x", []byte(tx.Tx)),
Height: tx.Height,
Index: tx.Index,
TxResult: ResponseDeliverTx{
Code: tx.TxResult.Code,
Data: tx.TxResult.Data,
Log: tx.TxResult.Log,
Info: tx.TxResult.Info,
GasWanted: tx.TxResult.GasWanted,
GasUsed: tx.TxResult.GasUsed,
Tags: tx.TxResult.Tags,
},
Hash: common.HexBytes(tx.Tx.Hash()),
RawTx: fmt.Sprintf("%x", []byte(tx.Tx)),
Height: tx.Height,
Index: tx.Index,
From: sender.String(),
Nonce: decodedTx.Nonce,
GasPrice: decodedTx.GasPrice,
GasCoin: decodedTx.GasCoin,
GasUsed: tx.TxResult.GasUsed,
Type: decodedTx.Type,
Data: decodedTx.GetDecodedData(),
Payload: decodedTx.Payload,
Tags: tags,
}
}

Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func DefaultBaseConfig() BaseConfig {
DBPath: "data",
GUIListenAddress: ":3000",
APIListenAddress: ":8841",
EnableEvents: false,
EnableEvents: false,
}
}

Expand Down
10 changes: 7 additions & 3 deletions core/minter/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/MinterTeam/minter-go-node/core/transaction"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/MinterTeam/minter-go-node/core/validators"
"github.com/MinterTeam/minter-go-node/eventsdb"
"github.com/MinterTeam/minter-go-node/genesis"
"github.com/MinterTeam/minter-go-node/helpers"
"github.com/MinterTeam/minter-go-node/log"
Expand Down Expand Up @@ -132,7 +133,8 @@ func (app *Blockchain) BeginBlock(req abciTypes.RequestBeginBlock) abciTypes.Res
}

// give penalty to Byzantine validators
for _, v := range req.ByzantineValidators {
for i := range req.ByzantineValidators {
v := &req.ByzantineValidators[i]
var address [20]byte
copy(address[:], v.Validator.Address)

Expand Down Expand Up @@ -262,6 +264,8 @@ func (app *Blockchain) EndBlock(req abciTypes.RequestEndBlock) abciTypes.Respons
}
}

eventsdb.GetCurrent().FlushEvents(req.Height)

return abciTypes.ResponseEndBlock{
ValidatorUpdates: updates,
}
Expand Down Expand Up @@ -317,8 +321,8 @@ func (app *Blockchain) Commit() abciTypes.ResponseCommit {

// todo: make provider
height := make([]byte, 8)
binary.BigEndian.PutUint64(height[:], app.height)
err = appTable.Put([]byte("height"), height[:])
binary.BigEndian.PutUint64(height, app.height)
err = appTable.Put([]byte("height"), height)

if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions core/state/state_frozen_fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ func (c *stateFrozenFund) punishFund(candidateAddress [20]byte) {
slashed := big.NewInt(0).Set(item.Value)
slashed.Sub(slashed, newValue)

edb.SaveEvent(int64(c.blockHeight), eventsdb.SlashEvent{
edb.AddEvent(int64(c.blockHeight), eventsdb.SlashEvent{
Address: item.Address,
Amount: slashed.String(),
Amount: slashed.Bytes(),
Coin: item.Coin,
ValidatorPubKey: item.CandidateKey,
})
Expand Down
44 changes: 21 additions & 23 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (s *StateDB) updateStateFrozenFund(stateFrozenFund *stateFrozenFund) {
panic(fmt.Errorf("can't encode frozen fund at %d: %v", blockHeight, err))
}
height := make([]byte, 8)
binary.BigEndian.PutUint64(height[:], stateFrozenFund.blockHeight)
binary.BigEndian.PutUint64(height, stateFrozenFund.blockHeight)

key := append(frozenFundsPrefix, height...)
s.setError(s.trie.TryUpdate(key, data))
Expand Down Expand Up @@ -280,7 +280,7 @@ func (s *StateDB) deleteStateCoin(stateCoin *stateCoin) {
func (s *StateDB) deleteFrozenFunds(stateFrozenFund *stateFrozenFund) {
stateFrozenFund.deleted = true
height := make([]byte, 8)
binary.BigEndian.PutUint64(height[:], stateFrozenFund.blockHeight)
binary.BigEndian.PutUint64(height, stateFrozenFund.blockHeight)
key := append(frozenFundsPrefix, height...)
s.setError(s.trie.TryDelete(key))
}
Expand All @@ -293,7 +293,7 @@ func (s *StateDB) getStateFrozenFunds(blockHeight uint64) (stateFrozenFund *stat
}

height := make([]byte, 8)
binary.BigEndian.PutUint64(height[:], blockHeight)
binary.BigEndian.PutUint64(height, blockHeight)
key := append(frozenFundsPrefix, height...)

// Load the object from the database.
Expand Down Expand Up @@ -640,16 +640,14 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (root types.Hash, err error) {
// Commit coins to the trie.
for symbol, stateCoin := range s.stateCoins {
_, isDirty := s.stateCoinsDirty[symbol]
switch {
case isDirty:
{
if stateCoin.data.Volume.Cmp(types.Big0) == 0 {
s.deleteStateCoin(stateCoin)
} else {
s.updateStateCoin(stateCoin)
}
if isDirty {
if stateCoin.data.Volume.Cmp(types.Big0) == 0 {
s.deleteStateCoin(stateCoin)
} else {
s.updateStateCoin(stateCoin)
}
}

delete(s.stateCoinsDirty, symbol)
}

Expand Down Expand Up @@ -830,10 +828,10 @@ func (s *StateDB) PayRewards(height int64) {
DAOReward.Mul(DAOReward, big.NewInt(int64(dao.Commission)))
DAOReward.Div(DAOReward, big.NewInt(100))
s.AddBalance(dao.Address, types.GetBaseCoin(), DAOReward)
edb.SaveEvent(height, eventsdb.RewardEvent{
edb.AddEvent(height, eventsdb.RewardEvent{
Role: eventsdb.RoleDAO,
Address: dao.Address,
Amount: DAOReward.String(),
Amount: DAOReward.Bytes(),
ValidatorPubKey: validator.PubKey,
})

Expand All @@ -842,10 +840,10 @@ func (s *StateDB) PayRewards(height int64) {
DevelopersReward.Mul(DevelopersReward, big.NewInt(int64(developers.Commission)))
DevelopersReward.Div(DevelopersReward, big.NewInt(100))
s.AddBalance(developers.Address, types.GetBaseCoin(), DevelopersReward)
edb.SaveEvent(height, eventsdb.RewardEvent{
edb.AddEvent(height, eventsdb.RewardEvent{
Role: eventsdb.RoleDevelopers,
Address: developers.Address,
Amount: DevelopersReward.String(),
Amount: DevelopersReward.Bytes(),
ValidatorPubKey: validator.PubKey,
})

Expand All @@ -858,10 +856,10 @@ func (s *StateDB) PayRewards(height int64) {
validatorReward.Div(validatorReward, big.NewInt(100))
totalReward.Sub(totalReward, validatorReward)
s.AddBalance(validator.CandidateAddress, types.GetBaseCoin(), validatorReward)
edb.SaveEvent(height, eventsdb.RewardEvent{
edb.AddEvent(height, eventsdb.RewardEvent{
Role: eventsdb.RoleValidator,
Address: validator.CandidateAddress,
Amount: validatorReward.String(),
Amount: validatorReward.Bytes(),
ValidatorPubKey: validator.PubKey,
})

Expand All @@ -885,10 +883,10 @@ func (s *StateDB) PayRewards(height int64) {

s.AddBalance(stake.Owner, types.GetBaseCoin(), reward)

edb.SaveEvent(height, eventsdb.RewardEvent{
edb.AddEvent(height, eventsdb.RewardEvent{
Role: eventsdb.RoleDelegator,
Address: stake.Owner,
Amount: reward.String(),
Amount: reward.Bytes(),
ValidatorPubKey: candidate.PubKey,
})
}
Expand Down Expand Up @@ -1063,9 +1061,9 @@ func (s *StateDB) SetValidatorAbsent(height int64, address [20]byte) {
slashed := big.NewInt(0).Set(stake.Value)
slashed.Sub(slashed, newValue)

edb.SaveEvent(height, eventsdb.SlashEvent{
edb.AddEvent(height, eventsdb.SlashEvent{
Address: stake.Owner,
Amount: slashed.String(),
Amount: slashed.Bytes(),
Coin: stake.Coin,
ValidatorPubKey: candidate.PubKey,
})
Expand Down Expand Up @@ -1121,9 +1119,9 @@ func (s *StateDB) PunishByzantineValidator(currentBlock uint64, address [20]byte
slashed := big.NewInt(0).Set(stake.Value)
slashed.Sub(slashed, newValue)

edb.SaveEvent(int64(currentBlock), eventsdb.SlashEvent{
edb.AddEvent(int64(currentBlock), eventsdb.SlashEvent{
Address: stake.Owner,
Amount: slashed.String(),
Amount: slashed.Bytes(),
Coin: stake.Coin,
ValidatorPubKey: candidate.PubKey,
})
Expand Down
2 changes: 1 addition & 1 deletion core/transaction/declare_candidacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (data DeclareCandidacyData) MarshalJSON() ([]byte, error) {

func (data DeclareCandidacyData) String() string {
return fmt.Sprintf("DECLARE CANDIDACY address:%s pubkey:%s commission: %d ",
data.Address.String(), hexutil.Encode(data.PubKey[:]), data.Commission)
data.Address.String(), hexutil.Encode(data.PubKey), data.Commission)
}

func (data DeclareCandidacyData) Gas() int64 {
Expand Down
2 changes: 1 addition & 1 deletion core/transaction/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (data DelegateData) MarshalJSON() ([]byte, error) {

func (data DelegateData) String() string {
return fmt.Sprintf("DELEGATE pubkey:%s ",
hexutil.Encode(data.PubKey[:]))
hexutil.Encode(data.PubKey))
}

func (data DelegateData) Gas() int64 {
Expand Down
2 changes: 1 addition & 1 deletion core/transaction/unbond.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (data UnbondData) MarshalJSON() ([]byte, error) {

func (data UnbondData) String() string {
return fmt.Sprintf("UNBOND pubkey:%s",
hexutil.Encode(data.PubKey[:]))
hexutil.Encode(data.PubKey))
}

func (data UnbondData) Gas() int64 {
Expand Down
2 changes: 1 addition & 1 deletion core/types/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func Hex2BytesFixed(str string, flen int) []byte {
return h[len(h)-flen:]
} else {
hh := make([]byte, flen)
copy(hh[flen-len(h):flen], h[:])
copy(hh[flen-len(h):flen], h)
return hh
}
}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.4"
services:
minter:
image: minterteam/minter:0.3.2
image: minterteam/minter:0.3.4
volumes:
- ~/.minter:/minter
ports:
Expand Down
Loading

0 comments on commit 3be4dc0

Please sign in to comment.