From c017ec12a003b4034e64e30997a8936b3b37584c Mon Sep 17 00:00:00 2001
From: Robert Martin-Legene <robert@martin-legene.dk>
Date: Fri, 17 Jul 2020 04:08:50 -0300
Subject: [PATCH] Support for defining transaction types

---
 collector/Dockerfile     |   7 +-
 collector/sql.pm         |  13 +--
 postgres/10-postgres.sql | 217 ++++++++++++++++++++++++++++++---------
 3 files changed, 177 insertions(+), 60 deletions(-)

diff --git a/collector/Dockerfile b/collector/Dockerfile
index 2581bb0..a0b09ad 100644
--- a/collector/Dockerfile
+++ b/collector/Dockerfile
@@ -1,12 +1,11 @@
 FROM    debian
 RUN     useradd --create-home --shell /bin/bash bfa && mkdir /home/bfa/collector
 RUN     apt-get update && apt-get upgrade -y
-RUN     apt-get install -y libjson-perl libnet-dns-perl libdbd-pg-perl libwww-perl vim
-RUN     apt-get install -y libclass-accessor-perl
+RUN     apt-get install -y libjson-perl libnet-dns-perl libdbd-pg-perl libwww-perl libclass-accessor-perl
+WORKDIR /home/bfa
 COPY    collector.pl sql.pm /home/bfa/collector/
 RUN     chown -R bfa /home/bfa
 USER    bfa
-WORKDIR /home/bfa
-ENV     BFANODE http://public.47525974938.bfa.martin-legene.dk:8545/
 CMD     /home/bfa/collector/collector.pl
 #CMD     sleep 14d
+ENV     BFANODE http://public.47525974938.bfa.martin-legene.dk:8545/
diff --git a/collector/sql.pm b/collector/sql.pm
index be768e3..0f384ef 100644
--- a/collector/sql.pm
+++ b/collector/sql.pm
@@ -62,8 +62,8 @@ sub     insertTransactionWithContractAddress
                 (   hash,                       "blockId",    nonce,          gas,
                     "gasPrice",                 value,        "fromAccountId","toAccountId",
                     status,                     "gasUsed",    input,          inputlen,
-                    "contractaddressAccountId" )
-                VALUES  (?,?,?,?,?,?,?,?,?,?,?,?,?)
+                    "type",                     "contractaddressAccountId" )
+                VALUES  (?,?,?,?,?,?,?,?,?,?,?,?,?,?)
             ))
         );
     }
@@ -71,7 +71,7 @@ sub     insertTransactionWithContractAddress
     $_[6]                   =   $self->selectAccountIdByAddress( $_[6] );
     $_[7]                   =   $self->selectAccountIdByAddress( $_[7] );
     $_[12]                  =   $self->selectAccountIdByAddress( $_[12] );
-    $self->sth_insertTransactionWithContractAddress->execute( @_ );
+    $self->sth_insertTransactionWithContractAddress->execute( @_, 'explicit' );
 }
 
 __PACKAGE__->mk_accessors( 'sth_insertTransaction' );
@@ -85,15 +85,16 @@ sub     insertTransaction
                 INSERT INTO transaction
                 (   hash,       "blockId",  nonce,          gas,
                     "gasPrice", value,      "fromAccountId","toAccountId",
-                    status,     "gasUsed",  input,          inputlen )
-                VALUES  (?,?,?,?,?,?,?,?,?,?,?,?)
+                    status,     "gasUsed",  input,          inputlen,
+                    "type" )
+                VALUES  (?,?,?,?,?,?,?,?,?,?,?,?,?)
             ))
         );
     }
     $_[1]                   =   $self->selectBlockhashByHash( $_[1] );
     $_[6]                   =   $self->selectAccountIdByAddress( $_[6] );
     $_[7]                   =   $self->selectAccountIdByAddress( $_[7] );
-    $self->sth_insertTransaction->execute( @_ );
+    $self->sth_insertTransaction->execute( @_, 'explicit' );
 }
 
 __PACKAGE__->mk_accessors( 'sth_selectBlockhashById' );
diff --git a/postgres/10-postgres.sql b/postgres/10-postgres.sql
index eb4e4a7..20fcab0 100644
--- a/postgres/10-postgres.sql
+++ b/postgres/10-postgres.sql
@@ -2,69 +2,186 @@
 
 \connect "postgres";
 
-CREATE SEQUENCE account_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1000 CACHE 1;
+CREATE TYPE transactiontype
+    AS ENUM ( 'unknown', 'internal', 'explicit' );
+
+CREATE SEQUENCE account_id_seq
+    INCREMENT   1
+    MINVALUE    1
+    MAXVALUE    2147483647
+    START       1000
+    CACHE       1;
 CREATE TABLE "public"."account" (
-    "id" bigint DEFAULT nextval('account_id_seq') NOT NULL,
-    "address" character(42) NOT NULL,
-    "shortname" character varying(16),
-    "name" character varying(255),
-    CONSTRAINT "account_id" PRIMARY KEY ("id"),
-    CONSTRAINT "account_hash" UNIQUE ("address")
+    "id"                bigint
+        DEFAULT             nextval('account_id_seq')
+        NOT NULL,
+    "address"           character(42)
+        NOT NULL,
+    "shortname"         character varying(16),
+    "name"              character varying(255),
+    CONSTRAINT          "account_id"
+        PRIMARY KEY         ("id"),
+    CONSTRAINT          "account_hash"
+        UNIQUE              ("address")
 ) WITH (oids = false);
 
-CREATE SEQUENCE blockhash_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1000000 CACHE 1;
+CREATE SEQUENCE blockhash_id_seq
+    INCREMENT   1
+    MINVALUE    1
+    MAXVALUE    2147483647
+    START       1000000
+    CACHE       1;
 CREATE TABLE "public"."blockhash" (
-    "id" bigint DEFAULT nextval('blockhash_id_seq') NOT NULL,
-    "hash" character(66) NOT NULL,
-    CONSTRAINT "blockhash_id" PRIMARY KEY ("id"),
-    CONSTRAINT "blockhash_hash" UNIQUE ("hash")
+    "id"                bigint
+        DEFAULT             nextval('blockhash_id_seq')
+        NOT NULL,
+    "hash"              character(66)
+        NOT NULL,
+    CONSTRAINT          "blockhash_id"
+        PRIMARY KEY         ("id"),
+    CONSTRAINT          "blockhash_hash"
+        UNIQUE              ("hash")
 ) WITH (oids = false);
 
 CREATE TABLE "public"."block" (
-    "id" bigint NOT NULL,
+    "id"                bigint      NOT NULL,
     "parentBlockhashId" bigint,
-    "number" bigint NOT NULL,
-    "sealerAccountId" bigint,
-    "timestamp" bigint NOT NULL,
-    "difficulty" smallint NOT NULL,
-    "gasUsed" bigint NOT NULL,
-    "gasLimit" bigint NOT NULL,
-    "size" bigint NOT NULL,
-    CONSTRAINT "block_id" PRIMARY KEY ("id"),
-    CONSTRAINT "block_sealerAccountId_fkey" FOREIGN KEY ("sealerAccountId") REFERENCES account(id) NOT DEFERRABLE,
-    CONSTRAINT "block_id_fkey" FOREIGN KEY (id) REFERENCES blockhash(id) NOT DEFERRABLE,
-    CONSTRAINT "block_parentBlockhashId_fkey" FOREIGN KEY ("parentBlockhashId") REFERENCES blockhash(id) NOT DEFERRABLE
+    "number"            bigint      NOT NULL,
+    "sealerAccountId"   bigint,
+    "timestamp"         bigint      NOT NULL,
+    "difficulty"        smallint    NOT NULL,
+    "gasUsed"           bigint      NOT NULL,
+    "gasLimit"          bigint      NOT NULL,
+    "size"              bigint      NOT NULL,
+    CONSTRAINT          "block_id"
+        PRIMARY KEY     ("id"),
+    CONSTRAINT          "block_sealerAccountId_fkey"
+        FOREIGN KEY     ("sealerAccountId")
+        REFERENCES      account(id)
+        NOT DEFERRABLE,
+    CONSTRAINT          "block_id_fkey"
+        FOREIGN KEY     (id)
+        REFERENCES      blockhash(id)
+        NOT DEFERRABLE,
+    CONSTRAINT          "block_parentBlockhashId_fkey"
+        FOREIGN KEY     ("parentBlockhashId")
+        REFERENCES      blockhash(id)
+        NOT DEFERRABLE
 ) WITH (oids = false);
-CREATE INDEX "block_parentBlockHashId" ON "public"."block" USING btree ("parentBlockhashId");
-CREATE INDEX "block_sealerAccountId" ON "public"."block" USING btree ("sealerAccountId");
-CREATE INDEX "block_number" ON "public"."block" USING btree ("number");
-CREATE INDEX "block_timestamp" ON "public"."block" USING btree ("timestamp");
+CREATE INDEX            "block_parentBlockHashId"
+    ON                      "public"."block"
+    USING btree             ("parentBlockhashId");
+CREATE INDEX            "block_sealerAccountId"
+    ON                      "public"."block"
+    USING btree             ("sealerAccountId");
+CREATE INDEX            "block_number"
+    ON                      "public"."block"
+    USING btree             ("number");
+CREATE INDEX            "block_timestamp"
+    ON                      "public"."block"
+    USING btree             ("timestamp");
 
 CREATE TABLE "public"."transaction" (
-    "hash" character(66) NOT NULL,
-    "blockId" bigint,
-    "nonce" bigint NOT NULL,
-    "gas" bigint NOT NULL,
-    "gasPrice" bigint NOT NULL,
-    "value" bigint NOT NULL,
-    "fromAccountId" bigint NOT NULL,
-    "toAccountId" bigint NOT NULL,
-    "contractaddressAccountId" bigint,
-    "status" smallint NOT NULL,
-    "gasUsed" bigint NOT NULL,
-    "inputlen" smallint NOT NULL,
-    "input" text NOT NULL,
-    CONSTRAINT "transaction_contractaddressAccountId_fkey" FOREIGN KEY ("contractaddressAccountId") REFERENCES account(id) NOT DEFERRABLE,
-    CONSTRAINT "transaction_fromAccountId_fkey" FOREIGN KEY ("fromAccountId") REFERENCES account(id) NOT DEFERRABLE,
-    CONSTRAINT "transaction_toAccountId_fkey" FOREIGN KEY ("toAccountId") REFERENCES account(id) NOT DEFERRABLE,
-    CONSTRAINT "transaction_blockId_fkey" FOREIGN KEY ("blockId") REFERENCES block(id) NOT DEFERRABLE
+    "hash"              character(66)
+        NOT NULL,
+    "blockId"           bigint,
+    "type"              transactiontype
+        DEFAULT         'unknown'
+        NOT NULL,
+    "nonce"             bigint
+        NOT NULL,
+    "gas"               bigint
+        NOT NULL,
+    "gasPrice"          bigint
+        NOT NULL,
+    "value"             bigint
+        NOT NULL,
+    "fromAccountId"     bigint
+        NOT NULL,
+    "toAccountId"       bigint
+        NOT NULL,
+    "contractaddressAccountId"
+                        bigint,
+    "status"            smallint
+        NOT NULL,
+    "gasUsed"           bigint
+        NOT NULL,
+    "inputlen"          smallint
+        NOT NULL,
+    "input"             text
+        NOT NULL,
+    CONSTRAINT          "transaction_contractaddressAccountId_fkey"
+        FOREIGN KEY         ("contractaddressAccountId")
+        REFERENCES          account(id)
+        NOT DEFERRABLE,
+    CONSTRAINT          "transaction_fromAccountId_fkey"
+        FOREIGN KEY         ("fromAccountId")
+        REFERENCES          account(id)
+        NOT DEFERRABLE,
+    CONSTRAINT          "transaction_toAccountId_fkey"
+        FOREIGN KEY         ("toAccountId")
+        REFERENCES          account(id)
+        NOT DEFERRABLE,
+    CONSTRAINT          "transaction_blockId_fkey"
+        FOREIGN KEY         ("blockId")
+        REFERENCES          block(id)
+        NOT DEFERRABLE
 ) WITH (oids = false);
-CREATE INDEX "transaction_contractaddressAccountId" ON "public"."transaction" USING btree ("contractaddressAccountId");
-CREATE INDEX "transaction_fromAccountId" ON "public"."transaction" USING btree ("fromAccountId");
-CREATE INDEX "transaction_toAccountId" ON "public"."transaction" USING btree ("toAccountId");
-CREATE INDEX "transaction_blockId" ON "public"."transaction" USING btree ("blockId");
+CREATE INDEX            "transaction_contractaddressAccountId"
+    ON                      "public"."transaction"
+    USING btree             ("contractaddressAccountId");
+CREATE INDEX            "transaction_fromAccountId"
+    ON                      "public"."transaction"
+    USING btree             ("fromAccountId");
+CREATE INDEX            "transaction_toAccountId"
+    ON                      "public"."transaction"
+    USING btree             ("toAccountId");
+CREATE INDEX            "transaction_blockId"
+    ON                      "public"."transaction"
+    USING btree             ("blockId");
 
-CREATE VIEW b AS SELECT b1.hash "hash", b2.hash "parentHash", number, account.address sealer, timestamp, difficulty, "gasUsed", "gasLimit", size FROM block, blockhash b1, blockhash b2, account WHERE block.id=b1.id AND block."parentBlockhashId"=b2.id AND block."sealerAccountId"=account.id;
+CREATE VIEW             b
+    AS
+    SELECT              b1.hash "hash",
+                        b2.hash "parentHash",
+                        number,
+                        account.address sealer,
+                        timestamp,
+                        difficulty,
+                        "gasUsed",
+                        "gasLimit",
+                        size
+    FROM                block,
+                        blockhash b1,
+                        blockhash b2,
+                        account
+    WHERE               block.id = b1.id
+    AND                 block."parentBlockhashId"= b2.id
+    AND                 block."sealerAccountId"  = account.id;
+CREATE VIEW             t
+    AS
+    SELECT              transaction.hash "transaction hash",
+                        block.number "block number",
+                        "type",
+                        nonce,
+                        gas,
+                        "gasPrice" "gas price",
+                        value,
+                        a1.address "from",
+                        a2.address "to",
+                        a3.address "contractaddress",
+                        status,
+                        transaction."gasUsed" "gas used",
+                        inputlen
+    FROM                transaction
+    LEFT JOIN           account a3
+    ON                  (transaction."contractaddressAccountId" = a3.id),
+                        block,
+                        account a1,
+                        account a2
+    WHERE               block.id = transaction."blockId"
+    AND                 a1.id    = "fromAccountId"
+    AND                 a2.id    = "toAccountId";
 
 INSERT INTO "account" ("id", "address", "shortname", "name") VALUES
 (1,	'0x377ab0cd00744dbb07b369cd5c0872dcd362c8f0',	'UNER',	'Universidad Nacional de Entre Rios'),
-- 
GitLab