Skip to content
Snippets Groups Projects
Commit e4f4c6ff authored by Hans Moog's avatar Hans Moog
Browse files

Feat: added approvers model for the tangle

parent 7f510710
No related branches found
No related tags found
No related merge requests found
package approvers
import (
"sync"
"github.com/iotaledger/goshimmer/packages/binary/transaction"
"github.com/iotaledger/hive.go/objectstorage"
)
type Approvers struct {
objectstorage.StorableObjectFlags
transactionId transaction.Id
approvers map[transaction.Id]empty
approversMutex sync.RWMutex
}
func New(transactionId transaction.Id) *Approvers {
return &Approvers{
transactionId: transactionId,
approvers: make(map[transaction.Id]empty),
}
}
// Get's called when we restore the approvers from storage. The bytes and the content will be unmarshaled by an external
// caller (the objectStorage factory).
func FromStorage(id []byte) (result *Approvers) {
var transactionId transaction.Id
copy(transactionId[:], id)
result = &Approvers{
transactionId: transactionId,
}
return
}
func (approvers *Approvers) GetTransactionId() transaction.Id {
return approvers.transactionId
}
func (approvers *Approvers) Get() (result map[transaction.Id]empty) {
approvers.approversMutex.RLock()
result = make(map[transaction.Id]empty, len(approvers.approvers))
for approverId := range approvers.approvers {
result[approverId] = void
}
approvers.approversMutex.RUnlock()
return
}
func (approvers *Approvers) Add(transactionId transaction.Id) (modified bool) {
approvers.approversMutex.RLock()
if _, exists := approvers.approvers[transactionId]; !exists {
approvers.approversMutex.RUnlock()
approvers.approversMutex.Lock()
if _, exists := approvers.approvers[transactionId]; !exists {
approvers.approvers[transactionId] = void
modified = true
approvers.SetModified()
}
approvers.approversMutex.Unlock()
} else {
approvers.approversMutex.RUnlock()
}
return
}
func (approvers *Approvers) Remove(transactionId transaction.Id) (modified bool) {
approvers.approversMutex.RLock()
if _, exists := approvers.approvers[transactionId]; exists {
approvers.approversMutex.RUnlock()
approvers.approversMutex.Lock()
if _, exists := approvers.approvers[transactionId]; exists {
delete(approvers.approvers, transactionId)
modified = true
approvers.SetModified()
}
approvers.approversMutex.Unlock()
} else {
approvers.approversMutex.RUnlock()
}
return
}
func (approvers *Approvers) GetStorageKey() []byte {
transactionId := approvers.GetTransactionId()
return transactionId[:]
}
func (approvers *Approvers) Update(other objectstorage.StorableObject) {
panic("approvers should never be overwritten and only stored once to optimize IO")
}
func (approvers *Approvers) MarshalBinary() (result []byte, err error) {
return
}
func (approvers *Approvers) UnmarshalBinary(data []byte) (err error) {
return
}
package approvers
type empty struct{}
var void empty
......@@ -20,9 +20,9 @@ func New(publicKey []byte, optionalPrivateKey ...[]byte) *Identity {
copy(this.PublicKey, publicKey)
if len(optionalPrivateKey) == 0 {
this.Type = PUBLIC_TYPE
this.Type = Public
} else {
this.Type = PRIVATE_TYPE
this.Type = Private
this.PrivateKey = optionalPrivateKey[0]
}
......
package identity
import (
"fmt"
"sync"
"testing"
......@@ -37,8 +36,6 @@ func Test(t *testing.T) {
signature := identity.Sign([]byte("TESTDATA1"))
fmt.Println(len(signature))
assert.Equal(t, true, identity.VerifySignature([]byte("TESTDATA1"), signature))
assert.Equal(t, false, identity.VerifySignature([]byte("TESTDATA2"), signature))
}
......@@ -3,6 +3,6 @@ package identity
type Type int
const (
PRIVATE_TYPE = Type(0)
PUBLIC_TYPE = Type(1)
Private = Type(0)
Public = Type(1)
)
package tangle
import (
"github.com/iotaledger/goshimmer/packages/binary/approvers"
"github.com/iotaledger/goshimmer/packages/binary/transaction"
"github.com/iotaledger/hive.go/objectstorage"
)
type Tangle struct {
transactionStorage *objectstorage.ObjectStorage
approversStorage *objectstorage.ObjectStorage
}
func New(storageId string) *Tangle {
return &Tangle{
transactionStorage: objectstorage.New(storageId+"TANGLE_TRANSACTION_STORAGE", transactionFactory),
approversStorage: objectstorage.New(storageId+"TANGLE_APPROVERS_STORAGE", approversFactory),
}
}
......@@ -20,3 +23,9 @@ func transactionFactory(key []byte) objectstorage.StorableObject {
return result
}
func approversFactory(key []byte) objectstorage.StorableObject {
result := approvers.FromStorage(key)
return result
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment