2020-12-09 18:24:08 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2021-05-06 17:37:38 +00:00
|
|
|
"bytes"
|
2020-12-09 18:24:08 +00:00
|
|
|
"context"
|
2021-05-06 17:37:38 +00:00
|
|
|
"encoding/json"
|
2020-12-09 18:24:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-05-06 17:37:38 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/eth/tracers"
|
|
|
|
"github.com/ledgerwatch/erigon/internal/ethapi"
|
2020-12-09 18:24:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var debugTraceTransactionTests = []struct {
|
|
|
|
txHash string
|
|
|
|
gas uint64
|
|
|
|
failed bool
|
|
|
|
returnValue string
|
|
|
|
}{
|
|
|
|
{"3f3cb8a0e13ed2481f97f53f7095b9cbc78b6ffb779f2d3e565146371a8830ea", 21000, false, ""},
|
2021-03-17 16:49:02 +00:00
|
|
|
{"f588c6426861d9ad25d5ccc12324a8d213f35ef1ed4153193f0c13eb81ca7f4a", 49189, false, "0000000000000000000000000000000000000000000000000000000000000001"},
|
|
|
|
{"b6449d8e167a8826d050afe4c9f07095236ff769a985f02649b1023c2ded2059", 38899, false, ""},
|
2020-12-09 18:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var debugTraceTransactionNoRefundTests = []struct {
|
|
|
|
txHash string
|
|
|
|
gas uint64
|
|
|
|
failed bool
|
|
|
|
returnValue string
|
|
|
|
}{
|
|
|
|
{"3f3cb8a0e13ed2481f97f53f7095b9cbc78b6ffb779f2d3e565146371a8830ea", 21000, false, ""},
|
2021-03-17 16:49:02 +00:00
|
|
|
{"f588c6426861d9ad25d5ccc12324a8d213f35ef1ed4153193f0c13eb81ca7f4a", 49189, false, "0000000000000000000000000000000000000000000000000000000000000001"},
|
|
|
|
{"b6449d8e167a8826d050afe4c9f07095236ff769a985f02649b1023c2ded2059", 62899, false, ""},
|
2020-12-09 18:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTraceTransaction(t *testing.T) {
|
2021-06-29 08:46:00 +00:00
|
|
|
db := createTestKV(t)
|
2021-05-17 12:15:19 +00:00
|
|
|
api := NewPrivateDebugAPI(NewBaseApi(nil), db, 0)
|
2020-12-09 18:24:08 +00:00
|
|
|
for _, tt := range debugTraceTransactionTests {
|
2021-05-06 17:37:38 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
stream := jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 4096)
|
2021-06-05 15:17:04 +00:00
|
|
|
err := api.TraceTransaction(context.Background(), common.HexToHash(tt.txHash), &tracers.TraceConfig{}, stream)
|
2021-05-06 17:37:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("traceTransaction %s: %v", tt.txHash, err)
|
|
|
|
}
|
|
|
|
if err = stream.Flush(); err != nil {
|
|
|
|
t.Fatalf("error flusing: %v", err)
|
|
|
|
}
|
|
|
|
var er ethapi.ExecutionResult
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), &er); err != nil {
|
|
|
|
t.Fatalf("parsing result: %v", err)
|
2020-12-09 18:24:08 +00:00
|
|
|
}
|
|
|
|
if er.Gas != tt.gas {
|
|
|
|
t.Errorf("wrong gas for transaction %s, got %d, expected %d", tt.txHash, er.Gas, tt.gas)
|
|
|
|
}
|
|
|
|
if er.Failed != tt.failed {
|
|
|
|
t.Errorf("wrong failed flag for transaction %s, got %t, expected %t", tt.txHash, er.Failed, tt.failed)
|
|
|
|
}
|
|
|
|
if er.ReturnValue != tt.returnValue {
|
|
|
|
t.Errorf("wrong return value for transaction %s, got %s, expected %s", tt.txHash, er.ReturnValue, tt.returnValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTraceTransactionNoRefund(t *testing.T) {
|
2021-06-29 08:46:00 +00:00
|
|
|
db := createTestKV(t)
|
2021-05-17 12:15:19 +00:00
|
|
|
api := NewPrivateDebugAPI(NewBaseApi(nil), db, 0)
|
2020-12-09 18:24:08 +00:00
|
|
|
for _, tt := range debugTraceTransactionNoRefundTests {
|
2021-05-06 17:37:38 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
stream := jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 4096)
|
2021-05-01 07:42:23 +00:00
|
|
|
var norefunds = true
|
2021-06-05 15:17:04 +00:00
|
|
|
err := api.TraceTransaction(context.Background(), common.HexToHash(tt.txHash), &tracers.TraceConfig{NoRefunds: &norefunds}, stream)
|
2021-05-06 17:37:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("traceTransaction %s: %v", tt.txHash, err)
|
|
|
|
}
|
|
|
|
if err = stream.Flush(); err != nil {
|
|
|
|
t.Fatalf("error flusing: %v", err)
|
|
|
|
}
|
|
|
|
var er ethapi.ExecutionResult
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), &er); err != nil {
|
|
|
|
t.Fatalf("parsing result: %v", err)
|
2020-12-09 18:24:08 +00:00
|
|
|
}
|
|
|
|
if er.Gas != tt.gas {
|
|
|
|
t.Errorf("wrong gas for transaction %s, got %d, expected %d", tt.txHash, er.Gas, tt.gas)
|
|
|
|
}
|
|
|
|
if er.Failed != tt.failed {
|
|
|
|
t.Errorf("wrong failed flag for transaction %s, got %t, expected %t", tt.txHash, er.Failed, tt.failed)
|
|
|
|
}
|
|
|
|
if er.ReturnValue != tt.returnValue {
|
|
|
|
t.Errorf("wrong return value for transaction %s, got %s, expected %s", tt.txHash, er.ReturnValue, tt.returnValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|