Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge master to atomic base branch #744

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package atomic
package atomictest

import (
"testing"
Expand All @@ -9,21 +9,21 @@ import (
)

type SharedMemories struct {
thisChain atomic.SharedMemory
peerChain atomic.SharedMemory
ThisChain atomic.SharedMemory
PeerChain atomic.SharedMemory
thisChainID ids.ID
peerChainID ids.ID
}

func (s *SharedMemories) addItemsToBeRemovedToPeerChain(ops map[ids.ID]*atomic.Requests) error {
func (s *SharedMemories) AddItemsToBeRemovedToPeerChain(ops map[ids.ID]*atomic.Requests) error {
for _, reqs := range ops {
puts := make(map[ids.ID]*atomic.Requests)
puts[s.thisChainID] = &atomic.Requests{}
for _, key := range reqs.RemoveRequests {
val := []byte{0x1}
puts[s.thisChainID].PutRequests = append(puts[s.thisChainID].PutRequests, &atomic.Element{Key: key, Value: val})
}
if err := s.peerChain.Apply(puts); err != nil {
if err := s.PeerChain.Apply(puts); err != nil {
return err
}
}
Expand All @@ -35,7 +35,7 @@ func (s *SharedMemories) AssertOpsApplied(t *testing.T, ops map[ids.ID]*atomic.R
for _, reqs := range ops {
// should be able to get put requests
for _, elem := range reqs.PutRequests {
val, err := s.peerChain.Get(s.thisChainID, [][]byte{elem.Key})
val, err := s.PeerChain.Get(s.thisChainID, [][]byte{elem.Key})
if err != nil {
t.Fatalf("error finding puts in peerChainMemory: %s", err)
}
Expand All @@ -44,24 +44,24 @@ func (s *SharedMemories) AssertOpsApplied(t *testing.T, ops map[ids.ID]*atomic.R

// should not be able to get remove requests
for _, key := range reqs.RemoveRequests {
_, err := s.thisChain.Get(s.peerChainID, [][]byte{key})
_, err := s.ThisChain.Get(s.peerChainID, [][]byte{key})
assert.EqualError(t, err, "not found")
}
}
}

func (s *SharedMemories) assertOpsNotApplied(t *testing.T, ops map[ids.ID]*atomic.Requests) {
func (s *SharedMemories) AssertOpsNotApplied(t *testing.T, ops map[ids.ID]*atomic.Requests) {
t.Helper()
for _, reqs := range ops {
// should not be able to get put requests
for _, elem := range reqs.PutRequests {
_, err := s.peerChain.Get(s.thisChainID, [][]byte{elem.Key})
_, err := s.PeerChain.Get(s.thisChainID, [][]byte{elem.Key})
assert.EqualError(t, err, "not found")
}

// should be able to get remove requests (these were previously added as puts on peerChain)
for _, key := range reqs.RemoveRequests {
val, err := s.thisChain.Get(s.peerChainID, [][]byte{key})
val, err := s.ThisChain.Get(s.peerChainID, [][]byte{key})
assert.NoError(t, err)
assert.Equal(t, []byte{0x1}, val[0])
}
Expand All @@ -71,8 +71,8 @@ func (s *SharedMemories) assertOpsNotApplied(t *testing.T, ops map[ids.ID]*atomi
// TODO: once tests are moved to atomic package, unexport this function
func NewSharedMemories(atomicMemory *atomic.Memory, thisChainID, peerChainID ids.ID) *SharedMemories {
return &SharedMemories{
thisChain: atomicMemory.NewSharedMemory(thisChainID),
peerChain: atomicMemory.NewSharedMemory(peerChainID),
ThisChain: atomicMemory.NewSharedMemory(thisChainID),
PeerChain: atomicMemory.NewSharedMemory(peerChainID),
thisChainID: thisChainID,
peerChainID: peerChainID,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) 2020-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package atomic
package atomictest

import (
"math/big"
Expand All @@ -17,20 +17,21 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/plugin/evm/atomic"
)

const testCodecVersion = 0

var testTxCodec codec.Manager
var TestTxCodec codec.Manager

func init() {
testTxCodec = codec.NewDefaultManager()
TestTxCodec = codec.NewDefaultManager()
c := linearcodec.NewDefault()

errs := wrappers.Errs{}
errs.Add(
c.RegisterType(&TestUnsignedTx{}),
testTxCodec.RegisterCodec(testCodecVersion, c),
TestTxCodec.RegisterCodec(testCodecVersion, c),
)

if errs.Errored() {
Expand All @@ -52,7 +53,7 @@ type TestUnsignedTx struct {
EVMStateTransferV error
}

var _ UnsignedAtomicTx = &TestUnsignedTx{}
var _ atomic.UnsignedAtomicTx = &TestUnsignedTx{}

// GasUsed implements the UnsignedAtomicTx interface
func (t *TestUnsignedTx) GasUsed(fixedFee bool) (uint64, error) { return t.GasUsedV, nil }
Expand Down Expand Up @@ -84,19 +85,19 @@ func (t *TestUnsignedTx) SignedBytes() []byte { return t.SignedBytesV }
func (t *TestUnsignedTx) InputUTXOs() set.Set[ids.ID] { return t.InputUTXOsV }

// SemanticVerify implements the UnsignedAtomicTx interface
func (t *TestUnsignedTx) SemanticVerify(backend *VerifierBackend, stx *Tx, parent AtomicBlockContext, baseFee *big.Int) error {
func (t *TestUnsignedTx) SemanticVerify(backend *atomic.VerifierBackend, stx *atomic.Tx, parent atomic.AtomicBlockContext, baseFee *big.Int) error {
return t.SemanticVerifyV
}

// EVMStateTransfer implements the UnsignedAtomicTx interface
func (t *TestUnsignedTx) EVMStateTransfer(ctx *snow.Context, state StateDB) error {
func (t *TestUnsignedTx) EVMStateTransfer(ctx *snow.Context, state atomic.StateDB) error {
return t.EVMStateTransferV
}

var TestBlockchainID = ids.GenerateTestID()

func GenerateTestImportTxWithGas(gasUsed uint64, burned uint64) *Tx {
return &Tx{
func GenerateTestImportTxWithGas(gasUsed uint64, burned uint64) *atomic.Tx {
return &atomic.Tx{
UnsignedAtomicTx: &TestUnsignedTx{
IDV: ids.GenerateTestID(),
GasUsedV: gasUsed,
Expand All @@ -112,8 +113,8 @@ func GenerateTestImportTxWithGas(gasUsed uint64, burned uint64) *Tx {
}
}

func GenerateTestImportTx() *Tx {
return &Tx{
func GenerateTestImportTx() *atomic.Tx {
return &atomic.Tx{
UnsignedAtomicTx: &TestUnsignedTx{
IDV: ids.GenerateTestID(),
AcceptRequestsBlockchainIDV: TestBlockchainID,
Expand All @@ -127,8 +128,8 @@ func GenerateTestImportTx() *Tx {
}
}

func GenerateTestExportTx() *Tx {
return &Tx{
func GenerateTestExportTx() *atomic.Tx {
return &atomic.Tx{
UnsignedAtomicTx: &TestUnsignedTx{
IDV: ids.GenerateTestID(),
AcceptRequestsBlockchainIDV: TestBlockchainID,
Expand All @@ -148,7 +149,7 @@ func GenerateTestExportTx() *Tx {
}
}

func NewTestTx() *Tx {
func NewTestTx() *atomic.Tx {
txType := rand.Intn(2)
switch txType {
case 0:
Expand All @@ -160,8 +161,8 @@ func NewTestTx() *Tx {
}
}

func NewTestTxs(numTxs int) []*Tx {
txs := make([]*Tx, 0, numTxs)
func NewTestTxs(numTxs int) []*atomic.Tx {
txs := make([]*atomic.Tx, 0, numTxs)
for i := 0; i < numTxs; i++ {
txs = append(txs, NewTestTx())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// (c) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package atomic
package atomictest

import (
avalancheatomic "github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/coreth/plugin/evm/atomic"
)

func ConvertToAtomicOps(tx *Tx) (map[ids.ID]*avalancheatomic.Requests, error) {
func ConvertToAtomicOps(tx *atomic.Tx) (map[ids.ID]*avalancheatomic.Requests, error) {
id, reqs, err := tx.AtomicOps()
if err != nil {
return nil, err
Expand Down
81 changes: 0 additions & 81 deletions plugin/evm/atomic/gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ package atomic
import (
"testing"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)

Expand All @@ -37,81 +34,3 @@ func TestGossipAtomicTxMarshaller(t *testing.T) {
require.NoError(err)
require.Equal(want.GossipID(), got.GossipID())
}

func TestAtomicMempoolIterate(t *testing.T) {
txs := []*GossipAtomicTx{
{
Tx: &Tx{
UnsignedAtomicTx: &TestUnsignedTx{
IDV: ids.GenerateTestID(),
},
},
},
{
Tx: &Tx{
UnsignedAtomicTx: &TestUnsignedTx{
IDV: ids.GenerateTestID(),
},
},
},
}

tests := []struct {
name string
add []*GossipAtomicTx
f func(tx *GossipAtomicTx) bool
expectedTxs []*GossipAtomicTx
}{
{
name: "func matches nothing",
add: txs,
f: func(*GossipAtomicTx) bool {
return false
},
expectedTxs: []*GossipAtomicTx{},
},
{
name: "func matches all",
add: txs,
f: func(*GossipAtomicTx) bool {
return true
},
expectedTxs: txs,
},
{
name: "func matches subset",
add: txs,
f: func(tx *GossipAtomicTx) bool {
return tx.Tx == txs[0].Tx
},
expectedTxs: []*GossipAtomicTx{txs[0]},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)
m, err := NewMempool(&snow.Context{}, prometheus.NewRegistry(), 10, nil)
require.NoError(err)

for _, add := range tt.add {
require.NoError(m.Add(add))
}

matches := make([]*GossipAtomicTx, 0)
f := func(tx *GossipAtomicTx) bool {
match := tt.f(tx)

if match {
matches = append(matches, tx)
}

return match
}

m.Iterate(f)

require.ElementsMatch(tt.expectedTxs, matches)
})
}
}
56 changes: 0 additions & 56 deletions plugin/evm/atomic/mempool_test.go

This file was deleted.

Loading
Loading