Skip to content

Commit

Permalink
core/types: remove header accessors
Browse files Browse the repository at this point in the history
These accessors were introduced by light client changes, but
the only method that is actually used is GetNumberU64. This
commit replaces all uses of .GetNumberU64 with .Number.Uint64.
  • Loading branch information
fjl committed Nov 9, 2016
1 parent 0f19cbc commit be38652
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 35 deletions.
12 changes: 6 additions & 6 deletions core/database_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,24 +632,24 @@ func GetChainConfig(db ethdb.Database, hash common.Hash) (*ChainConfig, error) {

// FindCommonAncestor returns the last common ancestor of two block headers
func FindCommonAncestor(db ethdb.Database, a, b *types.Header) *types.Header {
for a.GetNumberU64() > b.GetNumberU64() {
a = GetHeader(db, a.ParentHash, a.GetNumberU64()-1)
for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
a = GetHeader(db, a.ParentHash, a.Number.Uint64()-1)
if a == nil {
return nil
}
}
for a.GetNumberU64() < b.GetNumberU64() {
b = GetHeader(db, b.ParentHash, b.GetNumberU64()-1)
for an := a.Number.Uint64(); an < b.Number.Uint64(); {
b = GetHeader(db, b.ParentHash, b.Number.Uint64()-1)
if b == nil {
return nil
}
}
for a.Hash() != b.Hash() {
a = GetHeader(db, a.ParentHash, a.GetNumberU64()-1)
a = GetHeader(db, a.ParentHash, a.Number.Uint64()-1)
if a == nil {
return nil
}
b = GetHeader(db, b.ParentHash, b.GetNumberU64()-1)
b = GetHeader(db, b.ParentHash, b.Number.Uint64()-1)
if b == nil {
return nil
}
Expand Down
9 changes: 0 additions & 9 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,6 @@ type jsonHeader struct {
Nonce *BlockNonce `json:"nonce"`
}

func (h *Header) GetNumber() *big.Int { return new(big.Int).Set(h.Number) }
func (h *Header) GetGasLimit() *big.Int { return new(big.Int).Set(h.GasLimit) }
func (h *Header) GetGasUsed() *big.Int { return new(big.Int).Set(h.GasUsed) }
func (h *Header) GetDifficulty() *big.Int { return new(big.Int).Set(h.Difficulty) }
func (h *Header) GetTime() *big.Int { return new(big.Int).Set(h.Time) }
func (h *Header) GetNumberU64() uint64 { return h.Number.Uint64() }
func (h *Header) GetNonce() uint64 { return binary.BigEndian.Uint64(h.Nonce[:]) }
func (h *Header) GetExtra() []byte { return common.CopyBytes(h.Extra) }

// Hash returns the block hash of the header, which is simply the keccak256 hash of its
// RLP encoding.
func (h *Header) Hash() common.Hash {
Expand Down
4 changes: 2 additions & 2 deletions eth/filters/filter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ func (es *EventSystem) lightFilterNewHead(newHeader *types.Header, callBack func
// find common ancestor, create list of rolled back and new block hashes
var oldHeaders, newHeaders []*types.Header
for oldh.Hash() != newh.Hash() {
if oldh.GetNumberU64() >= newh.GetNumberU64() {
if oldh.Number.Uint64() >= newh.Number.Uint64() {
oldHeaders = append(oldHeaders, oldh)
oldh = core.GetHeader(es.backend.ChainDb(), oldh.ParentHash, oldh.Number.Uint64()-1)
}
if oldh.GetNumberU64() < newh.GetNumberU64() {
if oldh.Number.Uint64() < newh.Number.Uint64() {
newHeaders = append(newHeaders, newh)
newh = core.GetHeader(es.backend.ChainDb(), newh.ParentHash, newh.Number.Uint64()-1)
if newh == nil {
Expand Down
2 changes: 1 addition & 1 deletion eth/gasprice/lightprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (self *LightPriceOracle) SuggestPrice(ctx context.Context) (*big.Int, error
return lastPrice, nil
}

blockNum := head.GetNumberU64()
blockNum := head.Number.Uint64()
chn := make(chan lpResult, LpoMaxBlocks)
sent := 0
exp := 0
Expand Down
4 changes: 2 additions & 2 deletions les/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (f *lightFetcher) gotHeader(header *types.Header) {
if peerList == nil {
return
}
number := header.GetNumberU64()
number := header.Number.Uint64()
td := core.GetTd(f.pm.chainDb, hash, number)
for _, peer := range peerList {
peer.lock.Lock()
Expand Down Expand Up @@ -201,7 +201,7 @@ func (f *lightFetcher) processResponse(req fetchRequest, resp fetchResponse) boo
return false
}
for _, header := range headers {
td := core.GetTd(f.pm.chainDb, header.Hash(), header.GetNumberU64())
td := core.GetTd(f.pm.chainDb, header.Hash(), header.Number.Uint64())
if td == nil {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion les/odr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) {
lpm.synchronise(lpeer)

test := func(expFail uint64) {
for i := uint64(0); i <= pm.blockchain.CurrentHeader().GetNumberU64(); i++ {
for i := uint64(0); i <= pm.blockchain.CurrentHeader().Number.Uint64(); i++ {
bhash := core.GetCanonicalHash(db, i)
b1 := fn(light.NoOdr, db, pm.chainConfig, pm.blockchain.(*core.BlockChain), nil, bhash)
ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
Expand Down
4 changes: 2 additions & 2 deletions les/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestCodeAccessLes1(t *testing.T) { testAccess(t, 1, tfCodeAccess) }

func tfCodeAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
header := core.GetHeader(db, bhash, core.GetBlockNumber(db, bhash))
if header.GetNumberU64() < testContractDeployed {
if header.Number.Uint64() < testContractDeployed {
return nil
}
sti := light.StateTrieID(header)
Expand All @@ -66,7 +66,7 @@ func testAccess(t *testing.T, protocol int, fn accessTestFn) {
lpm.synchronise(lpeer)

test := func(expFail uint64) {
for i := uint64(0); i <= pm.blockchain.CurrentHeader().GetNumberU64(); i++ {
for i := uint64(0); i <= pm.blockchain.CurrentHeader().Number.Uint64(); i++ {
bhash := core.GetCanonicalHash(db, i)
if req := fn(ldb, bhash, i); req != nil {
ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
Expand Down
4 changes: 2 additions & 2 deletions les/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,12 @@ func (pm *ProtocolManager) blockLoop() {
if len(peers) > 0 {
header := ev.Data.(core.ChainHeadEvent).Block.Header()
hash := header.Hash()
number := header.GetNumberU64()
number := header.Number.Uint64()
td := core.GetTd(pm.chainDb, hash, number)
if td != nil && td.Cmp(lastBroadcastTd) > 0 {
var reorg uint64
if lastHead != nil {
reorg = lastHead.GetNumberU64() - core.FindCommonAncestor(pm.chainDb, header, lastHead).GetNumberU64()
reorg = lastHead.Number.Uint64() - core.FindCommonAncestor(pm.chainDb, header, lastHead).Number.Uint64()
}
lastHead = header
lastBroadcastTd = td
Expand Down
2 changes: 1 addition & 1 deletion light/lightchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func testFork(t *testing.T, LightChain *LightChain, i, n int, comparator func(td
}

func printChain(bc *LightChain) {
for i := bc.CurrentHeader().GetNumberU64(); i > 0; i-- {
for i := bc.CurrentHeader().Number.Uint64(); i > 0; i-- {
b := bc.GetHeaderByNumber(uint64(i))
fmt.Printf("\t%x %v\n", b.Hash(), b.Difficulty)
}
Expand Down
2 changes: 1 addition & 1 deletion light/odr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func testChainOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) {
}

test := func(expFail uint64) {
for i := uint64(0); i <= blockchain.CurrentHeader().GetNumberU64(); i++ {
for i := uint64(0); i <= blockchain.CurrentHeader().Number.Uint64(); i++ {
bhash := core.GetCanonicalHash(sdb, i)
b1 := fn(NoOdr, sdb, blockchain, nil, bhash)
ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
Expand Down
14 changes: 7 additions & 7 deletions light/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewTxPool(config *core.ChainConfig, eventMux *event.TypeMux, chain *LightCh
odr: chain.Odr(),
chainDb: chain.Odr().Database(),
head: chain.CurrentHeader().Hash(),
clearIdx: chain.CurrentHeader().GetNumberU64(),
clearIdx: chain.CurrentHeader().Number.Uint64(),
}
go pool.eventLoop()

Expand Down Expand Up @@ -241,11 +241,11 @@ func (pool *TxPool) setNewHead(ctx context.Context, newHeader *types.Header) (tx
// find common ancestor, create list of rolled back and new block hashes
var oldHashes, newHashes []common.Hash
for oldh.Hash() != newh.Hash() {
if oldh.GetNumberU64() >= newh.GetNumberU64() {
if oldh.Number.Uint64() >= newh.Number.Uint64() {
oldHashes = append(oldHashes, oldh.Hash())
oldh = pool.chain.GetHeader(oldh.ParentHash, oldh.Number.Uint64()-1)
}
if oldh.GetNumberU64() < newh.GetNumberU64() {
if oldh.Number.Uint64() < newh.Number.Uint64() {
newHashes = append(newHashes, newh.Hash())
newh = pool.chain.GetHeader(newh.ParentHash, newh.Number.Uint64()-1)
if newh == nil {
Expand All @@ -254,8 +254,8 @@ func (pool *TxPool) setNewHead(ctx context.Context, newHeader *types.Header) (tx
}
}
}
if oldh.GetNumberU64() < pool.clearIdx {
pool.clearIdx = oldh.GetNumberU64()
if oldh.Number.Uint64() < pool.clearIdx {
pool.clearIdx = oldh.Number.Uint64()
}
// roll back old blocks
for _, hash := range oldHashes {
Expand All @@ -265,14 +265,14 @@ func (pool *TxPool) setNewHead(ctx context.Context, newHeader *types.Header) (tx
// check mined txs of new blocks (array is in reversed order)
for i := len(newHashes) - 1; i >= 0; i-- {
hash := newHashes[i]
if err := pool.checkMinedTxs(ctx, hash, newHeader.GetNumberU64()-uint64(i), txc); err != nil {
if err := pool.checkMinedTxs(ctx, hash, newHeader.Number.Uint64()-uint64(i), txc); err != nil {
return txc, err
}
pool.head = hash
}

// clear old mined tx entries of old blocks
if idx := newHeader.GetNumberU64(); idx > pool.clearIdx+txPermanent {
if idx := newHeader.Number.Uint64(); idx > pool.clearIdx+txPermanent {
idx2 := idx - txPermanent
for i := pool.clearIdx; i < idx2; i++ {
hash := core.GetCanonicalHash(pool.chainDb, i)
Expand Down
2 changes: 1 addition & 1 deletion light/vm_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (self *VMEnv) Depth() int { return self.depth }
func (self *VMEnv) SetDepth(i int) { self.depth = i }
func (self *VMEnv) GetHash(n uint64) common.Hash {
for header := self.chain.GetHeader(self.header.ParentHash, self.header.Number.Uint64()-1); header != nil; header = self.chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) {
if header.GetNumberU64() == n {
if header.Number.Uint64() == n {
return header.Hash()
}
}
Expand Down

0 comments on commit be38652

Please sign in to comment.