From 6afc16399f9624663579ad72950b4ea3b887db57 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 25 Aug 2014 12:53:06 +0200 Subject: [PATCH] Block size --- ethchain/block.go | 7 ++++++- ethpipe/js_types.go | 3 ++- ethutil/size.go | 15 +++++++++++++++ ethutil/size_test.go | 12 ++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 ethutil/size.go create mode 100644 ethutil/size_test.go diff --git a/ethchain/block.go b/ethchain/block.go index 5765abd519..d2d012e559 100644 --- a/ethchain/block.go +++ b/ethchain/block.go @@ -351,7 +351,7 @@ func (block *Block) header() []interface{} { func (block *Block) String() string { return fmt.Sprintf(` - BLOCK(%x): + BLOCK(%x): Size: %v PrevHash: %x UncleSha: %x Coinbase: %x @@ -368,6 +368,7 @@ func (block *Block) String() string { NumTx: %v `, block.Hash(), + block.Size(), block.PrevHash, block.UncleSha, block.Coinbase, @@ -384,3 +385,7 @@ func (block *Block) String() string { len(block.transactions), ) } + +func (self *Block) Size() ethutil.StorageSize { + return ethutil.StorageSize(len(self.RlpEncode())) +} diff --git a/ethpipe/js_types.go b/ethpipe/js_types.go index 0fb3a38768..d9cbef12dc 100644 --- a/ethpipe/js_types.go +++ b/ethpipe/js_types.go @@ -14,6 +14,7 @@ import ( // Block interface exposed to QML type JSBlock struct { ref *ethchain.Block + Size string `json:"size"` Number int `json:"number"` Hash string `json:"hash"` Transactions string `json:"transactions"` @@ -40,7 +41,7 @@ func NewJSBlock(block *ethchain.Block) *JSBlock { return nil } - return &JSBlock{ref: block, Number: int(block.Number.Uint64()), GasUsed: block.GasUsed.String(), GasLimit: block.GasLimit.String(), Hash: ethutil.Bytes2Hex(block.Hash()), Transactions: string(txJson), Time: block.Time, Coinbase: ethutil.Bytes2Hex(block.Coinbase)} + return &JSBlock{ref: block, Size: block.Size().String(), Number: int(block.Number.Uint64()), GasUsed: block.GasUsed.String(), GasLimit: block.GasLimit.String(), Hash: ethutil.Bytes2Hex(block.Hash()), Transactions: string(txJson), Time: block.Time, Coinbase: ethutil.Bytes2Hex(block.Coinbase)} } func (self *JSBlock) ToString() string { diff --git a/ethutil/size.go b/ethutil/size.go new file mode 100644 index 0000000000..b4426465e6 --- /dev/null +++ b/ethutil/size.go @@ -0,0 +1,15 @@ +package ethutil + +import "fmt" + +type StorageSize float64 + +func (self StorageSize) String() string { + if self > 1000000 { + return fmt.Sprintf("%.2f mB", self/1000000) + } else if self > 1000 { + return fmt.Sprintf("%.2f kB", self/1000) + } else { + return fmt.Sprintf("%.2f B", self) + } +} diff --git a/ethutil/size_test.go b/ethutil/size_test.go new file mode 100644 index 0000000000..82aa1c6533 --- /dev/null +++ b/ethutil/size_test.go @@ -0,0 +1,12 @@ +package ethutil + +import ( + "fmt" + "testing" +) + +func TestSize(t *testing.T) { + fmt.Println(StorageSize(2381273)) + fmt.Println(StorageSize(2192)) + fmt.Println(StorageSize(12)) +}