-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cmd] Broken contract migration (#665)
* add tracing for fees * add migration for broken contracts * Update cmd/util/ledger/migrations/broken_contract_migration.go Co-authored-by: Peter Siemens <peterjsiemens@gmail.com> * add migration to the list Co-authored-by: Peter Siemens <peterjsiemens@gmail.com>
- Loading branch information
Showing
9 changed files
with
144 additions
and
8 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
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,85 @@ | ||
package migrations | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/onflow/flow-go/fvm/state" | ||
"github.com/onflow/flow-go/ledger" | ||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
// BrokenContractMigration fixes some of the early contracts that have been broken due to Cadence upgrades. | ||
func BrokenContractMigration(payloads []ledger.Payload) ([]ledger.Payload, error) { | ||
l := NewView(payloads) | ||
st := state.NewState(l) | ||
sth := state.NewStateHolder(st) | ||
a := state.NewAccounts(sth) | ||
|
||
err := migrateContractForAccount(a, "937cbdee135c656c") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = migrateContractForAccount(a, "7f560d7e3ff04a31") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return l.Payloads(), nil | ||
} | ||
|
||
func migrateContractForAccount(accounts *state.Accounts, addressInHex string) error { | ||
address := flow.HexToAddress(addressInHex) | ||
ok, err := accounts.Exists(address) | ||
if err != nil { | ||
return err | ||
} | ||
if ok { | ||
err = accounts.SetContract("TokenHolderKeyManager", address, GetKeyManagerContractContent()) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
// if not exist log and return gracefully | ||
fmt.Println("warning account does not exist: ", addressInHex) | ||
} | ||
return nil | ||
} | ||
|
||
func GetKeyManagerContractContent() []byte { | ||
return []byte(`import KeyManager from 0x840b99b76051d886 | ||
// The TokenHolderKeyManager contract is an implementation of | ||
// the KeyManager interface intended for use by FLOW token holders. | ||
// | ||
// One instance is deployed to each token holder account. | ||
// Deployment is executed a with signature from the administrator, | ||
// allowing them to take possession of a KeyAdder resource | ||
// upon initialization. | ||
pub contract TokenHolderKeyManager: KeyManager { | ||
access(contract) fun addPublicKey(_ publicKey: [UInt8]) { | ||
self.account.addPublicKey(publicKey) | ||
} | ||
pub resource KeyAdder: KeyManager.KeyAdder { | ||
pub let address: Address | ||
pub fun addPublicKey(_ publicKey: [UInt8]) { | ||
TokenHolderKeyManager.addPublicKey(publicKey) | ||
} | ||
init(address: Address) { | ||
self.address = address | ||
} | ||
} | ||
init(admin: AuthAccount, path: StoragePath) { | ||
let keyAdder <- create KeyAdder(address: self.account.address) | ||
admin.save(<- keyAdder, to: path) | ||
} | ||
} | ||
`) | ||
} |
36 changes: 36 additions & 0 deletions
36
cmd/util/ledger/migrations/broken_contract_migration_test.go
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,36 @@ | ||
package migrations_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onflow/flow-go/cmd/util/ledger/migrations" | ||
"github.com/onflow/flow-go/fvm/state" | ||
"github.com/onflow/flow-go/ledger" | ||
"github.com/onflow/flow-go/model/flow" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBrokenContractMigration(t *testing.T) { | ||
|
||
address := flow.HexToAddress("937cbdee135c656c") | ||
l := migrations.NewView(make([]ledger.Payload, 0)) | ||
st := state.NewState(l) | ||
sth := state.NewStateHolder(st) | ||
accounts := state.NewAccounts(sth) | ||
|
||
err := accounts.Create(nil, address) | ||
require.NoError(t, err) | ||
|
||
err = accounts.SetContract("TokenHolderKeyManager", address, []byte("OLD CONTENT")) | ||
require.NoError(t, err) | ||
|
||
newPayloads, err := migrations.BrokenContractMigration(l.Payloads()) | ||
require.NoError(t, err) | ||
|
||
st = state.NewState(migrations.NewView(newPayloads)) | ||
sth = state.NewStateHolder(st) | ||
accounts = state.NewAccounts(sth) | ||
content, err := accounts.GetContract("TokenHolderKeyManager", address) | ||
require.NoError(t, err) | ||
require.Equal(t, content, migrations.GetKeyManagerContractContent()) | ||
} |
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
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
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