s/CallTx/AppTx/g; NamedPlugins

This commit is contained in:
Jae Kwon
2016-03-27 12:47:50 -07:00
parent 601a654b7d
commit fa39c9da5c
9 changed files with 153 additions and 131 deletions
+41 -1
View File
@@ -9,16 +9,19 @@ import (
// if any gas is left the user is
type Plugin interface {
SetOption(key string, value string) (log string)
CallTx(ctx CallContext, txBytes []byte) (res tmsp.Result)
RunTx(ctx CallContext, txBytes []byte) (res tmsp.Result)
Query(query []byte) (res tmsp.Result)
Commit() (res tmsp.Result)
}
type NamedPlugin struct {
Byte byte
Name string
Plugin
}
//----------------------------------------
type CallContext struct {
Cache AccountCacher
Caller *Account
@@ -34,3 +37,40 @@ func NewCallContext(cache AccountCacher, caller *Account, value int64, gas *int6
Gas: gas,
}
}
//----------------------------------------
type Plugins struct {
byByte map[byte]Plugin
byName map[string]Plugin
plist []NamedPlugin
}
func NewPlugins() *Plugins {
return &Plugins{
byByte: make(map[byte]Plugin),
byName: make(map[string]Plugin),
}
}
func (pgz *Plugins) RegisterPlugin(typeByte byte, name string, plugin Plugin) {
pgz.byByte[typeByte] = plugin
pgz.byName[name] = plugin
pgz.plist = append(pgz.plist, NamedPlugin{
Byte: typeByte,
Name: name,
Plugin: plugin,
})
}
func (pgz *Plugins) GetByByte(typeByte byte) Plugin {
return pgz.byByte[typeByte]
}
func (pgz *Plugins) GetByName(name string) Plugin {
return pgz.byName[name]
}
func (pgz *Plugins) GetList() []NamedPlugin {
return pgz.plist
}
+27 -56
View File
@@ -1,14 +1,12 @@
package types
import (
"bytes"
"encoding/json"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
tmsp "github.com/tendermint/tmsp/types"
"golang.org/x/crypto/ripemd160"
)
/*
@@ -16,7 +14,7 @@ Tx (Transaction) is an atomic operation on the ledger state.
Account Types:
- SendTx Send coins to address
- CallTx Send a msg to a contract that runs in the vm
- AppTx Send a msg to a contract that runs in the vm
*/
type Tx interface {
@@ -27,13 +25,13 @@ type Tx interface {
const (
// Account transactions
TxTypeSend = byte(0x01)
TxTypeCall = byte(0x02)
TxTypeApp = byte(0x02)
)
var _ = wire.RegisterInterface(
struct{ Tx }{},
wire.ConcreteType{&SendTx{}, TxTypeSend},
wire.ConcreteType{&CallTx{}, TxTypeCall},
wire.ConcreteType{&AppTx{}, TxTypeApp},
)
//-----------------------------------------------------------------------------
@@ -56,11 +54,6 @@ func (txIn TxInput) ValidateBasic() tmsp.Result {
return tmsp.OK
}
func (txIn TxInput) SignBytes() []byte {
return []byte(Fmt(`{"address":"%X","amount":%v,"sequence":%v}`,
txIn.Address, txIn.Amount, txIn.Sequence))
}
func (txIn TxInput) String() string {
return Fmt("TxInput{%X,%v,%v,%v,%v}", txIn.Address, txIn.Amount, txIn.Sequence, txIn.Signature, txIn.PubKey)
}
@@ -82,11 +75,6 @@ func (txOut TxOutput) ValidateBasic() tmsp.Result {
return tmsp.OK
}
func (txOut TxOutput) SignBytes() []byte {
return []byte(Fmt(`{"address":"%X","amount":%v}`,
txOut.Address, txOut.Amount))
}
func (txOut TxOutput) String() string {
return Fmt("TxOutput{%X,%v}", txOut.Address, txOut.Amount)
}
@@ -99,24 +87,17 @@ type SendTx struct {
}
func (tx *SendTx) SignBytes(chainID string) []byte {
var buf = new(bytes.Buffer)
buf.Write([]byte(Fmt(`{"chain_id":%s`, jsonEscape(chainID))))
buf.Write([]byte(Fmt(`,"tx":[%v,{"inputs":[`, TxTypeSend)))
for i, in := range tx.Inputs {
buf.Write(in.SignBytes())
if i != len(tx.Inputs)-1 {
buf.Write([]byte(","))
}
signBytes := wire.BinaryBytes(chainID)
sigz := make([]crypto.Signature, len(tx.Inputs))
for i, input := range tx.Inputs {
sigz[i] = input.Signature
tx.Inputs[i].Signature = nil
}
buf.Write([]byte(`],"outputs":[`))
for i, out := range tx.Outputs {
buf.Write(out.SignBytes())
if i != len(tx.Outputs)-1 {
buf.Write([]byte(","))
}
signBytes = append(signBytes, wire.BinaryBytes(tx)...)
for i := range tx.Inputs {
tx.Inputs[i].Signature = sigz[i]
}
buf.Write([]byte(`]}]}`))
return buf.Bytes()
return signBytes
}
func (tx *SendTx) String() string {
@@ -125,35 +106,25 @@ func (tx *SendTx) String() string {
//-----------------------------------------------------------------------------
type CallTx struct {
Input TxInput `json:"input"`
Address []byte `json:"address"`
GasLimit int64 `json:"gas_limit"`
Fee int64 `json:"fee"`
Data []byte `json:"data"`
type AppTx struct {
Type byte `json:"type"` // Which app
Gas int64 `json:"gas"`
Fee int64 `json:"fee"`
Input TxInput `json:"input"`
Data []byte `json:"data"`
}
func (tx *CallTx) SignBytes(chainID string) []byte {
var buf = new(bytes.Buffer)
buf.Write([]byte(Fmt(`{"chain_id":%s`, jsonEscape(chainID))))
buf.Write([]byte(Fmt(`,"tx":[%v,{"address":"%X","data":"%X"`, TxTypeCall, tx.Address, tx.Data)))
buf.Write([]byte(Fmt(`,"fee":%v,"gas_limit":%v,"input":`, tx.Fee, tx.GasLimit)))
buf.Write(tx.Input.SignBytes())
buf.Write([]byte(`}]}`))
return buf.Bytes()
func (tx *AppTx) SignBytes(chainID string) []byte {
signBytes := wire.BinaryBytes(chainID)
sig := tx.Input.Signature
tx.Input.Signature = nil
signBytes = append(signBytes, wire.BinaryBytes(tx)...)
tx.Input.Signature = sig
return signBytes
}
func (tx *CallTx) String() string {
return Fmt("CallTx{%v -> %x: %x}", tx.Input, tx.Address, tx.Data)
}
func NewContractAddress(caller []byte, nonce int) []byte {
temp := make([]byte, 32+8)
copy(temp, caller)
PutInt64BE(temp[32:], int64(nonce))
hasher := ripemd160.New()
hasher.Write(temp) // does not error
return hasher.Sum(nil)
func (tx *AppTx) String() string {
return Fmt("AppTx{%v %v %v %v -> %X}", tx.Type, tx.Gas, tx.Fee, tx.Input, tx.Data)
}
//-----------------------------------------------------------------------------
+14 -16
View File
@@ -34,31 +34,29 @@ func TestSendTxSignable(t *testing.T) {
},
}
signBytes := sendTx.SignBytes(chainID)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%s","tx":[1,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"outputs":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
chainID)
if signStr != expected {
t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signStr)
signBytesHex := Fmt("%X", signBytes)
expected := "010A746573745F636861696E0101020106696E7075743100000000000030390301093200000106696E70757432000000000000006F01DE0000010201076F757470757431000000000000014D01076F75747075743200000000000001BC"
if signBytesHex != expected {
t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)
}
}
func TestCallTxSignable(t *testing.T) {
callTx := &CallTx{
func TestAppTxSignable(t *testing.T) {
callTx := &AppTx{
Type: 0x01,
Gas: 111,
Fee: 222,
Input: TxInput{
Address: []byte("input1"),
Amount: 12345,
Sequence: 67890,
},
Address: []byte("contract1"),
GasLimit: 111,
Fee: 222,
Data: []byte("data1"),
Data: []byte("data1"),
}
signBytes := callTx.SignBytes(chainID)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%s","tx":[2,{"address":"636F6E747261637431","data":"6461746131","fee":222,"gas_limit":111,"input":{"address":"696E70757431","amount":12345,"sequence":67890}}]}`,
chainID)
if signStr != expected {
t.Errorf("Got unexpected sign string for CallTx. Expected:\n%v\nGot:\n%v", expected, signStr)
signBytesHex := Fmt("%X", signBytes)
expected := "010A746573745F636861696E0101000000000000006F00000000000000DE0106696E70757431000000000000303903010932000001056461746131"
if signBytesHex != expected {
t.Errorf("Got unexpected sign string for AppTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)
}
}