From b6a5086e92e0c8c7b1db45fe8de3dab8af02043d Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Sat, 15 Dec 2018 11:02:13 -0800 Subject: [PATCH 1/5] fix field format --- services/explorer/service.go | 13 +++++++------ services/explorer/structs.go | 5 ++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/services/explorer/service.go b/services/explorer/service.go index 0f42d85e8..7ac5292b0 100644 --- a/services/explorer/service.go +++ b/services/explorer/service.go @@ -198,11 +198,12 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) { continue } block := &Block{ - Height: string(id + fromInt - 1), - Hash: accountBlock.Hash().Hex(), - TXCount: string(accountBlock.Transactions().Len()), - Timestamp: GetTime(accountBlock.Time().Int64()), + Height: strconv.Itoa(id + fromInt - 1), + ID: accountBlock.Hash().Hex(), + TXCount: strconv.Itoa(accountBlock.Transactions().Len()), + Timestamp: strconv.Itoa(int(accountBlock.Time().Int64())), MerkleRoot: accountBlock.Hash().Hex(), + Bytes: strconv.Itoa(int(accountBlock.Size())), } // Populate transactions for _, tx := range accountBlock.Transactions() { @@ -216,7 +217,7 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) { } else { block.PrevBlock = RefBlock{ ID: accountBlocks[id-1].Hash().Hex(), - Height: string(id + fromInt - 2), + Height: strconv.Itoa(id + fromInt - 2), } } if accountBlocks[id+1] == nil { @@ -227,7 +228,7 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) { } else { block.NextBlock = RefBlock{ ID: accountBlocks[id+1].Hash().Hex(), - Height: string(id + fromInt), + Height: strconv.Itoa(id + fromInt), } } data.Blocks = append(data.Blocks, block) diff --git a/services/explorer/structs.go b/services/explorer/structs.go index 5c204fdcb..e1d4a9a23 100644 --- a/services/explorer/structs.go +++ b/services/explorer/structs.go @@ -14,7 +14,7 @@ type Data struct { // Address ... type Address struct { - Hash string `json:"hash"` + ID string `json:"id"` Balance float64 `json:"balance"` TXCount int `json:"txCount"` TXs []Transaction `json:"txs"` @@ -41,12 +41,11 @@ type BlockInfo struct { // Block ... type Block struct { Height string `json:"height"` - Hash string `json:"hash"` + ID string `json:"id"` TXCount string `json:"txCount"` Timestamp string `json:"timestamp"` MerkleRoot string `json:"merkleRoot"` PrevBlock RefBlock `json:"prevBlock"` - Bits string `json:"bits"` Bytes string `json:"bytes"` NextBlock RefBlock `json:"nextBlock"` TXs []Transaction `json:"txs"` From b7e0cf046de6f5ad364a506e91dc99560d154a1b Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Sat, 15 Dec 2018 11:19:08 -0800 Subject: [PATCH 2/5] fix transaction timestamp --- services/explorer/service.go | 15 +++++++-------- services/explorer/storage.go | 12 +++++++++++- services/explorer/structs.go | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/services/explorer/service.go b/services/explorer/service.go index 7ac5292b0..b2771bb04 100644 --- a/services/explorer/service.go +++ b/services/explorer/service.go @@ -148,17 +148,16 @@ func GetTime(timestamp int64) string { } // GetTransaction ... -func GetTransaction(tx *types.Transaction) Transaction { +func GetTransaction(tx *types.Transaction, accountBlock *types.Block) Transaction { if tx.To() == nil { return Transaction{} } return Transaction{ - ID: tx.Hash().Hex(), - // Timestamp: GetTime(accountBlock.Time().Int64()), - Timestamp: GetTime(3422343), + ID: tx.Hash().Hex(), + Timestamp: strconv.Itoa(int(accountBlock.Size())), From: tx.To().Hex(), To: tx.To().Hex(), - Value: tx.GasPrice().Int64(), // TODO(minh): fix it + Value: strconv.Itoa(int(tx.GasPrice().Int64())), } } @@ -207,7 +206,7 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) { } // Populate transactions for _, tx := range accountBlock.Transactions() { - block.TXs = append(block.TXs, GetTransaction(tx)) + block.TXs = append(block.TXs, GetTransaction(tx, accountBlock)) } if accountBlocks[id-1] == nil { block.PrevBlock = RefBlock{ @@ -252,11 +251,11 @@ func (s *Service) GetExplorerTransaction(w http.ResponseWriter, r *http.Request) json.NewEncoder(w).Encode(data.TX) return } - tx := new(types.Transaction) + tx := new(Transaction) if rlp.DecodeBytes(bytes, tx) != nil { json.NewEncoder(w).Encode(data.TX) return } - data.TX = GetTransaction(tx) + data.TX = *tx json.NewEncoder(w).Encode(data.TX) } diff --git a/services/explorer/storage.go b/services/explorer/storage.go index f38789fb2..d17a8cb32 100644 --- a/services/explorer/storage.go +++ b/services/explorer/storage.go @@ -108,7 +108,17 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) { // Store txs fmt.Println("# of txs ", len(block.Transactions())) for _, tx := range block.Transactions() { - if data, err := rlp.EncodeToBytes(tx); err == nil { + if tx.To() == nil { + continue + } + explorerTransaction := Transaction{ + ID: tx.Hash().Hex(), + Timestamp: strconv.Itoa(int(block.Time().Int64())), + From: tx.To().Hex(), + To: tx.To().Hex(), + Value: strconv.Itoa(int(tx.Value().Int64())), + } + if data, err := rlp.EncodeToBytes(explorerTransaction); err == nil { key := GetTXKey(tx.Hash().Hex()) storage.db.Put([]byte(key), data) } else { diff --git a/services/explorer/structs.go b/services/explorer/structs.go index e1d4a9a23..8d31c6935 100644 --- a/services/explorer/structs.go +++ b/services/explorer/structs.go @@ -26,7 +26,7 @@ type Transaction struct { Timestamp string `json:"timestamp"` From string `json:"from"` To string `json:"to"` - Value int64 `json:"value"` + Value string `json:"value"` } // BlockInfo ... From 825073418bf25724ee1f8d4b61dc6aeee0591c48 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Sat, 15 Dec 2018 11:29:32 -0800 Subject: [PATCH 3/5] clean up code --- services/explorer/service.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/services/explorer/service.go b/services/explorer/service.go index b2771bb04..9100369f9 100644 --- a/services/explorer/service.go +++ b/services/explorer/service.go @@ -8,7 +8,6 @@ import ( "net/http" "os" "strconv" - "time" "github.com/ethereum/go-ethereum/rlp" "github.com/gorilla/mux" @@ -142,11 +141,6 @@ func (s *Service) GetAccountBlocks(from, to int) []*types.Block { return blocks } -// GetTime ... -func GetTime(timestamp int64) string { - return time.Unix(timestamp, 0).String() -} - // GetTransaction ... func GetTransaction(tx *types.Transaction, accountBlock *types.Block) Transaction { if tx.To() == nil { From 7c8bfda2577edbf71f5460e0f3bee6fc5aaa58f4 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Sat, 15 Dec 2018 11:37:42 -0800 Subject: [PATCH 4/5] fix the timestamp --- services/explorer/service.go | 2 +- services/explorer/storage.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/explorer/service.go b/services/explorer/service.go index 9100369f9..93227aa3a 100644 --- a/services/explorer/service.go +++ b/services/explorer/service.go @@ -148,7 +148,7 @@ func GetTransaction(tx *types.Transaction, accountBlock *types.Block) Transactio } return Transaction{ ID: tx.Hash().Hex(), - Timestamp: strconv.Itoa(int(accountBlock.Size())), + Timestamp: strconv.Itoa(int(accountBlock.Time().Int64())), From: tx.To().Hex(), To: tx.To().Hex(), Value: strconv.Itoa(int(tx.GasPrice().Int64())), diff --git a/services/explorer/storage.go b/services/explorer/storage.go index d17a8cb32..8e90560c9 100644 --- a/services/explorer/storage.go +++ b/services/explorer/storage.go @@ -90,7 +90,7 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) { blockInfo := BlockInfo{ ID: block.Hash().Hex(), Height: string(height), - Timestamp: string(block.Time().Int64()), + Timestamp: strconv.Itoa(int(block.Time().Int64())), TXCount: string(block.Transactions().Len()), Size: block.Size().String(), } From dab273a441b29d0a0b3bb18777015f90d5215b50 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Sat, 15 Dec 2018 12:20:44 -0800 Subject: [PATCH 5/5] add size for tx --- services/explorer/service.go | 1 + services/explorer/storage.go | 1 + services/explorer/structs.go | 1 + 3 files changed, 3 insertions(+) diff --git a/services/explorer/service.go b/services/explorer/service.go index 93227aa3a..d7a4263d9 100644 --- a/services/explorer/service.go +++ b/services/explorer/service.go @@ -152,6 +152,7 @@ func GetTransaction(tx *types.Transaction, accountBlock *types.Block) Transactio From: tx.To().Hex(), To: tx.To().Hex(), Value: strconv.Itoa(int(tx.GasPrice().Int64())), + Bytes: strconv.Itoa(int(tx.Size())), } } diff --git a/services/explorer/storage.go b/services/explorer/storage.go index 8e90560c9..3f4264f88 100644 --- a/services/explorer/storage.go +++ b/services/explorer/storage.go @@ -117,6 +117,7 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) { From: tx.To().Hex(), To: tx.To().Hex(), Value: strconv.Itoa(int(tx.Value().Int64())), + Bytes: strconv.Itoa(int(tx.Size())), } if data, err := rlp.EncodeToBytes(explorerTransaction); err == nil { key := GetTXKey(tx.Hash().Hex()) diff --git a/services/explorer/structs.go b/services/explorer/structs.go index 8d31c6935..0b6270fb8 100644 --- a/services/explorer/structs.go +++ b/services/explorer/structs.go @@ -27,6 +27,7 @@ type Transaction struct { From string `json:"from"` To string `json:"to"` Value string `json:"value"` + Bytes string `json:"bytes"` } // BlockInfo ...