Skip to content

Commit 34702ce

Browse files
Try #6508:
2 parents ffd0749 + d3183ae commit 34702ce

File tree

16 files changed

+126
-50
lines changed

16 files changed

+126
-50
lines changed

common/types/address.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ func (a Address) String() string {
112112
return result
113113
}
114114

115-
// Format implements fmt.Formatter, forcing the byte slice to be formatted as is,
116-
// without going through the stringer interface used for logging.
117-
func (a Address) Format(s fmt.State, c rune) {
118-
fmt.Fprintf(s, "%"+string(c), a[:])
119-
}
120-
121115
// EncodeScale implements scale codec interface.
122116
func (a *Address) EncodeScale(e *scale.Encoder) (int, error) {
123117
return scale.EncodeByteArray(e, a[:])

common/types/hashes.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ func (h Hash20) ShortString() string {
4444
return hex.EncodeToString(h[:5])
4545
}
4646

47-
// Format implements fmt.Formatter, forcing the byte slice to be formatted as is,
48-
// without going through the stringer interface used for logging.
49-
func (h Hash20) Format(s fmt.State, c rune) {
50-
fmt.Fprintf(s, "%"+string(c), h[:])
51-
}
52-
5347
// UnmarshalText parses a hash in hex syntax.
5448
func (h *Hash20) UnmarshalText(input []byte) error {
5549
if err := util.UnmarshalFixedText("Hash", input, h[:]); err != nil {
@@ -163,12 +157,6 @@ func (h Hash32) ShortString() string {
163157
return hex.EncodeToString(h[:5])
164158
}
165159

166-
// Format implements fmt.Formatter, forcing the byte slice to be formatted as is,
167-
// without going through the stringer interface used for logging.
168-
func (h Hash32) Format(s fmt.State, c rune) {
169-
fmt.Fprintf(s, "%"+string(c), h[:])
170-
}
171-
172160
// UnmarshalText parses a hash in hex syntax.
173161
func (h *Hash32) UnmarshalText(input []byte) error {
174162
if err := util.UnmarshalFixedText("Hash", input, h[:]); err != nil {

common/types/testutil.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package types
22

33
import (
44
"crypto/rand"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
58
)
69

710
// RandomBytes generates random data in bytes for testing.
@@ -137,3 +140,10 @@ func RandomVrfSignature() VrfSignature {
137140
}
138141
return VrfSignature(b)
139142
}
143+
144+
func RandomAddress(tb testing.TB) Address {
145+
var a Address
146+
_, err := rand.Read(a[:])
147+
require.NoError(tb, err)
148+
return a
149+
}

genvm/core/context.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67

78
"github.com/spacemeshos/go-scale"
@@ -235,7 +236,10 @@ func (c *Context) load(address types.Address) (*Account, error) {
235236
account, exist := c.changed[address]
236237
if !exist {
237238
loaded, err := c.Loader.Get(address)
238-
if err != nil {
239+
switch {
240+
case errors.Is(err, ErrNotFound):
241+
loaded = types.Account{Address: address}
242+
case err != nil:
239243
return nil, fmt.Errorf("%w: %w", ErrInternal, err)
240244
}
241245
account = &loaded

genvm/core/context_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,8 @@ func TestRelay(t *testing.T) {
193193
require.Equal(t, amount1, int(rec1state.Balance))
194194
require.NotEqual(t, encoded, rec1state.State)
195195

196-
rec2state, err := cache.Get(receiver2)
197-
require.NoError(t, err)
198-
require.Equal(t, 0, int(rec2state.Balance))
199-
require.NotEqual(t, encoded, rec2state.State)
196+
_, err = cache.Get(receiver2)
197+
require.ErrorIs(t, err, core.ErrNotFound) // relay to receiver2 failed
200198

201199
remoteState, err := cache.Get(remote)
202200
require.NoError(t, err)

genvm/core/staged_cache.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package core
22

33
import (
4+
"errors"
5+
46
"github.com/spacemeshos/go-spacemesh/common/types"
57
"github.com/spacemeshos/go-spacemesh/sql"
68
"github.com/spacemeshos/go-spacemesh/sql/accounts"
@@ -11,7 +13,11 @@ type DBLoader struct {
1113
}
1214

1315
func (db DBLoader) Get(address types.Address) (types.Account, error) {
14-
return accounts.Latest(db.Executor, address)
16+
account, err := accounts.Latest(db.Executor, address)
17+
if errors.Is(err, sql.ErrNotFound) {
18+
return types.Account{}, ErrNotFound
19+
}
20+
return account, err
1521
}
1622

1723
// NewStagedCache returns instance of the staged cache.

genvm/core/staged_cache_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func TestCacheGetCopies(t *testing.T) {
1313
db := statesql.InMemoryTest(t)
1414
ss := core.NewStagedCache(core.DBLoader{db})
1515
address := core.Address{1}
16+
ss.Update(core.Account{Address: address})
1617
account, err := ss.Get(address)
1718
require.NoError(t, err)
1819
account.Balance = 100

genvm/core/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package core
22

33
import (
4+
"errors"
5+
46
"github.com/spacemeshos/go-scale"
57

68
"github.com/spacemeshos/go-spacemesh/common/types"
@@ -76,8 +78,13 @@ type Template interface {
7678
Verify(Host, []byte, *scale.Decoder) bool
7779
}
7880

81+
var ErrNotFound = errors.New("not found")
82+
7983
// AccountLoader is an interface for loading accounts.
8084
type AccountLoader interface {
85+
// Get account for given address
86+
//
87+
// Returns ErrNotFound if the account doesn't exist.
8188
Get(Address) (Account, error)
8289
}
8390

genvm/rewards.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package vm
22

33
import (
4+
"errors"
45
"fmt"
56
"math/big"
67

@@ -67,7 +68,10 @@ func (v *VM) addRewards(
6768
}
6869
result = append(result, reward)
6970
account, err := ss.Get(blockReward.Coinbase)
70-
if err != nil {
71+
switch {
72+
case errors.Is(err, core.ErrNotFound):
73+
account = types.Account{Address: blockReward.Coinbase}
74+
case err != nil:
7175
return nil, fmt.Errorf("%w: %w", core.ErrInternal, err)
7276
}
7377
account.Balance += reward.TotalReward

genvm/vm.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ func (v *VM) AccountExists(address core.Address) (bool, error) {
156156
// GetNonce returns expected next nonce for the address.
157157
func (v *VM) GetNonce(address core.Address) (core.Nonce, error) {
158158
account, err := accounts.Latest(v.db, address)
159-
if err != nil {
159+
switch {
160+
case errors.Is(err, sql.ErrNotFound):
161+
return 0, nil
162+
case err != nil:
160163
return 0, err
161164
}
162165
return account.NextNonce, nil
@@ -165,7 +168,10 @@ func (v *VM) GetNonce(address core.Address) (core.Nonce, error) {
165168
// GetBalance returns balance for an address.
166169
func (v *VM) GetBalance(address types.Address) (uint64, error) {
167170
account, err := accounts.Latest(v.db, address)
168-
if err != nil {
171+
switch {
172+
case errors.Is(err, sql.ErrNotFound):
173+
return 0, nil
174+
case err != nil:
169175
return 0, err
170176
}
171177
return account.Balance, nil
@@ -501,9 +507,12 @@ func parse(
501507
return nil, nil, nil, fmt.Errorf("%w: failed to decode method selector %w", core.ErrMalformed, err)
502508
}
503509
account, err := loader.Get(principal)
504-
if err != nil {
510+
switch {
511+
case errors.Is(err, core.ErrNotFound):
512+
account = types.Account{Address: principal}
513+
case err != nil:
505514
return nil, nil, nil, fmt.Errorf(
506-
"%w: failed load state for principal %s - %w",
515+
"%w: failed load state for principal %s: %w",
507516
core.ErrInternal,
508517
principal,
509518
err,

0 commit comments

Comments
 (0)