From e8f933af1d7b28cb5852a417ee731c5ec341fa30 Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Sun, 29 Sep 2019 06:23:46 +0300 Subject: [PATCH 1/7] Fix apis for contract tx --- api/service/explorer/storage.go | 8 +++----- api/service/explorer/structs.go | 15 +++++++-------- internal/hmyapi/blockchain.go | 3 ++- internal/hmyapi/transactionpool.go | 13 ++++++++++--- internal/hmyapi/types.go | 31 ++++++++++++++++++------------ 5 files changed, 41 insertions(+), 29 deletions(-) diff --git a/api/service/explorer/storage.go b/api/service/explorer/storage.go index 27699a881..85b669719 100644 --- a/api/service/explorer/storage.go +++ b/api/service/explorer/storage.go @@ -112,10 +112,6 @@ func (storage *Storage) Dump(block *types.Block, height uint64) { // Store txs for _, tx := range block.Transactions() { - if tx.To() == nil { - continue - } - explorerTransaction := GetTransaction(tx, block) storage.UpdateTXStorage(batch, explorerTransaction, tx) storage.UpdateAddress(batch, explorerTransaction, tx) @@ -158,7 +154,9 @@ func (storage *Storage) UpdateTXStorage(batch ethdb.Batch, explorerTransaction * // TODO: deprecate this logic func (storage *Storage) UpdateAddress(batch ethdb.Batch, explorerTransaction *Transaction, tx *types.Transaction) { explorerTransaction.Type = Received - storage.UpdateAddressStorage(batch, explorerTransaction.To, explorerTransaction, tx) + if explorerTransaction.To != "" { + storage.UpdateAddressStorage(batch, explorerTransaction.To, explorerTransaction, tx) + } explorerTransaction.Type = Sent storage.UpdateAddressStorage(batch, explorerTransaction.From, explorerTransaction, tx) } diff --git a/api/service/explorer/structs.go b/api/service/explorer/structs.go index 5060b4908..f898e09f9 100644 --- a/api/service/explorer/structs.go +++ b/api/service/explorer/structs.go @@ -5,10 +5,8 @@ import ( "math/big" "strconv" - "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/harmony/core/types" - common2 "github.com/harmony-one/harmony/internal/common" + "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/utils" ) @@ -114,20 +112,21 @@ func NewBlock(block *types.Block, height int) *Block { // GetTransaction ... func GetTransaction(tx *types.Transaction, addressBlock *types.Block) *Transaction { - if tx.To() == nil { - return nil - } msg, err := tx.AsMessage(types.NewEIP155Signer(tx.ChainID())) if err != nil { utils.Logger().Error().Err(err).Msg("Error when parsing tx into message") } gasFee := big.NewInt(0) gasFee = gasFee.Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas())) + to := "" + if msg.To() != nil { + to = common.MustAddressToBech32(*msg.To()) + } return &Transaction{ ID: tx.Hash().Hex(), Timestamp: strconv.Itoa(int(addressBlock.Time().Int64() * 1000)), - From: common2.MustAddressToBech32(common.HexToAddress(msg.From().Hex())), - To: common2.MustAddressToBech32(common.HexToAddress(msg.To().Hex())), + From: common.MustAddressToBech32(msg.From()), + To: to, Value: msg.Value(), Bytes: strconv.Itoa(int(tx.Size())), Data: hex.EncodeToString(tx.Data()), diff --git a/internal/hmyapi/blockchain.go b/internal/hmyapi/blockchain.go index c36a8b374..e81250867 100644 --- a/internal/hmyapi/blockchain.go +++ b/internal/hmyapi/blockchain.go @@ -41,6 +41,7 @@ func NewPublicBlockChainAPI(b Backend) *PublicBlockChainAPI { func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, blockNr rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { block, err := s.b.BlockByNumber(ctx, blockNr) if block != nil { + utils.Logger().Info().Msgf("HASH block %s", block.Hash().String()) response, err := RPCMarshalBlock(block, true, fullTx) if err == nil && blockNr == rpc.PendingBlockNumber { // Pending blocks need to nil out a few fields @@ -58,7 +59,7 @@ func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, blockNr rpc. func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, blockHash common.Hash, fullTx bool) (map[string]interface{}, error) { block, err := s.b.GetBlock(ctx, blockHash) if block != nil { - return RPCMarshalBlock(block, false, false) + return RPCMarshalBlock(block, true, fullTx) } return nil, err } diff --git a/internal/hmyapi/transactionpool.go b/internal/hmyapi/transactionpool.go index 25ceee573..77513b154 100644 --- a/internal/hmyapi/transactionpool.go +++ b/internal/hmyapi/transactionpool.go @@ -13,6 +13,7 @@ import ( "github.com/harmony-one/harmony/core/rawdb" "github.com/harmony-one/harmony/core/types" internal_common "github.com/harmony-one/harmony/internal/common" + "github.com/harmony-one/harmony/internal/utils" ) // TxHistoryArgs is struct to make GetTransactionsHistory request @@ -93,6 +94,7 @@ func (s *PublicTransactionPoolAPI) GetTransactionByBlockHashAndIndex(ctx context func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) *RPCTransaction { // Try to return an already finalized transaction if tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash); tx != nil { + utils.Logger().Info().Msgf("HASH %s", tx.Hash().String()) return newRPCTransaction(tx, blockHash, blockNumber, index) } // No finalized transaction, try to retrieve it from the pool @@ -174,6 +176,7 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, ha if tx == nil { return nil, nil } + utils.Logger().Info().Msgf("HASH %s", tx.Hash().String()) receipts, err := s.b.GetReceipts(ctx, blockHash) if err != nil { return nil, err @@ -188,14 +191,18 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, ha signer = types.NewEIP155Signer(tx.ChainID()) } from, _ := types.Sender(signer, tx) - + to := "" + utils.Logger().Info().Msgf("LOLOLO %s", internal_common.MustAddressToBech32(from)) + if tx.To() != nil { + to = internal_common.MustAddressToBech32(*tx.To()) + } fields := map[string]interface{}{ "blockHash": blockHash, "blockNumber": hexutil.Uint64(blockNumber), "transactionHash": hash, "transactionIndex": hexutil.Uint64(index), - "from": from, - "to": tx.To(), + "from": internal_common.MustAddressToBech32(from), + "to": to, "shardID": tx.ShardID(), "gasUsed": hexutil.Uint64(receipt.GasUsed), "cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed), diff --git a/internal/hmyapi/types.go b/internal/hmyapi/types.go index 210cd3f7e..f4f021732 100644 --- a/internal/hmyapi/types.go +++ b/internal/hmyapi/types.go @@ -10,7 +10,8 @@ import ( ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/harmony-one/harmony/block" "github.com/harmony-one/harmony/core/types" - common2 "github.com/harmony-one/harmony/internal/common" + internal_common "github.com/harmony-one/harmony/internal/common" + "github.com/harmony-one/harmony/internal/utils" ) // RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction @@ -78,7 +79,7 @@ func newHeaderInformation(header *block.Header) *HeaderInformation { sig := header.LastCommitSignature() result.LastCommitSig = hex.EncodeToString(sig[:]) - bechAddr, err := common2.AddressToBech32(header.Coinbase()) + bechAddr, err := internal_common.AddressToBech32(header.Coinbase()) if err != nil { bechAddr = header.Coinbase().Hex() } @@ -101,13 +102,15 @@ func newRPCCXReceipt(cx *types.CXReceipt, blockHash common.Hash, blockNumber uin result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber)) } - fromAddr, err := common2.AddressToBech32(cx.From) + fromAddr, err := internal_common.AddressToBech32(cx.From) if err != nil { - fromAddr = cx.From.Hex() + return nil } - toAddr, err := common2.AddressToBech32(*cx.To) - if err != nil { - toAddr = (*cx.To).Hex() + toAddr := "" + if cx.To != nil { + if toAddr, err = internal_common.AddressToBech32(*cx.To); err != nil { + return nil + } } result.From = fromAddr result.To = toAddr @@ -144,13 +147,17 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber result.TransactionIndex = hexutil.Uint(index) } - fromAddr, err := common2.AddressToBech32(from) + fromAddr, err := internal_common.AddressToBech32(from) if err != nil { - fromAddr = from.Hex() + return nil } - toAddr, err := common2.AddressToBech32(*tx.To()) - if err != nil { - toAddr = (*tx.To()).Hex() + toAddr := "" + + utils.Logger().Info().Msgf("LOLOLO %s", fromAddr) + if tx.To() != nil { + if toAddr, err = internal_common.AddressToBech32(*tx.To()); err != nil { + return nil + } } result.From = fromAddr result.To = toAddr From b1e5b91cfe2af2295559d5d4e4a44bdfe6cfca8b Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Sun, 29 Sep 2019 06:30:06 +0300 Subject: [PATCH 2/7] Remove debug log --- internal/hmyapi/blockchain.go | 1 - internal/hmyapi/transactionpool.go | 3 --- 2 files changed, 4 deletions(-) diff --git a/internal/hmyapi/blockchain.go b/internal/hmyapi/blockchain.go index e81250867..28a6d82dc 100644 --- a/internal/hmyapi/blockchain.go +++ b/internal/hmyapi/blockchain.go @@ -41,7 +41,6 @@ func NewPublicBlockChainAPI(b Backend) *PublicBlockChainAPI { func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, blockNr rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { block, err := s.b.BlockByNumber(ctx, blockNr) if block != nil { - utils.Logger().Info().Msgf("HASH block %s", block.Hash().String()) response, err := RPCMarshalBlock(block, true, fullTx) if err == nil && blockNr == rpc.PendingBlockNumber { // Pending blocks need to nil out a few fields diff --git a/internal/hmyapi/transactionpool.go b/internal/hmyapi/transactionpool.go index 77513b154..08e9d6669 100644 --- a/internal/hmyapi/transactionpool.go +++ b/internal/hmyapi/transactionpool.go @@ -94,7 +94,6 @@ func (s *PublicTransactionPoolAPI) GetTransactionByBlockHashAndIndex(ctx context func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) *RPCTransaction { // Try to return an already finalized transaction if tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash); tx != nil { - utils.Logger().Info().Msgf("HASH %s", tx.Hash().String()) return newRPCTransaction(tx, blockHash, blockNumber, index) } // No finalized transaction, try to retrieve it from the pool @@ -176,7 +175,6 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, ha if tx == nil { return nil, nil } - utils.Logger().Info().Msgf("HASH %s", tx.Hash().String()) receipts, err := s.b.GetReceipts(ctx, blockHash) if err != nil { return nil, err @@ -192,7 +190,6 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, ha } from, _ := types.Sender(signer, tx) to := "" - utils.Logger().Info().Msgf("LOLOLO %s", internal_common.MustAddressToBech32(from)) if tx.To() != nil { to = internal_common.MustAddressToBech32(*tx.To()) } From 63a7f5af5bc54c4519d784711e11e84375c7d9a7 Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Sun, 29 Sep 2019 06:31:33 +0300 Subject: [PATCH 3/7] Fix travis --- internal/hmyapi/transactionpool.go | 1 - internal/hmyapi/types.go | 2 -- 2 files changed, 3 deletions(-) diff --git a/internal/hmyapi/transactionpool.go b/internal/hmyapi/transactionpool.go index 08e9d6669..1e7c9ab57 100644 --- a/internal/hmyapi/transactionpool.go +++ b/internal/hmyapi/transactionpool.go @@ -13,7 +13,6 @@ import ( "github.com/harmony-one/harmony/core/rawdb" "github.com/harmony-one/harmony/core/types" internal_common "github.com/harmony-one/harmony/internal/common" - "github.com/harmony-one/harmony/internal/utils" ) // TxHistoryArgs is struct to make GetTransactionsHistory request diff --git a/internal/hmyapi/types.go b/internal/hmyapi/types.go index f4f021732..c58941ad3 100644 --- a/internal/hmyapi/types.go +++ b/internal/hmyapi/types.go @@ -11,7 +11,6 @@ import ( "github.com/harmony-one/harmony/block" "github.com/harmony-one/harmony/core/types" internal_common "github.com/harmony-one/harmony/internal/common" - "github.com/harmony-one/harmony/internal/utils" ) // RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction @@ -153,7 +152,6 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber } toAddr := "" - utils.Logger().Info().Msgf("LOLOLO %s", fromAddr) if tx.To() != nil { if toAddr, err = internal_common.AddressToBech32(*tx.To()); err != nil { return nil From daeca66ef25e2fd40f731cde17181afe931c27cd Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Mon, 30 Sep 2019 00:29:05 +0300 Subject: [PATCH 4/7] Replace MustAddressToBech32 with AddressToBech32 --- api/service/explorer/structs.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/api/service/explorer/structs.go b/api/service/explorer/structs.go index f898e09f9..0097f45fe 100644 --- a/api/service/explorer/structs.go +++ b/api/service/explorer/structs.go @@ -119,13 +119,20 @@ func GetTransaction(tx *types.Transaction, addressBlock *types.Block) *Transacti gasFee := big.NewInt(0) gasFee = gasFee.Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas())) to := "" + var err error if msg.To() != nil { - to = common.MustAddressToBech32(*msg.To()) + if to, err = common.AddressToBech32(*msg.To()); err != nil { + return nil + } + } + from := "" + if from, err = common.AddressToBech32(msg.From()); err != nil { + return nil } return &Transaction{ ID: tx.Hash().Hex(), Timestamp: strconv.Itoa(int(addressBlock.Time().Int64() * 1000)), - From: common.MustAddressToBech32(msg.From()), + From: from, To: to, Value: msg.Value(), Bytes: strconv.Itoa(int(tx.Size())), From 829e027c04b71adb23e26070c77a86eb3b540460 Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Mon, 30 Sep 2019 00:29:56 +0300 Subject: [PATCH 5/7] Compile error --- api/service/explorer/structs.go | 1 - 1 file changed, 1 deletion(-) diff --git a/api/service/explorer/structs.go b/api/service/explorer/structs.go index 0097f45fe..8bf69c7a9 100644 --- a/api/service/explorer/structs.go +++ b/api/service/explorer/structs.go @@ -119,7 +119,6 @@ func GetTransaction(tx *types.Transaction, addressBlock *types.Block) *Transacti gasFee := big.NewInt(0) gasFee = gasFee.Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas())) to := "" - var err error if msg.To() != nil { if to, err = common.AddressToBech32(*msg.To()); err != nil { return nil From bc482ddcc9971c3aaf995a49ae9abc2a509156a8 Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Mon, 30 Sep 2019 00:30:53 +0300 Subject: [PATCH 6/7] Golint --- api/service/explorer/structs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/service/explorer/structs.go b/api/service/explorer/structs.go index 8bf69c7a9..f14742101 100644 --- a/api/service/explorer/structs.go +++ b/api/service/explorer/structs.go @@ -121,7 +121,7 @@ func GetTransaction(tx *types.Transaction, addressBlock *types.Block) *Transacti to := "" if msg.To() != nil { if to, err = common.AddressToBech32(*msg.To()); err != nil { - return nil + return nil } } from := "" From 3f4953a357b40ed28c0aa2e1bc0f4c9ac0b1d276 Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Tue, 1 Oct 2019 01:19:10 +0300 Subject: [PATCH 7/7] Remove truffle one1 address error --- internal/hmyapi/transactionpool.go | 20 ++++++++++++-------- internal/hmyapi/types.go | 5 ++++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/internal/hmyapi/transactionpool.go b/internal/hmyapi/transactionpool.go index 1e7c9ab57..a9391a771 100644 --- a/internal/hmyapi/transactionpool.go +++ b/internal/hmyapi/transactionpool.go @@ -187,18 +187,11 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, ha if tx.Protected() { signer = types.NewEIP155Signer(tx.ChainID()) } - from, _ := types.Sender(signer, tx) - to := "" - if tx.To() != nil { - to = internal_common.MustAddressToBech32(*tx.To()) - } fields := map[string]interface{}{ "blockHash": blockHash, "blockNumber": hexutil.Uint64(blockNumber), "transactionHash": hash, "transactionIndex": hexutil.Uint64(index), - "from": internal_common.MustAddressToBech32(from), - "to": to, "shardID": tx.ShardID(), "gasUsed": hexutil.Uint64(receipt.GasUsed), "cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed), @@ -206,7 +199,18 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, ha "logs": receipt.Logs, "logsBloom": receipt.Bloom, } - + from, _ := types.Sender(signer, tx) + fields["from"] = from + if tx.To() != nil { + fields["to"], err = internal_common.AddressToBech32(*tx.To()) + if err != nil { + return nil, err + } + fields["from"], err = internal_common.AddressToBech32(from) + if err != nil { + return nil, err + } + } // Assign receipt status or post state. if len(receipt.PostState) > 0 { fields["root"] = hexutil.Bytes(receipt.PostState) diff --git a/internal/hmyapi/types.go b/internal/hmyapi/types.go index c58941ad3..d1b02b966 100644 --- a/internal/hmyapi/types.go +++ b/internal/hmyapi/types.go @@ -3,6 +3,7 @@ package hmyapi import ( "encoding/hex" "math/big" + "strings" "time" "github.com/ethereum/go-ethereum/common" @@ -156,8 +157,10 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber if toAddr, err = internal_common.AddressToBech32(*tx.To()); err != nil { return nil } + result.From = fromAddr + } else { + result.From = strings.ToLower(from.Hex()) } - result.From = fromAddr result.To = toAddr return result