Skip to content

Commit

Permalink
Merge pull request erigontech#7 from cffls/feat/zero
Browse files Browse the repository at this point in the history
Add 'witness' flag to zeroTracer config
  • Loading branch information
cffls authored May 28, 2024
2 parents ebdb238 + 2002fde commit d96fa3a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
90 changes: 51 additions & 39 deletions turbo/jsonrpc/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (api *PrivateDebugAPIImpl) TraceBlockByHash(ctx context.Context, hash commo
return api.traceBlock(ctx, rpc.BlockNumberOrHashWithHash(hash, true), config, stream)
}

type ZeroTracerConfig struct {
Witness bool `json:"witness"` // If true, this tracer will return block witness
}

func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, config *tracers.TraceConfig, stream *jsoniter.Stream) error {
tx, err := api.db.BeginRo(ctx)
if err != nil {
Expand Down Expand Up @@ -127,11 +131,9 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp

stream.WriteObjectStart()

if config.Tracer != nil && *config.Tracer != "zeroTracer" {
stream.WriteObjectField("txHash")
stream.WriteString(txnHash.Hex())
stream.WriteMore()
}
stream.WriteObjectField("txHash")
stream.WriteString(txnHash.Hex())
stream.WriteMore()

stream.WriteObjectField("result")
select {
Expand Down Expand Up @@ -200,53 +202,63 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
}

if config.Tracer != nil && *config.Tracer == "zeroTracer" {
if len(txns) != 0 {
stream.WriteMore()
var zeroConfig ZeroTracerConfig
if config.TracerConfig != nil {
if err := json.Unmarshal(*config.TracerConfig, &zeroConfig); err != nil {
return err
}
}
stream.WriteObjectStart()
stream.WriteObjectField("block_witness")

k := make([]byte, 8)
if zeroConfig.Witness {

binary.LittleEndian.PutUint64(k[:], block.NumberU64())
if len(txns) != 0 {
stream.WriteMore()
}
stream.WriteObjectStart()
stream.WriteObjectField("block_witness")

var witness_bytes []byte
k := make([]byte, 8)

// Try read from DB
// if block.NumberU64() > 0 {
// witness_bytes, err = stateless.ReadChunks(tx, kv.Witnesses, k)
// }
binary.LittleEndian.PutUint64(k[:], block.NumberU64())

// If not found, compute witness directly
if len(witness_bytes) == 0 || err != nil {
witness_bytes, err = api.getWitness(ctx, api.db, blockNrOrHash, 0, true, 100000, log.Root())
}
var witness_bytes []byte

if err != nil {
log.Warn("error while getting witness", "err", err)
stream.WriteNil()
return err
}
// Try read from DB
// if block.NumberU64() > 0 {
// witness_bytes, err = stateless.ReadChunks(tx, kv.Witnesses, k)
// }

preImage := types.TriePreImage{
Combined: types.CombinedPreImages{
Compact: hexutility.Bytes(witness_bytes),
},
}
// If not found, compute witness directly
if len(witness_bytes) == 0 || err != nil {
witness_bytes, err = api.getWitness(ctx, api.db, blockNrOrHash, 0, true, 100000, log.Root())
}

preImageHex, err := json.Marshal(preImage)
if err != nil {
log.Warn("error while getting witness", "err", err)
stream.WriteNil()
return err
}

if err != nil {
log.Warn("error while marshalling preImage", "err", err)
stream.WriteNil()
return err
}
preImage := types.TriePreImage{
Combined: types.CombinedPreImages{
Compact: hexutility.Bytes(witness_bytes),
},
}

stream.Write(json.RawMessage(preImageHex))
preImageHex, err := json.Marshal(preImage)

stream.WriteObjectEnd()
if err != nil {
log.Warn("error while marshalling preImage", "err", err)
stream.WriteNil()
return err
}

stream.Flush()
stream.Write(json.RawMessage(preImageHex))

stream.WriteObjectEnd()

stream.Flush()
}
}

stream.WriteArrayEnd()
Expand Down
2 changes: 1 addition & 1 deletion turbo/trie/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type WitnessStorage interface {
// WitnessVersion represents the current version of the block witness
// in case of incompatible changes it should be updated and the code to migrate the
// old witness format should be present
const WitnessVersion = uint8(1)
const WitnessVersion = uint8(0)

// WitnessHeader contains version information and maybe some future format bits
// the version is always the 1st bit.
Expand Down

0 comments on commit d96fa3a

Please sign in to comment.