Skip to content

Commit 760fd65

Browse files
zsfelfoldifjl
authored andcommitted
light: light chain, VM env and tx pool
1 parent 8b1df1a commit 760fd65

15 files changed

Lines changed: 2642 additions & 124 deletions

‎core/blockchain.go‎

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,37 @@ func (self *BlockChain) Rollback(chain []common.Hash) {
632632
}
633633
}
634634

635+
// SetReceiptsData computes all the non-consensus fields of the receipts
636+
func SetReceiptsData(block *types.Block, receipts types.Receipts) {
637+
transactions, logIndex := block.Transactions(), uint(0)
638+
639+
for j := 0; j < len(receipts); j++ {
640+
// The transaction hash can be retrieved from the transaction itself
641+
receipts[j].TxHash = transactions[j].Hash()
642+
643+
// The contract address can be derived from the transaction itself
644+
if MessageCreatesContract(transactions[j]) {
645+
from, _ := transactions[j].From()
646+
receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce())
647+
}
648+
// The used gas can be calculated based on previous receipts
649+
if j == 0 {
650+
receipts[j].GasUsed = new(big.Int).Set(receipts[j].CumulativeGasUsed)
651+
} else {
652+
receipts[j].GasUsed = new(big.Int).Sub(receipts[j].CumulativeGasUsed, receipts[j-1].CumulativeGasUsed)
653+
}
654+
// The derived log fields can simply be set from the block and transaction
655+
for k := 0; k < len(receipts[j].Logs); k++ {
656+
receipts[j].Logs[k].BlockNumber = block.NumberU64()
657+
receipts[j].Logs[k].BlockHash = block.Hash()
658+
receipts[j].Logs[k].TxHash = receipts[j].TxHash
659+
receipts[j].Logs[k].TxIndex = uint(j)
660+
receipts[j].Logs[k].Index = logIndex
661+
logIndex++
662+
}
663+
}
664+
}
665+
635666
// InsertReceiptChain attempts to complete an already existing header chain with
636667
// transaction and receipt data.
637668
func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) {
@@ -673,32 +704,7 @@ func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain
673704
continue
674705
}
675706
// Compute all the non-consensus fields of the receipts
676-
transactions, logIndex := block.Transactions(), uint(0)
677-
for j := 0; j < len(receipts); j++ {
678-
// The transaction hash can be retrieved from the transaction itself
679-
receipts[j].TxHash = transactions[j].Hash()
680-
681-
// The contract address can be derived from the transaction itself
682-
if MessageCreatesContract(transactions[j]) {
683-
from, _ := transactions[j].From()
684-
receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce())
685-
}
686-
// The used gas can be calculated based on previous receipts
687-
if j == 0 {
688-
receipts[j].GasUsed = new(big.Int).Set(receipts[j].CumulativeGasUsed)
689-
} else {
690-
receipts[j].GasUsed = new(big.Int).Sub(receipts[j].CumulativeGasUsed, receipts[j-1].CumulativeGasUsed)
691-
}
692-
// The derived log fields can simply be set from the block and transaction
693-
for k := 0; k < len(receipts[j].Logs); k++ {
694-
receipts[j].Logs[k].BlockNumber = block.NumberU64()
695-
receipts[j].Logs[k].BlockHash = block.Hash()
696-
receipts[j].Logs[k].TxHash = receipts[j].TxHash
697-
receipts[j].Logs[k].TxIndex = uint(j)
698-
receipts[j].Logs[k].Index = logIndex
699-
logIndex++
700-
}
701-
}
707+
SetReceiptsData(block, receipts)
702708
// Write all the data out into the database
703709
if err := WriteBody(self.chainDb, block.Hash(), block.NumberU64(), block.Body()); err != nil {
704710
errs[index] = fmt.Errorf("failed to write block body: %v", err)

‎core/database_util.go‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,13 @@ func WriteBody(db ethdb.Database, hash common.Hash, number uint64, body *types.B
347347
if err != nil {
348348
return err
349349
}
350+
return WriteBodyRLP(db, hash, number, data)
351+
}
352+
353+
// WriteBodyRLP writes a serialized body of a block into the database.
354+
func WriteBodyRLP(db ethdb.Database, hash common.Hash, number uint64, rlp rlp.RawValue) error {
350355
key := append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
351-
if err := db.Put(key, data); err != nil {
356+
if err := db.Put(key, rlp); err != nil {
352357
glog.Fatalf("failed to store block body into database: %v", err)
353358
}
354359
glog.V(logger.Debug).Infof("stored block body [%x…]", hash.Bytes()[:4])
@@ -446,6 +451,16 @@ func WriteTransactions(db ethdb.Database, block *types.Block) error {
446451
return nil
447452
}
448453

454+
// WriteReceipt stores a single transaction receipt into the database.
455+
func WriteReceipt(db ethdb.Database, receipt *types.Receipt) error {
456+
storageReceipt := (*types.ReceiptForStorage)(receipt)
457+
data, err := rlp.EncodeToBytes(storageReceipt)
458+
if err != nil {
459+
return err
460+
}
461+
return db.Put(append(receiptsPrefix, receipt.TxHash.Bytes()...), data)
462+
}
463+
449464
// WriteReceipts stores a batch of transaction receipts into the database.
450465
func WriteReceipts(db ethdb.Database, receipts types.Receipts) error {
451466
batch := db.NewBatch()
@@ -614,3 +629,30 @@ func GetChainConfig(db ethdb.Database, hash common.Hash) (*ChainConfig, error) {
614629

615630
return &config, nil
616631
}
632+
633+
// FindCommonAncestor returns the last common ancestor of two block headers
634+
func FindCommonAncestor(db ethdb.Database, a, b *types.Header) *types.Header {
635+
for a.GetNumberU64() > b.GetNumberU64() {
636+
a = GetHeader(db, a.ParentHash, a.GetNumberU64()-1)
637+
if a == nil {
638+
return nil
639+
}
640+
}
641+
for a.GetNumberU64() < b.GetNumberU64() {
642+
b = GetHeader(db, b.ParentHash, b.GetNumberU64()-1)
643+
if b == nil {
644+
return nil
645+
}
646+
}
647+
for a.Hash() != b.Hash() {
648+
a = GetHeader(db, a.ParentHash, a.GetNumberU64()-1)
649+
if a == nil {
650+
return nil
651+
}
652+
b = GetHeader(db, b.ParentHash, b.GetNumberU64()-1)
653+
if b == nil {
654+
return nil
655+
}
656+
}
657+
return a
658+
}

‎core/types/block.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ type jsonHeader struct {
116116
Nonce *BlockNonce `json:"nonce"`
117117
}
118118

119+
func (h *Header) GetNumber() *big.Int { return new(big.Int).Set(h.Number) }
120+
func (h *Header) GetGasLimit() *big.Int { return new(big.Int).Set(h.GasLimit) }
121+
func (h *Header) GetGasUsed() *big.Int { return new(big.Int).Set(h.GasUsed) }
122+
func (h *Header) GetDifficulty() *big.Int { return new(big.Int).Set(h.Difficulty) }
123+
func (h *Header) GetTime() *big.Int { return new(big.Int).Set(h.Time) }
124+
func (h *Header) GetNumberU64() uint64 { return h.Number.Uint64() }
125+
func (h *Header) GetNonce() uint64 { return binary.BigEndian.Uint64(h.Nonce[:]) }
126+
func (h *Header) GetExtra() []byte { return common.CopyBytes(h.Extra) }
127+
119128
// Hash returns the block hash of the header, which is simply the keccak256 hash of its
120129
// RLP encoding.
121130
func (h *Header) Hash() common.Hash {

0 commit comments

Comments
 (0)