Skip to content

Commit 5953799

Browse files
authored
Bugfix update account (#43)
* reintroduce index on account * load index * load index * bugfix for account update * add test asserting bugfix
1 parent c005d43 commit 5953799

File tree

6 files changed

+88
-17
lines changed

6 files changed

+88
-17
lines changed

blockchain/projects.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func (s *Projects) CreateInitialAccounts(projectID uuid.UUID) ([]*model.Internal
158158
ProjectID: projectID,
159159
},
160160
Address: account.Address,
161+
Index: i,
161162
}
162163
}
163164

controller/accounts.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
package controller
2020

2121
import (
22-
"fmt"
23-
2422
"github.com/dapperlabs/flow-playground-api/blockchain"
2523
"github.com/dapperlabs/flow-playground-api/model"
2624
"github.com/dapperlabs/flow-playground-api/storage"
@@ -88,7 +86,7 @@ func (a *Accounts) Update(input model.UpdateAccount) (*model.Account, error) {
8886
var acc model.InternalAccount
8987

9088
// if we provided draft code then just do a storage update of an account
91-
if input.DraftCode != nil {
89+
if input.DeployedCode == nil {
9290
err := a.store.UpdateAccount(input, &acc)
9391
if err != nil {
9492
return nil, err
@@ -102,11 +100,6 @@ func (a *Accounts) Update(input model.UpdateAccount) (*model.Account, error) {
102100
return nil, err
103101
}
104102

105-
// if deployed code is not provided fail, else continue and deploy new contracts
106-
if input.DeployedCode == nil {
107-
return nil, fmt.Errorf("must provide either deployed code or draft code for update")
108-
}
109-
110103
account, err := a.blockchain.GetAccount(input.ProjectID, acc.Address)
111104
if err != nil {
112105
return nil, err

model/account.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type InternalAccount struct {
2929
ProjectChildID
3030
Address Address
3131
DraftCode string
32+
Index int
3233
}
3334

3435
func (a *InternalAccount) NameKey() *datastore.Key {
@@ -41,6 +42,7 @@ func (a *InternalAccount) Load(ps []datastore.Property) error {
4142
ProjectID string
4243
Address []byte
4344
DraftCode string
45+
Index int
4446
}{}
4547

4648
if err := datastore.LoadStruct(&tmp, ps); err != nil {
@@ -56,7 +58,7 @@ func (a *InternalAccount) Load(ps []datastore.Property) error {
5658
}
5759

5860
copy(a.Address[:], tmp.Address[:])
59-
61+
a.Index = tmp.Index
6062
a.DraftCode = tmp.DraftCode
6163

6264
return nil
@@ -80,6 +82,10 @@ func (a *InternalAccount) Save() ([]datastore.Property, error) {
8082
Name: "DraftCode",
8183
Value: a.DraftCode,
8284
},
85+
{
86+
Name: "Index",
87+
Value: a.Index,
88+
},
8389
}, nil
8490
}
8591

playground_test.go

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ type Project struct {
5959
Persist bool
6060
Version string
6161
Accounts []struct {
62-
ID string
63-
Address string
64-
DraftCode string
62+
ID string
63+
Address string
64+
DraftCode string
65+
DeployedCode string
6566
}
6667
TransactionTemplates []TransactionTemplate
6768
Secret string
@@ -103,6 +104,8 @@ query($projectId: UUID!) {
103104
accounts {
104105
id
105106
address
107+
draftCode
108+
deployedCode
106109
}
107110
}
108111
}
@@ -1072,9 +1075,9 @@ func TestTransactionExecutions(t *testing.T) {
10721075
// manually construct resolver
10731076
store := memory.NewStore()
10741077

1075-
chain := blockchain.NewProjects(store, lru.New(128), initAccounts)
1078+
projects := blockchain.NewProjects(store, lru.New(128), initAccounts)
10761079
authenticator := auth.NewAuthenticator(store, sessionName)
1077-
resolver := playground.NewResolver(version, store, authenticator, chain)
1080+
resolver := playground.NewResolver(version, store, authenticator, projects)
10781081

10791082
c := newClientWithResolver(resolver)
10801083

@@ -1106,7 +1109,7 @@ func TestTransactionExecutions(t *testing.T) {
11061109
eventA.Values[0],
11071110
)
11081111

1109-
err = chain.Reset(&model.InternalProject{
1112+
err = projects.Reset(&model.InternalProject{
11101113
ID: uuid.MustParse(project.ID),
11111114
})
11121115
require.NoError(t, err)
@@ -1799,6 +1802,74 @@ func TestAccounts(t *testing.T) {
17991802
assert.Contains(t, respB.UpdateAccount.DeployedContracts, "Foo")
18001803
})
18011804

1805+
t.Run("Update account and redeploy code", func(t *testing.T) {
1806+
c := newClient()
1807+
1808+
project := createProject(t, c)
1809+
1810+
account := project.Accounts[0]
1811+
1812+
var respA GetAccountResponse
1813+
1814+
err := c.Post(
1815+
QueryGetAccount,
1816+
&respA,
1817+
client.Var("projectId", project.ID),
1818+
client.Var("accountId", account.ID),
1819+
)
1820+
require.NoError(t, err)
1821+
1822+
assert.Equal(t, "", respA.Account.DeployedCode)
1823+
1824+
var respB UpdateAccountResponse
1825+
1826+
const contract = "pub contract Foo {}"
1827+
1828+
err = c.Post(
1829+
MutationUpdateAccountDeployedCode,
1830+
&respB,
1831+
client.Var("projectId", project.ID),
1832+
client.Var("accountId", account.ID),
1833+
client.Var("code", contract),
1834+
client.AddCookie(c.SessionCookie()),
1835+
)
1836+
require.NoError(t, err)
1837+
1838+
assert.Equal(t, contract, respB.UpdateAccount.DeployedCode)
1839+
assert.Contains(t, respB.UpdateAccount.DeployedContracts, "Foo")
1840+
1841+
var respC UpdateAccountResponse
1842+
1843+
const contract2 = "pub contract Bar {}"
1844+
1845+
err = c.Post(
1846+
MutationUpdateAccountDeployedCode,
1847+
&respC,
1848+
client.Var("projectId", project.ID),
1849+
client.Var("accountId", account.ID),
1850+
client.Var("code", contract2),
1851+
client.AddCookie(c.SessionCookie()),
1852+
)
1853+
require.NoError(t, err)
1854+
1855+
assert.Equal(t, contract2, respC.UpdateAccount.DeployedCode)
1856+
assert.Contains(t, respC.UpdateAccount.DeployedContracts, "Bar")
1857+
1858+
var resp GetProjectResponse
1859+
1860+
err = c.Post(
1861+
QueryGetProject,
1862+
&resp,
1863+
client.Var("projectId", project.ID),
1864+
)
1865+
require.NoError(t, err)
1866+
1867+
assert.Equal(t, project.ID, resp.Project.ID)
1868+
assert.Len(t, resp.Project.Accounts, 5)
1869+
1870+
assert.Equal(t, contract2, resp.Project.Accounts[0].DeployedCode)
1871+
})
1872+
18021873
t.Run("Update non-existent account", func(t *testing.T) {
18031874
c := newClient()
18041875

storage/datastore/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (d *Datastore) ResetProjectState(proj *model.InternalProject) error {
278278
proj.TransactionCount = 0
279279
proj.UpdatedAt = time.Now()
280280

281-
_, err = tx.Put(proj.NameKey(), &proj)
281+
_, err = tx.Put(proj.NameKey(), proj)
282282
if err != nil {
283283
return err
284284
}

storage/memory/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (s *Store) getAccountsForProject(projectID uuid.UUID, accs *[]*model.Intern
288288

289289
// sort results by index
290290
sort.Slice(res, func(i, j int) bool {
291-
return res[i].Address[len(res[i].Address)-1] < res[j].Address[len(res[j].Address)-1]
291+
return res[i].Index < res[j].Index
292292
})
293293

294294
*accs = res

0 commit comments

Comments
 (0)