-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* started implementation of stateful mam * mam: refactored state into transmitter * fixed transmitter * fixed tests * implemented MAM receiver * refactored channel and transmitter * implemented private and restricted modes * tidy dependency * Update mam/v1/receiver.go Co-Authored-By: Luca Moser <[email protected]> * Update mam/v1/receiver.go Co-Authored-By: Luca Moser <[email protected]> * Update mam/v1/transmitter.go Co-Authored-By: Luca Moser <[email protected]> * fixed several small subjects * updated gitignore * improved transmitter's set mode method * fixed bug in receiver
- Loading branch information
1 parent
c5a0c53
commit 51b838c
Showing
15 changed files
with
972 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/simia-tech/env" | ||
|
||
"github.com/iotaledger/iota.go/api" | ||
"github.com/iotaledger/iota.go/mam/v1" | ||
) | ||
|
||
var ( | ||
endpointURL = env.String("ENDPOINT_URL", "https://nodes.thetangle.org:443") | ||
mode = env.String("MODE", "public", env.AllowedValues("public", "private", "restricted")) | ||
sideKey = env.String("SIDE_KEY", "") | ||
) | ||
|
||
func main() { | ||
follow := flag.Bool("f", false, "don't exit and check every second for new messages") | ||
flag.Parse() | ||
root := flag.Arg(0) | ||
|
||
cm, err := mam.ParseChannelMode(mode.Get()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
api, err := api.ComposeAPI(api.HTTPClientSettings{ | ||
URI: endpointURL.Get(), | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
receiver := mam.NewReceiver(api) | ||
if err := receiver.SetMode(cm, sideKey.Get()); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Printf("receive root %q from %s channel...\n", root, cm) | ||
loop: | ||
nextRoot, messages, err := receiver.Receive(root) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for _, message := range messages { | ||
fmt.Println(message) | ||
} | ||
if *follow { | ||
time.Sleep(time.Second) | ||
if len(messages) > 0 { | ||
root = nextRoot | ||
} | ||
goto loop | ||
} | ||
if len(messages) == 0 { | ||
fmt.Println("no messages found") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/simia-tech/env" | ||
|
||
"github.com/iotaledger/iota.go/api" | ||
"github.com/iotaledger/iota.go/consts" | ||
"github.com/iotaledger/iota.go/mam/v1" | ||
"github.com/iotaledger/iota.go/pow" | ||
) | ||
|
||
var ( | ||
endpointURL = env.String("ENDPOINT_URL", "https://nodes.thetangle.org:443") | ||
seed = env.String("SEED", "") | ||
mwm = env.Int("MWM", 9) | ||
mode = env.String("MODE", "public", env.AllowedValues("public", "private", "restricted")) | ||
sideKey = env.String("SIDE_KEY", "") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
messages := flag.Args() | ||
|
||
cm, err := mam.ParseChannelMode(mode.Get()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
_, powFunc := pow.GetFastestProofOfWorkImpl() | ||
|
||
api, err := api.ComposeAPI(api.HTTPClientSettings{ | ||
URI: endpointURL.Get(), | ||
LocalProofOfWorkFunc: powFunc, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
transmitter := mam.NewTransmitter(api, seed.Get(), uint64(mwm.Get()), consts.SecurityLevelMedium) | ||
if err := transmitter.SetMode(cm, sideKey.Get()); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
for _, message := range messages { | ||
fmt.Printf("transmit message %q to %s channel...\n", message, cm) | ||
root, err := transmitter.Transmit(message) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("transmitted to root %q\n", root) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package mam | ||
|
||
import ( | ||
"github.com/iotaledger/iota.go/address" | ||
"github.com/iotaledger/iota.go/consts" | ||
"github.com/iotaledger/iota.go/curl" | ||
"github.com/iotaledger/iota.go/trinary" | ||
) | ||
|
||
func makeAddress(mode ChannelMode, root trinary.Trits, sideKey trinary.Trytes) (trinary.Trytes, error) { | ||
if mode == ChannelModePublic { | ||
return toAddress(root) | ||
} | ||
|
||
sideKeyTrits, err := trinary.TrytesToTrits(sideKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
h := curl.NewCurlP81() | ||
if err := h.Absorb(sideKeyTrits); err != nil { | ||
return "", err | ||
} | ||
if err := h.Absorb(root); err != nil { | ||
return "", err | ||
} | ||
hashedRoot, err := h.Squeeze(consts.HashTrinarySize) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return toAddress(hashedRoot) | ||
} | ||
|
||
func toAddress(root trinary.Trits) (trinary.Trytes, error) { | ||
rootTrytes, err := trinary.TritsToTrytes(root) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
chkSum, err := address.Checksum(rootTrytes) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return rootTrytes + chkSum, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mam | ||
|
||
import ( | ||
"github.com/iotaledger/iota.go/api" | ||
"github.com/iotaledger/iota.go/bundle" | ||
"github.com/iotaledger/iota.go/transaction" | ||
"github.com/iotaledger/iota.go/trinary" | ||
) | ||
|
||
// API defines an interface with a subset of methods of `api.API`. | ||
type API interface { | ||
PrepareTransfers(seed trinary.Trytes, transfers bundle.Transfers, opts api.PrepareTransfersOptions) ([]trinary.Trytes, error) | ||
SendTrytes(trytes []trinary.Trytes, depth uint64, mwm uint64, reference ...trinary.Hash) (bundle.Bundle, error) | ||
FindTransactionObjects(query api.FindTransactionsQuery) (transaction.Transactions, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package mam_test | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
"github.com/iotaledger/iota.go/api" | ||
"github.com/iotaledger/iota.go/bundle" | ||
"github.com/iotaledger/iota.go/transaction" | ||
"github.com/iotaledger/iota.go/trinary" | ||
) | ||
|
||
type fakeAPI struct { | ||
prepareTransfers func(trinary.Trytes, bundle.Transfers, api.PrepareTransfersOptions) ([]trinary.Trytes, error) | ||
sendTrytes func([]trinary.Trytes, uint64, uint64, ...trinary.Hash) (bundle.Bundle, error) | ||
findTransactionObjects func(api.FindTransactionsQuery) (transaction.Transactions, error) | ||
} | ||
|
||
func newFakeAPI() *fakeAPI { | ||
return &fakeAPI{ | ||
prepareTransfers: func(_ trinary.Trytes, _ bundle.Transfers, _ api.PrepareTransfersOptions) ([]trinary.Trytes, error) { | ||
return nil, errors.New("not implemented") | ||
}, | ||
sendTrytes: func(_ []trinary.Trytes, _ uint64, _ uint64, _ ...trinary.Hash) (bundle.Bundle, error) { | ||
return nil, errors.New("not implemented") | ||
}, | ||
findTransactionObjects: func(_ api.FindTransactionsQuery) (transaction.Transactions, error) { | ||
return nil, errors.New("not implemented") | ||
}, | ||
} | ||
} | ||
|
||
func (f *fakeAPI) PrepareTransfers(seed trinary.Trytes, transfers bundle.Transfers, opts api.PrepareTransfersOptions) ([]trinary.Trytes, error) { | ||
return f.prepareTransfers(seed, transfers, opts) | ||
} | ||
|
||
func (f *fakeAPI) SendTrytes(trytes []trinary.Trytes, depth uint64, mwm uint64, reference ...trinary.Hash) (bundle.Bundle, error) { | ||
return f.sendTrytes(trytes, depth, mwm, reference...) | ||
} | ||
|
||
func (f *fakeAPI) FindTransactionObjects(query api.FindTransactionsQuery) (transaction.Transactions, error) { | ||
return f.findTransactionObjects(query) | ||
} | ||
|
||
func err(returns []interface{}) error { | ||
if len(returns) == 0 { | ||
return nil | ||
} | ||
last := returns[len(returns)-1] | ||
if last == nil { | ||
return nil | ||
} | ||
if e, ok := last.(error); ok { | ||
return e | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package mam | ||
|
||
import ( | ||
"github.com/iotaledger/iota.go/consts" | ||
"github.com/iotaledger/iota.go/trinary" | ||
) | ||
|
||
type channel struct { | ||
mode ChannelMode | ||
sideKey trinary.Trytes | ||
nextRoot trinary.Trits | ||
securityLevel consts.SecurityLevel | ||
start uint64 | ||
count uint64 | ||
nextCount uint64 | ||
index uint64 | ||
} | ||
|
||
func newChannel(securityLevel consts.SecurityLevel) *channel { | ||
return &channel{ | ||
mode: ChannelModePublic, | ||
sideKey: consts.NullHashTrytes, | ||
securityLevel: securityLevel, | ||
start: 0, | ||
count: 1, | ||
nextCount: 1, | ||
index: 0, | ||
} | ||
} | ||
|
||
func (c *channel) incIndex() { | ||
if c.index == c.count-1 { | ||
c.start += c.nextCount | ||
c.index = 0 | ||
} else { | ||
c.index++ | ||
} | ||
} | ||
|
||
func (c *channel) nextStart() uint64 { | ||
return c.start + c.count | ||
} |
Oops, something went wrong.