-
Notifications
You must be signed in to change notification settings - Fork 127
tests: init pre-funded local authFactories and URIs usage #1814
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
7d4e45d
7801685
cbb5a7f
09f80be
882baaa
601348e
d34c08d
42d6ce8
ae81fde
578dded
da093a5
9515e1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,8 @@ import ( | |
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/hypersdk/auth" | ||
"github.com/ava-labs/hypersdk/api/jsonrpc" | ||
"github.com/ava-labs/hypersdk/chain" | ||
"github.com/ava-labs/hypersdk/crypto/ed25519" | ||
"github.com/ava-labs/hypersdk/examples/morpheusvm/actions" | ||
"github.com/ava-labs/hypersdk/tests/registry" | ||
|
||
|
@@ -24,23 +23,31 @@ import ( | |
// ref https://onsi.github.io/ginkgo/#mental-model-how-ginkgo-traverses-the-spec-hierarchy | ||
var TestsRegistry = ®istry.Registry{} | ||
|
||
var _ = registry.Register(TestsRegistry, "Transfer Transaction", func(t ginkgo.FullGinkgoTInterface, tn tworkload.TestNetwork) { | ||
require := require.New(t) | ||
other, err := ed25519.GeneratePrivateKey() | ||
require.NoError(err) | ||
toAddress := auth.NewED25519Address(other.PublicKey()) | ||
|
||
authFactory := tn.Configuration().AuthFactories()[0] | ||
tx, err := tn.GenerateTx(context.Background(), []chain.Action{&actions.Transfer{ | ||
To: toAddress, | ||
Value: 1, | ||
}}, | ||
authFactory, | ||
) | ||
require.NoError(err) | ||
|
||
timeoutCtx, timeoutCtxFnc := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) | ||
defer timeoutCtxFnc() | ||
|
||
require.NoError(tn.ConfirmTxs(timeoutCtx, []*chain.Transaction{tx})) | ||
}) | ||
var _ = registry.Register(TestsRegistry, "Transfer Transaction", | ||
func(t ginkgo.FullGinkgoTInterface, tn tworkload.TestNetwork, authFactories ...chain.AuthFactory) { | ||
require := require.New(t) | ||
ctx := context.Background() | ||
targetFactory := authFactories[0] | ||
|
||
authFactory := tn.Configuration().AuthFactories()[0] | ||
|
||
client := jsonrpc.NewJSONRPCClient(tn.URIs()[0]) | ||
balance, err := client.GetBalance(ctx, targetFactory.Address()) | ||
require.NoError(err) | ||
require.Equal(uint64(1000), balance) | ||
|
||
tx, err := tn.GenerateTx(ctx, []chain.Action{&actions.Transfer{ | ||
To: targetFactory.Address(), | ||
Value: 1, | ||
}}, | ||
|
||
authFactory, | ||
) | ||
require.NoError(err) | ||
|
||
timeoutCtx, timeoutCtxFnc := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) | ||
defer timeoutCtxFnc() | ||
require.NoError(tn.ConfirmTxs(timeoutCtx, []*chain.Transaction{tx})) | ||
|
||
balance, err = client.GetBalance(ctx, targetFactory.Address()) | ||
require.NoError(err) | ||
require.Equal(uint64(1001), balance) | ||
}, 1000) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
"github.com/ava-labs/hypersdk/codec" | ||
"github.com/ava-labs/hypersdk/crypto/ed25519" | ||
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts" | ||
"github.com/ava-labs/hypersdk/examples/morpheusvm/tests" | ||
"github.com/ava-labs/hypersdk/examples/morpheusvm/vm" | ||
"github.com/ava-labs/hypersdk/fees" | ||
"github.com/ava-labs/hypersdk/genesis" | ||
|
@@ -32,7 +33,7 @@ var ed25519HexKeys = []string{ | |
"8a7be2e0c9a2d09ac2861c34326d6fe5a461d920ba9c2b345ae28e603d517df148735063f8d5d8ba79ea4668358943e5c80bc09e9b2b9a15b5b15db6c1862e88", //nolint:lll | ||
} | ||
|
||
func newGenesis(authFactories []chain.AuthFactory, minBlockGap time.Duration) *genesis.DefaultGenesis { | ||
func newGenesis(authFactories []chain.AuthFactory, testsLocalAllocations []*genesis.CustomAllocation, minBlockGap time.Duration) *genesis.DefaultGenesis { | ||
// allocate the initial balance to the addresses | ||
customAllocs := make([]*genesis.CustomAllocation, 0, len(authFactories)) | ||
for _, authFactory := range authFactories { | ||
|
@@ -41,6 +42,7 @@ func newGenesis(authFactories []chain.AuthFactory, minBlockGap time.Duration) *g | |
Balance: InitialBalance, | ||
}) | ||
} | ||
customAllocs = append(customAllocs, testsLocalAllocations...) | ||
|
||
genesis := genesis.NewDefaultGenesis(customAllocs) | ||
|
||
|
@@ -72,7 +74,17 @@ func newDefaultAuthFactories() []chain.AuthFactory { | |
|
||
func NewTestNetworkConfig(minBlockGap time.Duration) (workload.DefaultTestNetworkConfiguration, error) { | ||
keys := newDefaultAuthFactories() | ||
genesis := newGenesis(keys, minBlockGap) | ||
testsLocalAllocations, err := tests.TestsRegistry.GenerateCustomAllocations(func() (chain.AuthFactory, error) { | ||
privateKey, err := ed25519.GeneratePrivateKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return auth.NewED25519Factory(privateKey), nil | ||
}) | ||
|
||
if err != nil { | ||
return workload.DefaultTestNetworkConfiguration{}, err | ||
} | ||
genesis := newGenesis(keys, testsLocalAllocations, minBlockGap) | ||
genesisBytes, err := json.Marshal(genesis) | ||
if err != nil { | ||
return workload.DefaultTestNetworkConfiguration{}, err | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,37 +6,63 @@ package registry | |
import ( | ||
"github.com/onsi/ginkgo/v2" | ||
|
||
"github.com/ava-labs/hypersdk/chain" | ||
"github.com/ava-labs/hypersdk/genesis" | ||
"github.com/ava-labs/hypersdk/tests/workload" | ||
) | ||
|
||
type TestFunc func(t ginkgo.FullGinkgoTInterface, tn workload.TestNetwork) | ||
type TestFunc func(t ginkgo.FullGinkgoTInterface, tn workload.TestNetwork, authFactories ...chain.AuthFactory) | ||
|
||
type namedTest struct { | ||
Fnc TestFunc | ||
Name string | ||
Fnc TestFunc | ||
Name string | ||
AuthFactories []chain.AuthFactory | ||
requestedBalances []uint64 | ||
} | ||
type Registry struct { | ||
tests []namedTest | ||
tests []*namedTest | ||
} | ||
|
||
func (r *Registry) Add(name string, f TestFunc) { | ||
r.tests = append(r.tests, namedTest{Fnc: f, Name: name}) | ||
func (r *Registry) Add(name string, f TestFunc, requestedBalances ...uint64) { | ||
r.tests = append(r.tests, &namedTest{Fnc: f, Name: name, requestedBalances: requestedBalances}) | ||
} | ||
|
||
func (r *Registry) List() []namedTest { | ||
func (r *Registry) List() []*namedTest { | ||
if r == nil { | ||
return []namedTest{} | ||
return []*namedTest{} | ||
} | ||
return r.tests | ||
} | ||
|
||
func (r *Registry) GenerateCustomAllocations(generateAuthFactory func() (chain.AuthFactory, error)) ([]*genesis.CustomAllocation, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The authFactories creation is delayed because when registering the tests, we don't know which private key type to use. |
||
requestedAllocations := make([]*genesis.CustomAllocation, 0) | ||
for index, test := range r.tests { | ||
for _, requestedBalance := range test.requestedBalances { | ||
authFactory, err := generateAuthFactory() | ||
if err != nil { | ||
return nil, err | ||
} | ||
test.AuthFactories = append(test.AuthFactories, authFactory) | ||
requestedAllocations = append( | ||
requestedAllocations, | ||
&genesis.CustomAllocation{ | ||
Address: authFactory.Address(), | ||
Balance: requestedBalance, | ||
}, | ||
) | ||
} | ||
r.tests[index] = test | ||
} | ||
return requestedAllocations, nil | ||
} | ||
|
||
// we need to pre-register all the test registries that are created externally in order to comply with the ginko execution order. | ||
// i.e. the global `var _ = ginkgo.Describe` used in the integration/e2e tests need to have this field populated before the iteration | ||
// over the top level nodes. | ||
var testRegistries = map[*Registry]bool{} | ||
|
||
func Register(registry *Registry, name string, f TestFunc) bool { | ||
registry.Add(name, f) | ||
func Register(registry *Registry, name string, f TestFunc, requestedBalances ...uint64) bool { | ||
registry.Add(name, f, requestedBalances...) | ||
testRegistries[registry] = true | ||
return true | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we replace
...chain.AuthFactory
with[]chain.AuthFactory
? I don't think we need the syntactic sugar here