-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration to add a key to all service accounts
- Loading branch information
1 parent
da4f52c
commit 3320dbe
Showing
3 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/rs/zerolog" | ||
|
||
"github.com/onflow/cadence/runtime/common" | ||
"github.com/onflow/crypto/hash" | ||
"github.com/onflow/flow-go-sdk/crypto" | ||
"github.com/onflow/flow-go/cmd/util/ledger/util/registers" | ||
"github.com/onflow/flow-go/fvm" | ||
"github.com/onflow/flow-go/fvm/systemcontracts" | ||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
// AddKeyMigration adds a new key to the core contracts accounts | ||
type AddKeyMigration struct { | ||
log zerolog.Logger | ||
|
||
accountsToAddKeyTo map[common.Address]AddKeyMigrationAccountPublicKeyData | ||
chainID flow.ChainID | ||
} | ||
|
||
var _ AccountBasedMigration = &AddKeyMigration{} | ||
|
||
func NewAddKeyMigration( | ||
chainID flow.ChainID, | ||
key crypto.PublicKey, | ||
) *AddKeyMigration { | ||
|
||
addresses := make(map[common.Address]AddKeyMigrationAccountPublicKeyData) | ||
sc := systemcontracts.SystemContractsForChain(chainID).All() | ||
|
||
for _, sc := range sc { | ||
addresses[common.Address(sc.Address)] = AddKeyMigrationAccountPublicKeyData{ | ||
PublicKey: key, | ||
HashAlgo: hash.SHA3_256, | ||
} | ||
} | ||
|
||
return &AddKeyMigration{ | ||
accountsToAddKeyTo: addresses, | ||
chainID: chainID, | ||
} | ||
} | ||
|
||
type AddKeyMigrationAccountPublicKeyData struct { | ||
PublicKey crypto.PublicKey | ||
HashAlgo hash.HashingAlgorithm | ||
} | ||
|
||
func (m *AddKeyMigration) InitMigration( | ||
log zerolog.Logger, | ||
_ *registers.ByAccount, | ||
_ int, | ||
) error { | ||
m.log = log.With().Str("component", "AddKeyMigration").Logger() | ||
return nil | ||
} | ||
|
||
func (m *AddKeyMigration) Close() error { | ||
return nil | ||
} | ||
|
||
func (m *AddKeyMigration) MigrateAccount( | ||
_ context.Context, | ||
address common.Address, | ||
accountRegisters *registers.AccountRegisters, | ||
) error { | ||
|
||
keyData, ok := m.accountsToAddKeyTo[address] | ||
if !ok { | ||
return nil | ||
} | ||
|
||
// Create all the runtime components we need for the migration | ||
migrationRuntime, err := NewInterpreterMigrationRuntime( | ||
accountRegisters, | ||
m.chainID, | ||
InterpreterMigrationRuntimeConfig{}, | ||
) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
key := flow.AccountPublicKey{ | ||
PublicKey: keyData.PublicKey, | ||
SignAlgo: keyData.PublicKey.Algorithm(), | ||
HashAlgo: keyData.HashAlgo, | ||
Weight: fvm.AccountKeyWeightThreshold, | ||
} | ||
|
||
err = migrationRuntime.Accounts.AppendPublicKey(flow.ConvertAddress(address), key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Finalize the transaction | ||
result, err := migrationRuntime.TransactionState.FinalizeMainTransaction() | ||
if err != nil { | ||
return fmt.Errorf("failed to finalize main transaction: %w", err) | ||
} | ||
|
||
// Merge the changes into the registers | ||
expectedAddresses := map[flow.Address]struct{}{ | ||
flow.Address(address): {}, | ||
} | ||
|
||
err = registers.ApplyChanges( | ||
accountRegisters, | ||
result.WriteSet, | ||
expectedAddresses, | ||
m.log, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to apply register changes: %w", err) | ||
} | ||
|
||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/onflow/cadence/runtime/common" | ||
"github.com/onflow/flow-go-sdk/crypto" | ||
"github.com/onflow/flow-go/cmd/util/ledger/util" | ||
"github.com/onflow/flow-go/cmd/util/ledger/util/registers" | ||
"github.com/onflow/flow-go/fvm" | ||
"github.com/onflow/flow-go/fvm/systemcontracts" | ||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
func TestCoreContractsKeys(t *testing.T) { | ||
t.Parallel() | ||
|
||
log := zerolog.New(zerolog.NewTestWriter(t)) | ||
|
||
// Get the old payloads | ||
payloads, err := util.PayloadsFromEmulatorSnapshot(snapshotPath) | ||
require.NoError(t, err) | ||
|
||
registersByAccount, err := registers.NewByAccountFromPayloads(payloads) | ||
require.NoError(t, err) | ||
|
||
chainID := flow.Emulator | ||
sc := systemcontracts.SystemContractsForChain(chainID) | ||
|
||
serviceRegisters := registersByAccount.AccountRegisters(string(sc.FlowServiceAccount.Address.Bytes())) | ||
|
||
pk, err := crypto.GeneratePrivateKey(crypto.ECDSA_P256, nil) | ||
require.NoError(t, err) | ||
expectedKey := pk.PublicKey() | ||
|
||
mig := NewAddKeyMigration( | ||
chainID, | ||
expectedKey, | ||
) | ||
defer func() { | ||
err := mig.Close() | ||
require.NoError(t, err) | ||
}() | ||
|
||
err = mig.InitMigration(log, registersByAccount, 1) | ||
require.NoError(t, err) | ||
|
||
ctx := context.Background() | ||
err = mig.MigrateAccount(ctx, common.Address(sc.FlowServiceAccount.Address), serviceRegisters) | ||
require.NoError(t, err) | ||
|
||
// Create all the runtime components we need for the migration | ||
migrationRuntime, err := NewInterpreterMigrationRuntime( | ||
serviceRegisters, | ||
chainID, | ||
InterpreterMigrationRuntimeConfig{}, | ||
) | ||
require.NoError(t, err) | ||
|
||
// The last key should be the one we added | ||
keys, err := migrationRuntime.Accounts.GetPublicKeyCount(sc.FlowServiceAccount.Address) | ||
require.NoError(t, err) | ||
|
||
key, err := migrationRuntime.Accounts.GetPublicKey(sc.FlowServiceAccount.Address, keys-1) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, expectedKey.String(), key.PublicKey.String()) | ||
require.Equal(t, fvm.AccountKeyWeightThreshold, key.Weight) | ||
} | ||
|
||
func Test_DO_NOT_MERGE(t *testing.T) { | ||
t.Parallel() | ||
// this branch should not be merged to master | ||
// This is only to be used for migration mainnet testing | ||
t.Fail() | ||
} |