@ -47,7 +47,17 @@ func TestLookupStorage(t *testing.T) {
tx3 := types . NewTransaction ( 3 , common . BytesToAddress ( [ ] byte { 0x33 } ) , 0 , big . NewInt ( 333 ) , 3333 , big . NewInt ( 33333 ) , [ ] byte { 0x33 , 0x33 , 0x33 } )
tx3 := types . NewTransaction ( 3 , common . BytesToAddress ( [ ] byte { 0x33 } ) , 0 , big . NewInt ( 333 ) , 3333 , big . NewInt ( 33333 ) , [ ] byte { 0x33 , 0x33 , 0x33 } )
txs := [ ] * types . Transaction { tx1 , tx2 , tx3 }
txs := [ ] * types . Transaction { tx1 , tx2 , tx3 }
block := types . NewBlock ( blockfactory . NewTestHeader ( ) . With ( ) . Number ( big . NewInt ( 314 ) ) . Header ( ) , txs , types . Receipts { & types . Receipt { } , & types . Receipt { } , & types . Receipt { } } , nil , nil , nil )
stx := sampleCreateValidatorStakingTxn ( )
stxs := [ ] * staking . StakingTransaction { stx }
receipts := types . Receipts {
& types . Receipt { } ,
& types . Receipt { } ,
& types . Receipt { } ,
& types . Receipt { } ,
}
block := types . NewBlock ( blockfactory . NewTestHeader ( ) . With ( ) . Number ( big . NewInt ( 314 ) ) . Header ( ) , txs , receipts , nil , nil , stxs )
// Check that no transactions entries are in a pristine database
// Check that no transactions entries are in a pristine database
for i , tx := range txs {
for i , tx := range txs {
@ -55,6 +65,11 @@ func TestLookupStorage(t *testing.T) {
t . Fatalf ( "tx #%d [%x]: non existent transaction returned: %v" , i , tx . Hash ( ) , txn )
t . Fatalf ( "tx #%d [%x]: non existent transaction returned: %v" , i , tx . Hash ( ) , txn )
}
}
}
}
for i , stx := range stxs {
if stxn , _ , _ , _ := ReadStakingTransaction ( db , stx . Hash ( ) ) ; stxn != nil {
t . Fatalf ( "stx #%d [%x]: non existent staking transaction returned: %v" , i , stxn . Hash ( ) , stxn )
}
}
// Insert all the transactions into the database, and verify contents
// Insert all the transactions into the database, and verify contents
WriteBlock ( db , block )
WriteBlock ( db , block )
WriteTxLookupEntries ( db , block )
WriteTxLookupEntries ( db , block )
@ -71,6 +86,18 @@ func TestLookupStorage(t *testing.T) {
}
}
}
}
}
}
for i , stx := range stxs {
if txn , hash , number , index := ReadStakingTransaction ( db , stx . Hash ( ) ) ; txn == nil {
t . Fatalf ( "tx #%d [%x]: staking transaction not found" , i , stx . Hash ( ) )
} else {
if hash != block . Hash ( ) || number != block . NumberU64 ( ) || index != uint64 ( i ) {
t . Fatalf ( "stx #%d [%x]: positional metadata mismatch: have %x/%d/%d, want %x/%v/%v" , i , stx . Hash ( ) , hash , number , index , block . Hash ( ) , block . NumberU64 ( ) , i )
}
if stx . Hash ( ) != txn . Hash ( ) {
t . Fatalf ( "stx #%d [%x]: staking transaction mismatch: have %v, want %v" , i , stx . Hash ( ) , txn , stx )
}
}
}
// Delete the transactions and check purge
// Delete the transactions and check purge
for i , tx := range txs {
for i , tx := range txs {
DeleteTxLookupEntry ( db , tx . Hash ( ) )
DeleteTxLookupEntry ( db , tx . Hash ( ) )
@ -78,12 +105,37 @@ func TestLookupStorage(t *testing.T) {
t . Fatalf ( "tx #%d [%x]: deleted transaction returned: %v" , i , tx . Hash ( ) , txn )
t . Fatalf ( "tx #%d [%x]: deleted transaction returned: %v" , i , tx . Hash ( ) , txn )
}
}
}
}
for i , tx := range txs {
DeleteTxLookupEntry ( db , tx . Hash ( ) )
if stxn , _ , _ , _ := ReadStakingTransaction ( db , tx . Hash ( ) ) ; stxn != nil {
t . Fatalf ( "stx #%d [%x]: deleted staking transaction returned: %v" , i , stx . Hash ( ) , stxn )
}
}
}
}
// Test that staking tx hash does not find a plain tx hash (and visa versa) within the same block
// Test that staking tx hash does not find a plain tx hash (and visa versa) within the same block
func TestMixedLookupStorage ( t * testing . T ) {
func TestMixedLookupStorage ( t * testing . T ) {
db := ethdb . NewMemDatabase ( )
db := ethdb . NewMemDatabase ( )
tx := types . NewTransaction ( 1 , common . BytesToAddress ( [ ] byte { 0x11 } ) , 0 , big . NewInt ( 111 ) , 1111 , big . NewInt ( 11111 ) , [ ] byte { 0x11 , 0x11 , 0x11 } )
tx := types . NewTransaction ( 1 , common . BytesToAddress ( [ ] byte { 0x11 } ) , 0 , big . NewInt ( 111 ) , 1111 , big . NewInt ( 11111 ) , [ ] byte { 0x11 , 0x11 , 0x11 } )
stx := sampleCreateValidatorStakingTxn ( )
txs := [ ] * types . Transaction { tx }
stxs := [ ] * staking . StakingTransaction { stx }
header := blockfactory . NewTestHeader ( ) . With ( ) . Number ( big . NewInt ( 314 ) ) . Header ( )
block := types . NewBlock ( header , txs , types . Receipts { & types . Receipt { } , & types . Receipt { } } , nil , nil , stxs )
WriteBlock ( db , block )
WriteTxLookupEntries ( db , block )
if recTx , _ , _ , _ := ReadStakingTransaction ( db , tx . Hash ( ) ) ; recTx != nil {
t . Fatal ( "got staking transactions with plain tx hash" )
}
if recTx , _ , _ , _ := ReadTransaction ( db , stx . Hash ( ) ) ; recTx != nil {
t . Fatal ( "got plain transactions with staking tx hash" )
}
}
func sampleCreateValidatorStakingTxn ( ) * staking . StakingTransaction {
key , _ := crypto . GenerateKey ( )
key , _ := crypto . GenerateKey ( )
stakePayloadMaker := func ( ) ( staking . Directive , interface { } ) {
stakePayloadMaker := func ( ) ( staking . Directive , interface { } ) {
p := & bls . PublicKey { }
p := & bls . PublicKey { }
@ -123,17 +175,5 @@ func TestMixedLookupStorage(t *testing.T) {
}
}
}
}
stx , _ := staking . NewStakingTransaction ( 0 , 1e10 , big . NewInt ( 10000 ) , stakePayloadMaker )
stx , _ := staking . NewStakingTransaction ( 0 , 1e10 , big . NewInt ( 10000 ) , stakePayloadMaker )
txs := [ ] * types . Transaction { tx }
return stx
stxs := [ ] * staking . StakingTransaction { stx }
header := blockfactory . NewTestHeader ( ) . With ( ) . Number ( big . NewInt ( 314 ) ) . Header ( )
block := types . NewBlock ( header , txs , types . Receipts { & types . Receipt { } , & types . Receipt { } } , nil , nil , stxs )
WriteBlock ( db , block )
WriteTxLookupEntries ( db , block )
if recTx , _ , _ , _ := ReadStakingTransaction ( db , tx . Hash ( ) ) ; recTx != nil {
t . Fatal ( "got staking transactions with plain tx hash" )
}
if recTx , _ , _ , _ := ReadTransaction ( db , stx . Hash ( ) ) ; recTx != nil {
t . Fatal ( "got plain transactions with staking tx hash" )
}
}
}