Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix multiple same addresses save in db #130

Merged
merged 1 commit into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions models/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestAddress(t *testing.T) {
t.Run("SaveAddress", func(t *testing.T) {
assert.NoError(t, addressRepo.SaveAddress(ctx, addrInfo))
assert.NoError(t, addressRepo.SaveAddress(ctx, addrInfo2))
assert.NoError(t, addressRepo.SaveAddress(ctx, addrInfo3))
assert.Error(t, addressRepo.SaveAddress(ctx, addrInfo3))
})

checkField := func(t *testing.T, expect, actual *types.Address) {
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestAddress(t *testing.T) {
t.Run("ListAddress", func(t *testing.T) {
rs, err := addressRepo.ListAddress(ctx)
assert.NoError(t, err)
assert.LessOrEqual(t, 2, len(rs))
assert.LessOrEqual(t, 1, len(rs))
})
}

Expand Down
44 changes: 23 additions & 21 deletions models/mysql/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type mysqlAddress struct {
ID types.UUID `gorm:"column:id;type:varchar(256);primary_key"`
Addr string `gorm:"column:addr;type:varchar(256);NOT NULL"` // 主键
Addr string `gorm:"column:addr;type:varchar(256);uniqueIndex;NOT NULL"`
Nonce uint64 `gorm:"column:nonce;type:bigint unsigned;index;NOT NULL"`
Weight int64 `gorm:"column:weight;type:bigint;index;NOT NULL"`
SelMsgNum uint64 `gorm:"column:sel_msg_num;type:bigint unsigned;NOT NULL"`
Expand All @@ -34,15 +34,16 @@ func (s mysqlAddress) TableName() string {

func FromAddress(addr *types.Address) *mysqlAddress {
mysqlAddr := &mysqlAddress{
ID: addr.ID,
Addr: addr.Addr.String(),
Nonce: addr.Nonce,
Weight: addr.Weight,
SelMsgNum: addr.SelMsgNum,
State: addr.State,
IsDeleted: addr.IsDeleted,
CreatedAt: addr.CreatedAt,
UpdatedAt: addr.UpdatedAt,
ID: addr.ID,
Addr: addr.Addr.String(),
Nonce: addr.Nonce,
Weight: addr.Weight,
SelMsgNum: addr.SelMsgNum,
State: addr.State,
GasOverEstimation: addr.GasOverEstimation,
IsDeleted: addr.IsDeleted,
CreatedAt: addr.CreatedAt,
UpdatedAt: addr.UpdatedAt,
}

if !addr.MaxFee.Nil() {
Expand All @@ -61,17 +62,18 @@ func (s mysqlAddress) Address() (*types.Address, error) {
return nil, err
}
return &types.Address{
ID: s.ID,
Addr: addr,
Nonce: s.Nonce,
Weight: s.Weight,
SelMsgNum: s.SelMsgNum,
State: s.State,
MaxFee: big.Int{Int: s.MaxFee.Int},
MaxFeeCap: big.Int{Int: s.MaxFeeCap.Int},
IsDeleted: s.IsDeleted,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
ID: s.ID,
Addr: addr,
Nonce: s.Nonce,
Weight: s.Weight,
SelMsgNum: s.SelMsgNum,
State: s.State,
MaxFee: big.Int{Int: s.MaxFee.Int},
MaxFeeCap: big.Int{Int: s.MaxFeeCap.Int},
GasOverEstimation: s.GasOverEstimation,
IsDeleted: s.IsDeleted,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion models/sqlite/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

type sqliteAddress struct {
ID types.UUID `gorm:"column:id;type:varchar(256);primary_key"`
Addr string `gorm:"column:addr;type:varchar(256);NOT NULL"` // 主键
Addr string `gorm:"column:addr;type:varchar(256);uniqueIndex;NOT NULL"`
Nonce uint64 `gorm:"column:nonce;type:unsigned bigint;index;NOT NULL"`
Weight int64 `gorm:"column:weight;type:bigint;index;NOT NULL"`
SelMsgNum uint64 `gorm:"column:sel_msg_num;type:unsigned bigint;NOT NULL"`
Expand Down
2 changes: 1 addition & 1 deletion service/message_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (messageSelector *MessageSelector) addrSelectMsgNum(addrList []*types.Addre
}
selMsgNum := make(map[address.Address]uint64)
for _, addr := range addrList {
if num, ok := selMsgNum[addr.Addr]; ok && (num < addr.SelMsgNum || addr.SelMsgNum < defSelMsgNum) {
if num, ok := selMsgNum[addr.Addr]; ok && addr.SelMsgNum > 0 && num < addr.SelMsgNum {
selMsgNum[addr.Addr] = addr.SelMsgNum
} else if !ok {
if addr.SelMsgNum == 0 {
Expand Down
41 changes: 21 additions & 20 deletions service/message_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,35 +128,36 @@ func (ms *MessageService) pushMessage(ctx context.Context, msg *types.Message) e
if err != nil {
return err
}
if has {
saveAddr := func() error {
_, err = ms.addressService.SaveAddress(ctx, &types.Address{
if !has {
return xerrors.Errorf("wallet(%s) address %s not exists", msg.WalletName, msg.From)
}
var addrInfo *types.Address
if err := ms.repo.Transaction(func(txRepo repo.TxRepo) error {
addrInfo, err = ms.addressService.GetAddress(ctx, msg.From)
if err == nil {
return nil
}
if xerrors.Is(err, gorm.ErrRecordNotFound) {
if err = ms.repo.AddressRepo().SaveAddress(ctx, &types.Address{
ID: types.NewUUID(),
Addr: msg.From,
Nonce: 0,
State: types.Alive,
IsDeleted: repo.NotDeleted,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
return err
}
addrInfo, err := ms.addressService.GetAddress(ctx, msg.From)
if err != nil {
if xerrors.Is(err, gorm.ErrRecordNotFound) {
if err := saveAddr(); err != nil {
return xerrors.Errorf("save address %s failed %v", msg.From.String(), err)
}
ms.log.Infof("add new address %s", msg.From.String())
} else {
return err
}); err != nil {
return xerrors.Errorf("save address %s failed %v", msg.From.String(), err)
}
} else if addrInfo.State == types.Forbiden {
ms.log.Errorf("address(%s) is forbidden", msg.From.String())
return xerrors.Errorf("address(%s) is forbidden", msg.From.String())
ms.log.Infof("add new address %s", msg.From.String())
}
} else {
return xerrors.Errorf("wallet(%s) address %s not exists", msg.WalletName, msg.From)
return err
}); err != nil {
return err
}
if addrInfo != nil && addrInfo.State == types.Forbiden {
ms.log.Errorf("address(%s) is forbidden", msg.From.String())
return xerrors.Errorf("address(%s) is forbidden", msg.From.String())
}

msg.Nonce = 0
Expand Down