Skip to content

Commit

Permalink
export address generator constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Nov 27, 2024
1 parent 95ca4de commit 8e6fbbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
16 changes: 8 additions & 8 deletions model/flow/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func testAddressConstants(t *testing.T) {
assert.Equal(t, address, chain.ServiceAddress())

// check high state values: generation should fail for high value states
state = chain.newAddressGeneratorAtIndex(maxIndex - 1)
state = chain.NewAddressGeneratorAtIndex(maxIndex - 1)
_, err = state.NextAddress()
assert.NoError(t, err)
_, err = state.NextAddress()
Expand Down Expand Up @@ -200,7 +200,7 @@ func testAddressGeneration(t *testing.T) {
// this is only a sanity check of the implementation and not an exhaustive proof
if chainID == Mainnet {
r := uint64(rand.Intn(maxIndex - loop))
state = chain.newAddressGeneratorAtIndex(r)
state = chain.NewAddressGeneratorAtIndex(r)
for i := 0; i < loop; i++ {
address, err := state.NextAddress()
require.NoError(t, err)
Expand All @@ -213,7 +213,7 @@ func testAddressGeneration(t *testing.T) {
// All distances between any two addresses must be larger than d.
// this is only a sanity check of the implementation and not an exhaustive proof
r := uint64(rand.Intn(maxIndex - loop - 1))
state = chain.newAddressGeneratorAtIndex(r)
state = chain.NewAddressGeneratorAtIndex(r)
refAddress, err := state.NextAddress()
require.NoError(t, err)
for i := 0; i < loop; i++ {
Expand All @@ -226,7 +226,7 @@ func testAddressGeneration(t *testing.T) {
// sanity check of valid account addresses.
// All valid addresses must pass IsValid.
r = uint64(rand.Intn(maxIndex - loop))
state = chain.newAddressGeneratorAtIndex(r)
state = chain.NewAddressGeneratorAtIndex(r)
for i := 0; i < loop; i++ {
address, err := state.NextAddress()
require.NoError(t, err)
Expand All @@ -241,7 +241,7 @@ func testAddressGeneration(t *testing.T) {
assert.False(t, check, "account address format should be invalid")
r = uint64(rand.Intn(maxIndex - loop))

state = chain.newAddressGeneratorAtIndex(r)
state = chain.NewAddressGeneratorAtIndex(r)
for i := 0; i < loop; i++ {
address, err := state.NextAddress()
require.NoError(t, err)
Expand Down Expand Up @@ -271,7 +271,7 @@ func testAddressesIntersection(t *testing.T) {

// a valid address in one network must be invalid in all other networks
r := uint64(rand.Intn(maxIndex - loop))
state := chain.newAddressGeneratorAtIndex(r)
state := chain.NewAddressGeneratorAtIndex(r)
for k := 0; k < loop; k++ {
address, err := state.NextAddress()
require.NoError(t, err)
Expand All @@ -297,7 +297,7 @@ func testAddressesIntersection(t *testing.T) {
// build invalid addresses using `invalidCodeWord` and make sure they all
// fail the check for all networks
r = uint64(rand.Intn(maxIndex - loop))
state = chain.newAddressGeneratorAtIndex(r)
state = chain.NewAddressGeneratorAtIndex(r)
for k := 0; k < loop; k++ {
address, err := state.NextAddress()
require.NoError(t, err)
Expand Down Expand Up @@ -328,7 +328,7 @@ func testIndexFromAddress(t *testing.T) {
// random valid index
r := uint64(rand.Intn(maxIndex)) + 1
// generate the address
address := chain.newAddressGeneratorAtIndex(r).CurrentAddress()
address := chain.NewAddressGeneratorAtIndex(r).CurrentAddress()
// extract the index and compare
index, err := chain.IndexFromAddress(address)
assert.NoError(t, err) // address should be valid
Expand Down
14 changes: 7 additions & 7 deletions model/flow/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c ChainID) getChainCodeWord() uint64 {
}

type chainImpl interface {
newAddressGeneratorAtIndex(index uint64) AddressGenerator
NewAddressGeneratorAtIndex(index uint64) AddressGenerator
// IsValid returns true if a given address is a valid account address on a given chain,
// and false otherwise.
//
Expand All @@ -102,7 +102,7 @@ type chainImpl interface {
// where addresses are simply the index of the account.
type monotonicImpl struct{}

func (m *monotonicImpl) newAddressGeneratorAtIndex(index uint64) AddressGenerator {
func (m *monotonicImpl) NewAddressGeneratorAtIndex(index uint64) AddressGenerator {
return &MonotonicAddressGenerator{
index: index,
}
Expand Down Expand Up @@ -131,7 +131,7 @@ type linearCodeImpl struct {
chainID ChainID
}

func (l *linearCodeImpl) newAddressGeneratorAtIndex(index uint64) AddressGenerator {
func (l *linearCodeImpl) NewAddressGeneratorAtIndex(index uint64) AddressGenerator {
return &linearCodeAddressGenerator{
index: index,
chainCodeWord: l.chainID.getChainCodeWord(),
Expand Down Expand Up @@ -262,6 +262,7 @@ func (c ChainID) String() string {
// Chain is the interface for address generation implementations.
type Chain interface {
NewAddressGenerator() AddressGenerator
NewAddressGeneratorAtIndex(index uint64) AddressGenerator
AddressAtIndex(index uint64) (Address, error)
ServiceAddress() Address
BytesToAddressGenerator(b []byte) AddressGenerator
Expand All @@ -271,21 +272,20 @@ type Chain interface {
ChainID() ChainID
// required for tests
zeroAddress() Address
newAddressGeneratorAtIndex(index uint64) AddressGenerator
}

// NewAddressGenerator returns a new AddressGenerator with an
// initialized index.
func (id *addressedChain) NewAddressGenerator() AddressGenerator {
return id.newAddressGeneratorAtIndex(0)
return id.NewAddressGeneratorAtIndex(0)
}

// AddressAtIndex returns the index-th generated account address.
func (id *addressedChain) AddressAtIndex(index uint64) (Address, error) {
if index > maxIndex {
return EmptyAddress, fmt.Errorf("index must be less or equal to %x", maxIndex)
}
return id.newAddressGeneratorAtIndex(index).CurrentAddress(), nil
return id.NewAddressGeneratorAtIndex(index).CurrentAddress(), nil
}

// ServiceAddress returns the root (first) generated account address.
Expand All @@ -307,7 +307,7 @@ func (id *addressedChain) BytesToAddressGenerator(b []byte) AddressGenerator {
bytes := slices.EnsureByteSliceSize(b, addressIndexLength)

index := uint48(bytes[:])
return id.newAddressGeneratorAtIndex(index)
return id.NewAddressGeneratorAtIndex(index)
}

// ChainID returns the chain ID of the chain.
Expand Down

0 comments on commit 8e6fbbf

Please sign in to comment.