Skip to content

Commit

Permalink
refactor(all): rename SignedPayload to Transaction (Argus-Labs#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
technicallyty committed Nov 6, 2023
1 parent 607513e commit e188199
Show file tree
Hide file tree
Showing 44 changed files with 353 additions and 1,580 deletions.
9 changes: 0 additions & 9 deletions .gitmodules

This file was deleted.

16 changes: 8 additions & 8 deletions cardinal/ecs/chain_recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"sort"
"testing"

shardv1 "buf.build/gen/go/argus-labs/world-engine/protocolbuffers/go/shard/v1"
"google.golang.org/protobuf/proto"
"gotest.tools/v3/assert"
shardv1 "pkg.world.dev/world-engine/rift/shard/v1"

"github.com/cometbft/cometbft/libs/rand"
"pkg.world.dev/world-engine/cardinal/ecs"
Expand All @@ -23,8 +23,8 @@ type DummyAdapter struct {
txs map[uint64][]*types.Transaction
}

func (d *DummyAdapter) Submit(_ context.Context, p *sign.SignedPayload, txID, tick uint64) error {
sp := &shardv1.SignedPayload{
func (d *DummyAdapter) Submit(_ context.Context, p *sign.Transaction, txID, tick uint64) error {
sp := &shardv1.Transaction{
PersonaTag: p.PersonaTag,
Namespace: p.Namespace,
Nonce: p.Nonce,
Expand All @@ -39,8 +39,8 @@ func (d *DummyAdapter) Submit(_ context.Context, p *sign.SignedPayload, txID, ti
d.txs[tick] = make([]*types.Transaction, 0)
}
d.txs[tick] = append(d.txs[tick], &types.Transaction{
TxId: txID,
SignedPayload: bz,
TxId: txID,
GameShardTransaction: bz,
})
return nil
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestWorld_RecoverFromChain(t *testing.T) {
return nil
})
namespace := "game1"
payloads := make([]*sign.SignedPayload, 0, 10)
payloads := make([]*sign.Transaction, 0, 10)
var finalTick uint64 = 20
for i := 0; i <= 10; i++ {
payload := generateRandomTransaction(t, namespace, sendEnergyTx)
Expand All @@ -126,15 +126,15 @@ func TestWorld_RecoverFromChain(t *testing.T) {
}

func generateRandomTransaction(t *testing.T, ns string, tx *ecs.TransactionType[SendEnergyTransaction,
SendEnergyTransactionResponse]) *sign.SignedPayload {
SendEnergyTransactionResponse]) *sign.Transaction {
tx1 := SendEnergyTransaction{
To: rand.Str(5),
From: rand.Str(4),
Amount: rand.Uint64(),
}
bz, err := tx.Encode(tx1)
assert.NilError(t, err)
return &sign.SignedPayload{
return &sign.Transaction{
PersonaTag: rand.Str(5),
Namespace: ns,
Nonce: rand.Uint64(),
Expand Down
2 changes: 1 addition & 1 deletion cardinal/ecs/ecb/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type pendingTransaction struct {
TypeID transaction.TypeID
TxHash transaction.TxHash
Data []byte
Sig *sign.SignedPayload
Sig *sign.Transaction
}

func addPendingTransactionToPipe(ctx context.Context, pipe redis.Pipeliner, txs []transaction.ITransaction,
Expand Down
4 changes: 2 additions & 2 deletions cardinal/ecs/internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ func init() {
}
}

func UniqueSignature(t *testing.T) *sign.SignedPayload {
func UniqueSignature(t *testing.T) *sign.Transaction {
nonce++
sig, err := sign.NewSignedPayload(privateKey, "some-persona-tag", "namespace", nonce, `{"some":"data"}`)
sig, err := sign.NewTransaction(privateKey, "some-persona-tag", "namespace", nonce, `{"some":"data"}`)
assert.NilError(t, err)
return sig
}
2 changes: 1 addition & 1 deletion cardinal/ecs/persona_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestCanAuthorizeAddress(t *testing.T) {
wantAddr := "0xfoobar"
ecs.AuthorizePersonaAddressTx.AddToQueue(world, ecs.AuthorizePersonaAddress{
Address: wantAddr,
}, &sign.SignedPayload{PersonaTag: wantTag})
}, &sign.Transaction{PersonaTag: wantTag})
// PersonaTag registration doesn't take place until the relevant system is run during a game tick.
assert.NilError(t, world.Tick(context.Background()))

Expand Down
8 changes: 4 additions & 4 deletions cardinal/ecs/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ func (t *TransactionType[In, Out]) ID() transaction.TypeID {
return t.id
}

var emptySignature = &sign.SignedPayload{}
var emptySignature = &sign.Transaction{}

// AddToQueue adds a transaction with the given data to the world object. The transaction will be executed
// at the next game tick. An optional sign.SignedPayload can be associated with this transaction.
func (t *TransactionType[In, Out]) AddToQueue(world *World, data In, sigs ...*sign.SignedPayload) transaction.TxHash {
// at the next game tick. An optional sign.Transaction can be associated with this transaction.
func (t *TransactionType[In, Out]) AddToQueue(world *World, data In, sigs ...*sign.Transaction) transaction.TxHash {
sig := emptySignature
if len(sigs) > 0 {
sig = sigs[0]
Expand All @@ -126,7 +126,7 @@ func (t *TransactionType[In, Out]) SetID(id transaction.TypeID) error {
type TxData[In any] struct {
TxHash transaction.TxHash
Value In
Sig *sign.SignedPayload
Sig *sign.Transaction
}

func (t *TransactionType[In, Out]) AddError(wCtx WorldContext, hash transaction.TxHash, err error) {
Expand Down
8 changes: 4 additions & 4 deletions cardinal/ecs/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ func (t *TxQueue) GetEVMTxs() []TxAny {
return transactions
}

func (t *TxQueue) AddTransaction(id TypeID, v any, sig *sign.SignedPayload) TxHash {
func (t *TxQueue) AddTransaction(id TypeID, v any, sig *sign.Transaction) TxHash {
return t.addTransaction(id, v, sig, "")
}

func (t *TxQueue) AddEVMTransaction(id TypeID, v any, sig *sign.SignedPayload, evmTxHash string) TxHash {
func (t *TxQueue) AddEVMTransaction(id TypeID, v any, sig *sign.Transaction, evmTxHash string) TxHash {
return t.addTransaction(id, v, sig, evmTxHash)
}

func (t *TxQueue) addTransaction(id TypeID, v any, sig *sign.SignedPayload, evmTxHash string) TxHash {
func (t *TxQueue) addTransaction(id TypeID, v any, sig *sign.Transaction, evmTxHash string) TxHash {
t.mux.Lock()
defer t.mux.Unlock()
txHash := TxHash(sig.HashHex())
Expand Down Expand Up @@ -88,7 +88,7 @@ type TxAny struct {
TxID TypeID
Value any
TxHash TxHash
Sig *sign.SignedPayload
Sig *sign.Transaction
// EVMSourceTxHash is the tx hash of the EVM tx that triggered this tx.
EVMSourceTxHash string
}
Expand Down
4 changes: 2 additions & 2 deletions cardinal/ecs/transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ func TestCopyTransactions(t *testing.T) {
X int
}
txq := transaction.NewTxQueue()
txq.AddTransaction(1, FooTx{X: 3}, &sign.SignedPayload{PersonaTag: "foo"})
txq.AddTransaction(2, FooTx{X: 4}, &sign.SignedPayload{PersonaTag: "bar"})
txq.AddTransaction(1, FooTx{X: 3}, &sign.Transaction{PersonaTag: "foo"})
txq.AddTransaction(2, FooTx{X: 4}, &sign.Transaction{PersonaTag: "bar"})

copyTxq := txq.CopyTransactions()
assert.Equal(t, copyTxq.GetAmountOfTxs(), 2)
Expand Down
18 changes: 9 additions & 9 deletions cardinal/ecs/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"sync/atomic"
"time"

shardv1 "buf.build/gen/go/argus-labs/world-engine/protocolbuffers/go/shard/v1"
"google.golang.org/protobuf/proto"
shardv1 "pkg.world.dev/world-engine/rift/shard/v1"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -297,7 +297,7 @@ func (w *World) ConsumeEVMTxResult(evmTxHash string) (EVMTxReceipt, bool) {
// AddTransaction adds a transaction to the transaction queue. This should not be used directly.
// Instead, use a TransactionType.AddToQueue to ensure type consistency. Returns the tick this transaction will be
// executed in.
func (w *World) AddTransaction(id transaction.TypeID, v any, sig *sign.SignedPayload) (
func (w *World) AddTransaction(id transaction.TypeID, v any, sig *sign.Transaction) (
tick uint64, txHash transaction.TxHash,
) {
// TODO: There's no locking between getting the tick and adding the transaction, so there's no guarantee that this
Expand All @@ -307,7 +307,7 @@ func (w *World) AddTransaction(id transaction.TypeID, v any, sig *sign.SignedPay
return tick, txHash
}

func (w *World) AddEVMTransaction(id transaction.TypeID, v any, sig *sign.SignedPayload, evmTxHash string) (
func (w *World) AddEVMTransaction(id transaction.TypeID, v any, sig *sign.Transaction, evmTxHash string) (
tick uint64, txHash transaction.TxHash,
) {
tick = w.CurrentTick()
Expand Down Expand Up @@ -596,7 +596,7 @@ func (w *World) RecoverFromChain(ctx context.Context) error {
// we've now reached target. we need to inject the transactions and tick.
transactions := tickedTxs.Txs
for _, tx := range transactions {
sp, err := w.decodeTransaction(tx.SignedPayload)
sp, err := w.decodeTransaction(tx.GameShardTransaction)
if err != nil {
return err
}
Expand All @@ -608,7 +608,7 @@ func (w *World) RecoverFromChain(ctx context.Context) error {
if err != nil {
return err
}
w.AddTransaction(transaction.TypeID(tx.TxId), v, w.protoSignedPayloadToGo(sp))
w.AddTransaction(transaction.TypeID(tx.TxId), v, w.protoTransactionToGo(sp))
}
// run the tick for this batch
if err = w.Tick(ctx); err != nil {
Expand All @@ -631,8 +631,8 @@ func (w *World) RecoverFromChain(ctx context.Context) error {
return nil
}

func (w *World) protoSignedPayloadToGo(sp *shardv1.SignedPayload) *sign.SignedPayload {
return &sign.SignedPayload{
func (w *World) protoTransactionToGo(sp *shardv1.Transaction) *sign.Transaction {
return &sign.Transaction{
PersonaTag: sp.PersonaTag,
Namespace: sp.Namespace,
Nonce: sp.Nonce,
Expand All @@ -641,8 +641,8 @@ func (w *World) protoSignedPayloadToGo(sp *shardv1.SignedPayload) *sign.SignedPa
}
}

func (w *World) decodeTransaction(bz []byte) (*shardv1.SignedPayload, error) {
payload := new(shardv1.SignedPayload)
func (w *World) decodeTransaction(bz []byte) (*shardv1.Transaction, error) {
payload := new(shardv1.Transaction)
err := proto.Unmarshal(bz, payload)
return payload, err
}
Expand Down
4 changes: 2 additions & 2 deletions cardinal/ecs/world_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestEVMTxConsume(t *testing.T) {

// add tx to queue
evmTxHash := "0xFooBar"
w.AddEVMTransaction(fooTx.ID(), FooIn{X: 32}, &sign.SignedPayload{PersonaTag: "foo"}, evmTxHash)
w.AddEVMTransaction(fooTx.ID(), FooIn{X: 32}, &sign.Transaction{PersonaTag: "foo"}, evmTxHash)

// let's check against a system that returns a result and no error
returnVal = FooOut{Y: "hi"}
Expand All @@ -70,7 +70,7 @@ func TestEVMTxConsume(t *testing.T) {
// lets check against a system that returns an error
returnVal = FooOut{}
returnErr = errors.New("omg error")
w.AddEVMTransaction(fooTx.ID(), FooIn{X: 32}, &sign.SignedPayload{PersonaTag: "foo"}, evmTxHash)
w.AddEVMTransaction(fooTx.ID(), FooIn{X: 32}, &sign.Transaction{PersonaTag: "foo"}, evmTxHash)
assert.NilError(t, w.Tick(ctx))
evmTxReceipt, ok = w.ConsumeEVMTxResult(evmTxHash)

Expand Down
2 changes: 1 addition & 1 deletion cardinal/evm/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (s *msgServerImpl) SendMessage(_ context.Context, msg *routerv1.SendMessage

// since we are injecting the tx directly, all we need is the persona tag in the signed payload.
// the sig checking happens in the server's Handler, not in ecs.World.
sig := &sign.SignedPayload{PersonaTag: sc.PersonaTag}
sig := &sign.Transaction{PersonaTag: sc.PersonaTag}
s.world.AddEVMTransaction(itx.ID(), tx, sig, msg.EvmTxHash)

// wait for the next tick so the tx gets processed
Expand Down
2 changes: 1 addition & 1 deletion cardinal/evm/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestServer_SendMessage(t *testing.T) {
})
ecs.AuthorizePersonaAddressTx.AddToQueue(w, ecs.AuthorizePersonaAddress{
Address: sender,
}, &sign.SignedPayload{PersonaTag: personaTag})
}, &sign.Transaction{PersonaTag: personaTag})
err := w.Tick(context.Background())
assert.NilError(t, err)

Expand Down
12 changes: 4 additions & 8 deletions cardinal/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ replace (
)

require (
buf.build/gen/go/argus-labs/world-engine/grpc/go v1.3.0-20230808004839-11a21a99bf62.1
buf.build/gen/go/argus-labs/world-engine/protocolbuffers/go v1.31.0-20230808004839-11a21a99bf62.1
github.com/alecthomas/participle/v2 v2.1.0
github.com/alicebob/miniredis/v2 v2.30.5
github.com/cometbft/cometbft v0.38.0-rc3
github.com/ethereum/go-ethereum v1.12.0
github.com/go-openapi/loads v0.21.2
github.com/go-openapi/runtime v0.26.0
github.com/google/uuid v1.3.0
github.com/google/uuid v1.3.1
github.com/gorilla/websocket v1.5.0
github.com/invopop/jsonschema v0.7.0
github.com/mitchellh/mapstructure v1.5.0
Expand All @@ -32,9 +30,9 @@ require (
google.golang.org/grpc v1.58.3
google.golang.org/protobuf v1.31.0
gotest.tools/v3 v3.5.1
pkg.world.dev/world-engine/chain v0.1.11-alpha
pkg.world.dev/world-engine/rift v0.0.5-0.20231101203803-499e27c9ed1a
pkg.world.dev/world-engine/sign v0.1.9-alpha
pkg.world.dev/world-engine/chain v0.1.12-alpha
pkg.world.dev/world-engine/rift v0.0.5
pkg.world.dev/world-engine/sign v0.1.10-alpha
)

require (
Expand Down Expand Up @@ -73,7 +71,6 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emicklei/dot v1.6.0 // indirect
github.com/getsentry/sentry-go v0.23.0 // indirect
github.com/go-kit/kit v0.13.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
Expand Down Expand Up @@ -115,7 +112,6 @@ require (
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.27.10 // indirect
github.com/petermattis/goid v0.0.0-20230808133559-b036b712a89b // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down
Loading

0 comments on commit e188199

Please sign in to comment.