Skip to content

Commit

Permalink
Add test to check compatibility between witness and zero trace
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Apr 4, 2024
1 parent b62bf09 commit 7740dc1
Show file tree
Hide file tree
Showing 5 changed files with 7,291 additions and 7 deletions.
73 changes: 73 additions & 0 deletions core/state/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -1339,6 +1340,78 @@ func TestGetStateFromWitness(t *testing.T) {
fmt.Printf("Balance: %s\n", balance)
}

func TestValidateZeroTracer(t *testing.T) {
// Load a block trace from a file
d, err := os.ReadFile("testdata/blockZeroTrace.json")
if err != nil {
t.Fatalf("error reading trace: %v", err)
}

// Decode data into json
var trace types.BlockTrace
err = json.Unmarshal(d, &trace)
if err != nil {
t.Fatalf("error decoding trace: %v", err)
}

witness, err := trie.NewWitnessFromReader(bytes.NewReader(trace.TriePreImage.Combined.Compact), false)

if err != nil {
t.Fatalf("error reading witness: %v", err)
}

s, err := state.NewStateless(libcommon.Hash{}, witness, 0, false, true)

if err != nil {
t.Fatalf("error creating state: %v", err)
}

s.SetStrictHash(true)

ibs := state.New(s)

for _, txnInfo := range trace.TxnInfo {
for addr, accTrace := range txnInfo.Traces {
ibs.GetBalance(addr)

if accTrace.Balance != nil {
ibs.SetBalance(addr, accTrace.Balance)
}

if accTrace.Nonce != nil {
ibs.SetNonce(addr, accTrace.Nonce.Uint64())
}

if accTrace.CodeUsage != nil {
if accTrace.CodeUsage.Read != nil {
ibs.GetCode(addr)
}

if accTrace.CodeUsage.Write != nil {
ibs.SetCode(addr, accTrace.CodeUsage.Write)
}
}

if accTrace.SelfDestructed != nil && *accTrace.SelfDestructed {
ibs.Selfdestruct(addr)
}

for _, r := range accTrace.StorageRead {
var value uint256.Int
ibs.GetState(addr, &r, &value)
}

for k, v := range accTrace.StorageWritten {
ibs.SetState(addr, &k, *v)
}
}

ibs.FinalizeTx(&chain.Rules{}, s)
}

s.Finalize()
}

func TestTDSWitness(t *testing.T) {
contract := libcommon.HexToAddress("0x71dd1027069078091B3ca48093B00E4735B20624")
sKey := libcommon.HexToHash("0x4321")
Expand Down
4 changes: 4 additions & 0 deletions core/state/stateless.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (s *Stateless) SetBlockNr(blockNr uint64) {
s.blockNr = blockNr
}

func (s *Stateless) SetStrictHash(strict bool) {
s.t.SetStrictHash(strict)
}

// ReadAccountData is a part of the StateReader interface
// This implementation attempts to look up account data in the state trie, and fails if it is not found
func (s *Stateless) ReadAccountData(address common.Address) (*accounts.Account, error) {
Expand Down
Loading

0 comments on commit 7740dc1

Please sign in to comment.