@ -1158,37 +1158,53 @@ func TestLogRebirth(t *testing.T) {
blockchain . SubscribeLogsEvent ( newLogCh )
blockchain . SubscribeRemovedLogsEvent ( rmLogsCh )
// This chain contains a single log.
genDb , chain , _ := GenerateChainWithGenesis ( gspec , engine , 2 , func ( i int , gen * BlockGen ) {
if i == 1 {
tx , err := types . SignTx ( types . NewContractCreation ( gen . TxNonce ( addr1 ) , new ( big . Int ) , 1000000 , gen . header . BaseFee , logCode ) , signer , key1 )
// This chain contains 10 logs.
genDb , chain , _ := GenerateChainWithGenesis ( gspec , engine , 3 , func ( i int , gen * BlockGen ) {
if i < 2 {
for ii := 0 ; ii < 5 ; ii ++ {
tx , err := types . SignNewTx ( key1 , signer , & types . LegacyTx {
Nonce : gen . TxNonce ( addr1 ) ,
GasPrice : gen . header . BaseFee ,
Gas : uint64 ( 1000001 ) ,
Data : logCode ,
} )
if err != nil {
t . Fatalf ( "failed to create tx: %v" , err )
}
gen . AddTx ( tx )
}
}
} )
if _ , err := blockchain . InsertChain ( chain ) ; err != nil {
t . Fatalf ( "failed to insert chain: %v" , err )
}
checkLogEvents ( t , newLogCh , rmLogsCh , 1 , 0 )
checkLogEvents ( t , newLogCh , rmLogsCh , 10 , 0 )
// Generate long reorg chain containing another log. Inserting the
// chain removes one log and adds one.
_ , forkChain , _ := GenerateChainWithGenesis ( gspec , engine , 2 , func ( i int , gen * BlockGen ) {
if i == 1 {
tx , err := types . SignTx ( types . NewContractCreation ( gen . TxNonce ( addr1 ) , new ( big . Int ) , 1000000 , gen . header . BaseFee , logCode ) , signer , key1 )
// Generate long reorg chain containing more logs. Inserting the
// chain removes one log and adds four.
_ , forkChain , _ := GenerateChainWithGenesis ( gspec , engine , 3 , func ( i int , gen * BlockGen ) {
if i == 2 {
// The last (head) block is not part of the reorg-chain, we can ignore it
return
}
for ii := 0 ; ii < 5 ; ii ++ {
tx , err := types . SignNewTx ( key1 , signer , & types . LegacyTx {
Nonce : gen . TxNonce ( addr1 ) ,
GasPrice : gen . header . BaseFee ,
Gas : uint64 ( 1000000 ) ,
Data : logCode ,
} )
if err != nil {
t . Fatalf ( "failed to create tx: %v" , err )
}
gen . AddTx ( tx )
gen . OffsetTime ( - 9 ) // higher block difficulty
}
gen . OffsetTime ( - 9 ) // higher block difficulty
} )
if _ , err := blockchain . InsertChain ( forkChain ) ; err != nil {
t . Fatalf ( "failed to insert forked chain: %v" , err )
}
checkLogEvents ( t , newLogCh , rmLogsCh , 1 , 1 )
checkLogEvents ( t , newLogCh , rmLogsCh , 10 , 10 )
// This chain segment is rooted in the original chain, but doesn't contain any logs.
// When inserting it, the canonical chain switches away from forkChain and re-emits
@ -1197,7 +1213,7 @@ func TestLogRebirth(t *testing.T) {
if _ , err := blockchain . InsertChain ( newBlocks ) ; err != nil {
t . Fatalf ( "failed to insert forked chain: %v" , err )
}
checkLogEvents ( t , newLogCh , rmLogsCh , 1 , 1 )
checkLogEvents ( t , newLogCh , rmLogsCh , 10 , 10 )
}
// This test is a variation of TestLogRebirth. It verifies that log events are emitted
@ -1252,19 +1268,43 @@ func TestSideLogRebirth(t *testing.T) {
func checkLogEvents ( t * testing . T , logsCh <- chan [ ] * types . Log , rmLogsCh <- chan RemovedLogsEvent , wantNew , wantRemoved int ) {
t . Helper ( )
if len ( logsCh ) != wantNew {
t . Fatalf ( "wrong number of log events: got %d, want %d" , len ( logsCh ) , wantNew )
var (
countNew int
countRm int
prev int
)
// Drain events.
for len ( logsCh ) > 0 {
x := <- logsCh
countNew += len ( x )
for _ , log := range x {
// We expect added logs to be in ascending order: 0:0, 0:1, 1:0 ...
have := 100 * int ( log . BlockNumber ) + int ( log . TxIndex )
if have < prev {
t . Fatalf ( "Expected new logs to arrive in ascending order (%d < %d)" , have , prev )
}
if len ( rmLogsCh ) != wantRemoved {
t . Fatalf ( "wrong number of removed log events: got %d, want %d" , len ( rmLogsCh ) , wantRemoved )
prev = have
}
// Drain events.
for i := 0 ; i < len ( logsCh ) ; i ++ {
<- logsCh
}
for i := 0 ; i < len ( rmLogsCh ) ; i ++ {
<- rmLogsCh
prev = 0
for len ( rmLogsCh ) > 0 {
x := <- rmLogsCh
countRm += len ( x . Logs )
for _ , log := range x . Logs {
// We expect removed logs to be in ascending order: 0:0, 0:1, 1:0 ...
have := 100 * int ( log . BlockNumber ) + int ( log . TxIndex )
if have < prev {
t . Fatalf ( "Expected removed logs to arrive in ascending order (%d < %d)" , have , prev )
}
prev = have
}
}
if countNew != wantNew {
t . Fatalf ( "wrong number of log events: got %d, want %d" , countNew , wantNew )
}
if countRm != wantRemoved {
t . Fatalf ( "wrong number of removed log events: got %d, want %d" , countRm , wantRemoved )
}
}