Skip to content

Commit 8e20673

Browse files
committed
review fixes/changes
1 parent f7e973d commit 8e20673

File tree

7 files changed

+28
-51
lines changed

7 files changed

+28
-51
lines changed

core/blockchain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
13371337
break
13381338
}
13391339
if bc.cacheConfig.ProcessingStateDiffs {
1340-
if !bc.allowedRootToBeDereferenced(root.(common.Hash)) {
1340+
if !bc.rootAllowedToBeDereferenced(root.(common.Hash)) {
13411341
bc.triegc.Push(root, number)
13421342
break
13431343
} else {
@@ -1406,7 +1406,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
14061406
// since we need the state tries of the current block and its parent in-memory
14071407
// in order to process statediffs, we should avoid dereferencing roots until
14081408
// its statediff and its child have been processed
1409-
func (bc *BlockChain) allowedRootToBeDereferenced(root common.Hash) bool {
1409+
func (bc *BlockChain) rootAllowedToBeDereferenced(root common.Hash) bool {
14101410
diffProcessedForSelfAndChildCount := 2
14111411
count := bc.stateDiffsProcessed[root]
14121412
return count >= diffProcessedForSelfAndChildCount

eth/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ type Config struct {
151151
// RPCGasCap is the global gas cap for eth-call variants.
152152
RPCGasCap *big.Int `toml:",omitempty"`
153153

154-
StateDiff bool
155-
156154
// Checkpoint is a hardcoded checkpoint which can be nil.
157155
Checkpoint *params.TrustedCheckpoint `toml:",omitempty"`
158156

statediff/builder.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"math/big"
2626

2727
"github.com/ethereum/go-ethereum/common"
28-
"github.com/ethereum/go-ethereum/common/hexutil"
2928
"github.com/ethereum/go-ethereum/core"
3029
"github.com/ethereum/go-ethereum/core/state"
3130
"github.com/ethereum/go-ethereum/crypto"
@@ -318,15 +317,3 @@ func (sdb *builder) buildStorageDiffsFromTrie(it trie.NodeIterator) ([]StorageDi
318317

319318
return storageDiffs, nil
320319
}
321-
322-
func (sdb *builder) addressByPath(path []byte) (*common.Address, error) {
323-
log.Debug("Looking up address from path", "path", hexutil.Encode(append([]byte("secure-key-"), path...)))
324-
addrBytes, err := sdb.chainDB.Get(append([]byte("secure-key-"), hexToKeyBytes(path)...))
325-
if err != nil {
326-
log.Error("Error looking up address via path", "path", hexutil.Encode(append([]byte("secure-key-"), path...)), "error", err)
327-
return nil, err
328-
}
329-
addr := common.BytesToAddress(addrBytes)
330-
log.Debug("Address found", "Address", addr)
331-
return &addr, nil
332-
}

statediff/doc.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,28 @@
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/*
18-
This work is adapted from work by Charles Crain at https://github.com/jpmorganchase/quorum/blob/9b7fd9af8082795eeeb6863d9746f12b82dd5078/statediff/statediff.go
19-
2018
Package statediff provides an auxiliary service that processes state diff objects from incoming chain events,
2119
relaying the objects to any rpc subscriptions.
2220
21+
This work is adapted from work by Charles Crain at https://github.com/jpmorganchase/quorum/blob/9b7fd9af8082795eeeb6863d9746f12b82dd5078/statediff/statediff.go
22+
2323
The service is spun up using the below CLI flags
2424
--statediff: boolean flag, turns on the service
2525
--statediff.streamblock: boolean flag, configures the service to associate and stream out the rest of the block data with the state diffs.
2626
--statediff.intermediatenodes: boolean flag, tells service to include intermediate (branch and extension) nodes; default (false) processes leaf nodes only.
2727
--statediff.pathsandproofs: boolean flag, tells service to generate paths and proofs for the diffed storage and state trie leaf nodes.
2828
--statediff.watchedaddresses: string slice flag, used to limit the state diffing process to the given addresses. Usage: --statediff.watchedaddresses=addr1 --statediff.watchedaddresses=addr2 --statediff.watchedaddresses=addr3
2929
30-
If you wish to use the websocket endpoint to subscribe to the statediff service, be sure to open up the Websocket RPC server with the `--ws` flag.
30+
If you wish to use the websocket endpoint to subscribe to the statediff service, be sure to open up the Websocket RPC server with the `--ws` flag. The IPC-RPC server is turned on by default.
31+
32+
The statediffing services works only with `--syncmode="full", but -importantly- does not require garbage collection to be turned off (does not require an archival node).
33+
34+
e.g.
35+
36+
$ ./geth --statediff --statediff.streamblock --ws --syncmode "full"
37+
38+
This starts up the geth node in full sync mode, starts up the statediffing service, and opens up the websocket endpoint to subscribe to the service.
39+
Because the "streamblock" flag has been turned on, the service will strean out block data (headers, transactions, and receipts) along with the diffed state and storage leafs.
3140
3241
Rpc subscriptions to the service can be created using the rpc.Client.Subscribe() method,
3342
with the "statediff" namespace, a statediff.Payload channel, and the name of the statediff api's rpc method- "stream".
@@ -41,7 +50,7 @@ for {
4150
select {
4251
case stateDiffPayload := <- stateDiffPayloadChan:
4352
processPayload(stateDiffPayload)
44-
case err := <= rpcSub.Err():
53+
case err := <- rpcSub.Err():
4554
log.Error(err)
4655
}
4756
}

statediff/helpers.go

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func sortKeys(data AccountsMap) []string {
3737
return keys
3838
}
3939

40-
// BytesToNiblePath
40+
// bytesToNiblePath converts the byte representation of a path to its string representation
4141
func bytesToNiblePath(path []byte) string {
4242
if hasTerm(path) {
4343
path = path[:len(path)-1]
@@ -53,6 +53,8 @@ func bytesToNiblePath(path []byte) string {
5353
return nibblePath
5454
}
5555

56+
// findIntersection finds the set of strings from both arrays that are equivalent (same key as same index)
57+
// this is used to find which keys have been both "deleted" and "created" i.e. they were "updated".
5658
func findIntersection(a, b []string) []string {
5759
lenA := len(a)
5860
lenB := len(b)
@@ -63,21 +65,21 @@ func findIntersection(a, b []string) []string {
6365
}
6466
for {
6567
switch strings.Compare(a[iOfA], b[iOfB]) {
66-
// a[iOfA] < b[iOfB]
68+
// -1 when a[iOfA] < b[iOfB]
6769
case -1:
6870
iOfA++
6971
if iOfA >= lenA {
7072
return updates
7173
}
72-
// a[iOfA] == b[iOfB]
74+
// 0 when a[iOfA] == b[iOfB]
7375
case 0:
7476
updates = append(updates, a[iOfA])
7577
iOfA++
7678
iOfB++
7779
if iOfA >= lenA || iOfB >= lenB {
7880
return updates
7981
}
80-
// a[iOfA] > b[iOfB]
82+
// 1 when a[iOfA] > b[iOfB]
8183
case 1:
8284
iOfB++
8385
if iOfB >= lenB {
@@ -88,30 +90,11 @@ func findIntersection(a, b []string) []string {
8890

8991
}
9092

93+
// pathToStr converts the NodeIterator path to a string representation
9194
func pathToStr(it trie.NodeIterator) string {
9295
return bytesToNiblePath(it.Path())
9396
}
9497

95-
// Duplicated from trie/encoding.go
96-
func hexToKeyBytes(hex []byte) []byte {
97-
if hasTerm(hex) {
98-
hex = hex[:len(hex)-1]
99-
}
100-
if len(hex)&1 != 0 {
101-
panic("can't convert hex key of odd length")
102-
}
103-
key := make([]byte, (len(hex)+1)/2)
104-
decodeNibbles(hex, key)
105-
106-
return key
107-
}
108-
109-
func decodeNibbles(nibbles []byte, bytes []byte) {
110-
for bi, ni := 0, 0; ni < len(nibbles); bi, ni = bi+1, ni+2 {
111-
bytes[bi] = nibbles[ni]<<4 | nibbles[ni+1]
112-
}
113-
}
114-
11598
// hasTerm returns whether a hex key has the terminator flag.
11699
func hasTerm(s []byte) bool {
117100
return len(s) > 0 && s[len(s)-1] == 16

statediff/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (sds *Service) processStateDiff(currentBlock, parentBlock *types.Block) err
176176
payload.ReceiptsRlp = receiptBuff.Bytes()
177177
}
178178

179-
// If we have any websocket subscriptions listening in, send the data to them
179+
// If we have any rpc subscriptions listening in, send the data to them
180180
sds.send(payload)
181181
return nil
182182
}

statediff/service_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,27 @@ func testErrorInChainEventLoop(t *testing.T) {
131131
for i, payload := range payloads {
132132
if !bytes.Equal(payload.ReceiptsRlp, expectedReceiptsRlp[i]) {
133133
t.Error("Test failure:", t.Name())
134-
t.Logf("Actual reeipt rlp for payload %d does not equal expected.\nactual: %+v\nexpected: %+v", i, payload.ReceiptsRlp, expectedReceiptsRlp[i])
134+
t.Logf("Actual receipt rlp for payload %d does not equal expected.\nactual: %+v\nexpected: %+v", i, payload.ReceiptsRlp, expectedReceiptsRlp[i])
135135
}
136136
}
137137

138138
if !reflect.DeepEqual(builder.BlockHash, testBlock2.Hash()) {
139139
t.Error("Test failure:", t.Name())
140-
t.Logf("Actual does not equal expected.\nactual:%+v\nexpected: %+v", builder.BlockHash, testBlock2.Hash())
140+
t.Logf("Actual blockhash does not equal expected.\nactual:%+v\nexpected: %+v", builder.BlockHash, testBlock2.Hash())
141141
}
142142
if !bytes.Equal(builder.OldStateRoot.Bytes(), parentBlock2.Root().Bytes()) {
143143
t.Error("Test failure:", t.Name())
144-
t.Logf("Actual does not equal expected.\nactual:%+v\nexpected: %+v", builder.OldStateRoot, parentBlock2.Root())
144+
t.Logf("Actual root does not equal expected.\nactual:%+v\nexpected: %+v", builder.OldStateRoot, parentBlock2.Root())
145145
}
146146
if !bytes.Equal(builder.NewStateRoot.Bytes(), testBlock2.Root().Bytes()) {
147147
t.Error("Test failure:", t.Name())
148-
t.Logf("Actual does not equal expected.\nactual:%+v\nexpected: %+v", builder.NewStateRoot, testBlock2.Root())
148+
t.Logf("Actual root does not equal expected.\nactual:%+v\nexpected: %+v", builder.NewStateRoot, testBlock2.Root())
149149
}
150150
//look up the parent block from its hash
151151
expectedHashes := []common.Hash{testBlock1.ParentHash(), testBlock2.ParentHash()}
152152
if !reflect.DeepEqual(blockChain.ParentHashesLookedUp, expectedHashes) {
153153
t.Error("Test failure:", t.Name())
154-
t.Logf("Actual does not equal expected.\nactual:%+v\nexpected: %+v", blockChain.ParentHashesLookedUp, expectedHashes)
154+
t.Logf("Actual parent hash does not equal expected.\nactual:%+v\nexpected: %+v", blockChain.ParentHashesLookedUp, expectedHashes)
155155
}
156156
}
157157

0 commit comments

Comments
 (0)