Skip to content

Commit

Permalink
Merge branch 'main' into tyler/world-471-evm-base-shard-start-up-scri…
Browse files Browse the repository at this point in the history
…pt-dx
  • Loading branch information
technicallyty committed Nov 9, 2023
2 parents 3854cfd + dee26dd commit 1d2c537
Show file tree
Hide file tree
Showing 68 changed files with 1,248 additions and 1,959 deletions.
21 changes: 13 additions & 8 deletions cardinal/cardinal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ func TestCanQueryInsideSystem(t *testing.T) {
world, doTick := testutils.MakeWorldAndTicker(t, cardinal.WithCORS())
assert.NilError(t, cardinal.RegisterComponent[Foo](world))
wantNumOfEntities := 10
world.Init(func(worldCtx cardinal.WorldContext) {
_, err := cardinal.CreateMany(worldCtx, wantNumOfEntities, Foo{})
assert.NilError(t, err)
})
wCtx := cardinal.TestingWorldToWorldContext(world)
_, err := cardinal.CreateMany(wCtx, wantNumOfEntities, Foo{})
assert.NilError(t, err)
gotNumOfEntities := 0
cardinal.RegisterSystems(world, func(worldCtx cardinal.WorldContext) error {
q, err := worldCtx.NewSearch(cardinal.Exact(Foo{}))
Expand All @@ -50,7 +49,7 @@ func TestCanQueryInsideSystem(t *testing.T) {

doTick()
assert.Equal(t, world.CurrentTick(), uint64(1))
err := world.ShutDown()
err = world.ShutDown()
assert.Assert(t, err)
assert.Equal(t, gotNumOfEntities, wantNumOfEntities)
}
Expand All @@ -63,9 +62,12 @@ func TestShutdownViaSignal(t *testing.T) {
assert.NilError(t, cardinal.RegisterComponent[Foo](world))
assert.NilError(t, err)
wantNumOfEntities := 10
world.Init(func(worldCtx cardinal.WorldContext) {
_, err := cardinal.CreateMany(worldCtx, wantNumOfEntities, Foo{})
assert.NilError(t, err)
world.Init(func(worldCtx cardinal.WorldContext) error {
_, err := cardinal.CreateMany(worldCtx, wantNumOfEntities/2, Foo{})
if err != nil {
return err
}
return nil
})
wg.Add(1)
go func() {
Expand All @@ -77,6 +79,9 @@ func TestShutdownViaSignal(t *testing.T) {
// wait until game loop is running
time.Sleep(500 * time.Millisecond)
}
wCtx := cardinal.TestingWorldToWorldContext(world)
_, err = cardinal.CreateMany(wCtx, wantNumOfEntities/2, Foo{})
assert.NilError(t, err)
// test CORS with cardinal
client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, "http://localhost:4040/query/http/endpoints", nil)
Expand Down
17 changes: 8 additions & 9 deletions cardinal/ecs/chain_recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ecs_test
import (
"context"
"encoding/binary"
"pkg.world.dev/world-engine/cardinal/ecs/message"
"sort"
"testing"

Expand Down Expand Up @@ -77,12 +78,12 @@ func (d *DummyAdapter) QueryTransactions(_ context.Context, request *types.Query
}, nil
}

type SendEnergyTransaction struct {
type SendEnergyMsg struct {
To, From string
Amount uint64
}

type SendEnergyTransactionResponse struct{}
type SendEnergyResult struct{}

// TestWorld_RecoverFromChain tests that after submitting transactions to the chain, they can be queried, re-ran,
// and end up with the same game state as before.
Expand All @@ -91,8 +92,8 @@ func TestWorld_RecoverFromChain(t *testing.T) {
ctx := context.Background()
adapter := &DummyAdapter{txs: make(map[uint64][]*types.Transaction, 0)}
w := ecs.NewTestWorld(t, ecs.WithAdapter(adapter))
sendEnergyTx := ecs.NewTransactionType[SendEnergyTransaction, SendEnergyTransactionResponse]("send_energy")
err := w.RegisterTransactions(sendEnergyTx)
sendEnergyTx := ecs.NewMessageType[SendEnergyMsg, SendEnergyResult]("send_energy")
err := w.RegisterMessages(sendEnergyTx)
assert.NilError(t, err)

sysRuns := uint64(0)
Expand Down Expand Up @@ -125,14 +126,13 @@ func TestWorld_RecoverFromChain(t *testing.T) {
assert.Equal(t, len(payloads), timesSendEnergyRan)
}

func generateRandomTransaction(t *testing.T, ns string, tx *ecs.TransactionType[SendEnergyTransaction,
SendEnergyTransactionResponse]) *sign.Transaction {
tx1 := SendEnergyTransaction{
func generateRandomTransaction(t *testing.T, ns string, msg message.Message) *sign.Transaction {
tx1 := SendEnergyMsg{
To: rand.Str(5),
From: rand.Str(4),
Amount: rand.Uint64(),
}
bz, err := tx.Encode(tx1)
bz, err := msg.Encode(tx1)
assert.NilError(t, err)
return &sign.Transaction{
PersonaTag: rand.Str(5),
Expand All @@ -144,7 +144,6 @@ func generateRandomTransaction(t *testing.T, ns string, tx *ecs.TransactionType[
}

func TestWorld_RecoverShouldErrorIfTickExists(t *testing.T) {
// setup world and transactions
ctx := context.Background()
adapter := &DummyAdapter{}
w := ecs.NewTestWorld(t, ecs.WithAdapter(adapter))
Expand Down
2 changes: 1 addition & 1 deletion cardinal/ecs/cql/cql.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (t *cqlTerm) String() string {

var internalCQLParser = participle.MustBuild[cqlTerm]()

// TODO: Value is sum type is represented as a product type. There is a case where multiple properties are filled out.
// TODO: Msg is sum type is represented as a product type. There is a case where multiple properties are filled out.
// Only one property may not be nil, The parser should prevent this from happening but for safety this should eventually
// be checked.
func valueToComponentFilter(value *cqlValue, stringToComponent func(string) (metadata.ComponentMetadata, error)) (
Expand Down
26 changes: 13 additions & 13 deletions cardinal/ecs/ecb/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package ecb
import (
"context"
"errors"
"pkg.world.dev/world-engine/cardinal/ecs/message"

"github.com/redis/go-redis/v9"
"pkg.world.dev/world-engine/cardinal/ecs/codec"
"pkg.world.dev/world-engine/cardinal/ecs/store"
"pkg.world.dev/world-engine/cardinal/ecs/transaction"
"pkg.world.dev/world-engine/sign"
)

Expand Down Expand Up @@ -38,7 +38,7 @@ func (m *Manager) GetTickNumbers() (start, end uint64, err error) {

// StartNextTick saves the given transactions to the DB and sets the tick trackers to indicate we are in the middle
// of a tick. While transactions are saved to the DB, no state changes take place at this time.
func (m *Manager) StartNextTick(txs []transaction.ITransaction, queue *transaction.TxQueue) error {
func (m *Manager) StartNextTick(txs []message.Message, queue *message.TxQueue) error {
ctx := context.Background()
pipe := m.client.TxPipeline()
if err := addPendingTransactionToPipe(ctx, pipe, txs, queue); err != nil {
Expand Down Expand Up @@ -70,7 +70,7 @@ func (m *Manager) FinalizeTick() error {

// Recover fetches the pending transactions for an incomplete tick. This should only be called if GetTickNumbers
// indicates that the previous tick was started, but never completed.
func (m *Manager) Recover(txs []transaction.ITransaction) (*transaction.TxQueue, error) {
func (m *Manager) Recover(txs []message.Message) (*message.TxQueue, error) {
ctx := context.Background()
key := redisPendingTransactionKey()
bz, err := m.client.Get(ctx, key).Bytes()
Expand All @@ -81,45 +81,45 @@ func (m *Manager) Recover(txs []transaction.ITransaction) (*transaction.TxQueue,
if err != nil {
return nil, err
}
idToTx := map[transaction.TypeID]transaction.ITransaction{}
idToTx := map[message.TypeID]message.Message{}
for _, tx := range txs {
idToTx[tx.ID()] = tx
}

txQueue := transaction.NewTxQueue()
txQueue := message.NewTxQueue()
for _, p := range pending {
tx := idToTx[p.TypeID]
var txData any
txData, err = tx.Decode(p.Data)
if err != nil {
return nil, err
}
txQueue.AddTransaction(tx.ID(), txData, p.Sig)
txQueue.AddTransaction(tx.ID(), txData, p.Tx)
}
return txQueue, nil
}

type pendingTransaction struct {
TypeID transaction.TypeID
TxHash transaction.TxHash
TypeID message.TypeID
TxHash message.TxHash
Data []byte
Sig *sign.Transaction
Tx *sign.Transaction
}

func addPendingTransactionToPipe(ctx context.Context, pipe redis.Pipeliner, txs []transaction.ITransaction,
queue *transaction.TxQueue) error {
func addPendingTransactionToPipe(ctx context.Context, pipe redis.Pipeliner, txs []message.Message,
queue *message.TxQueue) error {
var pending []pendingTransaction
for _, tx := range txs {
currList := queue.ForID(tx.ID())
for _, txData := range currList {
buf, err := tx.Encode(txData.Value)
buf, err := tx.Encode(txData.Msg)
if err != nil {
return err
}
currItem := pendingTransaction{
TypeID: tx.ID(),
TxHash: txData.TxHash,
Sig: txData.Sig,
Tx: txData.Tx,
Data: buf,
}
pending = append(pending, currItem)
Expand Down
26 changes: 13 additions & 13 deletions cardinal/ecs/ecb/tick_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package ecb_test

import (
"pkg.world.dev/world-engine/cardinal/ecs/message"
"testing"

"gotest.tools/v3/assert"
"pkg.world.dev/world-engine/cardinal/ecs"
"pkg.world.dev/world-engine/cardinal/ecs/internal/testutil"
"pkg.world.dev/world-engine/cardinal/ecs/transaction"
)

func TestCanSaveAndRecoverTransactions(t *testing.T) {
type TxIn struct {
type MsgIn struct {
Value int
}
type TxOut struct {
type MsgOut struct {
Value int
}

txAlpha := ecs.NewTransactionType[TxIn, TxOut]("alpha")
txBeta := ecs.NewTransactionType[TxIn, TxOut]("beta")
assert.NilError(t, txAlpha.SetID(16))
assert.NilError(t, txBeta.SetID(32))
txs := []transaction.ITransaction{txAlpha, txBeta}
msgAlpha := ecs.NewMessageType[MsgIn, MsgOut]("alpha")
msgBeta := ecs.NewMessageType[MsgIn, MsgOut]("beta")
assert.NilError(t, msgAlpha.SetID(16))
assert.NilError(t, msgBeta.SetID(32))
msgs := []message.Message{msgAlpha, msgBeta}

manager, client := newCmdBufferAndRedisClientForTest(t, nil)
originalQueue := transaction.NewTxQueue()
originalQueue := message.NewTxQueue()
sig := testutil.UniqueSignature(t)
_ = originalQueue.AddTransaction(txAlpha.ID(), TxIn{100}, sig)
_ = originalQueue.AddTransaction(msgAlpha.ID(), MsgIn{100}, sig)

assert.NilError(t, manager.StartNextTick(txs, originalQueue))
assert.NilError(t, manager.StartNextTick(msgs, originalQueue))

// Pretend some problem was encountered here. Make sure we can recover the transactions from redis.
manager, _ = newCmdBufferAndRedisClientForTest(t, client)
gotQueue, err := manager.Recover(txs)
gotQueue, err := manager.Recover(msgs)
assert.NilError(t, err)

assert.Equal(t, gotQueue.GetAmountOfTxs(), originalQueue.GetAmountOfTxs())

// Make sure we can finalize the tick
assert.NilError(t, manager.StartNextTick(txs, gotQueue))
assert.NilError(t, manager.StartNextTick(msgs, gotQueue))
assert.NilError(t, manager.FinalizeTick())
}

Expand Down
4 changes: 2 additions & 2 deletions cardinal/ecs/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func TestWorldLogger(t *testing.T) {
&bufLogger,
}
w.InjectLogger(&cardinalLogger)
alphaTx := ecs.NewTransactionType[SendEnergyTx, SendEnergyTxResult]("alpha")
assert.NilError(t, w.RegisterTransactions(alphaTx))
alphaTx := ecs.NewMessageType[SendEnergyTx, SendEnergyTxResult]("alpha")
assert.NilError(t, w.RegisterMessages(alphaTx))
assert.NilError(t, ecs.RegisterComponent[EnergyComp](w))
cardinalLogger.LogWorld(w, zerolog.InfoLevel)
jsonWorldInfoString := `{
Expand Down
Loading

0 comments on commit 1d2c537

Please sign in to comment.