Skip to content

Commit

Permalink
refactor: test files
Browse files Browse the repository at this point in the history
  • Loading branch information
akaladarshi committed Oct 2, 2024
1 parent bdb443c commit 3495c30
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 151 deletions.
44 changes: 0 additions & 44 deletions chain/index/ddls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,47 +662,3 @@ func verifyNonRevertedEventEntriesCount(t *testing.T, s *SqliteIndexer, tipsetKe
require.NoError(t, err)
require.Equal(t, expectedCount, count)
}

func insertTipsetMessage(t *testing.T, s *SqliteIndexer, ts tipsetMessage) int64 {
res, err := s.stmts.insertTipsetMessageStmt.Exec(ts.tipsetKeyCid, ts.height, ts.reverted, ts.messageCid, ts.messageIndex)
require.NoError(t, err)

rowsAffected, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)

messageID, err := res.LastInsertId()
require.NoError(t, err)
require.NotEqual(t, int64(0), messageID)

// read back the message to verify it was inserted correctly
verifyTipsetMessage(t, s, messageID, ts)

return messageID
}

func insertEvent(t *testing.T, s *SqliteIndexer, e event) int64 {
res, err := s.stmts.insertEventStmt.Exec(e.messageID, e.eventIndex, e.emitterAddr, e.reverted)
require.NoError(t, err)

rowsAffected, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)

eventID, err := res.LastInsertId()
require.NoError(t, err)
require.NotEqual(t, int64(0), eventID)

verifyEvent(t, s, eventID, e)

return eventID
}

func insertEventEntry(t *testing.T, s *SqliteIndexer, ee eventEntry) {
res, err := s.stmts.insertEventEntryStmt.Exec(ee.eventID, ee.indexed, ee.flags, ee.key, ee.codec, ee.value)
require.NoError(t, err)

rowsAffected, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)
}
151 changes: 151 additions & 0 deletions chain/index/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,119 @@ import (
"testing"
"time"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/test-go/testify/require"
)

func randomIDAddr(tb testing.TB, rng *pseudo.Rand) address.Address {
tb.Helper()
addr, err := address.NewIDAddress(uint64(rng.Int63()))
require.NoError(tb, err)
return addr
}

func randomCid(tb testing.TB, rng *pseudo.Rand) cid.Cid {
tb.Helper()
cb := cid.V1Builder{Codec: cid.Raw, MhType: mh.IDENTITY}
c, err := cb.Sum(randomBytes(10, rng))
require.NoError(tb, err)
return c
}

func randomBytes(n int, rng *pseudo.Rand) []byte {
buf := make([]byte, n)
rng.Read(buf)
return buf
}

func fakeTipSet(tb testing.TB, rng *pseudo.Rand, h abi.ChainEpoch, parents []cid.Cid) *types.TipSet {
tb.Helper()
timeStamp := uint64(time.Now().Add(time.Duration(h) * builtin.EpochDurationSeconds * time.Second).Unix())

ts, err := types.NewTipSet([]*types.BlockHeader{
{
Height: h,
Miner: randomIDAddr(tb, rng),

Parents: parents,

Ticket: &types.Ticket{VRFProof: []byte{byte(h % 2)}},

ParentStateRoot: randomCid(tb, rng),
Messages: randomCid(tb, rng),
ParentMessageReceipts: randomCid(tb, rng),

BlockSig: &crypto.Signature{Type: crypto.SigTypeBLS},
BLSAggregate: &crypto.Signature{Type: crypto.SigTypeBLS},

Timestamp: timeStamp,
},
{
Height: h,
Miner: randomIDAddr(tb, rng),

Parents: parents,

Ticket: &types.Ticket{VRFProof: []byte{byte((h + 1) % 2)}},

ParentStateRoot: randomCid(tb, rng),
Messages: randomCid(tb, rng),
ParentMessageReceipts: randomCid(tb, rng),

BlockSig: &crypto.Signature{Type: crypto.SigTypeBLS},
BLSAggregate: &crypto.Signature{Type: crypto.SigTypeBLS},

Timestamp: timeStamp,
},
})

require.NoError(tb, err)

return ts
}

func setupWithHeadIndexed(t *testing.T, headHeight abi.ChainEpoch, rng *pseudo.Rand) (*SqliteIndexer, *types.TipSet, *dummyChainStore) {
head := fakeTipSet(t, rng, headHeight, []cid.Cid{})
d := newDummyChainStore()
d.SetHeaviestTipSet(head)

s, err := NewSqliteIndexer(":memory:", d, 0, false, 0)
require.NoError(t, err)
insertHead(t, s, head, headHeight)

return s, head, d
}

func insertHead(t *testing.T, s *SqliteIndexer, head *types.TipSet, height abi.ChainEpoch) {
headKeyBytes, err := toTipsetKeyCidBytes(head)
require.NoError(t, err)

insertTipsetMessage(t, s, tipsetMessage{
tipsetKeyCid: headKeyBytes,
height: uint64(height),
reverted: false,
messageCid: nil,
messageIndex: -1,
})
}

func insertEthTxHash(t *testing.T, s *SqliteIndexer, ethTxHash ethtypes.EthHash, messageCid cid.Cid) {
msgCidBytes := messageCid.Bytes()

res, err := s.stmts.insertEthTxHashStmt.Exec(ethTxHash.String(), msgCidBytes)
require.NoError(t, err)
rowsAffected, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)
}

func insertRandomTipsetAtHeight(t *testing.T, si *SqliteIndexer, height uint64, reverted bool, genesisTime time.Time) (cid.Cid, cid.Cid, int64) {
rng := pseudo.New(pseudo.NewSource(time.Now().UnixNano()))
tsCid := randomCid(t, rng)
Expand Down Expand Up @@ -60,3 +167,47 @@ func insertEthTxHashAtTimeStamp(t *testing.T, s *SqliteIndexer, ethTxHash ethtyp
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)
}

func insertTipsetMessage(t *testing.T, s *SqliteIndexer, ts tipsetMessage) int64 {
res, err := s.stmts.insertTipsetMessageStmt.Exec(ts.tipsetKeyCid, ts.height, ts.reverted, ts.messageCid, ts.messageIndex)
require.NoError(t, err)

rowsAffected, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)

messageID, err := res.LastInsertId()
require.NoError(t, err)
require.NotEqual(t, int64(0), messageID)

// read back the message to verify it was inserted correctly
verifyTipsetMessage(t, s, messageID, ts)

return messageID
}

func insertEvent(t *testing.T, s *SqliteIndexer, e event) int64 {
res, err := s.stmts.insertEventStmt.Exec(e.messageID, e.eventIndex, e.emitterAddr, e.reverted)
require.NoError(t, err)

rowsAffected, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)

eventID, err := res.LastInsertId()
require.NoError(t, err)
require.NotEqual(t, int64(0), eventID)

verifyEvent(t, s, eventID, e)

return eventID
}

func insertEventEntry(t *testing.T, s *SqliteIndexer, ee eventEntry) {
res, err := s.stmts.insertEventEntryStmt.Exec(ee.eventID, ee.indexed, ee.flags, ee.key, ee.codec, ee.value)
require.NoError(t, err)

rowsAffected, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)
}
107 changes: 0 additions & 107 deletions chain/index/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ import (
"time"

"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"

"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
)
Expand Down Expand Up @@ -80,41 +75,6 @@ func TestGetMsgInfo(t *testing.T) {
require.Equal(t, abi.ChainEpoch(1), mi.Epoch)
}

func setupWithHeadIndexed(t *testing.T, headHeight abi.ChainEpoch, rng *pseudo.Rand) (*SqliteIndexer, *types.TipSet, *dummyChainStore) {
head := fakeTipSet(t, rng, headHeight, []cid.Cid{})
d := newDummyChainStore()
d.SetHeaviestTipSet(head)

s, err := NewSqliteIndexer(":memory:", d, 0, false, 0)
require.NoError(t, err)
insertHead(t, s, head, headHeight)

return s, head, d
}

func insertHead(t *testing.T, s *SqliteIndexer, head *types.TipSet, height abi.ChainEpoch) {
headKeyBytes, err := toTipsetKeyCidBytes(head)
require.NoError(t, err)

insertTipsetMessage(t, s, tipsetMessage{
tipsetKeyCid: headKeyBytes,
height: uint64(height),
reverted: false,
messageCid: nil,
messageIndex: -1,
})
}

func insertEthTxHash(t *testing.T, s *SqliteIndexer, ethTxHash ethtypes.EthHash, messageCid cid.Cid) {
msgCidBytes := messageCid.Bytes()

res, err := s.stmts.insertEthTxHashStmt.Exec(ethTxHash.String(), msgCidBytes)
require.NoError(t, err)
rowsAffected, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)
}

type dummyChainStore struct {
mu sync.RWMutex

Expand Down Expand Up @@ -241,70 +201,3 @@ func (d *dummyChainStore) SetTipsetByHeightAndKey(h abi.ChainEpoch, tsk types.Ti
d.heightToTipSet[h] = ts
d.keyToTipSet[tsk] = ts
}

func randomIDAddr(tb testing.TB, rng *pseudo.Rand) address.Address {
tb.Helper()
addr, err := address.NewIDAddress(uint64(rng.Int63()))
require.NoError(tb, err)
return addr
}

func randomCid(tb testing.TB, rng *pseudo.Rand) cid.Cid {
tb.Helper()
cb := cid.V1Builder{Codec: cid.Raw, MhType: mh.IDENTITY}
c, err := cb.Sum(randomBytes(10, rng))
require.NoError(tb, err)
return c
}

func randomBytes(n int, rng *pseudo.Rand) []byte {
buf := make([]byte, n)
rng.Read(buf)
return buf
}

func fakeTipSet(tb testing.TB, rng *pseudo.Rand, h abi.ChainEpoch, parents []cid.Cid) *types.TipSet {
tb.Helper()
timeStamp := uint64(time.Now().Add(time.Duration(h) * builtin.EpochDurationSeconds * time.Second).Unix())

ts, err := types.NewTipSet([]*types.BlockHeader{
{
Height: h,
Miner: randomIDAddr(tb, rng),

Parents: parents,

Ticket: &types.Ticket{VRFProof: []byte{byte(h % 2)}},

ParentStateRoot: randomCid(tb, rng),
Messages: randomCid(tb, rng),
ParentMessageReceipts: randomCid(tb, rng),

BlockSig: &crypto.Signature{Type: crypto.SigTypeBLS},
BLSAggregate: &crypto.Signature{Type: crypto.SigTypeBLS},

Timestamp: timeStamp,
},
{
Height: h,
Miner: randomIDAddr(tb, rng),

Parents: parents,

Ticket: &types.Ticket{VRFProof: []byte{byte((h + 1) % 2)}},

ParentStateRoot: randomCid(tb, rng),
Messages: randomCid(tb, rng),
ParentMessageReceipts: randomCid(tb, rng),

BlockSig: &crypto.Signature{Type: crypto.SigTypeBLS},
BLSAggregate: &crypto.Signature{Type: crypto.SigTypeBLS},

Timestamp: timeStamp,
},
})

require.NoError(tb, err)

return ts
}

0 comments on commit 3495c30

Please sign in to comment.