You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
5.8 KiB
207 lines
5.8 KiB
3 years ago
|
// Copyright 2017 The go-ethereum Authors
|
||
|
// This file is part of the go-ethereum library.
|
||
|
//
|
||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||
|
// the Free Software Foundation, either version 3 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU Lesser General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU Lesser General Public License
|
||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
package tracers
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
3 years ago
|
"errors"
|
||
3 years ago
|
"fmt"
|
||
|
"math/big"
|
||
|
|
||
|
"github.com/ethereum/go-ethereum/common"
|
||
|
"github.com/ethereum/go-ethereum/rlp"
|
||
8 months ago
|
"github.com/woop-chain/woop/crypto/hash"
|
||
3 years ago
|
)
|
||
|
|
||
|
type ActionStorage struct {
|
||
|
Subtraces uint
|
||
|
TraceAddress []uint
|
||
|
TraceData []byte
|
||
|
}
|
||
|
|
||
|
func (storage *ActionStorage) appendByte(byt byte) {
|
||
|
storage.TraceData = append(storage.TraceData, byt)
|
||
|
}
|
||
|
|
||
|
func (storage *ActionStorage) appendFixed(data []byte) {
|
||
|
storage.TraceData = append(storage.TraceData, data...)
|
||
|
}
|
||
|
func (storage *ActionStorage) appendNumber(num *big.Int) {
|
||
|
bytes, _ := rlp.EncodeToBytes(num)
|
||
|
storage.appendByte(uint8(len(bytes)))
|
||
|
storage.appendFixed(bytes)
|
||
|
}
|
||
|
|
||
|
func (storage *ActionStorage) readByte() byte {
|
||
|
val := storage.TraceData[0]
|
||
|
storage.TraceData = storage.TraceData[1:]
|
||
|
return val
|
||
|
}
|
||
|
func (storage *ActionStorage) readFixedData(size uint) []byte {
|
||
|
fixedData := storage.TraceData[:size]
|
||
|
storage.TraceData = storage.TraceData[size:]
|
||
|
return fixedData
|
||
|
}
|
||
|
func (storage *ActionStorage) readNumber() *big.Int {
|
||
|
size := storage.readByte()
|
||
|
bytes := storage.readFixedData(uint(size))
|
||
|
var num big.Int
|
||
|
rlp.DecodeBytes(bytes, &num)
|
||
|
return &num
|
||
|
}
|
||
|
|
||
|
type TxStorage struct {
|
||
|
Hash common.Hash
|
||
|
Storages []*ActionStorage
|
||
|
}
|
||
|
|
||
|
type TraceBlockStorage struct {
|
||
|
Hash common.Hash
|
||
|
Number uint64
|
||
|
AddressTable []common.Address // address table
|
||
|
DataKeyTable []common.Hash // data key table
|
||
|
dataValueTable [][]byte // data, store in db, avoid RLPEncode
|
||
3 years ago
|
TraceStorages [][]byte // trace data, length equal the number of transaction in a block
|
||
3 years ago
|
addressIndex map[common.Address]int // address index in AddressTable
|
||
|
dataIndex map[common.Hash]int // data index in DataKeyTable
|
||
|
}
|
||
|
|
||
3 years ago
|
// get data of index i
|
||
3 years ago
|
func (ts *TraceBlockStorage) getData(i int) []byte {
|
||
|
return ts.dataValueTable[i]
|
||
|
}
|
||
|
|
||
3 years ago
|
// get address of index i
|
||
3 years ago
|
func (ts *TraceBlockStorage) getAddress(i int) common.Address {
|
||
|
return ts.AddressTable[i]
|
||
|
}
|
||
|
|
||
3 years ago
|
// store data and assign an index to it. if data existed, just return it's index
|
||
3 years ago
|
func (ts *TraceBlockStorage) indexData(data []byte) int {
|
||
|
key := hash.Keccak256Hash(data)
|
||
|
if index, exist := ts.dataIndex[key]; exist {
|
||
|
return index
|
||
|
}
|
||
|
index := len(ts.DataKeyTable)
|
||
|
ts.DataKeyTable = append(ts.DataKeyTable, key)
|
||
|
ts.dataValueTable = append(ts.dataValueTable, data)
|
||
|
ts.dataIndex[key] = index
|
||
|
return index
|
||
|
}
|
||
|
|
||
3 years ago
|
// store address and assign an index to it. if address existed, just return it's index
|
||
3 years ago
|
func (ts *TraceBlockStorage) indexAddress(address common.Address) int {
|
||
|
if index, exist := ts.addressIndex[address]; exist {
|
||
|
return index
|
||
|
}
|
||
|
index := len(ts.addressIndex)
|
||
|
ts.AddressTable = append(ts.AddressTable, address)
|
||
|
ts.addressIndex[address] = index
|
||
|
return index
|
||
|
}
|
||
|
|
||
3 years ago
|
// use this key as
|
||
3 years ago
|
func (ts *TraceBlockStorage) KeyDB() []byte {
|
||
|
return ts.Hash[:]
|
||
|
}
|
||
|
|
||
3 years ago
|
// store TraceBlockStorage to db
|
||
3 years ago
|
func (ts *TraceBlockStorage) ToDB(write func([]byte, []byte)) {
|
||
3 years ago
|
for index, key := range ts.DataKeyTable {
|
||
|
write(key[:], ts.dataValueTable[index])
|
||
|
}
|
||
|
bytes, _ := rlp.EncodeToBytes(ts)
|
||
3 years ago
|
write(ts.KeyDB(), bytes)
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// load TraceBlockStorage from db
|
||
3 years ago
|
func (ts *TraceBlockStorage) FromDB(read func([]byte) ([]byte, error)) error {
|
||
|
bytes, err := read(ts.KeyDB())
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
err = rlp.DecodeBytes(bytes, ts)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
3 years ago
|
for _, key := range ts.DataKeyTable {
|
||
3 years ago
|
data, err := read(key[:])
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
ts.dataValueTable = append(ts.dataValueTable, data)
|
||
3 years ago
|
}
|
||
3 years ago
|
return nil
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// return trace result of a tx for giving index
|
||
3 years ago
|
func (ts *TraceBlockStorage) TxJson(index int) ([]json.RawMessage, error) {
|
||
3 years ago
|
var results []json.RawMessage
|
||
|
var txStorage TxStorage
|
||
3 years ago
|
var err error
|
||
3 years ago
|
b := ts.TraceStorages[index]
|
||
3 years ago
|
err = rlp.DecodeBytes(b, &txStorage)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
|
||
|
headPiece := fmt.Sprintf(
|
||
|
`"blockNumber":%d,"blockHash":"%s","transactionHash":"%s","transactionPosition":%d`,
|
||
|
ts.Number, ts.Hash.Hex(), txStorage.Hash.Hex(), index,
|
||
|
)
|
||
|
|
||
|
for _, acStorage := range txStorage.Storages {
|
||
|
ac := &action{}
|
||
|
ac.fromStorage(ts, acStorage)
|
||
|
|
||
|
typStr, acStr, outStr := ac.toJsonStr()
|
||
|
if acStr == nil {
|
||
3 years ago
|
err = errors.New("tracer internal failure")
|
||
|
return nil, err
|
||
3 years ago
|
}
|
||
|
traceStr, _ := json.Marshal(acStorage.TraceAddress)
|
||
|
bodyPiece := fmt.Sprintf(
|
||
|
`,"subtraces":%d,"traceAddress":%s,"type":"%s","action":%s`,
|
||
|
acStorage.Subtraces, string(traceStr), typStr, *acStr,
|
||
|
)
|
||
|
var resultPiece string
|
||
|
if ac.err != nil {
|
||
|
resultPiece = fmt.Sprintf(`,"error":"Reverted","revert":"0x%x"`, ac.revert)
|
||
|
} else if outStr != nil {
|
||
|
resultPiece = fmt.Sprintf(`,"result":%s`, *outStr)
|
||
|
} else {
|
||
|
resultPiece = `,"result":null`
|
||
|
}
|
||
|
jstr := "{" + headPiece + bodyPiece + resultPiece + "}"
|
||
|
results = append(results, json.RawMessage(jstr))
|
||
|
}
|
||
3 years ago
|
return results, nil
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// return trace result of a block
|
||
3 years ago
|
func (ts *TraceBlockStorage) ToJson() (json.RawMessage, error) {
|
||
3 years ago
|
var results []json.RawMessage
|
||
|
for i := range ts.TraceStorages {
|
||
3 years ago
|
tx, err := ts.TxJson(i)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
results = append(results, tx...)
|
||
|
}
|
||
|
return json.Marshal(results)
|
||
|
}
|