-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow alternative methods of proposer payments in validation api.
- Loading branch information
Showing
4 changed files
with
206 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package logger | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
) | ||
|
||
// BalanceChangeTracer is a tracer that captures the balance changes of an address before and after each transaction | ||
type BalanceChangeTracer struct { | ||
address common.Address | ||
outerLogger vm.EVMLogger | ||
stateDB vm.StateDB | ||
|
||
balanceChanges []*big.Int | ||
tempBalance *big.Int | ||
} | ||
|
||
func NewBalanceChangeTracer(address common.Address, outerLogger vm.EVMLogger, stateDB vm.StateDB) *BalanceChangeTracer { | ||
return &BalanceChangeTracer{ | ||
address: address, | ||
outerLogger: outerLogger, | ||
stateDB: stateDB, | ||
|
||
balanceChanges: nil, | ||
tempBalance: new(big.Int), | ||
} | ||
} | ||
|
||
// GetBalanceChanges returns the balance changes of the address during the execution of the transaction | ||
// It should be called after all transactions were executed with this tracer | ||
func (b *BalanceChangeTracer) GetBalanceChanges() []*big.Int { | ||
return b.balanceChanges | ||
} | ||
|
||
func (b *BalanceChangeTracer) CaptureTxStart(gasLimit uint64) { | ||
b.tempBalance.Set(b.stateDB.GetBalance(b.address)) | ||
if b.outerLogger != nil { | ||
b.outerLogger.CaptureTxStart(gasLimit) | ||
} | ||
} | ||
|
||
func (b *BalanceChangeTracer) CaptureTxEnd(restGas uint64) { | ||
balanceChange := new(big.Int).Sub(b.stateDB.GetBalance(b.address), b.tempBalance) | ||
b.balanceChanges = append(b.balanceChanges, balanceChange) | ||
|
||
if b.outerLogger != nil { | ||
b.outerLogger.CaptureTxEnd(restGas) | ||
} | ||
} | ||
|
||
func (b *BalanceChangeTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) { | ||
if b.outerLogger != nil { | ||
b.outerLogger.CaptureStart(env, from, to, create, input, gas, value) | ||
} | ||
} | ||
|
||
func (b *BalanceChangeTracer) CaptureEnd(output []byte, gasUsed uint64, err error) { | ||
if b.outerLogger != nil { | ||
b.outerLogger.CaptureEnd(output, gasUsed, err) | ||
} | ||
} | ||
|
||
func (b *BalanceChangeTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { | ||
if b.outerLogger != nil { | ||
b.outerLogger.CaptureEnter(typ, from, to, input, gas, value) | ||
} | ||
} | ||
|
||
func (b *BalanceChangeTracer) CaptureExit(output []byte, gasUsed uint64, err error) { | ||
if b.outerLogger != nil { | ||
b.outerLogger.CaptureExit(output, gasUsed, err) | ||
} | ||
} | ||
|
||
func (b *BalanceChangeTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) { | ||
if b.outerLogger != nil { | ||
b.outerLogger.CaptureState(pc, op, gas, cost, scope, rData, depth, err) | ||
} | ||
} | ||
|
||
func (b *BalanceChangeTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) { | ||
if b.outerLogger != nil { | ||
b.outerLogger.CaptureFault(pc, op, gas, cost, scope, depth, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package logger | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/state" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type dummyStateDB struct { | ||
state.StateDB | ||
address common.Address | ||
balance *big.Int | ||
} | ||
|
||
func (db *dummyStateDB) GetBalance(address common.Address) *big.Int { | ||
if address == db.address { | ||
return db.balance | ||
} else { | ||
return big.NewInt(0) | ||
} | ||
} | ||
|
||
func Test_balanceChangeTracer(t *testing.T) { | ||
address := common.HexToAddress("0x123") | ||
stateDB := &dummyStateDB{address: address} | ||
|
||
tracer := NewBalanceChangeTracer(address, nil, stateDB) | ||
|
||
// before the block balance is 100 | ||
stateDB.balance = big.NewInt(100) | ||
|
||
// 1-st tx, gain of 7 | ||
tracer.CaptureTxStart(0) | ||
stateDB.balance = big.NewInt(107) | ||
tracer.CaptureTxEnd(0) | ||
|
||
// 2-end tx, gain of 17 | ||
tracer.CaptureTxStart(0) | ||
stateDB.balance = big.NewInt(124) | ||
tracer.CaptureTxEnd(0) | ||
|
||
// 3-rd tx, loss of 5 | ||
tracer.CaptureTxStart(0) | ||
stateDB.balance = big.NewInt(119) | ||
tracer.CaptureTxEnd(0) | ||
|
||
// 4-rd tx, gain of 3 | ||
tracer.CaptureTxStart(0) | ||
stateDB.balance = big.NewInt(122) | ||
tracer.CaptureTxEnd(0) | ||
|
||
result := tracer.GetBalanceChanges() | ||
expectedResult := []*big.Int{big.NewInt(7), big.NewInt(17), big.NewInt(-5), big.NewInt(3)} | ||
|
||
require.Equal(t, expectedResult, result) | ||
} |