diff --git a/account/account.go b/account/account.go index 9a0121ebc122d794379aae6b1c96b8052a2f3a32..99b545f212a8c3be9322eecdf74fe6d8ad3b4f10 100644 --- a/account/account.go +++ b/account/account.go @@ -28,7 +28,6 @@ import ( ptypes "github.com/eris-ltd/eris-db/permission/types" . "github.com/tendermint/go-common" "github.com/tendermint/go-crypto" - "github.com/tendermint/go-merkle" "github.com/tendermint/go-wire" ) @@ -45,12 +44,8 @@ func SignBytes(chainID string, o Signable) []byte { if *err != nil { PanicCrisis(err) } - return buf.Bytes() -} -// HashSignBytes is a convenience method for getting the hash of the bytes of a signable -func HashSignBytes(chainID string, o Signable) []byte { - return merkle.SimpleHashFromBinary(SignBytes(chainID, o)) + return buf.Bytes() } //----------------------------------------------------------------------------- diff --git a/core/types/types.go b/core/types/types.go index b84b844ac3d7dbe373927bd33e6d00cd25f0dba3..b08678d4d1ea63334e23122b8462c768fa9f9a2b 100644 --- a/core/types/types.go +++ b/core/types/types.go @@ -25,7 +25,6 @@ import ( "github.com/tendermint/tendermint/types" account "github.com/eris-ltd/eris-db/account" - transaction "github.com/eris-ltd/eris-db/txs" ) type ( @@ -158,21 +157,6 @@ type ( GasUsed int64 `json:"gas_used"` // TODO ... } - - // UnconfirmedTxs - UnconfirmedTxs struct { - Txs []transaction.Tx `json:"txs"` - } - - // BroadcastTx or Transact - Receipt struct { - TxHash []byte `json:"tx_hash"` - CreatesContract uint8 `json:"creates_contract"` - ContractAddr []byte `json:"contract_addr"` - } - - TransactionResult struct { - } ) func FromRoundState(rs *csus.RoundState) *ConsensusState { @@ -191,7 +175,16 @@ func FromRoundState(rs *csus.RoundState) *ConsensusState { //------------------------------------------------------------------------------ // copied in from NameReg -type ResultListNames struct { - BlockHeight int `json:"block_height"` - Names []*transaction.NameRegEntry `json:"names"` -} +type ( + NameRegEntry struct { + Name string `json:"name"` // registered name for the entry + Owner []byte `json:"owner"` // address that created the entry + Data string `json:"data"` // data to store under this name + Expires int `json:"expires"` // block at which this entry expires + } + + ResultListNames struct { + BlockHeight int `json:"block_height"` + Names []*NameRegEntry `json:"names"` + } +) diff --git a/definitions/pipe.go b/definitions/pipe.go index 83953e1467422c5aaba98a75bbb60247984f3f57..f2f7ba8057650c8ad21bdf2b1bf3d244b6ccd524 100644 --- a/definitions/pipe.go +++ b/definitions/pipe.go @@ -28,10 +28,11 @@ import ( tendermint_types "github.com/tendermint/tendermint/types" account "github.com/eris-ltd/eris-db/account" + core_types "github.com/eris-ltd/eris-db/core/types" types "github.com/eris-ltd/eris-db/core/types" event "github.com/eris-ltd/eris-db/event" manager_types "github.com/eris-ltd/eris-db/manager/types" - transaction "github.com/eris-ltd/eris-db/txs" + "github.com/eris-ltd/eris-db/txs" ) type Pipe interface { @@ -45,6 +46,7 @@ type Pipe interface { // NOTE: [ben] added to Pipe interface on 0.12 refactor GetApplication() manager_types.Application SetConsensusEngine(consensus ConsensusEngine) error + GetConsensusEngine() ConsensusEngine // Support for Tendermint RPC GetTendermintPipe() (TendermintPipe, error) } @@ -74,7 +76,7 @@ type Consensus interface { } type NameReg interface { - Entry(key string) (*transaction.NameRegEntry, error) + Entry(key string) (*core_types.NameRegEntry, error) Entries([]*event.FilterData) (*types.ResultListNames, error) } @@ -93,14 +95,14 @@ type Transactor interface { CallCode(fromAddress, code, data []byte) (*types.Call, error) // Send(privKey, toAddress []byte, amount int64) (*types.Receipt, error) // SendAndHold(privKey, toAddress []byte, amount int64) (*types.Receipt, error) - BroadcastTx(tx transaction.Tx) (*types.Receipt, error) + BroadcastTx(tx txs.Tx) (*txs.Receipt, error) Transact(privKey, address, data []byte, gasLimit, - fee int64) (*types.Receipt, error) + fee int64) (*txs.Receipt, error) TransactAndHold(privKey, address, data []byte, gasLimit, - fee int64) (*transaction.EventDataCall, error) + fee int64) (*txs.EventDataCall, error) TransactNameReg(privKey []byte, name, data string, amount, - fee int64) (*types.Receipt, error) - UnconfirmedTxs() (*types.UnconfirmedTxs, error) - SignTx(tx transaction.Tx, - privAccounts []*account.PrivAccount) (transaction.Tx, error) + fee int64) (*txs.Receipt, error) + UnconfirmedTxs() (*txs.UnconfirmedTxs, error) + SignTx(tx txs.Tx, + privAccounts []*account.PrivAccount) (txs.Tx, error) } diff --git a/definitions/tendermint_pipe.go b/definitions/tendermint_pipe.go index 9627901f95f8432c51e2e5fc8cb59a965f7b72b7..01d5d849827ae233d70302012ce69e41cf1fb2bd 100644 --- a/definitions/tendermint_pipe.go +++ b/definitions/tendermint_pipe.go @@ -19,7 +19,7 @@ package definitions import ( account "github.com/eris-ltd/eris-db/account" rpc_tendermint_types "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" - transaction "github.com/eris-ltd/eris-db/txs" + "github.com/eris-ltd/eris-db/txs" ) // NOTE: [ben] TendermintPipe is the additional pipe to carry over @@ -57,7 +57,7 @@ type TendermintPipe interface { // TODO: [ben] deprecate as we should not allow unsafe behaviour // where a user is allowed to send a private key over the wire, // especially unencrypted. - SignTransaction(tx transaction.Tx, + SignTransaction(tx txs.Tx, privAccounts []*account.PrivAccount) (*rpc_tendermint_types.ResultSignTx, error) @@ -67,9 +67,9 @@ type TendermintPipe interface { ListNames() (*rpc_tendermint_types.ResultListNames, error) // Memory pool - BroadcastTxAsync(transaction transaction.Tx) (*rpc_tendermint_types.ResultBroadcastTx, + BroadcastTxAsync(transaction txs.Tx) (*rpc_tendermint_types.ResultBroadcastTx, error) - BroadcastTxSync(transaction transaction.Tx) (*rpc_tendermint_types.ResultBroadcastTx, + BroadcastTxSync(transaction txs.Tx) (*rpc_tendermint_types.ResultBroadcastTx, error) } diff --git a/manager/eris-mint/eris-mint.go b/manager/eris-mint/eris-mint.go index 656493b7b078beb9a4c7a043f80904139b3ab520..fb86908318454066ad3ba61f10d7e2cd2b49ed50 100644 --- a/manager/eris-mint/eris-mint.go +++ b/manager/eris-mint/eris-mint.go @@ -23,7 +23,7 @@ import ( "sync" tendermint_events "github.com/tendermint/go-events" - client "github.com/tendermint/go-rpc/client" + rpcclient "github.com/tendermint/go-rpc/client" wire "github.com/tendermint/go-wire" ctypes "github.com/tendermint/tendermint/rpc/core/types" tmsp "github.com/tendermint/tmsp/types" @@ -32,7 +32,7 @@ import ( sm "github.com/eris-ltd/eris-db/manager/eris-mint/state" manager_types "github.com/eris-ltd/eris-db/manager/types" - types "github.com/eris-ltd/eris-db/txs" + "github.com/eris-ltd/eris-db/txs" ) //-------------------------------------------------------------------------------- @@ -51,7 +51,7 @@ type ErisMint struct { evsw *tendermint_events.EventSwitch // client to the tendermint core rpc - client *client.ClientURI + client *rpcclient.ClientURI host string // tendermint core endpoint nTxs int // count txs in a block @@ -80,16 +80,16 @@ func (app *ErisMint) GetCheckCache() *sm.BlockCache { func (app *ErisMint) SetHostAddress(host string) { app.host = host - app.client = client.NewClientURI(host) //fmt.Sprintf("http://%s", host)) + app.client = rpcclient.NewClientURI(host) //fmt.Sprintf("http://%s", host)) } // Broadcast a tx to the tendermint core // NOTE: this assumes we know the address of core -func (app *ErisMint) BroadcastTx(tx types.Tx) error { +func (app *ErisMint) BroadcastTx(tx txs.Tx) error { buf := new(bytes.Buffer) var n int var err error - wire.WriteBinary(struct{ types.Tx }{tx}, buf, &n, &err) + wire.WriteBinary(struct{ txs.Tx }{tx}, buf, &n, &err) if err != nil { return err } @@ -131,7 +131,7 @@ func (app *ErisMint) AppendTx(txBytes []byte) (res tmsp.Result) { // XXX: if we had tx ids we could cache the decoded txs on CheckTx var n int var err error - tx := new(types.Tx) + tx := new(txs.Tx) buf := bytes.NewBuffer(txBytes) wire.ReadBinaryPtr(tx, buf, len(txBytes), &n, &err) if err != nil { @@ -144,15 +144,17 @@ func (app *ErisMint) AppendTx(txBytes []byte) (res tmsp.Result) { if err != nil { return tmsp.NewError(tmsp.CodeType_InternalError, fmt.Sprintf("Internal error: %v", err)) } - // TODO: need to return receipt so rpc.ResultBroadcastTx.Data (or Log) is the receipt - return tmsp.NewResultOK(nil, "Success") + + receipt := txs.GenerateReceipt(app.state.ChainID, *tx) + receiptBytes := wire.BinaryBytes(receipt) + return tmsp.NewResultOK(receiptBytes, "Success") } // Implements manager/types.Application func (app *ErisMint) CheckTx(txBytes []byte) (res tmsp.Result) { var n int var err error - tx := new(types.Tx) + tx := new(txs.Tx) buf := bytes.NewBuffer(txBytes) wire.ReadBinaryPtr(tx, buf, len(txBytes), &n, &err) if err != nil { @@ -166,9 +168,9 @@ func (app *ErisMint) CheckTx(txBytes []byte) (res tmsp.Result) { if err != nil { return tmsp.NewError(tmsp.CodeType_InternalError, fmt.Sprintf("Internal error: %v", err)) } - - // TODO: need to return receipt so rpc.ResultBroadcastTx.Data (or Log) is the receipt - return tmsp.NewResultOK(nil, "Success") + receipt := txs.GenerateReceipt(app.state.ChainID, *tx) + receiptBytes := wire.BinaryBytes(receipt) + return tmsp.NewResultOK(receiptBytes, "Success") } // Implements manager/types.Application diff --git a/manager/eris-mint/namereg.go b/manager/eris-mint/namereg.go index 0fd28700be2a171ff80a15bb238a9326fb704253..25d95ffad3f97d19b654e8917c2e45a47900087d 100644 --- a/manager/eris-mint/namereg.go +++ b/manager/eris-mint/namereg.go @@ -25,7 +25,6 @@ import ( "sync" sm "github.com/eris-ltd/eris-db/manager/eris-mint/state" - "github.com/eris-ltd/eris-db/txs" core_types "github.com/eris-ltd/eris-db/core/types" event "github.com/eris-ltd/eris-db/event" @@ -68,7 +67,7 @@ func newNameReg(erisMint *ErisMint) *namereg { return &namereg{erisMint, ff} } -func (this *namereg) Entry(key string) (*txs.NameRegEntry, error) { +func (this *namereg) Entry(key string) (*core_types.NameRegEntry, error) { st := this.erisMint.GetState() // performs a copy entry := st.GetNameRegEntry(key) if entry == nil { @@ -79,7 +78,7 @@ func (this *namereg) Entry(key string) (*txs.NameRegEntry, error) { func (this *namereg) Entries(filters []*event.FilterData) (*core_types.ResultListNames, error) { var blockHeight int - var names []*txs.NameRegEntry + var names []*core_types.NameRegEntry state := this.erisMint.GetState() blockHeight = state.LastBlockHeight filter, err := this.filterFactory.NewFilter(filters) @@ -97,8 +96,8 @@ func (this *namereg) Entries(filters []*event.FilterData) (*core_types.ResultLis } type ResultListNames struct { - BlockHeight int `json:"block_height"` - Names []*txs.NameRegEntry `json:"names"` + BlockHeight int `json:"block_height"` + Names []*core_types.NameRegEntry `json:"names"` } // Filter for namereg name. This should not be used to get individual entries by name. @@ -130,7 +129,7 @@ func (this *NameRegNameFilter) Configure(fd *event.FilterData) error { } func (this *NameRegNameFilter) Match(v interface{}) bool { - nre, ok := v.(*txs.NameRegEntry) + nre, ok := v.(*core_types.NameRegEntry) if !ok { return false } @@ -169,7 +168,7 @@ func (this *NameRegOwnerFilter) Configure(fd *event.FilterData) error { } func (this *NameRegOwnerFilter) Match(v interface{}) bool { - nre, ok := v.(*txs.NameRegEntry) + nre, ok := v.(*core_types.NameRegEntry) if !ok { return false } @@ -205,7 +204,7 @@ func (this *NameRegDataFilter) Configure(fd *event.FilterData) error { } func (this *NameRegDataFilter) Match(v interface{}) bool { - nre, ok := v.(*txs.NameRegEntry) + nre, ok := v.(*core_types.NameRegEntry) if !ok { return false } @@ -236,7 +235,7 @@ func (this *NameRegExpiresFilter) Configure(fd *event.FilterData) error { } func (this *NameRegExpiresFilter) Match(v interface{}) bool { - nre, ok := v.(*txs.NameRegEntry) + nre, ok := v.(*core_types.NameRegEntry) if !ok { return false } diff --git a/manager/eris-mint/pipe.go b/manager/eris-mint/pipe.go index 07734946316ab0e3336d40026cc76a4f4e061a0b..02c06af91bf94046a928f4d611e0c3971c7e49c7 100644 --- a/manager/eris-mint/pipe.go +++ b/manager/eris-mint/pipe.go @@ -32,6 +32,7 @@ import ( account "github.com/eris-ltd/eris-db/account" config "github.com/eris-ltd/eris-db/config" + core_types "github.com/eris-ltd/eris-db/core/types" definitions "github.com/eris-ltd/eris-db/definitions" event "github.com/eris-ltd/eris-db/event" vm "github.com/eris-ltd/eris-db/manager/eris-mint/evm" @@ -39,7 +40,7 @@ import ( state_types "github.com/eris-ltd/eris-db/manager/eris-mint/state/types" manager_types "github.com/eris-ltd/eris-db/manager/types" rpc_tendermint_types "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" - transaction "github.com/eris-ltd/eris-db/txs" + "github.com/eris-ltd/eris-db/txs" ) type ErisMintPipe struct { @@ -211,6 +212,10 @@ func (pipe *ErisMintPipe) SetConsensusEngine( return nil } +func (pipe *ErisMintPipe) GetConsensusEngine() definitions.ConsensusEngine { + return pipe.consensusEngine +} + func (pipe *ErisMintPipe) GetTendermintPipe() (definitions.TendermintPipe, error) { return definitions.TendermintPipe(pipe), nil @@ -385,7 +390,7 @@ func (pipe *ErisMintPipe) CallCode(fromAddress, code, data []byte) (*rpc_tenderm // TODO: [ben] deprecate as we should not allow unsafe behaviour // where a user is allowed to send a private key over the wire, // especially unencrypted. -func (pipe *ErisMintPipe) SignTransaction(tx transaction.Tx, +func (pipe *ErisMintPipe) SignTransaction(tx txs.Tx, privAccounts []*account.PrivAccount) (*rpc_tendermint_types.ResultSignTx, error) { @@ -395,18 +400,18 @@ func (pipe *ErisMintPipe) SignTransaction(tx transaction.Tx, } } switch tx.(type) { - case *transaction.SendTx: - sendTx := tx.(*transaction.SendTx) + case *txs.SendTx: + sendTx := tx.(*txs.SendTx) for i, input := range sendTx.Inputs { input.PubKey = privAccounts[i].PubKey input.Signature = privAccounts[i].Sign(pipe.transactor.chainID, sendTx) } - case *transaction.CallTx: - callTx := tx.(*transaction.CallTx) + case *txs.CallTx: + callTx := tx.(*txs.CallTx) callTx.Input.PubKey = privAccounts[0].PubKey callTx.Input.Signature = privAccounts[0].Sign(pipe.transactor.chainID, callTx) - case *transaction.BondTx: - bondTx := tx.(*transaction.BondTx) + case *txs.BondTx: + bondTx := tx.(*txs.BondTx) // the first privaccount corresponds to the BondTx pub key. // the rest to the inputs bondTx.Signature = privAccounts[0].Sign(pipe.transactor.chainID, bondTx).(crypto.SignatureEd25519) @@ -414,11 +419,11 @@ func (pipe *ErisMintPipe) SignTransaction(tx transaction.Tx, input.PubKey = privAccounts[i+1].PubKey input.Signature = privAccounts[i+1].Sign(pipe.transactor.chainID, bondTx) } - case *transaction.UnbondTx: - unbondTx := tx.(*transaction.UnbondTx) + case *txs.UnbondTx: + unbondTx := tx.(*txs.UnbondTx) unbondTx.Signature = privAccounts[0].Sign(pipe.transactor.chainID, unbondTx).(crypto.SignatureEd25519) - case *transaction.RebondTx: - rebondTx := tx.(*transaction.RebondTx) + case *txs.RebondTx: + rebondTx := tx.(*txs.RebondTx) rebondTx.Signature = privAccounts[0].Sign(pipe.transactor.chainID, rebondTx).(crypto.SignatureEd25519) } return &rpc_tendermint_types.ResultSignTx{tx}, nil @@ -436,7 +441,7 @@ func (pipe *ErisMintPipe) GetName(name string) (*rpc_tendermint_types.ResultGetN func (pipe *ErisMintPipe) ListNames() (*rpc_tendermint_types.ResultListNames, error) { var blockHeight int - var names []*transaction.NameRegEntry + var names []*core_types.NameRegEntry currentState := pipe.erisMint.GetState() blockHeight = currentState.LastBlockHeight currentState.GetNames().Iterate(func(key []byte, value []byte) bool { @@ -447,29 +452,29 @@ func (pipe *ErisMintPipe) ListNames() (*rpc_tendermint_types.ResultListNames, er } // Memory pool -// NOTE: transaction must be signed -func (pipe *ErisMintPipe) BroadcastTxAsync(tx transaction.Tx) ( +// NOTE: txs must be signed +func (pipe *ErisMintPipe) BroadcastTxAsync(tx txs.Tx) ( *rpc_tendermint_types.ResultBroadcastTx, error) { - err := pipe.consensusEngine.BroadcastTransaction(transaction.EncodeTx(tx), nil) + err := pipe.consensusEngine.BroadcastTransaction(txs.EncodeTx(tx), nil) if err != nil { - return nil, fmt.Errorf("Error broadcasting transaction: %v", err) + return nil, fmt.Errorf("Error broadcasting txs: %v", err) } return &rpc_tendermint_types.ResultBroadcastTx{}, nil } -func (pipe *ErisMintPipe) BroadcastTxSync(tx transaction.Tx) (*rpc_tendermint_types.ResultBroadcastTx, +func (pipe *ErisMintPipe) BroadcastTxSync(tx txs.Tx) (*rpc_tendermint_types.ResultBroadcastTx, error) { responseChannel := make(chan *tmsp_types.Response, 1) - err := pipe.consensusEngine.BroadcastTransaction(transaction.EncodeTx(tx), + err := pipe.consensusEngine.BroadcastTransaction(txs.EncodeTx(tx), func(res *tmsp_types.Response) { responseChannel <- res }) if err != nil { - return nil, fmt.Errorf("Error broadcasting transaction: %v", err) + return nil, fmt.Errorf("Error broadcasting txs: %v", err) } - // NOTE: [ben] This Response is set in /tmsp/client/local_remote_client.go + // NOTE: [ben] This Response is set in /consensus/tendermint/local_client.go // a call to Application, here implemented by ErisMint, over local callback, // or TMSP RPC call. Hence the result is determined by ErisMint/erismint.go // CheckTx() Result (Result converted to ReqRes into Response returned here) - // NOTE: [ben] BroadcastTx wraps around CheckTx for Tendermint + // NOTE: [ben] BroadcastTx just calls CheckTx in Tendermint (oddly... [Silas]) response := <-responseChannel responseCheckTx := response.GetCheckTx() if responseCheckTx == nil { @@ -480,6 +485,7 @@ func (pipe *ErisMintPipe) BroadcastTxSync(tx transaction.Tx) (*rpc_tendermint_ty Data: responseCheckTx.Data, Log: responseCheckTx.Log, } + fmt.Println("MARMOT resultBroadcastTx", resultBroadCastTx) switch responseCheckTx.Code { case tmsp_types.CodeType_OK: return resultBroadCastTx, nil diff --git a/manager/eris-mint/state/block_cache.go b/manager/eris-mint/state/block_cache.go index 1cefa6a3b18b996800a2d89e0670675ee14dd1d2..31210114e07acf57ab95aab0b1ec7b996c193445 100644 --- a/manager/eris-mint/state/block_cache.go +++ b/manager/eris-mint/state/block_cache.go @@ -9,7 +9,7 @@ import ( "github.com/tendermint/go-merkle" acm "github.com/eris-ltd/eris-db/account" - "github.com/eris-ltd/eris-db/txs" + core_types "github.com/eris-ltd/eris-db/core/types" ) func makeStorage(db dbm.DB, root []byte) merkle.Tree { @@ -120,7 +120,7 @@ func (cache *BlockCache) SetStorage(addr Word256, key Word256, value Word256) { //------------------------------------- // BlockCache.names -func (cache *BlockCache) GetNameRegEntry(name string) *txs.NameRegEntry { +func (cache *BlockCache) GetNameRegEntry(name string) *core_types.NameRegEntry { entry, removed, _ := cache.names[name].unpack() if removed { return nil @@ -133,7 +133,7 @@ func (cache *BlockCache) GetNameRegEntry(name string) *txs.NameRegEntry { } } -func (cache *BlockCache) UpdateNameRegEntry(entry *txs.NameRegEntry) { +func (cache *BlockCache) UpdateNameRegEntry(entry *core_types.NameRegEntry) { name := entry.Name cache.names[name] = nameInfo{entry, false, true} } @@ -278,11 +278,11 @@ func (stjInfo storageInfo) unpack() (Word256, bool) { } type nameInfo struct { - name *txs.NameRegEntry + name *core_types.NameRegEntry removed bool dirty bool } -func (nInfo nameInfo) unpack() (*txs.NameRegEntry, bool, bool) { +func (nInfo nameInfo) unpack() (*core_types.NameRegEntry, bool, bool) { return nInfo.name, nInfo.removed, nInfo.dirty } diff --git a/manager/eris-mint/state/execution.go b/manager/eris-mint/state/execution.go index 252fce6efe40d45638bf216e1cda69fc73b0c290..9d2aaa2e96965554b813a9f7171050b751acd737 100644 --- a/manager/eris-mint/state/execution.go +++ b/manager/eris-mint/state/execution.go @@ -12,6 +12,7 @@ import ( "github.com/eris-ltd/eris-db/manager/eris-mint/evm" ptypes "github.com/eris-ltd/eris-db/permission/types" // for GlobalPermissionAddress ... + core_types "github.com/eris-ltd/eris-db/core/types" "github.com/eris-ltd/eris-db/txs" ) @@ -473,7 +474,7 @@ func ExecTx(blockCache *BlockCache, tx txs.Tx, runCall bool, evc events.Fireable // Write caller/callee to txCache. txCache.UpdateAccount(caller) txCache.UpdateAccount(callee) - vmach := vm.NewVM(txCache, params, caller.Address, txs.TxID(_s.ChainID, tx)) + vmach := vm.NewVM(txCache, params, caller.Address, txs.TxHash(_s.ChainID, tx)) vmach.SetFireable(evc) // NOTE: Call() transfers the value from caller to callee iff call succeeds. ret, err = vmach.Call(caller, callee, code, tx.Data, value, &gas) @@ -615,7 +616,7 @@ func ExecTx(blockCache *BlockCache, tx txs.Tx, runCall bool, evc events.Fireable return errors.New(Fmt("Names must be registered for at least %d blocks", txs.MinNameRegistrationPeriod)) } // entry does not exist, so create it - entry = &txs.NameRegEntry{ + entry = &core_types.NameRegEntry{ Name: tx.Name, Owner: tx.Input.Address, Data: tx.Data, diff --git a/manager/eris-mint/state/state.go b/manager/eris-mint/state/state.go index 582c573af9cee3dadcf42b7eaed86a6b1fddd96f..a36bc8ec2f607eaa505fcf8b1b7ad66c04a44033 100644 --- a/manager/eris-mint/state/state.go +++ b/manager/eris-mint/state/state.go @@ -10,7 +10,7 @@ import ( acm "github.com/eris-ltd/eris-db/account" . "github.com/eris-ltd/eris-db/manager/eris-mint/state/types" ptypes "github.com/eris-ltd/eris-db/permission/types" - txs "github.com/eris-ltd/eris-db/txs" + "github.com/eris-ltd/eris-db/txs" . "github.com/tendermint/go-common" dbm "github.com/tendermint/go-db" @@ -18,6 +18,7 @@ import ( "github.com/tendermint/go-merkle" "github.com/tendermint/go-wire" + core_types "github.com/eris-ltd/eris-db/core/types" "github.com/tendermint/tendermint/types" ) @@ -335,7 +336,7 @@ func (s *State) LoadStorage(hash []byte) (storage merkle.Tree) { //------------------------------------- // State.nameReg -func (s *State) GetNameRegEntry(name string) *txs.NameRegEntry { +func (s *State) GetNameRegEntry(name string) *core_types.NameRegEntry { _, valueBytes, _ := s.nameReg.Get([]byte(name)) if valueBytes == nil { return nil @@ -344,14 +345,14 @@ func (s *State) GetNameRegEntry(name string) *txs.NameRegEntry { return DecodeNameRegEntry(valueBytes) } -func DecodeNameRegEntry(entryBytes []byte) *txs.NameRegEntry { +func DecodeNameRegEntry(entryBytes []byte) *core_types.NameRegEntry { var n int var err error value := NameRegCodec.Decode(bytes.NewBuffer(entryBytes), &n, &err) - return value.(*txs.NameRegEntry) + return value.(*core_types.NameRegEntry) } -func (s *State) UpdateNameRegEntry(entry *txs.NameRegEntry) bool { +func (s *State) UpdateNameRegEntry(entry *core_types.NameRegEntry) bool { w := new(bytes.Buffer) var n int var err error @@ -374,11 +375,11 @@ func (s *State) SetNameReg(nameReg merkle.Tree) { } func NameRegEncoder(o interface{}, w io.Writer, n *int, err *error) { - wire.WriteBinary(o.(*txs.NameRegEntry), w, n, err) + wire.WriteBinary(o.(*core_types.NameRegEntry), w, n, err) } func NameRegDecoder(r io.Reader, n *int, err *error) interface{} { - return wire.ReadBinary(&txs.NameRegEntry{}, r, txs.MaxDataLength, n, err) + return wire.ReadBinary(&core_types.NameRegEntry{}, r, txs.MaxDataLength, n, err) } var NameRegCodec = wire.Codec{ diff --git a/manager/eris-mint/state/state_test.go b/manager/eris-mint/state/state_test.go index eba2226bba2854d62851fea37896fb5648811b74..9dcd465d19bdcd4a00acbbbd882a458800994549 100644 --- a/manager/eris-mint/state/state_test.go +++ b/manager/eris-mint/state/state_test.go @@ -8,6 +8,7 @@ import ( "github.com/tendermint/tendermint/config/tendermint_test" // tmtypes "github.com/tendermint/tendermint/types" + core_types "github.com/eris-ltd/eris-db/core/types" "github.com/eris-ltd/eris-db/txs" ) @@ -252,7 +253,8 @@ func TestNameTxs(t *testing.T) { } } - validateEntry := func(t *testing.T, entry *txs.NameRegEntry, name, data string, addr []byte, expires int) { + validateEntry := func(t *testing.T, entry *core_types.NameRegEntry, name, + data string, addr []byte, expires int) { if entry == nil { t.Fatalf("Could not find name %s", name) diff --git a/manager/eris-mint/transactor.go b/manager/eris-mint/transactor.go index a3f515329a8ff9a40e26ce1264c7c6ab8ea126dd..e9882f77cbab3a9f10211a09d575ad78e7f14317 100644 --- a/manager/eris-mint/transactor.go +++ b/manager/eris-mint/transactor.go @@ -120,14 +120,14 @@ func (this *transactor) CallCode(fromAddress, code, data []byte) ( } // Broadcast a transaction. -func (this *transactor) BroadcastTx(tx txs.Tx) (*core_types.Receipt, error) { +func (this *transactor) BroadcastTx(tx txs.Tx) (*txs.Receipt, error) { err := this.erisMint.BroadcastTx(tx) if err != nil { return nil, fmt.Errorf("Error broadcasting transaction: %v", err) } - txHash := txs.TxID(this.chainID, tx) + txHash := txs.TxHash(this.chainID, tx) var createsContract uint8 var contractAddr []byte // check if creates new contract @@ -137,18 +137,18 @@ func (this *transactor) BroadcastTx(tx txs.Tx) (*core_types.Receipt, error) { contractAddr = state.NewContractAddress(callTx.Input.Address, callTx.Input.Sequence) } } - return &core_types.Receipt{txHash, createsContract, contractAddr}, nil + return &txs.Receipt{txHash, createsContract, contractAddr}, nil } // Get all unconfirmed txs. -func (this *transactor) UnconfirmedTxs() (*core_types.UnconfirmedTxs, error) { +func (this *transactor) UnconfirmedTxs() (*txs.UnconfirmedTxs, error) { // TODO-RPC - return &core_types.UnconfirmedTxs{}, nil + return &txs.UnconfirmedTxs{}, nil } // Orders calls to BroadcastTx using lock (waits for response from core before releasing) func (this *transactor) Transact(privKey, address, data []byte, gasLimit, - fee int64) (*core_types.Receipt, error) { + fee int64) (*txs.Receipt, error) { var addr []byte if len(address) == 0 { addr = nil @@ -236,7 +236,7 @@ func (this *transactor) TransactAndHold(privKey, address, data []byte, gasLimit, } func (this *transactor) TransactNameReg(privKey []byte, name, data string, - amount, fee int64) (*core_types.Receipt, error) { + amount, fee int64) (*txs.Receipt, error) { if len(privKey) != 64 { return nil, fmt.Errorf("Private key is not of the right length: %d\n", len(privKey)) diff --git a/rpc/tendermint/client/client.go b/rpc/tendermint/client/client.go index a8efbc20508de1e96b4e91db1ce98b24d4451298..75731a20aa5b2cc560f85b7f434a41653dc5e015 100644 --- a/rpc/tendermint/client/client.go +++ b/rpc/tendermint/client/client.go @@ -1,15 +1,17 @@ package client import ( - rpcclient "github.com/tendermint/go-rpc/client" - + "fmt" acm "github.com/eris-ltd/eris-db/account" - ctypes "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" + core_types "github.com/eris-ltd/eris-db/core/types" + rpc_types "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" "github.com/eris-ltd/eris-db/txs" + rpcclient "github.com/tendermint/go-rpc/client" + "github.com/tendermint/go-wire" ) -func Status(client rpcclient.Client) (*ctypes.ResultStatus, error) { - var res ctypes.ErisDBResult //ResultStatus) +func Status(client rpcclient.Client) (*rpc_types.ResultStatus, error) { + var res rpc_types.ErisDBResult //ResultStatus) var err error switch cli := client.(type) { case *rpcclient.ClientJSONRPC: @@ -20,11 +22,11 @@ func Status(client rpcclient.Client) (*ctypes.ResultStatus, error) { if err != nil { return nil, err } - return res.(*ctypes.ResultStatus), nil + return res.(*rpc_types.ResultStatus), nil } func GenPrivAccount(client rpcclient.Client) (*acm.PrivAccount, error) { - var res ctypes.ErisDBResult + var res rpc_types.ErisDBResult var err error switch cli := client.(type) { case *rpcclient.ClientJSONRPC: @@ -35,128 +37,151 @@ func GenPrivAccount(client rpcclient.Client) (*acm.PrivAccount, error) { if err != nil { return nil, err } - return res.(*ctypes.ResultGenPrivAccount).PrivAccount, nil + return res.(*rpc_types.ResultGenPrivAccount).PrivAccount, nil } func GetAccount(client rpcclient.Client, addr []byte) (*acm.Account, error) { - var res ctypes.ErisDBResult + var res rpc_types.ErisDBResult var err error switch cli := client.(type) { case *rpcclient.ClientJSONRPC: _, err = cli.Call("get_account", []interface{}{addr}, &res) case *rpcclient.ClientURI: - _, err = cli.Call("get_account", map[string]interface{}{"address": addr}, &res) + _, err = cli.Call("get_account", map[string]interface{}{"address": addr}, + &res) } if err != nil { return nil, err } - return res.(*ctypes.ResultGetAccount).Account, nil + return res.(*rpc_types.ResultGetAccount).Account, nil } -func SignTx(client rpcclient.Client, tx txs.Tx, privAccs []*acm.PrivAccount) (txs.Tx, error) { +func SignTx(client rpcclient.Client, tx txs.Tx, + privAccs []*acm.PrivAccount) (txs.Tx, error) { wrapTx := struct { txs.Tx `json:"unwrap"` }{tx} - var res ctypes.ErisDBResult + var res rpc_types.ErisDBResult var err error switch cli := client.(type) { case *rpcclient.ClientJSONRPC: _, err = cli.Call("unsafe/sign_tx", []interface{}{wrapTx, privAccs}, &res) case *rpcclient.ClientURI: - _, err = cli.Call("unsafe/sign_tx", map[string]interface{}{"tx": wrapTx, "privAccounts": privAccs}, &res) + _, err = cli.Call("unsafe/sign_tx", map[string]interface{}{ + "tx": wrapTx, + "privAccounts": privAccs, + }, &res) } if err != nil { return nil, err } - return res.(*ctypes.ResultSignTx).Tx, nil + return res.(*rpc_types.ResultSignTx).Tx, nil } -func BroadcastTx(client rpcclient.Client, tx txs.Tx) (ctypes.Receipt, error) { +func BroadcastTx(client rpcclient.Client, + tx txs.Tx) (txs.Receipt, error) { wrapTx := struct { txs.Tx `json:"unwrap"` }{tx} - var res ctypes.ErisDBResult + var res rpc_types.ErisDBResult var err error switch cli := client.(type) { case *rpcclient.ClientJSONRPC: _, err = cli.Call("broadcast_tx", []interface{}{wrapTx}, &res) case *rpcclient.ClientURI: - _, err = cli.Call("broadcast_tx", map[string]interface{}{"tx": wrapTx}, &res) + _, err = cli.Call("broadcast_tx", map[string]interface{}{"tx": wrapTx}, + &res) } if err != nil { - return ctypes.Receipt{}, err + return txs.Receipt{}, err } - data := res.(*ctypes.ResultBroadcastTx).Data - // TODO: unmarshal data to receuipt - _ = data - return ctypes.Receipt{}, err + receiptBytes := res.(*rpc_types.ResultBroadcastTx).Data + receipt := txs.Receipt{} + err = wire.ReadBinaryBytes(receiptBytes, &receipt) + fmt.Printf("rec: %#v\n", receipt) + return receipt, err } -func DumpStorage(client rpcclient.Client, addr []byte) (*ctypes.ResultDumpStorage, error) { - var res ctypes.ErisDBResult +func DumpStorage(client rpcclient.Client, + addr []byte) (*rpc_types.ResultDumpStorage, error) { + var res rpc_types.ErisDBResult var err error switch cli := client.(type) { case *rpcclient.ClientJSONRPC: _, err = cli.Call("dump_storage", []interface{}{addr}, &res) case *rpcclient.ClientURI: - _, err = cli.Call("dump_storage", map[string]interface{}{"address": addr}, &res) + _, err = cli.Call("dump_storage", map[string]interface{}{"address": addr}, + &res) } if err != nil { return nil, err } - return res.(*ctypes.ResultDumpStorage), err + return res.(*rpc_types.ResultDumpStorage), err } func GetStorage(client rpcclient.Client, addr, key []byte) ([]byte, error) { - var res ctypes.ErisDBResult + var res rpc_types.ErisDBResult var err error switch cli := client.(type) { case *rpcclient.ClientJSONRPC: _, err = cli.Call("get_storage", []interface{}{addr, key}, &res) case *rpcclient.ClientURI: - _, err = cli.Call("get_storage", map[string]interface{}{"address": addr, "key": key}, &res) + _, err = cli.Call("get_storage", map[string]interface{}{ + "address": addr, + "key": key, + }, &res) } if err != nil { return nil, err } - return res.(*ctypes.ResultGetStorage).Value, nil + return res.(*rpc_types.ResultGetStorage).Value, nil } -func CallCode(client rpcclient.Client, fromAddress, code, data []byte) (*ctypes.ResultCall, error) { - var res ctypes.ErisDBResult +func CallCode(client rpcclient.Client, + fromAddress, code, data []byte) (*rpc_types.ResultCall, error) { + var res rpc_types.ErisDBResult var err error switch cli := client.(type) { case *rpcclient.ClientJSONRPC: _, err = cli.Call("call_code", []interface{}{fromAddress, code, data}, &res) case *rpcclient.ClientURI: - _, err = cli.Call("call_code", map[string]interface{}{"fromAddress": fromAddress, "code": code, "data": data}, &res) + _, err = cli.Call("call_code", map[string]interface{}{ + "fromAddress": fromAddress, + "code": code, + "data": data, + }, &res) } if err != nil { return nil, err } - return res.(*ctypes.ResultCall), err + return res.(*rpc_types.ResultCall), err } -func Call(client rpcclient.Client, fromAddress, toAddress, data []byte) (*ctypes.ResultCall, error) { - var res ctypes.ErisDBResult +func Call(client rpcclient.Client, fromAddress, toAddress, + data []byte) (*rpc_types.ResultCall, error) { + var res rpc_types.ErisDBResult var err error switch cli := client.(type) { case *rpcclient.ClientJSONRPC: _, err = cli.Call("call", []interface{}{fromAddress, toAddress, data}, &res) case *rpcclient.ClientURI: - _, err = cli.Call("call", map[string]interface{}{"fromAddress": fromAddress, "toAddress": toAddress, "data": data}, &res) + _, err = cli.Call("call", map[string]interface{}{ + "fromAddress": fromAddress, + "toAddress": toAddress, + "data": data, + }, &res) } if err != nil { return nil, err } - return res.(*ctypes.ResultCall), err + return res.(*rpc_types.ResultCall), err } -func GetName(client rpcclient.Client, name string) (*txs.NameRegEntry, error) { - var res ctypes.ErisDBResult +func GetName(client rpcclient.Client, name string) (*core_types.NameRegEntry, error) { + var res rpc_types.ErisDBResult var err error switch cli := client.(type) { case *rpcclient.ClientJSONRPC: @@ -167,5 +192,5 @@ func GetName(client rpcclient.Client, name string) (*txs.NameRegEntry, error) { if err != nil { return nil, err } - return res.(*ctypes.ResultGetName).Entry, nil + return res.(*rpc_types.ResultGetName).Entry, nil } diff --git a/rpc/tendermint/core/routes.go b/rpc/tendermint/core/routes.go index 19752f367ad1291223d6e7e42c753934e79883e2..ea041d305f0e0a3b6258a2cd2c04555db6f806e2 100644 --- a/rpc/tendermint/core/routes.go +++ b/rpc/tendermint/core/routes.go @@ -4,7 +4,7 @@ import ( "fmt" acm "github.com/eris-ltd/eris-db/account" - definitions "github.com/eris-ltd/eris-db/definitions" + "github.com/eris-ltd/eris-db/definitions" ctypes "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" "github.com/eris-ltd/eris-db/txs" rpc "github.com/tendermint/go-rpc/server" @@ -159,11 +159,11 @@ func (this *TendermintRoutes) ListNamesResult() (ctypes.ErisDBResult, error) { } func (this *TendermintRoutes) GenPrivAccountResult() (ctypes.ErisDBResult, error) { - // if r, err := this.tendermintPipe.GenPrivAccount(); err != nil { - // return nil, err - // } else { - // return r, nil - // } + //if r, err := this.tendermintPipe.GenPrivAccount(); err != nil { + // return nil, err + //} else { + // return r, nil + //} return nil, fmt.Errorf("Unimplemented as poor practice to generate private account over unencrypted RPC") } diff --git a/rpc/tendermint/core/types/responses.go b/rpc/tendermint/core/types/responses.go index 4f9a00faf85a417f7b7a1d568f135f5cfd7813d2..4c48f7ab7a5410ef1dc6cb488218fa54e88895f8 100644 --- a/rpc/tendermint/core/types/responses.go +++ b/rpc/tendermint/core/types/responses.go @@ -2,8 +2,9 @@ package core_types import ( acm "github.com/eris-ltd/eris-db/account" + core_types "github.com/eris-ltd/eris-db/core/types" stypes "github.com/eris-ltd/eris-db/manager/eris-mint/state/types" - txtypes "github.com/eris-ltd/eris-db/txs" + "github.com/eris-ltd/eris-db/txs" "github.com/tendermint/tendermint/types" "github.com/tendermint/go-crypto" @@ -81,8 +82,8 @@ type ResultDumpConsensusState struct { } type ResultListNames struct { - BlockHeight int `json:"block_height"` - Names []*txtypes.NameRegEntry `json:"names"` + BlockHeight int `json:"block_height"` + Names []*core_types.NameRegEntry `json:"names"` } type ResultGenPrivAccount struct { @@ -99,19 +100,13 @@ type ResultBroadcastTx struct { Log string `json:"log"` } -type Receipt struct { - TxHash []byte `json:"tx_hash"` - CreatesContract uint8 `json:"creates_contract"` - ContractAddr []byte `json:"contract_addr"` -} - type ResultListUnconfirmedTxs struct { - N int `json:"n_txs"` - Txs []txtypes.Tx `json:"txs"` + N int `json:"n_txs"` + Txs []txs.Tx `json:"txs"` } type ResultGetName struct { - Entry *txtypes.NameRegEntry `json:"entry"` + Entry *core_types.NameRegEntry `json:"entry"` } type ResultGenesis struct { @@ -119,7 +114,7 @@ type ResultGenesis struct { } type ResultSignTx struct { - Tx txtypes.Tx `json:"tx"` + Tx txs.Tx `json:"tx"` } type ResultEvent struct { diff --git a/rpc/tendermint/test/client_rpc_test.go b/rpc/tendermint/test/client_rpc_test.go index eb8a0945748f1fe8cb5dfd0359166eb5cc88290f..12514eae673bf96ecf075abacba2a0261cab0ff0 100644 --- a/rpc/tendermint/test/client_rpc_test.go +++ b/rpc/tendermint/test/client_rpc_test.go @@ -15,13 +15,6 @@ func TestHTTPStatus(t *testing.T) { testStatus(t, "HTTP") } -func TestHTTPGenPriv(t *testing.T) { - if testing.Short() { - t.Skip("skipping test in short mode.") - } - testGenPriv(t, "HTTP") -} - func TestHTTPGetAccount(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") @@ -29,13 +22,6 @@ func TestHTTPGetAccount(t *testing.T) { testGetAccount(t, "HTTP") } -func TestHTTPSignedTx(t *testing.T) { - if testing.Short() { - t.Skip("skipping test in short mode.") - } - testSignedTx(t, "HTTP") -} - func TestHTTPBroadcastTx(t *testing.T) { testBroadcastTx(t, "HTTP") } @@ -75,13 +61,6 @@ func TestJSONStatus(t *testing.T) { testStatus(t, "JSONRPC") } -func TestJSONGenPriv(t *testing.T) { - if testing.Short() { - t.Skip("skipping test in short mode.") - } - testGenPriv(t, "JSONRPC") -} - func TestJSONGetAccount(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") @@ -89,13 +68,6 @@ func TestJSONGetAccount(t *testing.T) { testGetAccount(t, "JSONRPC") } -func TestJSONSignedTx(t *testing.T) { - if testing.Short() { - t.Skip("skipping test in short mode.") - } - testSignedTx(t, "JSONRPC") -} - func TestJSONBroadcastTx(t *testing.T) { testBroadcastTx(t, "JSONRPC") } diff --git a/rpc/tendermint/test/client_ws_test.go b/rpc/tendermint/test/client_ws_test.go index 7cb64ecd9a0e3a9b90f5724252c4b23c4b579fee..d00343728fb4674d8c81344576cf7c5cba55ea05 100644 --- a/rpc/tendermint/test/client_ws_test.go +++ b/rpc/tendermint/test/client_ws_test.go @@ -207,6 +207,6 @@ func TestWSCallCall(t *testing.T) { waitForEvent(t, wsc, eid1, true, func() { tx := makeDefaultCallTx(t, wsTyp, contractAddr2, nil, amt, gasLim, fee) broadcastTx(t, wsTyp, tx) - *txid = txs.TxID(chainID, tx) + *txid = txs.TxHash(chainID, tx) }, unmarshalValidateCall(user[0].Address, returnVal, txid)) } diff --git a/rpc/tendermint/test/runner/main.go b/rpc/tendermint/test/runner/main.go index b610d564c64c77164cd51211a93cbd5be036091a..73883109df0bd67e91d119cebbc4b4b2bdf1938d 100644 --- a/rpc/tendermint/test/runner/main.go +++ b/rpc/tendermint/test/runner/main.go @@ -8,6 +8,7 @@ import ( ) func main() { + //fmt.Printf("%s", util.IsAddress("hello")) fmt.Printf("%s", util.IsAddress("hello"), rpctest.Successor(2)) //defer os.Exit(0) } diff --git a/rpc/tendermint/test/shared.go b/rpc/tendermint/test/shared.go index 374d6aa5871f3841a9f8c2308ee5f2da13cfad3b..530f1c6054382acefdc736b8e66f74b9c30842bc 100644 --- a/rpc/tendermint/test/shared.go +++ b/rpc/tendermint/test/shared.go @@ -7,18 +7,18 @@ import ( acm "github.com/eris-ltd/eris-db/account" "github.com/eris-ltd/eris-db/core" + core_types "github.com/eris-ltd/eris-db/core/types" edbcli "github.com/eris-ltd/eris-db/rpc/tendermint/client" - ctypes "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" + rpc_types "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" "github.com/eris-ltd/eris-db/server" "github.com/eris-ltd/eris-db/test/fixtures" "github.com/eris-ltd/eris-db/txs" - . "github.com/tendermint/go-common" "github.com/tendermint/go-crypto" rpcclient "github.com/tendermint/go-rpc/client" "github.com/spf13/viper" - nm "github.com/tendermint/tendermint/node" + tm_common "github.com/tendermint/go-common" "github.com/tendermint/tendermint/types" "path" ) @@ -27,7 +27,6 @@ import ( var ( config = server.DefaultServerConfig() rootWorkDir string - node *nm.Node mempoolCount = 0 chainID string websocketAddr string @@ -126,7 +125,8 @@ func saveNewPriv() { //------------------------------------------------------------------------------- // some default transaction functions -func makeDefaultSendTx(t *testing.T, typ string, addr []byte, amt int64) *txs.SendTx { +func makeDefaultSendTx(t *testing.T, typ string, addr []byte, + amt int64) *txs.SendTx { nonce := getNonce(t, typ, user[0].Address) tx := txs.NewSendTx() tx.AddInputWithNonce(user[0].PubKey, amt, nonce+1) @@ -134,20 +134,24 @@ func makeDefaultSendTx(t *testing.T, typ string, addr []byte, amt int64) *txs.Se return tx } -func makeDefaultSendTxSigned(t *testing.T, typ string, addr []byte, amt int64) *txs.SendTx { +func makeDefaultSendTxSigned(t *testing.T, typ string, addr []byte, + amt int64) *txs.SendTx { tx := makeDefaultSendTx(t, typ, addr, amt) tx.SignInput(chainID, 0, user[0]) return tx } -func makeDefaultCallTx(t *testing.T, typ string, addr, code []byte, amt, gasLim, fee int64) *txs.CallTx { +func makeDefaultCallTx(t *testing.T, typ string, addr, code []byte, amt, gasLim, + fee int64) *txs.CallTx { nonce := getNonce(t, typ, user[0].Address) - tx := txs.NewCallTxWithNonce(user[0].PubKey, addr, code, amt, gasLim, fee, nonce+1) + tx := txs.NewCallTxWithNonce(user[0].PubKey, addr, code, amt, gasLim, fee, + nonce+1) tx.Sign(chainID, user[0]) return tx } -func makeDefaultNameTx(t *testing.T, typ string, name, value string, amt, fee int64) *txs.NameTx { +func makeDefaultNameTx(t *testing.T, typ string, name, value string, amt, + fee int64) *txs.NameTx { nonce := getNonce(t, typ, user[0].Address) tx := txs.NewNameTxWithNonce(user[0].PubKey, name, value, amt, fee, nonce+1) tx.Sign(chainID, user[0]) @@ -181,7 +185,8 @@ func getAccount(t *testing.T, typ string, addr []byte) *acm.Account { } // sign transaction -func signTx(t *testing.T, typ string, tx txs.Tx, privAcc *acm.PrivAccount) txs.Tx { +func signTx(t *testing.T, typ string, tx txs.Tx, + privAcc *acm.PrivAccount) txs.Tx { client := clients[typ] signedTx, err := edbcli.SignTx(client, tx, []*acm.PrivAccount{privAcc}) if err != nil { @@ -191,7 +196,7 @@ func signTx(t *testing.T, typ string, tx txs.Tx, privAcc *acm.PrivAccount) txs.T } // broadcast transaction -func broadcastTx(t *testing.T, typ string, tx txs.Tx) ctypes.Receipt { +func broadcastTx(t *testing.T, typ string, tx txs.Tx) txs.Receipt { client := clients[typ] rec, err := edbcli.BroadcastTx(client, tx) if err != nil { @@ -202,7 +207,7 @@ func broadcastTx(t *testing.T, typ string, tx txs.Tx) ctypes.Receipt { } // dump all storage for an account. currently unused -func dumpStorage(t *testing.T, addr []byte) *ctypes.ResultDumpStorage { +func dumpStorage(t *testing.T, addr []byte) *rpc_types.ResultDumpStorage { client := clients["HTTP"] resp, err := edbcli.DumpStorage(client, addr) if err != nil { @@ -220,32 +225,34 @@ func getStorage(t *testing.T, typ string, addr, key []byte) []byte { return resp } -func callCode(t *testing.T, client rpcclient.Client, fromAddress, code, data, expected []byte) { +func callCode(t *testing.T, client rpcclient.Client, fromAddress, code, data, + expected []byte) { resp, err := edbcli.CallCode(client, fromAddress, code, data) if err != nil { t.Fatal(err) } ret := resp.Return // NOTE: we don't flip memory when it comes out of RETURN (?!) - if bytes.Compare(ret, LeftPadWord256(expected).Bytes()) != 0 { + if bytes.Compare(ret, tm_common.LeftPadWord256(expected).Bytes()) != 0 { t.Fatalf("Conflicting return value. Got %x, expected %x", ret, expected) } } -func callContract(t *testing.T, client rpcclient.Client, fromAddress, toAddress, data, expected []byte) { +func callContract(t *testing.T, client rpcclient.Client, fromAddress, toAddress, + data, expected []byte) { resp, err := edbcli.Call(client, fromAddress, toAddress, data) if err != nil { t.Fatal(err) } ret := resp.Return // NOTE: we don't flip memory when it comes out of RETURN (?!) - if bytes.Compare(ret, LeftPadWord256(expected).Bytes()) != 0 { + if bytes.Compare(ret, tm_common.LeftPadWord256(expected).Bytes()) != 0 { t.Fatalf("Conflicting return value. Got %x, expected %x", ret, expected) } } // get the namereg entry -func getNameRegEntry(t *testing.T, typ string, name string) *txs.NameRegEntry { +func getNameRegEntry(t *testing.T, typ string, name string) *core_types.NameRegEntry { client := clients[typ] entry, err := edbcli.GetName(client, name) if err != nil { @@ -257,7 +264,8 @@ func getNameRegEntry(t *testing.T, typ string, name string) *txs.NameRegEntry { //-------------------------------------------------------------------------------- // utility verification function -func checkTx(t *testing.T, fromAddr []byte, priv *acm.PrivAccount, tx *txs.SendTx) { +func checkTx(t *testing.T, fromAddr []byte, priv *acm.PrivAccount, + tx *txs.SendTx) { if bytes.Compare(tx.Inputs[0].Address, fromAddr) != 0 { t.Fatal("Tx input addresses don't match!") } @@ -279,19 +287,21 @@ func checkTx(t *testing.T, fromAddr []byte, priv *acm.PrivAccount, tx *txs.SendT // simple contract returns 5 + 6 = 0xb func simpleContract() ([]byte, []byte, []byte) { // this is the code we want to run when the contract is called - contractCode := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3} + contractCode := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, + 0x60, 0x0, 0xf3} // the is the code we need to return the contractCode when the contract is initialized lenCode := len(contractCode) // push code to the stack //code := append([]byte{byte(0x60 + lenCode - 1)}, RightPadWord256(contractCode).Bytes()...) - code := append([]byte{0x7f}, RightPadWord256(contractCode).Bytes()...) + code := append([]byte{0x7f}, + tm_common.RightPadWord256(contractCode).Bytes()...) // store it in memory code = append(code, []byte{0x60, 0x0, 0x52}...) // return whats in memory //code = append(code, []byte{0x60, byte(32 - lenCode), 0x60, byte(lenCode), 0xf3}...) code = append(code, []byte{0x60, byte(lenCode), 0x60, 0x0, 0xf3}...) // return init code, contract code, expected return - return code, contractCode, LeftPadBytes([]byte{0xb}, 32) + return code, contractCode, tm_common.LeftPadBytes([]byte{0xb}, 32) } // simple call contract calls another contract @@ -301,9 +311,11 @@ func simpleCallContract(addr []byte) ([]byte, []byte, []byte) { inOff, inSize := byte(0x0), byte(0x0) // no call data retOff, retSize := byte(0x0), byte(0x20) // this is the code we want to run (call a contract and return) - contractCode := []byte{0x60, retSize, 0x60, retOff, 0x60, inSize, 0x60, inOff, 0x60, value, 0x73} + contractCode := []byte{0x60, retSize, 0x60, retOff, 0x60, inSize, 0x60, inOff, + 0x60, value, 0x73} contractCode = append(contractCode, addr...) - contractCode = append(contractCode, []byte{0x61, gas1, gas2, 0xf1, 0x60, 0x20, 0x60, 0x0, 0xf3}...) + contractCode = append(contractCode, []byte{0x61, gas1, gas2, 0xf1, 0x60, 0x20, + 0x60, 0x0, 0xf3}...) // the is the code we need to return; the contractCode when the contract is initialized // it should copy the code from the input into memory @@ -317,5 +329,5 @@ func simpleCallContract(addr []byte) ([]byte, []byte, []byte) { code = append(code, []byte{0x60, byte(lenCode), 0x60, 0x0, 0xf3}...) code = append(code, contractCode...) // return init code, contract code, expected return - return code, contractCode, LeftPadBytes([]byte{0xb}, 32) + return code, contractCode, tm_common.LeftPadBytes([]byte{0xb}, 32) } diff --git a/rpc/tendermint/test/tests.go b/rpc/tendermint/test/tests.go index a200f56a7780251d02d1be82cefed143c15caccf..f09a552431ee8efc83bba5438b7dd32d4c5a16ed 100644 --- a/rpc/tendermint/test/tests.go +++ b/rpc/tendermint/test/tests.go @@ -8,7 +8,8 @@ import ( edbcli "github.com/eris-ltd/eris-db/rpc/tendermint/client" "github.com/eris-ltd/eris-db/txs" - . "github.com/tendermint/go-common" + tm_common "github.com/tendermint/go-common" + "golang.org/x/crypto/ripemd160" ) var doNothing = func(eid string, b interface{}) error { return nil } @@ -26,17 +27,6 @@ func testStatus(t *testing.T, typ string) { } } -func testGenPriv(t *testing.T, typ string) { - client := clients[typ] - privAcc, err := edbcli.GenPrivAccount(client) - if err != nil { - t.Fatal(err) - } - if len(privAcc.Address) == 0 { - t.Fatal("Failed to generate an address") - } -} - func testGetAccount(t *testing.T, typ string) { acc := getAccount(t, typ, user[0].Address) if acc == nil { @@ -47,24 +37,12 @@ func testGetAccount(t *testing.T, typ string) { } } -func testSignedTx(t *testing.T, typ string) { - amt := int64(100) - toAddr := user[1].Address - testOneSignTx(t, typ, toAddr, amt) - - toAddr = user[2].Address - testOneSignTx(t, typ, toAddr, amt) - - toAddr = user[3].Address - testOneSignTx(t, typ, toAddr, amt) -} - func testOneSignTx(t *testing.T, typ string, addr []byte, amt int64) { tx := makeDefaultSendTx(t, typ, addr, amt) tx2 := signTx(t, typ, tx, user[0]) - tx2hash := txs.TxID(chainID, tx2) + tx2hash := txs.TxHash(chainID, tx2) tx.SignInput(chainID, 0, user[0]) - txhash := txs.TxID(chainID, tx) + txhash := txs.TxHash(chainID, tx) if bytes.Compare(txhash, tx2hash) != 0 { t.Fatal("Got different signatures for signing via rpc vs tx_utils") } @@ -85,18 +63,18 @@ func testBroadcastTx(t *testing.T, typ string) { if len(receipt.TxHash) == 0 { t.Fatal("Failed to compute tx hash") } - pool := node.MempoolReactor().Mempool - trnxs := pool.Reap(-1) - if len(trnxs) != mempoolCount { - t.Fatalf("The mem pool has %d txs. Expected %d", len(trnxs), mempoolCount) - } - tx2 := txs.DecodeTx(trnxs[mempoolCount-1]).(*txs.SendTx) n, err := new(int), new(error) - buf1, buf2 := new(bytes.Buffer), new(bytes.Buffer) - tx.WriteSignBytes(chainID, buf1, n, err) - tx2.WriteSignBytes(chainID, buf2, n, err) - if bytes.Compare(buf1.Bytes(), buf2.Bytes()) != 0 { - t.Fatal("inconsistent hashes for mempool tx and sent tx") + buf := new(bytes.Buffer) + hasher := ripemd160.New() + tx.WriteSignBytes(chainID, buf, n, err) + // [Silas] Currently tx.TxHash uses go-wire, we we drop that we can drop the prefix here + goWireBytes := append([]byte{0x01, 0xcf}, buf.Bytes()...) + hasher.Write(goWireBytes) + txHashExpected := hasher.Sum(nil) + if bytes.Compare(receipt.TxHash, txHashExpected) != 0 { + t.Fatalf("The receipt hash '%x' does not equal the ripemd160 hash of the "+ + "transaction signed bytes calculated in the test: '%x'", + receipt.TxHash, txHashExpected) } } @@ -129,10 +107,11 @@ func testGetStorage(t *testing.T, typ string) { mempoolCount = 0 v := getStorage(t, typ, contractAddr, []byte{0x1}) - got := LeftPadWord256(v) - expected := LeftPadWord256([]byte{0x5}) + got := tm_common.LeftPadWord256(v) + expected := tm_common.LeftPadWord256([]byte{0x5}) if got.Compare(expected) != 0 { - t.Fatalf("Wrong storage value. Got %x, expected %x", got.Bytes(), expected.Bytes()) + t.Fatalf("Wrong storage value. Got %x, expected %x", got.Bytes(), + expected.Bytes()) } } @@ -140,14 +119,17 @@ func testCallCode(t *testing.T, typ string) { client := clients[typ] // add two integers and return the result - code := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3} + code := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, + 0x0, 0xf3} data := []byte{} expected := []byte{0xb} callCode(t, client, user[0].PubKey.Address(), code, data, expected) // pass two ints as calldata, add, and return the result - code = []byte{0x60, 0x0, 0x35, 0x60, 0x20, 0x35, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3} - data = append(LeftPadWord256([]byte{0x5}).Bytes(), LeftPadWord256([]byte{0x6}).Bytes()...) + code = []byte{0x60, 0x0, 0x35, 0x60, 0x20, 0x35, 0x1, 0x60, 0x0, 0x52, 0x60, + 0x20, 0x60, 0x0, 0xf3} + data = append(tm_common.LeftPadWord256([]byte{0x5}).Bytes(), + tm_common.LeftPadWord256([]byte{0x6}).Bytes()...) expected = []byte{0xb} callCode(t, client, user[0].PubKey.Address(), code, data, expected) } diff --git a/rpc/v0/json_service.go b/rpc/v0/json_service.go index e7226487d58613cd50961b827d257e57078aeab9..adce33f211aec98d9c5eb9c339596ec2fc8424d5 100644 --- a/rpc/v0/json_service.go +++ b/rpc/v0/json_service.go @@ -27,7 +27,8 @@ func NewJsonRpcServer(service server.HttpService) *JsonRpcServer { } // Start adds the rpc path to the router. -func (this *JsonRpcServer) Start(config *server.ServerConfig, router *gin.Engine) { +func (this *JsonRpcServer) Start(config *server.ServerConfig, + router *gin.Engine) { router.POST(config.HTTP.JsonRpcEndpoint, this.handleFunc) this.running = true } @@ -85,13 +86,15 @@ func (this *ErisDbJsonService) Process(r *http.Request, w http.ResponseWriter) { // Error when decoding. if errU != nil { - this.writeError("Failed to parse request: "+errU.Error(), "", rpc_tendermint.PARSE_ERROR, w) + this.writeError("Failed to parse request: "+errU.Error(), "", + rpc_tendermint.PARSE_ERROR, w) return } // Wrong protocol version. if req.JSONRPC != "2.0" { - this.writeError("Wrong protocol version: "+req.JSONRPC, req.Id, rpc_tendermint.INVALID_REQUEST, w) + this.writeError("Wrong protocol version: "+req.JSONRPC, req.Id, + rpc_tendermint.INVALID_REQUEST, w) return } @@ -137,7 +140,8 @@ func (this *ErisDbJsonService) writeResponse(id string, result interface{}, w ht // *************************************** Events ************************************ // Subscribe to an event. -func (this *ErisDbJsonService) EventSubscribe(request *rpc_tendermint.RPCRequest, requester interface{}) (interface{}, int, error) { +func (this *ErisDbJsonService) EventSubscribe(request *rpc_tendermint.RPCRequest, + requester interface{}) (interface{}, int, error) { param := &EventIdParam{} err := json.Unmarshal(request.Params, param) if err != nil { @@ -152,7 +156,8 @@ func (this *ErisDbJsonService) EventSubscribe(request *rpc_tendermint.RPCRequest } // Un-subscribe from an event. -func (this *ErisDbJsonService) EventUnsubscribe(request *rpc_tendermint.RPCRequest, requester interface{}) (interface{}, int, error) { +func (this *ErisDbJsonService) EventUnsubscribe(request *rpc_tendermint.RPCRequest, + requester interface{}) (interface{}, int, error) { param := &SubIdParam{} err := json.Unmarshal(request.Params, param) if err != nil { @@ -168,7 +173,8 @@ func (this *ErisDbJsonService) EventUnsubscribe(request *rpc_tendermint.RPCReque } // Check subscription event cache for new data. -func (this *ErisDbJsonService) EventPoll(request *rpc_tendermint.RPCRequest, requester interface{}) (interface{}, int, error) { +func (this *ErisDbJsonService) EventPoll(request *rpc_tendermint.RPCRequest, + requester interface{}) (interface{}, int, error) { param := &SubIdParam{} err := json.Unmarshal(request.Params, param) if err != nil { diff --git a/test/mock/mock_web_api_test.g_ b/test/mock/mock_web_api_test.g_ index 0579640bbf5dde56ec90c22afb7da5f307e245f3..68abad450be936403d864210783d63731c603fe6 100644 --- a/test/mock/mock_web_api_test.g_ +++ b/test/mock/mock_web_api_test.g_ @@ -173,7 +173,7 @@ func (this *MockSuite) TestGetValidators() { func (this *MockSuite) TestGetNameRegEntry() { resp := this.get("/namereg/" + this.testData.GetNameRegEntry.Input.Name) - ret := &txs.NameRegEntry{} + ret := &core_types.NameRegEntry{} errD := this.codec.Decode(ret, resp.Body) this.NoError(errD) this.Equal(ret, this.testData.GetNameRegEntry.Output) diff --git a/txs/names.go b/txs/names.go index ddaec87d36486bf47766f9cb3b8c98d99c886173..657cdc15744781e86cb7d3844049a1f79c8141e1 100644 --- a/txs/names.go +++ b/txs/names.go @@ -1,6 +1,7 @@ package txs import ( + core_types "github.com/eris-ltd/eris-db/core/types" "regexp" ) @@ -42,20 +43,8 @@ func NameCostPerBlock(baseCost int64) int64 { return NameBlockCostMultiplier * NameByteCostMultiplier * baseCost } -type NameRegEntry struct { - Name string `json:"name"` // registered name for the entry - Owner []byte `json:"owner"` // address that created the entry - Data string `json:"data"` // data to store under this name - Expires int `json:"expires"` // block at which this entry expires -} - -func (entry *NameRegEntry) Copy() *NameRegEntry { - entryCopy := *entry - return &entryCopy -} - // XXX: vestige of an older time type ResultListNames struct { - BlockHeight int `json:"block_height"` - Names []*NameRegEntry `json:"names"` + BlockHeight int `json:"block_height"` + Names []*core_types.NameRegEntry `json:"names"` } diff --git a/txs/tx.go b/txs/tx.go index a67eea42827d121c6d00e6ef46290a4c8f2ecf5e..9957d570060d6c37ae266e8fc3b75adcb63e7796 100644 --- a/txs/tx.go +++ b/txs/tx.go @@ -63,10 +63,6 @@ Admin Txs: - PermissionsTx */ -type Tx interface { - WriteSignBytes(chainID string, w io.Writer, n *int, err *error) -} - // Types of Tx implementations const ( // Account transactions @@ -99,13 +95,56 @@ var _ = wire.RegisterInterface( //----------------------------------------------------------------------------- -type TxInput struct { - Address []byte `json:"address"` // Hash of the PubKey - Amount int64 `json:"amount"` // Must not exceed account balance - Sequence int `json:"sequence"` // Must be 1 greater than the last committed TxInput - Signature crypto.Signature `json:"signature"` // Depends on the PubKey type and the whole Tx - PubKey crypto.PubKey `json:"pub_key"` // Must not be nil, may be nil -} +type ( + Tx interface { + WriteSignBytes(chainID string, w io.Writer, n *int, err *error) + } + + // UnconfirmedTxs + UnconfirmedTxs struct { + Txs []Tx `json:"txs"` + } + + SendTx struct { + Inputs []*TxInput `json:"inputs"` + Outputs []*TxOutput `json:"outputs"` + } + + // BroadcastTx or Transact + Receipt struct { + TxHash []byte `json:"tx_hash"` + CreatesContract uint8 `json:"creates_contract"` + ContractAddr []byte `json:"contract_addr"` + } + + NameTx struct { + Input *TxInput `json:"input"` + Name string `json:"name"` + Data string `json:"data"` + Fee int64 `json:"fee"` + } + + CallTx struct { + Input *TxInput `json:"input"` + Address []byte `json:"address"` + GasLimit int64 `json:"gas_limit"` + Fee int64 `json:"fee"` + Data []byte `json:"data"` + } + + TxInput struct { + Address []byte `json:"address"` // Hash of the PubKey + Amount int64 `json:"amount"` // Must not exceed account balance + Sequence int `json:"sequence"` // Must be 1 greater than the last committed TxInput + Signature crypto.Signature `json:"signature"` // Depends on the PubKey type and the whole Tx + PubKey crypto.PubKey `json:"pub_key"` // Must not be nil, may be nil + } + + TxOutput struct { + Address []byte `json:"address"` // Hash of the PubKey + Amount int64 `json:"amount"` // The sum of all outputs must not exceed the inputs. + } +) func (txIn *TxInput) ValidateBasic() error { if len(txIn.Address) != 20 { @@ -127,11 +166,6 @@ func (txIn *TxInput) String() string { //----------------------------------------------------------------------------- -type TxOutput struct { - Address []byte `json:"address"` // Hash of the PubKey - Amount int64 `json:"amount"` // The sum of all outputs must not exceed the inputs. -} - func (txOut *TxOutput) ValidateBasic() error { if len(txOut.Address) != 20 { return ErrTxInvalidAddress @@ -152,11 +186,6 @@ func (txOut *TxOutput) String() string { //----------------------------------------------------------------------------- -type SendTx struct { - Inputs []*TxInput `json:"inputs"` - Outputs []*TxOutput `json:"outputs"` -} - func (tx *SendTx) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) { wire.WriteTo([]byte(Fmt(`{"chain_id":%s`, jsonEscape(chainID))), w, n, err) wire.WriteTo([]byte(Fmt(`,"tx":[%v,{"inputs":[`, TxTypeSend)), w, n, err) @@ -182,14 +211,6 @@ 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"` -} - func (tx *CallTx) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) { wire.WriteTo([]byte(Fmt(`{"chain_id":%s`, jsonEscape(chainID))), w, n, err) wire.WriteTo([]byte(Fmt(`,"tx":[%v,{"address":"%X","data":"%X"`, TxTypeCall, tx.Address, tx.Data)), w, n, err) @@ -213,13 +234,6 @@ func NewContractAddress(caller []byte, nonce int) []byte { //----------------------------------------------------------------------------- -type NameTx struct { - Input *TxInput `json:"input"` - Name string `json:"name"` - Data string `json:"data"` - Fee int64 `json:"fee"` -} - func (tx *NameTx) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) { wire.WriteTo([]byte(Fmt(`{"chain_id":%s`, jsonEscape(chainID))), w, n, err) wire.WriteTo([]byte(Fmt(`,"tx":[%v,{"data":%s,"fee":%v`, TxTypeName, jsonEscape(tx.Data), tx.Fee)), w, n, err) @@ -363,21 +377,39 @@ func (tx *PermissionsTx) String() string { //----------------------------------------------------------------------------- -// This should match the leaf hashes of Block.Data.Hash()'s SimpleMerkleTree. -func TxID(chainID string, tx Tx) []byte { +func TxHash(chainID string, tx Tx) []byte { signBytes := acm.SignBytes(chainID, tx) return wire.BinaryRipemd160(signBytes) } +// [Silas] Leaving this implementation here for when we transition +func TxHashFuture(chainID string, tx Tx) []byte { + signBytes := acm.SignBytes(chainID, tx) + hasher := ripemd160.New() + hasher.Write(signBytes) + // Calling Sum(nil) just gives us the digest with nothing prefixed + return hasher.Sum(nil) +} + //----------------------------------------------------------------------------- func EncodeTx(tx Tx) []byte { wrapTx := struct { Tx Tx `json:"unwrap"` }{tx} - return wire.JSONBytes(wrapTx) + return wire.BinaryBytes(wrapTx) } +//func EncodeTx(tx txs.Tx) []byte { +// buf := new(bytes.Buffer) +// var n int +// var err error +// wire.WriteBinary(struct{ types.Tx }{tx}, buf, &n, &err) +// if err != nil { +// return err +// } +//} + // panic on err func DecodeTx(txBytes []byte) Tx { var n int @@ -391,6 +423,22 @@ func DecodeTx(txBytes []byte) Tx { return *tx } +func GenerateReceipt(chainId string, tx Tx) Receipt { + receipt := Receipt{ + TxHash: TxHash(chainId, tx), + CreatesContract: 0, + ContractAddr: nil, + } + if callTx, ok := tx.(*CallTx); ok { + if len(callTx.Address) == 0 { + receipt.CreatesContract = 1 + receipt.ContractAddr = NewContractAddress(callTx.Input.Address, + callTx.Input.Sequence) + } + } + return receipt +} + //-------------------------------------------------------------------------------- // Contract: This function is deterministic and completely reversible.