forked from cerc-io/plugeth
Block size
This commit is contained in:
parent
3f904bf3ac
commit
6afc16399f
@ -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()))
|
||||
}
|
||||
|
@ -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 {
|
||||
|
15
ethutil/size.go
Normal file
15
ethutil/size.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
12
ethutil/size_test.go
Normal file
12
ethutil/size_test.go
Normal file
@ -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))
|
||||
}
|
Loading…
Reference in New Issue
Block a user