-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from cloudstruct/feature/local-tx-submission
Implement local-tx-submission mini-protocol
- Loading branch information
Showing
12 changed files
with
451 additions
and
51 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
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 |
---|---|---|
|
@@ -8,6 +8,8 @@ const ( | |
BLOCK_TYPE_MARY = 4 | ||
|
||
BLOCK_HEADER_TYPE_MARY = 3 | ||
|
||
TX_TYPE_MARY = 3 | ||
) | ||
|
||
type MaryBlock struct { | ||
|
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,89 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
ouroboros "github.com/cloudstruct/go-ouroboros-network" | ||
"github.com/cloudstruct/go-ouroboros-network/block" | ||
"github.com/cloudstruct/go-ouroboros-network/protocol/localtxsubmission" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
type localTxSubmissionState struct { | ||
submitResponse chan bool | ||
} | ||
|
||
var localTxSubmitState localTxSubmissionState | ||
|
||
type localTxSubmissionFlags struct { | ||
flagset *flag.FlagSet | ||
txFile string | ||
} | ||
|
||
func newLocalTxSubmissionFlags() *localTxSubmissionFlags { | ||
f := &localTxSubmissionFlags{ | ||
flagset: flag.NewFlagSet("local-tx-submission", flag.ExitOnError), | ||
} | ||
f.flagset.StringVar(&f.txFile, "tx-file", "", "path to the transaction file to submit") | ||
return f | ||
} | ||
|
||
func buildLocalTxSubmissionCallbackConfig() *localtxsubmission.CallbackConfig { | ||
return &localtxsubmission.CallbackConfig{ | ||
AcceptTxFunc: localTxSubmissionAcceptTxHandler, | ||
RejectTxFunc: localTxSubmissionRejectTxHandler, | ||
} | ||
} | ||
|
||
func testLocalTxSubmission(o *ouroboros.Ouroboros, f *globalFlags) { | ||
localTxSubmissionFlags := newLocalTxSubmissionFlags() | ||
err := localTxSubmissionFlags.flagset.Parse(f.flagset.Args()[1:]) | ||
if err != nil { | ||
fmt.Printf("failed to parse subcommand args: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
localTxSubmitState.submitResponse = make(chan bool) | ||
|
||
txData, err := ioutil.ReadFile(localTxSubmissionFlags.txFile) | ||
if err != nil { | ||
fmt.Printf("Failed to load transaction file: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
var jsonData map[string]string | ||
err = json.Unmarshal(txData, &jsonData) | ||
if err != nil { | ||
fmt.Printf("failed to parse transaction file: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
txBytes, err := hex.DecodeString(jsonData["cborHex"]) | ||
if err != nil { | ||
fmt.Printf("failed to decode transaction: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
if err = o.LocalTxSubmission.SubmitTx(block.TX_TYPE_ALONZO, txBytes); err != nil { | ||
fmt.Printf("Error submitting transaction: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Wait for response | ||
<-localTxSubmitState.submitResponse | ||
} | ||
|
||
func localTxSubmissionAcceptTxHandler() error { | ||
fmt.Print("The transaction was accepted\n") | ||
localTxSubmitState.submitResponse <- true | ||
return nil | ||
} | ||
|
||
func localTxSubmissionRejectTxHandler(reason interface{}) error { | ||
fmt.Printf("The transaction was rejected: %#v\n", reason) | ||
os.Exit(1) | ||
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
Oops, something went wrong.