|
| 1 | +package stateless |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/ethereum/go-ethereum/common" |
| 7 | + "github.com/ethereum/go-ethereum/consensus/beacon" |
| 8 | + "github.com/ethereum/go-ethereum/consensus/ethash" |
| 9 | + "github.com/ethereum/go-ethereum/core" |
| 10 | + "github.com/ethereum/go-ethereum/core/rawdb" |
| 11 | + "github.com/ethereum/go-ethereum/core/state" |
| 12 | + "github.com/ethereum/go-ethereum/core/vm" |
| 13 | + "github.com/ethereum/go-ethereum/crypto" |
| 14 | + "github.com/ethereum/go-ethereum/params" |
| 15 | + "github.com/ethereum/go-ethereum/triedb" |
| 16 | +) |
| 17 | + |
| 18 | +// StatelessExecute executes the block contained in the Witness returning the post state root or an error |
| 19 | +func StatelessExecute(chainCfg *params.ChainConfig, witness *state.Witness) (root common.Hash, err error) { |
| 20 | + rawDb := rawdb.NewMemoryDatabase() |
| 21 | + if err := witness.PopulateDB(rawDb); err != nil { |
| 22 | + return common.Hash{}, err |
| 23 | + } |
| 24 | + blob := rawdb.ReadAccountTrieNode(rawDb, nil) |
| 25 | + prestateRoot := crypto.Keccak256Hash(blob) |
| 26 | + |
| 27 | + db, err := state.New(prestateRoot, state.NewDatabaseWithConfig(rawDb, triedb.PathDefaults), nil) |
| 28 | + if err != nil { |
| 29 | + return common.Hash{}, err |
| 30 | + } |
| 31 | + engine := beacon.New(ethash.NewFaker()) |
| 32 | + validator := core.NewBlockValidator(chainCfg, nil, engine) |
| 33 | + processor := core.NewStateProcessor(chainCfg, nil, engine) |
| 34 | + |
| 35 | + receipts, _, usedGas, err := processor.Process(witness.Block, db, vm.Config{}, witness) |
| 36 | + if err != nil { |
| 37 | + return common.Hash{}, err |
| 38 | + } |
| 39 | + |
| 40 | + // compute the state root. |
| 41 | + if root, err = validator.ValidateState(witness.Block, db, receipts, usedGas, false); err != nil { |
| 42 | + return common.Hash{}, err |
| 43 | + } |
| 44 | + return root, nil |
| 45 | +} |
| 46 | + |
| 47 | +// BuildStatelessProof executes a block, collecting the accessed pre-state into |
| 48 | +// a Witness. The RLP-encoded witness is returned. |
| 49 | +func BuildStatelessProof(blockHash common.Hash, bc *core.BlockChain) ([]byte, error) { |
| 50 | + block := bc.GetBlockByHash(blockHash) |
| 51 | + if block == nil { |
| 52 | + return nil, fmt.Errorf("non-existent block %x", blockHash) |
| 53 | + } else if block.NumberU64() == 0 { |
| 54 | + return nil, fmt.Errorf("cannot build a stateless proof of the genesis block") |
| 55 | + } |
| 56 | + parentHash := block.ParentHash() |
| 57 | + parent := bc.GetBlockByHash(parentHash) |
| 58 | + if parent == nil { |
| 59 | + return nil, fmt.Errorf("block %x parent not present", parentHash) |
| 60 | + } |
| 61 | + |
| 62 | + db, err := bc.StateAt(parent.Header().Root) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + db.EnableWitnessBuilding() |
| 67 | + if bc.Snapshots() != nil { |
| 68 | + db.StartPrefetcher("BuildStatelessProof", false) |
| 69 | + defer db.StopPrefetcher() |
| 70 | + } |
| 71 | + stateProcessor := core.NewStateProcessor(bc.Config(), bc, bc.Engine()) |
| 72 | + _, _, _, err = stateProcessor.Process(block, db, vm.Config{}, nil) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + if _, err = db.Commit(block.NumberU64(), true); err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + proof := db.Witness() |
| 80 | + proof.Block = block |
| 81 | + enc, err := proof.EncodeRLP() |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + return enc, nil |
| 86 | +} |
0 commit comments