Skip to content

Commit 2c896ba

Browse files
gballetjwasinger
andauthored
Various fixes for rust-verkle proof format compatibility (ethereum#67)
* code to extract the block * separate proof from keys in block * display state root of block 0 * change file name to reflect the correct block number * use RLP instead of flat binary for keyvals in block * update go-verkle to fix build * fix rebase issues * make test pass * fix issue in map copy Co-authored-by: Jared Wasinger <j-wasinger@hotmail.com>
1 parent 469945b commit 2c896ba

File tree

8 files changed

+51
-33
lines changed

8 files changed

+51
-33
lines changed

core/chain_makers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
348348
}
349349
}
350350
vtr.Hash()
351-
p, err := vtr.ProveAndSerialize(keys, statedb.Witness().KeyVals())
352-
block.SetVerkleProof(p)
351+
p, k, err := vtr.ProveAndSerialize(keys, statedb.Witness().KeyVals())
352+
block.SetVerkleProof(p, k)
353353
if err != nil {
354354
panic(err)
355355
}

core/state_processor_test.go

+26-16
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package core
1818

1919
import (
20+
"bytes"
2021
"crypto/ecdsa"
22+
"fmt"
2123
"math/big"
24+
"os"
2225
"testing"
2326

2427
"github.com/ethereum/go-ethereum/common"
@@ -31,6 +34,7 @@ import (
3134
"github.com/ethereum/go-ethereum/core/vm"
3235
"github.com/ethereum/go-ethereum/crypto"
3336
"github.com/ethereum/go-ethereum/params"
37+
"github.com/ethereum/go-ethereum/rlp"
3438
"github.com/ethereum/go-ethereum/trie"
3539
"golang.org/x/crypto/sha3"
3640
)
@@ -377,32 +381,38 @@ func TestProcessStateless(t *testing.T) {
377381
genesis := gspec.MustCommit(db)
378382
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
379383
defer blockchain.Stop()
380-
var blockGasUsedExpected uint64
381-
chain, _ := GenerateVerkleChain(gspec.Config, genesis, ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {
384+
txCost1 := params.WitnessBranchWriteCost*2 + params.WitnessBranchReadCost*2 + params.WitnessChunkWriteCost*3 + params.WitnessChunkReadCost*12 + params.TxGas
385+
txCost2 := params.WitnessBranchWriteCost + params.WitnessBranchReadCost*2 + params.WitnessChunkWriteCost*2 + params.WitnessChunkReadCost*10 + params.TxGas
386+
blockGasUsedExpected := txCost1 * 2 + txCost2
387+
chain, _ := GenerateVerkleChain(gspec.Config, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
382388
// TODO need to check that the tx cost provided is the exact amount used (no remaining left-over)
383-
txCost := params.WitnessBranchWriteCost*2 + params.WitnessBranchReadCost*2 + params.WitnessChunkWriteCost*3 + params.WitnessChunkReadCost*12 + params.TxGas
384-
blockGasUsedExpected += txCost * 2
385-
tx, _ := types.SignTx(types.NewTransaction(uint64(i) * 3, common.Address{1, 2, 3}, big.NewInt(999), txCost, big.NewInt(875000000), nil), signer, testKey)
389+
tx, _ := types.SignTx(types.NewTransaction(uint64(i)*3, common.Address{1, 2, 3}, big.NewInt(999), txCost1, big.NewInt(875000000), nil), signer, testKey)
386390
gen.AddTx(tx)
387-
tx, _ = types.SignTx(types.NewTransaction(uint64(i) * 3 + 1, common.Address{}, big.NewInt(999), txCost, big.NewInt(875000000), nil), signer, testKey)
391+
tx, _ = types.SignTx(types.NewTransaction(uint64(i)*3+1, common.Address{}, big.NewInt(999), txCost1, big.NewInt(875000000), nil), signer, testKey)
388392
gen.AddTx(tx)
389-
txCost = params.WitnessBranchWriteCost + params.WitnessBranchReadCost*2 + params.WitnessChunkWriteCost*2 + params.WitnessChunkReadCost*10 + params.TxGas
390-
blockGasUsedExpected += txCost
391-
tx, _ = types.SignTx(types.NewTransaction(uint64(i) * 3 + 2, common.Address{}, big.NewInt(0), txCost, big.NewInt(875000000), nil), signer, testKey)
393+
tx, _ = types.SignTx(types.NewTransaction(uint64(i)*3+2, common.Address{}, big.NewInt(0), txCost2, big.NewInt(875000000), nil), signer, testKey)
392394
gen.AddTx(tx)
393395
})
394396

397+
f, _ := os.Create("block2.rlp")
398+
defer f.Close()
399+
var buf bytes.Buffer
400+
rlp.Encode(&buf, chain[1])
401+
f.Write(buf.Bytes())
402+
fmt.Printf("%x\n", chain[0].Root())
403+
395404
_, err := blockchain.InsertChain(chain)
396405
if err != nil {
397406
t.Fatalf("block imported with error: %v", err)
398407
}
399408

400-
b := blockchain.GetBlockByNumber(1)
401-
if b == nil {
402-
t.Fatalf("expected block 1 to be present in chain")
403-
}
404-
405-
if b.GasUsed() != blockGasUsedExpected {
406-
t.Fatalf("expected block txs to use %d, got %d\n", blockGasUsedExpected, b.GasUsed())
409+
for i := 0; i < 2; i++ {
410+
b := blockchain.GetBlockByNumber(uint64(i) + 1)
411+
if b == nil {
412+
t.Fatalf("expected block %d to be present in chain", i + 1)
413+
}
414+
if b.GasUsed() != blockGasUsedExpected {
415+
t.Fatalf("expected block txs to use %d, got %d\n", blockGasUsedExpected, b.GasUsed())
416+
}
407417
}
408418
}

core/types/access_witness.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func (aw *AccessWitness) SetLeafValue(addr []byte, value []byte) {
6969

7070
if chunk, exists := aw.Chunks[common.BytesToHash(addr)]; exists {
7171
chunk.value = value
72+
aw.Chunks[common.BytesToHash(addr)] = chunk
7273
} else {
7374
panic(fmt.Sprintf("address not in access witness: %x", addr))
7475
}
@@ -204,10 +205,10 @@ func (aw *AccessWitness) Keys() [][]byte {
204205
return keys
205206
}
206207

207-
func (aw *AccessWitness) KeyVals() map[common.Hash][]byte {
208-
result := make(map[common.Hash][]byte)
208+
func (aw *AccessWitness) KeyVals() map[string][]byte {
209+
result := make(map[string][]byte)
209210
for k, v := range aw.Chunks {
210-
result[k] = v.value
211+
result[string(k[:])] = v.value
211212
}
212213
return result
213214
}

core/types/block.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/ethereum/go-ethereum/common"
3030
"github.com/ethereum/go-ethereum/common/hexutil"
3131
"github.com/ethereum/go-ethereum/rlp"
32+
"github.com/gballet/go-verkle"
3233
)
3334

3435
var (
@@ -87,7 +88,8 @@ type Header struct {
8788
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
8889

8990
// The verkle proof is ignored in legacy headers
90-
VerkleProof []byte `json:"verkleProof" rlp:"optional"`
91+
VerkleProof []byte `json:"verkleProof" rlp:"optional"`
92+
VerkleKeyVals []verkle.KeyValuePair `json:"verkleKeyVals" rlp:"optional"`
9193

9294
/*
9395
TODO (MariusVanDerWijden) Add this field once needed
@@ -340,8 +342,9 @@ func (b *Block) SanityCheck() error {
340342
return b.header.SanityCheck()
341343
}
342344

343-
func (b *Block) SetVerkleProof(vp []byte) {
345+
func (b *Block) SetVerkleProof(vp []byte, kv []verkle.KeyValuePair) {
344346
b.header.VerkleProof = vp
347+
b.header.VerkleKeyVals = kv
345348
}
346349

347350
type writeCounter common.StorageSize

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/fatih/color v1.7.0
2727
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5
2828
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff
29-
github.com/gballet/go-verkle v0.0.0-20220121105610-351986d619a8
29+
github.com/gballet/go-verkle v0.0.0-20220128155149-95499dfcd74a
3030
github.com/go-ole/go-ole v1.2.1 // indirect
3131
github.com/go-stack/stack v1.8.0
3232
github.com/golang/protobuf v1.4.3

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ github.com/gballet/go-verkle v0.0.0-20220119205306-b466d7bff5ba h1:iOhT1XFktDNN7
163163
github.com/gballet/go-verkle v0.0.0-20220119205306-b466d7bff5ba/go.mod h1:e1m+0UuY3AFFTubxZZ3ePf4yO/rHMAeTb7udhUAJduk=
164164
github.com/gballet/go-verkle v0.0.0-20220121105610-351986d619a8 h1:6fW03c2tXGttQv1b3mt9zmcXw4/srV62DltEER5mp/U=
165165
github.com/gballet/go-verkle v0.0.0-20220121105610-351986d619a8/go.mod h1:e1m+0UuY3AFFTubxZZ3ePf4yO/rHMAeTb7udhUAJduk=
166+
github.com/gballet/go-verkle v0.0.0-20220127081734-1fca5295178c h1:EoP3VKIxU+29cA4coL9TJfD8lPuaUURxbW5nHoK1joM=
167+
github.com/gballet/go-verkle v0.0.0-20220127081734-1fca5295178c/go.mod h1:NhFaKv4U1IBG8QkKCsKh4xBMmdori0B0eZjdywcrktE=
168+
github.com/gballet/go-verkle v0.0.0-20220128155149-95499dfcd74a h1:0GpdT+zyEoa8OTBtJk5EdZm7nF4HXzU+blG3io0pm5A=
169+
github.com/gballet/go-verkle v0.0.0-20220128155149-95499dfcd74a/go.mod h1:NhFaKv4U1IBG8QkKCsKh4xBMmdori0B0eZjdywcrktE=
166170
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
167171
github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
168172
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=

miner/worker.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1042,11 +1042,11 @@ func (w *worker) commit(uncles []*types.Header, interval func(), update bool, st
10421042
if tr := s.GetTrie(); tr.IsVerkle() {
10431043
vtr := tr.(*trie.VerkleTrie)
10441044
// Generate the proof if we are using a verkle tree
1045-
p, err := vtr.ProveAndSerialize(s.Witness().Keys(), s.Witness().KeyVals())
1045+
p, k, err := vtr.ProveAndSerialize(s.Witness().Keys(), s.Witness().KeyVals())
10461046
if err != nil {
10471047
return err
10481048
}
1049-
block.SetVerkleProof(p)
1049+
block.SetVerkleProof(p, k)
10501050
}
10511051
if w.isRunning() && !w.merger.TDDReached() {
10521052
if interval != nil {

trie/verkle.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ func (trie *VerkleTrie) IsVerkle() bool {
174174
return true
175175
}
176176

177-
type KeyValuePair struct {
178-
Key []byte
179-
Value []byte
180-
}
177+
func (trie *VerkleTrie) ProveAndSerialize(keys [][]byte, kv map[string][]byte) ([]byte, []verkle.KeyValuePair, error) {
178+
proof, _, _, _ := verkle.MakeVerkleMultiProof(trie.root, keys, kv)
179+
p, kvps, err := verkle.SerializeProof(proof)
180+
if err != nil {
181+
return nil, nil, err
182+
}
181183

182-
func (trie *VerkleTrie) ProveAndSerialize(keys [][]byte, kv map[common.Hash][]byte) ([]byte, error) {
183-
proof, _, _, _ := verkle.MakeVerkleMultiProof(trie.root, keys)
184-
return verkle.SerializeProof(proof)
184+
return p, kvps, nil
185185
}
186186

187187
type set = map[string]struct{}

0 commit comments

Comments
 (0)