Skip to content

Commit

Permalink
Fix testAllContracts behavior (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
anishnaik authored and s4nsec committed Jul 8, 2024
1 parent ae4c36e commit 1f8afda
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 15 deletions.
10 changes: 6 additions & 4 deletions chain/test_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,9 @@ func (t *TestChain) emitContractChangeEvents(reverting bool, messageResults ...*
// this execution result is being committed to chain.
if deploymentChange.Creation {
err = t.Events.ContractDeploymentAddedEventEmitter.Publish(ContractDeploymentsAddedEvent{
Chain: t,
Contract: deploymentChange.Contract,
Chain: t,
Contract: deploymentChange.Contract,
DynamicDeployment: deploymentChange.DynamicCreation,
})
} else if deploymentChange.Destroyed {
err = t.Events.ContractDeploymentRemovedEventEmitter.Publish(ContractDeploymentsRemovedEvent{
Expand Down Expand Up @@ -887,8 +888,9 @@ func (t *TestChain) emitContractChangeEvents(reverting bool, messageResults ...*
})
} else if deploymentChange.Destroyed {
err = t.Events.ContractDeploymentAddedEventEmitter.Publish(ContractDeploymentsAddedEvent{
Chain: t,
Contract: deploymentChange.Contract,
Chain: t,
Contract: deploymentChange.Contract,
DynamicDeployment: deploymentChange.DynamicCreation,
})
}
if err != nil {
Expand Down
21 changes: 12 additions & 9 deletions chain/test_chain_deployments_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ func (t *testChainDeploymentsTracer) CaptureStart(env *vm.EVM, from common.Addre
InitBytecode: input,
RuntimeBytecode: nil,
},
Creation: true,
SelfDestructed: false,
Destroyed: false,
Creation: true,
DynamicCreation: false,
SelfDestructed: false,
Destroyed: false,
})
}
}
Expand Down Expand Up @@ -118,9 +119,10 @@ func (t *testChainDeploymentsTracer) CaptureEnter(typ vm.OpCode, from common.Add
InitBytecode: input,
RuntimeBytecode: nil,
},
Creation: true,
SelfDestructed: false,
Destroyed: false,
Creation: true,
DynamicCreation: true,
SelfDestructed: false,
Destroyed: false,
})
}
}
Expand Down Expand Up @@ -158,9 +160,10 @@ func (t *testChainDeploymentsTracer) CaptureState(pc uint64, op vm.OpCode, gas,
InitBytecode: nil,
RuntimeBytecode: t.evm.StateDB.GetCode(scope.Contract.Address()),
},
Creation: false,
SelfDestructed: true,
Destroyed: t.selfDestructDestroysCode,
Creation: false,
DynamicCreation: false,
SelfDestructed: true,
Destroyed: t.selfDestructDestroysCode,
})
}
}
Expand Down
4 changes: 4 additions & 0 deletions chain/test_chain_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ type ContractDeploymentsAddedEvent struct {

// Contract defines information for the contract which was deployed to the Chain.
Contract *types.DeployedContractBytecode

// DynamicDeployment describes whether this contract deployment was dynamic (e.g. `c = new MyContract()`) or was
// because of a traditional transaction
DynamicDeployment bool
}

// ContractDeploymentsRemovedEvent describes an event where a contract has become unavailable on the TestChain, either
Expand Down
4 changes: 4 additions & 0 deletions chain/types/deployed_contract_bytecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type DeployedContractBytecodeChange struct {
// Destroyed are true.
Creation bool

// DynamicCreation indicates whether the change made was a _dynamic_ contract creation. This cannot be true if
// Creation is false.
DynamicCreation bool

// SelfDestructed indicates whether the change made was due to a self-destruct instruction being executed. This
// cannot be true if Creation is true.
// Note: This may not be indicative of contract removal (as is the case with Destroyed), as proposed changes to
Expand Down
2 changes: 1 addition & 1 deletion fuzzing/config/config_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func GetDefaultProjectConfig(platform string) (*ProjectConfig, error) {
TransactionGasLimit: 12_500_000,
Testing: TestingConfig{
StopOnFailedTest: true,
StopOnFailedContractMatching: true,
StopOnFailedContractMatching: false,
StopOnNoTests: true,
TestAllContracts: false,
TraceAll: false,
Expand Down
1 change: 1 addition & 0 deletions fuzzing/fuzzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ func TestDeploymentsSelfDestruct(t *testing.T) {
config.Fuzzing.DeploymentOrder = []string{"InnerDeploymentFactory"}
config.Fuzzing.TestLimit = 500 // this test should expose a failure quickly.
config.Fuzzing.Testing.StopOnNoTests = false
config.Fuzzing.Testing.TestAllContracts = true
},
method: func(f *fuzzerTestContext) {
// Subscribe to any mined block events globally. When receiving them, check contract changes for a
Expand Down
5 changes: 5 additions & 0 deletions fuzzing/fuzzer_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ func (fw *FuzzerWorker) getNewCorpusCallSequenceWeight() *big.Int {
// onChainContractDeploymentAddedEvent is the event callback used when the chain detects a new contract deployment.
// It attempts bytecode matching and updates the list of deployed contracts the worker should use for fuzz testing.
func (fw *FuzzerWorker) onChainContractDeploymentAddedEvent(event chain.ContractDeploymentsAddedEvent) error {
// Do not track the deployed contract if the contract deployment was a dynamic one and testAllContracts is false
if !fw.fuzzer.config.Fuzzing.Testing.TestAllContracts && event.DynamicDeployment {
return nil
}

// Add the contract address to our value set so our generator can use it in calls.
fw.valueSet.AddAddress(event.Contract.Address)

Expand Down
2 changes: 1 addition & 1 deletion logging/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ func TestDisabledColors(t *testing.T) {

// Ensure that msg doesn't include colors afterwards
prefix := fmt.Sprintf("%s %s", colors.LEFT_ARROW, "foo")
_, ok := strings.CutPrefix(buf.String(), prefix)
_, _, ok := strings.Cut(buf.String(), prefix)
assert.True(t, ok)
}

0 comments on commit 1f8afda

Please sign in to comment.