OSDN Git Service

fix create same alias assets/accounts concurrently
authorlbqds <lbqds@outlook.com>
Thu, 19 Apr 2018 12:07:53 +0000 (20:07 +0800)
committerlbqds <lbqds@outlook.com>
Thu, 19 Apr 2018 12:07:53 +0000 (20:07 +0800)
account/accounts.go
asset/asset.go

index fc404b0..b003fdd 100644 (file)
@@ -105,6 +105,7 @@ type Manager struct {
        delayedACPs   map[*txbuilder.TemplateBuilder][]*CtrlProgram
 
        accIndexMu sync.Mutex
+       accountMu  sync.Mutex
 }
 
 // ExpireReservations removes reservations that have expired periodically.
@@ -148,6 +149,9 @@ func (m *Manager) getNextXpubsIndex(xpubs []chainkd.XPub) uint64 {
 
 // Create creates a new Account.
 func (m *Manager) Create(ctx context.Context, xpubs []chainkd.XPub, quorum int, alias string) (*Account, error) {
+       m.accountMu.Lock()
+       defer m.accountMu.Unlock()
+
        normalizedAlias := strings.ToLower(strings.TrimSpace(alias))
        if existed := m.db.Get(aliasKey(normalizedAlias)); existed != nil {
                return nil, ErrDuplicateAlias
index 3bea5ef..2b6d6a2 100644 (file)
@@ -103,6 +103,7 @@ type Registry struct {
        aliasCache *lru.Cache
 
        assetIndexMu sync.Mutex
+       assetMu      sync.Mutex
 }
 
 //Asset describe asset on bytom chain
@@ -148,6 +149,9 @@ func (reg *Registry) Define(xpubs []chainkd.XPub, quorum int, definition map[str
                return nil, ErrInternalAsset
        }
 
+       reg.assetMu.Lock()
+       defer reg.assetMu.Unlock()
+
        if existed := reg.db.Get(AliasKey(normalizedAlias)); existed != nil {
                return nil, ErrDuplicateAlias
        }